1.适配TSG 23.07及以上功能,添加数据传输统计指标,并输出至pushgateway。(GAL-409)
2.原URL参数domain从http_domain字段取值,更新为从common_server_domain字段取值。(GAL-410)
This commit is contained in:
75
src/test/java/com/zdjizhi/function/Base64Test.java
Normal file
75
src/test/java/com/zdjizhi/function/Base64Test.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* @author qidaijie
|
||||
* @Package com.zdjizhi.function
|
||||
* @Description:
|
||||
* @date 2022/11/39:36
|
||||
*/
|
||||
public class Base64Test {
|
||||
private static final Log logger = LogFactory.get();
|
||||
|
||||
/**
|
||||
* 根据编码解码base64(hutool)
|
||||
|
||||
* @return 解码字符串
|
||||
*/
|
||||
@Test
|
||||
public void decodeBase64Hutool() {
|
||||
try {
|
||||
System.out.println(Base64.decodeStr("bWFpbF90ZXN0X2VuZ2xpc2gudHh0"));
|
||||
System.out.println(Base64.decodeStr("aGVsbG8="));
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Resolve Base64 exception, exception information:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据编码解码base64
|
||||
|
||||
* @return 解码字符串
|
||||
*/
|
||||
@Test
|
||||
public void encodeBase64() {
|
||||
try {
|
||||
System.out.println(java.util.Base64.getUrlEncoder().encodeToString("runoob?java8".getBytes("ISO-8859-1")));
|
||||
System.out.println(java.util.Base64.getUrlEncoder().encodeToString("runoob?java8".getBytes("utf-8")));
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Resolve Base64 exception, exception information:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编码解码base64
|
||||
|
||||
* @return 解码字符串
|
||||
*/
|
||||
@Test
|
||||
public void decodeBase64() {
|
||||
try {
|
||||
byte[] base64decodedBytes = java.util.Base64.getDecoder().decode("bWFpbF90ZXN0X2VuZ2xpc2gudHh0");
|
||||
|
||||
System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
|
||||
System.out.println("原始字符串: " + new String(base64decodedBytes));
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Resolve Base64 exception, exception information:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/test/java/com/zdjizhi/function/EncryptorTest.java
Normal file
53
src/test/java/com/zdjizhi/function/EncryptorTest.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author qidaijie
|
||||
* @Package com.zdjizhi
|
||||
* @Description:
|
||||
* @date 2022/3/1610:55
|
||||
*/
|
||||
public class EncryptorTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void passwordTest(){
|
||||
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
||||
// 配置加密解密的密码/salt值
|
||||
encryptor.setPassword("galaxy");
|
||||
// 对"raw_password"进行加密:S5kR+Y7CI8k7MaecZpde25yK8NKUnd6p
|
||||
String kafkaUser = encryptor.encrypt("admin");
|
||||
String kafkaPin = encryptor.encrypt("galaxy2019");
|
||||
String nacosPin = encryptor.encrypt("nacos");
|
||||
String nacosUser = encryptor.encrypt("nacos");
|
||||
|
||||
System.out.println("Kafka:\n"+"The username is: "+kafkaUser);
|
||||
System.out.println("The pin is: "+kafkaPin);
|
||||
System.out.println("Nacos:\n"+"The username is: "+nacosUser);
|
||||
System.out.println("The pin is: "+nacosPin);
|
||||
// 再进行解密:raw_password
|
||||
System.out.println("Kafka:\n"+"The username is: "+encryptor.decrypt(kafkaUser));
|
||||
System.out.println("The pin is: "+encryptor.decrypt(kafkaPin));
|
||||
|
||||
System.out.println("Nacos:\n"+"The username is: "+encryptor.decrypt(nacosUser));
|
||||
System.out.println("The pin is: "+encryptor.decrypt(nacosPin));
|
||||
System.out.println("------------------------------------------------------");
|
||||
|
||||
System.out.println("The vknRT6U4I739rLIha9CvojM+4uFyXZLEYpO2HZayLnRak1HPW0K2yZ3vnQBA2foo decryption result is: "+encryptor.decrypt("vknRT6U4I739rLIha9CvojM+4uFyXZLEYpO2HZayLnRak1HPW0K2yZ3vnQBA2foo"));
|
||||
System.out.println("The SU05WiYTLLrF+cVZ410gJdrfEgxgnYG9RGsI+3dZZq54XFLOXEzOvRuMAvLlBf4k decryption result is: "+encryptor.decrypt("SU05WiYTLLrF+cVZ410gJdrfEgxgnYG9RGsI+3dZZq54XFLOXEzOvRuMAvLlBf4k"));
|
||||
System.out.println("The qUA355VopKSx6kwwwXZwqWWEYSu76Slz decryption result is: "+encryptor.decrypt("qUA355VopKSx6kwwwXZwqWWEYSu76Slz"));
|
||||
System.out.println("The sJ9iiTeE/7moI2hKn8asMg== decryption result is: "+encryptor.decrypt("sJ9iiTeE/7moI2hKn8asMg=="));
|
||||
System.out.println("The 63aTpwv2vH0vPikW+3Jjig== decryption result is: "+encryptor.decrypt("63aTpwv2vH0vPikW+3Jjig=="));
|
||||
System.out.println("The Ei1P4R1e5KTdJR+ZVnBmug== decryption result is: "+encryptor.decrypt("Ei1P4R1e5KTdJR+ZVnBmug=="));
|
||||
System.out.println("The iW8ekP1SZC6v/7cfJKAqXXrjApJox+cH decryption result is: "+encryptor.decrypt("iW8ekP1SZC6v/7cfJKAqXXrjApJox+cH"));
|
||||
System.out.println("The TV7Jm4dQCE/LJznp4iTm4ICkBscquv9G decryption result is: "+encryptor.decrypt("TV7Jm4dQCE/LJznp4iTm4ICkBscquv9G"));
|
||||
System.out.println("The LDEb2OekU7iZWiFw6pUYBSozVKP27r1y decryption result is: "+encryptor.decrypt("LDEb2OekU7iZWiFw6pUYBSozVKP27r1y"));
|
||||
|
||||
StandardPBEStringEncryptor encryptorCM = new StandardPBEStringEncryptor();
|
||||
encryptorCM.setPassword("bifang-api");
|
||||
System.out.println("The Zp65tFN3CsAXwpNfgfjZ0PrquSrokM1c decryption result is: "+encryptorCM.decrypt("Zp65tFN3CsAXwpNfgfjZ0PrquSrokM1c"));
|
||||
}
|
||||
|
||||
}
|
||||
47
src/test/java/com/zdjizhi/function/GtpcTest.java
Normal file
47
src/test/java/com/zdjizhi/function/GtpcTest.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.zdjizhi.common.FlowWriteConfig;
|
||||
import com.zdjizhi.tools.connections.hbase.HBaseUtils;
|
||||
import com.zdjizhi.tools.json.JsonPathUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class GtpcTest {
|
||||
private static final Log logger = LogFactory.get();
|
||||
|
||||
@Test
|
||||
public void gtpcMatch() {
|
||||
String param = "$.[?(@.tunnels_schema_type=='GTP')].gtp_endpoint_a2b_teid,$.[?(@.tunnels_schema_type=='GTP')].gtp_endpoint_b2a_teid";
|
||||
String logValue = "[{\"tunnels_schema_type\":\"GTP\",\"gtp_endpoint_a2b_teid\":4129335432,\"gtp_endpoint_b2a_teid\":4129335434,\"gtp_sgw_ip\":\"120.36.3.97\",\"gtp_pgw_ip\":\"43.224.53.100\",\"gtp_sgw_port\":2152,\"gtp_pgw_port\":51454},{\"tunnels_schema_type\":\"ETHERNET\",\"source_mac\":\"80:69:33:ea:a5:57\",\"destination_mac\":\"14:09:dc:df:a3:40\"}]";
|
||||
String appendToKey = "common_imsi,common_imei,common_phone_number";
|
||||
|
||||
try {
|
||||
String teid = null;
|
||||
String[] exprs = param.split(FlowWriteConfig.FORMAT_SPLITTER);
|
||||
for (String expr : exprs) {
|
||||
String value = JsonPathUtil.analysis(logValue, expr).toString();
|
||||
if (value != null) {
|
||||
teid = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(teid);
|
||||
if (teid != null) {
|
||||
String[] appendToKeys = appendToKey.split(FlowWriteConfig.FORMAT_SPLITTER);
|
||||
HashMap<String, Object> userData = HBaseUtils.getGtpData(teid);
|
||||
if (userData != null) {
|
||||
for (String key : appendToKeys) {
|
||||
System.out.println(userData.get(key).toString());
|
||||
}
|
||||
} else {
|
||||
logger.warn("Description The user whose TEID is " + teid + " was not matched!");
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException re) {
|
||||
logger.error("An exception occurred in teid type conversion or parsing of user information!" + re);
|
||||
}
|
||||
}
|
||||
}
|
||||
239
src/test/java/com/zdjizhi/function/HBaseTest.java
Normal file
239
src/test/java/com/zdjizhi/function/HBaseTest.java
Normal file
@@ -0,0 +1,239 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.zdjizhi.common.FlowWriteConfig;
|
||||
import com.geedgenetworks.utils.StringUtil;
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||||
import org.apache.hadoop.hbase.TableName;
|
||||
import org.apache.hadoop.hbase.client.*;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author qidaijie
|
||||
* @Package com.zdjizhi
|
||||
* @Description:
|
||||
* @date 2021/12/310:42
|
||||
*/
|
||||
public class HBaseTest {
|
||||
private static final Log logger = LogFactory.get();
|
||||
private static Map<String, String> radiusMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
private static Map<String,HashMap<String, Object>> gtpcMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
@Test
|
||||
public void getColumn() {
|
||||
// 管理Hbase的配置信息
|
||||
Configuration configuration = HBaseConfiguration.create();
|
||||
// 设置zookeeper节点
|
||||
configuration.set("hbase.zookeeper.quorum", "192.168.44.12:2181");
|
||||
configuration.set("hbase.client.retries.number", "1");
|
||||
configuration.set("hbase.client.pause", "50");
|
||||
configuration.set("hbase.rpc.timeout", "3000");
|
||||
configuration.set("zookeeper.recovery.retry", "1");
|
||||
configuration.set("zookeeper.recovery.retry.intervalmill", "200");
|
||||
try {
|
||||
System.out.println(System.currentTimeMillis());
|
||||
Connection connection = ConnectionFactory.createConnection(configuration);
|
||||
Table table = connection.getTable(TableName.valueOf("tsg_galaxy:relation_framedip_account"));
|
||||
Scan scan2 = new Scan();
|
||||
ResultScanner scanner = table.getScanner(scan2);
|
||||
for (Result result : scanner) {
|
||||
int acctStatusType;
|
||||
boolean hasType = result.containsColumn(Bytes.toBytes("radius"), Bytes.toBytes("acct_status_type"));
|
||||
if (hasType) {
|
||||
acctStatusType = Bytes.toInt(result.getValue(Bytes.toBytes("radius"), Bytes.toBytes("acct_status_type")));
|
||||
} else {
|
||||
acctStatusType = 3;
|
||||
}
|
||||
String framedIp = Bytes.toString(result.getValue(Bytes.toBytes("radius"), Bytes.toBytes("framed_ip")));
|
||||
String account = Bytes.toString(result.getValue(Bytes.toBytes("radius"), Bytes.toBytes("account")));
|
||||
System.out.println("status" + acctStatusType + "key:" + framedIp + "value:" + account);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
System.out.println(System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getGtpcData() {
|
||||
// 管理Hbase的配置信息
|
||||
Configuration configuration = HBaseConfiguration.create();
|
||||
// 设置zookeeper节点
|
||||
configuration.set("hbase.zookeeper.quorum", "192.168.44.12:2181");
|
||||
configuration.set("hbase.client.retries.number", "1");
|
||||
configuration.set("hbase.client.pause", "50");
|
||||
configuration.set("hbase.rpc.timeout", "3000");
|
||||
configuration.set("zookeeper.recovery.retry", "1");
|
||||
configuration.set("zookeeper.recovery.retry.intervalmill", "200");
|
||||
long begin = System.currentTimeMillis();
|
||||
ResultScanner scanner = null;
|
||||
try {
|
||||
Connection connection = ConnectionFactory.createConnection(configuration);
|
||||
Table table = connection.getTable(TableName.valueOf(FlowWriteConfig.HBASE_GTPC_TABLE_NAME));
|
||||
Scan scan2 = new Scan();
|
||||
scanner = table.getScanner(scan2);
|
||||
for (Result result : scanner) {
|
||||
String upLinkTeid = getTeid(result, "uplink_teid");
|
||||
String downLinkTeid = getTeid(result, "downlink_teid");
|
||||
String phoneNumber = getString(result, FlowWriteConfig.GTPC_FAMILY_NAME, "phone_number").trim();
|
||||
String imsi = getString(result, FlowWriteConfig.GTPC_FAMILY_NAME, "imsi").trim();
|
||||
String imei = getString(result, FlowWriteConfig.GTPC_FAMILY_NAME, "imei").trim();
|
||||
Long lastUpdateTime = getLong(result, FlowWriteConfig.GTPC_FAMILY_NAME, "last_update_time");
|
||||
|
||||
HashMap<String, Object> buildUserData = buildUserData(phoneNumber, imsi, imei, lastUpdateTime);
|
||||
|
||||
if (FlowWriteConfig.DEFAULT_RELATIONSHIP_MODULE.equals(FlowWriteConfig.DATA_RELATIONSHIP_MODEL)) {
|
||||
String vsysId = getVsysId(result).trim();
|
||||
updateCache(gtpcMap, upLinkTeid+vsysId, buildUserData, lastUpdateTime);
|
||||
updateCache(gtpcMap, downLinkTeid+vsysId, buildUserData, lastUpdateTime);
|
||||
} else {
|
||||
updateCache(gtpcMap, upLinkTeid, buildUserData, lastUpdateTime);
|
||||
updateCache(gtpcMap, downLinkTeid, buildUserData, lastUpdateTime);
|
||||
}
|
||||
}
|
||||
logger.warn("The obtain the number of GTP-C relationships : " + gtpcMap.size());
|
||||
logger.warn("The time spent to obtain GTP-C relationships : " + (System.currentTimeMillis() - begin));
|
||||
} catch (IOException | RuntimeException e) {
|
||||
logger.error("The relationship between USER and TEID obtained from HBase is abnormal! message is :" + e);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (scanner != null) {
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
for (String key : gtpcMap.keySet()){
|
||||
System.out.println(key +"---"+gtpcMap.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HBase内String类型的值
|
||||
*
|
||||
* @param result 结果集
|
||||
* @param familyName 列族名称
|
||||
* @param columnName 列名称
|
||||
* @return 结果数据
|
||||
*/
|
||||
private static String getString(Result result, String familyName, String columnName) {
|
||||
byte[] familyBytes = Bytes.toBytes(familyName);
|
||||
byte[] columnBytes = Bytes.toBytes(columnName);
|
||||
boolean contains = result.containsColumn(familyBytes, columnBytes);
|
||||
if (contains) {
|
||||
String data = Bytes.toString(result.getValue(familyBytes, columnBytes)).trim();
|
||||
if (StringUtil.isNotBlank(data)) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HBase内String类型的值
|
||||
*
|
||||
* @param result 结果集
|
||||
* @param columnName 列名称
|
||||
* @return 结果数据
|
||||
*/
|
||||
private static Long getLong(Result result, String familyName, String columnName) {
|
||||
byte[] familyBytes = Bytes.toBytes(familyName);
|
||||
byte[] columnBytes = Bytes.toBytes(columnName);
|
||||
boolean contains = result.containsColumn(familyBytes, columnBytes);
|
||||
if (contains) {
|
||||
return Bytes.toLong(result.getValue(familyBytes, columnBytes));
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HBase内String类型的值
|
||||
*
|
||||
* @param result 结果集
|
||||
* @param columnName 列名称
|
||||
* @return 结果数据
|
||||
*/
|
||||
private static String getTeid(Result result, String columnName) {
|
||||
byte[] familyBytes = Bytes.toBytes(FlowWriteConfig.GTPC_FAMILY_NAME);
|
||||
byte[] columnBytes = Bytes.toBytes(columnName);
|
||||
boolean contains = result.containsColumn(familyBytes, columnBytes);
|
||||
if (contains) {
|
||||
String data = String.valueOf(Bytes.toLong(result.getValue(familyBytes, columnBytes))).trim();
|
||||
if (StringUtil.isNotBlank(data)) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建用户信息
|
||||
*
|
||||
* @param phoneNumber 手机号
|
||||
* @param imsi 用户标识
|
||||
* @param imei 设备标识
|
||||
* @return 用户信息
|
||||
*/
|
||||
private static HashMap<String, Object> buildUserData(String phoneNumber, String imsi, String imei, Long lastUpdateTime) {
|
||||
HashMap<String, Object> tmpMap = new HashMap<>(4);
|
||||
tmpMap.put("common_phone_number", phoneNumber);
|
||||
tmpMap.put("common_imsi", imsi);
|
||||
tmpMap.put("common_imei", imei);
|
||||
tmpMap.put("last_update_time", lastUpdateTime);
|
||||
return tmpMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取HBase内String类型的值
|
||||
*
|
||||
* @param result 结果集
|
||||
* @return 结果数据
|
||||
*/
|
||||
static String getVsysId(Result result) {
|
||||
byte[] familyBytes = Bytes.toBytes("common");
|
||||
byte[] columnBytes = Bytes.toBytes("vsys_id");
|
||||
boolean contains = result.containsColumn(familyBytes, columnBytes);
|
||||
if (contains) {
|
||||
String data = String.valueOf(Bytes.toInt(result.getValue(familyBytes, columnBytes))).trim();
|
||||
if (StringUtil.isNotBlank(data)) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return "1";
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存与新获取的数据时间戳大小,若大于缓存内记录的时间戳;则更新缓存
|
||||
*
|
||||
* @param gtpcMap 缓存集合
|
||||
* @param key 上下行teid
|
||||
* @param userData 获取HBase内的用户信息
|
||||
* @param lastUpdateTime 该用户信息最后更新时间
|
||||
*/
|
||||
private static void updateCache(Map<String, HashMap<String, Object>> gtpcMap, String key, HashMap<String, Object> userData, Long lastUpdateTime) {
|
||||
if (StringUtil.isNotBlank(key)){
|
||||
if (gtpcMap.containsKey(key)) {
|
||||
Long oldUpdateTime = Long.parseLong(gtpcMap.get(key).get("last_update_time").toString());
|
||||
if (lastUpdateTime > oldUpdateTime) {
|
||||
gtpcMap.put(key, userData);
|
||||
}
|
||||
} else {
|
||||
gtpcMap.put(key, userData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
165
src/test/java/com/zdjizhi/function/IpLookupTest.java
Normal file
165
src/test/java/com/zdjizhi/function/IpLookupTest.java
Normal file
@@ -0,0 +1,165 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.geedgenetworks.utils.GalaxyDataBaseReader;
|
||||
import com.geedgenetworks.utils.IpLookupV2;
|
||||
import com.maxmind.db.CHMCache;
|
||||
import com.maxmind.db.Reader;
|
||||
import com.zdjizhi.common.FlowWriteConfig;
|
||||
import com.zdjizhi.tools.connections.http.HttpClientService;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author qidaijie
|
||||
* @Package com.zdjizhi
|
||||
* @Description:
|
||||
* @date 2021/11/611:38
|
||||
*/
|
||||
public class IpLookupTest {
|
||||
private static final Log logger = LogFactory.get();
|
||||
private static final String DAT_FILEPATH = "D:\\workerspace\\dat\\";
|
||||
|
||||
|
||||
@Test
|
||||
public void ipLookupDatabaseTest() {
|
||||
|
||||
IpLookupV2.Builder ipLookupBuilder = new IpLookupV2.Builder(false);
|
||||
|
||||
ipLookupBuilder.loadDataFileV4(DAT_FILEPATH + "ip_v4_built_in.mmdb");
|
||||
// ipLookupBuilder.loadDataFileV6(DAT_FILEPATH + "ip_v6_built_in.mmdb");
|
||||
// ipLookupBuilder.loadDataFilePrivateV4(DAT_FILEPATH + "ip_v4_user_defined.mmdb");
|
||||
// ipLookupBuilder.loadDataFilePrivateV6(DAT_FILEPATH + "ip_v6_user_defined.mmdb");
|
||||
// ipLookupBuilder.loadAsnDataFile(DAT_FILEPATH + "asn_v4.mmdb");
|
||||
// ipLookupBuilder.loadAsnDataFileV6(DAT_FILEPATH + "asn_v6.mmdb");
|
||||
IpLookupV2 ipLookup = ipLookupBuilder.build();
|
||||
|
||||
|
||||
// String ip = "23.200.74.224";
|
||||
String ip = "121.14.89.209";
|
||||
|
||||
try {
|
||||
System.out.println(ipLookup.cityLookup(ip));
|
||||
System.out.println(ipLookup.cityLookupDetail(ip));
|
||||
System.out.println(ipLookup.cityLatLngLookup(ip));
|
||||
System.out.println(ipLookup.provinceLookup(ip));
|
||||
System.out.println(ipLookup.administrativeAreaLookupDetail(ip));
|
||||
System.out.println(ipLookup.countryLookup(ip));
|
||||
System.out.println(ipLookup.locationLookupDetail(ip));
|
||||
System.out.println(ipLookup.administrativeAreaLookupDetail(ip));
|
||||
System.out.println(ipLookup.infoLookupToJSONString(ip));
|
||||
|
||||
//ASN
|
||||
System.out.println(ipLookup.asnLookup(ip));
|
||||
System.out.println(ipLookup.asnLookupInfo(ip));
|
||||
System.out.println(ipLookup.asnLookupDetail(ip));
|
||||
System.out.println(ipLookup.asnLookupOrganization(ip));
|
||||
|
||||
} catch (NullPointerException npe) {
|
||||
logger.error("The MMDB file is not loaded or IP is null! " + npe.getMessage());
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Get clientIP location error! " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void ipLookupForBytesTest() {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
String url = "http://192.168.44.12:9098/hos/knowledge_base_hos_bucket/d2ab3313-1941-4847-84fa-5dbbd8a9007f-aXBfdjRfYnVpbHRfaW4=.mmdb";
|
||||
HttpClientService httpClientService = new HttpClientService();
|
||||
inputStream = httpClientService.httpGetInputStream(url, 3000);
|
||||
Reader reader = new Reader(inputStream, new CHMCache());
|
||||
InetAddress ipAddress = InetAddress.getByName("121.14.89.209");
|
||||
Map map = reader.get(ipAddress, Map.class);
|
||||
System.out.println(map.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IoUtil.close(inputStream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void ipLookupInputStreamTest() {
|
||||
InputStream asnInputStream = null;
|
||||
InputStream ipv4InputStream = null;
|
||||
String ip = "114.64.231.114";
|
||||
try {
|
||||
HttpClientService httpClientService = new HttpClientService();
|
||||
String asnUrl = "http://192.168.44.12:9098/hos/knowledge_base_hos_bucket/1b96764c-59dd-4d6b-8edb-623705f708a5-YXNuX3Y0.mmdb";
|
||||
String ipv4Url = "http://192.168.44.12:9098/hos/knowledge_base_hos_bucket/24cb6a74-f048-4672-988f-112858427a3b-aXBfdjRfYnVpbHRfaW4=.mmdb";
|
||||
asnInputStream = httpClientService.httpGetInputStream(asnUrl, 3000);
|
||||
ipv4InputStream = httpClientService.httpGetInputStream(ipv4Url, 3000);
|
||||
|
||||
IpLookupV2 ipLookup = new IpLookupV2.Builder(false)
|
||||
.loadDataFileV4(ipv4InputStream)
|
||||
.loadAsnDataFileV4(asnInputStream)
|
||||
.build();
|
||||
|
||||
System.out.println(ipLookup.cityLookup(ip));
|
||||
System.out.println(ipLookup.cityLookupDetail(ip));
|
||||
System.out.println(ipLookup.cityLatLngLookup(ip));
|
||||
System.out.println(ipLookup.provinceLookup(ip));
|
||||
System.out.println(ipLookup.administrativeAreaLookupDetail(ip));
|
||||
System.out.println(ipLookup.countryLookup(ip));
|
||||
System.out.println(ipLookup.locationLookupDetail(ip));
|
||||
System.out.println(ipLookup.asnLookup(ip));
|
||||
System.out.println(ipLookup.administrativeAreaLookupDetail(ip));
|
||||
|
||||
} catch (NullPointerException npe) {
|
||||
logger.error("The MMDB file is not loaded or IP is null! " + npe.getMessage());
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Get clientIP location error! " + e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(asnInputStream);
|
||||
IoUtil.close(ipv4InputStream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void inputLookupTest() {
|
||||
// String ip = "121.14.89.209";
|
||||
//
|
||||
byte[] localFile = new FileReader(DAT_FILEPATH + "built_in_ip_location.mmdb").readBytes();
|
||||
String localFileSha256 = DigestUtil.sha256Hex(localFile);
|
||||
System.out.println("本地文件SHA256:"+localFileSha256);
|
||||
//
|
||||
// IpLookupV2 ipLookup = new IpLookupV2.Builder(false)
|
||||
// .loadDataFileV4(DAT_FILEPATH + "built_in_ip_location.mmdb").build();
|
||||
//
|
||||
// System.out.println(ipLookup.infoLookup(ip));
|
||||
|
||||
try {
|
||||
HttpClientService httpClientService = new HttpClientService();
|
||||
InputStream inputStream = httpClientService.httpGetInputStream("http://192.168.44.55:9098/hos/knowledge_base_hos_bucket/9b1ce6b4-024d-4343-80d5-6e6dc0ad0863-aXA0.mmdb", FlowWriteConfig.HTTP_SOCKET_TIMEOUT);
|
||||
byte[] bytes = IOUtils.toByteArray(inputStream);
|
||||
String downloadFileSha256 = DigestUtil.sha256Hex(bytes);
|
||||
InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||
|
||||
System.out.println("HOS下载文件2 SHA256:" + downloadFileSha256);
|
||||
System.out.println("HOS下载文件2 size:" + bytes.length);
|
||||
|
||||
// IpLookupV2 ipLookup2 = new IpLookupV2.Builder(false)
|
||||
// .loadDataFileV4(byteArrayInputStream).build();
|
||||
new GalaxyDataBaseReader.Builder(byteArrayInputStream).withCache(new CHMCache()).build();
|
||||
InputStream inputStream1 = httpClientService.httpGetInputStream("http://192.168.44.55:9098/hos/knowledge_base_hos_bucket/9b1ce6b4-024d-4343-80d5-6e6dc0ad0863-aXA0.mmdb", FlowWriteConfig.HTTP_SOCKET_TIMEOUT);
|
||||
new GalaxyDataBaseReader.Builder(inputStream1).withCache(new CHMCache()).build();
|
||||
// System.out.println(ipLookup2.infoLookup(ip));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
89
src/test/java/com/zdjizhi/function/TimestampTest.java
Normal file
89
src/test/java/com/zdjizhi/function/TimestampTest.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimestampTest {
|
||||
|
||||
@Test
|
||||
public void timestampToDate() {
|
||||
System.out.println(getLocalDateTime(1693905281L, 0, "Asia/Shanghai"));
|
||||
System.out.println(getLocalDateTime(1693905281048L, 3, "Asia/Shanghai"));
|
||||
System.out.println(getLocalDateTime(1693905281048L, 6, "Asia/Shanghai"));
|
||||
System.out.println(getLocalDateTime(1693905281048L, 9, "UTC+03:00"));
|
||||
System.out.println(getZoneDateTime(1693905281L, 3, "Asia/Shanghai"));
|
||||
System.out.println(getZoneDateTime(1693905281048L, 6, "Asia/Shanghai"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void timestampConversion() {
|
||||
long nanosTimestamp = 1630988475000000000L; // 纳秒级时间戳
|
||||
System.out.println("纳秒级时间戳: " + timestampToSeconds(nanosTimestamp));
|
||||
|
||||
long microsTimestamp = 1630988475000000L; // 微秒级时间戳
|
||||
System.out.println("微秒级时间戳: " + timestampToSeconds(microsTimestamp));
|
||||
|
||||
long millisTimestamp = 1693969952127L; // 微秒级时间戳
|
||||
System.out.println("毫秒级时间戳: " + timestampToSeconds(millisTimestamp));
|
||||
|
||||
long errorTimestamp = 169396995L; // 微秒级时间戳
|
||||
System.out.println("异常时间戳: " + timestampToSeconds(errorTimestamp));
|
||||
}
|
||||
|
||||
private Long timestampToSeconds(long timestamp) {
|
||||
int timestampLength = Long.toString(timestamp).length();
|
||||
switch (timestampLength) {
|
||||
case 13:
|
||||
return TimeUnit.MILLISECONDS.toSeconds(timestamp);
|
||||
case 16:
|
||||
return TimeUnit.MICROSECONDS.toSeconds(timestamp);
|
||||
case 19:
|
||||
return TimeUnit.NANOSECONDS.toSeconds(timestamp);
|
||||
default:
|
||||
// throw new RuntimeException("This timestamp:" + timestamp + " format is not nanosecond, microsecond, millisecond, or second");
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
private String getLocalDateTime(Long timestamp, int precision, String timeZone) {
|
||||
boolean isMillis = String.valueOf(timestamp).length() > 10;
|
||||
String timePattern = "yyyy-MM-dd HH:mm:ss";
|
||||
if (precision > 0) {
|
||||
String s = String.join("", Collections.nCopies(precision, "S"));
|
||||
timePattern = String.join(".", timePattern, s);
|
||||
}
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timePattern);
|
||||
|
||||
Instant instant;
|
||||
if (isMillis) {
|
||||
instant = Instant.ofEpochMilli(timestamp);
|
||||
} else {
|
||||
instant = Instant.ofEpochSecond(timestamp);
|
||||
}
|
||||
return LocalDateTime.ofInstant(instant, ZoneId.of(timeZone)).format(formatter);
|
||||
}
|
||||
|
||||
|
||||
private String getZoneDateTime(Long timestamp, int precision, String timeZone) {
|
||||
boolean isMillis = String.valueOf(timestamp).length() > 10;
|
||||
String timePattern = "yyyy-MM-dd'T'HH:mm:ss.";
|
||||
if (precision > 0) {
|
||||
String s = String.join("", Collections.nCopies(precision, "S"));
|
||||
timePattern = String.join("", timePattern, s, "XXX");
|
||||
}
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timePattern);
|
||||
|
||||
Instant instant;
|
||||
if (isMillis) {
|
||||
instant = Instant.ofEpochMilli(timestamp);
|
||||
} else {
|
||||
instant = Instant.ofEpochSecond(timestamp);
|
||||
}
|
||||
return ZonedDateTime.ofInstant(instant, ZoneId.of(timeZone)).format(formatter);
|
||||
}
|
||||
}
|
||||
39
src/test/java/com/zdjizhi/function/TopDomainTest.java
Normal file
39
src/test/java/com/zdjizhi/function/TopDomainTest.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.zdjizhi.function;
|
||||
|
||||
import com.google.common.net.InternetDomainName;
|
||||
import com.geedgenetworks.utils.FormatUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author qidaijie
|
||||
* @Package com.zdjizhi.function
|
||||
* @Description:
|
||||
* @date 2023/7/2413:55
|
||||
*/
|
||||
public class TopDomainTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void getTopDomainTest() {
|
||||
String host = "heartsofsteel-.tumblr.com";
|
||||
System.out.println(FormatUtils.getTopPrivateDomain(host));
|
||||
host = "heartsofsteel.tumblr.com";
|
||||
System.out.println(FormatUtils.getTopPrivateDomain(host));
|
||||
|
||||
|
||||
// String sni = "www.googleapis.com";
|
||||
String sni = "juicebox-.tumblr.com";
|
||||
String domain = FormatUtils.getDomain(sni);
|
||||
System.out.println(domain);
|
||||
System.out.println(FormatUtils.getTopPrivateDomain(sni));
|
||||
|
||||
System.out.println(InternetDomainName.isValid(sni));
|
||||
System.out.println(InternetDomainName.isValid(domain));
|
||||
|
||||
InternetDomainName internetDomainName = InternetDomainName.from(domain);
|
||||
if (internetDomainName.isUnderPublicSuffix()) {
|
||||
System.out.println(internetDomainName.topPrivateDomain().toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user