Skip to content

Commit 0a719fb

Browse files
committed
Added support for IP2Proxy Web Service
1 parent efc06c5 commit 0a719fb

File tree

4 files changed

+282
-3
lines changed

4 files changed

+282
-3
lines changed

README.md

+118-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ This component allows user to query an IP address if it was being used as VPN an
55
* Free IP2Proxy BIN Data: https://lite.ip2location.com
66
* Commercial IP2Proxy BIN Data: https://www.ip2location.com/database/ip2proxy
77

8+
As an alternative, this component can also call the IP2Proxy Web Service. This requires an API key. If you don't have an existing API key, you can subscribe for one at the below:
9+
10+
https://www.ip2location.com/web-service/ip2proxy
11+
812
## Compilation
913

1014
```bash
1115
javac com/ip2proxy/*.java
1216
jar cf ip2proxy.jar com/ip2proxy/*.class
1317
```
1418

19+
## QUERY USING THE BIN FILE
20+
1521
## Methods
1622
Below are the methods supported in this class.
1723

@@ -144,10 +150,120 @@ public class Main {
144150
}
145151
Proxy.Close();
146152
}
147-
catch(Exception Ex) {
148-
System.out.println(Ex);
153+
catch(Exception e) {
154+
System.out.println(e);
149155
}
150156
}
151157
}
152158
```
153159

160+
## QUERY USING THE IP2PROXY PROXY DETECTION WEB SERVICE
161+
162+
## Methods
163+
Below are the methods supported in this class.
164+
165+
|Method Name|Description|
166+
|---|---|
167+
|Open(String APIKey, String Package, boolean UseSSL)| Expects 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX11)</li></li><li>Use HTTPS or HTTP</li></ol>|
168+
|IPQuery(String IPAddress)|Query IP address. This method returns a JsonObject containing the proxy info. <ul><li>countryCode</li><li>countryName</li><li>regionName</li><li>cityName</li><li>isp</li><li>domain</li><li>usageType</li><li>asn</li><li>as</li><li>lastSeen</li><li>threat</li><li>proxyType</li><li>isProxy</li><li>provider</li><ul>|
169+
|GetCredit()|This method returns the web service credit balance in a JsonObject.|
170+
171+
## Usage
172+
173+
```java
174+
import com.ip2proxy.*;
175+
import com.google.gson.*;
176+
177+
public class Main {
178+
public Main() {
179+
}
180+
181+
public static void main(String[] args) {
182+
try
183+
{
184+
IP2ProxyWebService ws = new IP2ProxyWebService();
185+
186+
String strIPAddress = "8.8.8.8";
187+
String strAPIKey = "YOUR_API_KEY";
188+
String strPackage = "PX11";
189+
boolean boolSSL = true;
190+
191+
ws.Open(strAPIKey, strPackage, boolSSL);
192+
193+
JsonObject myresult = ws.IPQuery(strIPAddress);
194+
195+
if ((myresult.get("response") != null) && (myresult.get("response").getAsString().equals("OK")))
196+
{
197+
System.out.println("countryCode: " + ((myresult.get("countryCode") != null) ? myresult.get("countryCode").getAsString() : ""));
198+
System.out.println("countryName: " + ((myresult.get("countryName") != null) ? myresult.get("countryName").getAsString() : ""));
199+
System.out.println("regionName: " + ((myresult.get("regionName") != null) ? myresult.get("regionName").getAsString() : ""));
200+
System.out.println("cityName: " + ((myresult.get("cityName") != null) ? myresult.get("cityName").getAsString() : ""));
201+
System.out.println("isp: " + ((myresult.get("isp") != null) ? myresult.get("isp").getAsString() : ""));
202+
System.out.println("domain: " + ((myresult.get("domain") != null) ? myresult.get("domain").getAsString() : ""));
203+
System.out.println("usageType: " + ((myresult.get("usageType") != null) ? myresult.get("usageType").getAsString() : ""));
204+
System.out.println("asn: " + ((myresult.get("asn") != null) ? myresult.get("asn").getAsString() : ""));
205+
System.out.println("as: " + ((myresult.get("as") != null) ? myresult.get("as").getAsString() : ""));
206+
System.out.println("lastSeen: " + ((myresult.get("lastSeen") != null) ? myresult.get("lastSeen").getAsString() : ""));
207+
System.out.println("proxyType: " + ((myresult.get("proxyType") != null) ? myresult.get("proxyType").getAsString() : ""));
208+
System.out.println("threat: " + ((myresult.get("threat") != null) ? myresult.get("threat").getAsString() : ""));
209+
System.out.println("isProxy: " + ((myresult.get("isProxy") != null) ? myresult.get("isProxy").getAsString() : ""));
210+
System.out.println("provider: " + ((myresult.get("provider") != null) ? myresult.get("provider").getAsString() : ""));
211+
}
212+
else if (myresult.get("response") != null)
213+
{
214+
System.out.println("Error: " + myresult.get("response").getAsString());
215+
}
216+
217+
myresult = ws.GetCredit();
218+
219+
if (myresult.get("response") != null)
220+
{
221+
System.out.println("Credit balance: " + myresult.get("response").getAsString());
222+
}
223+
}
224+
catch(Exception e)
225+
{
226+
System.out.println(e);
227+
e.printStackTrace(System.out);
228+
}
229+
230+
}
231+
}
232+
```
233+
234+
### Proxy Type
235+
236+
|Proxy Type|Description|
237+
|---|---|
238+
|VPN|Anonymizing VPN services|
239+
|TOR|Tor Exit Nodes|
240+
|PUB|Public Proxies|
241+
|WEB|Web Proxies|
242+
|DCH|Hosting Providers/Data Center|
243+
|SES|Search Engine Robots|
244+
|RES|Residential Proxies [PX10+]|
245+
246+
### Usage Type
247+
248+
|Usage Type|Description|
249+
|---|---|
250+
|COM|Commercial|
251+
|ORG|Organization|
252+
|GOV|Government|
253+
|MIL|Military|
254+
|EDU|University/College/School|
255+
|LIB|Library|
256+
|CDN|Content Delivery Network|
257+
|ISP|Fixed Line ISP|
258+
|MOB|Mobile ISP|
259+
|DCH|Data Center/Web Hosting/Transit|
260+
|SES|Search Engine Spider|
261+
|RSV|Reserved|
262+
263+
### Threat Type
264+
265+
|Threat Type|Description|
266+
|---|---|
267+
|SPAM|Spammer|
268+
|SCANNER|Security Scanner or Attack|
269+
|BOTNET|Spyware or Malware|

com/ip2proxy/Http.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.ip2proxy;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
class Http {
9+
public static String get(URL url) {
10+
try {
11+
java.lang.System.setProperty("https.protocols", "TLSv1.2");
12+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
13+
conn.setRequestMethod("GET");
14+
conn.setRequestProperty("Accept", "application/json");
15+
16+
if (conn.getResponseCode() != 200) {
17+
return ("Failed : HTTP error code : " + conn.getResponseCode());
18+
}
19+
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
20+
21+
String output;
22+
StringBuilder resultFromHttp = new StringBuilder();
23+
while ((output = br.readLine()) != null) {
24+
resultFromHttp.append(output).append("\n");
25+
}
26+
27+
br.close();
28+
conn.disconnect();
29+
return resultFromHttp.toString();
30+
} catch (Exception e) {
31+
throw new RuntimeException(e);
32+
}
33+
}
34+
35+
}

com/ip2proxy/IP2Proxy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private enum Modes {
125125
private boolean THREAT_ENABLED;
126126
private boolean PROVIDER_ENABLED;
127127

128-
private static final String _ModuleVersion = "3.1.0";
128+
private static final String _ModuleVersion = "3.2.0";
129129

130130
public IP2Proxy() {
131131

com/ip2proxy/IP2ProxyWebService.java

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.ip2proxy;
2+
3+
import java.util.regex.*;
4+
import java.net.URL;
5+
import java.net.URLEncoder;
6+
import java.lang.StringBuffer; // JDK 1.4 does not support StringBuilder so can't use that
7+
8+
import com.google.gson.*;
9+
10+
public class IP2ProxyWebService {
11+
private static final Pattern pattern = Pattern.compile("^[\\dA-Z]{10}$");
12+
private static final Pattern pattern2 = Pattern.compile("^PX\\d+$");
13+
14+
private String _APIKey = "";
15+
private String _Package = "";
16+
private boolean _UseSSL = true;
17+
18+
public IP2ProxyWebService() {
19+
20+
}
21+
22+
/**
23+
* This function initializes the params for the web service.
24+
*
25+
* @param APIKey IP2Proxy Web Service API key
26+
* @param Package IP2Proxy Web Service package (PX1 to PX11)
27+
* @throws IllegalArgumentException If an invalid parameter is specified
28+
*/
29+
public void Open(String APIKey, String Package) throws IllegalArgumentException {
30+
Open(APIKey, Package, true);
31+
}
32+
33+
/**
34+
* This function initializes the params for the web service.
35+
*
36+
* @param APIKey IP2Proxy Web Service API key
37+
* @param Package IP2Proxy Web Service package (PX1 to PX11)
38+
* @param UseSSL Set to true to call the web service using SSL
39+
* @throws IllegalArgumentException If an invalid parameter is specified
40+
*/
41+
public void Open(String APIKey, String Package, boolean UseSSL) throws IllegalArgumentException {
42+
_APIKey = APIKey;
43+
_Package = Package;
44+
_UseSSL = UseSSL;
45+
46+
CheckParams();
47+
}
48+
49+
/**
50+
* This function validates the API key and package params.
51+
*/
52+
private void CheckParams() throws IllegalArgumentException {
53+
if ((!pattern.matcher(_APIKey).matches()) && (!_APIKey.equals("demo"))) {
54+
throw new IllegalArgumentException("Invalid API key.");
55+
} else if (!pattern2.matcher(_Package).matches()) {
56+
throw new IllegalArgumentException("Invalid package name.");
57+
}
58+
}
59+
60+
/**
61+
* This function to query IP2Proxy data.
62+
*
63+
* @param IPAddress IP Address you wish to query
64+
* @return IP2Proxy data
65+
* @throws IllegalArgumentException If an invalid parameter is specified
66+
* @throws RuntimeException If an exception occurred at runtime
67+
*/
68+
public JsonObject IPQuery(String IPAddress) throws IllegalArgumentException, RuntimeException {
69+
try {
70+
String myurl;
71+
String myjson;
72+
CheckParams(); // check here in case user haven't called Open yet
73+
74+
StringBuffer bf = new StringBuffer();
75+
bf.append("http");
76+
if (_UseSSL) {
77+
bf.append("s");
78+
}
79+
bf.append("://api.ip2proxy.com/?key=").append(_APIKey).append("&package=").append(_Package).append("&ip=").append(URLEncoder.encode(IPAddress, "UTF-8"));
80+
81+
myurl = bf.toString();
82+
83+
myjson = Http.get(new URL(myurl));
84+
85+
JsonParser parser = new JsonParser();
86+
JsonObject myresult = parser.parse(myjson).getAsJsonObject();
87+
return myresult;
88+
} catch (IllegalArgumentException ex) {
89+
throw ex;
90+
} catch (Exception ex2) {
91+
throw new RuntimeException(ex2);
92+
}
93+
}
94+
95+
/**
96+
* This function to check web service credit balance.
97+
*
98+
* @return Credit balance
99+
* @throws IllegalArgumentException If an invalid parameter is specified
100+
* @throws RuntimeException If an exception occurred at runtime
101+
*/
102+
public JsonObject GetCredit() throws IllegalArgumentException, RuntimeException {
103+
try {
104+
String myurl;
105+
String myjson;
106+
CheckParams(); // check here in case user haven't called Open yet
107+
108+
StringBuffer bf = new StringBuffer();
109+
bf.append("http");
110+
if (_UseSSL) {
111+
bf.append("s");
112+
}
113+
bf.append("://api.ip2proxy.com/?key=").append(_APIKey).append("&check=true");
114+
115+
myurl = bf.toString();
116+
117+
myjson = Http.get(new URL(myurl));
118+
119+
JsonParser parser = new JsonParser();
120+
JsonObject myresult = parser.parse(myjson).getAsJsonObject();
121+
return myresult;
122+
} catch (IllegalArgumentException ex) {
123+
throw ex;
124+
} catch (Exception ex2) {
125+
throw new RuntimeException(ex2);
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)