internetdata 2.9.2 → 2.10.1
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 +4 -4
- data/README.md +146 -9
- data/lib/internetdata/api/database_v2_api.rb +2 -2
- data/lib/internetdata/api_client.rb +2 -2
- data/lib/internetdata/api_error.rb +2 -2
- data/lib/internetdata/api_model_base.rb +2 -2
- data/lib/internetdata/asn_result.rb +107 -0
- data/lib/internetdata/bogon.rb +92 -0
- data/lib/internetdata/bogons.rb +93 -0
- data/lib/internetdata/cache.rb +35 -0
- data/lib/internetdata/client.rb +396 -16
- data/lib/internetdata/configuration.rb +23 -2
- data/lib/internetdata/domain_result.rb +124 -0
- data/lib/internetdata/errors.rb +29 -6
- data/lib/internetdata/models/abuse_contact.rb +236 -0
- data/lib/internetdata/models/asn.rb +294 -0
- data/lib/internetdata/models/asn_lookup_response.rb +405 -0
- data/lib/internetdata/models/asn_prefix.rb +329 -0
- data/lib/internetdata/models/batch_lookup_error.rb +192 -0
- data/lib/internetdata/models/bit_torrent_detail.rb +240 -0
- data/lib/internetdata/models/carrier_detail.rb +188 -0
- data/lib/internetdata/models/class_detail.rb +178 -0
- data/lib/internetdata/models/client_registration.rb +2 -2
- data/lib/internetdata/models/company.rb +213 -0
- data/lib/internetdata/models/database.rb +2 -2
- data/lib/internetdata/models/database_checksums_response.rb +2 -2
- data/lib/internetdata/models/database_format.rb +2 -2
- data/lib/internetdata/models/database_list.rb +2 -2
- data/lib/internetdata/models/database_metadata.rb +2 -2
- data/lib/internetdata/models/database_metadata_column.rb +2 -2
- data/lib/internetdata/models/database_version.rb +2 -2
- data/lib/internetdata/models/db_checksums.rb +2 -2
- data/lib/internetdata/models/device_authorization.rb +2 -2
- data/lib/internetdata/models/disposable_detail.rb +180 -0
- data/lib/internetdata/models/dns_address_record.rb +192 -0
- data/lib/internetdata/models/dns_cname_record.rb +192 -0
- data/lib/internetdata/models/dns_mx_record.rb +219 -0
- data/lib/internetdata/models/dns_ns_record.rb +192 -0
- data/lib/internetdata/models/dns_resolver_detail.rb +198 -0
- data/lib/internetdata/models/domain_dns.rb +279 -0
- data/lib/internetdata/models/domain_ip_range.rb +219 -0
- data/lib/internetdata/models/domain_ip_ranges.rb +276 -0
- data/lib/internetdata/models/domain_lookup_response.rb +376 -0
- data/lib/internetdata/models/download.rb +2 -2
- data/lib/internetdata/models/download_list.rb +2 -2
- data/lib/internetdata/models/entitlement.rb +243 -0
- data/lib/internetdata/models/entitlement_apikey.rb +207 -0
- data/lib/internetdata/models/entitlement_plan.rb +216 -0
- data/lib/internetdata/models/entitlement_usage.rb +259 -0
- data/lib/internetdata/models/error_envelope.rb +2 -2
- data/lib/internetdata/models/hosted_domains.rb +166 -0
- data/lib/internetdata/models/location.rb +509 -0
- data/lib/internetdata/models/lookup_response.rb +897 -0
- data/lib/internetdata/models/oauth_metadata.rb +2 -2
- data/lib/internetdata/models/proxy_detail.rb +208 -0
- data/lib/internetdata/models/pubproxy_detail.rb +210 -0
- data/lib/internetdata/models/rwhois.rb +301 -0
- data/lib/internetdata/models/satellite_detail.rb +208 -0
- data/lib/internetdata/models/smtp_detail.rb +188 -0
- data/lib/internetdata/models/ssh_detail.rb +198 -0
- data/lib/internetdata/models/standing.rb +2 -2
- data/lib/internetdata/models/token_response.rb +2 -2
- data/lib/internetdata/models/web_detail.rb +516 -0
- data/lib/internetdata/models/whois.rb +382 -0
- data/lib/internetdata/oauth_api.rb +1 -1
- data/lib/internetdata/result.rb +431 -0
- data/lib/internetdata/transport.rb +210 -26
- data/lib/internetdata/version.rb +1 -1
- data/lib/internetdata.rb +17 -2
- metadata +58 -3
data/lib/internetdata/client.rb
CHANGED
|
@@ -3,43 +3,423 @@
|
|
|
3
3
|
require 'typhoeus'
|
|
4
4
|
|
|
5
5
|
module InternetData
|
|
6
|
-
DEFAULT_BASE_URL = 'https://internetdata.io'
|
|
6
|
+
DEFAULT_BASE_URL = 'https://api.internetdata.io'
|
|
7
|
+
DEFAULT_CACHE_MAX_SIZE = 10_000
|
|
8
|
+
DEFAULT_CACHE_TTL = 3600
|
|
9
|
+
DEFAULT_CONCURRENCY = 8
|
|
7
10
|
DEFAULT_RETRIES = 2
|
|
8
11
|
DEFAULT_TIMEOUT = 30
|
|
12
|
+
# The most addresses POST /batch takes in one call; a larger batch is sent in
|
|
13
|
+
# chunks of this size.
|
|
14
|
+
BATCH_MAX = 1000
|
|
9
15
|
|
|
10
16
|
# A client for the InternetData API.
|
|
11
17
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# a client built without one sends no `Authorization` header at all, which is
|
|
15
|
-
# what a database served without a license would need, and `oauth` needs none.
|
|
18
|
+
# The cache is per instance, so an answer is never shared between two clients
|
|
19
|
+
# holding different API keys and therefore entitled to different fields.
|
|
16
20
|
class Client
|
|
17
|
-
# The licensed database downloads,
|
|
21
|
+
# The licensed database downloads, for keys that carry the `db.download` scope.
|
|
18
22
|
attr_reader :database
|
|
19
23
|
# Signing a person in with OAuth, which needs no API key at all.
|
|
20
24
|
attr_reader :oauth
|
|
21
25
|
|
|
22
|
-
# @param api_key [String, nil] a
|
|
23
|
-
#
|
|
24
|
-
#
|
|
26
|
+
# @param api_key [String, nil] without one a lookup is on Unlimited, the free
|
|
27
|
+
# plan: an address's location, network, company, abuse contact, reverse
|
|
28
|
+
# DNS and every flag but the paid ones, an AS's registration and a
|
|
29
|
+
# domain's, limited per address per minute. A batch needs a key on a paid
|
|
30
|
+
# plan, and the databases a key carrying the `db.download` scope. It is
|
|
31
|
+
# trimmed, and a key of blanks alone is no key.
|
|
32
|
+
# @param base_url [String] the API; {#oauth} signs in at its apex, `api.`
|
|
33
|
+
# dropped and `api-<env>.` becoming `<env>.`, and at any other URL as given.
|
|
34
|
+
# @param cache [Boolean] pass false to disable caching.
|
|
35
|
+
# @param cache_ttl [Numeric] how long an answer stays fresh, in seconds.
|
|
36
|
+
# @param concurrency [Integer] batch requests - chunks of up to 1000 addresses -
|
|
37
|
+
# in flight during a batch, from 1.
|
|
25
38
|
# @param retries [Integer] extra attempts for a transient failure, from 0. Each
|
|
26
39
|
# waits the server's `Retry-After` when it sent one of at most about 24.8
|
|
27
40
|
# days, and otherwise a backoff of 250 ms that doubles per retry.
|
|
28
|
-
# @param timeout [Numeric] seconds
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
41
|
+
# @param timeout [Numeric] seconds one request may take before it is
|
|
42
|
+
# abandoned, 0 for no bound. Applies per ATTEMPT, so a retried call may take
|
|
43
|
+
# longer in total, and every call that takes `retries:` also takes
|
|
44
|
+
# `timeout:` to override it. A database transfer bounds only its connect
|
|
45
|
+
# phase with it.
|
|
46
|
+
# @raise [ArgumentError] for an `api_key` with a control character in it.
|
|
47
|
+
# @raise [ArgumentError] for a `timeout`, here or on any call, that is
|
|
48
|
+
# negative, not a finite number, or past 2147483.647 seconds (2**31 - 1 ms),
|
|
49
|
+
# the longest curl holds, for a `retries`, here or on any call, that is
|
|
50
|
+
# not an Integer from 0, and for a `concurrency` that is not an Integer
|
|
51
|
+
# from 1.
|
|
34
52
|
# @param transport [Transport, nil] override the HTTP layer, mostly for tests.
|
|
35
|
-
def initialize(api_key: nil, base_url: DEFAULT_BASE_URL,
|
|
53
|
+
def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, cache: true,
|
|
54
|
+
cache_max_size: DEFAULT_CACHE_MAX_SIZE, cache_ttl: DEFAULT_CACHE_TTL,
|
|
55
|
+
concurrency: DEFAULT_CONCURRENCY, retries: DEFAULT_RETRIES,
|
|
36
56
|
timeout: DEFAULT_TIMEOUT, transport: nil)
|
|
37
57
|
retries = Retries.checked_count(retries)
|
|
58
|
+
unless concurrency?(concurrency)
|
|
59
|
+
raise ArgumentError, "concurrency must be an Integer from 1, not #{concurrency.inspect}"
|
|
60
|
+
end
|
|
38
61
|
@transport = transport || Transport.new(
|
|
39
62
|
Transport::Config.new(api_key: api_key, base_url: base_url, timeout: timeout),
|
|
40
63
|
)
|
|
64
|
+
@cache = cache ? Cache.new(max_size: cache_max_size, ttl: cache_ttl) : nil
|
|
65
|
+
# The addresses with a request in flight, each a Flight its waiters block on.
|
|
66
|
+
@flights = {}
|
|
67
|
+
@flights_lock = Mutex.new
|
|
68
|
+
@concurrency = concurrency
|
|
69
|
+
@retries = retries
|
|
41
70
|
@database = DatabaseApi.new(@transport, retries: retries)
|
|
42
71
|
@oauth = OauthApi.new(@transport, retries: retries)
|
|
43
72
|
end
|
|
73
|
+
|
|
74
|
+
# Whether an address is private, loopback, link-local, documentation,
|
|
75
|
+
# multicast or otherwise not routable, including the IPv6 equivalents and
|
|
76
|
+
# the 6to4 and Teredo ranges.
|
|
77
|
+
#
|
|
78
|
+
# These are the addresses {#lookup} answers locally. Exposed here so the
|
|
79
|
+
# check is reachable from the client you already hold; the same predicate is
|
|
80
|
+
# also on the module itself, for code with no client to hand.
|
|
81
|
+
def bogon?(ip)
|
|
82
|
+
Bogon.bogon?(ip)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# What every database says about one address.
|
|
86
|
+
#
|
|
87
|
+
# A bogon is answered locally and never reaches the network. Everything else
|
|
88
|
+
# is served, then cached for this instance.
|
|
89
|
+
#
|
|
90
|
+
# @param retries [Integer, nil] extra attempts, for THIS call only.
|
|
91
|
+
# @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
|
|
92
|
+
# @raise [Error] `:bad_request`, before any request, for anything but an IP
|
|
93
|
+
# address: {#lookup_asn} and {#lookup_domain} look an AS and a domain up.
|
|
94
|
+
# @return [Result]
|
|
95
|
+
def lookup(ip, retries: nil, timeout: nil)
|
|
96
|
+
# Here as well as in the transport: a bogon or a cached answer returns before any request.
|
|
97
|
+
Transport.checked_timeout(timeout) unless timeout.nil?
|
|
98
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
99
|
+
# Judged, cached and sent as the IPv4 address it carries, if it is mapped.
|
|
100
|
+
ip = Bogon.unmapped(ip)
|
|
101
|
+
# The API answers an AS or a domain at this path too, as an answer this call
|
|
102
|
+
# cannot read, so one is refused unsent rather than retried as unreadable.
|
|
103
|
+
unless Bogon.address?(ip)
|
|
104
|
+
raise Error.new(:bad_request, "#{ip.inspect} is not an IP address: look an AS up with lookup_asn " \
|
|
105
|
+
'and a domain with lookup_domain')
|
|
106
|
+
end
|
|
107
|
+
return Bogon.result(ip) if Bogon.bogon?(ip)
|
|
108
|
+
|
|
109
|
+
loop do
|
|
110
|
+
hit = @cache&.get(ip)
|
|
111
|
+
return hit unless hit.nil?
|
|
112
|
+
|
|
113
|
+
flight, leader = board(ip)
|
|
114
|
+
return fetch(ip, retries, timeout) if flight.nil?
|
|
115
|
+
|
|
116
|
+
if leader
|
|
117
|
+
# Read once more now the address is boarded: a leader that landed
|
|
118
|
+
# between the miss above and boarding has already cached its answer.
|
|
119
|
+
hit = @cache.get(ip)
|
|
120
|
+
return unboard(ip, flight, :served, hit) unless hit.nil?
|
|
121
|
+
|
|
122
|
+
return lead(ip, flight) { fetch(ip, retries, timeout) }
|
|
123
|
+
end
|
|
124
|
+
state, value = flight.wait
|
|
125
|
+
return value if state == :served
|
|
126
|
+
raise value if state == :failed
|
|
127
|
+
# :abandoned - its leader never finished, so this caller asks again.
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# What every database says about one AS. Sent as given, so `9009` and
|
|
132
|
+
# `"AS9009"` ask the same; one the API cannot read is a `:bad_request`. Not
|
|
133
|
+
# cached.
|
|
134
|
+
#
|
|
135
|
+
# @param retries [Integer, nil] extra attempts, for THIS call only.
|
|
136
|
+
# @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
|
|
137
|
+
# @return [AsnResult]
|
|
138
|
+
def lookup_asn(asn, retries: nil, timeout: nil)
|
|
139
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
140
|
+
Retries.with_retries(retries || @retries) do
|
|
141
|
+
Transport.asn_result(@transport.asn_request(asn, timeout: timeout).run)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# What every database says about one domain. Sent as given: the API reads it
|
|
146
|
+
# in any case and answers an internationalized name under its punycode
|
|
147
|
+
# spelling, and one it cannot read is a `:bad_request`. Not cached.
|
|
148
|
+
#
|
|
149
|
+
# @param retries [Integer, nil] extra attempts, for THIS call only.
|
|
150
|
+
# @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
|
|
151
|
+
# @return [DomainResult]
|
|
152
|
+
def lookup_domain(domain, retries: nil, timeout: nil)
|
|
153
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
154
|
+
Retries.with_retries(retries || @retries) do
|
|
155
|
+
Transport.domain_result(@transport.domain_request(domain, timeout: timeout).run)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# What every database says about the address this client is calling from.
|
|
160
|
+
#
|
|
161
|
+
# The same answer {#lookup} would give for that address, at the same cost
|
|
162
|
+
# against your allowance. The address is the one our edge observed, so a
|
|
163
|
+
# call made through a proxy or a VPN reports the exit it left through -
|
|
164
|
+
# usually the point of asking.
|
|
165
|
+
#
|
|
166
|
+
# Deliberately NOT cached. The cache is keyed by address, and which address
|
|
167
|
+
# this is IS the question: a machine that moves between networks would
|
|
168
|
+
# otherwise be told where it used to be.
|
|
169
|
+
#
|
|
170
|
+
# @return [Result]
|
|
171
|
+
def my_ip(retries: nil, timeout: nil)
|
|
172
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
173
|
+
Retries.with_retries(retries || @retries) do
|
|
174
|
+
Transport.lookup_result(@transport.myip_request(timeout: timeout).run)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# What this client's key is entitled to, and how much of it has been used.
|
|
179
|
+
#
|
|
180
|
+
# Named for what it answers rather than `me`, which sits one letter from
|
|
181
|
+
# {#my_ip} and means something quite different: one is which address you are
|
|
182
|
+
# calling FROM, the other is what the key you are calling WITH may spend.
|
|
183
|
+
#
|
|
184
|
+
# Unlike a lookup there is no useful unauthenticated answer, so a client
|
|
185
|
+
# built without an API key gets an unauthorized error rather than a partial
|
|
186
|
+
# one.
|
|
187
|
+
#
|
|
188
|
+
# Usage counts against the ALLOWANCE WINDOW - on a paid plan the
|
|
189
|
+
# anniversary of the subscription, not the calendar month and not the
|
|
190
|
+
# billing period, and on the free plan the UTC day - and it is the same
|
|
191
|
+
# number a lookup is gated on. It can lag by a few seconds, because
|
|
192
|
+
# requests are counted in memory and flushed in aggregate.
|
|
193
|
+
#
|
|
194
|
+
# Deliberately NOT cached: the whole point is what has been spent, and a
|
|
195
|
+
# cached answer is a wrong one within seconds of the next request.
|
|
196
|
+
#
|
|
197
|
+
# @return [Entitlement]
|
|
198
|
+
def my_entitlement(retries: nil, timeout: nil)
|
|
199
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
200
|
+
Retries.with_retries(retries || @retries) do
|
|
201
|
+
Transport.entitlement_result(@transport.entitlement_request(timeout: timeout).run)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Look many addresses up in as few requests as possible.
|
|
206
|
+
#
|
|
207
|
+
# Bogons are answered locally and cached answers are reused; everything else
|
|
208
|
+
# goes to the batch endpoint in chunks of up to 1000 addresses, with at most
|
|
209
|
+
# `concurrency` chunks in flight. Keyed by address rather than positional, so
|
|
210
|
+
# duplicates in the input collapse to a single entry and the caller never has
|
|
211
|
+
# to line two lists up. An address that fails carries its error as its value,
|
|
212
|
+
# so one bad entry cannot lose the rest of the answers: the API reports a
|
|
213
|
+
# per-entry failure with the status the single lookup would have answered,
|
|
214
|
+
# and a chunk that fails as a whole marks every address in it. The batch
|
|
215
|
+
# endpoint needs a paid plan: without a key every address in a request of
|
|
216
|
+
# two or more carries an `:unauthorized` error, and a free-plan key's
|
|
217
|
+
# request of two or more a `:bad_request` (HTTP 402). A request of one
|
|
218
|
+
# address is answered as its lookup would be.
|
|
219
|
+
#
|
|
220
|
+
# @param concurrency [Integer, nil] chunks in flight, for THIS batch only.
|
|
221
|
+
# Anything but an Integer from 1 is refused as `:bad_request` before any
|
|
222
|
+
# request, and before a bogon or a cached answer is returned.
|
|
223
|
+
# @param retries [Integer, nil] extra attempts for a failed chunk, for THIS batch only.
|
|
224
|
+
# @param timeout [Numeric, nil] seconds each chunk's attempt may take, for THIS batch only.
|
|
225
|
+
# @return [Hash{String => Result, Error}] in the order the addresses were given
|
|
226
|
+
def lookup_batch(ips, concurrency: nil, retries: nil, timeout: nil)
|
|
227
|
+
limit = concurrency || @concurrency
|
|
228
|
+
unless concurrency?(limit)
|
|
229
|
+
raise Error.new(:bad_request, "concurrency must be an Integer from 1, not #{limit.inspect}")
|
|
230
|
+
end
|
|
231
|
+
Transport.checked_timeout(timeout) unless timeout.nil?
|
|
232
|
+
Retries.checked_count(retries) unless retries.nil?
|
|
233
|
+
|
|
234
|
+
# An IPv4-mapped address is sent as the address it carries, once however
|
|
235
|
+
# many of its spellings were asked, and answered under each one asked.
|
|
236
|
+
asked = ips.to_a.uniq
|
|
237
|
+
addresses = asked.map { |ip| Bogon.unmapped(ip) }.uniq
|
|
238
|
+
answers = {}
|
|
239
|
+
pending = []
|
|
240
|
+
|
|
241
|
+
addresses.each do |ip|
|
|
242
|
+
hit = Bogon.bogon?(ip) ? Bogon.result(ip) : @cache&.get(ip)
|
|
243
|
+
hit.nil? ? pending << ip : answers[ip] = hit
|
|
244
|
+
end
|
|
245
|
+
# Boarded before any chunk is built, so a lookup arriving meanwhile waits
|
|
246
|
+
# for this batch; an address already in flight is not sent again.
|
|
247
|
+
boarded = {}
|
|
248
|
+
joined = {}
|
|
249
|
+
pending.each do |ip|
|
|
250
|
+
flight, leader = board(ip)
|
|
251
|
+
if leader == false
|
|
252
|
+
joined[ip] = flight
|
|
253
|
+
else
|
|
254
|
+
boarded[ip] = flight
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
begin
|
|
258
|
+
unless boarded.empty?
|
|
259
|
+
chunks = boarded.keys.each_slice(BATCH_MAX).to_a
|
|
260
|
+
run_batch(chunks, answers, limit, retries || @retries, timeout, boarded)
|
|
261
|
+
end
|
|
262
|
+
ensure
|
|
263
|
+
boarded.each { |ip, flight| unboard(ip, flight, :abandoned) unless flight.nil? }
|
|
264
|
+
end
|
|
265
|
+
joined.each { |ip, flight| answers[ip] = joined_answer(ip, flight, retries, timeout) }
|
|
266
|
+
|
|
267
|
+
# Reinstated in input order: a hydra settles in completion order, and a
|
|
268
|
+
# caller iterating the hash should see what they passed in.
|
|
269
|
+
asked.to_h { |ip| [ip, answers[Bogon.unmapped(ip)]] }
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
private
|
|
273
|
+
|
|
274
|
+
# Whether `value` can be a batch's concurrency: chunks in flight are counted
|
|
275
|
+
# whole, and a hydra allowed fewer than one runs nothing.
|
|
276
|
+
def concurrency?(value)
|
|
277
|
+
value.is_a?(Integer) && value >= 1
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Concurrent misses for one address share ONE request. The first miss boards
|
|
281
|
+
# the address and every miss after it, a batch's included, waits on its
|
|
282
|
+
# Flight.
|
|
283
|
+
class Flight
|
|
284
|
+
def initialize
|
|
285
|
+
@lock = Mutex.new
|
|
286
|
+
@landed = ConditionVariable.new
|
|
287
|
+
@state = :pending
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Settles the flight once, for every waiter: :served with a Result,
|
|
291
|
+
# :failed with an Error, or :abandoned when its leader never finished.
|
|
292
|
+
def land(state, value = nil)
|
|
293
|
+
@lock.synchronize do
|
|
294
|
+
next unless @state == :pending
|
|
295
|
+
|
|
296
|
+
@state = state
|
|
297
|
+
@value = value
|
|
298
|
+
@landed.broadcast
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Blocks until the flight lands, then [state, value].
|
|
303
|
+
def wait
|
|
304
|
+
@lock.synchronize do
|
|
305
|
+
@landed.wait(@lock) while @state == :pending
|
|
306
|
+
[@state, @value]
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# [flight, true] when this caller leads the request for `ip`, [flight,
|
|
312
|
+
# false] when one is already in flight, and nil without a cache: every
|
|
313
|
+
# lookup is then served, so nothing is shared.
|
|
314
|
+
def board(ip)
|
|
315
|
+
return nil if @cache.nil?
|
|
316
|
+
|
|
317
|
+
@flights_lock.synchronize do
|
|
318
|
+
existing = @flights[ip]
|
|
319
|
+
existing ? [existing, false] : [@flights[ip] = Flight.new, true]
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Takes `ip` off the board and lands its flight. Returns `value`.
|
|
324
|
+
def unboard(ip, flight, state, value = nil)
|
|
325
|
+
@flights_lock.synchronize { @flights.delete(ip) if @flights[ip].equal?(flight) }
|
|
326
|
+
flight.land(state, value)
|
|
327
|
+
value
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Runs the request a flight is waiting on, under the options of the call
|
|
331
|
+
# that leads it. A served answer is cached before it lands; a failure
|
|
332
|
+
# reaches every waiter and is cached for none; and a leader that never
|
|
333
|
+
# finishes - a killed thread, a Timeout.timeout - abandons the flight, so
|
|
334
|
+
# its waiters ask again rather than failing with it.
|
|
335
|
+
def lead(ip, flight)
|
|
336
|
+
unboard(ip, flight, :served, yield)
|
|
337
|
+
rescue Error => e
|
|
338
|
+
unboard(ip, flight, :failed, e)
|
|
339
|
+
raise
|
|
340
|
+
ensure
|
|
341
|
+
unboard(ip, flight, :abandoned)
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def fetch(ip, retries, timeout)
|
|
345
|
+
result = Retries.with_retries(retries || @retries) do
|
|
346
|
+
Transport.lookup_result(@transport.lookup_request(ip, timeout: timeout).run)
|
|
347
|
+
end
|
|
348
|
+
@cache&.set(ip, result)
|
|
349
|
+
result
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# One hydra per call, sized for THIS call. Reusing an instance-level hydra
|
|
353
|
+
# would silently cap a per-call concurrency at the client's setting, and
|
|
354
|
+
# would not be safe to drive from two threads either. Each request is one
|
|
355
|
+
# chunk of up to 1000 addresses.
|
|
356
|
+
def run_batch(chunks, answers, concurrency, retries, timeout, boarded)
|
|
357
|
+
hydra = Typhoeus::Hydra.new(max_concurrency: concurrency)
|
|
358
|
+
attempts = Hash.new(0)
|
|
359
|
+
|
|
360
|
+
enqueue = lambda do |chunk|
|
|
361
|
+
request = @transport.batch_request(chunk, timeout: timeout)
|
|
362
|
+
request.on_complete do |response|
|
|
363
|
+
outcome = settle(chunk, response, attempts, retries, enqueue)
|
|
364
|
+
outcome&.each do |ip, answer|
|
|
365
|
+
answers[ip] = answer
|
|
366
|
+
flight = boarded[ip]
|
|
367
|
+
unboard(ip, flight, answer.is_a?(Result) ? :served : :failed, answer) unless flight.nil?
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
hydra.queue(request)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
chunks.each { |chunk| enqueue.call(chunk) }
|
|
374
|
+
hydra.run
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# One POST /batch, mapped back onto the addresses it was asked about. A
|
|
378
|
+
# chunk-level failure - the call refused, the transport failing, the retries
|
|
379
|
+
# exhausted - becomes every address's error, exactly as it would have been
|
|
380
|
+
# had each been looked up alone.
|
|
381
|
+
def settle(chunk, response, attempts, retries, enqueue)
|
|
382
|
+
body = Transport.batch_body(response)
|
|
383
|
+
chunk.to_h do |ip|
|
|
384
|
+
answer = batch_answer(ip, body)
|
|
385
|
+
@cache&.set(ip, answer) if answer.is_a?(Result)
|
|
386
|
+
[ip, answer]
|
|
387
|
+
end
|
|
388
|
+
rescue Error => e
|
|
389
|
+
return chunk.to_h { |ip| [ip, e] } unless e.retryable? && attempts[chunk] < retries
|
|
390
|
+
|
|
391
|
+
attempts[chunk] += 1
|
|
392
|
+
# Sleeping here stalls the whole hydra, which is what a server-supplied
|
|
393
|
+
# delay asks for: it is telling every request to this host to back off.
|
|
394
|
+
sleep(Retries.delay_for(e, attempts[chunk]))
|
|
395
|
+
enqueue.call(chunk)
|
|
396
|
+
nil
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# What a batch takes for an address another call was fetching: that call's
|
|
400
|
+
# answer or error, or, when its leader never finished, its own lookup.
|
|
401
|
+
def joined_answer(ip, flight, retries, timeout)
|
|
402
|
+
state, value = flight.wait
|
|
403
|
+
return value unless state == :abandoned
|
|
404
|
+
|
|
405
|
+
lookup(ip, retries: retries, timeout: timeout)
|
|
406
|
+
rescue Error => e
|
|
407
|
+
e
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# Every address lands in exactly one of `results` and `errors`; an address in
|
|
411
|
+
# neither, or with an entry that is not an answer, is the server breaking its
|
|
412
|
+
# own contract, and is reported as that address's error rather than lost.
|
|
413
|
+
def batch_answer(ip, body)
|
|
414
|
+
served = body['results'][ip]
|
|
415
|
+
failed = body['errors'][ip]
|
|
416
|
+
if Transport.conforms?(served, 'LookupResponse')
|
|
417
|
+
Result.new(served)
|
|
418
|
+
elsif Transport.conforms?(failed, 'BatchLookupError')
|
|
419
|
+
Error.from_entry(failed['status'], failed['error'])
|
|
420
|
+
else
|
|
421
|
+
Error.new(:server_error, "the batch answer carried no readable entry for #{ip}", status: 200)
|
|
422
|
+
end
|
|
423
|
+
end
|
|
44
424
|
end
|
|
45
425
|
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
=begin
|
|
2
2
|
#InternetData API
|
|
3
3
|
|
|
4
|
-
#The InternetData API: download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
|
|
4
|
+
#The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
|
|
5
5
|
|
|
6
|
-
The version of the OpenAPI document: 2026.11.
|
|
6
|
+
The version of the OpenAPI document: 2026.11.13
|
|
7
7
|
Contact: support@internetdata.io
|
|
8
8
|
Generated by: https://openapi-generator.tech
|
|
9
9
|
Generator version: 7.25.0
|
|
@@ -272,6 +272,27 @@ module InternetData
|
|
|
272
272
|
key: 'apikey',
|
|
273
273
|
value: api_key_with_prefix('apikey')
|
|
274
274
|
},
|
|
275
|
+
'bearerAuth' =>
|
|
276
|
+
{
|
|
277
|
+
type: 'bearer',
|
|
278
|
+
in: 'header',
|
|
279
|
+
key: 'Authorization',
|
|
280
|
+
value: "Bearer #{access_token_with_refresh}"
|
|
281
|
+
},
|
|
282
|
+
'apiKeyHeader' =>
|
|
283
|
+
{
|
|
284
|
+
type: 'api_key',
|
|
285
|
+
in: 'header',
|
|
286
|
+
key: 'X-Api-Key',
|
|
287
|
+
value: api_key_with_prefix('X-Api-Key')
|
|
288
|
+
},
|
|
289
|
+
'apiKeyQuery' =>
|
|
290
|
+
{
|
|
291
|
+
type: 'api_key',
|
|
292
|
+
in: 'query',
|
|
293
|
+
key: 'apikey',
|
|
294
|
+
value: api_key_with_prefix('apikey')
|
|
295
|
+
},
|
|
275
296
|
}
|
|
276
297
|
end
|
|
277
298
|
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module InternetData
|
|
4
|
+
# What a lookup of a domain answers.
|
|
5
|
+
#
|
|
6
|
+
# As with {Result}, an absent member is one your plan does not include, and
|
|
7
|
+
# reads as `nil`, never as false: Unlimited answers whether the domain is
|
|
8
|
+
# registered and which record types it has, and paid plans and the trial add
|
|
9
|
+
# the records themselves, its organization's IP ranges and Disposable Domain.
|
|
10
|
+
# Each flag has the three readers {Result}'s have: `is_disposable`,
|
|
11
|
+
# `disposable?` and `included?(:is_disposable)`.
|
|
12
|
+
class DomainResult
|
|
13
|
+
include Answer
|
|
14
|
+
|
|
15
|
+
# The domain, lowercase and in punycode.
|
|
16
|
+
def domain
|
|
17
|
+
@raw['domain']
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Whether it is a registered domain (Domain List). Every answer.
|
|
21
|
+
def is_registered
|
|
22
|
+
@raw['is_registered']
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# `is_registered` with an absent member read as false.
|
|
26
|
+
def registered?
|
|
27
|
+
@raw['is_registered'] == true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Whether it is a disposable email domain (Disposable Domain), or nil when
|
|
31
|
+
# your plan does not include it. Paid plans and the trial.
|
|
32
|
+
def is_disposable
|
|
33
|
+
@raw['is_disposable']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# `is_disposable` with an absent member read as false.
|
|
37
|
+
def disposable?
|
|
38
|
+
@raw['is_disposable'] == true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Whether it has A records. Every answer.
|
|
42
|
+
def has_a
|
|
43
|
+
@raw['has_a']
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# `has_a` with an absent member read as false.
|
|
47
|
+
def has_a?
|
|
48
|
+
@raw['has_a'] == true
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Whether it has AAAA records. Every answer.
|
|
52
|
+
def has_aaaa
|
|
53
|
+
@raw['has_aaaa']
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# `has_aaaa` with an absent member read as false.
|
|
57
|
+
def has_aaaa?
|
|
58
|
+
@raw['has_aaaa'] == true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Whether it has MX records. Every answer.
|
|
62
|
+
def has_mx
|
|
63
|
+
@raw['has_mx']
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# `has_mx` with an absent member read as false.
|
|
67
|
+
def has_mx?
|
|
68
|
+
@raw['has_mx'] == true
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Whether it has NS records. Every answer.
|
|
72
|
+
def has_ns
|
|
73
|
+
@raw['has_ns']
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# `has_ns` with an absent member read as false.
|
|
77
|
+
def has_ns?
|
|
78
|
+
@raw['has_ns'] == true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Whether it has a CNAME record. Every answer.
|
|
82
|
+
def has_cname
|
|
83
|
+
@raw['has_cname']
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# `has_cname` with an absent member read as false.
|
|
87
|
+
def has_cname?
|
|
88
|
+
@raw['has_cname'] == true
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Its records in Domain DNS, up to 100 a type, as a wire hash
|
|
92
|
+
# (`result.dns['mx']`). Paid plans and the trial.
|
|
93
|
+
def dns
|
|
94
|
+
@raw['dns']
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Its organization's IP ranges, from Domain IP Ranges. Paid plans and the
|
|
98
|
+
# trial.
|
|
99
|
+
def ip_ranges
|
|
100
|
+
@raw['ip_ranges']
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Detail for `is_disposable`, `{}` when it is false. Paid plans and the
|
|
104
|
+
# trial.
|
|
105
|
+
def disposable
|
|
106
|
+
@raw['disposable']
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# On answers to a key on the free plan only: false while its organization's
|
|
110
|
+
# daily trial lasts, true once the answer has Unlimited's; nil on any other.
|
|
111
|
+
def trial_expired
|
|
112
|
+
@raw['trial_expired']
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# `trial_expired` with an absent member read as false.
|
|
116
|
+
def trial_expired?
|
|
117
|
+
@raw['trial_expired'] == true
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def inspect
|
|
121
|
+
"#<#{self.class} domain=#{domain.inspect} is_registered=#{is_registered.inspect}>"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
data/lib/internetdata/errors.rb
CHANGED
|
@@ -21,6 +21,8 @@ module InternetData
|
|
|
21
21
|
# The API's machine-readable result code, when the refusal carried one:
|
|
22
22
|
# `NOT_LICENSED`, `UNKNOWN_DATASET`, `NOT_AVAILABLE` and so on. Deliberately
|
|
23
23
|
# not an enum, so a code added later stays readable by a client built today.
|
|
24
|
+
# A database call's refusal carries one; a lookup's carries none, and its
|
|
25
|
+
# message is the API's own words.
|
|
24
26
|
attr_reader :kind, :status, :rc, :retry_after_seconds
|
|
25
27
|
|
|
26
28
|
def initialize(kind, message, status: nil, rc: nil, retry_after_seconds: nil)
|
|
@@ -41,7 +43,7 @@ module InternetData
|
|
|
41
43
|
# from it is classified on the status alone.
|
|
42
44
|
def self.from_status(status, headers, body, message: nil)
|
|
43
45
|
rc = rc_of(body)
|
|
44
|
-
message ||=
|
|
46
|
+
message ||= message_of(body) || "request failed with status #{status}"
|
|
45
47
|
retry_after = parse_retry_after(header(headers, 'retry-after'))
|
|
46
48
|
kind = kind_for(status, headers)
|
|
47
49
|
|
|
@@ -73,6 +75,13 @@ module InternetData
|
|
|
73
75
|
new(:network, message)
|
|
74
76
|
end
|
|
75
77
|
|
|
78
|
+
# A per-entry failure inside a successful batch: the status the single lookup
|
|
79
|
+
# would have answered, and its message, with no headers at all - so a 429
|
|
80
|
+
# here is a spent allowance, which is the only kind the API puts in an entry.
|
|
81
|
+
def self.from_entry(status, message)
|
|
82
|
+
from_status(status.to_i, {}, nil, message: message.to_s)
|
|
83
|
+
end
|
|
84
|
+
|
|
76
85
|
# Case-insensitive, and works with a plain Hash as well as with the
|
|
77
86
|
# case-blind header object Typhoeus builds from a live response.
|
|
78
87
|
def self.header(headers, name)
|
|
@@ -83,16 +92,30 @@ module InternetData
|
|
|
83
92
|
value.is_a?(Array) ? value.last : value
|
|
84
93
|
end
|
|
85
94
|
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
95
|
+
# The two APIs behind this host answer with different envelopes: the lookup
|
|
96
|
+
# endpoints use `error`, the database endpoints `rc`. Both are read here so a
|
|
97
|
+
# caller never has to know which one they hit. An intermediary's HTML error
|
|
98
|
+
# page parses as neither and leaves the status to speak for itself.
|
|
99
|
+
def self.message_of(body)
|
|
100
|
+
parsed = parse_body(body)
|
|
101
|
+
return nil unless parsed.is_a?(Hash)
|
|
102
|
+
|
|
103
|
+
message = parsed['error'] || parsed['rc']
|
|
104
|
+
message.is_a?(String) && !message.empty? ? message : nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A database refusal's `{"rc": "..."}` code; a lookup refusal carries none.
|
|
89
108
|
def self.rc_of(body)
|
|
90
|
-
parsed =
|
|
109
|
+
parsed = parse_body(body)
|
|
91
110
|
return nil unless parsed.is_a?(Hash)
|
|
92
111
|
|
|
93
112
|
parsed['rc'].is_a?(String) && !parsed['rc'].empty? ? parsed['rc'] : nil
|
|
94
113
|
end
|
|
95
114
|
|
|
115
|
+
def self.parse_body(body)
|
|
116
|
+
body.is_a?(String) ? (JSON.parse(body) rescue nil) : body
|
|
117
|
+
end
|
|
118
|
+
|
|
96
119
|
# Seconds as digits, or an HTTP date, and nothing else: `Float()` would also
|
|
97
120
|
# read `0x10`, `1_0` and `1e3` as a wait.
|
|
98
121
|
def self.parse_retry_after(value)
|
|
@@ -109,7 +132,7 @@ module InternetData
|
|
|
109
132
|
[0, (when_at - Time.now).ceil].max
|
|
110
133
|
end
|
|
111
134
|
|
|
112
|
-
private_class_method :header, :rc_of, :parse_retry_after
|
|
135
|
+
private_class_method :header, :message_of, :rc_of, :parse_body, :parse_retry_after
|
|
113
136
|
end
|
|
114
137
|
|
|
115
138
|
# The authorization server refusing an OAuth request: a 4xx whose body names an
|