Skip to content

Commit

Permalink
Added support for threats (PX9) & residential proxies (PX10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Aug 10, 2020
1 parent c9b2453 commit 416eb79
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 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) 2019 IP2Location.com
Copyright (c) 2020 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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# IP2Proxy Erlang Module

This module 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
This module 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, search engine robots (SES) and residential (RES). It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at

* Free IP2Proxy BIN Data: https://lite.ip2location.com
* Commercial IP2Proxy BIN Data: https://www.ip2location.com/database/ip2proxy
Expand All @@ -19,22 +19,23 @@ Below are the methods supported in this package.
|---|---|
|open|Open the IP2Proxy BIN data for lookup.|
|close|Close and clean up metadata.|
|getpackageversion|Get the package version (1 to 8 for PX1 to PX8 respectively).|
|getpackageversion|Get the package version (1 to 10 for PX1 to PX10 respectively).|
|getmoduleversion|Get the module version.|
|getdatabaseversion|Get the database version.|
|isproxy|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>|
|getall|Return the proxy information in an array.|
|getproxytype|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.|
|getproxytype|Return the proxy type. Please visit <a href="https://www.ip2location.com/database/px10-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential" target="_blank">IP2Location</a> for the list of proxy types supported.|
|getcountryshort|Return the ISO3166-1 country code (2-digits) of the proxy.|
|getcountrylong|Return the ISO3166-1 country name of the proxy.|
|getregion|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.|
|getcity|Return the city name of the proxy.|
|getisp|Return the ISP name of the proxy.|
|getdomain|Return the domain name of the proxy.|
|getusagetype|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.|
|getusagetype|Return the usage type classification of the proxy. Please visit <a href="https://www.ip2location.com/database/px10-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential" target="_blank">IP2Location</a> for the list of usage types supported.|
|getasn|Return the autonomous system number of the proxy.|
|getas|Return the autonomous system name of the proxy.|
|getlastseen|Return the number of days that the proxy was last seen.|
|getthreat|Return the threat type of the proxy.|

## Example

Expand Down
47 changes: 33 additions & 14 deletions ip2proxy.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(ip2proxy).
-export([getpackageversion/0, getmoduleversion/0, getdatabaseversion/0, open/1, getall/1, getproxytype/1, getcountryshort/1, getcountrylong/1, getregion/1, getcity/1, getisp/1, getdomain/1, getusagetype/1, getasn/1, getas/1, getlastseen/1, isproxy/1, close/0]).
-export([getpackageversion/0, getmoduleversion/0, getdatabaseversion/0, open/1, getall/1, getproxytype/1, getcountryshort/1, getcountrylong/1, getregion/1, getcity/1, getisp/1, getdomain/1, getusagetype/1, getasn/1, getas/1, getlastseen/1, getthreat/1, isproxy/1, close/0]).
-record(ip2proxyrecord, {
country_short = "-",
country_long = "-",
Expand All @@ -12,6 +12,7 @@
asn = "-",
as = "-",
last_seen = "-",
threat = "-",
is_proxy = 0
}).
-define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)).
Expand All @@ -32,7 +33,7 @@ getpackageversion() ->
end.

getmoduleversion() ->
"2.2.0".
"3.0.0".

getdatabaseversion() ->
case ets:info(mymeta) of
Expand Down Expand Up @@ -186,16 +187,17 @@ readcolstringrow(S, R, Dbtype, Col) ->
end.

readrecord(S, Dbtype, Rowoffset, Mode) ->
Country_position = [0, 2, 3, 3, 3, 3, 3, 3, 3],
Region_position = [0, 0, 0, 4, 4, 4, 4, 4, 4],
City_position = [0, 0, 0, 5, 5, 5, 5, 5, 5],
Isp_position = [0, 0, 0, 0, 6, 6, 6, 6, 6],
Proxytype_position = [0, 0, 2, 2, 2, 2, 2, 2, 2],
Domain_position = [0, 0, 0, 0, 0, 7, 7, 7, 7],
Usagetype_position = [0, 0, 0, 0, 0, 0, 8, 8, 8],
Asn_position = [0, 0, 0, 0, 0, 0, 0, 9, 9],
As_position = [0, 0, 0, 0, 0, 0, 0, 10, 10],
Lastseen_position = [0, 0, 0, 0, 0, 0, 0, 0, 11],
Country_position = [0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3],
Region_position = [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4],
City_position = [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5],
Isp_position = [0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6],
Proxytype_position = [0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2],
Domain_position = [0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7],
Usagetype_position = [0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8],
Asn_position = [0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9],
As_position = [0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10],
Lastseen_position = [0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11],
Threat_position = [0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12],

Countryshort_field = 1,
Countrylong_field = 2,
Expand All @@ -209,8 +211,9 @@ readrecord(S, Dbtype, Rowoffset, Mode) ->
Asn_field = 512,
As_field = 1024,
Lastseen_field = 2048,
Threat_field = 4096,

Cols = ?IF(lists:nth(Dbtype, Country_position) == 0) + ?IF(lists:nth(Dbtype, Region_position) == 0) + ?IF(lists:nth(Dbtype, City_position) == 0) + ?IF(lists:nth(Dbtype, Isp_position) == 0) + ?IF(lists:nth(Dbtype, Proxytype_position) == 0) + ?IF(lists:nth(Dbtype, Domain_position) == 0) + ?IF(lists:nth(Dbtype, Usagetype_position) == 0) + ?IF(lists:nth(Dbtype, Asn_position) == 0) + ?IF(lists:nth(Dbtype, As_position) == 0) + ?IF(lists:nth(Dbtype, Lastseen_position) == 0),
Cols = ?IF(lists:nth(Dbtype, Country_position) == 0) + ?IF(lists:nth(Dbtype, Region_position) == 0) + ?IF(lists:nth(Dbtype, City_position) == 0) + ?IF(lists:nth(Dbtype, Isp_position) == 0) + ?IF(lists:nth(Dbtype, Proxytype_position) == 0) + ?IF(lists:nth(Dbtype, Domain_position) == 0) + ?IF(lists:nth(Dbtype, Usagetype_position) == 0) + ?IF(lists:nth(Dbtype, Asn_position) == 0) + ?IF(lists:nth(Dbtype, As_position) == 0) + ?IF(lists:nth(Dbtype, Lastseen_position) == 0) + ?IF(lists:nth(Dbtype, Threat_position) == 0),
Rowlength = Cols bsl 2,

case file:pread(S, Rowoffset - 1, Rowlength) of
Expand Down Expand Up @@ -299,6 +302,14 @@ readrecord(S, Dbtype, Rowoffset, Mode) ->
Last_seen = ""
end,

if
Mode band Threat_field /= 0 ->
% Threat = readcolstring(S, Dbtype, Rowoffset, Threat_position);
Threat = readcolstringrow(S, R, Dbtype, Threat_position);
true ->
Threat = ""
end,

if
(Country_short == "-") or (Proxy_type == "-") ->
Is_proxy = 0;
Expand All @@ -323,6 +334,7 @@ readrecord(S, Dbtype, Rowoffset, Mode) ->
asn = Asn,
as = As,
last_seen = Last_seen,
threat = Threat,
is_proxy = Is_proxy
}
end.
Expand Down Expand Up @@ -376,6 +388,7 @@ searchtree(S, Ipnum, Dbtype, Low, High, BaseAddr, Colsize, Iptype, Mode) ->
asn = X,
as = X,
last_seen = X,
threat = X,
is_proxy = -1
}
end.
Expand Down Expand Up @@ -403,7 +416,7 @@ search6(S, Ipnum, Dbtype, Low, High, Baseaddr, Indexbaseaddr, Colsize, Mode) ->
end.

getall(Ip) ->
query(Ip, 4095).
query(Ip, 8191).

getcountryshort(Ip) ->
Result = query(Ip, 1),
Expand Down Expand Up @@ -449,6 +462,10 @@ getlastseen(Ip) ->
Result = query(Ip, 2048),
Result#ip2proxyrecord.last_seen.

getthreat(Ip) ->
Result = query(Ip, 4096),
Result#ip2proxyrecord.threat.

isproxy(Ip) ->
Result = query(Ip, 64),
Result#ip2proxyrecord.is_proxy.
Expand Down Expand Up @@ -519,6 +536,7 @@ query(Ip, Mode) ->
asn = X,
as = X,
last_seen = X,
threat = X,
is_proxy = -1
}
end,
Expand All @@ -537,6 +555,7 @@ query(Ip, Mode) ->
asn = Y,
as = Y,
last_seen = Y,
threat = Y,
is_proxy = -1
}
end
Expand Down
6 changes: 4 additions & 2 deletions test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ printme(V) ->
case V of
{} ->
io:format("No results.~n", []);
{ip2proxyrecord, Country_short, Country_long, Region, City, Isp, Proxy_type, Domain, Usage_type, Asn, As, Last_seen, Is_proxy} ->
{ip2proxyrecord, Country_short, Country_long, Region, City, Isp, Proxy_type, Domain, Usage_type, Asn, As, Last_seen, Threat, Is_proxy} ->
io:format("Country_short: ~p~n", [Country_short]),
io:format("Country_long: ~p~n", [Country_long]),
io:format("Region: ~p~n", [Region]),
Expand All @@ -17,13 +17,14 @@ printme(V) ->
io:format("Asn: ~p~n", [Asn]),
io:format("As: ~p~n", [As]),
io:format("Last_seen: ~p~n", [Last_seen]),
io:format("Threat: ~p~n", [Threat]),
io:format("Is_proxy: ~p~n", [Is_proxy])
end,
io:format("===================================================================~n", []).

testme() ->
X = "37.252.228.50",
case ip2proxy:open("./IP2Proxy/bin/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.BIN") of
case ip2proxy:open("./IP2Proxy/bin/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.BIN") of
0 ->
io:format("getpackageversion: ~p~n", [ip2proxy:getpackageversion()]),
io:format("getmoduleversion: ~p~n", [ip2proxy:getmoduleversion()]),
Expand All @@ -42,6 +43,7 @@ testme() ->
io:format("Asn: ~p~n", [ip2proxy:getasn(X)]),
io:format("AS: ~p~n", [ip2proxy:getas(X)]),
io:format("Last_seen: ~p~n", [ip2proxy:getlastseen(X)]),
io:format("Threat: ~p~n", [ip2proxy:getthreat(X)]),
io:format("Is_proxy: ~p~n", [ip2proxy:isproxy(X)]);
_ ->
io:format("Error reading BIN file~n", [])
Expand Down

0 comments on commit 416eb79

Please sign in to comment.