ip2proxy_ruby 2.1.0 → 3.2.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: 1dc20326c582b22c3ada59ab4987c530b54a4c1693546783fe03ac0afa4d7c3d
4
- data.tar.gz: e40ee8fc6baec6675fbe8375646d78a3e7d38f0b37f5fb7d8840fbc6b78a7026
3
+ metadata.gz: 11876f082eb12299523abfba314eedec46e9c21a77402a87e9893b64e1bfc4cb
4
+ data.tar.gz: b05171fa3e3b7a1edcd2864a501cd26a4516870209507cb23878785861cc9634
5
5
  SHA512:
6
- metadata.gz: 947b66ffec9139e7531e9569c9da945b056c842304ab4d866b918995cfb20834d439197d7c7b1119c6a2611fbe2c4fff60ea69126960ef916f1c1e6ea1eb05ce
7
- data.tar.gz: e182e739e69e2160610b911e67d51997b4a41f58f851d526ea3742b1ddc692aa31cd10790984fad532d0bc92750d7096d2f7aa6201af361e6f7678a39e8e64be
6
+ metadata.gz: bbd818693fd97afaaf9abb6eb0de6e8235d584347309cac75d9c3c2a7f28e81c32f6b2cd2d227f74cd8058f82c688ba167aabadb7a04bdc279f7ab79dbfc632b
7
+ data.tar.gz: 2eb3b4766c5f7b28b477187f9bcdced970b7516e724f21639511734a6f3df92ffa578fb86bd752a8870515a05b7d0756221d58d540cb2d55a267b56cbdb14d6c
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,119 +1,98 @@
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 and data center ranges using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name 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 4 for PX1 to PX4 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/databases/px4-ip-proxytype-country-region-city-isp" 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
-
42
- ## Usage
43
-
44
- ```
45
- require 'ip2proxy_ruby'
46
-
47
- # open IP2Proxy BIN database for proxy lookup
48
- i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.BIN")
49
-
50
- # get versioning information
51
- print 'Module Version: ' + i2p.get_module_version + "\n"
52
- print 'Package Version: ' + i2p.get_package_version + "\n"
53
- print 'Database Version: ' + i2p.get_database_version + "\n"
54
-
55
- # individual proxy data check
56
- print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
57
- print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
58
- print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
59
- print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
60
- print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
61
- print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
62
- print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
63
- print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
64
- print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
65
- print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
66
- print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
67
- print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
68
-
69
- # single function to get all proxy data returned in array
70
- record = i2p.get_all('1.2.3.4')
71
- print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
72
- print 'Proxy Type: ' + record['proxy_type'] + "\n"
73
- print 'Country Code: ' + record['country_short'] + "\n"
74
- print 'Country Name: ' + record['country_long'] + "\n"
75
- print 'Region Name: ' + record['region'] + "\n"
76
- print 'City Name: ' + record['city'] + "\n"
77
- print 'ISP: ' + record['isp'] + "\n"
78
- print 'Domain: ' + record['domain'] + "\n"
79
- print 'Usage Type: ' + record['usagetype'] + "\n"
80
- print 'ASN: ' + record['asn'] + "\n"
81
- print 'AS: ' + record['as'] + "\n"
82
- print 'Last Seen: ' + record['last_seen'] + "\n"
83
-
84
- # close IP2Proxy BIN database
85
- i2p.close()
86
- ```
87
-
88
- ### Proxy Type
89
-
90
- |Proxy Type|Description|
91
- |---|---|
92
- |VPN|Anonymizing VPN services.|
93
- |TOR|Tor Exit Nodes.|
94
- |PUB|Public Proxies.|
95
- |WEB|Web Proxies.|
96
- |DCH|Hosting Providers/Data Center.|
97
- |SES|Search Engine Robots.
98
-
99
- ### Usage Type
100
-
101
- |Usage Type|Description|
102
- |---|---|
103
- |COM|Commercial|
104
- |ORG|Organization|
105
- |GOV|Government|
106
- |MIL|Military|
107
- |EDU|University/College/School|
108
- |LIB|Library|
109
- |CDN|Content Delivery Network|
110
- |ISP|Fixed Line ISP|
111
- |MOB|Mobile ISP|
112
- |DCH|Data Center/Web Hosting/Transit|
113
- |SES|Search Engine Spider|
114
- |RSV|Reserved|
115
-
116
- ## Support
117
-
118
- Email: support@ip2location.com
119
- 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
+ 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. It lookup the proxy IP address from **IP2Proxy BIN Data** file or web service.
6
+
7
+ For more details, please visit:
8
+ [https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
9
+
10
+ # Usage
11
+ You can check the **example.rb** file to learn more about usage.
12
+
13
+ ## BIN Database
14
+ Below is the description of the functions available in the **BIN Database** lookup.
15
+
16
+ |Method Name|Description|
17
+ |---|---|
18
+ |open|Open the IP2Proxy BIN data with **File I/O** mode for lookup.|
19
+ |close|Close and clean up the file pointer.|
20
+ |get_package_version|Get the package version (1 to 11 for PX1 to PX11 respectively).|
21
+ |get_module_version|Get the module version.|
22
+ |get_database_version|Get the database version.|
23
+ |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>|
24
+ |get_all|Return the proxy information in array.|
25
+ |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|
26
+ |get_country_short|Return the ISO3166-1 country code (2-digits) of the proxy.|
27
+ |get_country_long|Return the ISO3166-1 country name of the proxy.|
28
+ |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|
29
+ |get_city|Return the city name of the proxy.|
30
+ |get_isp|Return the ISP name of the proxy.|
31
+ |get_domain|Return the domain name of proxy's IP address or domain name.|
32
+ |get_usagetype|Return the ISP's usage type of proxy's IP address or domain name. Please see [Usage Type](#usage-type) for details.|
33
+ |get_asn|Return the autonomous system number (ASN) of proxy's IP address or domain name.|
34
+ |get_as|Return the autonomous system (AS) name of proxy's IP address or domain name.|
35
+ |get_last_seen|Return the last seen days ago value of proxy's IP address or domain name.|
36
+ |get_threat|Return the threat types reported to proxy's IP address or domain name. Please see [Threat Type](#threat-type) for details.|
37
+ |get_provider|Returns the VPN service provider name if available.|
38
+
39
+ ## Web Service
40
+ Below is the description of the functions available in the **Web Service** lookup.
41
+
42
+ | Function Name | Description |
43
+ |---|---|
44
+ | Constructor | Expect 3 input parameters:<ol><li>IP2Proxy API Key.</li><li>Package (PX1 - PX11)</li></li><li>Use HTTPS or HTTP</li></ol> |
45
+ | lookup | Return the proxy information in array.<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> |
46
+ | get_credit | Return remaining credit of the web service account. |
47
+
48
+ ### Proxy Type
49
+
50
+ |Proxy Type|Description|
51
+ |---|---|
52
+ |VPN|Anonymizing VPN services|
53
+ |TOR|Tor Exit Nodes|
54
+ |PUB|Public Proxies|
55
+ |WEB|Web Proxies|
56
+ |DCH|Hosting Providers/Data Center|
57
+ |SES|Search Engine Robots|
58
+ |RES|Residential Proxies [PX10+]|
59
+
60
+ ### Usage Type
61
+
62
+ |Usage Type|Description|
63
+ |---|---|
64
+ |COM|Commercial|
65
+ |ORG|Organization|
66
+ |GOV|Government|
67
+ |MIL|Military|
68
+ |EDU|University/College/School|
69
+ |LIB|Library|
70
+ |CDN|Content Delivery Network|
71
+ |ISP|Fixed Line ISP|
72
+ |MOB|Mobile ISP|
73
+ |DCH|Data Center/Web Hosting/Transit|
74
+ |SES|Search Engine Spider|
75
+ |RSV|Reserved|
76
+
77
+ ### Threat Type
78
+
79
+ |Threat Type|Description|
80
+ |---|---|
81
+ |SPAM|Spammer|
82
+ |SCANNER|Security Scanner or Attack|
83
+ |BOTNET|Spyware or Malware|
84
+
85
+ # Dependencies
86
+
87
+ This library requires IP2Proxy BIN data file to function. You may download the BIN data file at
88
+ * IP2Proxy LITE BIN Data (Free): https://lite.ip2location.com
89
+ * IP2Proxy Commercial BIN Data (Comprehensive): https://www.ip2location.com/proxy-database
90
+
91
+ An outdated BIN database was provided in the library for your testing. You are recommended to visit the above links to download the latest BIN database.
92
+
93
+ You can also sign up for [IP2Proxy Web Service](https://www.ip2location.com/web-service/ip2proxy) to lookup by IP2Proxy API.
94
+
95
+ # Support
96
+
97
+ Email: support@ip2location.com
98
+ URL: [https://www.ip2location.com](https://www.ip2location.com)
data/example.rb CHANGED
@@ -1,41 +1,52 @@
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
-
25
- # single function to get all proxy data returned in array
26
- record = i2p.get_all('1.2.3.4')
27
- print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
28
- print 'Proxy Type: ' + record['proxy_type'] + "\n"
29
- print 'Country Code: ' + record['country_short'] + "\n"
30
- print 'Country Name: ' + record['country_long'] + "\n"
31
- print 'Region Name: ' + record['region'] + "\n"
32
- print 'City Name: ' + record['city'] + "\n"
33
- print 'ISP: ' + record['isp'] + "\n"
34
- print 'Domain: ' + record['domain'] + "\n"
35
- print 'Usage Type: ' + record['usagetype'] + "\n"
36
- print 'ASN: ' + record['asn'] + "\n"
37
- print 'AS: ' + record['as'] + "\n"
38
- print 'Last Seen: ' + record['last_seen'] + "\n"
39
-
40
- # close IP2Proxy BIN database
41
- i2p.close()
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
45
+ i2p.close()
46
+
47
+ # Web Service
48
+ ws = Ip2proxyWebService.new('demo', 'PX11', true)
49
+ record = ws.lookup('1.2.3.4')
50
+ print record
51
+ print "\n"
52
+ print ws.get_credit()
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "ip2proxy_ruby"
5
- s.version = "2.1.0"
5
+ s.version = "3.2.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 and data center ranges using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name 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=)
@@ -50,25 +50,19 @@ Gem::Specification.new do |s|
50
50
 
51
51
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
52
  s.add_runtime_dependency(%q<bindata>, [">= 0"])
53
- s.add_development_dependency(%q<awesome_print>, [">= 0"])
54
53
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
55
54
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
56
55
  s.add_development_dependency(%q<bundler>, [">= 1.2.0"])
57
- s.add_development_dependency(%q<simplecov>, [">= 0"])
58
56
  else
59
57
  s.add_dependency(%q<bindata>, [">= 0"])
60
- s.add_dependency(%q<awesome_print>, [">= 0"])
61
58
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
62
59
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
60
  s.add_dependency(%q<bundler>, [">= 1.2.0"])
64
- s.add_dependency(%q<simplecov>, [">= 0"])
65
61
  end
66
62
  else
67
63
  s.add_dependency(%q<bindata>, [">= 0"])
68
- s.add_dependency(%q<awesome_print>, [">= 0"])
69
64
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
70
65
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
71
66
  s.add_dependency(%q<bundler>, [">= 1.2.0"])
72
- s.add_dependency(%q<simplecov>, [">= 0"])
73
67
  end
74
68
  end
data/lib/ip2proxy_ruby.rb CHANGED
@@ -1,409 +1,525 @@
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 = '2.1.0'
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 is_proxy(ip)
251
- valid = !(IPAddr.new(ip) rescue nil).nil?
252
- if valid
253
- rec = get_record(ip)
254
- if !(rec.nil?)
255
- if self.db_index == 1
256
- isproxy = (rec.country_short == '-') ? 0 : 1
257
- else
258
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
259
- end
260
- else
261
- isproxy = -1
262
- end
263
- else
264
- isproxy = -1
265
- end
266
- return isproxy
267
- end
268
-
269
- def get_all(ip)
270
- valid = !(IPAddr.new(ip) rescue nil).nil?
271
- if valid
272
- rec = get_record(ip)
273
- if !(rec.nil?)
274
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
275
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
276
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
277
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
278
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
279
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
280
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
281
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
282
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
283
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
284
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
285
- if self.db_index == 1
286
- isproxy = (rec.country_short == '-') ? 0 : 1
287
- else
288
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
289
- end
290
- else
291
- country_short = INVALID_IP_ADDRESS
292
- country_long = INVALID_IP_ADDRESS
293
- region = INVALID_IP_ADDRESS
294
- city = INVALID_IP_ADDRESS
295
- isp = INVALID_IP_ADDRESS
296
- proxytype = INVALID_IP_ADDRESS
297
- domain = INVALID_IP_ADDRESS
298
- usagetype = INVALID_IP_ADDRESS
299
- asn = INVALID_IP_ADDRESS
300
- as = INVALID_IP_ADDRESS
301
- last_seen = INVALID_IP_ADDRESS
302
- isproxy = -1
303
- end
304
- else
305
- country_short = INVALID_IP_ADDRESS
306
- country_long = INVALID_IP_ADDRESS
307
- region = INVALID_IP_ADDRESS
308
- city = INVALID_IP_ADDRESS
309
- isp = INVALID_IP_ADDRESS
310
- proxytype = INVALID_IP_ADDRESS
311
- domain = INVALID_IP_ADDRESS
312
- usagetype = INVALID_IP_ADDRESS
313
- asn = INVALID_IP_ADDRESS
314
- as = INVALID_IP_ADDRESS
315
- last_seen = INVALID_IP_ADDRESS
316
- isproxy = -1
317
- end
318
- results = {}
319
- results['is_proxy'] = isproxy
320
- results['proxy_type'] = proxytype
321
- results['country_short'] = country_short
322
- results['country_long'] = country_long
323
- results['region'] = region
324
- results['city'] = city
325
- results['isp'] = isp
326
- results['domain'] = domain
327
- results['usagetype'] = usagetype
328
- results['asn'] = asn
329
- results['as'] = as
330
- results['last_seen'] = last_seen
331
- return results
332
- end
333
-
334
- def bsearch(low, high, ipnum, base_addr, col_length)
335
- while low <= high do
336
- mid = (low + high) >> 1
337
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
338
- if ipnum >= ip_from && ipnum < ip_to
339
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
340
- file.seek(from_base)
341
- if v4
342
- return self.record_class4.read(file)
343
- else
344
- return self.record_class6.read(file)
345
- end
346
- else
347
- if ipnum < ip_from
348
- high = mid - 1
349
- else
350
- low = mid + 1
351
- end
352
- end
353
- end
354
- end
355
-
356
- def get_from_to(mid, base_addr, col_length)
357
- from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
358
- file.seek(from_base)
359
- ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
360
- file.seek(from_base + col_length + (v4 ? 0 : 12))
361
- ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
362
- [ip_from, ip_to]
363
- end
364
-
365
- def validateip(ip)
366
- if ip.ipv4?
367
- ipv = 4
368
- ipnum = ip.to_i + 0
369
- else
370
- ipv = 6
371
- ipnum = ip.to_i + 0
372
- #reformat ipv4 address in ipv6
373
- if ipnum >= 281470681743360 && ipnum <= 281474976710655
374
- ipv = 4
375
- ipnum = ipnum - 281470681743360
376
- end
377
- #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
378
- if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
379
- ipv = 4
380
- #bitshift right 80 bits
381
- ipnum = ipnum >> 80
382
- #bitwise modulus to get the last 32 bit
383
- ipnum = ipnum % 4294967296
384
- end
385
- #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
386
- if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
387
- ipv = 4
388
- #bitwise not to invert binary
389
- ipnum = ~ipnum
390
- #bitwise modulus to get the last 32 bit
391
- ipnum = ipnum % 4294967296
392
- end
393
- end
394
- [ipv, ipnum]
395
- end
396
-
397
- def read32(indexp)
398
- file.seek(indexp - 1)
399
- return file.read(4).unpack('V').first
400
- end
401
-
402
- def readipv6(filer)
403
- parts = filer.read(16).unpack('V*')
404
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
405
- end
406
-
407
- private :get_record, :bsearch, :get_from_to, :read32, :readipv6
408
-
1
+ require 'bindata'
2
+ require 'ipaddr'
3
+ require 'net/http'
4
+ require 'json'
5
+ require_relative 'ip2proxy_ruby/ip2proxy_config'
6
+ require_relative 'ip2proxy_ruby/i2p_database_config'
7
+ require_relative 'ip2proxy_ruby/i2p_string_data'
8
+ require_relative 'ip2proxy_ruby/i2p_ip_data'
9
+ require_relative 'ip2proxy_ruby/ip2proxy_record'
10
+
11
+ class Ip2proxy
12
+ 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
13
+
14
+ VERSION = '3.2.0'
15
+ FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
16
+ INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
17
+ INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.'
18
+
19
+ def open(url)
20
+ if url == ''
21
+ self.last_err_msg = 'Ip2proxy.new.open() requires a database path name.'
22
+ abort('Ip2proxy.new.open() requires a database path name.')
23
+ end
24
+
25
+ begin
26
+ self.file = File.open(File.expand_path url, 'rb')
27
+ rescue
28
+ self.last_err_msg = 'Ip2proxy.new.open() error in opening ' + url +'.'
29
+ abort('Ip2proxy.new.open() error in opening ' + url +'.')
30
+ else
31
+ end
32
+ i2p = Ip2proxyConfig.read(file)
33
+ if i2p.productcode == 2
34
+ else
35
+ if i2p.databaseyear <= 20 && i2p.productcode == 0
36
+ else
37
+ self.file.close
38
+ self.last_err_msg = INVALID_BIN_DATABASE
39
+ abort(INVALID_BIN_DATABASE)
40
+ end
41
+ end
42
+ self.db_index = i2p.databasetype
43
+ self.columns = i2p.databasecolumn + 0
44
+ self.databaseyear = 2000 + i2p.databaseyear
45
+ self.databasemonth = i2p.databasemonth
46
+ self.databaseday = i2p.databaseday
47
+ self.database = I2pDbConfig.setup_database(self.db_index)
48
+ self.ipv4databasecount = i2p.ipv4databasecount
49
+ self.ipv4databaseaddr = i2p.ipv4databaseaddr
50
+ self.ipv6databasecount = i2p.ipv6databasecount
51
+ self.ipv6databaseaddr = i2p.ipv6databaseaddr
52
+ self.ipv4indexbaseaddr = i2p.ipv4indexbaseaddr
53
+ self.ipv6indexbaseaddr = i2p.ipv6indexbaseaddr
54
+ self.record_class4 = (Ip2ProxyRecord.init database, 4)
55
+ self.record_class6 = (Ip2ProxyRecord.init database, 6)
56
+ self
57
+ end
58
+
59
+ def close()
60
+ self.file.close
61
+ end
62
+
63
+ def get_last_error_message()
64
+ return self.last_err_msg
65
+ end
66
+
67
+ def get_module_version()
68
+ return VERSION
69
+ end
70
+
71
+ def get_package_version()
72
+ return (self.db_index).to_s
73
+ end
74
+
75
+ def get_database_version()
76
+ return (self.databaseyear).to_s + "." + (self.databasemonth).to_s + "." + (self.databaseday).to_s
77
+ end
78
+
79
+ def get_record(ip)
80
+ ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
81
+ self.ip_version, ipnum = validateip(ipno)
82
+ self.v4 = ip_version == 4 ? true : false
83
+ self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
84
+ self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
85
+ col_length = columns * 4
86
+ if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
87
+ indexpos = 0
88
+ case ip_version
89
+ when 4
90
+ indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
91
+ realipno = ipnum
92
+ # if ipnum reach MAX_IPV4_RANGE
93
+ if realipno == 4294967295
94
+ ipnum = realipno - 1
95
+ end
96
+ when 6
97
+ indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
98
+ realipno = ipnum
99
+ # if ipnum reach MAX_IPV6_RANGE
100
+ if realipno == 340282366920938463463374607431768211455
101
+ ipnum = realipno - 1
102
+ end
103
+ end
104
+ low = read32(indexpos)
105
+ high = read32(indexpos + 4)
106
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
107
+ else
108
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
109
+ end
110
+ end
111
+
112
+ def get_country_short(ip)
113
+ valid = !(IPAddr.new(ip) rescue nil).nil?
114
+ if valid
115
+ rec = get_record(ip)
116
+ if !(rec.nil?)
117
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
118
+ else
119
+ country_short = INVALID_IP_ADDRESS
120
+ end
121
+ else
122
+ country_short = INVALID_IP_ADDRESS
123
+ end
124
+ return country_short
125
+ end
126
+
127
+ def get_country_long(ip)
128
+ valid = !(IPAddr.new(ip) rescue nil).nil?
129
+ if valid
130
+ rec = get_record(ip)
131
+ if !(rec.nil?)
132
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
133
+ else
134
+ country_long = INVALID_IP_ADDRESS
135
+ end
136
+ else
137
+ country_long = INVALID_IP_ADDRESS
138
+ end
139
+ return country_long
140
+ end
141
+
142
+ def get_region(ip)
143
+ valid = !(IPAddr.new(ip) rescue nil).nil?
144
+ if valid
145
+ rec = get_record(ip)
146
+ if !(rec.nil?)
147
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
148
+ else
149
+ region = INVALID_IP_ADDRESS
150
+ end
151
+ else
152
+ region = INVALID_IP_ADDRESS
153
+ end
154
+ return region
155
+ end
156
+
157
+ def get_city(ip)
158
+ valid = !(IPAddr.new(ip) rescue nil).nil?
159
+ if valid
160
+ rec = get_record(ip)
161
+ if !(rec.nil?)
162
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
163
+ else
164
+ city = INVALID_IP_ADDRESS
165
+ end
166
+ else
167
+ city = INVALID_IP_ADDRESS
168
+ end
169
+ return city
170
+ end
171
+
172
+ def get_isp(ip)
173
+ valid = !(IPAddr.new(ip) rescue nil).nil?
174
+ if valid
175
+ rec = get_record(ip)
176
+ if !(rec.nil?)
177
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
178
+ else
179
+ isp = INVALID_IP_ADDRESS
180
+ end
181
+ else
182
+ isp = INVALID_IP_ADDRESS
183
+ end
184
+ return isp
185
+ end
186
+
187
+ def get_proxytype(ip)
188
+ valid = !(IPAddr.new(ip) rescue nil).nil?
189
+ if valid
190
+ rec = get_record(ip)
191
+ if !(rec.nil?)
192
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
193
+ else
194
+ proxytype = INVALID_IP_ADDRESS
195
+ end
196
+ else
197
+ proxytype = INVALID_IP_ADDRESS
198
+ end
199
+ return proxytype
200
+ end
201
+
202
+ def get_domain(ip)
203
+ valid = !(IPAddr.new(ip) rescue nil).nil?
204
+ if valid
205
+ rec = get_record(ip)
206
+ if !(rec.nil?)
207
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
208
+ else
209
+ domain = INVALID_IP_ADDRESS
210
+ end
211
+ else
212
+ domain = INVALID_IP_ADDRESS
213
+ end
214
+ return domain
215
+ end
216
+
217
+ def get_usagetype(ip)
218
+ valid = !(IPAddr.new(ip) rescue nil).nil?
219
+ if valid
220
+ rec = get_record(ip)
221
+ if !(rec.nil?)
222
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
223
+ else
224
+ usagetype = INVALID_IP_ADDRESS
225
+ end
226
+ else
227
+ usagetype = INVALID_IP_ADDRESS
228
+ end
229
+ return usagetype
230
+ end
231
+
232
+ def get_asn(ip)
233
+ valid = !(IPAddr.new(ip) rescue nil).nil?
234
+ if valid
235
+ rec = get_record(ip)
236
+ if !(rec.nil?)
237
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
238
+ else
239
+ asn = INVALID_IP_ADDRESS
240
+ end
241
+ else
242
+ asn = INVALID_IP_ADDRESS
243
+ end
244
+ return asn
245
+ end
246
+
247
+ def get_as(ip)
248
+ valid = !(IPAddr.new(ip) rescue nil).nil?
249
+ if valid
250
+ rec = get_record(ip)
251
+ if !(rec.nil?)
252
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
253
+ else
254
+ as = INVALID_IP_ADDRESS
255
+ end
256
+ else
257
+ as = INVALID_IP_ADDRESS
258
+ end
259
+ return as
260
+ end
261
+
262
+ def get_last_seen(ip)
263
+ valid = !(IPAddr.new(ip) rescue nil).nil?
264
+ if valid
265
+ rec = get_record(ip)
266
+ if !(rec.nil?)
267
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
268
+ else
269
+ last_seen = INVALID_IP_ADDRESS
270
+ end
271
+ else
272
+ last_seen = INVALID_IP_ADDRESS
273
+ end
274
+ return last_seen
275
+ end
276
+
277
+ def get_threat(ip)
278
+ valid = !(IPAddr.new(ip) rescue nil).nil?
279
+ if valid
280
+ rec = get_record(ip)
281
+ if !(rec.nil?)
282
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
283
+ else
284
+ threat = INVALID_IP_ADDRESS
285
+ end
286
+ else
287
+ threat = INVALID_IP_ADDRESS
288
+ end
289
+ return threat
290
+ end
291
+
292
+ def is_proxy(ip)
293
+ valid = !(IPAddr.new(ip) rescue nil).nil?
294
+ if valid
295
+ rec = get_record(ip)
296
+ if !(rec.nil?)
297
+ if self.db_index == 1
298
+ isproxy = (rec.country_short == '-') ? 0 : 1
299
+ else
300
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
301
+ end
302
+ else
303
+ isproxy = -1
304
+ end
305
+ else
306
+ isproxy = -1
307
+ end
308
+ return isproxy
309
+ end
310
+
311
+ def get_provider(ip)
312
+ valid = !(IPAddr.new(ip) rescue nil).nil?
313
+ if valid
314
+ rec = get_record(ip)
315
+ if !(rec.nil?)
316
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
317
+ else
318
+ provider = INVALID_IP_ADDRESS
319
+ end
320
+ else
321
+ provider = INVALID_IP_ADDRESS
322
+ end
323
+ return provider
324
+ end
325
+
326
+ def get_all(ip)
327
+ valid = !(IPAddr.new(ip) rescue nil).nil?
328
+ if valid
329
+ rec = get_record(ip)
330
+ if !(rec.nil?)
331
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
332
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
333
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
334
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
335
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
336
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
337
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
338
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
339
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
340
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
341
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
342
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
343
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
344
+ if self.db_index == 1
345
+ isproxy = (rec.country_short == '-') ? 0 : 1
346
+ else
347
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
348
+ end
349
+ else
350
+ country_short = INVALID_IP_ADDRESS
351
+ country_long = INVALID_IP_ADDRESS
352
+ region = INVALID_IP_ADDRESS
353
+ city = INVALID_IP_ADDRESS
354
+ isp = INVALID_IP_ADDRESS
355
+ proxytype = INVALID_IP_ADDRESS
356
+ domain = INVALID_IP_ADDRESS
357
+ usagetype = INVALID_IP_ADDRESS
358
+ asn = INVALID_IP_ADDRESS
359
+ as = INVALID_IP_ADDRESS
360
+ last_seen = INVALID_IP_ADDRESS
361
+ threat = INVALID_IP_ADDRESS
362
+ provider = INVALID_IP_ADDRESS
363
+ isproxy = -1
364
+ end
365
+ else
366
+ country_short = INVALID_IP_ADDRESS
367
+ country_long = INVALID_IP_ADDRESS
368
+ region = INVALID_IP_ADDRESS
369
+ city = INVALID_IP_ADDRESS
370
+ isp = INVALID_IP_ADDRESS
371
+ proxytype = INVALID_IP_ADDRESS
372
+ domain = INVALID_IP_ADDRESS
373
+ usagetype = INVALID_IP_ADDRESS
374
+ asn = INVALID_IP_ADDRESS
375
+ as = INVALID_IP_ADDRESS
376
+ last_seen = INVALID_IP_ADDRESS
377
+ threat = INVALID_IP_ADDRESS
378
+ provider = INVALID_IP_ADDRESS
379
+ isproxy = -1
380
+ end
381
+ results = {}
382
+ results['is_proxy'] = isproxy
383
+ results['proxy_type'] = proxytype
384
+ results['country_short'] = country_short
385
+ results['country_long'] = country_long
386
+ results['region'] = region
387
+ results['city'] = city
388
+ results['isp'] = isp
389
+ results['domain'] = domain
390
+ results['usagetype'] = usagetype
391
+ results['asn'] = asn
392
+ results['as'] = as
393
+ results['last_seen'] = last_seen
394
+ results['threat'] = threat
395
+ results['provider'] = provider
396
+ return results
397
+ end
398
+
399
+ def bsearch(low, high, ipnum, base_addr, col_length)
400
+ while low <= high do
401
+ mid = (low + high) >> 1
402
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
403
+ if ipnum >= ip_from && ipnum < ip_to
404
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
405
+ file.seek(from_base)
406
+ if v4
407
+ return self.record_class4.read(file)
408
+ else
409
+ return self.record_class6.read(file)
410
+ end
411
+ else
412
+ if ipnum < ip_from
413
+ high = mid - 1
414
+ else
415
+ low = mid + 1
416
+ end
417
+ end
418
+ end
419
+ end
420
+
421
+ def get_from_to(mid, base_addr, col_length)
422
+ from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
423
+ file.seek(from_base)
424
+ ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
425
+ file.seek(from_base + col_length + (v4 ? 0 : 12))
426
+ ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
427
+ [ip_from, ip_to]
428
+ end
429
+
430
+ def validateip(ip)
431
+ if ip.ipv4?
432
+ ipv = 4
433
+ ipnum = ip.to_i + 0
434
+ else
435
+ ipv = 6
436
+ ipnum = ip.to_i + 0
437
+ #reformat ipv4 address in ipv6
438
+ if ipnum >= 281470681743360 && ipnum <= 281474976710655
439
+ ipv = 4
440
+ ipnum = ipnum - 281470681743360
441
+ end
442
+ #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
443
+ if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
444
+ ipv = 4
445
+ #bitshift right 80 bits
446
+ ipnum = ipnum >> 80
447
+ #bitwise modulus to get the last 32 bit
448
+ ipnum = ipnum % 4294967296
449
+ end
450
+ #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
451
+ if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
452
+ ipv = 4
453
+ #bitwise not to invert binary
454
+ ipnum = ~ipnum
455
+ #bitwise modulus to get the last 32 bit
456
+ ipnum = ipnum % 4294967296
457
+ end
458
+ end
459
+ [ipv, ipnum]
460
+ end
461
+
462
+ def read32(indexp)
463
+ file.seek(indexp - 1)
464
+ return file.read(4).unpack('V').first
465
+ end
466
+
467
+ def readipv6(filer)
468
+ parts = filer.read(16).unpack('V*')
469
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
470
+ end
471
+
472
+ private :get_record, :bsearch, :get_from_to, :read32, :readipv6
473
+
474
+ end
475
+
476
+ class Ip2proxyWebService
477
+ attr_accessor :ws_api_key, :ws_package, :ws_use_ssl
478
+
479
+ def initialize(api_key, package, use_ssl)
480
+ if !api_key.match(/^[0-9A-Z]{10}$/) && api_key != 'demo'
481
+ raise Exception.new "Please provide a valid IP2Proxy web service API key."
482
+ end
483
+ if !package.match(/^PX[0-9]+$/)
484
+ package = 'PX1'
485
+ end
486
+ if use_ssl == ''
487
+ use_ssl = true
488
+ end
489
+ self.ws_api_key = api_key
490
+ self.ws_package = package
491
+ self.ws_use_ssl = use_ssl
492
+ end
493
+
494
+ def lookup(ip)
495
+ if self.ws_use_ssl
496
+ response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
497
+ else
498
+ response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
499
+ end
500
+ parsed_response = JSON.parse(response)
501
+ if parsed_response.nil?
502
+ return false
503
+ end
504
+ if parsed_response["response"] != "OK"
505
+ raise Exception.new "Error: " + parsed_response["response"]
506
+ end
507
+ return parsed_response
508
+ end
509
+
510
+ def get_credit()
511
+ if self.ws_use_ssl
512
+ response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
513
+ else
514
+ response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
515
+ end
516
+ parsed_response = JSON.parse(response)
517
+ if parsed_response.nil?
518
+ return 0
519
+ end
520
+ if parsed_response["response"].nil?
521
+ return 0
522
+ end
523
+ return parsed_response["response"]
524
+ end
409
525
  end