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 +4 -4
- data/LICENSE.txt +20 -20
- data/README.md +98 -119
- data/example.rb +52 -41
- data/ip2proxy_ruby.gemspec +5 -11
- data/lib/ip2proxy_ruby.rb +524 -408
- data/lib/ip2proxy_ruby/i2p_database_config.rb +27 -25
- data/lib/ip2proxy_ruby/i2p_ip_data.rb +9 -9
- data/lib/ip2proxy_ruby/i2p_string_data.rb +15 -15
- data/lib/ip2proxy_ruby/ip2proxy_config.rb +19 -16
- data/lib/ip2proxy_ruby/ip2proxy_record.rb +19 -19
- data/rb/data/PX11.SAMPLE.BIN +0 -0
- data/spec/assets/PX11.SAMPLE.BIN +0 -0
- data/spec/ip2proxy_ruby_database_spec.rb +123 -0
- data/spec/spec_helper.rb +0 -1
- metadata +11 -37
- data/rb/data/IP2PROXY-LITE-PX1.BIN +0 -0
- data/spec/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN +0 -0
- data/spec/ip2proxy_ruby_spec.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11876f082eb12299523abfba314eedec46e9c21a77402a87e9893b64e1bfc4cb
|
4
|
+
data.tar.gz: b05171fa3e3b7a1edcd2864a501cd26a4516870209507cb23878785861cc9634
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbd818693fd97afaaf9abb6eb0de6e8235d584347309cac75d9c3c2a7f28e81c32f6b2cd2d227f74cd8058f82c688ba167aabadb7a04bdc279f7ab79dbfc632b
|
7
|
+
data.tar.gz: 2eb3b4766c5f7b28b477187f9bcdced970b7516e724f21639511734a6f3df92ffa578fb86bd752a8870515a05b7d0756221d58d540cb2d55a267b56cbdb14d6c
|
data/LICENSE.txt
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c)
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
|
22
|
-
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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/
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
print '
|
30
|
-
print '
|
31
|
-
print '
|
32
|
-
print '
|
33
|
-
print '
|
34
|
-
print '
|
35
|
-
print '
|
36
|
-
print '
|
37
|
-
print '
|
38
|
-
print '
|
39
|
-
|
40
|
-
|
41
|
-
|
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()
|
data/ip2proxy_ruby.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ip2proxy_ruby"
|
5
|
-
s.version = "2.
|
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
|
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/
|
35
|
-
"spec/
|
34
|
+
"spec/assets/PX11.SAMPLE.BIN",
|
35
|
+
"spec/ip2proxy_ruby_database_spec.rb",
|
36
36
|
"spec/spec_helper.rb",
|
37
|
-
"rb/data/
|
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
|
-
|
4
|
-
|
5
|
-
require_relative 'ip2proxy_ruby/
|
6
|
-
require_relative 'ip2proxy_ruby/
|
7
|
-
require_relative 'ip2proxy_ruby/
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
self.
|
55
|
-
self.
|
56
|
-
self
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
[
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
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
|