ip2proxy_ruby 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1429fb277332da40404fd0077859a04fcb89dbca1e02e41e7376366f7c58361
4
- data.tar.gz: 4a6bcfd25d6074e045c7af58744dd9ed5a208c080f464a90792fd3452adca7cc
3
+ metadata.gz: e85aa655931de9dec06fe78849d14e570b6410859cf0db44d14eaec4788b65ab
4
+ data.tar.gz: b11479694f3df231134bb86bcfbbb858d758b80bb73f59be2a5c45309372fbf9
5
5
  SHA512:
6
- metadata.gz: c615eaf3a642c9058e57b9b137297fea96aa08f3a3496caf23c98c4cd8444ccd5f1a5539053cf94cc7137d6064819d80231ad085a27d884460f27b834836d28b
7
- data.tar.gz: b203233664cfbed3e1ffdeb3f5fcee1618f03a2ddd2c7a2fad69fd27e07e2297aa387008b0120a4e244276c9e92c7010e491f28a23281f554c328a9af3fedf33
6
+ metadata.gz: 8a54a2ecc9cc1476f4695c3fac314f6df50c174b170e5b3a4a7c9a417cf46a5b1d8372378612eb40c1cd5b140baa22dd82f69fb71989d82a1c40421f9db35007
7
+ data.tar.gz: fd0d1ef56dcd0c47d8180047f89ebb301195e1a9cdc09f1908661dc4a383c056363b3e50c19e81f9fe36b2f78c66d648efbf0fe63445dadae088535d83b90f39
data/LICENSE.txt CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2019 IP2Location ( support@ip2location.com )
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2021 IP2Location ( support@ip2location.com )
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,131 +1,134 @@
1
- [![Latest Stable Version](https://img.shields.io/gem/v/ip2proxy_ruby.svg)](https://rubygems.org/gems/ip2proxy_ruby)
2
- [![Total Downloads](https://img.shields.io/gem/dt/ip2proxy_ruby.svg)](https://rubygems.org/gems/ip2proxy_ruby)
3
-
4
- # IP2Proxy Ruby Library
5
-
6
- This module allows user to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats and last seen date.
7
-
8
- It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at
9
-
10
- * Free IP2Proxy BIN Data: https://lite.ip2location.com
11
- * Commercial IP2Proxy BIN Data: https://www.ip2location.com/proxy-database
12
-
13
-
14
- For more details, please visit:
15
- [https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
16
-
17
- ## Methods
18
-
19
- Below are the methods supported in this module.
20
-
21
- |Method Name|Description|
22
- |---|---|
23
- |open|Open the IP2Proxy BIN data with **File I/O** mode for lookup.|
24
- |close|Close and clean up the file pointer.|
25
- |get_package_version|Get the package version (1 to 10 for PX1 to PX10 respectively).|
26
- |get_module_version|Get the module version.|
27
- |get_database_version|Get the database version.|
28
- |is_proxy|Check whether if an IP address was a proxy. Please see [Proxy Type](#proxy-type) for details. 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>|
29
- |get_all|Return the proxy information in array.|
30
- |get_proxytype|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|
31
- |get_country_short|Return the ISO3166-1 country code (2-digits) of the proxy.|
32
- |get_country_long|Return the ISO3166-1 country name of the proxy.|
33
- |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|
34
- |get_city|Return the city name of the proxy.|
35
- |get_isp|Return the ISP name of the proxy.|
36
- |get_domain|Return the domain name of proxy's IP address or domain name.|
37
- |get_usagetype|Return the ISP's usage type of proxy's IP address or domain name. Please see [Usage Type](#usage-type) for details.|
38
- |get_asn|Return the autonomous system number (ASN) of proxy's IP address or domain name.|
39
- |get_as|Return the autonomous system (AS) name of proxy's IP address or domain name.|
40
- |get_last_seen|Return the last seen days ago value of proxy's IP address or domain name.|
41
- |get_threat|Return the threat types reported to proxy's IP address or domain name. Please see [Threat Type](#threat-type) for details.|
42
-
43
- ## Usage
44
-
45
- ```
46
- require 'ip2proxy_ruby'
47
-
48
- # open IP2Proxy BIN database for proxy lookup
49
- i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.BIN")
50
-
51
- # get versioning information
52
- print 'Module Version: ' + i2p.get_module_version + "\n"
53
- print 'Package Version: ' + i2p.get_package_version + "\n"
54
- print 'Database Version: ' + i2p.get_database_version + "\n"
55
-
56
- # individual proxy data check
57
- print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
58
- print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
59
- print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
60
- print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
61
- print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
62
- print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
63
- print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
64
- print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
65
- print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
66
- print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
67
- print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
68
- print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
69
- print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
70
-
71
- # single function to get all proxy data returned in array
72
- record = i2p.get_all('1.2.3.4')
73
- print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
74
- print 'Proxy Type: ' + record['proxy_type'] + "\n"
75
- print 'Country Code: ' + record['country_short'] + "\n"
76
- print 'Country Name: ' + record['country_long'] + "\n"
77
- print 'Region Name: ' + record['region'] + "\n"
78
- print 'City Name: ' + record['city'] + "\n"
79
- print 'ISP: ' + record['isp'] + "\n"
80
- print 'Domain: ' + record['domain'] + "\n"
81
- print 'Usage Type: ' + record['usagetype'] + "\n"
82
- print 'ASN: ' + record['asn'] + "\n"
83
- print 'AS: ' + record['as'] + "\n"
84
- print 'Last Seen: ' + record['last_seen'] + "\n"
85
- print 'Threat: ' + record['threat'] + "\n"
86
-
87
- # close IP2Proxy BIN database
88
- i2p.close()
89
- ```
90
-
91
- ### Proxy Type
92
-
93
- |Proxy Type|Description|
94
- |---|---|
95
- |VPN|Anonymizing VPN services|
96
- |TOR|Tor Exit Nodes|
97
- |PUB|Public Proxies|
98
- |WEB|Web Proxies|
99
- |DCH|Hosting Providers/Data Center|
100
- |SES|Search Engine Robots|
101
- |RES|Residential Proxies [PX10+]|
102
-
103
- ### Usage Type
104
-
105
- |Usage Type|Description|
106
- |---|---|
107
- |COM|Commercial|
108
- |ORG|Organization|
109
- |GOV|Government|
110
- |MIL|Military|
111
- |EDU|University/College/School|
112
- |LIB|Library|
113
- |CDN|Content Delivery Network|
114
- |ISP|Fixed Line ISP|
115
- |MOB|Mobile ISP|
116
- |DCH|Data Center/Web Hosting/Transit|
117
- |SES|Search Engine Spider|
118
- |RSV|Reserved|
119
-
120
- ### Threat Type
121
-
122
- |Threat Type|Description|
123
- |---|---|
124
- |SPAM|Spammer|
125
- |SCANNER|Security Scanner or Attack|
126
- |BOTNET|Spyware or Malware|
127
-
128
- ## Support
129
-
130
- Email: support@ip2location.com
131
- URL: [https://www.ip2location.com](https://www.ip2location.com)
1
+ [![Latest Stable Version](https://img.shields.io/gem/v/ip2proxy_ruby.svg)](https://rubygems.org/gems/ip2proxy_ruby)
2
+ [![Total Downloads](https://img.shields.io/gem/dt/ip2proxy_ruby.svg)](https://rubygems.org/gems/ip2proxy_ruby)
3
+
4
+ # IP2Proxy Ruby Library
5
+
6
+ This module allows user to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names.
7
+
8
+ It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at
9
+
10
+ * Free IP2Proxy BIN Data: https://lite.ip2location.com
11
+ * Commercial IP2Proxy BIN Data: https://www.ip2location.com/proxy-database
12
+
13
+
14
+ For more details, please visit:
15
+ [https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
16
+
17
+ ## Methods
18
+
19
+ Below are the methods supported in this module.
20
+
21
+ |Method Name|Description|
22
+ |---|---|
23
+ |open|Open the IP2Proxy BIN data with **File I/O** mode for lookup.|
24
+ |close|Close and clean up the file pointer.|
25
+ |get_package_version|Get the package version (1 to 10 for PX1 to PX11 respectively).|
26
+ |get_module_version|Get the module version.|
27
+ |get_database_version|Get the database version.|
28
+ |is_proxy|Check whether if an IP address was a proxy. Please see [Proxy Type](#proxy-type) for details. 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>|
29
+ |get_all|Return the proxy information in array.|
30
+ |get_proxytype|Return the proxy type. Please visit <a href="https://www.ip2location.com/database/px11-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential-provider" target="_blank">IP2Location</a> for the list of proxy types supported|
31
+ |get_country_short|Return the ISO3166-1 country code (2-digits) of the proxy.|
32
+ |get_country_long|Return the ISO3166-1 country name of the proxy.|
33
+ |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|
34
+ |get_city|Return the city name of the proxy.|
35
+ |get_isp|Return the ISP name of the proxy.|
36
+ |get_domain|Return the domain name of proxy's IP address or domain name.|
37
+ |get_usagetype|Return the ISP's usage type of proxy's IP address or domain name. Please see [Usage Type](#usage-type) for details.|
38
+ |get_asn|Return the autonomous system number (ASN) of proxy's IP address or domain name.|
39
+ |get_as|Return the autonomous system (AS) name of proxy's IP address or domain name.|
40
+ |get_last_seen|Return the last seen days ago value of proxy's IP address or domain name.|
41
+ |get_threat|Return the threat types reported to proxy's IP address or domain name. Please see [Threat Type](#threat-type) for details.|
42
+ |get_provider|Returns the VPN service provider name if available.|
43
+
44
+ ## Usage
45
+
46
+ ```
47
+ require 'ip2proxy_ruby'
48
+
49
+ # open IP2Proxy BIN database for proxy lookup
50
+ i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN")
51
+
52
+ # get versioning information
53
+ print 'Module Version: ' + i2p.get_module_version + "\n"
54
+ print 'Package Version: ' + i2p.get_package_version + "\n"
55
+ print 'Database Version: ' + i2p.get_database_version + "\n"
56
+
57
+ # individual proxy data check
58
+ print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
59
+ print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
60
+ print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
61
+ print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
62
+ print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
63
+ print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
64
+ print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
65
+ print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
66
+ print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
67
+ print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
68
+ print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
69
+ print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
70
+ print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
71
+ print 'Provider: ' + i2p.get_provider('1.2.3.4') + "\n"
72
+
73
+ # single function to get all proxy data returned in array
74
+ record = i2p.get_all('1.2.3.4')
75
+ print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
76
+ print 'Proxy Type: ' + record['proxy_type'] + "\n"
77
+ print 'Country Code: ' + record['country_short'] + "\n"
78
+ print 'Country Name: ' + record['country_long'] + "\n"
79
+ print 'Region Name: ' + record['region'] + "\n"
80
+ print 'City Name: ' + record['city'] + "\n"
81
+ print 'ISP: ' + record['isp'] + "\n"
82
+ print 'Domain: ' + record['domain'] + "\n"
83
+ print 'Usage Type: ' + record['usagetype'] + "\n"
84
+ print 'ASN: ' + record['asn'] + "\n"
85
+ print 'AS: ' + record['as'] + "\n"
86
+ print 'Last Seen: ' + record['last_seen'] + "\n"
87
+ print 'Threat: ' + record['threat'] + "\n"
88
+ print 'Provider: ' + record['provider'] + "\n"
89
+
90
+ # close IP2Proxy BIN database
91
+ i2p.close()
92
+ ```
93
+
94
+ ### Proxy Type
95
+
96
+ |Proxy Type|Description|
97
+ |---|---|
98
+ |VPN|Anonymizing VPN services|
99
+ |TOR|Tor Exit Nodes|
100
+ |PUB|Public Proxies|
101
+ |WEB|Web Proxies|
102
+ |DCH|Hosting Providers/Data Center|
103
+ |SES|Search Engine Robots|
104
+ |RES|Residential Proxies [PX10+]|
105
+
106
+ ### Usage Type
107
+
108
+ |Usage Type|Description|
109
+ |---|---|
110
+ |COM|Commercial|
111
+ |ORG|Organization|
112
+ |GOV|Government|
113
+ |MIL|Military|
114
+ |EDU|University/College/School|
115
+ |LIB|Library|
116
+ |CDN|Content Delivery Network|
117
+ |ISP|Fixed Line ISP|
118
+ |MOB|Mobile ISP|
119
+ |DCH|Data Center/Web Hosting/Transit|
120
+ |SES|Search Engine Spider|
121
+ |RSV|Reserved|
122
+
123
+ ### Threat Type
124
+
125
+ |Threat Type|Description|
126
+ |---|---|
127
+ |SPAM|Spammer|
128
+ |SCANNER|Security Scanner or Attack|
129
+ |BOTNET|Spyware or Malware|
130
+
131
+ ## Support
132
+
133
+ Email: support@ip2location.com
134
+ URL: [https://www.ip2location.com](https://www.ip2location.com)
data/example.rb CHANGED
@@ -1,43 +1,45 @@
1
- require 'ip2proxy_ruby'
2
-
3
- # open IP2Proxy BIN database for proxy lookup
4
- i2p = Ip2proxy.new.open("./data/IP2PROXY-LITE-PX1.BIN")
5
-
6
- # get versioning information
7
- print 'Module Version: ' + i2p.get_module_version + "\n"
8
- print 'Package Version: ' + i2p.get_package_version + "\n"
9
- print 'Database Version: ' + i2p.get_database_version + "\n"
10
-
11
- # individual proxy data check
12
- print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
13
- print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
14
- print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
15
- print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
16
- print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
17
- print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
18
- print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
19
- print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
20
- print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
21
- print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
22
- print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
23
- print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
24
- print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
25
-
26
- # single function to get all proxy data returned in array
27
- record = i2p.get_all('1.2.3.4')
28
- print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
29
- print 'Proxy Type: ' + record['proxy_type'] + "\n"
30
- print 'Country Code: ' + record['country_short'] + "\n"
31
- print 'Country Name: ' + record['country_long'] + "\n"
32
- print 'Region Name: ' + record['region'] + "\n"
33
- print 'City Name: ' + record['city'] + "\n"
34
- print 'ISP: ' + record['isp'] + "\n"
35
- print 'Domain: ' + record['domain'] + "\n"
36
- print 'Usage Type: ' + record['usagetype'] + "\n"
37
- print 'ASN: ' + record['asn'] + "\n"
38
- print 'AS: ' + record['as'] + "\n"
39
- print 'Last Seen: ' + record['last_seen'] + "\n"
40
- print 'Threat: ' + record['threat'] + "\n"
41
-
42
- # close IP2Proxy BIN database
1
+ require 'ip2proxy_ruby'
2
+
3
+ # open IP2Proxy BIN database for proxy lookup
4
+ i2p = Ip2proxy.new.open("./data/PX11.SAMPLE.BIN")
5
+
6
+ # get versioning information
7
+ print 'Module Version: ' + i2p.get_module_version + "\n"
8
+ print 'Package Version: ' + i2p.get_package_version + "\n"
9
+ print 'Database Version: ' + i2p.get_database_version + "\n"
10
+
11
+ # individual proxy data check
12
+ print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
13
+ print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
14
+ print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
15
+ print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
16
+ print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
17
+ print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
18
+ print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
19
+ print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
20
+ print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
21
+ print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
22
+ print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
23
+ print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
24
+ print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
25
+ print 'Provider: ' + i2p.get_provider('1.2.3.4') + "\n"
26
+
27
+ # single function to get all proxy data returned in array
28
+ record = i2p.get_all('1.2.3.4')
29
+ print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
30
+ print 'Proxy Type: ' + record['proxy_type'] + "\n"
31
+ print 'Country Code: ' + record['country_short'] + "\n"
32
+ print 'Country Name: ' + record['country_long'] + "\n"
33
+ print 'Region Name: ' + record['region'] + "\n"
34
+ print 'City Name: ' + record['city'] + "\n"
35
+ print 'ISP: ' + record['isp'] + "\n"
36
+ print 'Domain: ' + record['domain'] + "\n"
37
+ print 'Usage Type: ' + record['usagetype'] + "\n"
38
+ print 'ASN: ' + record['asn'] + "\n"
39
+ print 'AS: ' + record['as'] + "\n"
40
+ print 'Last Seen: ' + record['last_seen'] + "\n"
41
+ print 'Threat: ' + record['threat'] + "\n"
42
+ print 'Provider: ' + record['provider'] + "\n"
43
+
44
+ # close IP2Proxy BIN database
43
45
  i2p.close()
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "ip2proxy_ruby"
5
- s.version = "3.0.1"
5
+ s.version = "3.1.0"
6
6
  s.authors = ["ip2location"]
7
7
  s.email = ["support@ip2location.com"]
8
8
 
9
9
  s.summary = "IP2Proxy Ruby library"
10
- s.description = "The official IP2Proxy Ruby library to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats and last seen date."
10
+ s.description = "The official IP2Proxy Ruby library to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names."
11
11
  s.homepage = "https://github.com/ip2location/ip2proxy-ruby"
12
12
  s.licenses = ["MIT"]
13
13
  s.require_paths = ["lib"]
@@ -31,10 +31,10 @@ Gem::Specification.new do |s|
31
31
  "lib/ip2proxy_ruby/i2p_string_data.rb",
32
32
  "lib/ip2proxy_ruby/ip2proxy_config.rb",
33
33
  "lib/ip2proxy_ruby/ip2proxy_record.rb",
34
- "spec/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN",
35
- "spec/ip2proxy_ruby_spec.rb",
34
+ "spec/assets/PX11.SAMPLE.BIN",
35
+ "spec/ip2proxy_ruby_database_spec.rb",
36
36
  "spec/spec_helper.rb",
37
- "rb/data/IP2PROXY-LITE-PX1.BIN"
37
+ "rb/data/PX11.SAMPLE.BIN"
38
38
  ]
39
39
 
40
40
  if s.respond_to?(:metadata=)
data/lib/ip2proxy_ruby.rb CHANGED
@@ -1,428 +1,472 @@
1
- require 'bindata'
2
- require 'ipaddr'
3
- require_relative 'ip2proxy_ruby/ip2proxy_config'
4
- require_relative 'ip2proxy_ruby/i2p_database_config'
5
- require_relative 'ip2proxy_ruby/i2p_string_data'
6
- require_relative 'ip2proxy_ruby/i2p_ip_data'
7
- require_relative 'ip2proxy_ruby/ip2proxy_record'
8
-
9
- class Ip2proxy
10
- attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr, :databaseyear, :databasemonth, :databaseday
11
-
12
- VERSION = '3.0.1'
13
- FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
14
- INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
15
-
16
- def open(url)
17
- self.file = File.open(File.expand_path url, 'rb')
18
- i2p = Ip2proxyConfig.read(file)
19
- self.db_index = i2p.databasetype
20
- self.columns = i2p.databasecolumn + 0
21
- self.databaseyear = 2000 + i2p.databaseyear
22
- self.databasemonth = i2p.databasemonth
23
- self.databaseday = i2p.databaseday
24
- self.database = I2pDbConfig.setup_database(self.db_index)
25
- self.ipv4databasecount = i2p.ipv4databasecount
26
- self.ipv4databaseaddr = i2p.ipv4databaseaddr
27
- self.ipv6databasecount = i2p.ipv6databasecount
28
- self.ipv6databaseaddr = i2p.ipv6databaseaddr
29
- self.ipv4indexbaseaddr = i2p.ipv4indexbaseaddr
30
- self.ipv6indexbaseaddr = i2p.ipv6indexbaseaddr
31
- self.record_class4 = (Ip2ProxyRecord.init database, 4)
32
- self.record_class6 = (Ip2ProxyRecord.init database, 6)
33
- self
34
- end
35
-
36
- def close()
37
- self.file.close
38
- end
39
-
40
- def get_module_version()
41
- return VERSION
42
- end
43
-
44
- def get_package_version()
45
- return (self.db_index).to_s
46
- end
47
-
48
- def get_database_version()
49
- return (self.databaseyear).to_s + "." + (self.databasemonth).to_s + "." + (self.databaseday).to_s
50
- end
51
-
52
- def get_record(ip)
53
- ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
54
- self.ip_version, ipnum = validateip(ipno)
55
- self.v4 = ip_version == 4 ? true : false
56
- self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
57
- self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
58
- col_length = columns * 4
59
- if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
60
- indexpos = 0
61
- case ip_version
62
- when 4
63
- indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
64
- realipno = ipnum
65
- # if ipnum reach MAX_IPV4_RANGE
66
- if realipno == 4294967295
67
- ipnum = realipno - 1
68
- end
69
- when 6
70
- indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
71
- realipno = ipnum
72
- # if ipnum reach MAX_IPV6_RANGE
73
- if realipno == 340282366920938463463374607431768211455
74
- ipnum = realipno - 1
75
- end
76
- end
77
- low = read32(indexpos)
78
- high = read32(indexpos + 4)
79
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
80
- else
81
- return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
82
- end
83
- end
84
-
85
- def get_country_short(ip)
86
- valid = !(IPAddr.new(ip) rescue nil).nil?
87
- if valid
88
- rec = get_record(ip)
89
- if !(rec.nil?)
90
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
91
- else
92
- country_short = INVALID_IP_ADDRESS
93
- end
94
- else
95
- country_short = INVALID_IP_ADDRESS
96
- end
97
- return country_short
98
- end
99
-
100
- def get_country_long(ip)
101
- valid = !(IPAddr.new(ip) rescue nil).nil?
102
- if valid
103
- rec = get_record(ip)
104
- if !(rec.nil?)
105
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
106
- else
107
- country_long = INVALID_IP_ADDRESS
108
- end
109
- else
110
- country_long = INVALID_IP_ADDRESS
111
- end
112
- return country_long
113
- end
114
-
115
- def get_region(ip)
116
- valid = !(IPAddr.new(ip) rescue nil).nil?
117
- if valid
118
- rec = get_record(ip)
119
- if !(rec.nil?)
120
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
121
- else
122
- region = INVALID_IP_ADDRESS
123
- end
124
- else
125
- region = INVALID_IP_ADDRESS
126
- end
127
- return region
128
- end
129
-
130
- def get_city(ip)
131
- valid = !(IPAddr.new(ip) rescue nil).nil?
132
- if valid
133
- rec = get_record(ip)
134
- if !(rec.nil?)
135
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
136
- else
137
- city = INVALID_IP_ADDRESS
138
- end
139
- else
140
- city = INVALID_IP_ADDRESS
141
- end
142
- return city
143
- end
144
-
145
- def get_isp(ip)
146
- valid = !(IPAddr.new(ip) rescue nil).nil?
147
- if valid
148
- rec = get_record(ip)
149
- if !(rec.nil?)
150
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
151
- else
152
- isp = INVALID_IP_ADDRESS
153
- end
154
- else
155
- isp = INVALID_IP_ADDRESS
156
- end
157
- return isp
158
- end
159
-
160
- def get_proxytype(ip)
161
- valid = !(IPAddr.new(ip) rescue nil).nil?
162
- if valid
163
- rec = get_record(ip)
164
- if !(rec.nil?)
165
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
166
- else
167
- proxytype = INVALID_IP_ADDRESS
168
- end
169
- else
170
- proxytype = INVALID_IP_ADDRESS
171
- end
172
- return proxytype
173
- end
174
-
175
- def get_domain(ip)
176
- valid = !(IPAddr.new(ip) rescue nil).nil?
177
- if valid
178
- rec = get_record(ip)
179
- if !(rec.nil?)
180
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
181
- else
182
- domain = INVALID_IP_ADDRESS
183
- end
184
- else
185
- domain = INVALID_IP_ADDRESS
186
- end
187
- return domain
188
- end
189
-
190
- def get_usagetype(ip)
191
- valid = !(IPAddr.new(ip) rescue nil).nil?
192
- if valid
193
- rec = get_record(ip)
194
- if !(rec.nil?)
195
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
196
- else
197
- usagetype = INVALID_IP_ADDRESS
198
- end
199
- else
200
- usagetype = INVALID_IP_ADDRESS
201
- end
202
- return usagetype
203
- end
204
-
205
- def get_asn(ip)
206
- valid = !(IPAddr.new(ip) rescue nil).nil?
207
- if valid
208
- rec = get_record(ip)
209
- if !(rec.nil?)
210
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
211
- else
212
- asn = INVALID_IP_ADDRESS
213
- end
214
- else
215
- asn = INVALID_IP_ADDRESS
216
- end
217
- return asn
218
- end
219
-
220
- def get_as(ip)
221
- valid = !(IPAddr.new(ip) rescue nil).nil?
222
- if valid
223
- rec = get_record(ip)
224
- if !(rec.nil?)
225
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
226
- else
227
- as = INVALID_IP_ADDRESS
228
- end
229
- else
230
- as = INVALID_IP_ADDRESS
231
- end
232
- return as
233
- end
234
-
235
- def get_last_seen(ip)
236
- valid = !(IPAddr.new(ip) rescue nil).nil?
237
- if valid
238
- rec = get_record(ip)
239
- if !(rec.nil?)
240
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
241
- else
242
- last_seen = INVALID_IP_ADDRESS
243
- end
244
- else
245
- last_seen = INVALID_IP_ADDRESS
246
- end
247
- return last_seen
248
- end
249
-
250
- def get_threat(ip)
251
- valid = !(IPAddr.new(ip) rescue nil).nil?
252
- if valid
253
- rec = get_record(ip)
254
- if !(rec.nil?)
255
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
256
- else
257
- threat = INVALID_IP_ADDRESS
258
- end
259
- else
260
- threat = INVALID_IP_ADDRESS
261
- end
262
- return threat
263
- end
264
-
265
- def is_proxy(ip)
266
- valid = !(IPAddr.new(ip) rescue nil).nil?
267
- if valid
268
- rec = get_record(ip)
269
- if !(rec.nil?)
270
- if self.db_index == 1
271
- isproxy = (rec.country_short == '-') ? 0 : 1
272
- else
273
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
274
- end
275
- else
276
- isproxy = -1
277
- end
278
- else
279
- isproxy = -1
280
- end
281
- return isproxy
282
- end
283
-
284
- def get_all(ip)
285
- valid = !(IPAddr.new(ip) rescue nil).nil?
286
- if valid
287
- rec = get_record(ip)
288
- if !(rec.nil?)
289
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
290
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
291
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
292
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
293
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
294
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
295
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
296
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
297
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
298
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
299
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
300
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
301
- if self.db_index == 1
302
- isproxy = (rec.country_short == '-') ? 0 : 1
303
- else
304
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
305
- end
306
- else
307
- country_short = INVALID_IP_ADDRESS
308
- country_long = INVALID_IP_ADDRESS
309
- region = INVALID_IP_ADDRESS
310
- city = INVALID_IP_ADDRESS
311
- isp = INVALID_IP_ADDRESS
312
- proxytype = INVALID_IP_ADDRESS
313
- domain = INVALID_IP_ADDRESS
314
- usagetype = INVALID_IP_ADDRESS
315
- asn = INVALID_IP_ADDRESS
316
- as = INVALID_IP_ADDRESS
317
- last_seen = INVALID_IP_ADDRESS
318
- threat = INVALID_IP_ADDRESS
319
- isproxy = -1
320
- end
321
- else
322
- country_short = INVALID_IP_ADDRESS
323
- country_long = INVALID_IP_ADDRESS
324
- region = INVALID_IP_ADDRESS
325
- city = INVALID_IP_ADDRESS
326
- isp = INVALID_IP_ADDRESS
327
- proxytype = INVALID_IP_ADDRESS
328
- domain = INVALID_IP_ADDRESS
329
- usagetype = INVALID_IP_ADDRESS
330
- asn = INVALID_IP_ADDRESS
331
- as = INVALID_IP_ADDRESS
332
- last_seen = INVALID_IP_ADDRESS
333
- threat = INVALID_IP_ADDRESS
334
- isproxy = -1
335
- end
336
- results = {}
337
- results['is_proxy'] = isproxy
338
- results['proxy_type'] = proxytype
339
- results['country_short'] = country_short
340
- results['country_long'] = country_long
341
- results['region'] = region
342
- results['city'] = city
343
- results['isp'] = isp
344
- results['domain'] = domain
345
- results['usagetype'] = usagetype
346
- results['asn'] = asn
347
- results['as'] = as
348
- results['last_seen'] = last_seen
349
- results['threat'] = threat
350
- return results
351
- end
352
-
353
- def bsearch(low, high, ipnum, base_addr, col_length)
354
- while low <= high do
355
- mid = (low + high) >> 1
356
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
357
- if ipnum >= ip_from && ipnum < ip_to
358
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
359
- file.seek(from_base)
360
- if v4
361
- return self.record_class4.read(file)
362
- else
363
- return self.record_class6.read(file)
364
- end
365
- else
366
- if ipnum < ip_from
367
- high = mid - 1
368
- else
369
- low = mid + 1
370
- end
371
- end
372
- end
373
- end
374
-
375
- def get_from_to(mid, base_addr, col_length)
376
- from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
377
- file.seek(from_base)
378
- ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
379
- file.seek(from_base + col_length + (v4 ? 0 : 12))
380
- ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
381
- [ip_from, ip_to]
382
- end
383
-
384
- def validateip(ip)
385
- if ip.ipv4?
386
- ipv = 4
387
- ipnum = ip.to_i + 0
388
- else
389
- ipv = 6
390
- ipnum = ip.to_i + 0
391
- #reformat ipv4 address in ipv6
392
- if ipnum >= 281470681743360 && ipnum <= 281474976710655
393
- ipv = 4
394
- ipnum = ipnum - 281470681743360
395
- end
396
- #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
397
- if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
398
- ipv = 4
399
- #bitshift right 80 bits
400
- ipnum = ipnum >> 80
401
- #bitwise modulus to get the last 32 bit
402
- ipnum = ipnum % 4294967296
403
- end
404
- #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
405
- if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
406
- ipv = 4
407
- #bitwise not to invert binary
408
- ipnum = ~ipnum
409
- #bitwise modulus to get the last 32 bit
410
- ipnum = ipnum % 4294967296
411
- end
412
- end
413
- [ipv, ipnum]
414
- end
415
-
416
- def read32(indexp)
417
- file.seek(indexp - 1)
418
- return file.read(4).unpack('V').first
419
- end
420
-
421
- def readipv6(filer)
422
- parts = filer.read(16).unpack('V*')
423
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
424
- end
425
-
426
- private :get_record, :bsearch, :get_from_to, :read32, :readipv6
427
-
1
+ require 'bindata'
2
+ require 'ipaddr'
3
+ require_relative 'ip2proxy_ruby/ip2proxy_config'
4
+ require_relative 'ip2proxy_ruby/i2p_database_config'
5
+ require_relative 'ip2proxy_ruby/i2p_string_data'
6
+ require_relative 'ip2proxy_ruby/i2p_ip_data'
7
+ require_relative 'ip2proxy_ruby/ip2proxy_record'
8
+
9
+ class Ip2proxy
10
+ attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr, :databaseyear, :databasemonth, :databaseday, :last_err_msg
11
+
12
+ VERSION = '3.1.0'
13
+ FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
14
+ INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
15
+ INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.'
16
+
17
+ def open(url)
18
+ if url == ''
19
+ self.last_err_msg = 'Ip2proxy.new.open() requires a database path name.'
20
+ abort('Ip2proxy.new.open() requires a database path name.')
21
+ end
22
+
23
+ begin
24
+ self.file = File.open(File.expand_path url, 'rb')
25
+ rescue
26
+ self.last_err_msg = 'Ip2proxy.new.open() error in opening ' + url +'.'
27
+ abort('Ip2proxy.new.open() error in opening ' + url +'.')
28
+ else
29
+ end
30
+ i2p = Ip2proxyConfig.read(file)
31
+ if i2p.productcode == 2
32
+ else
33
+ if i2p.databaseyear <= 20 && i2p.productcode == 0
34
+ else
35
+ self.file.close
36
+ self.last_err_msg = INVALID_BIN_DATABASE
37
+ abort(INVALID_BIN_DATABASE)
38
+ end
39
+ end
40
+ self.db_index = i2p.databasetype
41
+ self.columns = i2p.databasecolumn + 0
42
+ self.databaseyear = 2000 + i2p.databaseyear
43
+ self.databasemonth = i2p.databasemonth
44
+ self.databaseday = i2p.databaseday
45
+ self.database = I2pDbConfig.setup_database(self.db_index)
46
+ self.ipv4databasecount = i2p.ipv4databasecount
47
+ self.ipv4databaseaddr = i2p.ipv4databaseaddr
48
+ self.ipv6databasecount = i2p.ipv6databasecount
49
+ self.ipv6databaseaddr = i2p.ipv6databaseaddr
50
+ self.ipv4indexbaseaddr = i2p.ipv4indexbaseaddr
51
+ self.ipv6indexbaseaddr = i2p.ipv6indexbaseaddr
52
+ self.record_class4 = (Ip2ProxyRecord.init database, 4)
53
+ self.record_class6 = (Ip2ProxyRecord.init database, 6)
54
+ self
55
+ end
56
+
57
+ def close()
58
+ self.file.close
59
+ end
60
+
61
+ def get_last_error_message()
62
+ return self.last_err_msg
63
+ end
64
+
65
+ def get_module_version()
66
+ return VERSION
67
+ end
68
+
69
+ def get_package_version()
70
+ return (self.db_index).to_s
71
+ end
72
+
73
+ def get_database_version()
74
+ return (self.databaseyear).to_s + "." + (self.databasemonth).to_s + "." + (self.databaseday).to_s
75
+ end
76
+
77
+ def get_record(ip)
78
+ ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
79
+ self.ip_version, ipnum = validateip(ipno)
80
+ self.v4 = ip_version == 4 ? true : false
81
+ self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
82
+ self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
83
+ col_length = columns * 4
84
+ if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
85
+ indexpos = 0
86
+ case ip_version
87
+ when 4
88
+ indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
89
+ realipno = ipnum
90
+ # if ipnum reach MAX_IPV4_RANGE
91
+ if realipno == 4294967295
92
+ ipnum = realipno - 1
93
+ end
94
+ when 6
95
+ indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
96
+ realipno = ipnum
97
+ # if ipnum reach MAX_IPV6_RANGE
98
+ if realipno == 340282366920938463463374607431768211455
99
+ ipnum = realipno - 1
100
+ end
101
+ end
102
+ low = read32(indexpos)
103
+ high = read32(indexpos + 4)
104
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
105
+ else
106
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
107
+ end
108
+ end
109
+
110
+ def get_country_short(ip)
111
+ valid = !(IPAddr.new(ip) rescue nil).nil?
112
+ if valid
113
+ rec = get_record(ip)
114
+ if !(rec.nil?)
115
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
116
+ else
117
+ country_short = INVALID_IP_ADDRESS
118
+ end
119
+ else
120
+ country_short = INVALID_IP_ADDRESS
121
+ end
122
+ return country_short
123
+ end
124
+
125
+ def get_country_long(ip)
126
+ valid = !(IPAddr.new(ip) rescue nil).nil?
127
+ if valid
128
+ rec = get_record(ip)
129
+ if !(rec.nil?)
130
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
131
+ else
132
+ country_long = INVALID_IP_ADDRESS
133
+ end
134
+ else
135
+ country_long = INVALID_IP_ADDRESS
136
+ end
137
+ return country_long
138
+ end
139
+
140
+ def get_region(ip)
141
+ valid = !(IPAddr.new(ip) rescue nil).nil?
142
+ if valid
143
+ rec = get_record(ip)
144
+ if !(rec.nil?)
145
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
146
+ else
147
+ region = INVALID_IP_ADDRESS
148
+ end
149
+ else
150
+ region = INVALID_IP_ADDRESS
151
+ end
152
+ return region
153
+ end
154
+
155
+ def get_city(ip)
156
+ valid = !(IPAddr.new(ip) rescue nil).nil?
157
+ if valid
158
+ rec = get_record(ip)
159
+ if !(rec.nil?)
160
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
161
+ else
162
+ city = INVALID_IP_ADDRESS
163
+ end
164
+ else
165
+ city = INVALID_IP_ADDRESS
166
+ end
167
+ return city
168
+ end
169
+
170
+ def get_isp(ip)
171
+ valid = !(IPAddr.new(ip) rescue nil).nil?
172
+ if valid
173
+ rec = get_record(ip)
174
+ if !(rec.nil?)
175
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
176
+ else
177
+ isp = INVALID_IP_ADDRESS
178
+ end
179
+ else
180
+ isp = INVALID_IP_ADDRESS
181
+ end
182
+ return isp
183
+ end
184
+
185
+ def get_proxytype(ip)
186
+ valid = !(IPAddr.new(ip) rescue nil).nil?
187
+ if valid
188
+ rec = get_record(ip)
189
+ if !(rec.nil?)
190
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
191
+ else
192
+ proxytype = INVALID_IP_ADDRESS
193
+ end
194
+ else
195
+ proxytype = INVALID_IP_ADDRESS
196
+ end
197
+ return proxytype
198
+ end
199
+
200
+ def get_domain(ip)
201
+ valid = !(IPAddr.new(ip) rescue nil).nil?
202
+ if valid
203
+ rec = get_record(ip)
204
+ if !(rec.nil?)
205
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
206
+ else
207
+ domain = INVALID_IP_ADDRESS
208
+ end
209
+ else
210
+ domain = INVALID_IP_ADDRESS
211
+ end
212
+ return domain
213
+ end
214
+
215
+ def get_usagetype(ip)
216
+ valid = !(IPAddr.new(ip) rescue nil).nil?
217
+ if valid
218
+ rec = get_record(ip)
219
+ if !(rec.nil?)
220
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
221
+ else
222
+ usagetype = INVALID_IP_ADDRESS
223
+ end
224
+ else
225
+ usagetype = INVALID_IP_ADDRESS
226
+ end
227
+ return usagetype
228
+ end
229
+
230
+ def get_asn(ip)
231
+ valid = !(IPAddr.new(ip) rescue nil).nil?
232
+ if valid
233
+ rec = get_record(ip)
234
+ if !(rec.nil?)
235
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
236
+ else
237
+ asn = INVALID_IP_ADDRESS
238
+ end
239
+ else
240
+ asn = INVALID_IP_ADDRESS
241
+ end
242
+ return asn
243
+ end
244
+
245
+ def get_as(ip)
246
+ valid = !(IPAddr.new(ip) rescue nil).nil?
247
+ if valid
248
+ rec = get_record(ip)
249
+ if !(rec.nil?)
250
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
251
+ else
252
+ as = INVALID_IP_ADDRESS
253
+ end
254
+ else
255
+ as = INVALID_IP_ADDRESS
256
+ end
257
+ return as
258
+ end
259
+
260
+ def get_last_seen(ip)
261
+ valid = !(IPAddr.new(ip) rescue nil).nil?
262
+ if valid
263
+ rec = get_record(ip)
264
+ if !(rec.nil?)
265
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
266
+ else
267
+ last_seen = INVALID_IP_ADDRESS
268
+ end
269
+ else
270
+ last_seen = INVALID_IP_ADDRESS
271
+ end
272
+ return last_seen
273
+ end
274
+
275
+ def get_threat(ip)
276
+ valid = !(IPAddr.new(ip) rescue nil).nil?
277
+ if valid
278
+ rec = get_record(ip)
279
+ if !(rec.nil?)
280
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
281
+ else
282
+ threat = INVALID_IP_ADDRESS
283
+ end
284
+ else
285
+ threat = INVALID_IP_ADDRESS
286
+ end
287
+ return threat
288
+ end
289
+
290
+ def is_proxy(ip)
291
+ valid = !(IPAddr.new(ip) rescue nil).nil?
292
+ if valid
293
+ rec = get_record(ip)
294
+ if !(rec.nil?)
295
+ if self.db_index == 1
296
+ isproxy = (rec.country_short == '-') ? 0 : 1
297
+ else
298
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
299
+ end
300
+ else
301
+ isproxy = -1
302
+ end
303
+ else
304
+ isproxy = -1
305
+ end
306
+ return isproxy
307
+ end
308
+
309
+ def get_provider(ip)
310
+ valid = !(IPAddr.new(ip) rescue nil).nil?
311
+ if valid
312
+ rec = get_record(ip)
313
+ if !(rec.nil?)
314
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
315
+ else
316
+ provider = INVALID_IP_ADDRESS
317
+ end
318
+ else
319
+ provider = INVALID_IP_ADDRESS
320
+ end
321
+ return provider
322
+ end
323
+
324
+ def get_all(ip)
325
+ valid = !(IPAddr.new(ip) rescue nil).nil?
326
+ if valid
327
+ rec = get_record(ip)
328
+ if !(rec.nil?)
329
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
330
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
331
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
332
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
333
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
334
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
335
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
336
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
337
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
338
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
339
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
340
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
341
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
342
+ if self.db_index == 1
343
+ isproxy = (rec.country_short == '-') ? 0 : 1
344
+ else
345
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
346
+ end
347
+ else
348
+ country_short = INVALID_IP_ADDRESS
349
+ country_long = INVALID_IP_ADDRESS
350
+ region = INVALID_IP_ADDRESS
351
+ city = INVALID_IP_ADDRESS
352
+ isp = INVALID_IP_ADDRESS
353
+ proxytype = INVALID_IP_ADDRESS
354
+ domain = INVALID_IP_ADDRESS
355
+ usagetype = INVALID_IP_ADDRESS
356
+ asn = INVALID_IP_ADDRESS
357
+ as = INVALID_IP_ADDRESS
358
+ last_seen = INVALID_IP_ADDRESS
359
+ threat = INVALID_IP_ADDRESS
360
+ provider = INVALID_IP_ADDRESS
361
+ isproxy = -1
362
+ end
363
+ else
364
+ country_short = INVALID_IP_ADDRESS
365
+ country_long = INVALID_IP_ADDRESS
366
+ region = INVALID_IP_ADDRESS
367
+ city = INVALID_IP_ADDRESS
368
+ isp = INVALID_IP_ADDRESS
369
+ proxytype = INVALID_IP_ADDRESS
370
+ domain = INVALID_IP_ADDRESS
371
+ usagetype = INVALID_IP_ADDRESS
372
+ asn = INVALID_IP_ADDRESS
373
+ as = INVALID_IP_ADDRESS
374
+ last_seen = INVALID_IP_ADDRESS
375
+ threat = INVALID_IP_ADDRESS
376
+ provider = INVALID_IP_ADDRESS
377
+ isproxy = -1
378
+ end
379
+ results = {}
380
+ results['is_proxy'] = isproxy
381
+ results['proxy_type'] = proxytype
382
+ results['country_short'] = country_short
383
+ results['country_long'] = country_long
384
+ results['region'] = region
385
+ results['city'] = city
386
+ results['isp'] = isp
387
+ results['domain'] = domain
388
+ results['usagetype'] = usagetype
389
+ results['asn'] = asn
390
+ results['as'] = as
391
+ results['last_seen'] = last_seen
392
+ results['threat'] = threat
393
+ results['provider'] = provider
394
+ return results
395
+ end
396
+
397
+ def bsearch(low, high, ipnum, base_addr, col_length)
398
+ while low <= high do
399
+ mid = (low + high) >> 1
400
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
401
+ if ipnum >= ip_from && ipnum < ip_to
402
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
403
+ file.seek(from_base)
404
+ if v4
405
+ return self.record_class4.read(file)
406
+ else
407
+ return self.record_class6.read(file)
408
+ end
409
+ else
410
+ if ipnum < ip_from
411
+ high = mid - 1
412
+ else
413
+ low = mid + 1
414
+ end
415
+ end
416
+ end
417
+ end
418
+
419
+ def get_from_to(mid, base_addr, col_length)
420
+ from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
421
+ file.seek(from_base)
422
+ ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
423
+ file.seek(from_base + col_length + (v4 ? 0 : 12))
424
+ ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
425
+ [ip_from, ip_to]
426
+ end
427
+
428
+ def validateip(ip)
429
+ if ip.ipv4?
430
+ ipv = 4
431
+ ipnum = ip.to_i + 0
432
+ else
433
+ ipv = 6
434
+ ipnum = ip.to_i + 0
435
+ #reformat ipv4 address in ipv6
436
+ if ipnum >= 281470681743360 && ipnum <= 281474976710655
437
+ ipv = 4
438
+ ipnum = ipnum - 281470681743360
439
+ end
440
+ #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
441
+ if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
442
+ ipv = 4
443
+ #bitshift right 80 bits
444
+ ipnum = ipnum >> 80
445
+ #bitwise modulus to get the last 32 bit
446
+ ipnum = ipnum % 4294967296
447
+ end
448
+ #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
449
+ if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
450
+ ipv = 4
451
+ #bitwise not to invert binary
452
+ ipnum = ~ipnum
453
+ #bitwise modulus to get the last 32 bit
454
+ ipnum = ipnum % 4294967296
455
+ end
456
+ end
457
+ [ipv, ipnum]
458
+ end
459
+
460
+ def read32(indexp)
461
+ file.seek(indexp - 1)
462
+ return file.read(4).unpack('V').first
463
+ end
464
+
465
+ def readipv6(filer)
466
+ parts = filer.read(16).unpack('V*')
467
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
468
+ end
469
+
470
+ private :get_record, :bsearch, :get_from_to, :read32, :readipv6
471
+
428
472
  end