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.
- checksums.yaml +5 -5
- data/.document +5 -5
- data/.gitignore +9 -9
- data/Gemfile +5 -5
- data/LICENSE.txt +20 -20
- data/README.md +131 -47
- data/Rakefile +2 -2
- data/example.rb +24 -12
- data/ip2proxy_ruby.gemspec +74 -67
- data/lib/ip2proxy_ruby.rb +427 -279
- data/lib/ip2proxy_ruby/i2p_database_config.rb +26 -20
- 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 +16 -16
- data/lib/ip2proxy_ruby/ip2proxy_record.rb +19 -19
- data/rb/data/IP2PROXY-LITE-PX1.BIN +0 -0
- data/spec/ip2proxy_ruby_spec.rb +20 -20
- data/spec/spec_helper.rb +12 -12
- metadata +13 -7
- data/rb/data/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN +0 -0
data/lib/ip2proxy_ruby.rb
CHANGED
@@ -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 = '
|
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
|
41
|
-
return VERSION
|
42
|
-
end
|
43
|
-
|
44
|
-
def
|
45
|
-
return (self.db_index).to_s
|
46
|
-
end
|
47
|
-
|
48
|
-
def
|
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
|
55
|
-
self.v4 =
|
56
|
-
self.count =
|
57
|
-
self.base_addr = (
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
indexpos = ipv6indexbaseaddr + (
|
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
|
-
|
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
|