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.
data/lib/ip2proxy_ruby.rb CHANGED
@@ -1,428 +1,525 @@
1
- require 'bindata'
2
- require 'ipaddr'
3
- require_relative 'ip2proxy_ruby/ip2proxy_config'
4
- require_relative 'ip2proxy_ruby/i2p_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 = '3.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 = I2pDbConfig.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 get_module_version()
41
- return VERSION
42
- end
43
-
44
- def get_package_version()
45
- return (self.db_index).to_s
46
- end
47
-
48
- def get_database_version()
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, ipnum = validateip(ipno)
55
- self.v4 = ip_version == 4 ? true : false
56
- self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
57
- self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
58
- col_length = columns * 4
59
- if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
60
- indexpos = 0
61
- case ip_version
62
- when 4
63
- indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
64
- realipno = ipnum
65
- # if ipnum reach MAX_IPV4_RANGE
66
- if realipno == 4294967295
67
- ipnum = realipno - 1
68
- end
69
- when 6
70
- indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
71
- realipno = ipnum
72
- # if ipnum reach MAX_IPV6_RANGE
73
- if realipno == 340282366920938463463374607431768211455
74
- ipnum = realipno - 1
75
- end
76
- end
77
- low = read32(indexpos)
78
- high = read32(indexpos + 4)
79
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
80
- else
81
- return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
82
- end
83
- end
84
-
85
- def get_country_short(ip)
86
- valid = !(IPAddr.new(ip) rescue nil).nil?
87
- if valid
88
- rec = get_record(ip)
89
- if !(rec.nil?)
90
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
91
- else
92
- country_short = INVALID_IP_ADDRESS
93
- end
94
- else
95
- country_short = INVALID_IP_ADDRESS
96
- end
97
- return country_short
98
- end
99
-
100
- def get_country_long(ip)
101
- valid = !(IPAddr.new(ip) rescue nil).nil?
102
- if valid
103
- rec = get_record(ip)
104
- if !(rec.nil?)
105
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
106
- else
107
- country_long = INVALID_IP_ADDRESS
108
- end
109
- else
110
- country_long = INVALID_IP_ADDRESS
111
- end
112
- return country_long
113
- end
114
-
115
- def get_region(ip)
116
- valid = !(IPAddr.new(ip) rescue nil).nil?
117
- if valid
118
- rec = get_record(ip)
119
- if !(rec.nil?)
120
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
121
- else
122
- region = INVALID_IP_ADDRESS
123
- end
124
- else
125
- region = INVALID_IP_ADDRESS
126
- end
127
- return region
128
- end
129
-
130
- def get_city(ip)
131
- valid = !(IPAddr.new(ip) rescue nil).nil?
132
- if valid
133
- rec = get_record(ip)
134
- if !(rec.nil?)
135
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
136
- else
137
- city = INVALID_IP_ADDRESS
138
- end
139
- else
140
- city = INVALID_IP_ADDRESS
141
- end
142
- return city
143
- end
144
-
145
- def get_isp(ip)
146
- valid = !(IPAddr.new(ip) rescue nil).nil?
147
- if valid
148
- rec = get_record(ip)
149
- if !(rec.nil?)
150
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
151
- else
152
- isp = INVALID_IP_ADDRESS
153
- end
154
- else
155
- isp = INVALID_IP_ADDRESS
156
- end
157
- return isp
158
- end
159
-
160
- def get_proxytype(ip)
161
- valid = !(IPAddr.new(ip) rescue nil).nil?
162
- if valid
163
- rec = get_record(ip)
164
- if !(rec.nil?)
165
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
166
- else
167
- proxytype = INVALID_IP_ADDRESS
168
- end
169
- else
170
- proxytype = INVALID_IP_ADDRESS
171
- end
172
- return proxytype
173
- end
174
-
175
- def get_domain(ip)
176
- valid = !(IPAddr.new(ip) rescue nil).nil?
177
- if valid
178
- rec = get_record(ip)
179
- if !(rec.nil?)
180
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
181
- else
182
- domain = INVALID_IP_ADDRESS
183
- end
184
- else
185
- domain = INVALID_IP_ADDRESS
186
- end
187
- return domain
188
- end
189
-
190
- def get_usagetype(ip)
191
- valid = !(IPAddr.new(ip) rescue nil).nil?
192
- if valid
193
- rec = get_record(ip)
194
- if !(rec.nil?)
195
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
196
- else
197
- usagetype = INVALID_IP_ADDRESS
198
- end
199
- else
200
- usagetype = INVALID_IP_ADDRESS
201
- end
202
- return usagetype
203
- end
204
-
205
- def get_asn(ip)
206
- valid = !(IPAddr.new(ip) rescue nil).nil?
207
- if valid
208
- rec = get_record(ip)
209
- if !(rec.nil?)
210
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
211
- else
212
- asn = INVALID_IP_ADDRESS
213
- end
214
- else
215
- asn = INVALID_IP_ADDRESS
216
- end
217
- return asn
218
- end
219
-
220
- def get_as(ip)
221
- valid = !(IPAddr.new(ip) rescue nil).nil?
222
- if valid
223
- rec = get_record(ip)
224
- if !(rec.nil?)
225
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
226
- else
227
- as = INVALID_IP_ADDRESS
228
- end
229
- else
230
- as = INVALID_IP_ADDRESS
231
- end
232
- return as
233
- end
234
-
235
- def get_last_seen(ip)
236
- valid = !(IPAddr.new(ip) rescue nil).nil?
237
- if valid
238
- rec = get_record(ip)
239
- if !(rec.nil?)
240
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
241
- else
242
- last_seen = INVALID_IP_ADDRESS
243
- end
244
- else
245
- last_seen = INVALID_IP_ADDRESS
246
- end
247
- return last_seen
248
- end
249
-
250
- def get_threat(ip)
251
- valid = !(IPAddr.new(ip) rescue nil).nil?
252
- if valid
253
- rec = get_record(ip)
254
- if !(rec.nil?)
255
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
256
- else
257
- threat = INVALID_IP_ADDRESS
258
- end
259
- else
260
- threat = INVALID_IP_ADDRESS
261
- end
262
- return threat
263
- end
264
-
265
- def is_proxy(ip)
266
- valid = !(IPAddr.new(ip) rescue nil).nil?
267
- if valid
268
- rec = get_record(ip)
269
- if !(rec.nil?)
270
- if self.db_index == 1
271
- isproxy = (rec.country_short == '-') ? 0 : 1
272
- else
273
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
274
- end
275
- else
276
- isproxy = -1
277
- end
278
- else
279
- isproxy = -1
280
- end
281
- return isproxy
282
- end
283
-
284
- def get_all(ip)
285
- valid = !(IPAddr.new(ip) rescue nil).nil?
286
- if valid
287
- rec = get_record(ip)
288
- if !(rec.nil?)
289
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
290
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
291
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
292
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
293
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
294
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
295
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
296
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
297
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
298
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
299
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
300
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
301
- if self.db_index == 1
302
- isproxy = (rec.country_short == '-') ? 0 : 1
303
- else
304
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
305
- end
306
- else
307
- country_short = INVALID_IP_ADDRESS
308
- country_long = INVALID_IP_ADDRESS
309
- region = INVALID_IP_ADDRESS
310
- city = INVALID_IP_ADDRESS
311
- isp = INVALID_IP_ADDRESS
312
- proxytype = INVALID_IP_ADDRESS
313
- domain = INVALID_IP_ADDRESS
314
- usagetype = INVALID_IP_ADDRESS
315
- asn = INVALID_IP_ADDRESS
316
- as = INVALID_IP_ADDRESS
317
- last_seen = INVALID_IP_ADDRESS
318
- threat = INVALID_IP_ADDRESS
319
- isproxy = -1
320
- end
321
- else
322
- country_short = INVALID_IP_ADDRESS
323
- country_long = INVALID_IP_ADDRESS
324
- region = INVALID_IP_ADDRESS
325
- city = INVALID_IP_ADDRESS
326
- isp = INVALID_IP_ADDRESS
327
- proxytype = INVALID_IP_ADDRESS
328
- domain = INVALID_IP_ADDRESS
329
- usagetype = INVALID_IP_ADDRESS
330
- asn = INVALID_IP_ADDRESS
331
- as = INVALID_IP_ADDRESS
332
- last_seen = INVALID_IP_ADDRESS
333
- threat = INVALID_IP_ADDRESS
334
- isproxy = -1
335
- end
336
- results = {}
337
- results['is_proxy'] = isproxy
338
- results['proxy_type'] = proxytype
339
- results['country_short'] = country_short
340
- results['country_long'] = country_long
341
- results['region'] = region
342
- results['city'] = city
343
- results['isp'] = isp
344
- results['domain'] = domain
345
- results['usagetype'] = usagetype
346
- results['asn'] = asn
347
- results['as'] = as
348
- results['last_seen'] = last_seen
349
- results['threat'] = threat
350
- return results
351
- end
352
-
353
- def bsearch(low, high, ipnum, base_addr, col_length)
354
- while low <= high do
355
- mid = (low + high) >> 1
356
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
357
- if ipnum >= ip_from && ipnum < ip_to
358
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
359
- file.seek(from_base)
360
- if v4
361
- return self.record_class4.read(file)
362
- else
363
- return self.record_class6.read(file)
364
- end
365
- else
366
- if ipnum < ip_from
367
- high = mid - 1
368
- else
369
- low = mid + 1
370
- end
371
- end
372
- end
373
- end
374
-
375
- def get_from_to(mid, base_addr, col_length)
376
- from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
377
- file.seek(from_base)
378
- ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
379
- file.seek(from_base + col_length + (v4 ? 0 : 12))
380
- ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
381
- [ip_from, ip_to]
382
- end
383
-
384
- def validateip(ip)
385
- if ip.ipv4?
386
- ipv = 4
387
- ipnum = ip.to_i + 0
388
- else
389
- ipv = 6
390
- ipnum = ip.to_i + 0
391
- #reformat ipv4 address in ipv6
392
- if ipnum >= 281470681743360 && ipnum <= 281474976710655
393
- ipv = 4
394
- ipnum = ipnum - 281470681743360
395
- end
396
- #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
397
- if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
398
- ipv = 4
399
- #bitshift right 80 bits
400
- ipnum = ipnum >> 80
401
- #bitwise modulus to get the last 32 bit
402
- ipnum = ipnum % 4294967296
403
- end
404
- #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
405
- if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
406
- ipv = 4
407
- #bitwise not to invert binary
408
- ipnum = ~ipnum
409
- #bitwise modulus to get the last 32 bit
410
- ipnum = ipnum % 4294967296
411
- end
412
- end
413
- [ipv, ipnum]
414
- end
415
-
416
- def read32(indexp)
417
- file.seek(indexp - 1)
418
- return file.read(4).unpack('V').first
419
- end
420
-
421
- def readipv6(filer)
422
- parts = filer.read(16).unpack('V*')
423
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
424
- end
425
-
426
- private :get_record, :bsearch, :get_from_to, :read32, :readipv6
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