|
| 1 | +import fetch from "node-fetch" |
| 2 | +import type { RequestInit, Response } from "node-fetch" |
| 3 | +import Cache from "./cache/cache" |
| 4 | +import LruCache from "./cache/lruCache" |
| 5 | +import ApiLimitError from "./errors/apiLimitError" |
| 6 | +import { isInSubnet } from "subnet-check" |
| 7 | +import { |
| 8 | + REQUEST_TIMEOUT_DEFAULT, |
| 9 | + CACHE_VSN, |
| 10 | + HOST_RES_PROXY, |
| 11 | + BOGON_NETWORKS, |
| 12 | + IPinfoResProxy, |
| 13 | + IPBogon |
| 14 | +} from "./common" |
| 15 | +import VERSION from "./version" |
| 16 | + |
| 17 | +const clientUserAgent = `IPinfoClient/nodejs/${VERSION}` |
| 18 | + |
| 19 | +export default class IPinfoResProxyWrapper { |
| 20 | + private token: string; |
| 21 | + private baseUrl: string; |
| 22 | + private cache: Cache; |
| 23 | + private timeout: number; |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates IPinfoResProxyWrapper object to communicate with the IPinfo Res Proxy API. |
| 27 | + * |
| 28 | + * @param token Token string provided by IPinfo for the registered user. |
| 29 | + * @param cache An implementation of IPCache interface, or LruCache if not specified. |
| 30 | + * @param timeout Request timeout in milliseconds, or 5000ms if not specified. 0 disables the timeout. |
| 31 | + * @param baseUrl The base url to use for api requests, or "ipinfo.io" if not specified. |
| 32 | + */ |
| 33 | + constructor( |
| 34 | + token: string, |
| 35 | + cache?: Cache, |
| 36 | + timeout?: number, |
| 37 | + baseUrl?: string, |
| 38 | + ) { |
| 39 | + this.token = token |
| 40 | + this.cache = cache || new LruCache() |
| 41 | + this.timeout = |
| 42 | + timeout === null || timeout === undefined |
| 43 | + ? REQUEST_TIMEOUT_DEFAULT |
| 44 | + : timeout |
| 45 | + this.baseUrl = baseUrl || `https://${HOST_RES_PROXY}` |
| 46 | + } |
| 47 | + |
| 48 | + public static cacheKey(k: string): string { |
| 49 | + return `${k}:${CACHE_VSN}` |
| 50 | + } |
| 51 | + |
| 52 | + public async fetchApi( |
| 53 | + path: string, |
| 54 | + init: RequestInit = {} |
| 55 | + ): Promise<Response> { |
| 56 | + const headers = { |
| 57 | + Accept: "application/json", |
| 58 | + Authorization: `Bearer ${this.token}`, |
| 59 | + "Content-Type": "application/json", |
| 60 | + "User-Agent": clientUserAgent |
| 61 | + } |
| 62 | + |
| 63 | + const request = Object.assign( |
| 64 | + { |
| 65 | + timeout: this.timeout, |
| 66 | + method: "GET", |
| 67 | + compress: false, |
| 68 | + }, |
| 69 | + init, |
| 70 | + { headers: Object.assign(headers, init.headers) }, |
| 71 | + ) |
| 72 | + |
| 73 | + const url = [this.baseUrl, path].join( |
| 74 | + !this.baseUrl.endsWith("/") && !path.startsWith("/") |
| 75 | + ? "/" |
| 76 | + : "" |
| 77 | + ) |
| 78 | + |
| 79 | + return fetch(url, request).then((response: Response) => { |
| 80 | + if (response.status === 429) { |
| 81 | + throw new ApiLimitError() |
| 82 | + } |
| 83 | + |
| 84 | + if (response.status >= 400) { |
| 85 | + throw new Error( |
| 86 | + `Received an error from the IPinfo API ` + |
| 87 | + `(using authorization ${headers["Authorization"]}) ` + |
| 88 | + `${response.status} ${response.statusText} ${response.url}` |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + return response |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + public async lookupIp( |
| 97 | + ip: string | undefined = undefined |
| 98 | + ): Promise<IPinfoResProxy | IPBogon> { |
| 99 | + if (ip && this.isBogon(ip)) { |
| 100 | + return { ip, bogon: true } |
| 101 | + } |
| 102 | + |
| 103 | + if (!ip) { |
| 104 | + ip = "me" |
| 105 | + } |
| 106 | + |
| 107 | + const data = await this.cache.get(IPinfoResProxyWrapper.cacheKey(ip)) |
| 108 | + |
| 109 | + if (data) { |
| 110 | + return data |
| 111 | + } |
| 112 | + |
| 113 | + return this.fetchApi(ip) |
| 114 | + .then(async (response) => { |
| 115 | + const ipinfo = (await response.json()) as IPinfoResProxy |
| 116 | + this.cache.set(IPinfoResProxyWrapper.cacheKey(ip), ipinfo) |
| 117 | + |
| 118 | + return ipinfo |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + private isBogon(ip: string): boolean { |
| 123 | + if (ip != "") { |
| 124 | + for (let network of BOGON_NETWORKS) { |
| 125 | + if (isInSubnet(ip, network)) { |
| 126 | + return true |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return false |
| 131 | + } |
| 132 | +} |
0 commit comments