ip2proxy_ruby 3.0.0 → 3.2.1
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 -131
- data/example.rb +52 -43
- data/ip2proxy_ruby.gemspec +8 -14
- data/lib/ip2proxy_ruby/i2p_database_config.rb +27 -26
- 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/lib/ip2proxy_ruby.rb +524 -427
- 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 -39
- 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
data/lib/ip2proxy_ruby.rb
CHANGED
@@ -1,428 +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
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
end
|
420
|
-
|
421
|
-
def
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
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.1'
|
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 + '. No such file in the /your_ip2proxy_ruby_library_path/rb/ folder.')
|
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
|
428
525
|
end
|