Skip to content

Commit 85ae465

Browse files
committed
Support IP2Proxy Web Service
1 parent b17f152 commit 85ae465

File tree

5 files changed

+88
-64
lines changed

5 files changed

+88
-64
lines changed

README.md

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
[![Total Downloads](https://img.shields.io/gem/dt/ip2proxy_ruby.svg)](https://rubygems.org/gems/ip2proxy_ruby)
33

44
# IP2Proxy Ruby Library
5-
6-
This module allows user to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names.
7-
8-
It lookup the proxy IP address from **IP2Proxy BIN Data** file. This data file can be downloaded at
9-
10-
* Free IP2Proxy BIN Data: https://lite.ip2location.com
11-
* Commercial IP2Proxy BIN Data: https://www.ip2location.com/proxy-database
12-
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.
136

147
For more details, please visit:
158
[https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
169

17-
## Methods
10+
# Usage
11+
You can check the **example.rb** file to learn more about usage.
1812

19-
Below are the methods supported in this module.
13+
## BIN Database
14+
Below is the description of the functions available in the **BIN Database** lookup.
2015

2116
|Method Name|Description|
2217
|---|---|
@@ -41,55 +36,14 @@ Below are the methods supported in this module.
4136
|get_threat|Return the threat types reported to proxy's IP address or domain name. Please see [Threat Type](#threat-type) for details.|
4237
|get_provider|Returns the VPN service provider name if available.|
4338

44-
## Usage
45-
46-
```
47-
require 'ip2proxy_ruby'
48-
49-
# open IP2Proxy BIN database for proxy lookup
50-
i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN")
51-
52-
# get versioning information
53-
print 'Module Version: ' + i2p.get_module_version + "\n"
54-
print 'Package Version: ' + i2p.get_package_version + "\n"
55-
print 'Database Version: ' + i2p.get_database_version + "\n"
56-
57-
# individual proxy data check
58-
print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
59-
print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
60-
print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
61-
print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
62-
print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
63-
print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
64-
print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
65-
print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
66-
print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
67-
print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
68-
print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
69-
print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
70-
print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
71-
print 'Provider: ' + i2p.get_provider('1.2.3.4') + "\n"
72-
73-
# single function to get all proxy data returned in array
74-
record = i2p.get_all('1.2.3.4')
75-
print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
76-
print 'Proxy Type: ' + record['proxy_type'] + "\n"
77-
print 'Country Code: ' + record['country_short'] + "\n"
78-
print 'Country Name: ' + record['country_long'] + "\n"
79-
print 'Region Name: ' + record['region'] + "\n"
80-
print 'City Name: ' + record['city'] + "\n"
81-
print 'ISP: ' + record['isp'] + "\n"
82-
print 'Domain: ' + record['domain'] + "\n"
83-
print 'Usage Type: ' + record['usagetype'] + "\n"
84-
print 'ASN: ' + record['asn'] + "\n"
85-
print 'AS: ' + record['as'] + "\n"
86-
print 'Last Seen: ' + record['last_seen'] + "\n"
87-
print 'Threat: ' + record['threat'] + "\n"
88-
print 'Provider: ' + record['provider'] + "\n"
89-
90-
# close IP2Proxy BIN database
91-
i2p.close()
92-
```
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. |
9347

9448
### Proxy Type
9549

@@ -128,7 +82,17 @@ i2p.close()
12882
|SCANNER|Security Scanner or Attack|
12983
|BOTNET|Spyware or Malware|
13084

131-
## Support
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
13296

13397
13498
URL: [https://www.ip2location.com](https://www.ip2location.com)

example.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@
4242
print 'Provider: ' + record['provider'] + "\n"
4343

4444
# close IP2Proxy BIN database
45-
i2p.close()
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()

ip2proxy_ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |s|
44
s.name = "ip2proxy_ruby"
5-
s.version = "3.1.0"
5+
s.version = "3.2.0"
66
s.authors = ["ip2location"]
77
s.email = ["[email protected]"]
88

lib/ip2proxy_ruby.rb

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'bindata'
22
require 'ipaddr'
3+
require 'net/http'
4+
require 'json'
35
require_relative 'ip2proxy_ruby/ip2proxy_config'
46
require_relative 'ip2proxy_ruby/i2p_database_config'
57
require_relative 'ip2proxy_ruby/i2p_string_data'
@@ -9,7 +11,7 @@
911
class Ip2proxy
1012
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
1113

12-
VERSION = '3.1.0'
14+
VERSION = '3.2.0'
1315
FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
1416
INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
1517
INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.'
@@ -469,4 +471,55 @@ def readipv6(filer)
469471

470472
private :get_record, :bsearch, :get_from_to, :read32, :readipv6
471473

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
472525
end

spec/ip2proxy_ruby_database_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
it "work correctly with get_module_version" do
107107
i2p = Ip2proxy.new.open(File.dirname(__FILE__) + "/assets/PX11.SAMPLE.BIN")
108108
record = i2p.get_module_version()
109-
expect(record).to eq '3.1.0'
109+
expect(record).to eq '3.2.0'
110110
end
111111

112112
it "work correctly with get_package_version" do

0 commit comments

Comments
 (0)