ip2proxy_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c892658fc2d74fec8e6452e2b24ff01a09213c8
4
+ data.tar.gz: ebfb559012c9cb960af43be15a32f9a4c8db8c8b
5
+ SHA512:
6
+ metadata.gz: 63ac078d172c87f5af0ab76c49891ae8628875da2e6cad969b3519534592ce0127e9463c0e507a673b496519398ead88ae0c7101146196c47e07498b3f78c242
7
+ data.tar.gz: 20c9df73a1191f037c72dafe5ca07119cb5faa7425c2b4bfd08c932cc5432cc723ce249b708a9d6c7696aed6b891e19ea0f9194d790cd36b587ea3a0f67f1bb5
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ip2proxy_ruby.gemspec
4
+ gemspec
5
+ gem 'bindata'
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ip2proxy_ruby (1.0.0)
5
+ bindata
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ awesome_print (1.7.0)
11
+ bindata (2.3.4)
12
+ diff-lcs (1.1.3)
13
+ docile (1.1.5)
14
+ json (1.8.3)
15
+ rdoc (3.12.2)
16
+ json (~> 1.4)
17
+ rspec (2.8.0)
18
+ rspec-core (~> 2.8.0)
19
+ rspec-expectations (~> 2.8.0)
20
+ rspec-mocks (~> 2.8.0)
21
+ rspec-core (2.8.0)
22
+ rspec-expectations (2.8.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.8.0)
25
+ simplecov (0.12.0)
26
+ docile (~> 1.1.0)
27
+ json (>= 1.8, < 3)
28
+ simplecov-html (~> 0.10.0)
29
+ simplecov-html (0.10.0)
30
+
31
+ PLATFORMS
32
+ x64-mingw32
33
+
34
+ DEPENDENCIES
35
+ awesome_print
36
+ bindata
37
+ bundler (>= 1.2.0)
38
+ ip2proxy_ruby!
39
+ rdoc (~> 3.12)
40
+ rspec (~> 2.8.0)
41
+ simplecov
42
+
43
+ BUNDLED WITH
44
+ 1.13.6
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 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 ADDED
@@ -0,0 +1,47 @@
1
+ # IP2Proxy Ruby Library
2
+
3
+ This is IP2Proxy Ruby library that lookup IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes and data center ranges. The library reads the geo location information from [IP2Proxy database](https://www.ip2location.com/proxy-database)
4
+
5
+ For more details, please visit:
6
+ [https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
7
+
8
+ ## Usage
9
+
10
+ ```
11
+ require 'ip2proxy_ruby'
12
+
13
+ # open IP2Proxy BIN database for proxy lookup
14
+ i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN")
15
+
16
+ # get versioning information
17
+ print 'Module Version: ' + i2p.getModuleVersion + "\n"
18
+ print 'Package Version: ' + i2p.getPackageVersion + "\n"
19
+ print 'Database Version: ' + i2p.getDatabaseVersion + "\n"
20
+
21
+ # individual proxy data check
22
+ print 'Is Proxy: ' + i2p.isProxy('4.0.0.47').to_s + "\n"
23
+ print 'Proxy Type: ' + i2p.getProxyType('4.0.0.47') + "\n"
24
+ print 'Country Code: ' + i2p.getCountryShort('4.0.0.47') + "\n"
25
+ print 'Country Name: ' + i2p.getCountryLong('4.0.0.47') + "\n"
26
+ print 'Region Name: ' + i2p.getRegion('4.0.0.47') + "\n"
27
+ print 'City Name: ' + i2p.getCity('4.0.0.47') + "\n"
28
+ print 'ISP: ' + i2p.getISP('4.0.0.47') + "\n"
29
+
30
+ # single function to get all proxy data returned in array
31
+ record = i2p.getAll('4.0.0.47')
32
+ print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
33
+ print 'Proxy Type: ' + record['proxy_type'] + "\n"
34
+ print 'Country Code: ' + record['country_short'] + "\n"
35
+ print 'Country Name: ' + record['country_long'] + "\n"
36
+ print 'Region Name: ' + record['region'] + "\n"
37
+ print 'City Name: ' + record['city'] + "\n"
38
+ print 'ISP: ' + record['isp'] + "\n"
39
+
40
+ # close IP2Proxy BIN database
41
+ i2p.close()
42
+ ```
43
+
44
+ ## Support
45
+
46
+ Email: support@ip2location.com
47
+ URL: [http://www.ip2location.com](http://www.ip2location.com)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/example.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'ip2proxy_ruby'
2
+
3
+ # open IP2Proxy BIN database for proxy lookup
4
+ i2p = Ip2proxy.new.open("./data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN")
5
+
6
+ # get versioning information
7
+ print 'Module Version: ' + i2p.getModuleVersion + "\n"
8
+ print 'Package Version: ' + i2p.getPackageVersion + "\n"
9
+ print 'Database Version: ' + i2p.getDatabaseVersion + "\n"
10
+
11
+ # individual proxy data check
12
+ print 'Is Proxy: ' + i2p.isProxy('4.0.0.47').to_s + "\n"
13
+ print 'Proxy Type: ' + i2p.getProxyType('4.0.0.47') + "\n"
14
+ print 'Country Code: ' + i2p.getCountryShort('4.0.0.47') + "\n"
15
+ print 'Country Name: ' + i2p.getCountryLong('4.0.0.47') + "\n"
16
+ print 'Region Name: ' + i2p.getRegion('4.0.0.47') + "\n"
17
+ print 'City Name: ' + i2p.getCity('4.0.0.47') + "\n"
18
+ print 'ISP: ' + i2p.getISP('4.0.0.47') + "\n"
19
+
20
+ # single function to get all proxy data returned in array
21
+ record = i2p.getAll('4.0.0.47')
22
+ print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
23
+ print 'Proxy Type: ' + record['proxy_type'] + "\n"
24
+ print 'Country Code: ' + record['country_short'] + "\n"
25
+ print 'Country Name: ' + record['country_long'] + "\n"
26
+ print 'Region Name: ' + record['region'] + "\n"
27
+ print 'City Name: ' + record['city'] + "\n"
28
+ print 'ISP: ' + record['isp'] + "\n"
29
+
30
+ # close IP2Proxy BIN database
31
+ i2p.close()
@@ -0,0 +1,67 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "ip2proxy_ruby"
5
+ s.version = "1.0.0"
6
+ s.authors = ["ip2location"]
7
+ s.email = ["support@ip2location.com"]
8
+
9
+ s.summary = "IP2Proxy Ruby library"
10
+ s.description = "Ruby library for IP2Proxy"
11
+ s.homepage = "https://github.com/ip2location/ip2proxy-ruby"
12
+ s.licenses = ["MIT"]
13
+ s.require_paths = ["lib"]
14
+
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "ip2proxy_ruby.gemspec",
28
+ "example.rb",
29
+ "lib/ip2proxy_ruby.rb",
30
+ "lib/ip2proxy_ruby/database_config.rb",
31
+ "lib/ip2proxy_ruby/i2p_ip_data.rb",
32
+ "lib/ip2proxy_ruby/i2p_string_data.rb",
33
+ "lib/ip2proxy_ruby/ip2proxy_config.rb",
34
+ "lib/ip2proxy_ruby/ip2proxy_record.rb",
35
+ "spec/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN",
36
+ "spec/ip2proxy_ruby_spec.rb",
37
+ "spec/spec_helper.rb",
38
+ "rb/data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 4
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<bindata>, [">= 0"])
46
+ s.add_development_dependency(%q<awesome_print>, [">= 0"])
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
48
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_development_dependency(%q<bundler>, [">= 1.2.0"])
50
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<bindata>, [">= 0"])
53
+ s.add_dependency(%q<awesome_print>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_dependency(%q<bundler>, [">= 1.2.0"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<bindata>, [">= 0"])
61
+ s.add_dependency(%q<awesome_print>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
63
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
64
+ s.add_dependency(%q<bundler>, [">= 1.2.0"])
65
+ s.add_dependency(%q<simplecov>, [">= 0"])
66
+ end
67
+ end
@@ -0,0 +1,280 @@
1
+ require 'bindata'
2
+ require 'ipaddr'
3
+ require_relative 'ip2proxy_ruby/ip2proxy_config'
4
+ require_relative 'ip2proxy_ruby/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 = '1.0.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 = DbConfig.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 getModuleVersion()
41
+ return VERSION
42
+ end
43
+
44
+ def getPackageVersion()
45
+ return (self.db_index).to_s
46
+ end
47
+
48
+ def getDatabaseVersion()
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 = ipno.ipv4? ? 4 : 6
55
+ self.v4 = ipno.ipv4?
56
+ self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
57
+ self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
58
+
59
+ ipnum = ipno.to_i + 0
60
+ col_length = columns * 4
61
+
62
+ if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
63
+ indexpos = 0
64
+ case ip_version
65
+ when 4
66
+ ipnum1_2 = (ipnum >> 16)
67
+ indexpos = ipv4indexbaseaddr + (ipnum1_2 << 3)
68
+ when 6
69
+ ipnum1 = (ipnum / (2**112))
70
+ indexpos = ipv6indexbaseaddr + (ipnum1 << 3)
71
+ end
72
+ low = read32(indexpos)
73
+ high = read32(indexpos + 4)
74
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
75
+ else
76
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
77
+ end
78
+ end
79
+
80
+ def getCountryShort(ip)
81
+ valid = !(IPAddr.new(ip) rescue nil).nil?
82
+ if valid
83
+ rec = get_record(ip)
84
+ if !(rec.nil?)
85
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
86
+ else
87
+ country_short = INVALID_IP_ADDRESS
88
+ end
89
+ else
90
+ country_short = INVALID_IP_ADDRESS
91
+ end
92
+ return country_short
93
+ end
94
+
95
+ def getCountryLong(ip)
96
+ valid = !(IPAddr.new(ip) rescue nil).nil?
97
+ if valid
98
+ rec = get_record(ip)
99
+ if !(rec.nil?)
100
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
101
+ else
102
+ country_long = INVALID_IP_ADDRESS
103
+ end
104
+ else
105
+ country_long = INVALID_IP_ADDRESS
106
+ end
107
+ return country_long
108
+ end
109
+
110
+ def getRegion(ip)
111
+ valid = !(IPAddr.new(ip) rescue nil).nil?
112
+ if valid
113
+ rec = get_record(ip)
114
+ if !(rec.nil?)
115
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
116
+ else
117
+ region = INVALID_IP_ADDRESS
118
+ end
119
+ else
120
+ region = INVALID_IP_ADDRESS
121
+ end
122
+ return region
123
+ end
124
+
125
+ def getCity(ip)
126
+ valid = !(IPAddr.new(ip) rescue nil).nil?
127
+ if valid
128
+ rec = get_record(ip)
129
+ if !(rec.nil?)
130
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
131
+ else
132
+ city = INVALID_IP_ADDRESS
133
+ end
134
+ else
135
+ city = INVALID_IP_ADDRESS
136
+ end
137
+ return city
138
+ end
139
+
140
+ def getISP(ip)
141
+ valid = !(IPAddr.new(ip) rescue nil).nil?
142
+ if valid
143
+ rec = get_record(ip)
144
+ if !(rec.nil?)
145
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
146
+ else
147
+ isp = INVALID_IP_ADDRESS
148
+ end
149
+ else
150
+ isp = INVALID_IP_ADDRESS
151
+ end
152
+ return isp
153
+ end
154
+
155
+ def getProxyType(ip)
156
+ valid = !(IPAddr.new(ip) rescue nil).nil?
157
+ if valid
158
+ rec = get_record(ip)
159
+ if !(rec.nil?)
160
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
161
+ else
162
+ proxytype = INVALID_IP_ADDRESS
163
+ end
164
+ else
165
+ proxytype = INVALID_IP_ADDRESS
166
+ end
167
+ return proxytype
168
+ end
169
+
170
+ def isProxy(ip)
171
+ valid = !(IPAddr.new(ip) rescue nil).nil?
172
+ if valid
173
+ rec = get_record(ip)
174
+ if !(rec.nil?)
175
+ if self.db_index == 1
176
+ isproxy = (rec.country_short == '-') ? 0 : 1
177
+ else
178
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH') ? 2 : 1
179
+ end
180
+ else
181
+ isproxy = -1
182
+ end
183
+ else
184
+ isproxy = -1
185
+ end
186
+ return isproxy
187
+ end
188
+
189
+ def getAll(ip)
190
+ valid = !(IPAddr.new(ip) rescue nil).nil?
191
+ if valid
192
+ rec = get_record(ip)
193
+ if !(rec.nil?)
194
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
195
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
196
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
197
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
198
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
199
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
200
+
201
+ if self.db_index == 1
202
+ isproxy = (rec.country_short == '-') ? 0 : 1
203
+ else
204
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH') ? 2 : 1
205
+ end
206
+ else
207
+ country_short = INVALID_IP_ADDRESS
208
+ country_long = INVALID_IP_ADDRESS
209
+ region = INVALID_IP_ADDRESS
210
+ city = INVALID_IP_ADDRESS
211
+ isp = INVALID_IP_ADDRESS
212
+ proxytype = INVALID_IP_ADDRESS
213
+ isproxy = -1
214
+ end
215
+ else
216
+ country_short = INVALID_IP_ADDRESS
217
+ country_long = INVALID_IP_ADDRESS
218
+ region = INVALID_IP_ADDRESS
219
+ city = INVALID_IP_ADDRESS
220
+ isp = INVALID_IP_ADDRESS
221
+ proxytype = INVALID_IP_ADDRESS
222
+ isproxy = -1
223
+ end
224
+
225
+ results = {}
226
+ results['is_proxy'] = isproxy
227
+ results['proxy_type'] = proxytype
228
+ results['country_short'] = country_short
229
+ results['country_long'] = country_long
230
+ results['region'] = region
231
+ results['city'] = city
232
+ results['isp'] = isp
233
+
234
+ return results
235
+ end
236
+
237
+ def bsearch(low, high, ipnum, base_addr, col_length)
238
+ while low <= high do
239
+ mid = (low + high) >> 1
240
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
241
+ if ipnum >= ip_from && ipnum < ip_to
242
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
243
+ file.seek(from_base)
244
+ if v4
245
+ return self.record_class4.read(file)
246
+ else
247
+ return self.record_class6.read(file)
248
+ end
249
+ else
250
+ if ipnum < ip_from
251
+ high = mid - 1
252
+ else
253
+ low = mid + 1
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ def get_from_to(mid, base_addr, col_length)
260
+ from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
261
+ file.seek(from_base)
262
+ ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
263
+ file.seek(from_base + col_length + (v4 ? 0 : 12))
264
+ ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
265
+ [ip_from, ip_to]
266
+ end
267
+
268
+ def read32(indexp)
269
+ file.seek(indexp - 1)
270
+ return file.read(4).unpack('V').first
271
+ end
272
+
273
+ def readipv6(filer)
274
+ parts = filer.read(16).unpack('V*')
275
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
276
+ end
277
+
278
+ private :get_record, :bsearch, :get_from_to, :read32, :readipv6
279
+
280
+ end
@@ -0,0 +1,21 @@
1
+ class DbConfig
2
+ COLUMNS = {
3
+ :COUNTRY => [0, 2, 3, 3, 3],
4
+ :REGION => [0, 0, 0, 4, 4],
5
+ :CITY => [0, 0, 0, 5, 5],
6
+ :ISP => [0, 0, 0, 0, 6],
7
+ :PROXYTYPE => [0, 0, 2, 2, 2]
8
+ }
9
+
10
+ def self.setup_database(db_index)
11
+ # strip all 0 value & downcase keys
12
+ cols = COLUMNS.inject({}) {|memo, (key, value)|
13
+ (memo[key.to_s.downcase.to_sym] = value[db_index] if value[db_index] > 0)
14
+ memo
15
+ }
16
+ # order by value
17
+ col_array = cols.sort_by {|key,value| value}
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,10 @@
1
+ class I2pIpData < BinData::BasePrimitive
2
+ def read_and_return_value(io)
3
+ iv = eval_parameter(:ip_version)
4
+ if iv == 4
5
+ addr = BinData::Uint32le.read(io)
6
+ elsif iv == 6
7
+ addr = BinData::Uint128le.read(io)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ class I2pStringData < BinData::BasePrimitive
2
+
3
+ def read_and_return_value(io)
4
+ country_long = eval_parameter(:country_long)
5
+ io.seekbytes(-4) if country_long
6
+ file = io.instance_variable_get('@raw_io')
7
+ addr = BinData::Uint32le.read(io)
8
+ old_offset = file.tell
9
+ country_long ? file.seek(addr + 3) : file.seek(addr)
10
+ length = BinData::Uint8.read(file)
11
+ res = BinData::String.new(:length => length).read(file)
12
+ file.seek(old_offset)
13
+ res
14
+ end
15
+
16
+ end
@@ -0,0 +1,17 @@
1
+ class Ip2proxyConfig < BinData::Record
2
+ endian :little
3
+ uint8 :databasetype
4
+ uint8 :databasecolumn
5
+ uint8 :databaseday
6
+ uint8 :databasemonth
7
+ uint8 :databaseyear
8
+ # uint32 :databasecount
9
+ # uint32 :databaseaddr
10
+ # uint32 :ipversion
11
+ uint32 :ipv4databasecount
12
+ uint32 :ipv4databaseaddr
13
+ uint32 :ipv6databasecount
14
+ uint32 :ipv6databaseaddr
15
+ uint32 :ipv4indexbaseaddr
16
+ uint32 :ipv6indexbaseaddr
17
+ end
@@ -0,0 +1,20 @@
1
+ class Ip2ProxyRecord
2
+ def self.init(database, ip_version)
3
+ cls = Class.new(BinData::Record)
4
+ cls.class_eval {
5
+ endian :little
6
+ i2p_ip_data :ip_from, :ip_version => ip_version
7
+ database.each do |col|
8
+ if col.first == :country
9
+ i2p_string_data :country_short
10
+ i2p_string_data :country_long, :country_long => true
11
+ else
12
+ i2p_string_data col.first
13
+ end
14
+ end
15
+
16
+ i2p_ip_data :ip_to, :ip_version => ip_version
17
+ }
18
+ cls
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Ip2proxy" do
4
+ it "work correctly with ipv4" do
5
+ i2p = Ip2proxy.new.open(File.dirname(__FILE__) + "/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN")
6
+ record = i2p.getAll('1.0.241.135')
7
+ record.should_not be_nil
8
+ record['is_proxy'].should == 1
9
+ i2p.close()
10
+ end
11
+
12
+ it "work correctly with ipv6" do
13
+ i2p = Ip2proxy.new.open(File.dirname(__FILE__) + "/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN")
14
+ record = i2p.getAll('2001::')
15
+ record.should_not be_nil
16
+ record['is_proxy'].should == 0
17
+ i2p.close()
18
+ end
19
+
20
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ip2proxy_ruby'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+ require 'awesome_print'
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip2proxy_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ip2location
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bindata
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.8.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruby library for IP2Proxy
98
+ email:
99
+ - support@ip2location.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - LICENSE.txt
104
+ - README.md
105
+ files:
106
+ - ".document"
107
+ - ".gitignore"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - example.rb
114
+ - ip2proxy_ruby.gemspec
115
+ - lib/ip2proxy_ruby.rb
116
+ - lib/ip2proxy_ruby/database_config.rb
117
+ - lib/ip2proxy_ruby/i2p_ip_data.rb
118
+ - lib/ip2proxy_ruby/i2p_string_data.rb
119
+ - lib/ip2proxy_ruby/ip2proxy_config.rb
120
+ - lib/ip2proxy_ruby/ip2proxy_record.rb
121
+ - rb/data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN
122
+ - spec/assets/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN
123
+ - spec/ip2proxy_ruby_spec.rb
124
+ - spec/spec_helper.rb
125
+ homepage: https://github.com/ip2location/ip2proxy-ruby
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.6.8
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: IP2Proxy Ruby library
149
+ test_files: []