Class: Ip2proxy

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

Constant Summary collapse

VERSION =
'3.3.1'
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.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def base_addr
  @base_addr
end

#columnsObject

Returns the value of attribute columns.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def columns
  @columns
end

#countObject

Returns the value of attribute count.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def count
  @count
end

#databaseObject

Returns the value of attribute database.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def database
  @database
end

#databasedayObject

Returns the value of attribute databaseday.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def databaseday
  @databaseday
end

#databasemonthObject

Returns the value of attribute databasemonth.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def databasemonth
  @databasemonth
end

#databaseyearObject

Returns the value of attribute databaseyear.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def databaseyear
  @databaseyear
end

#db_indexObject

Returns the value of attribute db_index.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def db_index
  @db_index
end

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def file
  @file
end

#ip_versionObject

Returns the value of attribute ip_version.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ip_version
  @ip_version
end

#ipnoObject

Returns the value of attribute ipno.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipno
  @ipno
end

#ipv4databaseaddrObject

Returns the value of attribute ipv4databaseaddr.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv4databaseaddr
  @ipv4databaseaddr
end

#ipv4databasecountObject

Returns the value of attribute ipv4databasecount.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv4databasecount
  @ipv4databasecount
end

#ipv4indexbaseaddrObject

Returns the value of attribute ipv4indexbaseaddr.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv4indexbaseaddr
  @ipv4indexbaseaddr
end

#ipv6databaseaddrObject

Returns the value of attribute ipv6databaseaddr.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv6databaseaddr
  @ipv6databaseaddr
end

#ipv6databasecountObject

Returns the value of attribute ipv6databasecount.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv6databasecount
  @ipv6databasecount
end

#ipv6indexbaseaddrObject

Returns the value of attribute ipv6indexbaseaddr.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def ipv6indexbaseaddr
  @ipv6indexbaseaddr
end

#last_err_msgObject

Returns the value of attribute last_err_msg.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def last_err_msg
  @last_err_msg
end

#recordObject

Returns the value of attribute record.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def record
  @record
end

#record_class4Object

Returns the value of attribute record_class4.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def record_class4
  @record_class4
end

#record_class6Object

Returns the value of attribute record_class6.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def record_class6
  @record_class6
end

#v4Object

Returns the value of attribute v4.



12
13
14
# File 'lib/ip2proxy_ruby.rb', line 12

def v4
  @v4
end

Instance Method Details

#closeObject



59
60
61
# File 'lib/ip2proxy_ruby.rb', line 59

def close()
  self.file.close
end

#get_all(ip) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



75
76
77
# File 'lib/ip2proxy_ruby.rb', line 75

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

#get_domain(ip) ⇒ Object



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

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



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

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



63
64
65
# File 'lib/ip2proxy_ruby.rb', line 63

def get_last_error_message()
  return self.last_err_msg
end

#get_last_seen(ip) ⇒ Object



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

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



67
68
69
# File 'lib/ip2proxy_ruby.rb', line 67

def get_module_version()
  return VERSION
end

#get_package_versionObject



71
72
73
# File 'lib/ip2proxy_ruby.rb', line 71

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

#get_provider(ip) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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 + '. No such file in the /your_ip2proxy_ruby_library_path/rb/ folder.')
  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



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

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