Skip to content

Commit a96b2b4

Browse files
Added support for IP2Proxy Web Service
1 parent 4c9bb81 commit a96b2b4

7 files changed

Lines changed: 204 additions & 5 deletions

File tree

IP2ProxyScala/.bsp/sbt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"sbt","version":"1.4.7","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["C:\\Program Files\\Java\\jdk1.8.0_261\\jre/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/Kervin/AppData/Roaming/JetBrains/IdeaIC2021.1/plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp"]}
1+
{"name":"sbt","version":"1.4.7","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["C:\\Program Files\\Java\\jdk1.8.0_261\\jre/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/Kervin/AppData/Roaming/JetBrains/IdeaIC2021.2/plugins/Scala/launcher/sbt-launch.jar","xsbt.boot.Boot","-bsp"]}

IP2ProxyScala/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name := "IP2ProxyScala"
22

3-
version := "3.1.0"
3+
version := "3.2.0"
44

55
scalaVersion := "2.13.5"
66

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

IP2ProxyScala/src/main/scala/IP2Proxy.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object IP2Proxy {
4949
private val LASTSEEN_POSITION = Array(0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11)
5050
private val THREAT_POSITION = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12)
5151
private val PROVIDER_POSITION = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13)
52-
private val _ModuleVersion = "3.1.0"
52+
private val _ModuleVersion = "3.2.0"
5353
}
5454

5555
class IP2Proxy() {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.ip2proxy
2+
3+
import com.google.gson._
4+
import java.net.{URL, URLEncoder}
5+
import java.util.regex._
6+
7+
object IP2ProxyWebService {
8+
private val pattern = Pattern.compile("^[\\dA-Z]{10}$")
9+
private val pattern2 = Pattern.compile("^PX\\d+$")
10+
}
11+
12+
class IP2ProxyWebService() {
13+
private var _APIKey: String = _
14+
private var _Package: String = _
15+
private var _UseSSL: Boolean = _
16+
17+
/**
18+
* This function initializes the params for the web service.
19+
*
20+
* @param APIKey IP2Proxy Web Service API key
21+
* @param Package IP2Proxy Web Service package (PX1 to PX11)
22+
* @throws IllegalArgumentException If an invalid parameter is specified
23+
*/
24+
@throws[IllegalArgumentException]
25+
def Open(APIKey: String, Package: String): Unit = Open(APIKey, Package, true)
26+
27+
/**
28+
* This function initializes the params for the web service.
29+
*
30+
* @param APIKey IP2Proxy Web Service API key
31+
* @param Package IP2Proxy Web Service package (PX1 to PX11)
32+
* @param UseSSL Set to true to call the web service using SSL
33+
* @throws IllegalArgumentException If an invalid parameter is specified
34+
*/
35+
@throws[IllegalArgumentException]
36+
def Open(APIKey: String, Package: String, UseSSL: Boolean): Unit = {
37+
_APIKey = APIKey
38+
_Package = Package
39+
_UseSSL = UseSSL
40+
CheckParams()
41+
}
42+
43+
/**
44+
* This function validates the API key and package params.
45+
*/
46+
@throws[IllegalArgumentException]
47+
private def CheckParams(): Unit = if (!IP2ProxyWebService.pattern.matcher(_APIKey).matches) throw new IllegalArgumentException("Invalid API key.")
48+
else if (!IP2ProxyWebService.pattern2.matcher(_Package).matches) throw new IllegalArgumentException("Invalid package name.")
49+
50+
/**
51+
* This function to query IP2Proxy data.
52+
*
53+
* @param IPAddress IP Address you wish to query
54+
* @return IP2Proxy data
55+
* @throws IllegalArgumentException If an invalid parameter is specified
56+
* @throws RuntimeException If an exception occurred at runtime
57+
*/
58+
@throws[IllegalArgumentException]
59+
@throws[RuntimeException]
60+
def IPQuery(IPAddress: String): JsonObject = try {
61+
CheckParams() // check here in case user haven't called Open yet
62+
val bf = new StringBuffer
63+
bf.append("http")
64+
if (_UseSSL) bf.append("s")
65+
bf.append("://api.ip2proxy.com/?key=").append(_APIKey).append("&package=").append(_Package).append("&ip=").append(URLEncoder.encode(IPAddress, "UTF-8"))
66+
val myUrl = bf.toString
67+
val myJson = Http.get(new URL(myUrl))
68+
JsonParser.parseString(myJson).getAsJsonObject
69+
} catch {
70+
case ex: IllegalArgumentException =>
71+
throw ex
72+
case ex2: Exception =>
73+
throw new RuntimeException(ex2)
74+
}
75+
76+
/**
77+
* This function to check web service credit balance.
78+
*
79+
* @return Credit balance
80+
* @throws IllegalArgumentException If an invalid parameter is specified
81+
* @throws RuntimeException If an exception occurred at runtime
82+
*/
83+
@throws[IllegalArgumentException]
84+
@throws[RuntimeException]
85+
def GetCredit: JsonObject = try {
86+
CheckParams()
87+
val bf = new StringBuffer
88+
bf.append("http")
89+
if (_UseSSL) bf.append("s")
90+
bf.append("://api.ip2proxy.com/?key=").append(_APIKey).append("&check=true")
91+
val myUrl = bf.toString
92+
val myJson = Http.get(new URL(myUrl))
93+
JsonParser.parseString(myJson).getAsJsonObject
94+
} catch {
95+
case ex: IllegalArgumentException =>
96+
throw ex
97+
case ex2: Exception =>
98+
throw new RuntimeException(ex2)
99+
}
100+
}

IP2ProxyScala/src/test/scala/IP2ProxyTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class IP2ProxyTest extends AnyFunSuite with BeforeAndAfter with BeforeAndAfterAl
1212
private val ip = "8.8.8.8"
1313

1414

15-
override def beforeAll {
15+
override def beforeAll: Unit = {
1616
val binpath = Paths.get("src", "test", "resources", binfile)
1717
binfilepath = binpath.toFile.getAbsolutePath
1818
}

README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ This library allows user to query an IP address if it was being used as VPN anon
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 library 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
## Requirements ##
913
Intellij IDEA: https://www.jetbrains.com/idea/
1014

15+
## QUERY USING THE BIN FILE
16+
1117
## Methods
12-
Below are the methods supported in this class.
18+
Below are the methods supported in this library.
1319

1420
|Method Name|Description|
1521
|---|---|
@@ -124,3 +130,69 @@ object IP2ProxyTest {
124130
}
125131
```
126132

133+
## QUERY USING THE IP2PROXY PROXY DETECTION WEB SERVICE
134+
135+
## Methods
136+
Below are the methods supported in this library.
137+
138+
|Method Name|Description|
139+
|---|---|
140+
|Open| Expects 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX11)</li></li><li>Use HTTPS or HTTP</li></ol> |
141+
|IPQuery|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>|
142+
|GetCredit|This method returns the web service credit balance in a JsonObject.|
143+
144+
## Usage
145+
146+
```scala
147+
object IP2ProxyTest {
148+
def main(args: Array[String]): Unit = {
149+
try {
150+
val ws = new IP2ProxyWebService
151+
val strIPAddress = "8.8.8.8"
152+
val strAPIKey = "YOUR_API_KEY"
153+
val strPackage = "PX11"
154+
val boolSSL = true
155+
ws.Open(strAPIKey, strPackage, boolSSL)
156+
var myResult = ws.IPQuery(strIPAddress)
157+
if (myResult.get("response") != null && myResult.get("response").getAsString == "OK") {
158+
System.out.println("countryCode: " + (if (myResult.get("countryCode") != null) myResult.get("countryCode").getAsString
159+
else ""))
160+
System.out.println("countryName: " + (if (myResult.get("countryName") != null) myResult.get("countryName").getAsString
161+
else ""))
162+
System.out.println("regionName: " + (if (myResult.get("regionName") != null) myResult.get("regionName").getAsString
163+
else ""))
164+
System.out.println("cityName: " + (if (myResult.get("cityName") != null) myResult.get("cityName").getAsString
165+
else ""))
166+
System.out.println("isp: " + (if (myResult.get("isp") != null) myResult.get("isp").getAsString
167+
else ""))
168+
System.out.println("domain: " + (if (myResult.get("domain") != null) myResult.get("domain").getAsString
169+
else ""))
170+
System.out.println("usageType: " + (if (myResult.get("usageType") != null) myResult.get("usageType").getAsString
171+
else ""))
172+
System.out.println("asn: " + (if (myResult.get("asn") != null) myResult.get("asn").getAsString
173+
else ""))
174+
System.out.println("as: " + (if (myResult.get("as") != null) myResult.get("as").getAsString
175+
else ""))
176+
System.out.println("lastSeen: " + (if (myResult.get("lastSeen") != null) myResult.get("lastSeen").getAsString
177+
else ""))
178+
System.out.println("proxyType: " + (if (myResult.get("proxyType") != null) myResult.get("proxyType").getAsString
179+
else ""))
180+
System.out.println("threat: " + (if (myResult.get("threat") != null) myResult.get("threat").getAsString
181+
else ""))
182+
System.out.println("isProxy: " + (if (myResult.get("isProxy") != null) myResult.get("isProxy").getAsString
183+
else ""))
184+
System.out.println("provider: " + (if (myResult.get("provider") != null) myResult.get("provider").getAsString
185+
else ""))
186+
}
187+
else if (myResult.get("response") != null) System.out.println("Error: " + myResult.get("response").getAsString)
188+
myResult = ws.GetCredit
189+
if (myResult.get("response") != null) System.out.println("Credit balance: " + myResult.get("response").getAsString)
190+
} catch {
191+
case e: Exception =>
192+
System.out.println(e)
193+
e.printStackTrace(System.out)
194+
}
195+
}
196+
}
197+
198+
```

0 commit comments

Comments
 (0)