|
| 1 | +import os from 'node:os'; |
| 2 | +import config from 'config'; |
| 3 | +import is from '@sindresorhus/is'; |
| 4 | +import _ from 'lodash'; |
| 5 | +import { scopedLogger } from '../lib/logger.js'; |
| 6 | +import got, { RequestError } from 'got'; |
| 7 | +import type { Socket } from 'socket.io-client'; |
| 8 | + |
| 9 | +const mainLogger = scopedLogger('general'); |
| 10 | +const altIpsLogger = scopedLogger('api:connect:alt-ips-handler'); |
| 11 | + |
| 12 | +class AltIpsManager { |
| 13 | + private readonly INTERVAL_TIME = 10 * 60 * 1000; |
| 14 | + |
| 15 | + socket: Socket; |
| 16 | + ip: string; |
| 17 | + private timer?: NodeJS.Timeout; |
| 18 | + |
| 19 | + constructor (socket: Socket, ip: string) { |
| 20 | + this.socket = socket; |
| 21 | + this.ip = ip; |
| 22 | + this.start(); |
| 23 | + } |
| 24 | + |
| 25 | + updateConfig (socket: Socket, ip: string) { |
| 26 | + this.socket = socket; |
| 27 | + this.ip = ip; |
| 28 | + this.start(); |
| 29 | + } |
| 30 | + |
| 31 | + start () { |
| 32 | + clearTimeout(this.timer); |
| 33 | + this.run(); |
| 34 | + } |
| 35 | + |
| 36 | + private run () { |
| 37 | + void this.refreshAltIps().catch((error) => { |
| 38 | + altIpsLogger.error(error); |
| 39 | + }).finally(() => { |
| 40 | + this.timer = setTimeout(() => this.run(), this.INTERVAL_TIME); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + async refreshAltIps (): Promise<void> { |
| 46 | + const acceptedIps = [ this.ip ]; |
| 47 | + const acceptedIpsWithTokens: { ip: string; token: string }[] = []; |
| 48 | + const rejectedIps: string[] = []; |
| 49 | + const addresses = _(os.networkInterfaces()) |
| 50 | + .values() |
| 51 | + .filter(is.truthy) |
| 52 | + .flatten() |
| 53 | + .uniqBy('address') |
| 54 | + .filter(address => !address.internal) |
| 55 | + .filter(address => !address.address.startsWith('fe80:')) // filter out link-local addresses |
| 56 | + .filter(address => !address.address.startsWith('169.254.')) // filter out link-local addresses |
| 57 | + .value(); |
| 58 | + |
| 59 | + const results = await Promise.allSettled(addresses.map(({ address, family }) => this.getAltIp(address, family === 'IPv6' ? 6 : 4))); |
| 60 | + |
| 61 | + results.forEach((result, index) => { |
| 62 | + if (result.status === 'fulfilled') { |
| 63 | + acceptedIpsWithTokens.push(result.value); |
| 64 | + acceptedIps.push(result.value.ip); |
| 65 | + } else { |
| 66 | + if (!(result.reason instanceof RequestError)) { |
| 67 | + altIpsLogger.warn(result.reason); |
| 68 | + } else if (result.reason.response?.statusCode !== 400) { |
| 69 | + altIpsLogger.warn(`${result.reason.message} (via ${result.reason.options.localAddress}).`); |
| 70 | + } else { |
| 71 | + rejectedIps.push(addresses[index]!.address); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + const uniqAcceptedIps = _(acceptedIps).uniq().value(); |
| 77 | + const uniqRejectedIps = _(rejectedIps).uniq().value(); |
| 78 | + |
| 79 | + if (uniqRejectedIps.length === 1) { |
| 80 | + altIpsLogger.info(`IP address rejected by the API: ${uniqRejectedIps.join(', ')}.`); |
| 81 | + } else if (uniqRejectedIps.length > 1) { |
| 82 | + altIpsLogger.info(`IP addresses rejected by the API: ${uniqRejectedIps.join(', ')}.`); |
| 83 | + } |
| 84 | + |
| 85 | + if (uniqAcceptedIps.length === 1) { |
| 86 | + mainLogger.info(`IP address of the probe: ${uniqAcceptedIps[0]}.`); |
| 87 | + } else { |
| 88 | + mainLogger.info(`IP addresses of the probe: ${uniqAcceptedIps.join(', ')}.`); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + async getAltIp (ip: string, dnsLookupIpVersion: 4 | 6) { |
| 93 | + const httpHost = config.get<string>('api.httpHost'); |
| 94 | + const response = await got.post<{ ip: string; token: string }>(`${httpHost}/alternative-ip`, { |
| 95 | + localAddress: ip, |
| 96 | + dnsLookupIpVersion, |
| 97 | + retry: { |
| 98 | + limit: 2, |
| 99 | + methods: [ 'POST' ], |
| 100 | + statusCodes: [ 504 ], |
| 101 | + }, |
| 102 | + timeout: { |
| 103 | + request: 15_000, |
| 104 | + }, |
| 105 | + responseType: 'json', |
| 106 | + }); |
| 107 | + |
| 108 | + return response.body; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +let altIpsManager: AltIpsManager | null = null; |
| 113 | + |
| 114 | +export const ipHandler = (socket: Socket) => ({ ip }: { ip: string }) => { |
| 115 | + if (!altIpsManager) { |
| 116 | + altIpsManager = new AltIpsManager(socket, ip); |
| 117 | + } else { |
| 118 | + altIpsManager.updateConfig(socket, ip); |
| 119 | + } |
| 120 | +}; |
0 commit comments