diff options
| author | doufenghu <[email protected]> | 2023-08-25 00:49:31 +0800 |
|---|---|---|
| committer | doufenghu <[email protected]> | 2023-08-25 00:49:31 +0800 |
| commit | a6ce86f13e65ce9db0697604cd5ed7456cc273c1 (patch) | |
| tree | f8394df86d330c2a8c21c477d246c81f0ae6bd3b | |
| parent | 8d8cb9997e314f1a6b0660fd92372679860f612f (diff) | |
[improve][IPLookupV2&AsnLookup] 抽取接口与抽象类,完善单元测试用例
| -rw-r--r-- | src/main/java/com/geedgenetworks/utils/AbstractDatabaseReader.java | 106 | ||||
| -rw-r--r-- | src/main/java/com/geedgenetworks/utils/AsnLookup.java | 116 | ||||
| -rw-r--r-- | src/main/java/com/geedgenetworks/utils/IPLocator.java (renamed from src/main/java/com/geedgenetworks/utils/AbstractIpLookup.java) | 163 | ||||
| -rw-r--r-- | src/main/java/com/geedgenetworks/utils/IpLookupV2.java | 72 | ||||
| -rw-r--r-- | src/test/java/com/geedgenetworks/test/IpLookupV2Test.java | 121 |
5 files changed, 272 insertions, 306 deletions
diff --git a/src/main/java/com/geedgenetworks/utils/AbstractDatabaseReader.java b/src/main/java/com/geedgenetworks/utils/AbstractDatabaseReader.java new file mode 100644 index 0000000..28be192 --- /dev/null +++ b/src/main/java/com/geedgenetworks/utils/AbstractDatabaseReader.java @@ -0,0 +1,106 @@ +package com.geedgenetworks.utils; + +import com.google.common.collect.ObjectArrays; +import com.maxmind.db.CHMCache; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.List; + +/** + * @ClassName AbstractIpLookup + * @Description 对IP定位查找抽象类,规定可执行方法 + * @Author darnell + * @Date 2018-10-9 19:20 + * @Version 1.0 + **/ +public abstract class AbstractDatabaseReader { + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + public static final String UNKNOWN = ""; + public static final String LOCATION_SEPARATOR = "."; + public static final String LAT_LNG_SEPARATOR = ","; + + public static final String DEFAULT_SEPARATOR = ","; + public static final String OBJECT_SEPARATOR = "\t"; + public static final String PRIVATE_IP = "Private IP"; + + protected static final String DEFAULT_DATABASE_PATH = "dat"; + protected static final String DEFAULT_DB_IP_PUBLIC = "ip.mmdb"; + protected static final String DEFAULT_DB_IP_PUBLIC_V4 = "ip_v4_built_in.mmdb"; + protected static final String DEFAULT_DB_IP_PUBLIC_V6 = "ip_v6.mmdb"; + protected static final String DEFAULT_DB_IP_PRIVATE = "ip_private.mmdb"; + protected static final String DEFAULT_DB_IP_PRIVATE_V4 = "ip_private_v4.mmdb"; + protected static final String DEFAULT_DB_IP_PRIVATE_V6 = "ip_private_v6.mmdb"; + + protected static final String DEFAULT_DB_ASN_PUBLIC = "asn.mmdb"; + protected static final String DEFAULT_DB_ASN_PUBLIC_V4 = "asn_v4.mmdb"; + protected static final String DEFAULT_DB_ASN_PUBLIC_V6 = "asn_v6.mmdb"; + protected static final String DEFAULT_DB_ASN_PRIVATE = "asn_private.mmdb"; + protected static final String DEFAULT_DB_ASN_PRIVATE_V4 = "asn_private_v4.mmdb"; + protected static final String DEFAULT_DB_ASN_PRIVATE_V6 = "asn_private_v6.mmdb"; + + public enum Type { + PRIVATE, PUBLIC + } + public enum IpVersion { + IPv4, IPv6 + } + + protected GalaxyDataBaseReader constructDatabaseReader(String filePath, InputStream stream) { + try { + if (StringUtil.isNotBlank(filePath) && new File(filePath).exists()) { + return constructDatabaseReader(new File(filePath)); + } else if (stream != null) { + return constructDatabaseReader(stream); + } else { + return null; + } + } catch (IOException ie) { + logger.error("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " + ie); + } + throw new UnsupportedOperationException("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " ); + } + + protected GalaxyDataBaseReader constructDatabaseReader(File file) throws IOException { + return file.exists() ? new GalaxyDataBaseReader.Builder(file).withCache(new CHMCache()).build() : null; + } + + protected GalaxyDataBaseReader constructDatabaseReader(InputStream stream) throws IOException { + return stream != null ? new GalaxyDataBaseReader.Builder(stream).withCache(new CHMCache()).build() : null; + } + + protected void addIfNotEmpty(List<GalaxyDataBaseReader> dbReaders, GalaxyDataBaseReader dbReader) { + if (StringUtil.isNotEmpty(dbReader)) { + dbReaders.add(dbReader); + } + + } + + protected File[] buildMMDBFilesUseWorkDirectory(String... files) { + File[] dbFiles = ObjectArrays.newArray(File.class, files.length); + for (int i = 0; i < files.length; i++) { + dbFiles[i] = new File(System.getProperty("user.dir") + File.separator + DEFAULT_DATABASE_PATH + File.separator + files[i]); + } + return dbFiles; + } + + protected void updateFileIfNotExists(File[] files) { + for (File file : files) { + if (!file.exists()) { + URL url = IpLookupV2.class.getResource("/"+ DEFAULT_DATABASE_PATH + "/" + file.getName()); + if (url != null) { + file = new File(url.getPath()); + } + } + } + + } + + + + +} diff --git a/src/main/java/com/geedgenetworks/utils/AsnLookup.java b/src/main/java/com/geedgenetworks/utils/AsnLookup.java index bf7bba7..022999f 100644 --- a/src/main/java/com/geedgenetworks/utils/AsnLookup.java +++ b/src/main/java/com/geedgenetworks/utils/AsnLookup.java @@ -20,7 +20,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import static com.geedgenetworks.utils.AbstractIpLookup.UNKNOWN; +import static com.geedgenetworks.utils.AbstractDatabaseReader.UNKNOWN; /** * @ClassName AsnLookup @@ -29,28 +29,14 @@ import static com.geedgenetworks.utils.AbstractIpLookup.UNKNOWN; * @Date 2020-05-09 13:45 * @Version 1.0.3 **/ -public class AsnLookup { - - public enum Type { - PRIVATE, PUBLIC - } - protected static final String SEPARATOR = ","; - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - public final static String DEFAULT_DATABASE_PATH = "dat"; - private final static String DEFAULT_DB_ASN_PUBLIC = "asn.mmdb"; - private final static String DEFAULT_DB_ASN_PUBLIC_V4 = "asn_v4.mmdb"; - private final static String DEFAULT_DB_ASN_PUBLIC_V6 = "asn_v6.mmdb"; - private final static String DEFAULT_DB_ASN_PRIVATE = "asn_private.mmdb"; - private final static String DEFAULT_DB_ASN_PRIVATE_V4 = "asn_private_v4.mmdb"; - private final static String DEFAULT_DB_ASN_PRIVATE_V6 = "asn_private_v6.mmdb"; - private final Map<String, List<GalaxyDataBaseReader>> availableDBReaders = Maps.newHashMap(); - private static GalaxyDataBaseReader publicDatabaseReader; - private static GalaxyDataBaseReader publicV4DatabaseReader; - private static GalaxyDataBaseReader publicV6DatabaseReader; - - private static GalaxyDataBaseReader privateDatabaseReader; - private static GalaxyDataBaseReader privateV4DatabaseReader; - private static GalaxyDataBaseReader privateV6DatabaseReader; +public class AsnLookup extends AbstractDatabaseReader { + private Map<String, List<GalaxyDataBaseReader>> availableDBReaders = Maps.newHashMap(); + private GalaxyDataBaseReader publicDatabaseReader; + private GalaxyDataBaseReader publicV4DatabaseReader; + private GalaxyDataBaseReader publicV6DatabaseReader; + private GalaxyDataBaseReader privateDatabaseReader; + private GalaxyDataBaseReader privateV4DatabaseReader; + private GalaxyDataBaseReader privateV6DatabaseReader; synchronized void init(Builder builder) { initializeDatabaseReaders(builder); @@ -84,50 +70,6 @@ public class AsnLookup { } } - private File[] buildMMDBFilesUseWorkDirectory(String... files) { - File[] dbFiles = ObjectArrays.newArray(File.class, files.length); - for (int i = 0; i < files.length; i++) { - dbFiles[i] = new File(System.getProperty("user.dir") + File.separator + DEFAULT_DATABASE_PATH + File.separator + files[i]); - } - return dbFiles; - } - - private void updateFileIfNotExists(File[] files) { - for (File file : files) { - if (!file.exists()) { - URL url = IpLookupV2.class.getResource("/"+ DEFAULT_DATABASE_PATH + "/" + file.getName()); - if (url != null) { - file = new File(url.getPath()); - } - } - } - - } - - - private GalaxyDataBaseReader constructDatabaseReader(String filePath, InputStream stream) { - try { - if (StringUtil.isNotBlank(filePath) && new File(filePath).exists()) { - return constructDatabaseReader(new File(filePath)); - } else if (stream != null) { - return constructDatabaseReader(stream); - } else { - return null; - } - } catch (IOException ie) { - logger.error("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " + ie); - } - throw new UnsupportedOperationException("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " ); - } - - private GalaxyDataBaseReader constructDatabaseReader(File file) throws IOException { - return file.exists() ? new GalaxyDataBaseReader.Builder(file).withCache(new CHMCache()).build() : null; - } - - private GalaxyDataBaseReader constructDatabaseReader(InputStream stream) throws IOException { - return stream != null ? new GalaxyDataBaseReader.Builder(stream).withCache(new CHMCache()).build() : null; - } - private void initializeAvailableDatabaseReaders() { List<GalaxyDataBaseReader> ipv4DbReaders = Lists.newArrayList(); List<GalaxyDataBaseReader> ipv6DbReaders = Lists.newArrayList(); @@ -144,36 +86,14 @@ public class AsnLookup { addIfNotEmpty(ipv4DbReaders, publicDatabaseReader); addIfNotEmpty(ipv6DbReaders, publicDatabaseReader); - availableDBReaders.put(AbstractIpLookup.IpVersion.IPv4.name(), ipv4DbReaders); - availableDBReaders.put(AbstractIpLookup.IpVersion.IPv6.name(), ipv6DbReaders); - } - - private void addIfNotEmpty(List<GalaxyDataBaseReader> dbReaders, GalaxyDataBaseReader dbReader) { - if (StringUtil.isNotEmpty(dbReader)) { - dbReaders.add(dbReader); - } - + availableDBReaders.put(AbstractDatabaseReader.IpVersion.IPv4.name(), ipv4DbReaders); + availableDBReaders.put(AbstractDatabaseReader.IpVersion.IPv6.name(), ipv6DbReaders); } - private AsnLookup(AsnLookup.Builder builder) { init(builder); } - private GalaxyDataBaseReader buildDataReader(File database) throws IOException { - if (database.exists()) { - return new GalaxyDataBaseReader.Builder(database).withCache(new CHMCache()).build(); - } else { - return null; - } - } - private GalaxyDataBaseReader buildDataReader(InputStream stream) throws IOException { - if (stream != null) { - return new GalaxyDataBaseReader.Builder(stream).withCache(new CHMCache()).build(); - } else { - return null; - } - } /** * 加载mmdb数据字典文件,内部生成mmdb动态库。 @@ -207,7 +127,6 @@ public class AsnLookup { InputStream privateAsnV6DatabaseStream; boolean isDefaultDB; - /** * * @param isDefaultDB <code>false</code> 手动指定IP库路径 <code>true</code> 使用默认路径 @@ -298,8 +217,6 @@ public class AsnLookup { return this; } - - /** * * @param privateAsnV4DatabaseFile 加载ASN IPv4 database文件 @@ -348,7 +265,6 @@ public class AsnLookup { return new AsnLookup(this); } - } @@ -362,7 +278,6 @@ public class AsnLookup { return StringUtil.setDefaultIfEmpty(response.getAsn(), UNKNOWN).toString() ; - } public String asnLookupOrganization(String ip) { @@ -378,7 +293,7 @@ public class AsnLookup { if (StringUtil.isEmpty(response) || response.isPrivateIP()) { return UNKNOWN; } - return Joiner.on(SEPARATOR).useForNull("").join(response.getCountryCode(), response.getOrganization()); + return Joiner.on(DEFAULT_SEPARATOR).useForNull("").join(response.getCountryCode(), response.getOrganization()); } @@ -391,17 +306,16 @@ public class AsnLookup { if (StringUtil.isEmpty(response) || response.isPrivateIP()) { return UNKNOWN; } - return Joiner.on(SEPARATOR).useForNull("").join(response.getAsn(), response.getCountryCode(), response.getOrganization()); + return Joiner.on(DEFAULT_SEPARATOR).useForNull("").join(response.getAsn(), response.getCountryCode(), response.getOrganization()); } - private LocationResponse getResponse(String ip) { try { ip = Optional.ofNullable(ip).orElse("").trim(); InetAddress ipAddress = InetAddress.getByName(ip); if (ipAddress instanceof Inet4Address) { //Iterate through each 'dbReader' and execute them in the following order: private v4 -> private -> public v4 -> public. - for (GalaxyDataBaseReader reader : availableDBReaders.get(AbstractIpLookup.IpVersion.IPv4.name())) { + for (GalaxyDataBaseReader reader : availableDBReaders.get(AbstractDatabaseReader.IpVersion.IPv4.name())) { LocationResponse response = reader.locationV2(ipAddress); if(StringUtil.isNotEmpty(response)) { return response; @@ -409,7 +323,7 @@ public class AsnLookup { } } else if (ipAddress instanceof Inet6Address) { //Iterate through each 'dbReader' and execute them in the following order: private v6 -> private -> public v6 -> public. - for(GalaxyDataBaseReader reader : availableDBReaders.get(AbstractIpLookup.IpVersion.IPv6.name())) { + for(GalaxyDataBaseReader reader : availableDBReaders.get(AbstractDatabaseReader.IpVersion.IPv6.name())) { LocationResponse response = reader.locationV2(ipAddress); if(StringUtil.isNotEmpty(response)) { return response; diff --git a/src/main/java/com/geedgenetworks/utils/AbstractIpLookup.java b/src/main/java/com/geedgenetworks/utils/IPLocator.java index 1762bfa..445e91b 100644 --- a/src/main/java/com/geedgenetworks/utils/AbstractIpLookup.java +++ b/src/main/java/com/geedgenetworks/utils/IPLocator.java @@ -1,73 +1,22 @@ package com.geedgenetworks.utils; -import com.google.common.collect.Maps; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; - -/** - * @ClassName AbstractIpLookup - * @Description 对IP定位查找抽象类,规定可执行方法 - * @Author darnell - * @Date 2018-10-9 19:20 - * @Version 1.0 - **/ -public abstract class AbstractIpLookup { - protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - public static final String UNKNOWN = ""; - public static final String LOCATION_SEPARATOR = "."; - public static final String LAT_LNG_SEPARATOR = ","; - public static final String OBJECT_SEPARATOR = "\t"; - public static final String PRIVATE_IP = "Private IP"; - - protected static final String DEFAULT_DATABASE_PATH = "dat"; - protected static final String DEFAULT_DB_IP_PUBLIC = "ip.mmdb"; - protected static final String DEFAULT_DB_IP_PUBLIC_V4 = "ip_v4_built_in.mmdb"; - protected static final String DEFAULT_DB_IP_PUBLIC_V6 = "ip_v6.mmdb"; - protected static final String DEFAULT_DB_IP_PRIVATE = "ip_private.mmdb"; - protected static final String DEFAULT_DB_IP_PRIVATE_V4 = "ip_private_v4.mmdb"; - protected static final String DEFAULT_DB_IP_PRIVATE_V6 = "ip_private_v6.mmdb"; - public enum Type { - PRIVATE, PUBLIC - } - public enum IpVersion { - IPv4, IPv6 - } +public interface IPLocator { /** * 给定IP库文件路径,获取国家代码 * @param ip ip地址 * @return String 国家代码 */ - public abstract String countryLookup(String ip); + String countryLookup(String ip); /** * - * 获取到城市的3级定位信息 - * @param ip ip地址,点分十进制格式,ipv4 - * @return String 国家代码,省代码,城市代码 - */ - public abstract String cityLookupDetail(String ip); - - - /** - * - * 获取到区县的4级定位信息 - * @param ip ip地址,点分十进制格式,ipv4 - * @return String 国家代码,省,城市,区/县 - */ - public abstract String administrativeAreaLookupDetail(String ip); - - /** + * 获取所属省/州信息 * - * 获取位置信息明细,支持7级 - * @param ip ip地址,点分十进制格式,ipv4 - * @return String 国家代码,省,城市,区/县,街道 + * @param ip ip地址 + * @return String 州/省代码 */ - public abstract String locationLookupDetail(String ip); - + String provinceLookup(String ip); /** * @@ -76,16 +25,16 @@ public abstract class AbstractIpLookup { * @param ip ip地址 * @return String 城市代码 */ - public abstract String cityLookup(String ip); + String cityLookup(String ip); /** * - * 获取所属省/州信息 - * - * @param ip ip地址 - * @return String 州/省代码 + * 获取到区县的4级定位信息 + * @param ip ip地址,点分十进制格式,ipv4 + * @return String 国家代码,省,城市,区/县 */ - public abstract String provinceLookup(String ip); + String administrativeAreaLookupDetail(String ip); + /** * @@ -93,7 +42,7 @@ public abstract class AbstractIpLookup { * @param ip ip地址 * @return String 运营商 */ - public abstract String ispLookup(String ip); + String ispLookup(String ip); /** * @@ -101,7 +50,9 @@ public abstract class AbstractIpLookup { * @param ip ip地址 * @return String 组织 */ - public abstract String organizationLookup(String ip); + String organizationLookup(String ip); + + /** * @@ -110,9 +61,7 @@ public abstract class AbstractIpLookup { * @param ip ip地址 * @return String 纬度 经度 */ - public abstract String latLngLookup(String ip); - - + String latLngLookup(String ip); /** * @@ -121,54 +70,38 @@ public abstract class AbstractIpLookup { * @param ip ip地址 * @return String 省代码 纬度 经度 */ - public abstract String cityLatLngLookup(String ip); + String cityLatLngLookup(String ip); /** * - * 获取AS号,默认通过IP定位库获取ASN号 - * @param ip ip地址 - * @return String asn号 - */ - public abstract String asnLookup(String ip); - - /** - * - * 获取IP 所属AS的组织 - * @param ip ip地址 - * @return String asn号 - */ - public abstract String asnLookupOrganization(String ip); - - /** - * - * 获取IP 所属AS的组织+国家代码 - * @param ip ip地址 - * @return String asn号 + * 获取到城市的3级定位信息 + * @param ip ip地址,点分十进制格式,ipv4 + * @return String 国家代码,省代码,城市代码 */ - public abstract String asnLookupInfo(String ip); + String cityLookupDetail(String ip); /** * - * 获取AS号与组织信息 - * @param ip ip地址 - * @return asn号 组织名称 + * 获取位置信息明细,支持7级 + * @param ip ip地址,点分十进制格式,ipv4 + * @return String 国家代码,省,城市,区/县,街道 */ - public abstract String asnLookupDetail(String ip); + String locationLookupDetail(String ip); /** * Get A two-character ISO 3166-1 country code for the country associated with the location. * @param ip ip地址 * @return country code */ - public abstract String countryCodeLookup(String ip); + String countryCodeLookup(String ip); /** * A string of up to three characters containing the region-portion of the ISO 3166-2 code for the first level region associated with the IP address. * @param ip ip地址 * @return First Level Region Code */ - public abstract String regionCodeLookup(String ip); + String regionCodeLookup(String ip); /** * This method to return all fields for CSV. @@ -178,28 +111,60 @@ public abstract class AbstractIpLookup { * @param ip * @return */ - public abstract String infoLookupToCSV(String ip); + String infoLookupToCSV(String ip); /** * This method to return all fields for JSON String. * @param ip * @return */ - public abstract String infoLookupToJSONString(String ip); + String infoLookupToJSONString(String ip); /** * This method to return all fields for JSON Object. * @param ip * @return */ - public abstract Object infoLookupToJSON(String ip); + Object infoLookupToJSON(String ip); /** * This method returns all fields for the MMDB Response Object.. * @param ip * @return */ - public abstract Object infoLookup(String ip); + Object infoLookup(String ip); + + /** + * + * 获取AS号,默认通过IP定位库获取ASN号 + * @param ip ip地址 + * @return String asn号 + */ + String asnLookup(String ip); + /** + * + * 获取IP 所属AS的组织 + * @param ip ip地址 + * @return String asn号 + */ + String asnLookupOrganization(String ip); + + + /** + * + * 获取AS号与组织信息 + * @param ip ip地址 + * @return asn号 组织名称 + */ + String asnLookupDetail(String ip); + + /** + * + * 获取IP 所属AS的组织+国家代码 + * @param ip ip地址 + * @return String asn号 + */ + String asnLookupInfo(String ip); } diff --git a/src/main/java/com/geedgenetworks/utils/IpLookupV2.java b/src/main/java/com/geedgenetworks/utils/IpLookupV2.java index f63cfbb..b8eacb5 100644 --- a/src/main/java/com/geedgenetworks/utils/IpLookupV2.java +++ b/src/main/java/com/geedgenetworks/utils/IpLookupV2.java @@ -6,32 +6,24 @@ import com.alibaba.fastjson2.JSONWriter; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.ObjectArrays; -import com.maxmind.db.CHMCache; import com.geedgenetworks.domain.LocationResponse; - - import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; -import java.net.URL; import java.util.*; - /** * @ClassName IpLookupV2 - * @Description The IPLookupv2 class enriches information with geographic fields, given an ip address. It works with User-defined Maxmind's GeoIP binary database. + * @Description The IpLookupV2 class enriches information with geographic fields, given an ip address. It works with User-defined Maxmind's GeoIP binary database. * @Author darnell * @Date 2023-08-22 20:00 Geedge Networks * @Version 1.2 **/ -public final class IpLookupV2 extends AbstractIpLookup { - private AsnLookup asnLookup; - private final Map<String, List<GalaxyDataBaseReader>> availableDBReaders = Maps.newHashMap(); - +public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocator{ + private Map<String, List<GalaxyDataBaseReader>> availableDBReaders = Maps.newHashMap(); private GalaxyDataBaseReader publicDatabaseReader; private GalaxyDataBaseReader publicV4DatabaseReader;; private GalaxyDataBaseReader publicV6DatabaseReader;; @@ -39,6 +31,8 @@ public final class IpLookupV2 extends AbstractIpLookup { private GalaxyDataBaseReader privateV4DatabaseReader;; private GalaxyDataBaseReader privateV6DatabaseReader;; + private AsnLookup asnLookup; + private IpLookupV2(Builder builder) { init(builder); } @@ -46,8 +40,8 @@ public final class IpLookupV2 extends AbstractIpLookup { // Initialization of variables synchronized void init(Builder builder) { initializeDatabaseReaders(builder); - initializeAsnLookup(builder); initializeAvailableDatabaseReaders(); + initializeAsnLookup(builder); } private void initializeDatabaseReaders(Builder builder) { @@ -121,58 +115,6 @@ public final class IpLookupV2 extends AbstractIpLookup { } - private void addIfNotEmpty(List<GalaxyDataBaseReader> dbReaders, GalaxyDataBaseReader dbReader) { - if (StringUtil.isNotEmpty(dbReader)) { - dbReaders.add(dbReader); - } - - } - - private File[] buildMMDBFilesUseWorkDirectory(String... files) { - File[] dbFiles = ObjectArrays.newArray(File.class, files.length); - for (int i = 0; i < files.length; i++) { - dbFiles[i] = new File(System.getProperty("user.dir") + File.separator + DEFAULT_DATABASE_PATH + File.separator + files[i]); - } - return dbFiles; - } - - private void updateFileIfNotExists(File[] files) { - for (File file : files) { - if (!file.exists()) { - URL url = IpLookupV2.class.getResource("/"+ DEFAULT_DATABASE_PATH + "/" + file.getName()); - if (url != null) { - file = new File(url.getPath()); - } - } - } - - } - - - private GalaxyDataBaseReader constructDatabaseReader(String filePath, InputStream stream) { - try { - if (StringUtil.isNotBlank(filePath) && new File(filePath).exists()) { - return constructDatabaseReader(new File(filePath)); - } else if (stream != null) { - return constructDatabaseReader(stream); - } else { - return null; - } - } catch (IOException ie) { - logger.error("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " + ie); - } - throw new UnsupportedOperationException("Unsupported GeoIP2 *.mmdb database: expected either File path or Stream. " ); - } - - private GalaxyDataBaseReader constructDatabaseReader(File file) throws IOException { - return file.exists() ? new GalaxyDataBaseReader.Builder(file).withCache(new CHMCache()).build() : null; - } - - private GalaxyDataBaseReader constructDatabaseReader(InputStream stream) throws IOException { - return stream != null ? new GalaxyDataBaseReader.Builder(stream).withCache(new CHMCache()).build() : null; - } - - /** * 加载mmdb数据字典文件,内部生成mmdb动态库。 @@ -372,7 +314,6 @@ public final class IpLookupV2 extends AbstractIpLookup { return this; } - /** * * @param asnDatabaseFile 加载ASN IPv4 database文件 @@ -673,7 +614,6 @@ public final class IpLookupV2 extends AbstractIpLookup { return StringUtil.setDefaultIfEmpty(response.getOrganization(), UNKNOWN).toString() ; } - @Override public String latLngLookup(String ip) { LocationResponse response = getResponse(ip); diff --git a/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java b/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java index 8ff7d93..8e969e6 100644 --- a/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java +++ b/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java @@ -1,10 +1,12 @@ package com.geedgenetworks.test; import com.alibaba.fastjson2.JSONObject; -import com.geedgenetworks.utils.AbstractIpLookup; +import com.geedgenetworks.utils.AbstractDatabaseReader; +import com.geedgenetworks.utils.AsnLookup; import com.geedgenetworks.utils.IpLookupV2; import com.maxmind.db.Network; import org.apache.log4j.Logger; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -23,8 +25,16 @@ public class IpLookupV2Test { @Rule public ExpectedException expectedEx = ExpectedException.none(); - IpLookupV2 ipLookupA = getIpLookupByDatabase(); - IpLookupV2 ipLookup = getIpLookupByStream(); + IpLookupV2 ipLookupByDatabase; + IpLookupV2 ipLookupByStream; + AsnLookup asnLookup; + + @Before + public void begin() { + ipLookupByDatabase = getIpLookupByDatabase(); + ipLookupByStream = getIpLookupByStream(); + asnLookup = getAsnLookup(); + } private IpLookupV2 getIpLookupByDatabase() { return new IpLookupV2.Builder(false) @@ -40,7 +50,7 @@ public class IpLookupV2Test { private IpLookupV2 getIpLookupByStream() { IpLookupV2 ipLookupV2 = null; try { - ipLookupV2 = new IpLookupV2.Builder(true) + ipLookupV2 = new IpLookupV2.Builder(false) .loadDataFileV4(new FileInputStream(new File("dat/ip_v4_built_in.mmdb"))) .loadDataFilePrivateV4(new FileInputStream(new File("dat/ip_v4_user_defined.mmdb"))) .loadDataFileV6(new FileInputStream(new File("dat/ip_v6_built_in.mmdb"))) @@ -54,13 +64,26 @@ public class IpLookupV2Test { return ipLookupV2; } + private AsnLookup getAsnLookup() { + AsnLookup asnLookup = null; + try { + asnLookup = new AsnLookup.Builder(false) + .loadDataFileV4(new FileInputStream(new File("dat/asn_v4.mmdb"))) + .loadDataFileV6(new FileInputStream(new File("dat/asn_v6.mmdb"))) + .build(); + }catch (FileNotFoundException e) { + logger.error(" File is not found:", e); + } + return asnLookup; + } + @Test public void testLoadPrivateIPv4MMDBFiles() { try { IpLookupV2 ipLookupV4 = new IpLookupV2.Builder(false) .loadDataFilePrivateV4(new FileInputStream(new File("dat/ip_v4_user_defined.mmdb"))) .build(); - assertEquals(ipLookupV4.getDbReaders().get(AbstractIpLookup.IpVersion.IPv4.name()).size(), 1); + assertEquals(ipLookupV4.getDbReaders().get(AbstractDatabaseReader.IpVersion.IPv4.name()).size(), 1); }catch (FileNotFoundException e) { logger.error(" File is not found:", e); } @@ -74,7 +97,7 @@ public class IpLookupV2Test { .loadDataFilePrivateV4(new FileInputStream(new File("dat/ip_v4_user_defined.mmdb"))) .loadDataFileV4(new FileInputStream(new File("dat/ip_v4_built_in.mmdb"))) .build(); - assertEquals(ipLookupV4.getDbReaders().get(AbstractIpLookup.IpVersion.IPv4.name()).size(), 2); + assertEquals(ipLookupV4.getDbReaders().get(AbstractDatabaseReader.IpVersion.IPv4.name()).size(), 2); } catch (FileNotFoundException e) { logger.error(" File is not found:", e); } @@ -83,55 +106,69 @@ public class IpLookupV2Test { @Test public void testLoadDefaultMMDBFiles() { - - IpLookupV2 tmpIpLookup = new IpLookupV2.Builder(true).build(); - logger.info(tmpIpLookup.cityLookup("120.221.155.223")); - + IpLookupV2 ipLookup = new IpLookupV2.Builder(true).build(); + assertEquals(ipLookup.getDbReaders().values().size(), 2); } @Test public void testPrivateIP() { - assertEquals(ipLookupA.countryLookup("192.168.10.123"), "Private IP"); - Object jsonObject = ipLookupA.infoLookupToJSON("120.221.155.223"); - logger.info(jsonObject); + assertEquals(ipLookupByDatabase.countryLookup("192.168.10.123"), "Private IP"); + Object jsonObject = ipLookupByDatabase.infoLookupToJSON("192.168.10.123"); + assertEquals(JSONObject.from(jsonObject).get("privateIP"), true); } @Test(expected=IllegalArgumentException.class) public void testIpLookupIllegalArguments(){ - ipLookup.countryLookup("116.178.179.336"); + ipLookupByStream.countryLookup("116.178.179.336"); } @Test(expected=IllegalArgumentException.class) public void testIPIsBlank() { - ipLookup.countryLookup(""); + ipLookupByStream.countryLookup(""); } @Test(expected=IllegalArgumentException.class) public void testIPIsNull() { - ipLookup.countryLookup(null); + ipLookupByStream.countryLookup(null); + } + + @Test + public void testIpLookupByStream() { + assertEquals(ipLookupByDatabase.countryLookup("120.221.155.223"), "China"); + assertEquals(ipLookupByStream.provinceLookup("120.221.155.223"), "Shandong"); + assertEquals(ipLookupByStream.cityLookup("120.221.155.223"), "Other"); + assertEquals(ipLookupByStream.cityLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 3); + assertEquals(ipLookupByStream.locationLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 7); + assertEquals(ipLookupByStream.administrativeAreaLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 4); + assertEquals(ipLookupByStream.latLngLookup("120.221.155.223"), "36.0986,120.3719"); + assertEquals(ipLookupByStream.cityLatLngLookup("120.221.155.223").split( AbstractDatabaseReader.OBJECT_SEPARATOR).length, 2); + assertEquals(ipLookupByStream.ispLookup("120.221.155.223"), "-"); + assertEquals(ipLookupByStream.organizationLookup("120.221.155.223"), ""); + assertEquals(ipLookupByStream.infoLookupToCSV("120.221.155.223").split( AbstractDatabaseReader.OBJECT_SEPARATOR).length, 5); + assertEquals(JSONObject.from(ipLookupByStream.infoLookupToJSON("120.221.155.223")).get("country"),"China"); } @Test - public void testIpLookupOutput() { - assertEquals(ipLookup.countryLookup("120.221.155.223"), "China"); - assertEquals(ipLookup.provinceLookup("120.221.155.223"), "Shandong"); - assertEquals(ipLookup.cityLookup("120.221.155.223"), "Other"); - assertEquals(ipLookup.cityLookupDetail("120.221.155.223").split("\\" + AbstractIpLookup.LOCATION_SEPARATOR).length, 3); - assertEquals(ipLookup.locationLookupDetail("120.221.155.223").split("\\" + AbstractIpLookup.LOCATION_SEPARATOR).length, 7); - assertEquals(ipLookup.administrativeAreaLookupDetail("120.221.155.223").split("\\" + AbstractIpLookup.LOCATION_SEPARATOR).length, 4); - assertEquals(ipLookup.latLngLookup("120.221.155.223"), "36.0986,120.3719"); - assertEquals(ipLookup.cityLatLngLookup("120.221.155.223").split( AbstractIpLookup.OBJECT_SEPARATOR).length, 2); - assertEquals(ipLookup.ispLookup("120.221.155.223"), "-"); - assertEquals(ipLookup.organizationLookup("120.221.155.223"), ""); - assertEquals(ipLookup.infoLookupToCSV("120.221.155.223").split( AbstractIpLookup.OBJECT_SEPARATOR).length, 5); - assertEquals(JSONObject.from(ipLookup.infoLookupToJSON("120.221.155.223")).get("country"),"China"); + public void testIpLookupByDatabase() { + assertEquals(ipLookupByDatabase.countryLookup("120.221.155.223"), "China"); + assertEquals(ipLookupByDatabase.provinceLookup("120.221.155.223"), "Shandong"); + assertEquals(ipLookupByDatabase.cityLookup("120.221.155.223"), "Other"); + assertEquals(ipLookupByDatabase.cityLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 3); + assertEquals(ipLookupByDatabase.locationLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 7); + assertEquals(ipLookupByDatabase.administrativeAreaLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 4); + assertEquals(ipLookupByDatabase.latLngLookup("120.221.155.223"), "36.0986,120.3719"); + assertEquals(ipLookupByDatabase.cityLatLngLookup("120.221.155.223").split( AbstractDatabaseReader.OBJECT_SEPARATOR).length, 2); + assertEquals(ipLookupByDatabase.ispLookup("120.221.155.223"), "-"); + assertEquals(ipLookupByDatabase.organizationLookup("120.221.155.223"), ""); + assertEquals(ipLookupByDatabase.infoLookupToCSV("120.221.155.223").split( AbstractDatabaseReader.OBJECT_SEPARATOR).length, 5); + assertEquals(JSONObject.from(ipLookupByDatabase.infoLookupToJSON("120.221.155.223")).get("country"),"China"); } @Test public void testOutputJson() { - logger.info(ipLookup.infoLookupToJSON("120.221.155.223")); - logger.info(ipLookup.infoLookupToJSON("2001:db8:3333:4444:5555:6666:7777:8888")); - logger.info(ipLookup.infoLookupToJSON("2001:db8::")); - logger.info(ipLookup.infoLookupToJSON("120.221.155.223")); + logger.info(ipLookupByStream.infoLookupToJSON("120.221.155.223")); + logger.info(ipLookupByStream.infoLookupToJSON("2001:db8:3333:4444:5555:6666:7777:8888")); + logger.info(ipLookupByStream.infoLookupToJSON("2001:db8::")); + logger.info(ipLookupByStream.infoLookupToJSON("120.221.155.223")); } @@ -150,18 +187,22 @@ public class IpLookupV2Test { @Test public void testUnknownIP() { - logger.info(ipLookup.infoLookupToJSON("255.244.255.254")); - logger.info(ipLookup.infoLookupToJSON("8.8.8.8")); + logger.info(ipLookupByStream.infoLookupToJSON("255.244.255.254")); + logger.info(ipLookupByStream.infoLookupToJSON("8.8.8.8")); } @Test public void testAsnLookupOutput() { - logger.info(ipLookup.asnLookupOrganization("8.8.8.8")); - logger.info(ipLookup.asnLookupDetail("8.8.8.8")); - logger.info(ipLookup.asnLookupOrganization("8.8.8.8")); - - + logger.info(ipLookupByStream.asnLookupOrganization("8.8.8.8")); + logger.info(ipLookupByStream.asnLookupDetail("8.8.8.8")); + logger.info(ipLookupByStream.asnLookupInfo("8.8.8.8")); + logger.info(ipLookupByStream.asnLookupOrganization("8.8.8.8")); + + logger.info(asnLookup.asnLookupOrganization("8.8.8.8")); + logger.info(asnLookup.asnLookupDetail("8.8.8.8")); + logger.info(asnLookup.asnLookupInfo("8.8.8.8")); + logger.info(asnLookup.asnLookupOrganization("8.8.8.8")); } |
