Skip to content

Commit

Permalink
Added support for PX5 to PX8 packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed May 27, 2019
1 parent 6829550 commit 91df180
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 26 deletions.
2 changes: 1 addition & 1 deletion LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 IP2Location.com
Copyright (c) 2019 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# IP2Proxy D Library

This D library allows user to query an IP address if it was being used as open proxy, web proxy, VPN anonymizer and TOR exits. It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at
This D library allows user to query an IP address if it was being used as VPN anonymizer, open proxies, web proxies, Tor exits, data center, web hosting (DCH) range and search engine robots (SES). It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at

* Free IP2Proxy BIN Data: http://lite.ip2location.com
* Commercial IP2Proxy BIN Data: http://www.ip2location.com/proxy-database
* Commercial IP2Proxy BIN Data: https://www.ip2location.com/database/ip2proxy


## Installation
Expand All @@ -23,17 +23,22 @@ Below are the methods supported in this library.
|---|---|
|open|Open the IP2Proxy BIN data for lookup.|
|close|Close and reset metadata.|
|package_version|Get the package version (1 to 4 for PX1 to PX4 respectively).|
|package_version|Get the package version (1 to 8 for PX1 to PX8 respectively).|
|module_version|Get the module version.|
|database_version|Get the database version.|
|is_proxy|Check whether if an IP address was a proxy. Returned value:<ul><li>-1 : errors</li><li>0 : not a proxy</li><li>1 : a proxy</li><li>2 : a data center IP address</li></ul>|
|is_proxy|Check whether if an IP address was a proxy. Returned value:<ul><li>-1 : errors</li><li>0 : not a proxy</li><li>1 : a proxy</li><li>2 : a data center IP address or search engine robot</li></ul>|
|get_all|Return the proxy information in an array.|
|get_proxy_type|Return the proxy type. Please visit <a href="https://www.ip2location.com/databases/px4-ip-proxytype-country-region-city-isp" target="_blank">IP2Location</a> for the list of proxy types supported|
|get_proxy_type|Return the proxy type. Please visit <a href="https://www.ip2location.com/database/px8-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen" target="_blank">IP2Location</a> for the list of proxy types supported.|
|get_country_short|Return the ISO3166-1 country code (2-digits) of the proxy.|
|get_country_long|Return the ISO3166-1 country name of the proxy.|
|get_region|Return the ISO3166-2 region name of the proxy. Please visit <a href="https://www.ip2location.com/free/iso3166-2" target="_blank">ISO3166-2 Subdivision Code</a> for the information of ISO3166-2 supported|
|get_region|Return the ISO3166-2 region name of the proxy. Please visit <a href="https://www.ip2location.com/free/iso3166-2" target="_blank">ISO3166-2 Subdivision Code</a> for the information of ISO3166-2 supported.|
|get_city|Return the city name of the proxy.|
|get_isp|Return the ISP name of the proxy.|
|get_domain|Return the domain name of the proxy.|
|get_usage_type|Return the usage type classification of the proxy. Please visit <a href="https://www.ip2location.com/database/px8-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen" target="_blank">IP2Location</a> for the list of usage types supported.|
|get_asn|Return the autonomous system number of the proxy.|
|get_as|Return the autonomous system name of the proxy.|
|get_last_seen|Return the number of days that the proxy was last seen.|

## Usage

Expand All @@ -42,7 +47,7 @@ import std.stdio;
import ip2proxy;
int main() {
string db = "./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.BIN";
string db = "./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.BIN";
auto prox = new ip2proxy();
if (prox.open(db) == 0) {
Expand All @@ -60,6 +65,11 @@ int main() {
writeln("Region: ", prox.get_region(ip));
writeln("City: ", prox.get_city(ip));
writeln("ISP: ", prox.get_isp(ip));
writeln("Domain: ", prox.get_domain(ip));
writeln("UsageType: ", prox.get_usage_type(ip));
writeln("ASN: ", prox.get_asn(ip));
writeln("AS: ", prox.get_as(ip));
writeln("LastSeen: ", prox.get_last_seen(ip));
// function for all fields
auto all = prox.get_all(ip);
Expand All @@ -70,6 +80,11 @@ int main() {
writeln("Region: ", all["Region"]);
writeln("City: ", all["City"]);
writeln("ISP: ", all["ISP"]);
writeln("Domain: ", all["Domain"]);
writeln("UsageType: ", all["UsageType"]);
writeln("ASN: ", all["ASN"]);
writeln("AS: ", all["AS"]);
writeln("LastSeen: ", all["LastSeen"]);
}
else {
writeln("Error reading BIN file.");
Expand Down
4 changes: 2 additions & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"IP2Location"
],
"description": "IP2Proxy D Library",
"homepage": "https://www.ip2location.com/ip2proxy/developers/d",
"copyright": "Copyright © 2018, IP2Location",
"homepage": "https://www.ip2location.com/development-libraries/ip2proxy/d",
"copyright": "Copyright © 2019, IP2Location",
"license": "MIT",
"targetType": "library"
}
12 changes: 11 additions & 1 deletion source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import std.stdio;
import ip2proxy;

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

if (prox.open(db) == 0) {
Expand All @@ -20,6 +20,11 @@ int main() {
writeln("Region: ", prox.get_region(ip));
writeln("City: ", prox.get_city(ip));
writeln("ISP: ", prox.get_isp(ip));
writeln("Domain: ", prox.get_domain(ip));
writeln("UsageType: ", prox.get_usage_type(ip));
writeln("ASN: ", prox.get_asn(ip));
writeln("AS: ", prox.get_as(ip));
writeln("LastSeen: ", prox.get_last_seen(ip));

// function for all fields
auto all = prox.get_all(ip);
Expand All @@ -30,6 +35,11 @@ int main() {
writeln("Region: ", all["Region"]);
writeln("City: ", all["City"]);
writeln("ISP: ", all["ISP"]);
writeln("Domain: ", all["Domain"]);
writeln("UsageType: ", all["UsageType"]);
writeln("ASN: ", all["ASN"]);
writeln("AS: ", all["AS"]);
writeln("LastSeen: ", all["LastSeen"]);
}
else {
writeln("Error reading BIN file.");
Expand Down
132 changes: 117 additions & 15 deletions source/ip2proxy-d/ip2proxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ protected struct ip2proxyrecord {
string city = "-";
string isp = "-";
string proxy_type = "-";
string domain = "-";
string usage_type = "-";
string asn = "-";
string as = "-";
string last_seen = "-";
byte is_proxy = -1;
}

Expand All @@ -39,26 +44,36 @@ protected struct ipv {
uint ipindex = 0;
}

const ubyte[5] COUNTRY_POSITION = [0, 2, 3, 3, 3];
const ubyte[5] REGION_POSITION = [0, 0, 0, 4, 4];
const ubyte[5] CITY_POSITION = [0, 0, 0, 5, 5];
const ubyte[5] ISP_POSITION = [0, 0, 0, 0, 6];
const ubyte[5] PROXYTYPE_POSITION = [0, 0, 2, 2, 2];
const ubyte[9] COUNTRY_POSITION = [0, 2, 3, 3, 3, 3, 3, 3, 3];
const ubyte[9] REGION_POSITION = [0, 0, 0, 4, 4, 4, 4, 4, 4];
const ubyte[9] CITY_POSITION = [0, 0, 0, 5, 5, 5, 5, 5, 5];
const ubyte[9] ISP_POSITION = [0, 0, 0, 0, 6, 6, 6, 6, 6];
const ubyte[9] PROXYTYPE_POSITION = [0, 0, 2, 2, 2, 2, 2, 2, 2];
const ubyte[9] DOMAIN_POSITION = [0, 0, 0, 0, 0, 7, 7, 7, 7];
const ubyte[9] USAGETYPE_POSITION = [0, 0, 0, 0, 0, 0, 8, 8, 8];
const ubyte[9] ASN_POSITION = [0, 0, 0, 0, 0, 0, 0, 9, 9];
const ubyte[9] AS_POSITION = [0, 0, 0, 0, 0, 0, 0, 10, 10];
const ubyte[9] LASTSEEN_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 11];

protected const string MODULE_VERSION = "1.0.0";
protected const string MODULE_VERSION = "2.0.0";

protected const BigInt MAX_IPV4_RANGE = BigInt("4294967295");
protected const BigInt MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");

protected const uint COUNTRYSHORT = 0x00001;
protected const uint COUNTRYLONG = 0x00002;
protected const uint REGION = 0x00004;
protected const uint CITY = 0x00008;
protected const uint ISP = 0x00010;
protected const uint PROXYTYPE = 0x00020;
protected const uint ISPROXY = 0x00040;
protected const uint COUNTRYSHORT = 0X00001;
protected const uint COUNTRYLONG = 0X00002;
protected const uint REGION = 0X00004;
protected const uint CITY = 0X00008;
protected const uint ISP = 0X00010;
protected const uint PROXYTYPE = 0X00020;
protected const uint ISPROXY = 0X00040;
protected const uint DOMAIN = 0X00080;
protected const uint USAGETYPE = 0X00100;
protected const uint ASN = 0X00200;
protected const uint AS = 0X00400;
protected const uint LASTSEEN = 0X00800;

protected const uint ALL = COUNTRYSHORT | COUNTRYLONG | REGION | CITY | ISP | PROXYTYPE | ISPROXY;
protected const uint ALL = COUNTRYSHORT | COUNTRYLONG | REGION | CITY | ISP | PROXYTYPE | ISPROXY | DOMAIN | USAGETYPE | ASN | AS | LASTSEEN;

protected const string MSG_NOT_SUPPORTED = "NOT SUPPORTED";
protected const string MSG_INVALID_IP = "INVALID IP ADDRESS";
Expand All @@ -76,12 +91,22 @@ class ip2proxy {
private uint city_position_offset;
private uint isp_position_offset;
private uint proxytype_position_offset;
private uint domain_position_offset;
private uint usagetype_position_offset;
private uint asn_position_offset;
private uint as_position_offset;
private uint lastseen_position_offset;

private bool country_enabled;
private bool region_enabled;
private bool city_enabled;
private bool isp_enabled;
private bool proxytype_enabled;
private bool domain_enabled;
private bool usagetype_enabled;
private bool asn_enabled;
private bool as_enabled;
private bool lastseen_enabled;

// constructor
public this() {
Expand Down Expand Up @@ -130,11 +155,23 @@ class ip2proxy {
city_position_offset = 0;
isp_position_offset = 0;
proxytype_position_offset = 0;
domain_position_offset = 0;
usagetype_position_offset = 0;
asn_position_offset = 0;
as_position_offset = 0;
lastseen_position_offset = 0;
country_enabled = false;
region_enabled = false;
city_enabled = false;
isp_enabled = false;
proxytype_enabled = false;
domain_enabled = false;
usagetype_enabled = false;
asn_enabled = false;
as_enabled = false;
lastseen_enabled = false;

destroy(db);

return 0;
}
Expand Down Expand Up @@ -219,12 +256,22 @@ class ip2proxy {
city_position_offset = (CITY_POSITION[dbt] != 0) ? (CITY_POSITION[dbt] - 1) << 2 : 0;
isp_position_offset = (ISP_POSITION[dbt] != 0) ? (ISP_POSITION[dbt] - 1) << 2 : 0;
proxytype_position_offset = (PROXYTYPE_POSITION[dbt] != 0) ? (PROXYTYPE_POSITION[dbt] - 1) << 2 : 0;
domain_position_offset = (DOMAIN_POSITION[dbt] != 0) ? (DOMAIN_POSITION[dbt] - 1) << 2 : 0;
usagetype_position_offset = (USAGETYPE_POSITION[dbt] != 0) ? (USAGETYPE_POSITION[dbt] - 1) << 2 : 0;
asn_position_offset = (ASN_POSITION[dbt] != 0) ? (ASN_POSITION[dbt] - 1) << 2 : 0;
as_position_offset = (AS_POSITION[dbt] != 0) ? (AS_POSITION[dbt] - 1) << 2 : 0;
lastseen_position_offset = (LASTSEEN_POSITION[dbt] != 0) ? (LASTSEEN_POSITION[dbt] - 1) << 2 : 0;

country_enabled = (COUNTRY_POSITION[dbt] != 0) ? true : false;
region_enabled = (REGION_POSITION[dbt] != 0) ? true : false;
city_enabled = (CITY_POSITION[dbt] != 0) ? true : false;
isp_enabled = (ISP_POSITION[dbt] != 0) ? true : false;
proxytype_enabled = (PROXYTYPE_POSITION[dbt] != 0) ? true : false;
domain_enabled = (DOMAIN_POSITION[dbt] != 0) ? true : false;
usagetype_enabled = (USAGETYPE_POSITION[dbt] != 0) ? true : false;
asn_enabled = (ASN_POSITION[dbt] != 0) ? true : false;
as_enabled = (AS_POSITION[dbt] != 0) ? true : false;
lastseen_enabled = (LASTSEEN_POSITION[dbt] != 0) ? true : false;

metaok = true;
}
Expand Down Expand Up @@ -320,6 +367,11 @@ class ip2proxy {
x["Region"] = data.region;
x["City"] = data.city;
x["ISP"] = data.isp;
x["Domain"] = data.domain;
x["UsageType"] = data.usage_type;
x["ASN"] = data.asn;
x["AS"] = data.as;
x["LastSeen"] = data.last_seen;

return x;
}
Expand Down Expand Up @@ -360,6 +412,36 @@ class ip2proxy {
return data.proxy_type;
}

// get domain
public auto get_domain(const string ipaddress) {
auto data = query(ipaddress, DOMAIN);
return data.domain;
}

// get usage type
public auto get_usage_type(const string ipaddress) {
auto data = query(ipaddress, USAGETYPE);
return data.usage_type;
}

// get asn
public auto get_asn(const string ipaddress) {
auto data = query(ipaddress, ASN);
return data.asn;
}

// get as
public auto get_as(const string ipaddress) {
auto data = query(ipaddress, AS);
return data.as;
}

// get last seen
public auto get_last_seen(const string ipaddress) {
auto data = query(ipaddress, LASTSEEN);
return data.last_seen;
}

// is proxy
public auto is_proxy(const string ipaddress) {
auto data = query(ipaddress, ISPROXY);
Expand Down Expand Up @@ -473,11 +555,31 @@ class ip2proxy {
x.isp = readstr(readuint(rowoffset + isp_position_offset));
}

if ((mode & DOMAIN) && (domain_enabled)) {
x.domain = readstr(readuint(rowoffset + domain_position_offset));
}

if ((mode & USAGETYPE) && (usagetype_enabled)) {
x.usage_type = readstr(readuint(rowoffset + usagetype_position_offset));
}

if ((mode & ASN) && (asn_enabled)) {
x.asn = readstr(readuint(rowoffset + asn_position_offset));
}

if ((mode & AS) && (as_enabled)) {
x.as = readstr(readuint(rowoffset + as_position_offset));
}

if ((mode & LASTSEEN) && (lastseen_enabled)) {
x.last_seen = readstr(readuint(rowoffset + lastseen_position_offset));
}

if ((x.country_short == "-") || (x.proxy_type == "-")) {
x.is_proxy = 0;
}
else {
if (x.proxy_type == "DCH") {
if ((x.proxy_type == "DCH") || (x.proxy_type == "SES")) {
x.is_proxy = 2;
}
else {
Expand Down

0 comments on commit 91df180

Please sign in to comment.