-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathReader.java
More file actions
232 lines (184 loc) · 6.5 KB
/
Reader.java
File metadata and controls
232 lines (184 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package net.ipip.ipdb;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Reader {
private int fileSize;
private int nodeCount;
private MetaData meta;
private byte[] data;
private int v4offset;
public Reader(String name) throws IOException, InvalidDatabaseException {
this(new FileInputStream(new File(name)));
}
public Reader(InputStream in) throws IOException, InvalidDatabaseException {
this.init(this.readAllAsStream(in));
}
protected byte[] readAllAsStream(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
in.close();
return out.toByteArray();
}
protected void init(byte[] data) throws InvalidDatabaseException {
this.data = data;
this.fileSize = data.length;
if (this.fileSize < 5) {
throw new InvalidDatabaseException("database file size error");
}
long metaLength = bytesToLong(
this.data[0],
this.data[1],
this.data[2],
this.data[3]
);
try {
int end = Long.valueOf(metaLength).intValue() + 4;
byte[] metaBytes = Arrays.copyOfRange(this.data, 4, end);
MetaData meta = JSONObject.parseObject(new String(metaBytes), MetaData.class);
this.nodeCount = meta.nodeCount;
this.meta = meta;
} catch (Exception e) {
throw new InvalidDatabaseException(e.getMessage());
}
if ((meta.totalSize + Long.valueOf(metaLength).intValue() + 4) != this.data.length) {
throw new InvalidDatabaseException("database file size error");
}
this.data = Arrays.copyOfRange(this.data, Long.valueOf(metaLength).intValue() + 4, this.fileSize);
/** for ipv4 */
if (0x01 == (this.meta.IPVersion & 0x01))
{
int node = 0;
for (int i = 0; i < 96 && node < this.nodeCount; i++) {
if (i >= 80) {
node = this.readNode(node, 1);
} else {
node = this.readNode(node, 0);
}
}
this.v4offset = node;
}
}
public String[] find(String addr, String language) throws IPFormatException, InvalidDatabaseException {
int off;
try {
off = this.meta.Languages.get(language);
} catch (NullPointerException e) {
return null;
}
byte[] ipv = null;
if (addr.indexOf(":") > 0) {
try {
ipv = InetAddress.getByName(addr).getAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (ipv == null) {
throw new IPFormatException("ipv6 format error");
}
if ((this.meta.IPVersion & 0x02) != 0x02){
throw new IPFormatException("no support ipv6");
}
} else if (addr.indexOf(".") > 0) {
try {
ipv = InetAddress.getByName(addr).getAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (ipv == null) {
throw new IPFormatException("ipv4 format error");
}
if ((this.meta.IPVersion & 0x01) != 0x01){
throw new IPFormatException("no support ipv4");
}
} else {
throw new IPFormatException("ip format error");
}
int node = 0;
try {
node = this.findNode(ipv);
} catch (NotFoundException nfe) {
return null;
}
final String data = this.resolve(node);
return Arrays.copyOfRange(data.split("\t", this.meta.Fields.length * this.meta.Languages.size()), off, off+this.meta.Fields.length);
}
private int findNode(byte[] binary) throws NotFoundException {
int node = 0;
final int bit = binary.length * 8;
if (bit == 32) {
node = this.v4offset;
}
for (int i = 0; i < bit; i++) {
if (node > this.nodeCount) {
break;
}
node = this.readNode(node, 1 & ((0xFF & binary[i / 8]) >> 7 - (i % 8)));
}
if (node > this.nodeCount) {
return node;
}
throw new NotFoundException("ip not found");
}
private String resolve(int node) throws InvalidDatabaseException {
final int resoloved = node - this.nodeCount + this.nodeCount * 8;
if (resoloved >= this.fileSize) {
throw new InvalidDatabaseException("database resolve error");
}
byte b = 0;
int size = Long.valueOf(bytesToLong(
b,
b,
this.data[resoloved],
this.data[resoloved+1]
)).intValue();
if (this.data.length < (resoloved + 2 + size)) {
throw new InvalidDatabaseException("database resolve error");
}
try {
return new String(this.data, resoloved + 2, size, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InvalidDatabaseException("database resolve error");
}
}
private int readNode(int node, int index) {
int off = node * 8 + index * 4;
return Long.valueOf(bytesToLong(
this.data[off],
this.data[off+1],
this.data[off+2],
this.data[off+3]
)).intValue();
}
private static long bytesToLong(byte a, byte b, byte c, byte d) {
return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
}
private static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
}
public boolean isIPv4() {
return (this.meta.IPVersion & 0x01) == 0x01;
}
public boolean isIPv6() {
return (this.meta.IPVersion & 0x02) == 0x02;
}
public int getBuildUTCTime() {
return this.meta.Build;
}
public String[] getSupportFields() {
return this.meta.Fields;
}
public String getSupportLanguages() {
return this.meta.Languages.keySet().toString();
}
}