2021-07-29 10:02:31 +08:00
|
|
|
package com.zdjizhi.utils;
|
|
|
|
|
|
|
|
|
|
import com.zdjizhi.common.CommonConfig;
|
2022-11-23 15:30:24 +08:00
|
|
|
import com.zdjizhi.common.CustomFile;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2021-07-29 10:02:31 +08:00
|
|
|
|
|
|
|
|
public class IpUtils {
|
2022-11-23 15:30:24 +08:00
|
|
|
public static IpLookupV2 ipLookup ;
|
|
|
|
|
|
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(IpUtils.class);
|
|
|
|
|
|
2021-07-29 10:02:31 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IP定位库工具类
|
|
|
|
|
*/
|
2022-11-23 15:30:24 +08:00
|
|
|
// public static IpLookupV2 ipLookup = new IpLookupV2.Builder(false)
|
|
|
|
|
// .loadDataFileV4(CommonConfig.IP_MMDB_PATH + "ip_v4_built_in.mmdb")
|
|
|
|
|
// .loadDataFileV6(CommonConfig.IP_MMDB_PATH + "ip_v6_built_in.mmdb")
|
|
|
|
|
// .loadDataFilePrivateV4(CommonConfig.IP_MMDB_PATH + "ip_v4_user_defined.mmdb")
|
|
|
|
|
// .loadDataFilePrivateV6(CommonConfig.IP_MMDB_PATH + "ip_v6_user_defined.mmdb")
|
|
|
|
|
// .build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void loadIpLook(){
|
|
|
|
|
try {
|
|
|
|
|
IpLookupV2.Builder builder = new IpLookupV2.Builder(false);
|
|
|
|
|
if ("CLUSTER".equals(CommonConfig.CLUSTER_OR_SINGLE)) {
|
|
|
|
|
byte[] ipv4BuiltBytes = HdfsUtils.getFileBytes(CommonConfig.HDFS_PATH + "ip_v4_built_in.mmdb");
|
|
|
|
|
if (ipv4BuiltBytes!=null){
|
|
|
|
|
InputStream ipv4BuiltInputStream = new ByteArrayInputStream(ipv4BuiltBytes);
|
|
|
|
|
builder.loadDataFileV4(ipv4BuiltInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv6BuiltBytes = HdfsUtils.getFileBytes(CommonConfig.HDFS_PATH + "ip_v6_built_in.mmdb");
|
|
|
|
|
if (ipv6BuiltBytes!=null){
|
|
|
|
|
InputStream ipv6BuiltInputStream = new ByteArrayInputStream(ipv6BuiltBytes);
|
|
|
|
|
builder.loadDataFileV6(ipv6BuiltInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv4UserBytes = HdfsUtils.getFileBytes(CommonConfig.HDFS_PATH + "ip_v4_user_defined.mmdb");
|
|
|
|
|
if (ipv4UserBytes!=null){
|
|
|
|
|
InputStream ipv4UserInputStream = new ByteArrayInputStream(ipv4UserBytes);
|
|
|
|
|
builder.loadDataFilePrivateV4(ipv4UserInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv6UserBytes = HdfsUtils.getFileBytes(CommonConfig.HDFS_PATH + "ip_v6_user_defined.mmdb");
|
|
|
|
|
if (ipv6UserBytes!=null){
|
|
|
|
|
InputStream ipv6UserInputStream = new ByteArrayInputStream(ipv6UserBytes);
|
|
|
|
|
builder.loadDataFilePrivateV6(ipv6UserInputStream);
|
|
|
|
|
}
|
|
|
|
|
}else if ("SINGLE".equals(CommonConfig.CLUSTER_OR_SINGLE)){
|
|
|
|
|
byte[] ipv4BuiltBytes = FileByteUtils.getFileBytes(CommonConfig.DOWNLOAD_PATH + "ip_v4_built_in.mmdb");
|
|
|
|
|
if (ipv4BuiltBytes!=null){
|
|
|
|
|
InputStream ipv4BuiltInputStream = new ByteArrayInputStream(ipv4BuiltBytes);
|
|
|
|
|
builder.loadDataFileV4(ipv4BuiltInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv6BuiltBytes = FileByteUtils.getFileBytes(CommonConfig.DOWNLOAD_PATH + "ip_v6_built_in.mmdb");
|
|
|
|
|
if (ipv6BuiltBytes!=null){
|
|
|
|
|
InputStream ipv6BuiltInputStream = new ByteArrayInputStream(ipv6BuiltBytes);
|
|
|
|
|
builder.loadDataFileV6(ipv6BuiltInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv4UserBytes = FileByteUtils.getFileBytes(CommonConfig.DOWNLOAD_PATH + "ip_v4_user_defined.mmdb");
|
|
|
|
|
if (ipv4UserBytes!=null){
|
|
|
|
|
InputStream ipv4UserInputStream = new ByteArrayInputStream(ipv4UserBytes);
|
|
|
|
|
builder.loadDataFilePrivateV4(ipv4UserInputStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ipv6UserBytes = FileByteUtils.getFileBytes(CommonConfig.DOWNLOAD_PATH + "ip_v6_user_defined.mmdb");
|
|
|
|
|
if (ipv6UserBytes!=null){
|
|
|
|
|
InputStream ipv6UserInputStream = new ByteArrayInputStream(ipv6UserBytes);
|
|
|
|
|
builder.loadDataFilePrivateV6(ipv6UserInputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ipLookup = builder.build();
|
|
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
LOG.error("加载失败",e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void updateIpLook(Map<String, byte[]> knowledgeFileCache){
|
|
|
|
|
try{
|
|
|
|
|
IpLookupV2.Builder builder = new IpLookupV2.Builder(false);
|
|
|
|
|
ipLookup= builder.loadDataFileV4(new ByteArrayInputStream(knowledgeFileCache.get("ip_v4_built_in.mmdb")))
|
|
|
|
|
.loadDataFileV6(new ByteArrayInputStream(knowledgeFileCache.get("ip_v6_built_in.mmdb")))
|
|
|
|
|
.loadDataFilePrivateV4(new ByteArrayInputStream(knowledgeFileCache.get("ip_v4_user_defined.mmdb")))
|
|
|
|
|
.loadDataFilePrivateV6(new ByteArrayInputStream(knowledgeFileCache.get("ip_v6_user_defined.mmdb")))
|
|
|
|
|
.build();
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
LOG.error("加载失败",e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-07-29 10:02:31 +08:00
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2021-08-18 19:15:49 +08:00
|
|
|
System.out.println(ipLookup.countryLookup("49.7.115.37"));
|
2021-08-16 18:24:13 +08:00
|
|
|
|
2022-02-11 16:06:24 +08:00
|
|
|
String ips = "182.168.50.23,182.168.50.45,182.168.56.9,182.168.56.8,92.168.50.58,19.168.56.7,12.168.56.6,2.168.50.40,1.168.50.19,9.168.50.6,2.168.50.4,192.168.56.17,192.168.50.27,192.168.50.26,192.168.50.18,192.168.56.3,192.168.56.10";
|
|
|
|
|
for (String ip:ips.split(",")){
|
|
|
|
|
System.out.println(ip+"--"+ipLookup.countryLookup(ip));
|
|
|
|
|
}
|
2021-08-16 18:24:13 +08:00
|
|
|
|
2021-07-29 10:02:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|