Skip to content

Commit bd9af21

Browse files
committed
Added support for IP2Proxy Web Service
1 parent bd07027 commit bd9af21

File tree

6 files changed

+331
-35
lines changed

6 files changed

+331
-35
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Imports System.Net
2+
Imports System.Text
3+
4+
Public Class Http
5+
Public Function GetMethod(ByVal url As String) As String
6+
Dim request As HttpWebRequest
7+
Dim response As HttpWebResponse
8+
request = WebRequest.Create(url)
9+
request.Method = "GET"
10+
response = request.GetResponse()
11+
If response.StatusCode = HttpStatusCode.OK Then
12+
Dim reader As New IO.StreamReader(response.GetResponseStream())
13+
Dim raw As String = reader.ReadToEnd
14+
Return raw
15+
Else
16+
Return ("Failed : HTTP error code :" & response.StatusCode)
17+
End If
18+
End Function
19+
Public Function PostMethod(ByVal url As String, post As String) As String
20+
Dim request As HttpWebRequest
21+
Dim response As HttpWebResponse
22+
Dim encode As UTF8Encoding
23+
Dim postdata As String = post
24+
Dim postdatabytes As Byte()
25+
26+
request = WebRequest.Create(url)
27+
encode = New UTF8Encoding()
28+
postdatabytes = encode.GetBytes(postdata)
29+
request.Method = "POST"
30+
request.ContentType = "application/x-www-form-urlencoded"
31+
request.ContentLength = postdatabytes.Length
32+
33+
Using stream = request.GetRequestStream
34+
stream.Write(postdatabytes, 0, postdatabytes.Length)
35+
End Using
36+
response = request.GetResponse()
37+
If response.StatusCode = HttpStatusCode.OK Then
38+
Dim reader As New IO.StreamReader(response.GetResponseStream())
39+
Dim raw As String = reader.ReadToEnd
40+
Return raw
41+
Else
42+
Return ("Failed : HTTP error code :" & response.StatusCode)
43+
End If
44+
End Function
45+
End Class

IP2ProxyComponent/IP2ProxyComponent/IP2ProxyComponent.vbproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<RootNamespace>IP2Proxy</RootNamespace>
55
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
66
<AssemblyName>IP2Proxy</AssemblyName>
7-
<Version>3.1.0</Version>
7+
<Version>3.2.0</Version>
88
<Authors>IP2Proxy.com</Authors>
99
<Company>IP2Proxy.com</Company>
1010
<Product>IP2Proxy .NET Component</Product>
@@ -15,7 +15,7 @@
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageProjectUrl>https://www.ip2location.com/development-libraries/ip2proxy/dot-net</PackageProjectUrl>
1717
<PackageIcon>ip2proxy-logo-square-128.png</PackageIcon>
18-
<PackageReleaseNotes>Added provider field and exception handling for incorrect BIN database.</PackageReleaseNotes>
18+
<PackageReleaseNotes>Added support for IP2Proxy Web Service.</PackageReleaseNotes>
1919
<RepositoryUrl>https://github.com/ip2location/ip2proxy-dotnet.git</RepositoryUrl>
2020
<PackageTags>ip2proxy proxy detection</PackageTags>
2121
<RepositoryType>git</RepositoryType>
@@ -28,4 +28,8 @@
2828
</None>
2929
</ItemGroup>
3030

31+
<ItemGroup>
32+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
33+
</ItemGroup>
34+
3135
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Imports System.Text.RegularExpressions
2+
Imports Newtonsoft.Json
3+
Imports Newtonsoft.Json.Linq
4+
5+
Public NotInheritable Class ComponentWebService
6+
Private _APIKey As String = ""
7+
Private _Package As String = ""
8+
Private _UseSSL As Boolean = True
9+
Private ReadOnly _RegexAPIKey As New Regex("^[\dA-Z]{10}$")
10+
Private ReadOnly _RegexPackage As New Regex("^PX\d+$")
11+
Private Const BASE_URL As String = "api.ip2proxy.com/"
12+
13+
' Description: Initialize
14+
Public Sub New()
15+
End Sub
16+
17+
' Description: Set the API key and package for the queries
18+
Public Sub Open(ByVal APIKey As String, ByVal Package As String, ByVal Optional UseSSL As Boolean = True)
19+
_APIKey = APIKey
20+
_Package = Package
21+
_UseSSL = UseSSL
22+
23+
CheckParams()
24+
End Sub
25+
26+
' Description: Validate API key and package
27+
Private Sub CheckParams()
28+
If Not _RegexAPIKey.IsMatch(_APIKey) AndAlso _APIKey <> "demo" Then
29+
Throw New Exception("Invalid API key.")
30+
End If
31+
32+
If Not _RegexPackage.IsMatch(_Package) Then
33+
Throw New Exception("Invalid package name.")
34+
End If
35+
End Sub
36+
37+
' Description: Query web service to get location information by IP address
38+
Public Function IPQuery(ByVal IP As String) As JObject
39+
CheckParams() ' check here in case user haven't called Open yet
40+
41+
Dim url As String
42+
Dim protocol As String = If(_UseSSL, "https", "http")
43+
url = protocol & "://" & BASE_URL & "?key=" & _APIKey & "&package=" & _Package & "&ip=" & Net.WebUtility.UrlEncode(IP)
44+
Dim request As New Http
45+
Dim rawjson As String
46+
rawjson = request.GetMethod(url)
47+
Dim results = JsonConvert.DeserializeObject(Of Object)(rawjson)
48+
49+
Return results
50+
End Function
51+
52+
' Description: Check web service credit balance
53+
Public Function GetCredit() As JObject
54+
CheckParams() ' check here in case user haven't called Open yet
55+
56+
Dim url As String
57+
Dim protocol As String = If(_UseSSL, "https", "http")
58+
url = protocol & "://" & BASE_URL & "?key=" & _APIKey & "&check=true"
59+
Dim request As New Http
60+
Dim rawjson As String
61+
rawjson = request.GetMethod(url)
62+
Dim results = JsonConvert.DeserializeObject(Of Object)(rawjson)
63+
64+
Return results
65+
End Function
66+
67+
End Class

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ 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
## Requirements
913

1014
Microsoft .NET 4.72 framework or later.
1115
Compatible with .NET Core 2.x/3.x SDK.
1216

17+
## QUERY USING THE BIN FILE
18+
1319
## Methods
1420
Below are the methods supported in this class.
1521

@@ -134,3 +140,98 @@ End If
134140
proxy.Close()
135141

136142
```
143+
144+
## QUERY USING THE IP2PROXY PROXY DETECTION WEB SERVICE
145+
146+
## Methods
147+
Below are the methods supported in this class.
148+
149+
|Method Name|Description|
150+
|---|---|
151+
|Open(ByVal APIKey As String, ByVal Package As String, ByVal Optional UseSSL As Boolean = True)| Expects 2 or 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX11)</li></li><li>Use HTTPS or HTTP</li></ol> |
152+
|IPQuery(ByVal IP As String)|Query IP address. This method returns a JObject 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>|
153+
|GetCredit()|This method returns the web service credit balance in a JObject.|
154+
155+
## Usage
156+
157+
```vb.net
158+
Imports System.IO
159+
160+
Module TestIP2Proxy
161+
162+
Sub Main()
163+
Dim proxyws As New IP2Proxy.ComponentWebService()
164+
Dim ip = "221.121.146.0"
165+
Dim apikey = "YOUR_API_KEY"
166+
Dim package = "PX11"
167+
Dim ssl = True
168+
169+
proxyws.Open(apikey, package, ssl)
170+
Dim myresult = proxyws.IPQuery(ip)
171+
172+
If myresult.ContainsKey("response") Then
173+
If myresult("response").ToString <> "OK" Then
174+
Console.WriteLine("Error: " & myresult("response").ToString)
175+
Else
176+
Console.WriteLine("countryCode: " & If(myresult.ContainsKey("countryCode"), myresult("countryCode").ToString, ""))
177+
Console.WriteLine("countryName: " & If(myresult.ContainsKey("countryName"), myresult("countryName").ToString, ""))
178+
Console.WriteLine("regionName: " & If(myresult.ContainsKey("regionName"), myresult("regionName").ToString, ""))
179+
Console.WriteLine("cityName: " & If(myresult.ContainsKey("cityName"), myresult("cityName").ToString, ""))
180+
Console.WriteLine("isp: " & If(myresult.ContainsKey("isp"), myresult("isp").ToString, ""))
181+
Console.WriteLine("domain: " & If(myresult.ContainsKey("domain"), myresult("domain").ToString, ""))
182+
Console.WriteLine("usageType: " & If(myresult.ContainsKey("usageType"), myresult("usageType").ToString, ""))
183+
Console.WriteLine("asn: " & If(myresult.ContainsKey("asn"), myresult("asn").ToString, ""))
184+
Console.WriteLine("as: " & If(myresult.ContainsKey("as"), myresult("as").ToString, ""))
185+
Console.WriteLine("lastSeen: " & If(myresult.ContainsKey("lastSeen"), myresult("lastSeen").ToString, ""))
186+
Console.WriteLine("proxyType: " & If(myresult.ContainsKey("proxyType"), myresult("proxyType").ToString, ""))
187+
Console.WriteLine("threat: " & If(myresult.ContainsKey("threat"), myresult("threat").ToString, ""))
188+
Console.WriteLine("isProxy: " & If(myresult.ContainsKey("isProxy"), myresult("isProxy").ToString, ""))
189+
Console.WriteLine("provider: " & If(myresult.ContainsKey("provider"), myresult("provider").ToString, ""))
190+
End If
191+
End If
192+
193+
myresult = proxyws.GetCredit()
194+
If myresult.ContainsKey("response") Then
195+
Console.WriteLine("Credit balance: " & If(myresult.ContainsKey("response"), myresult("response").ToString, ""))
196+
End If
197+
End Sub
198+
199+
End Module
200+
```
201+
202+
### Proxy Type
203+
204+
|Proxy Type|Description|
205+
|---|---|
206+
|VPN|Anonymizing VPN services|
207+
|TOR|Tor Exit Nodes|
208+
|PUB|Public Proxies|
209+
|WEB|Web Proxies|
210+
|DCH|Hosting Providers/Data Center|
211+
|SES|Search Engine Robots|
212+
|RES|Residential Proxies [PX10+]|
213+
214+
### Usage Type
215+
216+
|Usage Type|Description|
217+
|---|---|
218+
|COM|Commercial|
219+
|ORG|Organization|
220+
|GOV|Government|
221+
|MIL|Military|
222+
|EDU|University/College/School|
223+
|LIB|Library|
224+
|CDN|Content Delivery Network|
225+
|ISP|Fixed Line ISP|
226+
|MOB|Mobile ISP|
227+
|DCH|Data Center/Web Hosting/Transit|
228+
|SES|Search Engine Spider|
229+
|RSV|Reserved|
230+
231+
### Threat Type
232+
233+
|Threat Type|Description|
234+
|---|---|
235+
|SPAM|Spammer|
236+
|SCANNER|Security Scanner or Attack|
237+
|BOTNET|Spyware or Malware|

0 commit comments

Comments
 (0)