Class: Ip2proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/ip2proxy_ruby.rb

Constant Summary collapse

VERSION =
'3.1.0'
FIELD_NOT_SUPPORTED =
'NOT SUPPORTED'
INVALID_IP_ADDRESS =
'INVALID IP ADDRESS'
INVALID_BIN_DATABASE =
'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_addrObject

Returns the value of attribute base_addr.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def base_addr
  @base_addr
end

#columnsObject

Returns the value of attribute columns.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def columns
  @columns
end

#countObject

Returns the value of attribute count.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def count
  @count
end

#databaseObject

Returns the value of attribute database.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def database
  @database
end

#databasedayObject

Returns the value of attribute databaseday.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def databaseday
  @databaseday
end

#databasemonthObject

Returns the value of attribute databasemonth.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def databasemonth
  @databasemonth
end

#databaseyearObject

Returns the value of attribute databaseyear.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def databaseyear
  @databaseyear
end

#db_indexObject

Returns the value of attribute db_index.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def db_index
  @db_index
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def file
  @file
end

#ip_versionObject

Returns the value of attribute ip_version.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ip_version
  @ip_version
end

#ipnoObject

Returns the value of attribute ipno.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipno
  @ipno
end

#ipv4databaseaddrObject

Returns the value of attribute ipv4databaseaddr.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv4databaseaddr
  @ipv4databaseaddr
end

#ipv4databasecountObject

Returns the value of attribute ipv4databasecount.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv4databasecount
  @ipv4databasecount
end

#ipv4indexbaseaddrObject

Returns the value of attribute ipv4indexbaseaddr.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv4indexbaseaddr
  @ipv4indexbaseaddr
end

#ipv6databaseaddrObject

Returns the value of attribute ipv6databaseaddr.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv6databaseaddr
  @ipv6databaseaddr
end

#ipv6databasecountObject

Returns the value of attribute ipv6databasecount.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv6databasecount
  @ipv6databasecount
end

#ipv6indexbaseaddrObject

Returns the value of attribute ipv6indexbaseaddr.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def ipv6indexbaseaddr
  @ipv6indexbaseaddr
end

#last_err_msgObject

Returns the value of attribute last_err_msg.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def last_err_msg
  @last_err_msg
end

#recordObject

Returns the value of attribute record.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def record
  @record
end

#record_class4Object

Returns the value of attribute record_class4.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def record_class4
  @record_class4
end

#record_class6Object

Returns the value of attribute record_class6.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def record_class6
  @record_class6
end

#v4Object

Returns the value of attribute v4.



10
11
12
# File 'lib/ip2proxy_ruby.rb', line 10

def v4
  @v4
end

Instance Method Details

#closeObject



57
58
59
# File 'lib/ip2proxy_ruby.rb', line 57

def close()
  self.file.close
end

#get_all(ip) ⇒ Object



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
# File 'lib/ip2proxy_ruby.rb', line 324

def get_all(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
          country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
          region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
          city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
          isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
          proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
          domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
          usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
          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
          threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
          provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
          if self.db_index == 1
              isproxy = (rec.country_short == '-') ? 0 : 1
          else
              isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
          end
      else
          country_short = INVALID_IP_ADDRESS
          country_long = INVALID_IP_ADDRESS
          region = INVALID_IP_ADDRESS
          city = INVALID_IP_ADDRESS
          isp = INVALID_IP_ADDRESS
          proxytype = INVALID_IP_ADDRESS
          domain = INVALID_IP_ADDRESS
          usagetype = INVALID_IP_ADDRESS
          asn = INVALID_IP_ADDRESS
          as = INVALID_IP_ADDRESS
          last_seen = INVALID_IP_ADDRESS
          threat = INVALID_IP_ADDRESS
          provider = INVALID_IP_ADDRESS
          isproxy = -1
      end
  else
      country_short = INVALID_IP_ADDRESS
      country_long = INVALID_IP_ADDRESS
      region = INVALID_IP_ADDRESS
      city = INVALID_IP_ADDRESS
      isp = INVALID_IP_ADDRESS
      proxytype = INVALID_IP_ADDRESS
      domain = INVALID_IP_ADDRESS
      usagetype = INVALID_IP_ADDRESS
      asn = INVALID_IP_ADDRESS
      as = INVALID_IP_ADDRESS
      last_seen = INVALID_IP_ADDRESS
      threat = INVALID_IP_ADDRESS
      provider = INVALID_IP_ADDRESS
      isproxy = -1
  end
  results = {}
  results['is_proxy'] = isproxy
  results['proxy_type'] = proxytype
  results['country_short'] = country_short
  results['country_long'] = country_long
  results['region'] = region
  results['city'] = city
  results['isp'] = isp
  results['domain'] = domain
  results['usagetype'] = usagetype
  results['asn'] = asn
  results['as'] = as
  results['last_seen'] = last_seen
  results['threat'] = threat
  results['provider'] = provider
  return results
end

#get_as(ip) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/ip2proxy_ruby.rb', line 245

def get_as(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
      else
          as = INVALID_IP_ADDRESS
      end
  else
      as = INVALID_IP_ADDRESS
  end
  return as
end

#get_asn(ip) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ip2proxy_ruby.rb', line 230

def get_asn(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
      else
          asn = INVALID_IP_ADDRESS
      end
  else
      asn = INVALID_IP_ADDRESS
  end
  return asn
end

#get_city(ip) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ip2proxy_ruby.rb', line 155

def get_city(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
      else
          city = INVALID_IP_ADDRESS
      end
  else
      city = INVALID_IP_ADDRESS
  end
  return city
end

#get_country_long(ip) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ip2proxy_ruby.rb', line 125

def get_country_long(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
      else
          country_long = INVALID_IP_ADDRESS
      end
  else
      country_long = INVALID_IP_ADDRESS
  end
  return country_long
end

#get_country_short(ip) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ip2proxy_ruby.rb', line 110

def get_country_short(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
      else
          country_short = INVALID_IP_ADDRESS
      end
  else
      country_short = INVALID_IP_ADDRESS
  end
  return country_short
end

#get_database_versionObject



73
74
75
# File 'lib/ip2proxy_ruby.rb', line 73

def get_database_version()
  return (self.databaseyear).to_s + "." + (self.databasemonth).to_s + "." + (self.databaseday).to_s
end

#get_domain(ip) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ip2proxy_ruby.rb', line 200

def get_domain(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
      else
          domain = INVALID_IP_ADDRESS
      end
  else
      domain = INVALID_IP_ADDRESS
  end
  return domain
end

#get_isp(ip) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ip2proxy_ruby.rb', line 170

def get_isp(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
      else
          isp = INVALID_IP_ADDRESS
      end
  else
      isp = INVALID_IP_ADDRESS
  end
  return isp
end

#get_last_error_messageObject



61
62
63
# File 'lib/ip2proxy_ruby.rb', line 61

def get_last_error_message()
  return self.last_err_msg
end

#get_last_seen(ip) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/ip2proxy_ruby.rb', line 260

def get_last_seen(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
      else
          last_seen = INVALID_IP_ADDRESS
      end
  else
      last_seen = INVALID_IP_ADDRESS
  end
  return last_seen
end

#get_module_versionObject



65
66
67
# File 'lib/ip2proxy_ruby.rb', line 65

def get_module_version()
  return VERSION
end

#get_package_versionObject



69
70
71
# File 'lib/ip2proxy_ruby.rb', line 69

def get_package_version()
  return (self.db_index).to_s
end

#get_provider(ip) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/ip2proxy_ruby.rb', line 309

def get_provider(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
      else
          provider = INVALID_IP_ADDRESS
      end
  else
      provider = INVALID_IP_ADDRESS
  end
  return provider
end

#get_proxytype(ip) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ip2proxy_ruby.rb', line 185

def get_proxytype(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
      else
          proxytype = INVALID_IP_ADDRESS
      end
  else
      proxytype = INVALID_IP_ADDRESS
  end
  return proxytype
end

#get_region(ip) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ip2proxy_ruby.rb', line 140

def get_region(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
      else
          region = INVALID_IP_ADDRESS
      end
  else
      region = INVALID_IP_ADDRESS
  end
  return region
end

#get_threat(ip) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ip2proxy_ruby.rb', line 275

def get_threat(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
      else
          threat = INVALID_IP_ADDRESS
      end
  else
      threat = INVALID_IP_ADDRESS
  end
  return threat
end

#get_usagetype(ip) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ip2proxy_ruby.rb', line 215

def get_usagetype(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
      else
          usagetype = INVALID_IP_ADDRESS
      end
  else
      usagetype = INVALID_IP_ADDRESS
  end
  return usagetype
end

#is_proxy(ip) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ip2proxy_ruby.rb', line 290

def is_proxy(ip)
  valid = !(IPAddr.new(ip) rescue nil).nil?
  if valid
      rec = get_record(ip)
      if !(rec.nil?)
          if self.db_index == 1
              isproxy = (rec.country_short == '-') ? 0 : 1
          else
              isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
          end
      else
          isproxy = -1
      end
  else
      isproxy = -1
  end
  return isproxy
end

#open(url) ⇒ Object



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
55
# File 'lib/ip2proxy_ruby.rb', line 17

def open(url)
  if url == ''
      self.last_err_msg = 'Ip2proxy.new.open() requires a database path name.'
      abort('Ip2proxy.new.open() requires a database path name.')
  end

  begin
      self.file = File.open(File.expand_path url, 'rb')
  rescue
      self.last_err_msg = 'Ip2proxy.new.open() error in opening ' + url +'.'
      abort('Ip2proxy.new.open() error in opening ' + url +'.')
  else
  end
  i2p = Ip2proxyConfig.read(file)
  if i2p.productcode == 2
  else
      if i2p.databaseyear <= 20 && i2p.productcode == 0
      else
          self.file.close
          self.last_err_msg = INVALID_BIN_DATABASE
          abort(INVALID_BIN_DATABASE)
      end
  end
  self.db_index = i2p.databasetype
  self.columns = i2p.databasecolumn + 0
  self.databaseyear = 2000 + i2p.databaseyear
  self.databasemonth = i2p.databasemonth
  self.databaseday = i2p.databaseday
  self.database = I2pDbConfig.setup_database(self.db_index)
  self.ipv4databasecount = i2p.ipv4databasecount
  self.ipv4databaseaddr = i2p.ipv4databaseaddr
  self.ipv6databasecount = i2p.ipv6databasecount
  self.ipv6databaseaddr = i2p.ipv6databaseaddr
  self.ipv4indexbaseaddr = i2p.ipv4indexbaseaddr
  self.ipv6indexbaseaddr = i2p.ipv6indexbaseaddr
  self.record_class4 = (Ip2ProxyRecord.init database, 4)
  self.record_class6 = (Ip2ProxyRecord.init database, 6)
  self
end

#validateip(ip) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/ip2proxy_ruby.rb', line 428

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