|
|
|
@ -6,9 +6,10 @@
|
|
|
|
|
import * as http from 'node:http';
|
|
|
|
|
import * as https from 'node:https';
|
|
|
|
|
import * as net from 'node:net';
|
|
|
|
|
import ipaddr from 'ipaddr.js';
|
|
|
|
|
import CacheableLookup from 'cacheable-lookup';
|
|
|
|
|
import fetch from 'node-fetch';
|
|
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
|
|
|
|
import { HttpsProxyAgent } from 'hpagent';
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
|
import type { Config } from '@/config.js';
|
|
|
|
@ -25,8 +26,97 @@ export type HttpRequestSendOptions = {
|
|
|
|
|
validators?: ((res: Response) => void)[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
declare module 'node:http' {
|
|
|
|
|
interface Agent {
|
|
|
|
|
createConnection(options: net.NetConnectOpts, callback?: (err: unknown, stream: net.Socket) => void): net.Socket;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HttpRequestServiceAgent extends http.Agent {
|
|
|
|
|
constructor(
|
|
|
|
|
private config: Config,
|
|
|
|
|
options?: http.AgentOptions,
|
|
|
|
|
) {
|
|
|
|
|
super(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public createConnection(options: net.NetConnectOpts, callback?: (err: unknown, stream: net.Socket) => void): net.Socket {
|
|
|
|
|
const socket = super.createConnection(options, callback)
|
|
|
|
|
.on('connect', () => {
|
|
|
|
|
const address = socket.remoteAddress;
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
if (address && ipaddr.isValid(address)) {
|
|
|
|
|
if (this.isPrivateIp(address)) {
|
|
|
|
|
socket.destroy(new Error(`Blocked address: ${address}`));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return socket;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private isPrivateIp(ip: string): boolean {
|
|
|
|
|
const parsedIp = ipaddr.parse(ip);
|
|
|
|
|
|
|
|
|
|
for (const net of this.config.allowedPrivateNetworks ?? []) {
|
|
|
|
|
const cidr = ipaddr.parseCIDR(net);
|
|
|
|
|
if (cidr[0].kind() === parsedIp.kind() && parsedIp.match(ipaddr.parseCIDR(net))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsedIp.range() !== 'unicast';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HttpsRequestServiceAgent extends https.Agent {
|
|
|
|
|
constructor(
|
|
|
|
|
private config: Config,
|
|
|
|
|
options?: https.AgentOptions,
|
|
|
|
|
) {
|
|
|
|
|
super(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public createConnection(options: net.NetConnectOpts, callback?: (err: unknown, stream: net.Socket) => void): net.Socket {
|
|
|
|
|
const socket = super.createConnection(options, callback)
|
|
|
|
|
.on('connect', () => {
|
|
|
|
|
const address = socket.remoteAddress;
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
if (address && ipaddr.isValid(address)) {
|
|
|
|
|
if (this.isPrivateIp(address)) {
|
|
|
|
|
socket.destroy(new Error(`Blocked address: ${address}`));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return socket;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private isPrivateIp(ip: string): boolean {
|
|
|
|
|
const parsedIp = ipaddr.parse(ip);
|
|
|
|
|
|
|
|
|
|
for (const net of this.config.allowedPrivateNetworks ?? []) {
|
|
|
|
|
const cidr = ipaddr.parseCIDR(net);
|
|
|
|
|
if (cidr[0].kind() === parsedIp.kind() && parsedIp.match(ipaddr.parseCIDR(net))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsedIp.range() !== 'unicast';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class HttpRequestService {
|
|
|
|
|
/**
|
|
|
|
|
* Get https non-proxy agent (without local address filtering)
|
|
|
|
|
*/
|
|
|
|
|
private httpsNative: https.Agent;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get https non-proxy agent
|
|
|
|
|
*/
|
|
|
|
@ -47,13 +137,18 @@ export class HttpRequestService {
|
|
|
|
|
lookup: false, // nativeのdns.lookupにfallbackしない
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.https = new https.Agent({
|
|
|
|
|
const agentOption = {
|
|
|
|
|
keepAlive: true,
|
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
|
lookup: cache.lookup as unknown as net.LookupFunction,
|
|
|
|
|
localAddress: config.outgoingAddress,
|
|
|
|
|
minVersion: 'TLSv1.2',
|
|
|
|
|
});
|
|
|
|
|
minVersion: 'TLSv1.2' as const,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.httpsNative = new https.Agent(agentOption);
|
|
|
|
|
|
|
|
|
|
this.https = new HttpsRequestServiceAgent(config, agentOption);
|
|
|
|
|
|
|
|
|
|
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
|
|
|
|
|
|
|
|
|
@ -91,7 +186,7 @@ export class HttpRequestService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public async getActivityJson(url: string): Promise<IObject> {
|
|
|
|
|
public async getActivityJson(url: string, isLocalAddressAllowed = false): Promise<IObject> {
|
|
|
|
|
const res = await this.send(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
@ -99,6 +194,7 @@ export class HttpRequestService {
|
|
|
|
|
},
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
size: 1024 * 256,
|
|
|
|
|
isLocalAddressAllowed: isLocalAddressAllowed,
|
|
|
|
|
}, {
|
|
|
|
|
throwErrorWhenResponseNotOk: true,
|
|
|
|
|
validators: [validateContentTypeSetAsActivityPub],
|
|
|
|
@ -113,7 +209,7 @@ export class HttpRequestService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public async getJson<T = unknown>(url: string, accept = 'application/json, */*', headers?: Record<string, string>): Promise<T> {
|
|
|
|
|
public async getJson<T = unknown>(url: string, accept = 'application/json, */*', headers?: Record<string, string>, isLocalAddressAllowed = false): Promise<T> {
|
|
|
|
|
const res = await this.send(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: Object.assign({
|
|
|
|
@ -121,19 +217,21 @@ export class HttpRequestService {
|
|
|
|
|
}, headers ?? {}),
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
size: 1024 * 256,
|
|
|
|
|
isLocalAddressAllowed: isLocalAddressAllowed,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await res.json() as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public async getHtml(url: string, accept = 'text/html, */*', headers?: Record<string, string>): Promise<string> {
|
|
|
|
|
public async getHtml(url: string, accept = 'text/html, */*', headers?: Record<string, string>, isLocalAddressAllowed = false): Promise<string> {
|
|
|
|
|
const res = await this.send(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: Object.assign({
|
|
|
|
|
Accept: accept,
|
|
|
|
|
}, headers ?? {}),
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
isLocalAddressAllowed: isLocalAddressAllowed,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await res.text();
|
|
|
|
@ -148,6 +246,7 @@ export class HttpRequestService {
|
|
|
|
|
headers?: Record<string, string>,
|
|
|
|
|
timeout?: number,
|
|
|
|
|
size?: number,
|
|
|
|
|
isLocalAddressAllowed?: boolean,
|
|
|
|
|
} = {},
|
|
|
|
|
extra: HttpRequestSendOptions = {
|
|
|
|
|
throwErrorWhenResponseNotOk: true,
|
|
|
|
@ -179,7 +278,7 @@ export class HttpRequestService {
|
|
|
|
|
},
|
|
|
|
|
body: args.body,
|
|
|
|
|
size: args.size ?? 10 * 1024 * 1024,
|
|
|
|
|
agent: (url) => this.getAgentByUrl(url),
|
|
|
|
|
agent: (url) => this.getAgentByUrl(url, false),
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|