fix:Adapt to CM interface(TSG-18052)

This commit is contained in:
wangchengcheng
2023-12-21 18:25:54 +08:00
parent bdfe5f73db
commit 322bb1e4cb
4 changed files with 91 additions and 33 deletions

View File

@@ -20,7 +20,12 @@ import org.apache.flink.shaded.guava18.com.google.common.collect.TreeRangeMap;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -81,7 +86,7 @@ public class ParseStaticThreshold {
URIBuilder uriBuilder = new URIBuilder(FlowWriteConfig.BIFANG_SERVER_URI); URIBuilder uriBuilder = new URIBuilder(FlowWriteConfig.BIFANG_SERVER_URI);
HashMap<String, Object> parms = new HashMap<>(); HashMap<String, Object> parms = new HashMap<>();
parms.put("page_size", -1); parms.put("page_size", -1);
// parms.put("orderBy", "vsysId desc"); // parms.put("orderBy", "vsysId desc");
parms.put("type", 1); parms.put("type", 1);
HttpClientUtils.setUrlWithParams(uriBuilder, FlowWriteConfig.BIFANG_SERVER_POLICY_VSYSID_PATH, parms); HttpClientUtils.setUrlWithParams(uriBuilder, FlowWriteConfig.BIFANG_SERVER_POLICY_VSYSID_PATH, parms);
String token = NacosUtils.getStringProperty("bifang.server.token"); String token = NacosUtils.getStringProperty("bifang.server.token");
@@ -95,7 +100,7 @@ public class ParseStaticThreshold {
String msg = resposeMap.get("msg").toString(); String msg = resposeMap.get("msg").toString();
if (success) { if (success) {
HashMap<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(resposeMap.get("data")), HashMap.class); HashMap<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(resposeMap.get("data")), HashMap.class);
Object list = data.get("list"); Object list = data.get("vsys_list");
if (list != null) { if (list != null) {
List<DosVsysId> dosVsysIds = JSON.parseArray(JSONObject.toJSONString(list), DosVsysId.class); List<DosVsysId> dosVsysIds = JSON.parseArray(JSONObject.toJSONString(list), DosVsysId.class);
vsysIdList= (ArrayList)dosVsysIds; vsysIdList= (ArrayList)dosVsysIds;
@@ -116,7 +121,6 @@ public class ParseStaticThreshold {
/** /**
* 根据vsysId获取静态阈值配置列表 * 根据vsysId获取静态阈值配置列表
*
* @return thresholds * @return thresholds
*/ */
private static ArrayList<DosDetectionThreshold> getDosDetectionThreshold() { private static ArrayList<DosDetectionThreshold> getDosDetectionThreshold() {
@@ -130,7 +134,7 @@ public class ParseStaticThreshold {
URIBuilder uriBuilder = new URIBuilder(FlowWriteConfig.BIFANG_SERVER_URI); URIBuilder uriBuilder = new URIBuilder(FlowWriteConfig.BIFANG_SERVER_URI);
HashMap<String, Object> parms = new HashMap<>(); HashMap<String, Object> parms = new HashMap<>();
parms.put("page_size", -1); parms.put("page_size", -1);
parms.put("order_by", "profileId asc"); // parms.put("order_by", "profileId asc");
parms.put("is_valid", 1); parms.put("is_valid", 1);
parms.put("vsys_id", vsysId); parms.put("vsys_id", vsysId);
HttpClientUtils.setUrlWithParams(uriBuilder, FlowWriteConfig.BIFANG_SERVER_POLICY_THRESHOLD_PATH, parms); HttpClientUtils.setUrlWithParams(uriBuilder, FlowWriteConfig.BIFANG_SERVER_POLICY_THRESHOLD_PATH, parms);
@@ -140,20 +144,14 @@ public class ParseStaticThreshold {
BasicHeader authorization1 = new BasicHeader("Content-Type", "application/x-www-form-urlencoded"); BasicHeader authorization1 = new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
String resposeJsonStr = HttpClientUtils.httpGet(uriBuilder.build(), authorization, authorization1); String resposeJsonStr = HttpClientUtils.httpGet(uriBuilder.build(), authorization, authorization1);
if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) { if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) {
HashMap<String, Object> resposeMap = JSONObject.parseObject(resposeJsonStr,HashMap.class); HashMap<String, Object> resposeMap = JSONObject.parseObject(resposeJsonStr,HashMap.class);
boolean success = (boolean) resposeMap.get("success"); boolean success = (boolean) resposeMap.get("success");
String msg = resposeMap.get("msg").toString(); String msg = resposeMap.get("msg").toString();
if (success) { if (success) {
HashMap<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(resposeMap.get("data")), HashMap.class); HashMap<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(resposeMap.get("data")), HashMap.class);
Object list = data.get("list"); Object list = data.get("dos_detections");
if (list != null) { if (list != null) {
System.out.println(list);
List<DosDetectionThreshold> dosDetectionThresholds = JSON.parseArray(JSONObject.toJSONString(list), DosDetectionThreshold.class); List<DosDetectionThreshold> dosDetectionThresholds = JSON.parseArray(JSONObject.toJSONString(list), DosDetectionThreshold.class);
System.out.println(dosDetectionThresholds);
ArrayList<DosDetectionThreshold> thresholds = (ArrayList)dosDetectionThresholds; ArrayList<DosDetectionThreshold> thresholds = (ArrayList)dosDetectionThresholds;
for (DosDetectionThreshold dosDetectionThreshold : thresholds) { for (DosDetectionThreshold dosDetectionThreshold : thresholds) {
dosDetectionThreshold.setSuperior_ids(superiorIds); dosDetectionThreshold.setSuperior_ids(superiorIds);
@@ -185,7 +183,6 @@ public class ParseStaticThreshold {
HashMap<Integer, HashMap<String, TreeRangeMap<IPAddress, DosDetectionThreshold>>> thresholdRangeMap = new HashMap<>(4); HashMap<Integer, HashMap<String, TreeRangeMap<IPAddress, DosDetectionThreshold>>> thresholdRangeMap = new HashMap<>(4);
try { try {
ArrayList<DosDetectionThreshold> dosDetectionThreshold = getDosDetectionThreshold(); ArrayList<DosDetectionThreshold> dosDetectionThreshold = getDosDetectionThreshold();
if (dosDetectionThreshold != null && !dosDetectionThreshold.isEmpty()) { if (dosDetectionThreshold != null && !dosDetectionThreshold.isEmpty()) {
for (DosDetectionThreshold threshold : dosDetectionThreshold) { for (DosDetectionThreshold threshold : dosDetectionThreshold) {
@@ -249,28 +246,41 @@ public class ParseStaticThreshold {
private static String loginBifangServer() { private static String loginBifangServer() {
String token = HttpClientUtils.ERROR_MESSAGE; String token = HttpClientUtils.ERROR_MESSAGE;
try { try {
if (!HttpClientUtils.ERROR_MESSAGE.equals(encryptpwd)) { final HashMap<String, Object> parmsMap = new HashMap<>();
URIBuilder uriBuilder = new URIBuilder(FlowWriteConfig.BIFANG_SERVER_URI); String urlString = FlowWriteConfig.BIFANG_SERVER_URI+FlowWriteConfig.BIFANG_SERVER_LOGIN_PATH;
HashMap<String, Object> parms = new HashMap<>(); parmsMap.put("username","admin");
parms.put("username", "admin"); parmsMap.put("password",encryptpwd);
parms.put("password", encryptpwd); parmsMap.put("auth_mode","");
HttpClientUtils.setUrlWithParams(uriBuilder, FlowWriteConfig.BIFANG_SERVER_LOGIN_PATH, parms); final String jsonInputString = JSON.toJSONString(parmsMap);
String resposeJsonStr = HttpClientUtils.httpPost(uriBuilder.build(), null); final URL url = new URL(urlString);
if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) { final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
HashMap<String, Object> resposeMap = JSONObject.parseObject(resposeJsonStr, HashMap.class); connection.setRequestMethod("POST");
boolean success = (boolean) resposeMap.get("success"); connection.setRequestProperty("Content-Type", "application/json");
String msg = resposeMap.get("msg").toString(); connection.setRequestProperty("Accept", "application/json");
if (success) { connection.setDoOutput(true);
HashMap<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(resposeMap.get("data")), HashMap.class); OutputStream os = connection.getOutputStream();
token = data.get("token").toString(); os.write(jsonInputString.getBytes());
} else { os.flush();
logger.error(msg); os.close();
} int responseCode = connection.getResponseCode();
if (responseCode == 200 ) {
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
} }
reader.close();
HashMap<String, Object> body = JSONObject.parseObject(String.valueOf(response), HashMap.class);
final HashMap data = JSONObject.parseObject(String.valueOf( body.get("data")), HashMap.class);
token = (String) data.get("token");
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("登录失败,未获取到token ", e); logger.error("登录失败,未获取到token ", e);
} }
return token; return token;
} }
} }

View File

@@ -2,7 +2,6 @@ package com.zdjizhi.sink;
import cn.hutool.log.Log; import cn.hutool.log.Log;
import cn.hutool.log.LogFactory; import cn.hutool.log.LogFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.zdjizhi.common.FlowWriteConfig; import com.zdjizhi.common.FlowWriteConfig;
import com.zdjizhi.common.DosEventLog; import com.zdjizhi.common.DosEventLog;
import com.zdjizhi.common.DosMetricsLog; import com.zdjizhi.common.DosMetricsLog;
@@ -18,7 +17,6 @@ import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindo
import org.apache.flink.streaming.api.windowing.time.Time; import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag; import org.apache.flink.util.OutputTag;
import java.util.Properties;
/** /**
* @author 94976 * @author 94976

View File

@@ -76,7 +76,7 @@ data.center.id.num=15
#bifang服务访问地址 #bifang服务访问地址
bifang.server.uri=http://192.168.44.72:80 bifang.server.uri=http://192.168.44.72
#bifang.server.uri=http://192.168.44.3:80 #bifang.server.uri=http://192.168.44.3:80
#加密密码路径信息 #加密密码路径信息
@@ -89,7 +89,7 @@ bifang.server.login.path=/v1/user/login
bifang.server.policy.vaysid.path=/v1/admin/vsys bifang.server.policy.vaysid.path=/v1/admin/vsys
#获取静态阈值路径信息 #获取静态阈值路径信息
bifang.server.policy.threshold.path=/v1/policy/profile/dos/detection/threshold bifang.server.policy.threshold.path=/v1/policy/profile/dos_detection
#http请求相关参数 #http请求相关参数
#最大连接数 #最大连接数

View File

@@ -0,0 +1,50 @@
package com.zdjizhi.Http;
import com.alibaba.fastjson2.JSON;
import com.zdjizhi.common.FlowWriteConfig;
import com.zdjizhi.utils.HttpClientUtils;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
public class HttpTest {
public static void main(String[] args) {
String token = HttpClientUtils.ERROR_MESSAGE;
try {
String urlString = FlowWriteConfig.BIFANG_SERVER_URI+"/v1/user/encryptpwd";
final HashMap<String, Object> parmsMap = new HashMap<>();
parmsMap.put("username","admin");
final String jsonInputString = JSON.toJSONString(parmsMap);
System.out.println("URL:"+urlString);
System.out.println("parmsString:"+jsonInputString);
final URL url = new URL(urlString);
final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
os.write(jsonInputString.getBytes());
os.flush();
os.close();
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
} catch (Exception e) {
System.out.println("失败");
}
}
}