├── .gitignore
├── LICENSE
├── README.md
├── README_en.md
├── README_zh.md
├── pom.xml
└── src
├── main
└── java
│ └── net
│ └── ipip
│ └── ipdb
│ ├── AsnInfo.java
│ ├── BaseStation.java
│ ├── BaseStationInfo.java
│ ├── City.java
│ ├── CityInfo.java
│ ├── District.java
│ ├── DistrictInfo.java
│ ├── IDC.java
│ ├── IDCInfo.java
│ ├── IPFormatException.java
│ ├── InvalidDatabaseException.java
│ ├── MetaData.java
│ ├── NotFoundException.java
│ ├── Reader.java
│ ├── Risk.java
│ └── RiskInfo.java
└── test
└── java
└── net
└── ipip
└── ipdb
├── BaseStationTest.java
├── CityTest.java
├── DistrictTest.java
├── IDCTest.java
├── ReaderTest.java
└── RiskTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.war
15 | *.nar
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ipdb-java
2 | IPIP.net officially supported IP database ipdb format parsing library
3 |
4 | # Notice
5 |
6 | 请直接用github上源码包
7 | 报错请检查 fastjson 版本是否太低,请使用最新版本
8 |
9 | # Documents
10 | * [中文文档](https://github.com/ipipdotnet/ipdb-java/blob/master/README_zh.md)
11 | * [English Docs](https://github.com/ipipdotnet/ipdb-java/blob/master/README_en.md)
12 |
13 | # CopyRight
14 | [IPIP.net](https://www.ipip.net)
15 |
--------------------------------------------------------------------------------
/README_en.md:
--------------------------------------------------------------------------------
1 | # ipdb-java
2 | IPIP.net officially supported IP database ipdb format parsing library
3 |
--------------------------------------------------------------------------------
/README_zh.md:
--------------------------------------------------------------------------------
1 | # ipdb-java
2 | 用于解析 IPIP.net 提供的IPDB格式离线数据库
3 |
4 | # IPv4中国地级市精度试用版
5 | [IPDB Database](https://www.ipip.net/product/client.html)
6 |
7 | # 代码示例
8 | ## 地级市精度库
9 |
10 | try {
11 | // City类可用于IPDB格式的IPv4/IPv6定制版本
12 | City db = new City("/path/to/ipiptest.ipdb");
13 |
14 | // db.find(address, language) 返回索引数组
15 | System.out.println(Arrays.toString(db.find("1.1.1.1", "CN")));
16 |
17 | // db.findInfo(address, language) 返回 CityInfo 对象
18 | CityInfo info = db.findInfo("118.28.1.1", "CN");
19 | System.out.println(info);
20 |
21 | } catch (Exception e) {
22 | e.printStackTrace();
23 | }
24 |
25 |
26 | ### IPDB格式可支持的字段说明
27 |
28 | country_name : 国家名字
29 | region_name : 省名字
30 | city_name : 城市名字
31 | owner_domain : 所有者域名
32 | isp_domain : 运营商域名
33 | latitude : 纬度
34 | longitude : 经度
35 | timezone : 时区
36 | utc_offset : UTC时区
37 | china_admin_code : 中国行政区划代码
38 | idd_code : 国家电话号码前缀
39 | country_code : 国家2位代码
40 | continent_code : 大洲代码
41 | line : 线路,国内特色
42 | idc : IDC | VPN
43 | base_station : 基站 | WIFI
44 | country_code3 : 国家3位代码
45 | european_union : 是否为欧盟成员国: 1 | 0
46 | currency_code : 当前国家货币代码
47 | currency_name : 当前国家货币名称
48 | anycast : ANYCAST
49 | usage_type: 应用类型/应用场景
50 | district_name: 区县名称(仅中国,购买了区县级别)
51 | asn: ASNumber
52 | route: route prefix
53 |
54 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 | ipdb-sdk
7 | IPIP.net Support ipdb database file parser SDK
8 | https://github.com/ipipdotnet/ipdb-java
9 | net.ipip
10 | ipdb
11 | 1.5.0
12 | jar
13 |
14 |
15 | frk
16 | ipip.net
17 | frk@ipip.net
18 |
19 |
20 |
21 |
22 | scm:git:https://github.com/ipipdotnet/ipdb-java.git
23 | scm:git:https://github.com/ipipdotnet/ipdb-java.git
24 | https://github.com/ipipdotnet/ipdb-java
25 |
26 |
27 |
28 |
29 |
30 | org.apache.maven.plugins
31 | maven-compiler-plugin
32 |
33 | 1.8
34 | 1.8
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-jar-plugin
40 |
41 |
42 |
43 | true
44 | false
45 |
46 |
47 |
48 |
49 |
50 |
51 | org.apache.maven.plugins
52 | maven-source-plugin
53 | 2.1.2
54 |
55 |
56 | attach-sources
57 | verify
58 |
59 | jar-no-fork
60 |
61 |
62 |
63 |
64 |
65 | org.apache.maven.plugins
66 | maven-javadoc-plugin
67 | 2.9.1
68 |
69 | true
70 | UTF-8
71 | UTF-8
72 | UTF-8
73 | -Xdoclint:none
74 |
75 |
76 |
77 | attach-javadocs
78 |
79 | jar
80 |
81 |
82 |
83 |
84 |
85 |
86 | org.apache.maven.plugins
87 | maven-surefire-plugin
88 | 2.18
89 |
90 | 0
91 |
92 |
93 |
94 | org.apache.maven.plugins
95 | maven-release-plugin
96 | 2.4.1
97 |
98 |
99 |
100 | org.apache.maven.plugins
101 | maven-scm-plugin
102 | 1.8.1
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 | org.apache.maven.plugins
133 | maven-gpg-plugin
134 | 1.5
135 |
136 |
137 | sign-artifacts
138 | verify
139 |
140 | sign
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 | releases
152 | https://oss.sonatype.org/service/local/staging/deploy/maven2
153 |
154 |
155 |
156 | snapshots
157 | https://oss.sonatype.org/content/repositories/snapshots
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 | junit
167 | junit
168 | 4.9
169 | test
170 |
171 |
172 |
173 | com.alibaba
174 | fastjson
175 | 1.2.83
176 |
177 |
178 |
179 |
180 |
181 | The Apache License, Version 2.0
182 | http://www.apache.org/licenses/LICENSE-2.0.txt
183 |
184 |
185 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/AsnInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import com.alibaba.fastjson.annotation.JSONField;
4 |
5 | public class AsnInfo {
6 |
7 | public int ASN;
8 |
9 | @JSONField(name = "reg")
10 | public String Registry;
11 |
12 | @JSONField(name = "cc")
13 | public String Country;
14 |
15 | @JSONField(name = "net")
16 | public String NetName;
17 |
18 | @JSONField(name = "org")
19 | public String OrgName;
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/BaseStation.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class BaseStation {
10 |
11 | /**
12 | * @var Reader
13 | */
14 | private Reader reader;
15 |
16 | public BaseStation(String name) throws IOException,InvalidDatabaseException {
17 | this.reader = new Reader(name);
18 | }
19 |
20 | public BaseStation(InputStream in) throws IOException, InvalidDatabaseException {
21 | this.reader = new Reader(in);
22 | }
23 |
24 | public boolean reload(String name) {
25 | try {
26 | Reader r = new Reader(name);
27 | this.reader = r;
28 | } catch (Exception e) {
29 | return false;
30 | }
31 |
32 | return true;
33 | }
34 |
35 | public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
36 | return this.reader.find(addr, language);
37 | }
38 |
39 | public Map findMap(String addr, String language) throws IPFormatException, InvalidDatabaseException {
40 | String[] data = this.reader.find(addr, language);
41 | if (data == null) {
42 | return null;
43 | }
44 | Map m = new HashMap();
45 |
46 | String[] fields = this.reader.getSupportFields();
47 |
48 | for (int i = 0, l = data.length; i < l; i++) {
49 | m.put(fields[i], data[i]);
50 | }
51 |
52 | return m;
53 | }
54 |
55 | public BaseStationInfo findInfo(String addr, String language) throws IPFormatException, InvalidDatabaseException {
56 |
57 | String[] data = this.reader.find(addr, language);
58 | if (data == null) {
59 | return null;
60 | }
61 |
62 | return new BaseStationInfo(data);
63 | }
64 |
65 | public boolean isIPv4() {
66 | return this.reader.isIPv4();
67 | }
68 |
69 | public boolean isIPv6() {
70 | return this.reader.isIPv6();
71 | }
72 |
73 | public String fields() {
74 | return Arrays.toString(this.reader.getSupportFields());
75 | }
76 |
77 | public int buildTime() {
78 | return this.reader.getBuildUTCTime();
79 | }
80 |
81 | public String languages() {
82 | return this.reader.getSupportLanguages();
83 | }
84 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/BaseStationInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | public class BaseStationInfo {
4 |
5 | private String[] data;
6 |
7 | public BaseStationInfo(String[] data) {
8 | this.data = data;
9 | }
10 |
11 | public String getCountryName() {
12 | return this.data[0];
13 | }
14 |
15 | public String getRegionName() {
16 | return this.data[1];
17 | }
18 |
19 | public String getCityName() {
20 | return this.data[2];
21 | }
22 |
23 | public String getOwnerDomain() {
24 | return this.data[3];
25 | }
26 |
27 | public String getIspDomain() {
28 | return this.data[4];
29 | }
30 |
31 | public String getBaseStation() {
32 | return this.data[5];
33 | }
34 |
35 | public String toString() {
36 | StringBuffer sb = new StringBuffer();
37 |
38 | sb.append("country_name:");
39 | sb.append(this.getCountryName());
40 | sb.append("\n");
41 | sb.append("region_name:");
42 | sb.append(this.getRegionName());
43 | sb.append("\n");
44 | sb.append("city_name:");
45 | sb.append(this.getCityName());
46 | sb.append("\n");
47 | sb.append("owner_domain:");
48 | sb.append(this.getOwnerDomain());
49 | sb.append("\n");
50 | sb.append("isp_domain:");
51 | sb.append(this.getIspDomain());
52 | sb.append("\n");
53 | sb.append("base_station:");
54 | sb.append(this.getBaseStation());
55 |
56 | return sb.toString();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/City.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.InputStream;
4 | import java.io.IOException;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class City {
10 |
11 | /**
12 | * @var Reader
13 | */
14 | private Reader reader;
15 |
16 | public City(String name) throws IOException,InvalidDatabaseException {
17 | this.reader = new Reader(name);
18 | }
19 |
20 | public City(InputStream in) throws IOException,InvalidDatabaseException {
21 | this.reader = new Reader(in);
22 | }
23 |
24 | public boolean reload(String name) {
25 | try {
26 | Reader r = new Reader(name);
27 | this.reader = r;
28 | } catch (Exception e) {
29 | return false;
30 | }
31 |
32 | return true;
33 | }
34 |
35 | public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
36 | return this.reader.find(addr, language);
37 | }
38 |
39 | public Map findMap(String addr, String language) throws IPFormatException, InvalidDatabaseException {
40 | String[] data = this.reader.find(addr, language);
41 | if (data == null) {
42 | return null;
43 | }
44 | String[] fields = this.reader.getSupportFields();
45 | Map m = new HashMap();
46 | for (int i = 0, l = data.length; i < l; i++) {
47 | m.put(fields[i], data[i]);
48 | }
49 |
50 | return m;
51 | }
52 |
53 | public CityInfo findInfo(String addr, String language) throws IPFormatException, InvalidDatabaseException {
54 |
55 | Map data = this.findMap(addr, language);
56 | if (data == null) {
57 | return null;
58 | }
59 |
60 | return new CityInfo(data);
61 | }
62 |
63 | public CityInfo findCity(String addr, String language) throws IPFormatException, InvalidDatabaseException {
64 |
65 | Map data = this.findMap(addr, language);
66 | if (data == null) {
67 | return null;
68 | }
69 |
70 | return new CityInfo(data);
71 | }
72 |
73 | public int buildTime() {
74 | return this.reader.getBuildUTCTime();
75 | }
76 |
77 | public boolean isIPv4() {
78 | return this.reader.isIPv4();
79 | }
80 |
81 | public boolean isIPv6() {
82 | return this.reader.isIPv6();
83 | }
84 |
85 | public String fields() {
86 | return Arrays.toString(this.reader.getSupportFields());
87 | }
88 |
89 | public String languages() {
90 | return this.reader.getSupportLanguages();
91 | }
92 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/CityInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 |
5 | import java.util.Map;
6 |
7 | public class CityInfo {
8 |
9 | private Map M;
10 |
11 | public CityInfo(Map kv) {
12 | this.M = kv;
13 | }
14 |
15 | public String getCountryName() {
16 | if (this.M.size() > 0) {
17 | return this.M.get("country_name");
18 | }
19 | return null;
20 | }
21 |
22 | public String getRegionName() {
23 | if (this.M.size() > 0) {
24 | return this.M.get("region_name");
25 | } else {
26 | return null;
27 | }
28 | }
29 |
30 | public String getCityName() {
31 | if (this.M.size() > 0) {
32 | return this.M.get("city_name");
33 | } else {
34 | return null;
35 | }
36 | }
37 |
38 | public String getDistrictName() {
39 | if (this.M.size() > 0) {
40 | return this.M.get("district_name");
41 | } else {
42 | return null;
43 | }
44 | }
45 |
46 | public String getOwnerDomain() {
47 | if (this.M.size() > 0) {
48 | return this.M.get("owner_domain");
49 | } else {
50 | return null;
51 | }
52 | }
53 |
54 | public String getIspDomain() {
55 | if (this.M.size() > 0) {
56 | return this.M.get("isp_domain");
57 | } else {
58 | return null;
59 | }
60 | }
61 |
62 | public String getLatitude() {
63 | if (this.M.size() > 0) {
64 | return this.M.get("latitude");
65 | } else {
66 | return null;
67 | }
68 | }
69 |
70 | public String getLongitude() {
71 | if (this.M.size() > 0) {
72 | return this.M.get("longitude");
73 | } else {
74 | return null;
75 | }
76 | }
77 |
78 | public String getTimezone() {
79 | if (this.M.size() > 0) {
80 | return this.M.get("timezone");
81 | } else {
82 | return null;
83 | }
84 | }
85 |
86 | public String getUtcOffset() {
87 | if (this.M.size() > 0) {
88 | return this.M.get("utc_offset");
89 | } else {
90 | return null;
91 | }
92 | }
93 |
94 | public String getChinaRegionCode() {
95 | if (this.M.size() > 0) {
96 | return this.M.get("china_region_code");
97 | } else {
98 | return null;
99 | }
100 | }
101 |
102 | public String getChinaCityCode() {
103 | if (this.M.size() > 0) {
104 | return this.M.get("china_city_code");
105 | } else {
106 | return null;
107 | }
108 | }
109 |
110 | public String getChinaDistrictCode() {
111 | if (this.M.size() > 0) {
112 | return this.M.get("china_district_code");
113 | } else {
114 | return null;
115 | }
116 | }
117 |
118 | public String getChinaAdminCode() {
119 | if (this.M.size() > 0) {
120 | return this.M.get("china_admin_code");
121 | } else {
122 | return null;
123 | }
124 | }
125 |
126 | public String getIddCode() {
127 | if (this.M.size() > 0) {
128 | return this.M.get("idd_code");
129 | } else {
130 | return null;
131 | }
132 | }
133 |
134 | public String getCountryCode() {
135 | if (this.M.size() > 0) {
136 | return this.M.get("country_code");
137 | } else {
138 | return null;
139 | }
140 | }
141 |
142 | public String getContinentCode() {
143 | if (this.M.size() > 0) {
144 | return this.M.get("continent_code");
145 | } else {
146 | return null;
147 | }
148 | }
149 |
150 | public String getLine() {
151 | if (this.M.size() > 0) {
152 | return this.M.get("line");
153 | } else {
154 | return null;
155 | }
156 | }
157 |
158 | public String getIDC() {
159 | if (this.M.size() > 0) {
160 | return this.M.get("idc");
161 | } else {
162 | return null;
163 | }
164 | }
165 |
166 | public String getBaseStation() {
167 | if (this.M.size() > 0) {
168 | return this.M.get("base_station");
169 | } else {
170 | return null;
171 | }
172 | }
173 |
174 | public String getCountryCode3() {
175 | if (this.M.size() > 0) {
176 | return this.M.get("country_code3");
177 | } else {
178 | return null;
179 | }
180 | }
181 |
182 | public String getEuropeanUnion() {
183 | if (this.M.size() > 0) {
184 | return this.M.get("european_union");
185 | } else {
186 | return null;
187 | }
188 | }
189 |
190 | public String getCurrencyCode() {
191 | if (this.M.size() > 0) {
192 | return this.M.get("currency_code");
193 | } else {
194 | return null;
195 | }
196 | }
197 |
198 | public String getCurrencyName() {
199 | if (this.M.size() > 0) {
200 | return this.M.get("currency_name");
201 | } else {
202 | return null;
203 | }
204 | }
205 |
206 | public String getAnycast() {
207 | if (this.M.size() > 0) {
208 | return this.M.get("anycast");
209 | } else {
210 | return null;
211 | }
212 | }
213 |
214 | public String getRoute() {
215 | if (this.M.size() > 0) {
216 | return this.M.get("route");
217 | } else {
218 | return null;
219 | }
220 | }
221 |
222 | public String getASN() {
223 | if (this.M.size() > 0) {
224 | return this.M.get("asn");
225 | } else {
226 | return null;
227 | }
228 | }
229 |
230 | public String getAreaCode() {
231 | if (this.M.size() > 0) {
232 | return this.M.get("area_code");
233 | } else {
234 | return null;
235 | }
236 | }
237 |
238 | public String getUsageType() {
239 | if (this.M.size() > 0) {
240 | return this.M.get("usage_type");
241 | } else {
242 | return null;
243 | }
244 | }
245 |
246 | public DistrictInfo getDistrictInfo() {
247 | if (this.M.size() > 0) {
248 | String str = this.M.get("district_info");
249 | if (str == null) {
250 | return null;
251 | }
252 | Map info = JSONObject.parseObject(str, Map.class);
253 |
254 | String[] data = new String[8];
255 | data[0] = this.getCountryName();
256 | data[1] = this.getRegionName();
257 | data[2] = this.getCityName();
258 | data[3] = info.get("district_name");
259 | data[4] = info.get("china_admin_code");
260 | data[5] = info.get("covering_radius");
261 | data[6] = info.get("longitude");
262 | data[7] = info.get("latitude");
263 |
264 | return new DistrictInfo(data);
265 | }
266 | return null;
267 | }
268 |
269 | public AsnInfo[] getAsnInfos() {
270 |
271 | if (this.M.size() > 0) {
272 | String str = this.M.get("asn_info");
273 | if (str == null) {
274 | return null;
275 | }
276 | if (!str.startsWith("[")) {
277 | return null;
278 | }
279 |
280 | return JSONObject.parseObject(str, AsnInfo[].class);
281 | }
282 |
283 | return null;
284 | }
285 |
286 | public String toString() {
287 |
288 | StringBuffer sb = new StringBuffer();
289 |
290 | sb.append("country_name:");
291 | sb.append(this.getCountryName());
292 | sb.append("\n");
293 | sb.append("region_name:");
294 | sb.append(this.getRegionName());
295 | sb.append("\n");
296 | sb.append("city_name:");
297 | sb.append(this.getCityName());
298 | sb.append("\n");
299 | sb.append("owner_domain:");
300 | sb.append(this.getOwnerDomain());
301 | sb.append("\n");
302 | sb.append("isp_domain:");
303 | sb.append(this.getIspDomain());
304 | sb.append("\n");
305 | sb.append("latitude:");
306 | sb.append(this.getLatitude());
307 | sb.append("\n");
308 | sb.append("longitude:");
309 | sb.append(this.getLongitude());
310 | sb.append("\n");
311 |
312 | sb.append("timezone:");
313 | sb.append(this.getTimezone());
314 | sb.append("\n");
315 |
316 | sb.append("utc_offset:");
317 | sb.append(this.getUtcOffset());
318 | sb.append("\n");
319 |
320 | sb.append("china_region_code:");
321 | sb.append(this.getChinaRegionCode());
322 | sb.append("\n");
323 |
324 | sb.append("china_city_code:");
325 | sb.append(this.getChinaCityCode());
326 | sb.append("\n");
327 |
328 | sb.append("china_admin_code:");
329 | sb.append(this.getChinaAdminCode());
330 | sb.append("\n");
331 |
332 | sb.append("idd_code:");
333 | sb.append(this.getIddCode());
334 | sb.append("\n");
335 |
336 | sb.append("country_code:");
337 | sb.append(this.getCountryCode());
338 | sb.append("\n");
339 |
340 | sb.append("continent_code:");
341 | sb.append(this.getContinentCode());
342 | sb.append("\n");
343 |
344 | sb.append("idc:");
345 | sb.append(this.getIDC());
346 | sb.append("\n");
347 |
348 | sb.append("base_station:");
349 | sb.append(this.getBaseStation());
350 | sb.append("\n");
351 |
352 | sb.append("country_code3:");
353 | sb.append(this.getCountryCode3());
354 | sb.append("\n");
355 |
356 | sb.append("european_union:");
357 | sb.append(this.getEuropeanUnion());
358 | sb.append("\n");
359 |
360 | sb.append("currency_code:");
361 | sb.append(this.getCurrencyCode());
362 | sb.append("\n");
363 |
364 | sb.append("currency_name:");
365 | sb.append(this.getCurrencyName());
366 | sb.append("\n");
367 |
368 | sb.append("anycast:");
369 | sb.append(this.getAnycast());
370 |
371 | return sb.toString();
372 |
373 | }
374 | }
375 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/District.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class District {
10 |
11 | /**
12 | * @var Reader
13 | */
14 | private Reader reader;
15 |
16 | public District(String name) throws IOException,InvalidDatabaseException {
17 | this.reader = new Reader(name);
18 | }
19 |
20 | public District(InputStream in) throws IOException, InvalidDatabaseException {
21 | this.reader = new Reader(in);
22 | }
23 |
24 | public boolean reload(String name) {
25 | try {
26 | Reader r = new Reader(name);
27 | this.reader = r;
28 | } catch (Exception e) {
29 | return false;
30 | }
31 |
32 | return true;
33 | }
34 |
35 | public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
36 | return this.reader.find(addr, language);
37 | }
38 |
39 | public Map findMap(String addr, String language) throws IPFormatException, InvalidDatabaseException {
40 | String[] data = this.reader.find(addr, language);
41 | if (data == null) {
42 | return null;
43 | }
44 |
45 | Map m = new HashMap();
46 |
47 | String[] fields = this.reader.getSupportFields();
48 |
49 | for (int i = 0, l = data.length; i < l; i++) {
50 | m.put(fields[i], data[i]);
51 | }
52 |
53 | return m;
54 | }
55 |
56 | public DistrictInfo findInfo(String addr, String language) throws IPFormatException, InvalidDatabaseException {
57 |
58 | String[] data = this.reader.find(addr, language);
59 | if (data == null) {
60 | return null;
61 | }
62 |
63 | return new DistrictInfo(data);
64 | }
65 |
66 | public boolean isIPv4() {
67 | return this.reader.isIPv4();
68 | }
69 |
70 | public boolean isIPv6() {
71 | return this.reader.isIPv6();
72 | }
73 |
74 | public String fields() {
75 | return Arrays.toString(this.reader.getSupportFields());
76 | }
77 |
78 | public String languages() {
79 | return this.reader.getSupportLanguages();
80 | }
81 |
82 | public int buildTime() {
83 | return this.reader.getBuildUTCTime();
84 | }
85 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/DistrictInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | public class DistrictInfo {
4 |
5 | private String[] data;
6 |
7 | public DistrictInfo(String[] data) {
8 | this.data = data;
9 | }
10 |
11 | public String getCountryName() {
12 | return this.data[0];
13 | }
14 |
15 | public String getRegionName() {
16 | return this.data[1];
17 | }
18 |
19 | public String getCityName() {
20 | return this.data[2];
21 | }
22 |
23 | public String getDistrictName() {
24 | return this.data[3];
25 | }
26 |
27 | public String getChinaAdminCode() {
28 | return this.data[4];
29 | }
30 |
31 | public String getCoveringRadius() {
32 | return this.data[5];
33 | }
34 |
35 | public String getLatitude() {
36 | return this.data[7];
37 | }
38 |
39 | public String getLongitude() {
40 | return this.data[6];
41 | }
42 |
43 | public String toString() {
44 | StringBuffer sb = new StringBuffer();
45 |
46 | sb.append("country_name:");
47 | sb.append(this.getCountryName());
48 | sb.append("\n");
49 | sb.append("region_name:");
50 | sb.append(this.getRegionName());
51 | sb.append("\n");
52 | sb.append("city_name:");
53 | sb.append(this.getCityName());
54 | sb.append("\n");
55 | sb.append("district_name:");
56 | sb.append(this.getDistrictName());
57 | sb.append("\n");
58 | sb.append("china_admin_code:");
59 | sb.append(this.getChinaAdminCode());
60 | sb.append("\n");
61 | sb.append("covering_radius:");
62 | sb.append(this.getCoveringRadius());
63 | sb.append("\n");
64 | sb.append("latitude:");
65 | sb.append(this.getLatitude());
66 | sb.append("\n");
67 | sb.append("longitude:");
68 | sb.append(this.getLongitude());
69 |
70 | return sb.toString();
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/IDC.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class IDC {
10 |
11 | /**
12 | * @var Reader
13 | */
14 | private Reader reader;
15 |
16 | public IDC(String name) throws IOException,InvalidDatabaseException {
17 | this.reader = new Reader(name);
18 | }
19 |
20 | public IDC(InputStream in) throws IOException, InvalidDatabaseException {
21 | this.reader = new Reader(in);
22 | }
23 |
24 | public boolean reload(String name) {
25 | try {
26 | Reader r = new Reader(name);
27 | this.reader = r;
28 | } catch (Exception e) {
29 | return false;
30 | }
31 |
32 | return true;
33 | }
34 |
35 | public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
36 | return this.reader.find(addr, language);
37 | }
38 |
39 | public Map findMap(String addr, String language) throws IPFormatException, InvalidDatabaseException {
40 | String[] data = this.reader.find(addr, language);
41 | if (data == null) {
42 | return null;
43 | }
44 |
45 | Map m = new HashMap();
46 |
47 | String[] fields = this.reader.getSupportFields();
48 |
49 | for (int i = 0, l = data.length; i < l; i++) {
50 | m.put(fields[i], data[i]);
51 | }
52 |
53 | return m;
54 | }
55 |
56 | public IDCInfo findInfo(String addr, String language) throws IPFormatException, InvalidDatabaseException {
57 |
58 | String[] data = this.reader.find(addr, language);
59 | if (data == null) {
60 | return null;
61 | }
62 | return new IDCInfo(data);
63 | }
64 |
65 | public boolean isIPv4() {
66 | return this.reader.isIPv4();
67 | }
68 |
69 | public boolean isIPv6() {
70 | return this.reader.isIPv6();
71 | }
72 |
73 | public String fields() {
74 | return Arrays.toString(this.reader.getSupportFields());
75 | }
76 |
77 | public int buildTime() {
78 | return this.reader.getBuildUTCTime();
79 | }
80 |
81 | public String languages() {
82 | return this.reader.getSupportLanguages();
83 | }
84 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/IDCInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | public class IDCInfo {
4 |
5 | private String[] data;
6 |
7 | public IDCInfo(String[] data) {
8 | this.data = data;
9 | }
10 |
11 | public String getCountryName() {
12 | return this.data[0];
13 | }
14 |
15 | public String getRegionName() {
16 | return this.data[1];
17 | }
18 |
19 | public String getCityName() {
20 | return this.data[2];
21 | }
22 |
23 | public String getOwnerDomain() {
24 | return this.data[3];
25 | }
26 |
27 | public String getIspDomain() {
28 | return this.data[4];
29 | }
30 |
31 | public String getIDC() {
32 | return this.data[5];
33 | }
34 |
35 | public String toString() {
36 | StringBuffer sb = new StringBuffer();
37 |
38 | sb.append("country_name:");
39 | sb.append(this.getCountryName());
40 | sb.append("\n");
41 | sb.append("region_name:");
42 | sb.append(this.getRegionName());
43 | sb.append("\n");
44 | sb.append("city_name:");
45 | sb.append(this.getCityName());
46 | sb.append("\n");
47 | sb.append("owner_domain:");
48 | sb.append(this.getOwnerDomain());
49 | sb.append("\n");
50 | sb.append("isp_domain:");
51 | sb.append(this.getIspDomain());
52 | sb.append("\n");
53 | sb.append("idc:");
54 | sb.append(this.getIDC());
55 |
56 | return sb.toString();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/IPFormatException.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 |
4 | public class IPFormatException extends Exception {
5 |
6 | public IPFormatException(String name) {
7 | super(name);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/InvalidDatabaseException.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.IOException;
4 |
5 | public class InvalidDatabaseException extends IOException {
6 |
7 | private static final long serialVersionUID = 7818375828106090155L;
8 |
9 | public InvalidDatabaseException(String message) {
10 | super(message);
11 | }
12 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/MetaData.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.util.Map;
4 |
5 | /**
6 | * @copyright IPIP.net
7 | */
8 | public class MetaData {
9 | public int Build;
10 | public int IPVersion;
11 | public int nodeCount;
12 | public Map Languages;
13 | public String[] Fields;
14 | public int totalSize;
15 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/NotFoundException.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 |
4 | public class NotFoundException extends Exception {
5 |
6 | public NotFoundException(String name) {
7 | super(name);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/Reader.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 | import sun.net.util.IPAddressUtil;
5 |
6 | import java.io.*;
7 | import java.util.Arrays;
8 |
9 |
10 | public class Reader {
11 |
12 | private int fileSize;
13 | private int nodeCount;
14 |
15 | private MetaData meta;
16 | private byte[] data;
17 |
18 | private int v4offset;
19 |
20 | public Reader(String name) throws IOException, InvalidDatabaseException {
21 | this(new FileInputStream(new File(name)));
22 | }
23 |
24 | public Reader(InputStream in) throws IOException, InvalidDatabaseException {
25 | this.init(this.readAllAsStream(in));
26 | }
27 |
28 | protected byte[] readAllAsStream(InputStream in) throws IOException {
29 | ByteArrayOutputStream out = new ByteArrayOutputStream();
30 | byte[] buffer = new byte[4096];
31 | int n;
32 | while ((n = in.read(buffer)) != -1) {
33 | out.write(buffer, 0, n);
34 | }
35 | in.close();
36 | return out.toByteArray();
37 | }
38 |
39 | protected void init(byte[] data) throws InvalidDatabaseException {
40 |
41 | this.data = data;
42 | this.fileSize = data.length;
43 | if (this.fileSize < 5) {
44 | throw new InvalidDatabaseException("database file size error");
45 | }
46 |
47 | long metaLength = bytesToLong(
48 | this.data[0],
49 | this.data[1],
50 | this.data[2],
51 | this.data[3]
52 | );
53 |
54 | try {
55 | int end = Long.valueOf(metaLength).intValue() + 4;
56 | byte[] metaBytes = Arrays.copyOfRange(this.data, 4, end);
57 |
58 | MetaData meta = JSONObject.parseObject(new String(metaBytes), MetaData.class);
59 | this.nodeCount = meta.nodeCount;
60 | this.meta = meta;
61 | } catch (Exception e) {
62 | throw new InvalidDatabaseException(e.getMessage());
63 | }
64 |
65 | if ((meta.totalSize + Long.valueOf(metaLength).intValue() + 4) != this.data.length) {
66 | throw new InvalidDatabaseException("database file size error");
67 | }
68 |
69 | this.data = Arrays.copyOfRange(this.data, Long.valueOf(metaLength).intValue() + 4, this.fileSize);
70 |
71 | /** for ipv4 */
72 | if (0x01 == (this.meta.IPVersion & 0x01))
73 | {
74 | int node = 0;
75 | for (int i = 0; i < 96 && node < this.nodeCount; i++) {
76 | if (i >= 80) {
77 | node = this.readNode(node, 1);
78 | } else {
79 | node = this.readNode(node, 0);
80 | }
81 | }
82 |
83 | this.v4offset = node;
84 | }
85 | }
86 |
87 | public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
88 |
89 | int off;
90 | try {
91 | off = this.meta.Languages.get(language);
92 | } catch (NullPointerException e) {
93 | return null;
94 | }
95 |
96 | byte[] ipv;
97 |
98 | if (addr.indexOf(":") >= 0) {
99 | ipv = IPAddressUtil.textToNumericFormatV6(addr);
100 | if (ipv == null) {
101 | throw new IPFormatException("ipv6 format error");
102 | }
103 | if ((this.meta.IPVersion & 0x02) != 0x02){
104 | throw new IPFormatException("no support ipv6");
105 | }
106 |
107 | } else if (addr.indexOf(".") > 0) {
108 | ipv = IPAddressUtil.textToNumericFormatV4(addr);
109 | if (ipv == null) {
110 | throw new IPFormatException("ipv4 format error");
111 | }
112 | if ((this.meta.IPVersion & 0x01) != 0x01){
113 | throw new IPFormatException("no support ipv4");
114 | }
115 | } else {
116 | throw new IPFormatException("ip format error");
117 | }
118 |
119 | int node = 0;
120 | try {
121 | node = this.findNode(ipv);
122 | } catch (NotFoundException nfe) {
123 | return null;
124 | }
125 |
126 | final String data = this.resolve(node);
127 |
128 | return Arrays.copyOfRange(data.split("\t", this.meta.Fields.length * this.meta.Languages.size()), off, off+this.meta.Fields.length);
129 | }
130 |
131 | private int findNode(byte[] binary) throws NotFoundException {
132 |
133 | int node = 0;
134 |
135 | final int bit = binary.length * 8;
136 |
137 | if (bit == 32) {
138 | node = this.v4offset;
139 | }
140 |
141 | for (int i = 0; i < bit; i++) {
142 | if (node > this.nodeCount) {
143 | break;
144 | }
145 |
146 | node = this.readNode(node, 1 & ((0xFF & binary[i / 8]) >> 7 - (i % 8)));
147 | }
148 |
149 | if (node > this.nodeCount) {
150 | return node;
151 | }
152 |
153 | throw new NotFoundException("ip not found");
154 | }
155 |
156 | private String resolve(int node) throws InvalidDatabaseException {
157 | final int resoloved = node - this.nodeCount + this.nodeCount * 8;
158 | if (resoloved >= this.fileSize) {
159 | throw new InvalidDatabaseException("database resolve error");
160 | }
161 |
162 | byte b = 0;
163 | int size = Long.valueOf(bytesToLong(
164 | b,
165 | b,
166 | this.data[resoloved],
167 | this.data[resoloved+1]
168 | )).intValue();
169 |
170 | if (this.data.length < (resoloved + 2 + size)) {
171 | throw new InvalidDatabaseException("database resolve error");
172 | }
173 |
174 | try {
175 | return new String(this.data, resoloved + 2, size, "UTF-8");
176 | } catch (UnsupportedEncodingException e) {
177 | throw new InvalidDatabaseException("database resolve error");
178 | }
179 | }
180 |
181 | private int readNode(int node, int index) {
182 | int off = node * 8 + index * 4;
183 |
184 | return Long.valueOf(bytesToLong(
185 | this.data[off],
186 | this.data[off+1],
187 | this.data[off+2],
188 | this.data[off+3]
189 | )).intValue();
190 | }
191 |
192 | private static long bytesToLong(byte a, byte b, byte c, byte d) {
193 | return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
194 | }
195 |
196 | private static long int2long(int i) {
197 | long l = i & 0x7fffffffL;
198 | if (i < 0) {
199 | l |= 0x080000000L;
200 | }
201 | return l;
202 | }
203 |
204 | public boolean isIPv4() {
205 | return (this.meta.IPVersion & 0x01) == 0x01;
206 | }
207 |
208 | public boolean isIPv6() {
209 | return (this.meta.IPVersion & 0x02) == 0x02;
210 | }
211 |
212 | public int getBuildUTCTime() {
213 | return this.meta.Build;
214 | }
215 |
216 | public String[] getSupportFields() {
217 | return this.meta.Fields;
218 | }
219 |
220 | public String getSupportLanguages() {
221 | return this.meta.Languages.keySet().toString();
222 | }
223 | }
224 |
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/Risk.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class Risk {
10 |
11 | /**
12 | * @var Reader
13 | */
14 | private Reader reader;
15 |
16 | public Risk(String name) throws IOException,InvalidDatabaseException {
17 | this.reader = new Reader(name);
18 | }
19 |
20 | public Risk(InputStream in) throws IOException, InvalidDatabaseException {
21 | this.reader = new Reader(in);
22 | }
23 |
24 | public boolean reload(String name) {
25 | try {
26 | Reader r = new Reader(name);
27 | this.reader = r;
28 | } catch (Exception e) {
29 | return false;
30 | }
31 |
32 | return true;
33 | }
34 |
35 | public Map findMap(String addr, String language) throws IPFormatException, InvalidDatabaseException {
36 | String[] data = this.reader.find(addr, language);
37 | if (data == null) {
38 | return null;
39 | }
40 |
41 | Map m = new HashMap();
42 |
43 | String[] fields = this.reader.getSupportFields();
44 |
45 | for (int i = 0, l = data.length; i < l; i++) {
46 | m.put(fields[i], data[i]);
47 | }
48 |
49 | return m;
50 | }
51 |
52 | public RiskInfo findInfo(String addr) throws IPFormatException, InvalidDatabaseException {
53 |
54 | Map data = this.findMap(addr, "CN");
55 | if (data == null) {
56 | return null;
57 | }
58 |
59 | return new RiskInfo(data);
60 | }
61 |
62 | public String fields() {
63 | return Arrays.toString(this.reader.getSupportFields());
64 | }
65 | }
--------------------------------------------------------------------------------
/src/main/java/net/ipip/ipdb/RiskInfo.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import java.util.Map;
4 |
5 | public class RiskInfo {
6 |
7 | private Map data;
8 |
9 | public RiskInfo(Map map) {
10 | this.data = map;
11 | }
12 |
13 | public int getScore() {
14 | String s = this.data.get("score");
15 | if (s == null) {
16 | return 0;
17 | }
18 | return Integer.parseInt(s);
19 | }
20 |
21 | public String getBehavior() {
22 | String s = this.data.get("behavior");
23 | if (s == null) {
24 | return "";
25 | } else {
26 | return s;
27 | }
28 | }
29 |
30 | public String getCountryCode() {
31 | String s = this.data.get("country_code");
32 | if (s == null) {
33 | return "";
34 | } else {
35 | return s;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/BaseStationTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class BaseStationTest {
8 | @Test
9 | public void testBaseStation() {
10 | try {
11 | BaseStation db = new BaseStation("c:/work/ipdb/base_station.ipdb");
12 | System.out.println(db.buildTime());
13 | System.out.println(db.languages());
14 | System.out.println(db.fields());
15 | System.out.println(db.isIPv4());
16 | System.out.println(db.isIPv6());
17 | } catch (IOException ioe) {
18 | ioe.printStackTrace();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/CityTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.concurrent.Callable;
9 | import java.util.concurrent.ExecutorService;
10 | import java.util.concurrent.Executors;
11 | import java.util.concurrent.TimeUnit;
12 |
13 | public class CityTest {
14 |
15 | private static City cityDb;
16 |
17 | static {
18 | try {
19 | cityDb = new City("c:/tiantexin/download/mydata4full.ipdb");
20 | } catch (Exception ex) {
21 | ex.printStackTrace();
22 | }
23 | }
24 |
25 | @Test
26 | public void findAsnInfo() {
27 | try {
28 | CityInfo cityInfo = cityDb.findCity("27.190.250.125", "CN");
29 | System.out.println(cityInfo);
30 | System.out.println(cityInfo.getDistrictInfo());
31 | System.out.println("\n");
32 | System.out.println(cityInfo.getRoute());
33 | System.out.println("\n");
34 | System.out.println(cityInfo.getUsageType());
35 | AsnInfo[] infos = cityInfo.getAsnInfos();
36 | if (infos != null) {
37 | for (int i = 0; i < infos.length; i++) {
38 | AsnInfo info = infos[i];
39 | System.out.println(info.ASN);
40 | System.out.println(info.Registry);
41 | System.out.println(info.Country);
42 | System.out.println(info.NetName);
43 | System.out.println(info.OrgName);
44 | }
45 | System.out.println(infos.length);
46 | }
47 |
48 | } catch (Exception ex) {
49 | ex.printStackTrace();
50 | }
51 | }
52 |
53 | @Test
54 | public void find() {
55 |
56 | try {
57 | CityInfo cityInfo = cityDb.findCity("111.199.81.160", "CN");
58 | System.out.println(cityInfo.getDistrictInfo());
59 | System.out.println("\n");
60 | System.out.println(cityInfo.toString());
61 |
62 | } catch (Exception ex) {
63 | ex.printStackTrace();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/DistrictTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class DistrictTest {
8 |
9 | @Test
10 | public void testDistrict() {
11 |
12 | try {
13 | District db = new District("d:/work/ipdb/china_district.ipdb");
14 | System.out.println(db.buildTime());
15 | System.out.println(db.languages());
16 | System.out.println(db.fields());
17 | System.out.println(db.isIPv4());
18 | System.out.println(db.isIPv6());
19 | DistrictInfo info = db.findInfo("123.121.1.69", "CN");
20 | System.out.println(info.getRegionName());
21 | System.out.println(info.getCityName());
22 | System.out.println(info.getDistrictName());
23 | System.out.println(info.getLatitude());
24 | System.out.println(info.getLongitude());
25 | } catch (Exception ioe) {
26 | ioe.printStackTrace();
27 | }
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/IDCTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class IDCTest {
8 |
9 | @Test
10 | public void testIDC() {
11 | try {
12 | IDC db = new IDC("c:/work/ipdb/idc_list.ipdb");
13 | System.out.println(db.buildTime());
14 | System.out.println(db.languages());
15 | System.out.println(db.fields());
16 | System.out.println(db.isIPv4());
17 | System.out.println(db.isIPv6());
18 | } catch (IOException ioe) {
19 | ioe.printStackTrace();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/ReaderTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class ReaderTest {
8 |
9 | @Test
10 | public void testReader() {
11 | try {
12 | IDC db = new IDC("c:/work/ipdb/idc_list.ipdb");
13 | System.out.println(db.buildTime());
14 | System.out.println(db.languages());
15 | System.out.println(db.fields());
16 | System.out.println(db.isIPv4());
17 | System.out.println(db.isIPv6());
18 | // Thread.sleep(30 * 1000000L);
19 | } catch (IOException ioe) {
20 | ioe.printStackTrace();
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/test/java/net/ipip/ipdb/RiskTest.java:
--------------------------------------------------------------------------------
1 | package net.ipip.ipdb;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.IOException;
6 |
7 | public class RiskTest {
8 | @Test
9 | public void testRisk() {
10 | try {
11 | Risk db = new Risk("c:/work/ipdb/v6risk.ipdb");
12 |
13 | System.out.println(db.fields());
14 |
15 | System.out.println(db.findInfo("2001:240:2a3b:7100::"));
16 |
17 | } catch (Exception ioe) {
18 | ioe.printStackTrace();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------