ip2proxy_ruby 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e344a1f199333b229ab18e7a461caa60ab70d6a287e44915b27007be9f854970
4
- data.tar.gz: efdd3f09798abc2bd99a7d3a391c607e9436b71ff151c48c4836d7cbe6a7940d
3
+ metadata.gz: 4fcaf2e6a17ab015cd891c8f518ad2fe7494faf1c0392fd53d4f65b91afc0a7c
4
+ data.tar.gz: 2db5fece7ac66539061f5755d91f7a71593fc5fc07aa4028e3e0e924fa946804
5
5
  SHA512:
6
- metadata.gz: dd25f8039cd727ddfb7ea5827fd90e115e89eb240cd15d7cdf33b222d3bb0c059c26d07bbd742a54f9001edde838394eda229ce7b62004fbf8c29dea661c9deb
7
- data.tar.gz: 7fb434d314ecdfda0762400463e372b216086e41c0139ed7ad5014452434580ecf5819e66bb0f0db854c9c2bb5a99828acd1e640f2010cac3496f57a26776452
6
+ metadata.gz: e5eb42c6941106364964185194e927068f762463a7c6e9ab360fc941886b1755bf58c9e2074c0e296769da45e713677e02093d39cba1072bbf64621d02c0abed
7
+ data.tar.gz: 8d4824e9317981ad0049906c3c3d001e04f0ff810ed26a7270829a2ce73056c2838ada560eb6c9c69c969e5cd61027998577bb1588f5daf1719a71570e6bda67
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016 - 2025 IP2Location ( support@ip2location.com )
1
+ Copyright (c) 2016 - 2026 IP2Location ( support@ip2location.com )
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  This module allows user to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges, residential proxies, consumer privacy networks, and enterprise private networks using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names. It lookup the proxy IP address from **IP2Proxy BIN Data** file or web service.
6
6
 
7
7
  For more details, please visit:
8
- [https://www.ip2location.com/ip2proxy/developers/ruby](https://www.ip2location.com/ip2proxy/developers/ruby)
8
+ [https://www.ip2location.com/documentation/ip2proxy-libraries/ruby](https://www.ip2location.com/documentation/ip2proxy-libraries/ruby)
9
9
 
10
10
  # Developer Documentation
11
11
  To learn more about installation, usage, and code examples, please visit the developer documentation at [https://ip2proxy-ruby.readthedocs.io/en/latest/index.html.](https://ip2proxy-ruby.readthedocs.io/en/latest/index.html)
@@ -24,6 +24,7 @@ To learn more about installation, usage, and code examples, please visit the dev
24
24
  |RES|Residential Proxies [PX10+]|
25
25
  |CPN|Consumer Privacy Networks. [PX11+]|
26
26
  |EPN|Enterprise Private Networks. [PX11+]|
27
+ |AIC|AI Crawler. [PX12]|
27
28
 
28
29
  ### Usage Type
29
30
 
@@ -41,14 +42,16 @@ To learn more about installation, usage, and code examples, please visit the dev
41
42
  |DCH|Data Center/Web Hosting/Transit|
42
43
  |SES|Search Engine Spider|
43
44
  |RSV|Reserved|
45
+ |AIC|AI Crawler|
44
46
 
45
47
  ### Threat Type
46
48
 
47
49
  |Threat Type|Description|
48
50
  |---|---|
49
- |SPAM|Spammer|
51
+ |SPAM|Email and forum spammers|
50
52
  |SCANNER|Security Scanner or Attack|
51
53
  |BOTNET|Spyware or Malware|
54
+ |BOGON|Unassigned or illegitimate IP addresses announced via BGP|
52
55
 
53
56
  # Dependencies
54
57
 
data/example.rb CHANGED
@@ -1,47 +1,47 @@
1
- require 'ip2proxy_ruby'
2
-
3
- # open IP2Proxy BIN database for proxy lookup
4
- i2p = Ip2proxy.new.open("./data/PX12.SAMPLE.BIN")
5
-
6
- # get versioning information
7
- print 'Module Version: ' + i2p.get_module_version + "\n"
8
- print 'Package Version: ' + i2p.get_package_version + "\n"
9
- print 'Database Version: ' + i2p.get_database_version + "\n"
10
-
11
- # individual proxy data check
12
- print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
13
- print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
14
- print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
15
- print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
16
- print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
17
- print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
18
- print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
19
- print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
20
- print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
21
- print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
22
- print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
23
- print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
24
- print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
25
- print 'Provider: ' + i2p.get_provider('1.2.3.4') + "\n"
26
- print 'Fraud Score: ' + i2p.get_fraud_score('1.2.3.4') + "\n"
27
-
28
- # single function to get all proxy data returned in array
29
- record = i2p.get_all('1.2.3.4')
30
- print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
31
- print 'Proxy Type: ' + record['proxy_type'] + "\n"
32
- print 'Country Code: ' + record['country_short'] + "\n"
33
- print 'Country Name: ' + record['country_long'] + "\n"
34
- print 'Region Name: ' + record['region'] + "\n"
35
- print 'City Name: ' + record['city'] + "\n"
36
- print 'ISP: ' + record['isp'] + "\n"
37
- print 'Domain: ' + record['domain'] + "\n"
38
- print 'Usage Type: ' + record['usagetype'] + "\n"
39
- print 'ASN: ' + record['asn'] + "\n"
40
- print 'AS: ' + record['as'] + "\n"
41
- print 'Last Seen: ' + record['last_seen'] + "\n"
42
- print 'Threat: ' + record['threat'] + "\n"
43
- print 'Provider: ' + record['provider'] + "\n"
44
- print 'Fraud Score: ' + record['fraud_score'] + "\n"
45
-
46
- # close IP2Proxy BIN database
1
+ require 'ip2proxy_ruby'
2
+
3
+ # open IP2Proxy BIN database for proxy lookup
4
+ i2p = Ip2proxy.new.open("./data/PX12.SAMPLE.BIN")
5
+
6
+ # get versioning information
7
+ print 'Module Version: ' + i2p.get_module_version + "\n"
8
+ print 'Package Version: ' + i2p.get_package_version + "\n"
9
+ print 'Database Version: ' + i2p.get_database_version + "\n"
10
+
11
+ # individual proxy data check
12
+ print 'Is Proxy: ' + i2p.is_proxy('1.2.3.4').to_s + "\n"
13
+ print 'Proxy Type: ' + i2p.get_proxytype('1.2.3.4') + "\n"
14
+ print 'Country Code: ' + i2p.get_country_short('1.2.3.4') + "\n"
15
+ print 'Country Name: ' + i2p.get_country_long('1.2.3.4') + "\n"
16
+ print 'Region Name: ' + i2p.get_region('1.2.3.4') + "\n"
17
+ print 'City Name: ' + i2p.get_city('1.2.3.4') + "\n"
18
+ print 'ISP: ' + i2p.get_isp('1.2.3.4') + "\n"
19
+ print 'Domain: ' + i2p.get_domain('1.2.3.4') + "\n"
20
+ print 'Usage Type: ' + i2p.get_usagetype('1.2.3.4') + "\n"
21
+ print 'ASN: ' + i2p.get_asn('1.2.3.4') + "\n"
22
+ print 'AS: ' + i2p.get_as('1.2.3.4') + "\n"
23
+ print 'Last Seen: ' + i2p.get_last_seen('1.2.3.4') + "\n"
24
+ print 'Threat: ' + i2p.get_threat('1.2.3.4') + "\n"
25
+ print 'Provider: ' + i2p.get_provider('1.2.3.4') + "\n"
26
+ print 'Fraud Score: ' + i2p.get_fraud_score('1.2.3.4') + "\n"
27
+
28
+ # single function to get all proxy data returned in array
29
+ record = i2p.get_all('1.2.3.4')
30
+ print 'is Proxy: ' + record['is_proxy'].to_s + "\n"
31
+ print 'Proxy Type: ' + record['proxy_type'] + "\n"
32
+ print 'Country Code: ' + record['country_short'] + "\n"
33
+ print 'Country Name: ' + record['country_long'] + "\n"
34
+ print 'Region Name: ' + record['region'] + "\n"
35
+ print 'City Name: ' + record['city'] + "\n"
36
+ print 'ISP: ' + record['isp'] + "\n"
37
+ print 'Domain: ' + record['domain'] + "\n"
38
+ print 'Usage Type: ' + record['usagetype'] + "\n"
39
+ print 'ASN: ' + record['asn'] + "\n"
40
+ print 'AS: ' + record['as'] + "\n"
41
+ print 'Last Seen: ' + record['last_seen'] + "\n"
42
+ print 'Threat: ' + record['threat'] + "\n"
43
+ print 'Provider: ' + record['provider'] + "\n"
44
+ print 'Fraud Score: ' + record['fraud_score'] + "\n"
45
+
46
+ # close IP2Proxy BIN database
47
47
  i2p.close()
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "ip2proxy_ruby"
5
- s.version = "3.4.0"
5
+ s.version = "3.5.0"
6
6
  s.authors = ["ip2location"]
7
7
  s.email = ["support@ip2location.com"]
8
8
 
data/lib/ip2proxy_ruby.rb CHANGED
@@ -1,546 +1,546 @@
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.4.0'
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, high = read32x2(indexpos)
105
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
106
- else
107
- return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
108
- end
109
- end
110
-
111
- def get_country_short(ip)
112
- valid = !(IPAddr.new(ip) rescue nil).nil?
113
- if valid
114
- rec = get_record(ip)
115
- if !(rec.nil?)
116
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
117
- else
118
- country_short = INVALID_IP_ADDRESS
119
- end
120
- else
121
- country_short = INVALID_IP_ADDRESS
122
- end
123
- return country_short
124
- end
125
-
126
- def get_country_long(ip)
127
- valid = !(IPAddr.new(ip) rescue nil).nil?
128
- if valid
129
- rec = get_record(ip)
130
- if !(rec.nil?)
131
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
132
- else
133
- country_long = INVALID_IP_ADDRESS
134
- end
135
- else
136
- country_long = INVALID_IP_ADDRESS
137
- end
138
- return country_long
139
- end
140
-
141
- def get_region(ip)
142
- valid = !(IPAddr.new(ip) rescue nil).nil?
143
- if valid
144
- rec = get_record(ip)
145
- if !(rec.nil?)
146
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
147
- else
148
- region = INVALID_IP_ADDRESS
149
- end
150
- else
151
- region = INVALID_IP_ADDRESS
152
- end
153
- return region
154
- end
155
-
156
- def get_city(ip)
157
- valid = !(IPAddr.new(ip) rescue nil).nil?
158
- if valid
159
- rec = get_record(ip)
160
- if !(rec.nil?)
161
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
162
- else
163
- city = INVALID_IP_ADDRESS
164
- end
165
- else
166
- city = INVALID_IP_ADDRESS
167
- end
168
- return city
169
- end
170
-
171
- def get_isp(ip)
172
- valid = !(IPAddr.new(ip) rescue nil).nil?
173
- if valid
174
- rec = get_record(ip)
175
- if !(rec.nil?)
176
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
177
- else
178
- isp = INVALID_IP_ADDRESS
179
- end
180
- else
181
- isp = INVALID_IP_ADDRESS
182
- end
183
- return isp
184
- end
185
-
186
- def get_proxytype(ip)
187
- valid = !(IPAddr.new(ip) rescue nil).nil?
188
- if valid
189
- rec = get_record(ip)
190
- if !(rec.nil?)
191
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
192
- else
193
- proxytype = INVALID_IP_ADDRESS
194
- end
195
- else
196
- proxytype = INVALID_IP_ADDRESS
197
- end
198
- return proxytype
199
- end
200
-
201
- def get_domain(ip)
202
- valid = !(IPAddr.new(ip) rescue nil).nil?
203
- if valid
204
- rec = get_record(ip)
205
- if !(rec.nil?)
206
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
207
- else
208
- domain = INVALID_IP_ADDRESS
209
- end
210
- else
211
- domain = INVALID_IP_ADDRESS
212
- end
213
- return domain
214
- end
215
-
216
- def get_usagetype(ip)
217
- valid = !(IPAddr.new(ip) rescue nil).nil?
218
- if valid
219
- rec = get_record(ip)
220
- if !(rec.nil?)
221
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
222
- else
223
- usagetype = INVALID_IP_ADDRESS
224
- end
225
- else
226
- usagetype = INVALID_IP_ADDRESS
227
- end
228
- return usagetype
229
- end
230
-
231
- def get_asn(ip)
232
- valid = !(IPAddr.new(ip) rescue nil).nil?
233
- if valid
234
- rec = get_record(ip)
235
- if !(rec.nil?)
236
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
237
- else
238
- asn = INVALID_IP_ADDRESS
239
- end
240
- else
241
- asn = INVALID_IP_ADDRESS
242
- end
243
- return asn
244
- end
245
-
246
- def get_as(ip)
247
- valid = !(IPAddr.new(ip) rescue nil).nil?
248
- if valid
249
- rec = get_record(ip)
250
- if !(rec.nil?)
251
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
252
- else
253
- as = INVALID_IP_ADDRESS
254
- end
255
- else
256
- as = INVALID_IP_ADDRESS
257
- end
258
- return as
259
- end
260
-
261
- def get_last_seen(ip)
262
- valid = !(IPAddr.new(ip) rescue nil).nil?
263
- if valid
264
- rec = get_record(ip)
265
- if !(rec.nil?)
266
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
267
- else
268
- last_seen = INVALID_IP_ADDRESS
269
- end
270
- else
271
- last_seen = INVALID_IP_ADDRESS
272
- end
273
- return last_seen
274
- end
275
-
276
- def get_threat(ip)
277
- valid = !(IPAddr.new(ip) rescue nil).nil?
278
- if valid
279
- rec = get_record(ip)
280
- if !(rec.nil?)
281
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
282
- else
283
- threat = INVALID_IP_ADDRESS
284
- end
285
- else
286
- threat = INVALID_IP_ADDRESS
287
- end
288
- return threat
289
- end
290
-
291
- def is_proxy(ip)
292
- valid = !(IPAddr.new(ip) rescue nil).nil?
293
- if valid
294
- rec = get_record(ip)
295
- if !(rec.nil?)
296
- if self.db_index == 1
297
- isproxy = (rec.country_short == '-') ? 0 : 1
298
- else
299
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
300
- end
301
- else
302
- isproxy = -1
303
- end
304
- else
305
- isproxy = -1
306
- end
307
- return isproxy
308
- end
309
-
310
- def get_provider(ip)
311
- valid = !(IPAddr.new(ip) rescue nil).nil?
312
- if valid
313
- rec = get_record(ip)
314
- if !(rec.nil?)
315
- provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
316
- else
317
- provider = INVALID_IP_ADDRESS
318
- end
319
- else
320
- provider = INVALID_IP_ADDRESS
321
- end
322
- return provider
323
- end
324
-
325
- def get_fraud_score(ip)
326
- valid = !(IPAddr.new(ip) rescue nil).nil?
327
- if valid
328
- rec = get_record(ip)
329
- if !(rec.nil?)
330
- fraud_score = (defined?(rec.fraud_score) && rec.fraud_score != '') ? rec.fraud_score : FIELD_NOT_SUPPORTED
331
- else
332
- fraud_score = INVALID_IP_ADDRESS
333
- end
334
- else
335
- fraud_score = INVALID_IP_ADDRESS
336
- end
337
- return fraud_score
338
- end
339
-
340
- def get_all(ip)
341
- valid = !(IPAddr.new(ip) rescue nil).nil?
342
- if valid
343
- rec = get_record(ip)
344
- if !(rec.nil?)
345
- country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
346
- country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
347
- region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
348
- city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
349
- isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
350
- proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
351
- domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
352
- usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
353
- asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
354
- as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
355
- last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
356
- threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
357
- provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
358
- fraud_score = (defined?(rec.fraud_score) && rec.fraud_score != '') ? rec.fraud_score : FIELD_NOT_SUPPORTED
359
- if self.db_index == 1
360
- isproxy = (rec.country_short == '-') ? 0 : 1
361
- else
362
- isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
363
- end
364
- else
365
- country_short = INVALID_IP_ADDRESS
366
- country_long = INVALID_IP_ADDRESS
367
- region = INVALID_IP_ADDRESS
368
- city = INVALID_IP_ADDRESS
369
- isp = INVALID_IP_ADDRESS
370
- proxytype = INVALID_IP_ADDRESS
371
- domain = INVALID_IP_ADDRESS
372
- usagetype = INVALID_IP_ADDRESS
373
- asn = INVALID_IP_ADDRESS
374
- as = INVALID_IP_ADDRESS
375
- last_seen = INVALID_IP_ADDRESS
376
- threat = INVALID_IP_ADDRESS
377
- provider = INVALID_IP_ADDRESS
378
- fraud_score = INVALID_IP_ADDRESS
379
- isproxy = -1
380
- end
381
- else
382
- country_short = INVALID_IP_ADDRESS
383
- country_long = INVALID_IP_ADDRESS
384
- region = INVALID_IP_ADDRESS
385
- city = INVALID_IP_ADDRESS
386
- isp = INVALID_IP_ADDRESS
387
- proxytype = INVALID_IP_ADDRESS
388
- domain = INVALID_IP_ADDRESS
389
- usagetype = INVALID_IP_ADDRESS
390
- asn = INVALID_IP_ADDRESS
391
- as = INVALID_IP_ADDRESS
392
- last_seen = INVALID_IP_ADDRESS
393
- threat = INVALID_IP_ADDRESS
394
- provider = INVALID_IP_ADDRESS
395
- fraud_score = INVALID_IP_ADDRESS
396
- isproxy = -1
397
- end
398
- results = {}
399
- results['is_proxy'] = isproxy
400
- results['proxy_type'] = proxytype
401
- results['country_short'] = country_short
402
- results['country_long'] = country_long
403
- results['region'] = region
404
- results['city'] = city
405
- results['isp'] = isp
406
- results['domain'] = domain
407
- results['usagetype'] = usagetype
408
- results['asn'] = asn
409
- results['as'] = as
410
- results['last_seen'] = last_seen
411
- results['threat'] = threat
412
- results['provider'] = provider
413
- results['fraud_score'] = fraud_score
414
- return results
415
- end
416
-
417
- def bsearch(low, high, ipnum, base_addr, col_length)
418
- while low <= high do
419
- mid = (low + high) >> 1
420
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
421
- if ipnum >= ip_from && ipnum < ip_to
422
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
423
- file.seek(from_base)
424
- if v4
425
- return self.record_class4.read(file)
426
- else
427
- return self.record_class6.read(file)
428
- end
429
- else
430
- if ipnum < ip_from
431
- high = mid - 1
432
- else
433
- low = mid + 1
434
- end
435
- end
436
- end
437
- end
438
-
439
- def get_from_to(mid, base_addr, col_length)
440
- from_base = (base_addr + mid * (col_length + (v4 ? 0 : 12)))
441
- data_length = col_length + (v4 ? 4 : (12 + 16))
442
- file.seek(from_base)
443
- data_read = file.read(data_length)
444
- ip_from = v4 ? data_read[0..3].unpack('V').first : readipv6(data_read[0..15].unpack('V*'))
445
- ip_to = v4 ? data_read[(data_length - 4)..(data_length - 1)].unpack('V').first : readipv6(data_read[(data_length - 16)..(data_length - 1)].unpack('V*'))
446
- [ip_from, ip_to]
447
- end
448
-
449
- def validateip(ip)
450
- if ip.ipv4?
451
- ipv = 4
452
- ipnum = ip.to_i + 0
453
- else
454
- ipv = 6
455
- ipnum = ip.to_i + 0
456
- #reformat ipv4 address in ipv6
457
- if ipnum >= 281470681743360 && ipnum <= 281474976710655
458
- ipv = 4
459
- ipnum = ipnum - 281470681743360
460
- end
461
- #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
462
- if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
463
- ipv = 4
464
- #bitshift right 80 bits
465
- ipnum = ipnum >> 80
466
- #bitwise modulus to get the last 32 bit
467
- ipnum = ipnum % 4294967296
468
- end
469
- #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
470
- if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
471
- ipv = 4
472
- #bitwise not to invert binary
473
- ipnum = ~ipnum
474
- #bitwise modulus to get the last 32 bit
475
- ipnum = ipnum % 4294967296
476
- end
477
- end
478
- [ipv, ipnum]
479
- end
480
-
481
- def read32x2(indexp)
482
- file.seek(indexp - 1)
483
- data_read = file.read(8)
484
- data1 = data_read[0..3].unpack('V').first
485
- data2 = data_read[4..7].unpack('V').first
486
- return [data1, data2]
487
- end
488
-
489
- def readipv6(parts)
490
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
491
- end
492
-
493
- private :get_record, :bsearch, :get_from_to, :read32x2, :readipv6
494
-
495
- end
496
-
497
- class Ip2proxyWebService
498
- attr_accessor :ws_api_key, :ws_package, :ws_use_ssl
499
-
500
- def initialize(api_key, package, use_ssl)
501
- if !api_key.match(/^[0-9A-Z]{10}$/) && api_key != 'demo'
502
- raise Exception.new "Please provide a valid IP2Proxy web service API key."
503
- end
504
- if !package.match(/^PX[0-9]+$/)
505
- package = 'PX1'
506
- end
507
- if use_ssl == ''
508
- use_ssl = true
509
- end
510
- self.ws_api_key = api_key
511
- self.ws_package = package
512
- self.ws_use_ssl = use_ssl
513
- end
514
-
515
- def lookup(ip)
516
- if self.ws_use_ssl
517
- response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
518
- else
519
- response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
520
- end
521
- parsed_response = JSON.parse(response)
522
- if parsed_response.nil?
523
- return false
524
- end
525
- if parsed_response["response"] != "OK"
526
- raise Exception.new "Error: " + parsed_response["response"]
527
- end
528
- return parsed_response
529
- end
530
-
531
- def get_credit()
532
- if self.ws_use_ssl
533
- response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
534
- else
535
- response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
536
- end
537
- parsed_response = JSON.parse(response)
538
- if parsed_response.nil?
539
- return 0
540
- end
541
- if parsed_response["response"].nil?
542
- return 0
543
- end
544
- return parsed_response["response"]
545
- end
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.5.0'
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, high = read32x2(indexpos)
105
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
106
+ else
107
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
108
+ end
109
+ end
110
+
111
+ def get_country_short(ip)
112
+ valid = !(IPAddr.new(ip) rescue nil).nil?
113
+ if valid
114
+ rec = get_record(ip)
115
+ if !(rec.nil?)
116
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
117
+ else
118
+ country_short = INVALID_IP_ADDRESS
119
+ end
120
+ else
121
+ country_short = INVALID_IP_ADDRESS
122
+ end
123
+ return country_short
124
+ end
125
+
126
+ def get_country_long(ip)
127
+ valid = !(IPAddr.new(ip) rescue nil).nil?
128
+ if valid
129
+ rec = get_record(ip)
130
+ if !(rec.nil?)
131
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
132
+ else
133
+ country_long = INVALID_IP_ADDRESS
134
+ end
135
+ else
136
+ country_long = INVALID_IP_ADDRESS
137
+ end
138
+ return country_long
139
+ end
140
+
141
+ def get_region(ip)
142
+ valid = !(IPAddr.new(ip) rescue nil).nil?
143
+ if valid
144
+ rec = get_record(ip)
145
+ if !(rec.nil?)
146
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
147
+ else
148
+ region = INVALID_IP_ADDRESS
149
+ end
150
+ else
151
+ region = INVALID_IP_ADDRESS
152
+ end
153
+ return region
154
+ end
155
+
156
+ def get_city(ip)
157
+ valid = !(IPAddr.new(ip) rescue nil).nil?
158
+ if valid
159
+ rec = get_record(ip)
160
+ if !(rec.nil?)
161
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
162
+ else
163
+ city = INVALID_IP_ADDRESS
164
+ end
165
+ else
166
+ city = INVALID_IP_ADDRESS
167
+ end
168
+ return city
169
+ end
170
+
171
+ def get_isp(ip)
172
+ valid = !(IPAddr.new(ip) rescue nil).nil?
173
+ if valid
174
+ rec = get_record(ip)
175
+ if !(rec.nil?)
176
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
177
+ else
178
+ isp = INVALID_IP_ADDRESS
179
+ end
180
+ else
181
+ isp = INVALID_IP_ADDRESS
182
+ end
183
+ return isp
184
+ end
185
+
186
+ def get_proxytype(ip)
187
+ valid = !(IPAddr.new(ip) rescue nil).nil?
188
+ if valid
189
+ rec = get_record(ip)
190
+ if !(rec.nil?)
191
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
192
+ else
193
+ proxytype = INVALID_IP_ADDRESS
194
+ end
195
+ else
196
+ proxytype = INVALID_IP_ADDRESS
197
+ end
198
+ return proxytype
199
+ end
200
+
201
+ def get_domain(ip)
202
+ valid = !(IPAddr.new(ip) rescue nil).nil?
203
+ if valid
204
+ rec = get_record(ip)
205
+ if !(rec.nil?)
206
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
207
+ else
208
+ domain = INVALID_IP_ADDRESS
209
+ end
210
+ else
211
+ domain = INVALID_IP_ADDRESS
212
+ end
213
+ return domain
214
+ end
215
+
216
+ def get_usagetype(ip)
217
+ valid = !(IPAddr.new(ip) rescue nil).nil?
218
+ if valid
219
+ rec = get_record(ip)
220
+ if !(rec.nil?)
221
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
222
+ else
223
+ usagetype = INVALID_IP_ADDRESS
224
+ end
225
+ else
226
+ usagetype = INVALID_IP_ADDRESS
227
+ end
228
+ return usagetype
229
+ end
230
+
231
+ def get_asn(ip)
232
+ valid = !(IPAddr.new(ip) rescue nil).nil?
233
+ if valid
234
+ rec = get_record(ip)
235
+ if !(rec.nil?)
236
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
237
+ else
238
+ asn = INVALID_IP_ADDRESS
239
+ end
240
+ else
241
+ asn = INVALID_IP_ADDRESS
242
+ end
243
+ return asn
244
+ end
245
+
246
+ def get_as(ip)
247
+ valid = !(IPAddr.new(ip) rescue nil).nil?
248
+ if valid
249
+ rec = get_record(ip)
250
+ if !(rec.nil?)
251
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
252
+ else
253
+ as = INVALID_IP_ADDRESS
254
+ end
255
+ else
256
+ as = INVALID_IP_ADDRESS
257
+ end
258
+ return as
259
+ end
260
+
261
+ def get_last_seen(ip)
262
+ valid = !(IPAddr.new(ip) rescue nil).nil?
263
+ if valid
264
+ rec = get_record(ip)
265
+ if !(rec.nil?)
266
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
267
+ else
268
+ last_seen = INVALID_IP_ADDRESS
269
+ end
270
+ else
271
+ last_seen = INVALID_IP_ADDRESS
272
+ end
273
+ return last_seen
274
+ end
275
+
276
+ def get_threat(ip)
277
+ valid = !(IPAddr.new(ip) rescue nil).nil?
278
+ if valid
279
+ rec = get_record(ip)
280
+ if !(rec.nil?)
281
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
282
+ else
283
+ threat = INVALID_IP_ADDRESS
284
+ end
285
+ else
286
+ threat = INVALID_IP_ADDRESS
287
+ end
288
+ return threat
289
+ end
290
+
291
+ def is_proxy(ip)
292
+ valid = !(IPAddr.new(ip) rescue nil).nil?
293
+ if valid
294
+ rec = get_record(ip)
295
+ if !(rec.nil?)
296
+ if self.db_index == 1
297
+ isproxy = (rec.country_short == '-') ? 0 : 1
298
+ else
299
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES' || rec.proxytype == 'AIC') ? 2 : 1
300
+ end
301
+ else
302
+ isproxy = -1
303
+ end
304
+ else
305
+ isproxy = -1
306
+ end
307
+ return isproxy
308
+ end
309
+
310
+ def get_provider(ip)
311
+ valid = !(IPAddr.new(ip) rescue nil).nil?
312
+ if valid
313
+ rec = get_record(ip)
314
+ if !(rec.nil?)
315
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
316
+ else
317
+ provider = INVALID_IP_ADDRESS
318
+ end
319
+ else
320
+ provider = INVALID_IP_ADDRESS
321
+ end
322
+ return provider
323
+ end
324
+
325
+ def get_fraud_score(ip)
326
+ valid = !(IPAddr.new(ip) rescue nil).nil?
327
+ if valid
328
+ rec = get_record(ip)
329
+ if !(rec.nil?)
330
+ fraud_score = (defined?(rec.fraud_score) && rec.fraud_score != '') ? rec.fraud_score : FIELD_NOT_SUPPORTED
331
+ else
332
+ fraud_score = INVALID_IP_ADDRESS
333
+ end
334
+ else
335
+ fraud_score = INVALID_IP_ADDRESS
336
+ end
337
+ return fraud_score
338
+ end
339
+
340
+ def get_all(ip)
341
+ valid = !(IPAddr.new(ip) rescue nil).nil?
342
+ if valid
343
+ rec = get_record(ip)
344
+ if !(rec.nil?)
345
+ country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
346
+ country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
347
+ region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
348
+ city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
349
+ isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
350
+ proxytype = (defined?(rec.proxytype) && rec.proxytype != '') ? rec.proxytype : FIELD_NOT_SUPPORTED
351
+ domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
352
+ usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
353
+ asn = (defined?(rec.asn) && rec.asn != '') ? rec.asn : FIELD_NOT_SUPPORTED
354
+ as = (defined?(rec.as) && rec.as != '') ? rec.as : FIELD_NOT_SUPPORTED
355
+ last_seen = (defined?(rec.lastseen) && rec.lastseen != '') ? rec.lastseen : FIELD_NOT_SUPPORTED
356
+ threat = (defined?(rec.threat) && rec.threat != '') ? rec.threat : FIELD_NOT_SUPPORTED
357
+ provider = (defined?(rec.provider) && rec.provider != '') ? rec.provider : FIELD_NOT_SUPPORTED
358
+ fraud_score = (defined?(rec.fraud_score) && rec.fraud_score != '') ? rec.fraud_score : FIELD_NOT_SUPPORTED
359
+ if self.db_index == 1
360
+ isproxy = (rec.country_short == '-') ? 0 : 1
361
+ else
362
+ isproxy = (rec.proxytype == '-') ? 0 : (rec.proxytype == 'DCH' || rec.proxytype == 'SES') ? 2 : 1
363
+ end
364
+ else
365
+ country_short = INVALID_IP_ADDRESS
366
+ country_long = INVALID_IP_ADDRESS
367
+ region = INVALID_IP_ADDRESS
368
+ city = INVALID_IP_ADDRESS
369
+ isp = INVALID_IP_ADDRESS
370
+ proxytype = INVALID_IP_ADDRESS
371
+ domain = INVALID_IP_ADDRESS
372
+ usagetype = INVALID_IP_ADDRESS
373
+ asn = INVALID_IP_ADDRESS
374
+ as = INVALID_IP_ADDRESS
375
+ last_seen = INVALID_IP_ADDRESS
376
+ threat = INVALID_IP_ADDRESS
377
+ provider = INVALID_IP_ADDRESS
378
+ fraud_score = INVALID_IP_ADDRESS
379
+ isproxy = -1
380
+ end
381
+ else
382
+ country_short = INVALID_IP_ADDRESS
383
+ country_long = INVALID_IP_ADDRESS
384
+ region = INVALID_IP_ADDRESS
385
+ city = INVALID_IP_ADDRESS
386
+ isp = INVALID_IP_ADDRESS
387
+ proxytype = INVALID_IP_ADDRESS
388
+ domain = INVALID_IP_ADDRESS
389
+ usagetype = INVALID_IP_ADDRESS
390
+ asn = INVALID_IP_ADDRESS
391
+ as = INVALID_IP_ADDRESS
392
+ last_seen = INVALID_IP_ADDRESS
393
+ threat = INVALID_IP_ADDRESS
394
+ provider = INVALID_IP_ADDRESS
395
+ fraud_score = INVALID_IP_ADDRESS
396
+ isproxy = -1
397
+ end
398
+ results = {}
399
+ results['is_proxy'] = isproxy
400
+ results['proxy_type'] = proxytype
401
+ results['country_short'] = country_short
402
+ results['country_long'] = country_long
403
+ results['region'] = region
404
+ results['city'] = city
405
+ results['isp'] = isp
406
+ results['domain'] = domain
407
+ results['usagetype'] = usagetype
408
+ results['asn'] = asn
409
+ results['as'] = as
410
+ results['last_seen'] = last_seen
411
+ results['threat'] = threat
412
+ results['provider'] = provider
413
+ results['fraud_score'] = fraud_score
414
+ return results
415
+ end
416
+
417
+ def bsearch(low, high, ipnum, base_addr, col_length)
418
+ while low <= high do
419
+ mid = (low + high) >> 1
420
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
421
+ if ipnum >= ip_from && ipnum < ip_to
422
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
423
+ file.seek(from_base)
424
+ if v4
425
+ return self.record_class4.read(file)
426
+ else
427
+ return self.record_class6.read(file)
428
+ end
429
+ else
430
+ if ipnum < ip_from
431
+ high = mid - 1
432
+ else
433
+ low = mid + 1
434
+ end
435
+ end
436
+ end
437
+ end
438
+
439
+ def get_from_to(mid, base_addr, col_length)
440
+ from_base = (base_addr + mid * (col_length + (v4 ? 0 : 12)))
441
+ data_length = col_length + (v4 ? 4 : (12 + 16))
442
+ file.seek(from_base)
443
+ data_read = file.read(data_length)
444
+ ip_from = v4 ? data_read[0..3].unpack('V').first : readipv6(data_read[0..15].unpack('V*'))
445
+ ip_to = v4 ? data_read[(data_length - 4)..(data_length - 1)].unpack('V').first : readipv6(data_read[(data_length - 16)..(data_length - 1)].unpack('V*'))
446
+ [ip_from, ip_to]
447
+ end
448
+
449
+ def validateip(ip)
450
+ if ip.ipv4?
451
+ ipv = 4
452
+ ipnum = ip.to_i + 0
453
+ else
454
+ ipv = 6
455
+ ipnum = ip.to_i + 0
456
+ #reformat ipv4 address in ipv6
457
+ if ipnum >= 281470681743360 && ipnum <= 281474976710655
458
+ ipv = 4
459
+ ipnum = ipnum - 281470681743360
460
+ end
461
+ #reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
462
+ if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
463
+ ipv = 4
464
+ #bitshift right 80 bits
465
+ ipnum = ipnum >> 80
466
+ #bitwise modulus to get the last 32 bit
467
+ ipnum = ipnum % 4294967296
468
+ end
469
+ #reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
470
+ if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
471
+ ipv = 4
472
+ #bitwise not to invert binary
473
+ ipnum = ~ipnum
474
+ #bitwise modulus to get the last 32 bit
475
+ ipnum = ipnum % 4294967296
476
+ end
477
+ end
478
+ [ipv, ipnum]
479
+ end
480
+
481
+ def read32x2(indexp)
482
+ file.seek(indexp - 1)
483
+ data_read = file.read(8)
484
+ data1 = data_read[0..3].unpack('V').first
485
+ data2 = data_read[4..7].unpack('V').first
486
+ return [data1, data2]
487
+ end
488
+
489
+ def readipv6(parts)
490
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
491
+ end
492
+
493
+ private :get_record, :bsearch, :get_from_to, :read32x2, :readipv6
494
+
495
+ end
496
+
497
+ class Ip2proxyWebService
498
+ attr_accessor :ws_api_key, :ws_package, :ws_use_ssl
499
+
500
+ def initialize(api_key, package, use_ssl)
501
+ if !api_key.match(/^[0-9A-Z]{10}$/) && api_key != 'demo'
502
+ raise Exception.new "Please provide a valid IP2Proxy web service API key."
503
+ end
504
+ if !package.match(/^PX[0-9]+$/)
505
+ package = 'PX1'
506
+ end
507
+ if use_ssl == ''
508
+ use_ssl = true
509
+ end
510
+ self.ws_api_key = api_key
511
+ self.ws_package = package
512
+ self.ws_use_ssl = use_ssl
513
+ end
514
+
515
+ def lookup(ip)
516
+ if self.ws_use_ssl
517
+ response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
518
+ else
519
+ response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&ip=" + ip + "&package=" + self.ws_package + "&format=json"))
520
+ end
521
+ parsed_response = JSON.parse(response)
522
+ if parsed_response.nil?
523
+ return false
524
+ end
525
+ if parsed_response["response"] != "OK"
526
+ raise Exception.new "Error: " + parsed_response["response"]
527
+ end
528
+ return parsed_response
529
+ end
530
+
531
+ def get_credit()
532
+ if self.ws_use_ssl
533
+ response = Net::HTTP.get(URI("https://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
534
+ else
535
+ response = Net::HTTP.get(URI("http://api.ip2proxy.com/?key=" + self.ws_api_key + "&check=true"))
536
+ end
537
+ parsed_response = JSON.parse(response)
538
+ if parsed_response.nil?
539
+ return 0
540
+ end
541
+ if parsed_response["response"].nil?
542
+ return 0
543
+ end
544
+ return parsed_response["response"]
545
+ end
546
546
  end
@@ -112,7 +112,7 @@ describe "Ip2proxy" do
112
112
  it "work correctly with get_module_version" do
113
113
  i2p = Ip2proxy.new.open(File.dirname(__FILE__) + "/assets/PX12.SAMPLE.BIN")
114
114
  record = i2p.get_module_version()
115
- expect(record).to eq '3.4.0'
115
+ expect(record).to eq '3.5.0'
116
116
  end
117
117
 
118
118
  it "work correctly with get_package_version" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip2proxy_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ip2location
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-20 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bindata
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  requirements: []
121
- rubygems_version: 3.6.4
121
+ rubygems_version: 4.0.10
122
122
  specification_version: 4
123
123
  summary: IP2Proxy Ruby library
124
124
  test_files: []