| | |
| | | |
| | | import com.aliyun.alidns20150109.models.DescribeSubDomainRecordsResponseBody; |
| | | import com.tencentcloudapi.clb.v20180317.models.LoadBalancer; |
| | | import com.tencentcloudapi.common.exception.TencentCloudSDKException; |
| | | import com.xxl.job.core.biz.model.ReturnT; |
| | | import com.xxl.job.core.handler.annotation.XxlJob; |
| | | import com.yeshi.buwan.domain.live.TVLiveChannel; |
| | |
| | | @Component |
| | | public class DDOSJob { |
| | | |
| | | private String getBindIP() throws Exception { |
| | | DescribeSubDomainRecordsResponseBody.DescribeSubDomainRecordsResponseBodyDomainRecordsRecord record = AliyunDomainParseUtil.getIP("api.ysdq.yeshitv.com"); |
| | | String ip = record.getValue().trim(); |
| | | return ip; |
| | | } |
| | | |
| | | //切换被腾讯云封堵的IP |
| | | @XxlJob("ddos-buwan-server-change") |
| | | public ReturnT<String> buwanServerChange(String params) throws Exception { |
| | |
| | | |
| | | List<String> blockIPS = new ArrayList<>(); |
| | | List<String> normalIPS = new ArrayList<>(); |
| | | List<LoadBalancer> normals = new ArrayList<>(); |
| | | for (LoadBalancer b : balances) { |
| | | if (b.getIsBlock()) { |
| | | blockIPS.add(b.getLoadBalancerVips()[0].trim()); |
| | | } else { |
| | | normalIPS.add(b.getLoadBalancerVips()[0].trim()); |
| | | normals.add(b); |
| | | } |
| | | } |
| | | |
| | | //无封堵 |
| | | if (blockIPS.size() == 0) { |
| | | //在没有封堵的情况下删除多余的负载均衡 |
| | | if (normalIPS.size() > 2) { |
| | | deleteMore(normals, getBindIP()); |
| | | } |
| | | return ReturnT.SUCCESS; |
| | | } |
| | | |
| | | //删除多余的负载均衡 |
| | | if (normalIPS.size() > 2) { |
| | | deleteMore(normals, getBindIP()); |
| | | } |
| | | |
| | | |
| | | //获取阿里云解析的域名 |
| | | DescribeSubDomainRecordsResponseBody.DescribeSubDomainRecordsResponseBodyDomainRecordsRecord record = AliyunDomainParseUtil.getIP("api.ysdq.yeshitv.com"); |
| | |
| | | throw new Exception("腾讯云负载均衡IP已全部被封堵,请增加IP"); |
| | | } |
| | | AliyunDomainParseUtil.setIP(record, normalIPS.get(normalIPS.size() - 1)); |
| | | |
| | | //每次切换IP后需要判断是否需要增加IP,用于创建新的负载均衡 |
| | | autoAdd(normalIPS.size(), balances.length); |
| | | |
| | | return ReturnT.SUCCESS; |
| | | } |
| | | |
| | | // public static void main(String[] args){ |
| | | // try { |
| | | // new DDOSJob().buwanServerChange(""); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | |
| | | private void autoAdd(int normalIPCount, int totalCount) throws TencentCloudSDKException { |
| | | if (normalIPCount < 2) { |
| | | //始终保持2个正常IP |
| | | TencentCloudCLBUtil.createBuWanCLB(totalCount); |
| | | } |
| | | } |
| | | |
| | | //删除多余的 |
| | | private void deleteMore(List<LoadBalancer> normals, String bindIP) throws TencentCloudSDKException { |
| | | if (StringUtil.isNullOrEmpty(bindIP)) { |
| | | return; |
| | | } |
| | | |
| | | if (normals.size() > 2) { |
| | | //始终保持2个正常IP |
| | | int deleteCount = normals.size() - 2; |
| | | List<String> bids = new ArrayList<>(); |
| | | for (LoadBalancer balancer : normals) { |
| | | //没有绑定,并且还能删除 |
| | | if (!balancer.getLoadBalancerVips()[0].trim().equalsIgnoreCase(bindIP)) { |
| | | if (bids.size() < deleteCount) { |
| | | bids.add(balancer.getLoadBalancerId()); |
| | | } |
| | | } |
| | | } |
| | | if (bids.size() > 0) { |
| | | for (String bid : bids) { |
| | | TencentCloudCLBUtil.deleteCLB(bid); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args){ |
| | | try { |
| | | new DDOSJob().buwanServerChange(""); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |