fix(ipintelligence): apply configured timeout to MMDB downloads and expose IP intelligence timeout option
This commit is contained in:
@@ -522,14 +522,21 @@ export class IpIntelligence {
|
||||
* Fetch a URL and return the response as a Buffer
|
||||
*/
|
||||
private async fetchBuffer(url: string): Promise<Buffer> {
|
||||
const response = await fetch(url, {
|
||||
headers: { 'User-Agent': '@push.rocks/smartnetwork' },
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: HTTP ${response.status}`);
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
signal: controller.signal,
|
||||
headers: { 'User-Agent': '@push.rocks/smartnetwork' },
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: HTTP ${response.status}`);
|
||||
}
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
return Buffer.from(arrayBuffer);
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
return Buffer.from(arrayBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,8 @@ type TSmartdnsClient = InstanceType<typeof plugins.smartdns.dnsClientMod.Smartdn
|
||||
export interface SmartNetworkOptions {
|
||||
/** Cache time-to-live in milliseconds for gateway and public IP lookups */
|
||||
cacheTtl?: number;
|
||||
/** Timeout in milliseconds for IP intelligence RDAP/DNS/MMDB requests. Default: 5000 */
|
||||
ipIntelligenceTimeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -437,7 +439,10 @@ export class SmartNetwork {
|
||||
*/
|
||||
public async getIpIntelligence(ip: string): Promise<IIpIntelligenceResult> {
|
||||
if (!this.ipIntelligence) {
|
||||
this.ipIntelligence = new IpIntelligence({ dnsClient: this.ensureDnsClient() });
|
||||
this.ipIntelligence = new IpIntelligence({
|
||||
dnsClient: this.ensureDnsClient(),
|
||||
timeout: this.options.ipIntelligenceTimeout,
|
||||
});
|
||||
}
|
||||
const fetcher = () => this.ipIntelligence!.getIntelligence(ip);
|
||||
if (this.options.cacheTtl && this.options.cacheTtl > 0) {
|
||||
|
||||
Reference in New Issue
Block a user