2021-08-18 19:15:49 +08:00
|
|
|
|
package com.zdjizhi.etl;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JavaType;
|
|
|
|
|
|
import com.zdjizhi.common.CommonConfig;
|
|
|
|
|
|
import com.zdjizhi.common.DosDetectionThreshold;
|
|
|
|
|
|
import com.zdjizhi.utils.HttpClientUtils;
|
|
|
|
|
|
import com.zdjizhi.utils.JsonMapper;
|
2021-08-20 11:52:20 +08:00
|
|
|
|
import inet.ipaddr.IPAddress;
|
|
|
|
|
|
import inet.ipaddr.IPAddressString;
|
|
|
|
|
|
import org.apache.flink.shaded.guava18.com.google.common.collect.Range;
|
|
|
|
|
|
import org.apache.flink.shaded.guava18.com.google.common.collect.TreeRangeMap;
|
2021-08-18 19:15:49 +08:00
|
|
|
|
import org.apache.http.client.utils.URIBuilder;
|
|
|
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.HashMap;
|
2021-08-20 11:52:20 +08:00
|
|
|
|
import java.util.Map;
|
2021-08-18 19:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author wlh
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class ParseStaticThreshold {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ParseStaticThreshold.class);
|
|
|
|
|
|
private static String encryptpwd;
|
|
|
|
|
|
|
|
|
|
|
|
private static JsonMapper jsonMapperInstance = JsonMapper.getInstance();
|
|
|
|
|
|
private static JavaType hashmapJsonType = jsonMapperInstance.createCollectionType(HashMap.class, String.class, Object.class);
|
|
|
|
|
|
private static JavaType thresholdType = jsonMapperInstance.createCollectionType(ArrayList.class, DosDetectionThreshold.class);
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
//加载加密登录密码
|
|
|
|
|
|
encryptpwd = getEncryptpwd();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取加密密码
|
|
|
|
|
|
*/
|
2021-08-24 16:35:31 +08:00
|
|
|
|
private static String getEncryptpwd() {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
String psw = HttpClientUtils.ERROR_MESSAGE;
|
|
|
|
|
|
try {
|
|
|
|
|
|
URIBuilder uriBuilder = new URIBuilder(CommonConfig.BIFANG_SERVER_URI);
|
2021-09-14 18:46:23 +08:00
|
|
|
|
HashMap<String, Object> parms = new HashMap<>();
|
2021-09-15 10:08:17 +08:00
|
|
|
|
parms.put("password", "admin");
|
2021-08-24 16:35:31 +08:00
|
|
|
|
HttpClientUtils.setUrlWithParams(uriBuilder, CommonConfig.BIFANG_SERVER_ENCRYPTPWD_PATH, parms);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
String resposeJsonStr = HttpClientUtils.httpGet(uriBuilder.build());
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> resposeMap = jsonMapperInstance.fromJson(resposeJsonStr, hashmapJsonType);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
boolean success = (boolean) resposeMap.get("success");
|
|
|
|
|
|
String msg = resposeMap.get("msg").toString();
|
|
|
|
|
|
if (success) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> data = jsonMapperInstance.fromJson(jsonMapperInstance.toJson(resposeMap.get("data")), hashmapJsonType);
|
|
|
|
|
|
psw = data.get("encryptpwd").toString();
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
logger.error(msg);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
|
logger.error("构造URI异常", e);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("获取encryptpwd失败", e);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
return psw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 登录bifang服务,获取token
|
2021-08-24 16:35:31 +08:00
|
|
|
|
*
|
2021-08-18 19:15:49 +08:00
|
|
|
|
* @return token
|
|
|
|
|
|
*/
|
2021-08-24 16:35:31 +08:00
|
|
|
|
private static String loginBifangServer() {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
String token = HttpClientUtils.ERROR_MESSAGE;
|
|
|
|
|
|
try {
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (!HttpClientUtils.ERROR_MESSAGE.equals(encryptpwd)) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
URIBuilder uriBuilder = new URIBuilder(CommonConfig.BIFANG_SERVER_URI);
|
2021-09-14 18:46:23 +08:00
|
|
|
|
HashMap<String, Object> parms = new HashMap<>();
|
2021-09-15 10:08:17 +08:00
|
|
|
|
parms.put("username", "admin");
|
2021-08-24 16:35:31 +08:00
|
|
|
|
parms.put("password", encryptpwd);
|
|
|
|
|
|
HttpClientUtils.setUrlWithParams(uriBuilder, CommonConfig.BIFANG_SERVER_LOGIN_PATH, parms);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
String resposeJsonStr = HttpClientUtils.httpPost(uriBuilder.build(), null);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> resposeMap = jsonMapperInstance.fromJson(resposeJsonStr, hashmapJsonType);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
boolean success = (boolean) resposeMap.get("success");
|
|
|
|
|
|
String msg = resposeMap.get("msg").toString();
|
|
|
|
|
|
if (success) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> data = jsonMapperInstance.fromJson(jsonMapperInstance.toJson(resposeMap.get("data")), hashmapJsonType);
|
|
|
|
|
|
token = data.get("token").toString();
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
logger.error(msg);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("登录失败,未获取到token ", e);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
return token;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取静态阈值配置列表
|
2021-08-24 16:35:31 +08:00
|
|
|
|
*
|
2021-08-18 19:15:49 +08:00
|
|
|
|
* @return thresholds
|
|
|
|
|
|
*/
|
2021-08-24 16:35:31 +08:00
|
|
|
|
private static ArrayList<DosDetectionThreshold> getDosDetectionThreshold() {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
ArrayList<DosDetectionThreshold> thresholds = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
URIBuilder uriBuilder = new URIBuilder(CommonConfig.BIFANG_SERVER_URI);
|
2021-09-14 18:46:23 +08:00
|
|
|
|
HashMap<String, Object> parms = new HashMap<>();
|
|
|
|
|
|
parms.put("pageSize",-1);
|
|
|
|
|
|
HttpClientUtils.setUrlWithParams(uriBuilder, CommonConfig.BIFANG_SERVER_POLICY_THRESHOLD_PATH, parms);
|
2021-09-15 10:08:17 +08:00
|
|
|
|
String token = CommonConfig.BIFANG_SERVER_TOKEN;
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (!HttpClientUtils.ERROR_MESSAGE.equals(token)) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
BasicHeader authorization = new BasicHeader("Authorization", token);
|
|
|
|
|
|
String resposeJsonStr = HttpClientUtils.httpGet(uriBuilder.build(), authorization);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (!HttpClientUtils.ERROR_MESSAGE.equals(resposeJsonStr)) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> resposeMap = jsonMapperInstance.fromJson(resposeJsonStr, hashmapJsonType);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
boolean success = (boolean) resposeMap.get("success");
|
|
|
|
|
|
String msg = resposeMap.get("msg").toString();
|
|
|
|
|
|
if (success) {
|
2021-08-18 19:15:49 +08:00
|
|
|
|
HashMap<String, Object> data = jsonMapperInstance.fromJson(jsonMapperInstance.toJson(resposeMap.get("data")), hashmapJsonType);
|
|
|
|
|
|
thresholds = jsonMapperInstance.fromJson(jsonMapperInstance.toJson(data.get("list")), thresholdType);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
logger.info("获取到静态阈值配置{}条", thresholds.size());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logger.error(msg);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("获取静态阈值配置失败,请检查bifang服务或登录配置信息 ", e);
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
return thresholds;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-20 11:52:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 基于静态阈值构建threshold RangeMap,k:IP段或具体IP,v:配置信息
|
2021-08-24 16:35:31 +08:00
|
|
|
|
*
|
2021-08-20 11:52:20 +08:00
|
|
|
|
* @return threshold RangeMap
|
|
|
|
|
|
*/
|
2021-08-24 16:35:31 +08:00
|
|
|
|
static TreeRangeMap<IPAddress, Map<String, DosDetectionThreshold>> createStaticThreshold() {
|
|
|
|
|
|
TreeRangeMap<IPAddress, Map<String, DosDetectionThreshold>> thresholdRangeMap = TreeRangeMap.create();
|
2021-08-20 11:52:20 +08:00
|
|
|
|
try {
|
|
|
|
|
|
ArrayList<DosDetectionThreshold> dosDetectionThreshold = getDosDetectionThreshold();
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (dosDetectionThreshold != null && !dosDetectionThreshold.isEmpty()) {
|
|
|
|
|
|
for (DosDetectionThreshold threshold : dosDetectionThreshold) {
|
2021-08-20 11:52:20 +08:00
|
|
|
|
ArrayList<String> serverIpList = threshold.getServerIpList();
|
2021-08-24 16:35:31 +08:00
|
|
|
|
for (String sip : serverIpList) {
|
2021-08-20 11:52:20 +08:00
|
|
|
|
IPAddressString ipAddressString = new IPAddressString(sip);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (ipAddressString.isIPAddress()) {
|
2021-08-20 11:52:20 +08:00
|
|
|
|
IPAddress address = ipAddressString.getAddress();
|
2021-08-20 18:34:40 +08:00
|
|
|
|
Map<String, DosDetectionThreshold> floodTypeThresholdMap = thresholdRangeMap.get(address);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
if (floodTypeThresholdMap == null) {
|
2021-08-20 18:34:40 +08:00
|
|
|
|
floodTypeThresholdMap = new HashMap<>();
|
|
|
|
|
|
}
|
2021-08-24 16:35:31 +08:00
|
|
|
|
floodTypeThresholdMap.put(threshold.getAttackType(), threshold);
|
2021-09-14 18:46:23 +08:00
|
|
|
|
if (address.isPrefixed()){
|
|
|
|
|
|
if (address.isMultiple()){
|
|
|
|
|
|
thresholdRangeMap.put(Range.closed(address.getLower(), address.getUpper()), floodTypeThresholdMap);
|
|
|
|
|
|
}else {
|
|
|
|
|
|
thresholdRangeMap.put(Range.closed(address.adjustPrefixLength(address.getBitCount()),
|
|
|
|
|
|
address.toMaxHost().withoutPrefixLength()), floodTypeThresholdMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
|
|
|
thresholdRangeMap.put(Range.closed(address, address), floodTypeThresholdMap);
|
|
|
|
|
|
}
|
2021-08-20 11:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-24 16:35:31 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("构建threshold RangeMap失败", e);
|
2021-08-20 11:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
return thresholdRangeMap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-18 19:15:49 +08:00
|
|
|
|
public static void main(String[] args) {
|
2021-09-14 18:46:23 +08:00
|
|
|
|
|
|
|
|
|
|
ArrayList<DosDetectionThreshold> dosDetectionThreshold = getDosDetectionThreshold();
|
|
|
|
|
|
dosDetectionThreshold.forEach(System.out::println);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("------------------------");
|
2021-08-20 18:34:40 +08:00
|
|
|
|
TreeRangeMap<IPAddress, Map<String, DosDetectionThreshold>> staticThreshold = createStaticThreshold();
|
2021-09-14 18:46:23 +08:00
|
|
|
|
|
|
|
|
|
|
/*
|
2021-08-20 18:34:40 +08:00
|
|
|
|
Map<Range<IPAddress>, Map<String, DosDetectionThreshold>> rangeMapMap = staticThreshold.asMapOfRanges();
|
2021-08-24 16:35:31 +08:00
|
|
|
|
for (Range<IPAddress> range : rangeMapMap.keySet()) {
|
2021-08-20 18:34:40 +08:00
|
|
|
|
Map<String, DosDetectionThreshold> thresholdMap = rangeMapMap.get(range);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
for (String type : thresholdMap.keySet()) {
|
2021-08-20 18:34:40 +08:00
|
|
|
|
DosDetectionThreshold threshold = thresholdMap.get(type);
|
2021-08-24 16:35:31 +08:00
|
|
|
|
System.out.println(range + "---" + type + "---" + threshold);
|
2021-08-20 18:34:40 +08:00
|
|
|
|
}
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
2021-09-14 18:46:23 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
2021-08-18 19:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|