From bfb5dfa709ec55ab0cc57981b7d1504088d1bfac Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期一, 25 四月 2022 18:44:38 +0800 Subject: [PATCH] 应对DDOS攻击IP切换优化 --- src/main/java/com/yeshi/buwan/job/domain/TencentCloudCLBUtil.java | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 269 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/job/domain/TencentCloudCLBUtil.java b/src/main/java/com/yeshi/buwan/job/domain/TencentCloudCLBUtil.java index d6b72e2..e380126 100644 --- a/src/main/java/com/yeshi/buwan/job/domain/TencentCloudCLBUtil.java +++ b/src/main/java/com/yeshi/buwan/job/domain/TencentCloudCLBUtil.java @@ -1,13 +1,15 @@ package com.yeshi.buwan.job.domain; import com.tencentcloudapi.clb.v20180317.ClbClient; -import com.tencentcloudapi.clb.v20180317.models.DescribeLoadBalancersRequest; -import com.tencentcloudapi.clb.v20180317.models.DescribeLoadBalancersResponse; -import com.tencentcloudapi.clb.v20180317.models.LoadBalancer; +import com.tencentcloudapi.clb.v20180317.models.*; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; +import com.yeshi.buwan.util.TimeUtil; + +import java.util.Arrays; +import java.util.List; /** * @author hxh @@ -51,9 +53,271 @@ } + /** + * @return java.lang.String[] + * @author hxh + * @description 娣诲姞璐熻浇鍧囪 + * @date 17:24 2022/4/25 + * @param: name CLB鍚嶇О + **/ + public static String[] addCLB(String name) throws TencentCloudSDKException { + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + CreateLoadBalancerRequest req = new CreateLoadBalancerRequest(); + req.setLoadBalancerType("OPEN"); + req.setLoadBalancerName(name); + // 杩斿洖鐨剅esp鏄竴涓狢reateLoadBalancerResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + CreateLoadBalancerResponse resp = client.CreateLoadBalancer(req); + return resp.getLoadBalancerIds(); + // 杈撳嚭json鏍煎紡鐨勫瓧绗︿覆鍥炲寘 + } + + public static LoadBalancer getCLB(String balanceId) throws TencentCloudSDKException { + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest(); + String[] loadBalancerIds1 = {balanceId}; + req.setLoadBalancerIds(loadBalancerIds1); + + // 杩斿洖鐨剅esp鏄竴涓狣escribeLoadBalancersResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + DescribeLoadBalancersResponse resp = client.DescribeLoadBalancers(req); + return resp.getLoadBalancerSet()[0]; + } + + + public static void deleteCLB(String balanceId) throws TencentCloudSDKException { + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + DeleteLoadBalancerRequest req = new DeleteLoadBalancerRequest(); + String[] loadBalancerIds1 = {balanceId}; + req.setLoadBalancerIds(loadBalancerIds1); + + // 杩斿洖鐨剅esp鏄竴涓狣eleteLoadBalancerResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + DeleteLoadBalancerResponse resp = client.DeleteLoadBalancer(req); + // 杈撳嚭json鏍煎紡鐨勫瓧绗︿覆鍥炲寘 + } + + /** + * @return java.lang.String[] + * @author hxh + * @description 娣诲姞鐩戝惉鍣� + * @date 17:26 2022/4/25 + * @param: balanceId + * @param: port + **/ + public static String[] addListener(String balanceId, Long port) throws TencentCloudSDKException { + // 瀹炰緥鍖栦竴涓璇佸璞★紝鍏ュ弬闇�瑕佷紶鍏ヨ吘璁簯璐︽埛secretId锛宻ecretKey,姝ゅ杩橀渶娉ㄦ剰瀵嗛挜瀵圭殑淇濆瘑 + // 瀵嗛挜鍙墠寰�https://console.cloud.tencent.com/cam/capi缃戠珯杩涜鑾峰彇 + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + CreateListenerRequest req = new CreateListenerRequest(); + req.setLoadBalancerId(balanceId); + + Long[] ports1 = {port}; + req.setPorts(ports1); + req.setProtocol("TCP"); + // 杩斿洖鐨剅esp鏄竴涓狢reateListenerResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + CreateListenerResponse resp = client.CreateListener(req); + return resp.getListenerIds(); + } + + + /** + * @return com.tencentcloudapi.clb.v20180317.models.Listener[] + * @author hxh + * @description 鑾峰彇璐熻浇鍧囪 鐨勭洃鍚櫒鎺ュ彛 + * @date 17:10 2022/4/25 + * @param: balanceId + **/ + public static Listener[] listListner(String balanceId) throws TencentCloudSDKException { + + // 瀹炰緥鍖栦竴涓璇佸璞★紝鍏ュ弬闇�瑕佷紶鍏ヨ吘璁簯璐︽埛secretId锛宻ecretKey,姝ゅ杩橀渶娉ㄦ剰瀵嗛挜瀵圭殑淇濆瘑 + // 瀵嗛挜鍙墠寰�https://console.cloud.tencent.com/cam/capi缃戠珯杩涜鑾峰彇 + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + DescribeListenersRequest req = new DescribeListenersRequest(); + req.setLoadBalancerId(balanceId); + // 杩斿洖鐨剅esp鏄竴涓狣escribeListenersResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + DescribeListenersResponse resp = client.DescribeListeners(req); + + return resp.getListeners(); + + } + + + /** + * @return void + * @author hxh + * @description 缁戝畾璐熻浇鍧囪 + * @date 17:11 2022/4/25 + * @param: balancerId + * @param: listenerId + * @param: servers + **/ + public static void bindServer(String balancerId, String listenerId, List<BindServerInfo> servers) throws TencentCloudSDKException { + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 瀹炰緥鍖栦竴涓猦ttp閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("clb.tencentcloudapi.com"); + // 瀹炰緥鍖栦竴涓猚lient閫夐」锛屽彲閫夌殑锛屾病鏈夌壒娈婇渶姹傚彲浠ヨ烦杩� + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 瀹炰緥鍖栬璇锋眰浜у搧鐨刢lient瀵硅薄,clientProfile鏄彲閫夌殑 + ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile); + // 瀹炰緥鍖栦竴涓姹傚璞�,姣忎釜鎺ュ彛閮戒細瀵瑰簲涓�涓猺equest瀵硅薄 + RegisterTargetsRequest req = new RegisterTargetsRequest(); + req.setLoadBalancerId(balancerId); + req.setListenerId(listenerId); + + Target[] targets1 = new Target[servers.size()]; + for (int i = 0; i < servers.size(); i++) { + Target target = new Target(); + target.setType("CVM"); + target.setInstanceId(servers.get(i).getCvmInstanceId()); + target.setPort(servers.get(i).getPort()); + target.setWeight(servers.get(i).getWeight()); + targets1[i] = target; + } + + req.setTargets(targets1); + + // 杩斿洖鐨剅esp鏄竴涓猂egisterTargetsResponse鐨勫疄渚嬶紝涓庤姹傚璞″搴� + RegisterTargetsResponse resp = client.RegisterTargets(req); + // 杈撳嚭json鏍煎紡鐨勫瓧绗︿覆鍥炲寘 + System.out.println(RegisterTargetsResponse.toJsonString(resp)); + } + + public static void createBuWanCLB(int count) throws TencentCloudSDKException { + String[] clbs = addCLB("甯冧父-澶囩敤" + count + "-" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMddHHmmss")); + if (clbs.length > 0) { + String balancerId = clbs[0]; + //鏌ヨ鏄惁姝e父 + for (int i = 0; i < 5; i++) { + LoadBalancer balancer = getCLB(balancerId); + if (balancer.getStatus() == 1) { + break; + } + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + try { + String[] listeners = addListener(balancerId, 8089L); + if (listeners.length > 0) { + for (int j = 0; j < 5; j++) { + try { + bindServer(balancerId, listeners[0], Arrays.asList(new BindServerInfo[]{ + new BindServerInfo("ins-j7d29j8o", 8089L, 10L), + new BindServerInfo("ins-j7d29j8o", 8086L, 1L), + })); + break; + } catch (Exception e) { + Thread.sleep(1000); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + for (int i = 0; i < 5; i++) { + try { + deleteCLB(balancerId); + break; + } catch (Exception e1) { + e1.printStackTrace(); + try { + Thread.sleep(2000); + } catch (InterruptedException e2) { + e2.printStackTrace(); + } + } + } + } + + } + } + + public static void main(String[] args) throws TencentCloudSDKException { - LoadBalancer[] bs = TencentCloudCLBUtil.getCLBList("甯冧父-澶囩敤"); - System.out.println(bs); + deleteCLB("lb-rmp1hyzm"); + } + + static class BindServerInfo { + //ins-j7d29j8o + private String cvmInstanceId; + private long port; + private long weight; + + public BindServerInfo(String cvmInstanceId, long port, long weight) { + this.cvmInstanceId = cvmInstanceId; + this.port = port; + this.weight = weight; + } + + public String getCvmInstanceId() { + return cvmInstanceId; + } + + public void setCvmInstanceId(String cvmInstanceId) { + this.cvmInstanceId = cvmInstanceId; + } + + public long getPort() { + return port; + } + + public void setPort(long port) { + this.port = port; + } + + public long getWeight() { + return weight; + } + + public void setWeight(long weight) { + this.weight = weight; + } } -- Gitblit v1.8.0