ip2proxy_ruby 1.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,280 +1,428 @@
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 = '1.0.2'
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 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
-
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
+
280
428
  end