vpndetection 5.19.0 → 5.20.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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -0
  3. data/lib/vpndetection/api/account_wire_api.rb +1 -1
  4. data/lib/vpndetection/api/api_keys_wire_api.rb +1 -1
  5. data/lib/vpndetection/api/authorization_wire_api.rb +1 -1
  6. data/lib/vpndetection/api/database_wire_api.rb +1 -1
  7. data/lib/vpndetection/api/entitlement_wire_api.rb +1 -1
  8. data/lib/vpndetection/api/lookup_wire_api.rb +1 -1
  9. data/lib/vpndetection/api/organization_wire_api.rb +1 -1
  10. data/lib/vpndetection/api_client.rb +1 -1
  11. data/lib/vpndetection/api_error.rb +1 -1
  12. data/lib/vpndetection/api_model_base.rb +1 -1
  13. data/lib/vpndetection/client.rb +78 -41
  14. data/lib/vpndetection/configuration.rb +1 -1
  15. data/lib/vpndetection/middleware/core.rb +61 -5
  16. data/lib/vpndetection/models/abuse_contact.rb +1 -1
  17. data/lib/vpndetection/models/account_create_apikey_request.rb +1 -1
  18. data/lib/vpndetection/models/account_created_apikey.rb +1 -1
  19. data/lib/vpndetection/models/account_org.rb +1 -1
  20. data/lib/vpndetection/models/account_org_ref.rb +1 -1
  21. data/lib/vpndetection/models/account_org_wrap.rb +1 -1
  22. data/lib/vpndetection/models/account_rc.rb +1 -1
  23. data/lib/vpndetection/models/account_revealed_apikey.rb +1 -1
  24. data/lib/vpndetection/models/account_user.rb +1 -1
  25. data/lib/vpndetection/models/apikey_detail.rb +1 -1
  26. data/lib/vpndetection/models/apikey_list.rb +1 -1
  27. data/lib/vpndetection/models/asn.rb +1 -1
  28. data/lib/vpndetection/models/batch_lookup_error.rb +1 -1
  29. data/lib/vpndetection/models/batch_lookup_request.rb +1 -1
  30. data/lib/vpndetection/models/batch_lookup_response.rb +1 -1
  31. data/lib/vpndetection/models/bit_torrent_detail.rb +1 -1
  32. data/lib/vpndetection/models/bot_detail.rb +3 -3
  33. data/lib/vpndetection/models/carrier_detail.rb +1 -1
  34. data/lib/vpndetection/models/class_detail.rb +1 -1
  35. data/lib/vpndetection/models/client_registration.rb +1 -1
  36. data/lib/vpndetection/models/client_registration_request.rb +1 -1
  37. data/lib/vpndetection/models/company.rb +1 -1
  38. data/lib/vpndetection/models/database.rb +1 -1
  39. data/lib/vpndetection/models/database_checksums_response.rb +1 -1
  40. data/lib/vpndetection/models/database_format.rb +1 -1
  41. data/lib/vpndetection/models/database_format_size.rb +1 -1
  42. data/lib/vpndetection/models/database_list.rb +1 -1
  43. data/lib/vpndetection/models/database_metadata.rb +1 -1
  44. data/lib/vpndetection/models/database_metadata_column.rb +1 -1
  45. data/lib/vpndetection/models/database_version.rb +1 -1
  46. data/lib/vpndetection/models/db_checksums.rb +1 -1
  47. data/lib/vpndetection/models/device_authorization.rb +1 -1
  48. data/lib/vpndetection/models/dns_resolver_detail.rb +1 -1
  49. data/lib/vpndetection/models/download.rb +1 -1
  50. data/lib/vpndetection/models/download_list.rb +1 -1
  51. data/lib/vpndetection/models/entitlement.rb +1 -1
  52. data/lib/vpndetection/models/entitlement_apikey.rb +1 -1
  53. data/lib/vpndetection/models/entitlement_error.rb +1 -1
  54. data/lib/vpndetection/models/entitlement_plan.rb +1 -1
  55. data/lib/vpndetection/models/entitlement_usage.rb +1 -1
  56. data/lib/vpndetection/models/error_envelope.rb +1 -1
  57. data/lib/vpndetection/models/identity.rb +1 -1
  58. data/lib/vpndetection/models/location.rb +1 -1
  59. data/lib/vpndetection/models/lookup_error.rb +1 -1
  60. data/lib/vpndetection/models/lookup_response.rb +1 -1
  61. data/lib/vpndetection/models/oauth_error.rb +1 -1
  62. data/lib/vpndetection/models/oauth_metadata.rb +1 -1
  63. data/lib/vpndetection/models/proxy_detail.rb +1 -1
  64. data/lib/vpndetection/models/pubproxy_detail.rb +1 -1
  65. data/lib/vpndetection/models/satellite_detail.rb +1 -1
  66. data/lib/vpndetection/models/smtp_detail.rb +1 -1
  67. data/lib/vpndetection/models/ssh_detail.rb +1 -1
  68. data/lib/vpndetection/models/standing.rb +1 -1
  69. data/lib/vpndetection/models/token_response.rb +1 -1
  70. data/lib/vpndetection/models/user_agent_verdict.rb +1 -1
  71. data/lib/vpndetection/models/vpn_detail.rb +1 -1
  72. data/lib/vpndetection/models/web_detail.rb +1 -1
  73. data/lib/vpndetection/result.rb +13 -0
  74. data/lib/vpndetection/transport.rb +26 -5
  75. data/lib/vpndetection/version.rb +1 -1
  76. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d37af7f65d9c768211355a3d46eec4e9ffd89cbc58dc8f808528befbad1f5aa7
4
- data.tar.gz: 110014ae00c5afe6796c553a1c4254f095caaa7ca6e5d21cbe56471f87877fac
3
+ metadata.gz: 0752e89f4d6355b042703b60a37c1c9e27d9d4d11d62faced4cf9adb29009b02
4
+ data.tar.gz: 0cbc519247f2cddc8bf2f79ba5b9b8f81f96f423aec1490ae885c69b22ee64e7
5
5
  SHA512:
6
- metadata.gz: b09e4e967731a1acbcf163515d9c4b1089c398ab748efa645209714e167607371b75ad9a5e4ccede040f2476a44fc9687f068d960e773e27128a52c957ee54c8
7
- data.tar.gz: 21baa1cb5afead61a6f0d98e6dc2794f577293550bae02aa65707ce27cc5cbe4415ffe64694a8f26443a21a1113993c2613b3bcc82c03d9b6b27ffdb6ece658f
6
+ metadata.gz: 88105b406ef6df997ce3a204f7d9b564810302e818d97bbec0238dacdebae40c9deb1ea1b2f3532de025e22fd8d2e7d3d07f7b8cf8d3886a78f4258d9e0d74d5
7
+ data.tar.gz: c779276341271b701c9e1b7ed3a3f9fd422bac442a18af256197e566c846452dee577129d5cd5d725c8094045b02fb0d36dcc3dc10cb348b1aa710c64d5b1f03
data/README.md CHANGED
@@ -72,6 +72,19 @@ Usage counts against the anniversary of your subscription on a paid plan, not th
72
72
 
73
73
  Show `plan.name` and compare against `plan.key`: a plan's name can change, and its key never does. A value the API adds later to a plan's `tier`, a database's `standing` or `license_type`, a format or a download's `outcome` comes back as the string it served (from 5.16.0), so check for the values you know rather than assuming the list is complete.
74
74
 
75
+ ### Is that crawler who it says it is?
76
+
77
+ Pass the `User-Agent` of the request you are deciding on, and with every field (a paid plan, or a free account's trial) the answer says whose agent it names and whether this address bears that out:
78
+
79
+ ```ruby
80
+ result = client.lookup('66.249.66.1', user_agent: request.user_agent)
81
+ result.user_agent # => {"agent"=>"Googlebot", "kind"=>"bot", "verdict"=>"verified"}
82
+
83
+ results = client.lookup_batch(ips, user_agents: { '66.249.66.1' => request.user_agent })
84
+ ```
85
+
86
+ `verdict` is `verified` where the agent's operator vouches for the address, `spoofed` where it does not, `unverifiable` for a bot nothing published can confirm, and `not_bot` for a browser, an app or a script. `my_ip` takes `user_agent:` too. A batch's `user_agents` is keyed by each address as you list it, and an address with no agent, or an empty one, is looked up plainly. An answer with an agent is cached apart from the address's plain answer and from another agent's. Unlimited's answers, a private address's and a lookup that sent none carry no `user_agent` (`nil`).
87
+
75
88
  ### Batch lookup
76
89
 
77
90
  Look up as many addresses as you like at once. Bogons and cached answers are handled locally, and everything else goes to the batch endpoint in chunks of up to 1000 addresses, in parallel. The batch endpoint needs a paid plan:
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -85,28 +85,37 @@ module VPNDetection
85
85
  #
86
86
  # @param retries [Integer, nil] extra attempts, for THIS call only.
87
87
  # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
88
- def lookup(ip, retries: nil, timeout: nil)
88
+ # @param user_agent [String, nil] the `User-Agent` of the request you are
89
+ # deciding on. With every field (a paid plan, or the trial) the answer then
90
+ # carries {Result#user_agent}: the agent it names, and whether this address
91
+ # bears that out. An empty one is none. An answer with one is cached apart
92
+ # from the address's plain answer and from another agent's. A bogon is still
93
+ # answered locally, with no `user_agent`.
94
+ # @raise [ArgumentError] for a `user_agent` that is not a String.
95
+ def lookup(ip, retries: nil, timeout: nil, user_agent: nil)
89
96
  # Here as well as in the transport: a bogon or a cached answer returns before any request.
90
97
  Transport.checked_timeout(timeout) unless timeout.nil?
91
98
  Retries.checked_count(retries) unless retries.nil?
99
+ agent = Transport.checked_user_agent(user_agent)
92
100
  # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
93
101
  ip = Bogon.unmapped(ip)
94
102
  return Bogon.result(ip) if Bogon.bogon?(ip)
95
103
 
104
+ key = key_of(ip, agent)
96
105
  loop do
97
- hit = @cache&.get(ip)
106
+ hit = @cache&.get(key)
98
107
  return hit unless hit.nil?
99
108
 
100
- flight, leader = board(ip)
101
- return fetch(ip, retries, timeout) if flight.nil?
109
+ flight, leader = board(key)
110
+ return fetch(ip, agent, retries, timeout) if flight.nil?
102
111
 
103
112
  if leader
104
113
  # Read once more now the address is boarded: a leader that landed
105
114
  # between the miss above and boarding has already cached its answer.
106
- hit = @cache.get(ip)
107
- return unboard(ip, flight, :served, hit) unless hit.nil?
115
+ hit = @cache.get(key)
116
+ return unboard(key, flight, :served, hit) unless hit.nil?
108
117
 
109
- return lead(ip, flight) { fetch(ip, retries, timeout) }
118
+ return lead(key, flight) { fetch(ip, agent, retries, timeout) }
110
119
  end
111
120
  state, value = flight.wait
112
121
  return value if state == :served
@@ -125,10 +134,13 @@ module VPNDetection
125
134
  # Deliberately NOT cached. The cache is keyed by address, and which address
126
135
  # this is IS the question: a machine that moves between networks would
127
136
  # otherwise be told where it used to be.
128
- def my_ip(retries: nil, timeout: nil)
137
+ #
138
+ # @param user_agent [String, nil] as {#lookup} takes it.
139
+ def my_ip(retries: nil, timeout: nil, user_agent: nil)
129
140
  Retries.checked_count(retries) unless retries.nil?
141
+ agent = Transport.checked_user_agent(user_agent)
130
142
  Retries.with_retries(retries || @retries) do
131
- Transport.lookup_result(@transport.myip_request(timeout: timeout).run)
143
+ Transport.lookup_result(@transport.myip_request(timeout: timeout, user_agent: agent).run)
132
144
  end
133
145
  end
134
146
 
@@ -179,32 +191,47 @@ module VPNDetection
179
191
  # request, and before a bogon or a cached answer is returned.
180
192
  # @param retries [Integer, nil] extra attempts for a failed chunk, for THIS batch only.
181
193
  # @param timeout [Numeric, nil] seconds each chunk's attempt may take, for THIS batch only.
194
+ # @param user_agents [Hash{String => String}, nil] the `User-Agent` to send
195
+ # with each address, keyed by the address as given in `ips`: as
196
+ # {#lookup}'s `user_agent:`, per address. An address with none, or an empty
197
+ # one, is looked up plainly. Where two spellings of one IPv4-mapped address
198
+ # carry different agents, the first given is sent.
199
+ # @raise [ArgumentError] for `user_agents` that is not a Hash, or an agent in
200
+ # it that is not a String.
182
201
  # @return [Hash{String => Result, Error}] in the order the addresses were given
183
- def lookup_batch(ips, concurrency: nil, retries: nil, timeout: nil)
202
+ def lookup_batch(ips, concurrency: nil, retries: nil, timeout: nil, user_agents: nil)
184
203
  limit = concurrency || @concurrency
185
204
  unless concurrency?(limit)
186
205
  raise Error.new(:bad_request, "concurrency must be an Integer from 1, not #{limit.inspect}")
187
206
  end
188
207
  Transport.checked_timeout(timeout) unless timeout.nil?
189
208
  Retries.checked_count(retries) unless retries.nil?
209
+ unless user_agents.nil? || user_agents.is_a?(Hash)
210
+ raise ArgumentError, "user_agents must be a Hash of address to agent, not #{user_agents.inspect}"
211
+ end
190
212
 
191
213
  # An IPv4-mapped address is sent as the address it carries, once however
192
214
  # many of its spellings were asked, and answered under each one asked.
193
215
  asked = ips.to_a.uniq
216
+ agents = {}
217
+ asked.each do |ip|
218
+ agent = Transport.checked_user_agent(user_agents&.[](ip))
219
+ agents[Bogon.unmapped(ip)] ||= agent unless agent.nil?
220
+ end
194
221
  addresses = asked.map { |ip| Bogon.unmapped(ip) }.uniq
195
222
  answers = {}
196
223
  pending = []
197
224
 
198
225
  addresses.each do |ip|
199
- hit = Bogon.bogon?(ip) ? Bogon.result(ip) : @cache&.get(ip)
226
+ hit = Bogon.bogon?(ip) ? Bogon.result(ip) : @cache&.get(key_of(ip, agents[ip]))
200
227
  hit.nil? ? pending << ip : answers[ip] = hit
201
228
  end
202
229
  # Boarded before any chunk is built, so a lookup arriving meanwhile waits
203
- # for this batch; an address already in flight is not sent again.
230
+ # for this batch; an address and agent already in flight is not sent again.
204
231
  boarded = {}
205
232
  joined = {}
206
233
  pending.each do |ip|
207
- flight, leader = board(ip)
234
+ flight, leader = board(key_of(ip, agents[ip]))
208
235
  if leader == false
209
236
  joined[ip] = flight
210
237
  else
@@ -214,12 +241,12 @@ module VPNDetection
214
241
  begin
215
242
  unless boarded.empty?
216
243
  chunks = boarded.keys.each_slice(BATCH_MAX).to_a
217
- run_batch(chunks, answers, limit, retries || @retries, timeout, boarded)
244
+ run_batch(chunks, agents, answers, limit, retries || @retries, timeout, boarded)
218
245
  end
219
246
  ensure
220
- boarded.each { |ip, flight| unboard(ip, flight, :abandoned) unless flight.nil? }
247
+ boarded.each { |ip, flight| unboard(key_of(ip, agents[ip]), flight, :abandoned) unless flight.nil? }
221
248
  end
222
- joined.each { |ip, flight| answers[ip] = joined_answer(ip, flight, retries, timeout) }
249
+ joined.each { |ip, flight| answers[ip] = joined_answer(ip, agents[ip], flight, retries, timeout) }
223
250
 
224
251
  # Reinstated in input order: a hydra settles in completion order, and a
225
252
  # caller iterating the hash should see what they passed in.
@@ -234,6 +261,13 @@ module VPNDetection
234
261
  value.is_a?(Integer) && value >= 1
235
262
  end
236
263
 
264
+ # What an answer is cached and shared under: the address alone, or the
265
+ # address and the agent sent with it, since that answer carries a verdict
266
+ # the plain one does not.
267
+ def key_of(ip, agent)
268
+ agent.nil? ? ip : [ip, agent]
269
+ end
270
+
237
271
  # Concurrent misses for one address share ONE request: 5.4.2 sent one per
238
272
  # calling thread. The first miss boards the
239
273
  # address and every miss after it, a batch's included, waits on its Flight.
@@ -265,21 +299,21 @@ module VPNDetection
265
299
  end
266
300
  end
267
301
 
268
- # [flight, true] when this caller leads the request for `ip`, [flight,
269
- # false] when one is already in flight, and nil without a cache: every
270
- # lookup is then served, so nothing is shared.
271
- def board(ip)
302
+ # [flight, true] when this caller leads the request for `key`, an address
303
+ # or an address and agent, [flight, false] when one is already in flight,
304
+ # and nil without a cache: every lookup is then served, so nothing is shared.
305
+ def board(key)
272
306
  return nil if @cache.nil?
273
307
 
274
308
  @flights_lock.synchronize do
275
- existing = @flights[ip]
276
- existing ? [existing, false] : [@flights[ip] = Flight.new, true]
309
+ existing = @flights[key]
310
+ existing ? [existing, false] : [@flights[key] = Flight.new, true]
277
311
  end
278
312
  end
279
313
 
280
- # Takes `ip` off the board and lands its flight. Returns `value`.
281
- def unboard(ip, flight, state, value = nil)
282
- @flights_lock.synchronize { @flights.delete(ip) if @flights[ip].equal?(flight) }
314
+ # Takes `key` off the board and lands its flight. Returns `value`.
315
+ def unboard(key, flight, state, value = nil)
316
+ @flights_lock.synchronize { @flights.delete(key) if @flights[key].equal?(flight) }
283
317
  flight.land(state, value)
284
318
  value
285
319
  end
@@ -289,39 +323,42 @@ module VPNDetection
289
323
  # reaches every waiter and is cached for none; and a leader that never
290
324
  # finishes - a killed thread, a Timeout.timeout - abandons the flight, so
291
325
  # its waiters ask again rather than failing with it.
292
- def lead(ip, flight)
293
- unboard(ip, flight, :served, yield)
326
+ def lead(key, flight)
327
+ unboard(key, flight, :served, yield)
294
328
  rescue Error => e
295
- unboard(ip, flight, :failed, e)
329
+ unboard(key, flight, :failed, e)
296
330
  raise
297
331
  ensure
298
- unboard(ip, flight, :abandoned)
332
+ unboard(key, flight, :abandoned)
299
333
  end
300
334
 
301
- def fetch(ip, retries, timeout)
335
+ def fetch(ip, agent, retries, timeout)
302
336
  result = Retries.with_retries(retries || @retries) do
303
- Transport.lookup_result(@transport.lookup_request(ip, timeout: timeout).run)
337
+ Transport.lookup_result(@transport.lookup_request(ip, timeout: timeout, user_agent: agent).run)
304
338
  end
305
- @cache&.set(ip, result)
339
+ @cache&.set(key_of(ip, agent), result)
306
340
  result
307
341
  end
308
342
 
309
343
  # One hydra per call, sized for THIS call. Reusing an instance-level hydra
310
344
  # would silently cap a per-call concurrency at the client's setting, and
311
345
  # would not be safe to drive from two threads either. Each request is one
312
- # chunk of up to 1000 addresses.
313
- def run_batch(chunks, answers, concurrency, retries, timeout, boarded)
346
+ # chunk of up to 1000 addresses, carrying the agents of that chunk's
347
+ # addresses alone.
348
+ def run_batch(chunks, agents, answers, concurrency, retries, timeout, boarded)
314
349
  hydra = Typhoeus::Hydra.new(max_concurrency: concurrency)
315
350
  attempts = Hash.new(0)
316
351
 
317
352
  enqueue = lambda do |chunk|
318
- request = @transport.batch_request(chunk, timeout: timeout)
353
+ sent = chunk.filter_map { |ip| [ip, agents[ip]] if agents.key?(ip) }.to_h
354
+ request = @transport.batch_request(chunk, timeout: timeout, user_agents: sent)
319
355
  request.on_complete do |response|
320
- outcome = settle(chunk, response, attempts, retries, enqueue)
356
+ outcome = settle(chunk, agents, response, attempts, retries, enqueue)
321
357
  outcome&.each do |ip, answer|
322
358
  answers[ip] = answer
323
359
  flight = boarded[ip]
324
- unboard(ip, flight, answer.is_a?(Result) ? :served : :failed, answer) unless flight.nil?
360
+ state = answer.is_a?(Result) ? :served : :failed
361
+ unboard(key_of(ip, agents[ip]), flight, state, answer) unless flight.nil?
325
362
  end
326
363
  end
327
364
  hydra.queue(request)
@@ -335,11 +372,11 @@ module VPNDetection
335
372
  # chunk-level failure - the call refused, the transport failing, the retries
336
373
  # exhausted - becomes every address's error, exactly as it would have been
337
374
  # had each been looked up alone.
338
- def settle(chunk, response, attempts, retries, enqueue)
375
+ def settle(chunk, agents, response, attempts, retries, enqueue)
339
376
  body = Transport.batch_body(response)
340
377
  chunk.to_h do |ip|
341
378
  answer = batch_answer(ip, body)
342
- @cache&.set(ip, answer) if answer.is_a?(Result)
379
+ @cache&.set(key_of(ip, agents[ip]), answer) if answer.is_a?(Result)
343
380
  [ip, answer]
344
381
  end
345
382
  rescue Error => e
@@ -355,11 +392,11 @@ module VPNDetection
355
392
 
356
393
  # What a batch takes for an address another call was fetching: that call's
357
394
  # answer or error, or, when its leader never finished, its own lookup.
358
- def joined_answer(ip, flight, retries, timeout)
395
+ def joined_answer(ip, agent, flight, retries, timeout)
359
396
  state, value = flight.wait
360
397
  return value unless state == :abandoned
361
398
 
362
- lookup(ip, retries: retries, timeout: timeout)
399
+ lookup(ip, retries: retries, timeout: timeout, user_agent: agent)
363
400
  rescue Error => e
364
401
  e
365
402
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -27,14 +27,29 @@ module VPNDetection
27
27
  DEFAULT_TIMEOUT = 2.5
28
28
  DEFAULT_RETRIES = 0
29
29
 
30
+ # Reads a request's `User-Agent` where the adapter names no reader: Rack's
31
+ # request has `user_agent`, and so do Sinatra's and Hanami's.
32
+ DEFAULT_USER_AGENT = ->(request) { request.user_agent if request.respond_to?(:user_agent) }
33
+
30
34
  # Resolve, classify, decide.
31
35
  class Core
32
36
  # @param default_ip_selector [#call] the framework's own accessor, used
33
37
  # when the caller named none.
34
- def initialize(default_ip_selector, **options)
38
+ # @param user_agent_of [#call] reads the request's own `User-Agent`, which
39
+ # `allow_bots: true` sends with the lookup.
40
+ # @option options [Boolean] :allow_bots (false) send the request's own
41
+ # `User-Agent` with its lookup, and never refuse a request whose answer
42
+ # says `user_agent.verdict` is `verified`, whatever the condition
43
+ # matches; the answer is still attached. An absent or empty header sends
44
+ # none. A keyless client, or a free key past its trial, never gets a
45
+ # verdict, so nothing is let through there, and that is warned of once
46
+ # under `on_missing_field`.
47
+ def initialize(default_ip_selector, user_agent_of = DEFAULT_USER_AGENT, **options)
35
48
  Condition.validate!(options[:block_condition])
36
49
  @condition = options[:block_condition]
37
50
  @selector = options[:ip_selector] || default_ip_selector
51
+ @allow_bots = options.fetch(:allow_bots, false) == true
52
+ @user_agent_of = user_agent_of
38
53
  @fail_closed = options.fetch(:fail_closed, false)
39
54
  @on_missing_field = options.fetch(:on_missing_field, :warn)
40
55
  @skip = options[:skip]
@@ -76,12 +91,14 @@ module VPNDetection
76
91
  )
77
92
  end
78
93
 
94
+ # A bogon is answered locally, so nothing is sent for it.
95
+ agent = @allow_bots && !Bogon.bogon?(ip) ? user_agent(request) : nil
79
96
  begin
80
- result = @client.lookup(ip, retries: @retries)
97
+ result = @client.lookup(ip, retries: @retries, **(agent ? { user_agent: agent } : {}))
81
98
  rescue VPNDetection::Error => e
82
99
  return Lookup.new(blocked: @fail_closed, ip: ip, error: e)
83
100
  end
84
- decide(ip, result)
101
+ decide(ip, result, agent)
85
102
  end
86
103
 
87
104
  private
@@ -97,19 +114,35 @@ module VPNDetection
97
114
  )
98
115
  end
99
116
 
100
- def decide(ip, result)
117
+ # The request's own `User-Agent`, or nil when it sent none or an empty one.
118
+ def user_agent(request)
119
+ agent = @user_agent_of.call(request)
120
+ agent.is_a?(String) && !agent.empty? ? agent : nil
121
+ end
122
+
123
+ def decide(ip, result, agent)
124
+ report_unanswered_agent(result) unless agent.nil?
101
125
  return Lookup.new(blocked: false, ip: ip, result: result) if @condition.nil?
102
126
 
103
127
  report_missing(result)
104
128
  Lookup.new(
105
- blocked: Condition.matches?(@condition, result),
129
+ blocked: Condition.matches?(@condition, result) && !verified?(result),
106
130
  ip: ip,
107
131
  result: result
108
132
  )
109
133
  end
110
134
 
135
+ # Whether `allow_bots` lets this answer through: its agent's operator
136
+ # vouches for the address.
137
+ def verified?(result)
138
+ @allow_bots && result.user_agent.is_a?(Hash) && result.user_agent['verdict'] == 'verified'
139
+ end
140
+
111
141
  def report_missing(result)
112
142
  return if @on_missing_field == :ignore
143
+ # A bogon is answered locally in the widest shape, which carries every
144
+ # member but `user_agent`, and no agent is sent for it.
145
+ return if result.bogon?
113
146
 
114
147
  missing = Condition.missing_members(@condition, result)
115
148
  return if missing.empty?
@@ -117,6 +150,24 @@ module VPNDetection
117
150
  message = "block_condition names #{missing.join(', ')}, which your plan does not " \
118
151
  'include, so those terms can never match. An absent member means "not in ' \
119
152
  'your plan", not "checked, and no".'
153
+ if missing.include?('user_agent') && !@allow_bots
154
+ message += ' user_agent is answered only to a lookup that sends one, which allow_bots does.'
155
+ end
156
+ report(message)
157
+ end
158
+
159
+ # An agent went with the lookup and the served answer carries no verdict:
160
+ # a keyless client, or a free key past its trial, is never answered one,
161
+ # so allow_bots lets no crawler through.
162
+ def report_unanswered_agent(result)
163
+ return if @on_missing_field == :ignore || result.included?(:user_agent)
164
+
165
+ report('allow_bots sends the request\'s User-Agent, but the answer carries no user_agent ' \
166
+ 'verdict, which your plan does not include, so no crawler is let through. A key on ' \
167
+ 'a paid plan, or on its trial, is answered one.')
168
+ end
169
+
170
+ def report(message)
120
171
  raise ArgumentError, "vpndetection: #{message}" if @on_missing_field == :raise
121
172
 
122
173
  warn_once(message)
@@ -147,6 +198,11 @@ module VPNDetection
147
198
  ->(request) { @view.call(request).framework_ip.call }
148
199
  end
149
200
 
201
+ # The request's own `User-Agent` header, which `allow_bots` sends.
202
+ def user_agent
203
+ ->(request) { @view.call(request).header.call('User-Agent') }
204
+ end
205
+
150
206
  # An address from `X-Forwarded-For`.
151
207
  #
152
208
  # The LEFT-MOST entry (depth 0) is whatever the caller sent, because
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The VPNDetection API: classify any IP address, and download the databases behind the answers. Lookups need no API key: Unlimited, the free plan, answers any address with no daily or monthly cap, with a key or without one. Every paid plan answers every field, and a free account tries them. Unlimited's answers may be used commercially, but not redistributed or used in a competing service (https://vpndetection.io/terms#no-key). See https://docs.vpndetection.io for guides and https://github.com/vpndetection-io for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.21
6
+ The version of the OpenAPI document: 2026.11.22
7
7
  Contact: support@vpndetection.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0