Skip to content

Commit

Permalink
Added support for IP2Proxy Web Service
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Sep 9, 2021
1 parent cf032ad commit ad4a1e7
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This D library allows user to query an IP address if it was being used as VPN an
* Free IP2Proxy BIN Data: http://lite.ip2location.com
* Commercial IP2Proxy BIN Data: https://www.ip2location.com/database/ip2proxy

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:

https://www.ip2location.com/web-service/ip2proxy

## Installation

Expand All @@ -16,6 +19,8 @@ To install this library using dub:
}
```

## QUERY USING THE BIN FILE

## Methods
Below are the methods supported in this library.

Expand Down Expand Up @@ -100,3 +105,106 @@ int main() {
return 0;
}
```

## QUERY USING THE IP2PROXY PROXY DETECTION WEB SERVICE

## Methods
Below are the methods supported in this class.

|Method Name|Description|
|---|---|
|open(const string apikey, const string apipackage, bool usessl = true)| Expects 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX11)</li></li><li>Use HTTPS or HTTP</li></ol>|
|lookup(const string ipaddress)|Query IP address. This method returns a JSONValue 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>|
|get_credit()|This method returns the web service credit balance in a JSONValue.|

## Usage

```d
import std.stdio;
import std.conv : to;
import ip2proxywebservice : ip2proxywebservice;
int main() {
auto ip = "8.8.8.8";
auto apikey = "YOUR_API_KEY";
auto apipackage = "PX11";
auto usessl = true;
auto ws = new ip2proxywebservice();
ws.open(apikey, apipackage, usessl);
auto result = ws.lookup(ip);
if ("response" in result && result["response"].str == "OK") {
writefln("countryCode: %s", ("countryCode" in result) ? result["countryCode"].str : "");
writefln("countryName: %s", ("countryName" in result) ? result["countryName"].str : "");
writefln("regionName: %s", ("regionName" in result) ? result["regionName"].str : "");
writefln("cityName: %s", ("cityName" in result) ? result["cityName"].str : "");
writefln("isp: %s", ("isp" in result) ? result["isp"].str : "");
writefln("domain: %s", ("domain" in result) ? result["domain"].str : "");
writefln("usageType: %s", ("usageType" in result) ? result["usageType"].str : "");
writefln("asn: %s", ("asn" in result) ? result["asn"].str : "");
writefln("as: %s", ("as" in result) ? result["as"].str : "");
writefln("lastSeen: %s", ("lastSeen" in result) ? result["lastSeen"].str : "");
writefln("proxyType: %s", ("proxyType" in result) ? result["proxyType"].str : "");
writefln("threat: %s", ("threat" in result) ? result["threat"].str : "");
writefln("isProxy: %s", ("isProxy" in result) ? result["isProxy"].str : "");
writefln("provider: %s", ("provider" in result) ? result["provider"].str : "");
}
else if ("response" in result) {
writefln("Error: %s", result["response"]);
}
else {
writeln("Error: Unknown error.");
}
auto result2 = ws.get_credit();
if ("response" in result2) {
writefln("Credit balance: %d", to!int(result2["response"].str));
}
else {
writeln("Error: Unknown error.");
}
return 0;
}
```

### Proxy Type

|Proxy Type|Description|
|---|---|
|VPN|Anonymizing VPN services|
|TOR|Tor Exit Nodes|
|PUB|Public Proxies|
|WEB|Web Proxies|
|DCH|Hosting Providers/Data Center|
|SES|Search Engine Robots|
|RES|Residential Proxies [PX10+]|

### Usage Type

|Usage Type|Description|
|---|---|
|COM|Commercial|
|ORG|Organization|
|GOV|Government|
|MIL|Military|
|EDU|University/College/School|
|LIB|Library|
|CDN|Content Delivery Network|
|ISP|Fixed Line ISP|
|MOB|Mobile ISP|
|DCH|Data Center/Web Hosting/Transit|
|SES|Search Engine Spider|
|RSV|Reserved|

### Threat Type

|Threat Type|Description|
|---|---|
|SPAM|Spammer|
|SCANNER|Security Scanner or Attack|
|BOTNET|Spyware or Malware|
47 changes: 47 additions & 0 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import std.stdio;
import std.conv : to;
import ip2proxy : ip2proxy;
import ip2proxywebservice : ip2proxywebservice;

int main() {
// Query using BIN file
string db = "./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN";
auto prox = new ip2proxy();

Expand Down Expand Up @@ -50,5 +53,49 @@ int main() {
}
prox.close();

// Query using web service
auto ip = "8.8.8.8";
auto apikey = "YOUR_API_KEY";
auto apipackage = "PX11";
auto usessl = true;

auto ws = new ip2proxywebservice();

ws.open(apikey, apipackage, usessl);

auto result = ws.lookup(ip);

if ("response" in result && result["response"].str == "OK") {
writefln("countryCode: %s", ("countryCode" in result) ? result["countryCode"].str : "");
writefln("countryName: %s", ("countryName" in result) ? result["countryName"].str : "");
writefln("regionName: %s", ("regionName" in result) ? result["regionName"].str : "");
writefln("cityName: %s", ("cityName" in result) ? result["cityName"].str : "");
writefln("isp: %s", ("isp" in result) ? result["isp"].str : "");
writefln("domain: %s", ("domain" in result) ? result["domain"].str : "");
writefln("usageType: %s", ("usageType" in result) ? result["usageType"].str : "");
writefln("asn: %s", ("asn" in result) ? result["asn"].str : "");
writefln("as: %s", ("as" in result) ? result["as"].str : "");
writefln("lastSeen: %s", ("lastSeen" in result) ? result["lastSeen"].str : "");
writefln("proxyType: %s", ("proxyType" in result) ? result["proxyType"].str : "");
writefln("threat: %s", ("threat" in result) ? result["threat"].str : "");
writefln("isProxy: %s", ("isProxy" in result) ? result["isProxy"].str : "");
writefln("provider: %s", ("provider" in result) ? result["provider"].str : "");
}
else if ("response" in result) {
writefln("Error: %s", result["response"]);
}
else {
writeln("Error: Unknown error.");
}

auto result2 = ws.get_credit();

if ("response" in result2) {
writefln("Credit balance: %d", to!int(result2["response"].str));
}
else {
writeln("Error: Unknown error.");
}

return 0;
}
2 changes: 1 addition & 1 deletion source/ip2proxy-d/ip2proxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const ubyte[12] LASTSEEN_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11];
const ubyte[12] THREAT_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12];
const ubyte[12] PROVIDER_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13];

protected const string MODULE_VERSION = "3.1.0";
protected const string MODULE_VERSION = "3.2.0";

protected const BigInt MAX_IPV4_RANGE = BigInt("4294967295");
protected const BigInt MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
Expand Down
57 changes: 57 additions & 0 deletions source/ip2proxy-d/ip2proxywebservice.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import std.uri;
import std.regex;
import std.stdio;
import std.net.curl;
import std.json;

class ip2proxywebservice {
private string _apikey = "";
private string _apipackage = "";
private bool _usessl = true;
private const string baseurl = "api.ip2proxy.com/";

// constructor
public this() {
}

// initialize with API key and package
public void open(const string apikey, const string apipackage, bool usessl = true) {
_apikey = apikey;
_apipackage = apipackage;
_usessl = usessl;
checkparams();
}

// check params
public void checkparams() {
auto apikeypattern = ctRegex!(`^[\dA-Z]{10}$`);
auto apipackagepattern = ctRegex!(`^PX\d+$`);

if (!matchFirst(_apikey, apikeypattern) && _apikey != "demo") {
throw new Exception("Invalid API key.");
}
if (!matchFirst(_apipackage, apipackagepattern)) {
throw new Exception("Invalid package name.");
}
}

// query web service for proxy information
public auto lookup(const string ipaddress) {
checkparams(); // check here in case user haven't called open yet
auto protocol = (_usessl) ? "https" : "http";
auto url = protocol ~ "://" ~ baseurl ~ "?key=" ~ _apikey ~ "&package=" ~ _apipackage ~ "&ip=" ~ ipaddress.encode;
auto content = get(url);
JSONValue j = parseJSON(content);
return j;
}

// check web service credit balance
public auto get_credit() {
checkparams(); // check here in case user haven't called open yet
auto protocol = (_usessl) ? "https" : "http";
auto url = protocol ~ "://" ~ baseurl ~ "?key=" ~ _apikey ~ "&check=true";
auto content = get(url);
JSONValue j = parseJSON(content);
return j;
}
}

0 comments on commit ad4a1e7

Please sign in to comment.