Skip to content

Commit

Permalink
Added support for 6to4 and Teredo
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Jul 5, 2019
1 parent ad65b88 commit 7f27228
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Below are the methods supported in this module.
|---|---|
|open|Open the IP2Proxy BIN data with **File I/O** mode for lookup.|
|close|Close and clean up the file pointer.|
|get_package_version|Get the package version (1 to 4 for PX1 to PX4 respectively).|
|get_package_version|Get the package version (1 to 8 for PX1 to PX8 respectively).|
|get_module_version|Get the module version.|
|get_database_version|Get the database version.|
|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>|
Expand Down Expand Up @@ -115,5 +115,5 @@ i2p.close()

## Support

Email: [email protected]
Email: [email protected]
URL: [https://www.ip2location.com](https://www.ip2location.com)
2 changes: 1 addition & 1 deletion example.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'ip2proxy_ruby'

# open IP2Proxy BIN database for proxy lookup
i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN.BIN")
i2p = Ip2proxy.new.open("./data/IP2PROXY-LITE-PX1.BIN")

# get versioning information
print 'Module Version: ' + i2p.get_module_version + "\n"
Expand Down
4 changes: 2 additions & 2 deletions ip2proxy_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = "ip2proxy_ruby"
s.version = "2.0.0"
s.version = "2.1.0"
s.authors = ["ip2location"]
s.email = ["[email protected]"]

Expand Down Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |s|
"spec/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN",
"spec/ip2proxy_ruby_spec.rb",
"spec/spec_helper.rb",
"rb/data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN"
"rb/data/IP2PROXY-LITE-PX1.BIN"
]

if s.respond_to?(:metadata=)
Expand Down
66 changes: 50 additions & 16 deletions lib/ip2proxy_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Ip2proxy
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

VERSION = '2.0.0'
VERSION = '2.1.0'
FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'

Expand Down Expand Up @@ -51,23 +51,28 @@ def get_database_version()

def get_record(ip)
ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
self.ip_version = ipno.ipv4? ? 4 : 6
self.v4 = ipno.ipv4?
self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)

ipnum = ipno.to_i + 0
self.ip_version, ipnum = validateip(ipno)
self.v4 = ip_version == 4 ? true : false
self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
col_length = columns * 4

if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
indexpos = 0
case ip_version
when 4
ipnum1_2 = (ipnum >> 16)
indexpos = ipv4indexbaseaddr + (ipnum1_2 << 3)
indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
realipno = ipnum
# if ipnum reach MAX_IPV4_RANGE
if realipno == 4294967295
ipnum = realipno - 1
end
when 6
ipnum1 = (ipnum / (2**112))
indexpos = ipv6indexbaseaddr + (ipnum1 << 3)
indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
realipno = ipnum
# if ipnum reach MAX_IPV6_RANGE
if realipno == 340282366920938463463374607431768211455
ipnum = realipno - 1
end
end
low = read32(indexpos)
high = read32(indexpos + 4)
Expand Down Expand Up @@ -277,7 +282,6 @@ def get_all(ip)
asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED

if self.db_index == 1
isproxy = (rec.country_short == '-') ? 0 : 1
else
Expand Down Expand Up @@ -311,7 +315,6 @@ def get_all(ip)
last_seen = INVALID_IP_ADDRESS
isproxy = -1
end

results = {}
results['is_proxy'] = isproxy
results['proxy_type'] = proxytype
Expand All @@ -325,7 +328,6 @@ def get_all(ip)
results['asn'] = asn
results['as'] = as
results['last_seen'] = last_seen

return results
end

Expand All @@ -348,7 +350,7 @@ def bsearch(low, high, ipnum, base_addr, col_length)
low = mid + 1
end
end
end
end
end

def get_from_to(mid, base_addr, col_length)
Expand All @@ -360,6 +362,38 @@ def get_from_to(mid, base_addr, col_length)
[ip_from, ip_to]
end

def validateip(ip)
if ip.ipv4?
ipv = 4
ipnum = ip.to_i + 0
else
ipv = 6
ipnum = ip.to_i + 0
#reformat ipv4 address in ipv6
if ipnum >= 281470681743360 && ipnum <= 281474976710655
ipv = 4
ipnum = ipnum - 281470681743360
end
#reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
ipv = 4
#bitshift right 80 bits
ipnum = ipnum >> 80
#bitwise modulus to get the last 32 bit
ipnum = ipnum % 4294967296
end
#reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
ipv = 4
#bitwise not to invert binary
ipnum = ~ipnum
#bitwise modulus to get the last 32 bit
ipnum = ipnum % 4294967296
end
end
[ipv, ipnum]
end

def read32(indexp)
file.seek(indexp - 1)
return file.read(4).unpack('V').first
Expand Down
Binary file not shown.
Binary file added rb/data/IP2PROXY-LITE-PX1.BIN
Binary file not shown.

0 comments on commit 7f27228

Please sign in to comment.