-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathipinfoWrapper.ts
More file actions
412 lines (373 loc) · 14 KB
/
ipinfoWrapper.ts
File metadata and controls
412 lines (373 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import fetch from "node-fetch";
import type { RequestInit, Response } from "node-fetch";
import {
defaultContinents,
defaultCountriesCurrencies,
defaultCountriesFlags,
defaultCountries,
defaultEuCountries
} from "../config/utils";
import Cache from "./cache/cache";
import LruCache from "./cache/lruCache";
import ApiLimitError from "./errors/apiLimitError";
const { isInSubnet } = require("subnet-check");
import {
IPinfo,
AsnResponse,
MapResponse,
BatchResponse,
Resproxy,
BATCH_MAX_SIZE,
BATCH_REQ_TIMEOUT_DEFAULT,
REQUEST_TIMEOUT_DEFAULT,
CACHE_VSN,
HOST,
BOGON_NETWORKS
} from "./common";
import VERSION from "./version";
const clientUserAgent = `IPinfoClient/nodejs/${VERSION}`;
const countryFlagURL = "https://cdn.ipinfo.io/static/images/countries-flags/";
export default class IPinfoWrapper {
private token: string;
private baseUrl: string;
private countries: any;
private countriesFlags: any;
private countriesCurrencies: any;
private continents: any;
private euCountries: Array<string>;
private cache: Cache;
private timeout: number;
private mapLimitErrorMessage: string =
"You have exceeded maximum IP upload limit per request.";
/**
* Creates IPinfoWrapper object to communicate with the [IPinfo](https://ipinfo.io/) API.
*
* @param token Token string provided by IPinfo for registered user.
* @param cache An implementation of IPCache interface. If it is not provided
* then LruCache is used as default.
* @param timeout Timeout in milliseconds that controls the timeout of requests.
* It defaults to 5000 i.e. 5 seconds. A timeout of 0 disables the timeout feature.
* @param i18nData Internationalization data for customizing countries-related information.
* @param i18nData.countries Custom countries data. If not provided, default countries data will be used.
* @param i18nData.countriesFlags Custom countries flags data. If not provided, default countries flags data will be used.
* @param i18nData.countriesCurrencies Custom countries currencies data. If not provided, default countries currencies data will be used.
* @param i18nData.continents Custom continents data. If not provided, default continents data will be used.
* @param i18nData.euCountries Custom EU countries data. If not provided or an empty array, default EU countries data will be used.
*/
constructor(
token: string,
cache?: Cache,
timeout?: number,
i18nData?: {
countries?: any;
countriesFlags?: any;
countriesCurrencies?: any;
continents?: any;
euCountries?: Array<string>;
},
baseUrl?: string
) {
this.token = token;
this.countries = i18nData?.countries
? i18nData.countries
: defaultCountries;
this.countriesFlags = i18nData?.countriesFlags
? i18nData.countriesFlags
: defaultCountriesFlags;
this.countriesCurrencies = i18nData?.countriesCurrencies
? i18nData.countriesCurrencies
: defaultCountriesCurrencies;
this.continents = i18nData?.continents
? i18nData.continents
: defaultContinents;
this.euCountries =
i18nData?.euCountries && i18nData?.euCountries.length !== 0
? i18nData.euCountries
: defaultEuCountries;
this.cache = cache ? cache : new LruCache();
this.timeout =
timeout === null || timeout === undefined
? REQUEST_TIMEOUT_DEFAULT
: timeout;
this.baseUrl = baseUrl || `https://${HOST}`;
}
public static cacheKey(k: string) {
return `${k}:${CACHE_VSN}`;
}
public async fetchApi(
path: string,
init: RequestInit = {}
): Promise<Response> {
const headers = {
Accept: "application/json",
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json",
"User-Agent": clientUserAgent
};
const request = Object.assign(
{
timeout: this.timeout,
method: "GET",
compress: false
},
init,
{ headers: Object.assign(headers, init.headers) }
);
const url = [this.baseUrl, path].join(
!this.baseUrl.endsWith("/") && !path.startsWith("/") ? "/" : ""
);
return fetch(url, request).then((response: Response) => {
if (response.status === 429) {
throw new ApiLimitError();
}
if (response.status >= 400) {
throw new Error(
`Received an error from the IPinfo API ` +
`(using authorization ${headers["Authorization"]}) ` +
`${response.status} ${response.statusText} ${response.url}`
);
}
return response;
});
}
/**
* Lookup IP information using the IP.
*
* @param ip IP address against which the location information is required.
* @return Response containing location information.
*/
public async lookupIp(ip: string): Promise<IPinfo> {
if (this.isBogon(ip)) {
const ipinfo: IPinfo = {} as IPinfo;
ipinfo.bogon = true;
ipinfo.ip = ip;
return ipinfo;
}
const data = await this.cache.get(IPinfoWrapper.cacheKey(ip));
if (data) {
return data;
}
return this.fetchApi(`${ip}/json`).then(async (response) => {
const ipinfo = (await response.json()) as IPinfo;
/* convert country code to full country name */
// NOTE: always do this _before_ setting cache.
if (ipinfo.country) {
ipinfo.countryCode = ipinfo.country;
ipinfo.country = this.countries[ipinfo.countryCode];
ipinfo.countryFlag = this.countriesFlags[ipinfo.countryCode];
ipinfo.countryFlagURL =
countryFlagURL + ipinfo.countryCode + ".svg";
ipinfo.countryCurrency =
this.countriesCurrencies[ipinfo.countryCode];
ipinfo.continent = this.continents[ipinfo.countryCode];
ipinfo.isEU = this.euCountries.includes(ipinfo.countryCode);
}
if (ipinfo.abuse && ipinfo.abuse.country) {
ipinfo.abuse.countryCode = ipinfo.abuse.country;
ipinfo.abuse.country =
this.countries[ipinfo.abuse.countryCode];
}
this.cache.set(IPinfoWrapper.cacheKey(ip), ipinfo);
return ipinfo;
});
}
/**
* Lookup ASN information using the AS number.
*
* @param asn the asn string to lookup.
* @return Response containing AsnResponse from the api.
*/
public async lookupASN(asn: string): Promise<AsnResponse> {
const data = await this.cache.get(IPinfoWrapper.cacheKey(asn));
if (data) {
return data;
}
return this.fetchApi(`${asn}/json`).then(async (response) => {
const asnResp = (await response.json()) as AsnResponse;
/* convert country code to full country name */
// NOTE: always do this _before_ setting cache.
if (asnResp.country) {
asnResp.countryCode = asnResp.country;
asnResp.country = this.countries[asnResp.countryCode];
}
this.cache.set(IPinfoWrapper.cacheKey(asn), asnResp);
return asnResp;
});
}
/**
* Lookup residential proxy information using the IP.
*
* @param ip IP address to check for residential proxy information.
* @return Response containing Resproxy data if the IP is a residential proxy.
*/
public async lookupResproxy(ip: string): Promise<Resproxy> {
const cacheKey = `resproxy:${ip}`;
const data = await this.cache.get(IPinfoWrapper.cacheKey(cacheKey));
if (data) {
return data;
}
return this.fetchApi(`resproxy/${ip}`).then(async (response) => {
const resproxy = (await response.json()) as Resproxy;
this.cache.set(IPinfoWrapper.cacheKey(cacheKey), resproxy);
return resproxy;
});
}
/**
* Get a mapping of a list of IPs on a world map.
*
* @param ips the array of IPs to map.
* @return Response containing MapResponse.
*/
public getMap(ips: string[]): Promise<MapResponse> {
if (ips.length > 500000) {
return new Promise((_resolve, reject) => {
reject(new Error(this.mapLimitErrorMessage));
});
}
return this.fetchApi("/tools/map?cli=1", {
method: "POST",
body: JSON.stringify(ips)
}).then((response) => response.json());
}
private __getBatch(
ips: string[],
batchTimeout: number,
filter: boolean
): Promise<any> {
return this.fetchApi(`batch${filter ? "?filter=1" : ""}`, {
Accept: "application/json",
headers: {
"Content-Type": "application/json"
},
method: "POST",
body: JSON.stringify(ips),
timeout: batchTimeout
}).then(async (response: Response) => await response.json());
}
/**
* Get the result of a list of URLs in bulk.
*
* @param urls the array of URLs.
* @param batchSize default value is set to max value for batch size, which is 1000.
* @param batchTimeout in milliseconds. Default value is 5000 milliseconds.
* @param timeoutTotal disabled by default.
* @param filter default value is false.
* @return Response containing BatchResponse for all URLs.
*/
public async getBatch(
urls: string[],
batchSize: number = BATCH_MAX_SIZE,
batchTimeout: number = BATCH_REQ_TIMEOUT_DEFAULT,
timeoutTotal: number = 0,
filter: boolean = false
): Promise<BatchResponse> {
let result: BatchResponse = {};
// no items?
if (urls.length == 0) {
return result;
}
// clip batch size.
if (batchSize <= 0 || batchSize > BATCH_MAX_SIZE) {
batchSize = BATCH_MAX_SIZE;
}
// filter out URLs already cached.
const lookupUrls: string[] = [];
for (const url of urls) {
const cachedUrl = await this.cache.get(
IPinfoWrapper.cacheKey(url)
);
if (cachedUrl) {
result[url] = cachedUrl;
} else {
lookupUrls.push(url);
}
}
// everything cached? exit early.
if (lookupUrls.length == 0) {
return result;
}
const promises: Promise<any>[] = [];
for (let i = 0; i < lookupUrls.length; i += batchSize) {
let end = i + batchSize;
if (end > lookupUrls.length) {
end = lookupUrls.length;
}
const resDetails = this.__getBatch(
lookupUrls.slice(i, end),
batchTimeout,
filter
);
promises.push(resDetails);
}
const batchPromise = Promise.all(promises).then((values) => {
values.forEach((el: any) => {
let batchResp;
try {
batchResp = el;
} catch {
batchResp = {};
}
for (var key in batchResp) {
if (batchResp.hasOwnProperty(key)) {
const ipinfo = batchResp[key];
/* convert country code to full country name */
// NOTE: always do this _before_ setting cache.
if (ipinfo.country) {
ipinfo.countryCode = ipinfo.country;
ipinfo.country =
this.countries[ipinfo.countryCode];
}
if (ipinfo.abuse && ipinfo.abuse.country) {
ipinfo.abuse.countryCode = ipinfo.abuse.country;
ipinfo.abuse.country =
this.countries[ipinfo.abuse.countryCode];
}
if (!ipinfo.error) {
this.cache.set(
IPinfoWrapper.cacheKey(key),
ipinfo
);
}
result[key] = batchResp[key];
}
}
});
});
const totalTimeoutReached = Symbol();
let totalTimeoutRef: any;
const timeoutPromise = new Promise((resolve) => {
if (timeoutTotal > 0) {
totalTimeoutRef = setTimeout(
resolve,
timeoutTotal,
totalTimeoutReached
);
}
});
return await Promise.race([batchPromise, timeoutPromise]).then(
(value) => {
return new Promise((resolve, reject) => {
if (value === totalTimeoutReached) {
reject(new Error("Total timeout has been exceeded."));
} else {
// timeout may still be running; cancel it.
if (totalTimeoutRef) {
clearTimeout(totalTimeoutRef);
}
resolve(result);
}
});
}
);
}
private isBogon(ip: string): boolean {
if (ip != "") {
for (var network of BOGON_NETWORKS) {
if (isInSubnet(ip, network)) {
return true;
}
}
}
return false;
}
}