Skip to content

Commit 8ca13d7

Browse files
committed
remove bogonType. make IpAddressMatcherList. replace gson parsing
1 parent d910490 commit 8ca13d7

File tree

2 files changed

+74
-109
lines changed

2 files changed

+74
-109
lines changed

src/main/java/io/ipinfo/api/model/IPResponse.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class IPResponse {
66
private final String ip;
77
private final String hostname;
88
private final boolean bogon;
9-
private final String bogonType;
109
private final boolean anycast;
1110
private final String city;
1211
private final String region;
@@ -27,7 +26,6 @@ public IPResponse(
2726
String ip,
2827
String hostname,
2928
boolean bogon,
30-
String bogonType,
3129
boolean anycast,
3230
String city,
3331
String region,
@@ -46,7 +44,6 @@ public IPResponse(
4644
this.ip = ip;
4745
this.hostname = hostname;
4846
this.bogon = bogon;
49-
this.bogonType = bogonType;
5047
this.anycast = anycast;
5148
this.city = city;
5249
this.region = region;
@@ -61,7 +58,13 @@ public IPResponse(
6158
this.privacy = privacy;
6259
this.abuse = abuse;
6360
this.domains = domains;
61+
}
6462

63+
public IPResponse(
64+
String ip,
65+
boolean bogon
66+
) {
67+
this(ip, null, bogon, false, null, null, null, null, null, null, null, null, null, null, null, null, null);
6568
}
6669

6770
/**
@@ -85,10 +88,6 @@ public boolean getBogon() {
8588
return bogon;
8689
}
8790

88-
public String getBogonType() {
89-
return bogonType;
90-
}
91-
9291
public boolean getAnycast() {
9392
return anycast;
9493
}
@@ -175,7 +174,6 @@ public String toString() {
175174
"IPResponse{" +
176175
"ip='" + ip + '\'' +
177176
", bogon='" + bogon + '\'' +
178-
", bogonType='" + bogonType + '\'' +
179177
"}"
180178
:
181179
"IPResponse{" +

src/main/java/io/ipinfo/api/request/IPRequest.java

Lines changed: 68 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import okhttp3.Response;
99
import java.net.InetAddress;
1010
import java.net.UnknownHostException;
11-
import java.net.Inet4Address;
1211

1312
public class IPRequest extends BaseRequest<IPResponse> {
1413
private final static String URL_FORMAT = "https://ipinfo.io/%s";
@@ -21,11 +20,9 @@ public IPRequest(OkHttpClient client, String token, String ip) {
2120

2221
@Override
2322
public IPResponse handle() throws RateLimitedException {
24-
String bogonType = handleBogon(ip);
25-
if (bogonType != null) {
26-
String bogonResponse = "{ip='" + ip + "', bogon='true', bogonType='" + bogonType + "'}";
23+
if (isBogon(ip)) {
2724
try {
28-
return gson.fromJson(bogonResponse.toString(), IPResponse.class);
25+
return new IPResponse(ip, true);
2926
} catch (Exception ex) {
3027
System.out.println("inException");
3128
throw new ErrorResponseException(ex);
@@ -48,105 +45,75 @@ public IPResponse handle() throws RateLimitedException {
4845
}
4946
}
5047

51-
static String handleBogon(String ip) {
52-
String[] bogonIPv4List = {
53-
"0.0.0.0/8",
54-
"10.0.0.0/8",
55-
"100.64.0.0/10",
56-
"127.0.0.0/8",
57-
"169.254.0.0/16",
58-
"172.16.0.0/12",
59-
"192.0.0.0/24",
60-
"192.0.2.0/24",
61-
"192.168.0.0/16",
62-
"198.18.0.0/15",
63-
"198.51.100.0/24",
64-
"203.0.113.0/24",
65-
"224.0.0.0/4",
66-
"240.0.0.0/4",
67-
"255.255.255.255/32"
68-
};
69-
70-
String[] bogonIPv6List = {
71-
"::/128",
72-
"::1/128",
73-
"::ffff:0:0/96",
74-
"::/96",
75-
"100::/64",
76-
"2001:10::/28",
77-
"2001:db8::/32",
78-
"fc00::/7",
79-
"fe80::/10",
80-
"fec0::/10",
81-
"ff00::/8"
82-
};
83-
84-
String[] bogon6to4List = {
85-
"2002::/24",
86-
"2002:a00::/24",
87-
"2002:7f00::/24",
88-
"2002:a9fe::/32",
89-
"2002:ac10::/28",
90-
"2002:c000::/40",
91-
"2002:c000:200::/40",
92-
"2002:c0a8::/32",
93-
"2002:c612::/31",
94-
"2002:c633:6400::/40",
95-
"2002:cb00:7100::/40",
96-
"2002:e000::/20",
97-
"2002:f000::/20",
98-
"2002:ffff:ffff::/48"
99-
};
100-
101-
String[] bogonTeredoList = {
102-
"2001::/40",
103-
"2001:0:a00::/40",
104-
"2001:0:7f00::/40",
105-
"2001:0:a9fe::/48",
106-
"2001:0:ac10::/44",
107-
"2001:0:c000::/56",
108-
"2001:0:c000:200::/56",
109-
"2001:0:c0a8::/48",
110-
"2001:0:c612::/47",
111-
"2001:0:c633:6400::/56",
112-
"2001:0:cb00:7100::/56",
113-
"2001:0:e000::/36",
114-
"2001:0:f000::/36",
115-
"2001:0:ffff:ffff::/64"
116-
};
117-
118-
InetAddress address = parseAddress(ip);
119-
if (address instanceof Inet4Address) {
120-
for (int i = 0; i < bogonIPv4List.length; i++) {
121-
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(bogonIPv4List[i]);
122-
if (ipAddressMatcher.matches(ip)) {
123-
return "IPv4";
124-
}
125-
}
126-
}
127-
128-
for (int i = 0; i < bogonIPv6List.length; i++) {
129-
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(bogonIPv6List[i]);
48+
static IpAddressMatcher[] IpAddressMatcherList = {
49+
// IPv4
50+
new IpAddressMatcher("0.0.0.0/8"),
51+
new IpAddressMatcher("10.0.0.0/8"),
52+
new IpAddressMatcher("100.64.0.0/10"),
53+
new IpAddressMatcher("127.0.0.0/8"),
54+
new IpAddressMatcher("169.254.0.0/16"),
55+
new IpAddressMatcher("172.16.0.0/12"),
56+
new IpAddressMatcher("192.0.0.0/24"),
57+
new IpAddressMatcher("192.0.2.0/24"),
58+
new IpAddressMatcher("192.168.0.0/16"),
59+
new IpAddressMatcher("198.18.0.0/15"),
60+
new IpAddressMatcher("198.51.100.0/24"),
61+
new IpAddressMatcher("203.0.113.0/24"),
62+
new IpAddressMatcher("224.0.0.0/4"),
63+
new IpAddressMatcher("240.0.0.0/4"),
64+
new IpAddressMatcher("255.255.255.255/32"),
65+
// IPv6
66+
new IpAddressMatcher("::/128"),
67+
new IpAddressMatcher("::1/128"),
68+
new IpAddressMatcher("::ffff:0:0/96"),
69+
new IpAddressMatcher("::/96"),
70+
new IpAddressMatcher("100::/64"),
71+
new IpAddressMatcher("2001:10::/28"),
72+
new IpAddressMatcher("2001:db8::/32"),
73+
new IpAddressMatcher("fc00::/7"),
74+
new IpAddressMatcher("fe80::/10"),
75+
new IpAddressMatcher("fec0::/10"),
76+
new IpAddressMatcher("ff00::/8"),
77+
// 6to4
78+
new IpAddressMatcher("2002::/24"),
79+
new IpAddressMatcher("2002:a00::/24"),
80+
new IpAddressMatcher("2002:7f00::/24"),
81+
new IpAddressMatcher("2002:a9fe::/32"),
82+
new IpAddressMatcher("2002:ac10::/28"),
83+
new IpAddressMatcher("2002:c000::/40"),
84+
new IpAddressMatcher("2002:c000:200::/40"),
85+
new IpAddressMatcher("2002:c0a8::/32"),
86+
new IpAddressMatcher("2002:c612::/31"),
87+
new IpAddressMatcher("2002:c633:6400::/40"),
88+
new IpAddressMatcher("2002:cb00:7100::/40"),
89+
new IpAddressMatcher("2002:e000::/20"),
90+
new IpAddressMatcher("2002:f000::/20"),
91+
new IpAddressMatcher("2002:ffff:ffff::/48"),
92+
// Teredo
93+
new IpAddressMatcher("2001::/40"),
94+
new IpAddressMatcher("2001:0:a00::/40"),
95+
new IpAddressMatcher("2001:0:7f00::/40"),
96+
new IpAddressMatcher("2001:0:a9fe::/48"),
97+
new IpAddressMatcher("2001:0:ac10::/44"),
98+
new IpAddressMatcher("2001:0:c000::/56"),
99+
new IpAddressMatcher("2001:0:c000:200::/56"),
100+
new IpAddressMatcher("2001:0:c0a8::/48"),
101+
new IpAddressMatcher("2001:0:c612::/47"),
102+
new IpAddressMatcher("2001:0:c633:6400::/56"),
103+
new IpAddressMatcher("2001:0:cb00:7100::/56"),
104+
new IpAddressMatcher("2001:0:e000::/36"),
105+
new IpAddressMatcher("2001:0:f000::/36"),
106+
new IpAddressMatcher("2001:0:ffff:ffff::/64")
107+
};
108+
109+
static boolean isBogon(String ip) {
110+
for (int i = 0; i < IpAddressMatcherList.length; i++) {
111+
IpAddressMatcher ipAddressMatcher = IpAddressMatcherList[i];
130112
if (ipAddressMatcher.matches(ip)) {
131-
return "IPv6";
113+
return true;
132114
}
133115
}
134-
135-
for (int i = 0; i < bogon6to4List.length; i++) {
136-
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(bogon6to4List[i]);
137-
if (ipAddressMatcher.matches(ip)) {
138-
return "6to4";
139-
}
140-
}
141-
142-
for (int i = 0; i < bogonTeredoList.length; i++) {
143-
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(bogonTeredoList[i]);
144-
if (ipAddressMatcher.matches(ip)) {
145-
return "Teredo";
146-
}
147-
}
148-
149-
return null;
116+
return false;
150117
}
151118

152119
static InetAddress parseAddress(String address) {

0 commit comments

Comments
 (0)