Skip to content

Commit fa728c8

Browse files
committed
update
1 parent ba0333b commit fa728c8

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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<String, String> 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<String, String> m = new HashMap<String, String>();
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<String, String> 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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package net.ipip.ipdb;
2+
3+
import java.util.Map;
4+
5+
public class RiskInfo {
6+
7+
private Map<String,String> data;
8+
9+
public RiskInfo(Map<String,String> 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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

0 commit comments

Comments
 (0)