From 28e72756746e2ad4826611fae00736bd141f9ce1 Mon Sep 17 00:00:00 2001
From: wanglihui <949764788@qq.com>
Date: Fri, 20 Aug 2021 11:52:20 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ErangeMap=E5=AD=98=E5=82=A8?=
=?UTF-8?q?=E5=AF=B9=E5=BA=94IP=E6=AE=B5=E9=85=8D=E7=BD=AE=E4=BF=A1?=
=?UTF-8?q?=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 6 ++
.../com/zdjizhi/etl/ParseStaticThreshold.java | 49 +++++++++--
src/test/java/com/zdjizhi/common/IpTest.java | 84 +++++++++++++++++++
3 files changed, 133 insertions(+), 6 deletions(-)
create mode 100644 src/test/java/com/zdjizhi/common/IpTest.java
diff --git a/pom.xml b/pom.xml
index 9bf8d43..0c4e1fd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -215,6 +215,12 @@
5.5.2
+
+ com.github.seancfoley
+ ipaddress
+ 5.3.3
+
+
com.zdjizhi
galaxy
diff --git a/src/main/java/com/zdjizhi/etl/ParseStaticThreshold.java b/src/main/java/com/zdjizhi/etl/ParseStaticThreshold.java
index 2b3daf3..40559ed 100644
--- a/src/main/java/com/zdjizhi/etl/ParseStaticThreshold.java
+++ b/src/main/java/com/zdjizhi/etl/ParseStaticThreshold.java
@@ -5,6 +5,10 @@ import com.zdjizhi.common.CommonConfig;
import com.zdjizhi.common.DosDetectionThreshold;
import com.zdjizhi.utils.HttpClientUtils;
import com.zdjizhi.utils.JsonMapper;
+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;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
@@ -13,6 +17,8 @@ import org.slf4j.LoggerFactory;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
/**
* @author wlh
@@ -81,7 +87,7 @@ public class ParseStaticThreshold {
}
}
}catch (Exception e){
- logger.error("登录失败,未获取到token",e);
+ logger.error("登录失败,未获取到token ",e);
}
return token;
}
@@ -110,16 +116,47 @@ public class ParseStaticThreshold {
}
}
}catch (Exception e){
- logger.error("获取静态阈值配置失败",e);
+ logger.error("获取静态阈值配置失败,请检查bifang服务或登录配置信息 ",e);
}
return thresholds;
}
- public static void main(String[] args) {
- ArrayList dosDetectionThreshold = getDosDetectionThreshold();
- if (dosDetectionThreshold != null && !dosDetectionThreshold.isEmpty()){
- dosDetectionThreshold.forEach(s -> System.out.println(s.toString()));
+ /**
+ * 基于静态阈值构建threshold RangeMap,k:IP段或具体IP,v:配置信息
+ * @return threshold RangeMap
+ */
+ public static TreeRangeMap createStaticThreshold(){
+ TreeRangeMap thresholdRangeMap = null;
+ try {
+ ArrayList dosDetectionThreshold = getDosDetectionThreshold();
+ if (dosDetectionThreshold != null && !dosDetectionThreshold.isEmpty()){
+ thresholdRangeMap = TreeRangeMap.create();
+ for (DosDetectionThreshold threshold:dosDetectionThreshold){
+ ArrayList serverIpList = threshold.getServerIpList();
+ for (String sip:serverIpList){
+ IPAddressString ipAddressString = new IPAddressString(sip);
+ if (ipAddressString.isIPAddress()){
+ IPAddress address = ipAddressString.getAddress();
+ thresholdRangeMap.put(Range.closed(address.getLower(),address.getUpper()),threshold);
+ }
+ }
+ }
+ }
+ }catch (Exception e){
+ logger.error("构建threshold RangeMap失败",e);
}
+ return thresholdRangeMap;
+ }
+
+ public static void main(String[] args) {
+
+ TreeRangeMap staticThreshold = createStaticThreshold();
+ Map, DosDetectionThreshold> rangeDosDetectionThresholdMap = staticThreshold.asMapOfRanges();
+ Set> ranges = rangeDosDetectionThresholdMap.keySet();
+ for (Range range:ranges){
+ System.out.println(range+"--"+rangeDosDetectionThresholdMap.get(range));
+ }
+
}
diff --git a/src/test/java/com/zdjizhi/common/IpTest.java b/src/test/java/com/zdjizhi/common/IpTest.java
new file mode 100644
index 0000000..830f7bf
--- /dev/null
+++ b/src/test/java/com/zdjizhi/common/IpTest.java
@@ -0,0 +1,84 @@
+package com.zdjizhi.common;
+
+import inet.ipaddr.Address;
+import inet.ipaddr.AddressStringException;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import inet.ipaddr.format.util.AddressTrieMap;
+import inet.ipaddr.format.util.AssociativeAddressTrie;
+import inet.ipaddr.ipv4.IPv4Address;
+import inet.ipaddr.ipv4.IPv4AddressAssociativeTrie;
+import org.apache.flink.shaded.guava18.com.google.common.collect.Range;
+import org.apache.flink.shaded.guava18.com.google.common.collect.TreeRangeMap;
+
+import java.util.Arrays;
+import java.util.HashMap;
+
+public class IpTest {
+ public static void main(String[] args) throws Exception {
+ IPv4AddressAssociativeTrie trie = new IPv4AddressAssociativeTrie<>();
+
+ IPAddress str1 = new IPAddressString("1.2.3.4").getAddress();
+ IPAddress str2 = new IPAddressString("10.0.0.0/15").getAddress();
+ IPAddress str3 = new IPAddressString("25.4.2.0/23").getAddress();
+ IPAddress str4 = new IPAddressString("192.168.8.0/21").getAddress();
+ IPAddress str5 = new IPAddressString("240.0.0.0/4").getAddress();
+ IPAddress str6 = new IPAddressString("fc00::0/64").getAddress();
+ IPAddress str7 = new IPAddressString("fc00::10:1").getAddress();
+
+ TreeRangeMap rangeMap = TreeRangeMap.create();
+ rangeMap.put(Range.closed(str1.getLower(),str1.getUpper()),1);
+ rangeMap.put(Range.closed(str2.getLower(),str2.getUpper()),2);
+ rangeMap.put(Range.closed(str3.getLower(),str3.getUpper()),3);
+ rangeMap.put(Range.closed(str4.getLower(),str4.getUpper()),4);
+ rangeMap.put(Range.closed(str5.getLower(),str5.getUpper()),5);
+ rangeMap.put(Range.closed(str6.getLower(),str6.getUpper()),6);
+ rangeMap.put(Range.closed(str7.getLower(),str7.getUpper()),7);
+
+ IPAddress pv4 = new IPAddressString("255.255.14.255").getAddress();
+ IPAddress pv42 = new IPAddressString("1.2.3.4").getAddress();
+ IPAddress pv43 = new IPAddressString("fc00::").getAddress();
+ IPAddress pv44 = new IPAddressString("fc00::10:1").getAddress();
+
+ System.out.println(rangeMap.get(pv4));
+ System.out.println(rangeMap.get(pv42));
+ System.out.println(rangeMap.get(pv43));
+ System.out.println(rangeMap.get(pv44));
+
+ /*
+ System.out.println(str5.toSequentialRange());
+// System.out.println(str2.contains(new IPAddressString("10.0.0.2")));
+// System.out.println(str5.toAddress().toIPv4().toSequentialRange());
+
+
+ trie.put(str1,1);
+ trie.put(str2,2);
+ trie.put(str3,3);
+ trie.put(str4,4);
+ trie.put(str5,5);
+
+ AddressTrieMap trieMap = new AddressTrieMap<>(trie);
+
+
+
+ trieMap.forEach((k,v) -> {
+ System.out.println(k.toString() + "--" + v);
+ });
+
+ System.out.println("-----------------");
+
+ trie.forEach((k) -> System.out.println(k.toString()));
+
+ System.out.println(str5.contains(pv4));
+ System.out.println(trie.contains(pv4));
+ System.out.println(trieMap.get(pv4));
+ System.out.println(trieMap.containsKey(pv4));
+// System.out.println(trieMap.getRange());
+// IPAddress str3 = new IPAddressString("fc00::10:1").getAddress();
+// IPAddress str4 = new IPAddressString("fc00::10:2/64").getAddress();
+
+// System.out.println(Arrays.toString(str1.mergeToPrefixBlocks(str2,str3,str4)));
+
+ */
+ }
+}