vpndetection-web 1.7.0 → 1.8.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.
- package/README.md +21 -2
- package/dist/client.d.ts +31 -5
- package/dist/client.js +102 -24
- package/dist/index.d.ts +2 -2
- package/dist/shapes.d.ts +2 -0
- package/dist/shapes.js +12 -8
- package/dist/store.d.ts +5 -3
- package/dist/store.js +8 -6
- package/dist/transport.d.ts +5 -0
- package/dist/transport.js +7 -0
- package/dist/types.d.ts +10 -7
- package/dist/types.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ console.log(result.asn?.name); // who runs the announcing network
|
|
|
48
48
|
|
|
49
49
|
### With an API key
|
|
50
50
|
|
|
51
|
-
Where the key stays private (see above), every paid plan answers every field: `is_hosting`, `is_cdn` and the four proxy flags as well, the carrier and satellite flags, the five service flags (SSH, SMTP, web, DNS resolver and BitTorrent), `bot` (whose bot it is and whether its operator vouches for the address), city-level `location`, the `company` and the `abuse` contact, and the provider (its ID and its name), confidence and last-seen detail behind each flag. The plans differ only in how many lookups they include, and a free account tries every field. Create a key in the [console](https://app.vpndetection.io), then pass it in:
|
|
51
|
+
Where the key stays private (see above), every paid plan answers every field: `is_hosting`, `is_cdn` and the four proxy flags as well, the carrier and satellite flags, the five service flags (SSH, SMTP, web, DNS resolver and BitTorrent), `bot` (whose bot it is and whether its operator vouches for the address), `user_agent` (whether a user agent you send is borne out by the address: see below), city-level `location`, the `company` and the `abuse` contact, and the provider (its ID and its name), confidence and last-seen detail behind each flag. The plans differ only in how many lookups they include, and a free account tries every field. Create a key in the [console](https://app.vpndetection.io), then pass it in:
|
|
52
52
|
|
|
53
53
|
```js
|
|
54
54
|
const client = new VPNDetection({ apiKey: 'your-api-key' });
|
|
@@ -70,6 +70,25 @@ console.log(result.ip); // the address we saw this call come from: in a page,
|
|
|
70
70
|
|
|
71
71
|
It is never cached, since which address the browser is calling from is the question, and a laptop moves between networks.
|
|
72
72
|
|
|
73
|
+
### A visitor's user agent
|
|
74
|
+
|
|
75
|
+
Pass the `User-Agent` of the client you are deciding on, and on a paid plan or the trial the answer's `userAgent` names the agent it claims to be and whether this address bears that out: `verified` when the agent's operator vouches for the address, `spoofed` when it does not, `unverifiable` for a bot nothing can confirm, `not_bot` for a browser, an app or a script. Check for the values you know: one the API adds later comes back as the string it served.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
const result = await client.lookup('66.249.66.1', {
|
|
79
|
+
userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
|
|
80
|
+
});
|
|
81
|
+
console.log(result.userAgent); // { agent: 'Googlebot', kind: 'bot', verdict: 'verified' }
|
|
82
|
+
|
|
83
|
+
const mine = await client.myIp({ userAgent: navigator.userAgent });
|
|
84
|
+
|
|
85
|
+
const results = await client.lookupBatch(['66.249.66.1', '45.83.91.1'], {
|
|
86
|
+
userAgents: { '66.249.66.1': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' },
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`userAgents` is keyed by each address as you pass it in the list, as an object or a `Map`. An empty user agent sends none, a long one is sent whole, and the agent is never kept. An answer to a user agent is cached apart from the address's plain answer and from another agent's. A private or reserved address is still answered locally and carries no `userAgent`; neither does an answer without every field.
|
|
91
|
+
|
|
73
92
|
### Your plan and usage
|
|
74
93
|
|
|
75
94
|
```js
|
|
@@ -166,7 +185,7 @@ const store = {
|
|
|
166
185
|
const client = new VPNDetection({ cache: { store: store } });
|
|
167
186
|
```
|
|
168
187
|
|
|
169
|
-
`set` is told how long the answer stays fresh, and dropping it after that is the store's job. Each `key` carries a hash of the client's API key and base URL beside the address, so clients holding different keys can share one store without reading each other's answers. Bogons, errors, `myIp` and `myEntitlement` are never stored, and a store that fails costs a request, never an answer: a `get` that throws reads as a miss, and a `set` that throws is ignored.
|
|
188
|
+
`set` is told how long the answer stays fresh, and dropping it after that is the store's job. Each `key` carries a hash of the client's API key and base URL beside the address, and the user agent sent with it, if any, so clients holding different keys can share one store without reading each other's answers. Bogons, errors, `myIp` and `myEntitlement` are never stored, and a store that fails costs a request, never an answer: a `get` that throws reads as a miss, and a `set` that throws is ignored.
|
|
170
189
|
|
|
171
190
|
### Private and reserved addresses
|
|
172
191
|
|
package/dist/client.d.ts
CHANGED
|
@@ -43,17 +43,32 @@ export interface Options {
|
|
|
43
43
|
/** Override the HTTP implementation, mostly for tests. Defaults to the global `fetch`. */
|
|
44
44
|
fetch?: typeof globalThis.fetch;
|
|
45
45
|
}
|
|
46
|
-
/** Per-call overrides for
|
|
47
|
-
export interface
|
|
46
|
+
/** Per-call overrides for one call. Anything omitted falls back to the client's setting. */
|
|
47
|
+
export interface CallOptions {
|
|
48
48
|
/** Retry attempts for a transient failure, a whole number from 0. */
|
|
49
49
|
retries?: number;
|
|
50
50
|
/** How long one attempt may take before it is abandoned, in milliseconds. */
|
|
51
51
|
timeoutMs?: number;
|
|
52
52
|
}
|
|
53
|
+
/** Per-call options for `lookup` and `myIp`. */
|
|
54
|
+
export interface LookupOptions extends CallOptions {
|
|
55
|
+
/**
|
|
56
|
+
* The `User-Agent` of the client that came from this address. With every field, on a paid
|
|
57
|
+
* plan or the trial, the answer's `userAgent` then names the agent it claims to be and whether
|
|
58
|
+
* this address bears that out. The empty string sends none, and a longer one than the API's
|
|
59
|
+
* 1024 characters is sent whole for the API to cut.
|
|
60
|
+
*/
|
|
61
|
+
userAgent?: string;
|
|
62
|
+
}
|
|
53
63
|
/** Per-call overrides for one batch. Anything omitted falls back to the client's setting. */
|
|
54
|
-
export interface BatchOptions extends
|
|
64
|
+
export interface BatchOptions extends CallOptions {
|
|
55
65
|
/** Concurrent batch requests for THIS batch only. */
|
|
56
66
|
concurrency?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Each address's `User-Agent`, keyed by the address as you pass it in `ips`, sent as
|
|
69
|
+
* `lookup`'s `userAgent` is. An address left out, or given the empty string, sends none.
|
|
70
|
+
*/
|
|
71
|
+
userAgents?: Readonly<Record<string, string>> | ReadonlyMap<string, string>;
|
|
57
72
|
}
|
|
58
73
|
/**
|
|
59
74
|
* A client for the VPNDetection API, for a browser: a page's own scripts, a web
|
|
@@ -96,6 +111,10 @@ export declare class VPNDetection {
|
|
|
96
111
|
* call that led it. An IPv4-mapped address (`::ffff:8.8.8.8`) is the IPv4
|
|
97
112
|
* address it carries: judged, sent and cached as that, so the answer names
|
|
98
113
|
* `8.8.8.8`.
|
|
114
|
+
*
|
|
115
|
+
* With `userAgent`, the answer is cached, stored and shared on the address and
|
|
116
|
+
* that agent together: apart from the address's plain answer and from another
|
|
117
|
+
* agent's. A bogon's answer carries no `userAgent`.
|
|
99
118
|
*/
|
|
100
119
|
lookup(ip: string, options?: LookupOptions): Promise<Result>;
|
|
101
120
|
private answer;
|
|
@@ -111,7 +130,8 @@ export declare class VPNDetection {
|
|
|
111
130
|
*
|
|
112
131
|
* Deliberately NOT cached. The cache is keyed by address, and which
|
|
113
132
|
* address this is IS the question: a device that moves between networks
|
|
114
|
-
* would otherwise be told where it used to be.
|
|
133
|
+
* would otherwise be told where it used to be. `userAgent` is sent as
|
|
134
|
+
* `lookup` sends it.
|
|
115
135
|
*/
|
|
116
136
|
myIp(options?: LookupOptions): Promise<Result>;
|
|
117
137
|
/**
|
|
@@ -133,7 +153,7 @@ export declare class VPNDetection {
|
|
|
133
153
|
* Deliberately NOT cached: the whole point is what has been spent, and a
|
|
134
154
|
* cached answer is a wrong one within seconds of the next request.
|
|
135
155
|
*/
|
|
136
|
-
myEntitlement(options?:
|
|
156
|
+
myEntitlement(options?: CallOptions): Promise<Entitlement>;
|
|
137
157
|
/**
|
|
138
158
|
* Classify many addresses in as few requests as possible.
|
|
139
159
|
*
|
|
@@ -155,6 +175,12 @@ export declare class VPNDetection {
|
|
|
155
175
|
* `unauthorized` error, and a free-plan key's request of two or more a
|
|
156
176
|
* `bad_request` (HTTP 402). A request of one address is answered as its
|
|
157
177
|
* lookup would be.
|
|
178
|
+
*
|
|
179
|
+
* `userAgents` sends each address's agent, as `user_agents` keyed by the address
|
|
180
|
+
* as the request sends it (an IPv4-mapped one as the IPv4 address it carries),
|
|
181
|
+
* and only in a request where one of its addresses has one. Each address's
|
|
182
|
+
* answer is cached, stored and shared on the address and its agent together,
|
|
183
|
+
* as `lookup`'s is.
|
|
158
184
|
*/
|
|
159
185
|
lookupBatch(ips: Iterable<string>, options?: BatchOptions): Promise<Map<string, Result | VPNDetectionError>>;
|
|
160
186
|
private unstored;
|
package/dist/client.js
CHANGED
|
@@ -7,7 +7,7 @@ import { errorFromEntry, VPNDetectionError } from './errors.js';
|
|
|
7
7
|
import { OauthApi } from './oauth.js';
|
|
8
8
|
import { isBatch, isBatchError, isEntitlement, isLookup } from './shapes.js';
|
|
9
9
|
import { isCacheStore, lifeMs, storeScope, StoreTier } from './store.js';
|
|
10
|
-
import { asError, checkConcurrency, checkRetries, checkTimeout, deadline, readable, withRetry, } from './transport.js';
|
|
10
|
+
import { asError, checkConcurrency, checkRetries, checkTimeout, deadline, readable, strictEncode, withRetry, } from './transport.js';
|
|
11
11
|
import { toResult } from './types.js';
|
|
12
12
|
export const DEFAULT_BASE_URL = 'https://api.vpndetection.io';
|
|
13
13
|
// fetch has no whole-request limit of its own, so without this a hung API would
|
|
@@ -25,9 +25,11 @@ const BATCH_MAX = 1000;
|
|
|
25
25
|
*/
|
|
26
26
|
export class VPNDetection {
|
|
27
27
|
client;
|
|
28
|
-
// Keyed by address
|
|
29
|
-
//
|
|
30
|
-
//
|
|
28
|
+
// Keyed by address, and by the user agent sent with it (`cacheKey`): an
|
|
29
|
+
// answer to one carries that agent's verdict. The context is how a call
|
|
30
|
+
// sends the request when the key has none in flight: `fetch()` runs it
|
|
31
|
+
// synchronously, so a batch knows which addresses it leads before it builds
|
|
32
|
+
// a chunk.
|
|
31
33
|
cache;
|
|
32
34
|
store;
|
|
33
35
|
limit;
|
|
@@ -100,35 +102,42 @@ export class VPNDetection {
|
|
|
100
102
|
* call that led it. An IPv4-mapped address (`::ffff:8.8.8.8`) is the IPv4
|
|
101
103
|
* address it carries: judged, sent and cached as that, so the answer names
|
|
102
104
|
* `8.8.8.8`.
|
|
105
|
+
*
|
|
106
|
+
* With `userAgent`, the answer is cached, stored and shared on the address and
|
|
107
|
+
* that agent together: apart from the address's plain answer and from another
|
|
108
|
+
* agent's. A bogon's answer carries no `userAgent`.
|
|
103
109
|
*/
|
|
104
110
|
async lookup(ip, options = {}) {
|
|
105
111
|
// First, or a bogon and a cached address would answer any value.
|
|
106
112
|
checkTimeout(options.timeoutMs);
|
|
107
113
|
checkRetries(options.retries);
|
|
114
|
+
const agent = checkUserAgent(options.userAgent);
|
|
108
115
|
const addr = unmapped(ip);
|
|
109
116
|
if (isBogon(addr)) {
|
|
110
117
|
return bogonResult(addr);
|
|
111
118
|
}
|
|
112
119
|
if (this.cache === null) {
|
|
113
|
-
return this.serve(addr, options);
|
|
120
|
+
return this.serve(addr, agent, options);
|
|
114
121
|
}
|
|
115
|
-
return (await this.cache.fetch(addr,
|
|
122
|
+
return (await this.cache.fetch(cacheKey(addr, agent), {
|
|
123
|
+
context: () => this.answer(addr, agent, options),
|
|
124
|
+
}));
|
|
116
125
|
}
|
|
117
126
|
// A miss in memory: the store's answer if it holds one, else the API's, kept.
|
|
118
|
-
async answer(ip, options) {
|
|
119
|
-
const stored = await this.store?.get(ip);
|
|
127
|
+
async answer(ip, agent, options) {
|
|
128
|
+
const stored = await this.store?.get(ip, agent);
|
|
120
129
|
if (stored !== undefined) {
|
|
121
130
|
return stored;
|
|
122
131
|
}
|
|
123
|
-
const served = await this.serve(ip, options);
|
|
124
|
-
await this.store?.set(ip, served);
|
|
132
|
+
const served = await this.serve(ip, agent, options);
|
|
133
|
+
await this.store?.set(ip, agent, served);
|
|
125
134
|
return served;
|
|
126
135
|
}
|
|
127
|
-
async serve(ip, options) {
|
|
136
|
+
async serve(ip, agent, options) {
|
|
128
137
|
const timeoutMs = options.timeoutMs ?? this.timeoutMs;
|
|
129
138
|
return withRetry(options.retries ?? this.retries, async () => {
|
|
130
139
|
const res = await deadline(timeoutMs, (signal) => lookupIp({
|
|
131
|
-
client: this.client, path: { ip: ip }, signal: signal,
|
|
140
|
+
client: this.client, path: { ip: ip }, signal: signal, ...agentQuery(agent),
|
|
132
141
|
}));
|
|
133
142
|
return toResult(readable(res, isLookup));
|
|
134
143
|
});
|
|
@@ -144,13 +153,16 @@ export class VPNDetection {
|
|
|
144
153
|
*
|
|
145
154
|
* Deliberately NOT cached. The cache is keyed by address, and which
|
|
146
155
|
* address this is IS the question: a device that moves between networks
|
|
147
|
-
* would otherwise be told where it used to be.
|
|
156
|
+
* would otherwise be told where it used to be. `userAgent` is sent as
|
|
157
|
+
* `lookup` sends it.
|
|
148
158
|
*/
|
|
149
159
|
async myIp(options = {}) {
|
|
150
160
|
const timeoutMs = checkTimeout(options.timeoutMs) ?? this.timeoutMs;
|
|
151
|
-
|
|
161
|
+
const retries = checkRetries(options.retries) ?? this.retries;
|
|
162
|
+
const agent = checkUserAgent(options.userAgent);
|
|
163
|
+
return withRetry(retries, async () => {
|
|
152
164
|
const res = await deadline(timeoutMs, (signal) => lookupMyIp({
|
|
153
|
-
client: this.client, signal: signal,
|
|
165
|
+
client: this.client, signal: signal, ...agentQuery(agent),
|
|
154
166
|
}));
|
|
155
167
|
return toResult(readable(res, isLookup));
|
|
156
168
|
});
|
|
@@ -204,11 +216,18 @@ export class VPNDetection {
|
|
|
204
216
|
* `unauthorized` error, and a free-plan key's request of two or more a
|
|
205
217
|
* `bad_request` (HTTP 402). A request of one address is answered as its
|
|
206
218
|
* lookup would be.
|
|
219
|
+
*
|
|
220
|
+
* `userAgents` sends each address's agent, as `user_agents` keyed by the address
|
|
221
|
+
* as the request sends it (an IPv4-mapped one as the IPv4 address it carries),
|
|
222
|
+
* and only in a request where one of its addresses has one. Each address's
|
|
223
|
+
* answer is cached, stored and shared on the address and its agent together,
|
|
224
|
+
* as `lookup`'s is.
|
|
207
225
|
*/
|
|
208
226
|
async lookupBatch(ips, options = {}) {
|
|
209
227
|
const concurrency = checkConcurrency(options.concurrency);
|
|
210
228
|
checkTimeout(options.timeoutMs);
|
|
211
229
|
checkRetries(options.retries);
|
|
230
|
+
const agents = checkUserAgents(options.userAgents);
|
|
212
231
|
const asked = [...new Set(ips)];
|
|
213
232
|
const unique = [...new Set(asked.map(unmapped))];
|
|
214
233
|
const out = new Map();
|
|
@@ -224,7 +243,8 @@ export class VPNDetection {
|
|
|
224
243
|
out.set(ip, bogonResult(ip));
|
|
225
244
|
continue;
|
|
226
245
|
}
|
|
227
|
-
const
|
|
246
|
+
const key = cacheKey(ip, agents.get(ip));
|
|
247
|
+
const hit = this.cache?.get(key);
|
|
228
248
|
if (hit !== undefined) {
|
|
229
249
|
out.set(ip, hit);
|
|
230
250
|
continue;
|
|
@@ -233,7 +253,7 @@ export class VPNDetection {
|
|
|
233
253
|
pending.push(ip);
|
|
234
254
|
continue;
|
|
235
255
|
}
|
|
236
|
-
shared.push([ip, this.cache.fetch(
|
|
256
|
+
shared.push([ip, this.cache.fetch(key, {
|
|
237
257
|
context: () => {
|
|
238
258
|
const landing = deferred();
|
|
239
259
|
led.set(ip, landing);
|
|
@@ -242,7 +262,7 @@ export class VPNDetection {
|
|
|
242
262
|
},
|
|
243
263
|
})]);
|
|
244
264
|
}
|
|
245
|
-
const sending = this.store === null ? pending : await this.unstored(pending, led);
|
|
265
|
+
const sending = this.store === null ? pending : await this.unstored(pending, agents, led);
|
|
246
266
|
// A per-call concurrency gets its own limiter; without one the call
|
|
247
267
|
// would share the instance's budget and silently ignore the override.
|
|
248
268
|
const limit = concurrency === undefined ? this.limit : pLimit(concurrency);
|
|
@@ -253,7 +273,7 @@ export class VPNDetection {
|
|
|
253
273
|
await Promise.all([
|
|
254
274
|
...chunks.map((chunk) => limit(async () => {
|
|
255
275
|
const kept = [];
|
|
256
|
-
for (const [ip, answer] of await this.lookupChunk(chunk, options)) {
|
|
276
|
+
for (const [ip, answer] of await this.lookupChunk(chunk, agents, options)) {
|
|
257
277
|
const landing = led.get(ip);
|
|
258
278
|
if (landing === undefined) {
|
|
259
279
|
out.set(ip, answer);
|
|
@@ -265,7 +285,7 @@ export class VPNDetection {
|
|
|
265
285
|
landing.resolve(answer);
|
|
266
286
|
}
|
|
267
287
|
if (this.store !== null && !(answer instanceof VPNDetectionError)) {
|
|
268
|
-
kept.push(this.store.set(ip, answer));
|
|
288
|
+
kept.push(this.store.set(ip, agents.get(ip), answer));
|
|
269
289
|
}
|
|
270
290
|
}
|
|
271
291
|
await Promise.all(kept);
|
|
@@ -286,8 +306,8 @@ export class VPNDetection {
|
|
|
286
306
|
}
|
|
287
307
|
// The addresses this batch leads that the store does not hold, in the order
|
|
288
308
|
// given. One the store holds lands at once, as a request's answer would.
|
|
289
|
-
async unstored(ips, led) {
|
|
290
|
-
const stored = await Promise.all(ips.map((ip) => this.store.get(ip)));
|
|
309
|
+
async unstored(ips, agents, led) {
|
|
310
|
+
const stored = await Promise.all(ips.map((ip) => this.store.get(ip, agents.get(ip))));
|
|
291
311
|
return ips.filter((ip, i) => {
|
|
292
312
|
const hit = stored[i];
|
|
293
313
|
if (hit === undefined) {
|
|
@@ -301,14 +321,15 @@ export class VPNDetection {
|
|
|
301
321
|
// chunk-level failure - the call refused, the transport failing, the
|
|
302
322
|
// retries exhausted - becomes every address's error, exactly as it would
|
|
303
323
|
// have been had each been looked up alone.
|
|
304
|
-
async lookupChunk(chunk, options) {
|
|
324
|
+
async lookupChunk(chunk, agents, options) {
|
|
305
325
|
const out = new Map();
|
|
306
326
|
const timeoutMs = options.timeoutMs ?? this.timeoutMs;
|
|
327
|
+
const request = batchRequest(chunk, agents);
|
|
307
328
|
let body;
|
|
308
329
|
try {
|
|
309
330
|
body = await withRetry(options.retries ?? this.retries, async () => {
|
|
310
331
|
const res = await deadline(timeoutMs, (signal) => lookupBatch({
|
|
311
|
-
client: this.client, body:
|
|
332
|
+
client: this.client, body: request, signal: signal,
|
|
312
333
|
}));
|
|
313
334
|
return readable(res, isBatch);
|
|
314
335
|
});
|
|
@@ -388,6 +409,63 @@ function checkCacheOption(name, value) {
|
|
|
388
409
|
}
|
|
389
410
|
return value;
|
|
390
411
|
}
|
|
412
|
+
/** The agent a call sends, `undefined` for none: the empty string is none, as the API reads `user_agent=`. */
|
|
413
|
+
function checkUserAgent(userAgent) {
|
|
414
|
+
if (userAgent === undefined || userAgent === null || userAgent === '') {
|
|
415
|
+
return undefined;
|
|
416
|
+
}
|
|
417
|
+
if (typeof userAgent !== 'string') {
|
|
418
|
+
throw new VPNDetectionError('bad_request', `userAgent must be a string, got ${typeof userAgent}`);
|
|
419
|
+
}
|
|
420
|
+
return userAgent;
|
|
421
|
+
}
|
|
422
|
+
/** A batch's user agents, keyed by each address as the batch sends it, every empty one dropped. */
|
|
423
|
+
function checkUserAgents(userAgents) {
|
|
424
|
+
const agents = new Map();
|
|
425
|
+
if (userAgents === undefined || userAgents === null) {
|
|
426
|
+
return agents;
|
|
427
|
+
}
|
|
428
|
+
if (typeof userAgents !== 'object') {
|
|
429
|
+
throw new VPNDetectionError('bad_request', `userAgents must map addresses to agents, got ${typeof userAgents}`);
|
|
430
|
+
}
|
|
431
|
+
const entries = userAgents instanceof Map ? [...userAgents.entries()] : Object.entries(userAgents);
|
|
432
|
+
for (const [ip, agent] of entries) {
|
|
433
|
+
const checked = checkUserAgent(agent);
|
|
434
|
+
if (checked !== undefined) {
|
|
435
|
+
agents.set(unmapped(String(ip)), checked);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return agents;
|
|
439
|
+
}
|
|
440
|
+
// An answer to an address with an agent carries that agent's verdict, so it is
|
|
441
|
+
// never served for the address alone or for another agent. The address alone
|
|
442
|
+
// stays the key of a lookup sending none.
|
|
443
|
+
function cacheKey(ip, agent) {
|
|
444
|
+
return agent === undefined ? ip : JSON.stringify([ip, agent]);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* One chunk's body: `user_agents` only when an address of THIS chunk has one, never
|
|
448
|
+
* `{}`, keyed by each address exactly as `ips` sends it.
|
|
449
|
+
*/
|
|
450
|
+
function batchRequest(chunk, agents) {
|
|
451
|
+
const sent = chunk.filter((ip) => agents.has(ip)).map((ip) => [ip, agents.get(ip)]);
|
|
452
|
+
return sent.length === 0 ? { ips: chunk } : { ips: chunk, user_agents: Object.fromEntries(sent) };
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* The query a lookup sends its agent in, every reserved character percent-encoded,
|
|
456
|
+
* `+` among them, which a server reads as a space. Set by hand, so nothing checks
|
|
457
|
+
* the spec's 1024-character bound before the API cuts it.
|
|
458
|
+
*/
|
|
459
|
+
function agentQuery(agent) {
|
|
460
|
+
if (agent === undefined) {
|
|
461
|
+
return {};
|
|
462
|
+
}
|
|
463
|
+
return { query: { user_agent: agent }, querySerializer: strictQuery };
|
|
464
|
+
}
|
|
465
|
+
function strictQuery(query) {
|
|
466
|
+
return Object.entries(query).filter(([, value]) => value !== undefined && value !== null)
|
|
467
|
+
.map(([name, value]) => `${strictEncode(name)}=${strictEncode(String(value))}`).join('&');
|
|
468
|
+
}
|
|
391
469
|
function unreadableEntry(ip) {
|
|
392
470
|
return new VPNDetectionError('server_error', `unreadable batch answer for ${ip}, status 200`, 200);
|
|
393
471
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { VPNDetection, DEFAULT_BASE_URL } from './client.js';
|
|
2
|
-
export type { Options, CacheOptions, LookupOptions, BatchOptions } from './client.js';
|
|
2
|
+
export type { Options, CacheOptions, CallOptions, LookupOptions, BatchOptions } from './client.js';
|
|
3
3
|
export type { CacheStore } from './store.js';
|
|
4
4
|
export { OauthApi } from './oauth.js';
|
|
5
5
|
export type { OauthOptions, AuthorizationUrlOptions, OauthMetadata, TokenResponse, Pkce } from './oauth.js';
|
|
6
6
|
export { isBogon } from './bogon.js';
|
|
7
7
|
export { VPNDetectionError, OauthError, OauthAccessDeniedError, OauthExpiredTokenError, } from './errors.js';
|
|
8
8
|
export type { ErrorKind } from './errors.js';
|
|
9
|
-
export type { Result, VpnDetail, ClassDetail, ProxyDetail, PubproxyDetail, CarrierDetail, SatelliteDetail, SshDetail, SmtpDetail, WebDetail, DnsResolverDetail, BitTorrentDetail, BotDetail, Location, Asn, Company, AbuseContact, LookupResponse, } from './types.js';
|
|
9
|
+
export type { Result, VpnDetail, ClassDetail, ProxyDetail, PubproxyDetail, CarrierDetail, SatelliteDetail, SshDetail, SmtpDetail, WebDetail, DnsResolverDetail, BitTorrentDetail, BotDetail, UserAgentVerdict, Location, Asn, Company, AbuseContact, LookupResponse, } from './types.js';
|
|
10
10
|
export type { Entitlement, EntitlementApikey, EntitlementPlan, EntitlementUsage, } from './generated/types.gen.js';
|
package/dist/shapes.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare const ENTITLEMENT_MEMBERS: readonly ["org_id", "apikey", "plan",
|
|
|
10
10
|
export declare const ENTITLEMENT_APIKEY_MEMBERS: readonly ["id", "allowed_cidrs"];
|
|
11
11
|
export declare const ENTITLEMENT_PLAN_MEMBERS: readonly ["key", "name", "tier"];
|
|
12
12
|
export declare const ENTITLEMENT_USAGE_MEMBERS: readonly ["requests", "quota", "window_start", "window_end"];
|
|
13
|
+
export declare const USER_AGENT_MEMBERS: readonly ["agent", "kind", "verdict"];
|
|
13
14
|
type Kind = 'string' | 'integer' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'date-time';
|
|
14
15
|
export declare const LOOKUP_TYPES: Record<string, Kind>;
|
|
15
16
|
export declare const VPN_DETAIL_TYPES: Record<string, Kind>;
|
|
@@ -17,6 +18,7 @@ export declare const CLASS_DETAIL_TYPES: Record<string, Kind>;
|
|
|
17
18
|
export declare const PROXY_DETAIL_TYPES: Record<string, Kind>;
|
|
18
19
|
export declare const PUBPROXY_DETAIL_TYPES: Record<string, Kind>;
|
|
19
20
|
export declare const BOT_DETAIL_TYPES: Record<string, Kind>;
|
|
21
|
+
export declare const USER_AGENT_TYPES: Record<string, Kind>;
|
|
20
22
|
export declare const CARRIER_DETAIL_TYPES: Record<string, Kind>;
|
|
21
23
|
export declare const SATELLITE_DETAIL_TYPES: Record<string, Kind>;
|
|
22
24
|
export declare const SSH_DETAIL_TYPES: Record<string, Kind>;
|
package/dist/shapes.js
CHANGED
|
@@ -20,6 +20,7 @@ export const ENTITLEMENT_MEMBERS = ['org_id', 'apikey', 'plan', 'usage'];
|
|
|
20
20
|
export const ENTITLEMENT_APIKEY_MEMBERS = ['id', 'allowed_cidrs'];
|
|
21
21
|
export const ENTITLEMENT_PLAN_MEMBERS = ['key', 'name', 'tier'];
|
|
22
22
|
export const ENTITLEMENT_USAGE_MEMBERS = ['requests', 'quota', 'window_start', 'window_end'];
|
|
23
|
+
export const USER_AGENT_MEMBERS = ['agent', 'kind', 'verdict'];
|
|
23
24
|
export const LOOKUP_TYPES = {
|
|
24
25
|
ip: 'string', is_bogon: 'boolean', is_vpn: 'boolean', is_hosting: 'boolean', is_relay: 'boolean', is_tor: 'boolean',
|
|
25
26
|
is_cdn: 'boolean', is_resproxy: 'boolean', is_dcproxy: 'boolean', is_mobproxy: 'boolean',
|
|
@@ -47,8 +48,10 @@ export const PUBPROXY_DETAIL_TYPES = {
|
|
|
47
48
|
};
|
|
48
49
|
export const BOT_DETAIL_TYPES = {
|
|
49
50
|
provider: 'string', provider_name: 'string', agents: 'array', categories: 'array', verified: 'boolean',
|
|
50
|
-
identity: 'string', verified_by: 'string', confidence: 'string', first_seen: '
|
|
51
|
+
identity: 'string', verified_by: 'string', confidence: 'string', first_seen: 'date', last_seen: 'date',
|
|
51
52
|
};
|
|
53
|
+
// `verdict` is read as any string, so a verdict the API adds comes back as served.
|
|
54
|
+
export const USER_AGENT_TYPES = { agent: 'string', kind: 'string', verdict: 'string' };
|
|
52
55
|
export const CARRIER_DETAIL_TYPES = {
|
|
53
56
|
name: 'string', mcc: 'string', mnc: 'string', country: 'string', confidence: 'string',
|
|
54
57
|
};
|
|
@@ -57,24 +60,24 @@ export const SATELLITE_DETAIL_TYPES = {
|
|
|
57
60
|
asn: 'integer', confidence: 'string',
|
|
58
61
|
};
|
|
59
62
|
export const SSH_DETAIL_TYPES = {
|
|
60
|
-
port: 'integer', banner: 'string', host_key_fp: 'string', session_ok: 'boolean', first_seen: '
|
|
61
|
-
last_seen: '
|
|
63
|
+
port: 'integer', banner: 'string', host_key_fp: 'string', session_ok: 'boolean', first_seen: 'date',
|
|
64
|
+
last_seen: 'date',
|
|
62
65
|
};
|
|
63
66
|
export const SMTP_DETAIL_TYPES = {
|
|
64
|
-
port: 'integer', banner: 'string', session_ok: 'boolean', first_seen: '
|
|
67
|
+
port: 'integer', banner: 'string', session_ok: 'boolean', first_seen: 'date', last_seen: 'date',
|
|
65
68
|
};
|
|
66
69
|
export const WEB_DETAIL_TYPES = {
|
|
67
70
|
port: 'integer', tls_ok: 'boolean', session_ok: 'boolean', status_code: 'integer', server: 'string',
|
|
68
71
|
location: 'string', title: 'string', cert_subject_cn: 'string', cert_subject_o: 'string',
|
|
69
|
-
cert_issuer_dn: 'string', cert_browser_trusted: 'boolean', tls_version: 'string', first_seen: '
|
|
70
|
-
last_seen: '
|
|
72
|
+
cert_issuer_dn: 'string', cert_browser_trusted: 'boolean', tls_version: 'string', first_seen: 'date',
|
|
73
|
+
last_seen: 'date',
|
|
71
74
|
};
|
|
72
75
|
export const DNS_RESOLVER_DETAIL_TYPES = {
|
|
73
76
|
port: 'integer', recursion_available: 'boolean', rcode: 'integer', answer_correct: 'boolean',
|
|
74
|
-
first_seen: '
|
|
77
|
+
first_seen: 'date', last_seen: 'date',
|
|
75
78
|
};
|
|
76
79
|
export const BITTORRENT_DETAIL_TYPES = {
|
|
77
|
-
verified: 'boolean', id_matches_ip: 'boolean', first_seen: '
|
|
80
|
+
verified: 'boolean', id_matches_ip: 'boolean', first_seen: 'date', last_seen: 'date',
|
|
78
81
|
last_reachable: 'string', days_seen: 'integer', num_ports: 'integer', peak_num_ports: 'integer',
|
|
79
82
|
indexer: 'boolean', clients: 'array',
|
|
80
83
|
};
|
|
@@ -178,6 +181,7 @@ const SHARED = [
|
|
|
178
181
|
['asn', ASN_MEMBERS, ASN_TYPES],
|
|
179
182
|
['company', COMPANY_MEMBERS, COMPANY_TYPES],
|
|
180
183
|
['abuse', ABUSE_CONTACT_MEMBERS, ABUSE_CONTACT_TYPES],
|
|
184
|
+
['user_agent', USER_AGENT_MEMBERS, USER_AGENT_TYPES],
|
|
181
185
|
];
|
|
182
186
|
export function isLookup(data) {
|
|
183
187
|
return typed(data, LOOKUP_MEMBERS, LOOKUP_TYPES)
|
package/dist/store.d.ts
CHANGED
|
@@ -6,7 +6,9 @@ import { type Result } from './types.js';
|
|
|
6
6
|
*
|
|
7
7
|
* `key` is the address with a hash of the client's API key and base URL in front of
|
|
8
8
|
* it, so clients holding different keys, and so entitled to different fields, can
|
|
9
|
-
* share one store without ever reading each other's answers.
|
|
9
|
+
* share one store without ever reading each other's answers. A lookup sending a user
|
|
10
|
+
* agent appends it, `?user_agent=` and the agent percent-encoded, since its answer
|
|
11
|
+
* carries that agent's verdict. `set` is handed how long
|
|
10
12
|
* the answer stays fresh; dropping it once that has passed is the store's job.
|
|
11
13
|
*/
|
|
12
14
|
export interface CacheStore {
|
|
@@ -25,8 +27,8 @@ export declare class StoreTier {
|
|
|
25
27
|
private readonly scope;
|
|
26
28
|
private readonly ttlMs;
|
|
27
29
|
constructor(store: CacheStore, scope: string, ttlMs: number);
|
|
28
|
-
get(ip: string): Promise<Result | undefined>;
|
|
29
|
-
set(ip: string, result: Result): Promise<void>;
|
|
30
|
+
get(ip: string, agent: string | undefined): Promise<Result | undefined>;
|
|
31
|
+
set(ip: string, agent: string | undefined, result: Result): Promise<void>;
|
|
30
32
|
private keyFor;
|
|
31
33
|
}
|
|
32
34
|
/**
|
package/dist/store.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isLookup } from './shapes.js';
|
|
2
|
+
import { strictEncode } from './transport.js';
|
|
2
3
|
import { toResult } from './types.js';
|
|
3
4
|
/**
|
|
4
5
|
* The client's side of a `CacheStore`. A store is a cache, never a source of
|
|
@@ -16,10 +17,10 @@ export class StoreTier {
|
|
|
16
17
|
this.scope = scope;
|
|
17
18
|
this.ttlMs = ttlMs;
|
|
18
19
|
}
|
|
19
|
-
async get(ip) {
|
|
20
|
+
async get(ip, agent) {
|
|
20
21
|
let stored;
|
|
21
22
|
try {
|
|
22
|
-
stored = await this.store.get(this.keyFor(ip));
|
|
23
|
+
stored = await this.store.get(this.keyFor(ip, agent));
|
|
23
24
|
}
|
|
24
25
|
catch {
|
|
25
26
|
return undefined;
|
|
@@ -30,16 +31,17 @@ export class StoreTier {
|
|
|
30
31
|
const raw = stored.raw;
|
|
31
32
|
return isLookup(raw) ? toResult(raw) : undefined;
|
|
32
33
|
}
|
|
33
|
-
async set(ip, result) {
|
|
34
|
+
async set(ip, agent, result) {
|
|
34
35
|
try {
|
|
35
|
-
await this.store.set(this.keyFor(ip), result, lifeMs(result, this.ttlMs));
|
|
36
|
+
await this.store.set(this.keyFor(ip, agent), result, lifeMs(result, this.ttlMs));
|
|
36
37
|
}
|
|
37
38
|
catch {
|
|
38
39
|
// Costs a request on the next miss, nothing more.
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
|
-
keyFor(ip) {
|
|
42
|
-
|
|
42
|
+
keyFor(ip, agent) {
|
|
43
|
+
const key = `${this.scope}:${ip}`;
|
|
44
|
+
return agent === undefined ? key : `${key}?user_agent=${strictEncode(agent)}`;
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
/**
|
package/dist/transport.d.ts
CHANGED
|
@@ -44,4 +44,9 @@ export declare function checkConcurrency(concurrency: number | null | undefined)
|
|
|
44
44
|
*/
|
|
45
45
|
export declare function deadline<T>(timeoutMs: number, fn: (signal: AbortSignal) => Promise<T>): Promise<T>;
|
|
46
46
|
export declare function withRetry<T>(retries: number, fn: () => Promise<T>): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* `encodeURIComponent`, and `!'()*` too, so every reserved character goes out
|
|
49
|
+
* percent-encoded: `+` among them, which a server reads as a space.
|
|
50
|
+
*/
|
|
51
|
+
export declare function strictEncode(value: string): string;
|
|
47
52
|
export declare function asError(err: unknown): VPNDetectionError;
|
package/dist/transport.js
CHANGED
|
@@ -131,6 +131,13 @@ export async function withRetry(retries, fn) {
|
|
|
131
131
|
throw asError(err);
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* `encodeURIComponent`, and `!'()*` too, so every reserved character goes out
|
|
136
|
+
* percent-encoded: `+` among them, which a server reads as a space.
|
|
137
|
+
*/
|
|
138
|
+
export function strictEncode(value) {
|
|
139
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
140
|
+
}
|
|
134
141
|
export function asError(err) {
|
|
135
142
|
if (err instanceof VPNDetectionError) {
|
|
136
143
|
return err;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import type { AbuseContact, Asn as WireAsn, BitTorrentDetail, BotDetail, CarrierDetail, ClassDetail, Company, DnsResolverDetail, Location, LookupResponse as WireLookupResponse, ProxyDetail, PubproxyDetail, SatelliteDetail, SmtpDetail, SshDetail, VpnDetail, WebDetail } from './generated/types.gen.js';
|
|
2
|
-
export type { AbuseContact, BitTorrentDetail, BotDetail, CarrierDetail, ClassDetail, Company, DnsResolverDetail, Location, ProxyDetail, PubproxyDetail, SatelliteDetail, SmtpDetail, SshDetail, VpnDetail, WebDetail, };
|
|
1
|
+
import type { AbuseContact, Asn as WireAsn, BitTorrentDetail, BotDetail, CarrierDetail, ClassDetail, Company, DnsResolverDetail, Location, LookupResponse as WireLookupResponse, ProxyDetail, PubproxyDetail, SatelliteDetail, SmtpDetail, SshDetail, UserAgentVerdict, VpnDetail, WebDetail } from './generated/types.gen.js';
|
|
2
|
+
export type { AbuseContact, BitTorrentDetail, BotDetail, CarrierDetail, ClassDetail, Company, DnsResolverDetail, Location, ProxyDetail, PubproxyDetail, SatelliteDetail, SmtpDetail, SshDetail, UserAgentVerdict, VpnDetail, WebDetail, };
|
|
3
3
|
/** The AS announcing an address. Every answer carries `country` and `rpki_status`. */
|
|
4
4
|
export type Asn = Omit<WireAsn, 'country' | 'rpki_status'> & Partial<Pick<WireAsn, 'country' | 'rpki_status'>>;
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
* a user agent sent with the lookup, which this client sends none of.
|
|
8
|
-
*/
|
|
9
|
-
export type LookupResponse = Omit<WireLookupResponse, 'asn' | 'user_agent'> & {
|
|
5
|
+
/** A lookup's answer as it came off the wire. */
|
|
6
|
+
export type LookupResponse = Omit<WireLookupResponse, 'asn'> & {
|
|
10
7
|
asn?: Asn;
|
|
11
8
|
};
|
|
12
9
|
/**
|
|
@@ -86,6 +83,12 @@ export interface Result {
|
|
|
86
83
|
* Paid plans. `agents` and `categories` keep the API's order.
|
|
87
84
|
*/
|
|
88
85
|
bot?: BotDetail;
|
|
86
|
+
/**
|
|
87
|
+
* The agent the lookup's `userAgent` names, and whether this address bears it out: `verified`,
|
|
88
|
+
* `spoofed`, `unverifiable` or `not_bot`. Only when the lookup sent a user agent, with every
|
|
89
|
+
* field; never on a bogon, which is answered locally.
|
|
90
|
+
*/
|
|
91
|
+
userAgent?: UserAgentVerdict;
|
|
89
92
|
/**
|
|
90
93
|
* Where the address is, on every answer, with or without a key: every key present, empty
|
|
91
94
|
* strings where it is not known. The region, city, postal code, coordinates and the rest
|
package/dist/types.js
CHANGED
|
@@ -7,7 +7,7 @@ const RESULT_NAMES = {
|
|
|
7
7
|
is_resproxy: 'isResproxy', is_dcproxy: 'isDcproxy', is_mobproxy: 'isMobproxy', is_pubproxy: 'isPubproxy',
|
|
8
8
|
is_anycast: 'isAnycast', is_carrier: 'isCarrier', is_satellite: 'isSatellite', is_ssh: 'isSsh',
|
|
9
9
|
is_smtp: 'isSmtp', is_web: 'isWeb', is_dns_resolver: 'isDnsResolver', is_bittorrent: 'isBittorrent',
|
|
10
|
-
is_bot: 'isBot', trial_expired: 'trialExpired', cap_reached: 'capReached', bot: 'bot',
|
|
10
|
+
is_bot: 'isBot', trial_expired: 'trialExpired', cap_reached: 'capReached', bot: 'bot', user_agent: 'userAgent',
|
|
11
11
|
vpn: 'vpn', hosting: 'hosting', relay: 'relay', tor: 'tor', cdn: 'cdn', resproxy: 'resproxy', dcproxy: 'dcproxy',
|
|
12
12
|
mobproxy: 'mobproxy', pubproxy: 'pubproxy', carrier: 'carrier', satellite: 'satellite', ssh: 'ssh', smtp: 'smtp',
|
|
13
13
|
web: 'web', dns_resolver: 'dnsResolver', bittorrent: 'bittorrent', location: 'location', asn: 'asn',
|
package/package.json
CHANGED