2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import * as http from 'node:http';
|
|
|
|
import * as https from 'node:https';
|
2023-07-15 13:07:31 +02:00
|
|
|
import * as net from 'node:net';
|
2022-09-17 20:27:08 +02:00
|
|
|
import CacheableLookup from 'cacheable-lookup';
|
2023-01-25 04:00:04 +01:00
|
|
|
import fetch from 'node-fetch';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { StatusError } from '@/misc/status-error.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-25 04:00:04 +01:00
|
|
|
import type { Response } from 'node-fetch';
|
|
|
|
import type { URL } from 'node:url';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
@Injectable()
|
|
|
|
export class HttpRequestService {
|
2022-09-17 20:27:08 +02:00
|
|
|
/**
|
2023-01-25 04:00:04 +01:00
|
|
|
* Get http non-proxy agent
|
2022-09-17 20:27:08 +02:00
|
|
|
*/
|
2023-01-25 04:00:04 +01:00
|
|
|
private http: http.Agent;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
/**
|
2023-01-25 04:00:04 +01:00
|
|
|
* Get https non-proxy agent
|
2022-09-17 20:27:08 +02:00
|
|
|
*/
|
2023-01-25 04:00:04 +01:00
|
|
|
private https: https.Agent;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
/**
|
2023-01-25 04:00:04 +01:00
|
|
|
* Get http proxy or non-proxy agent
|
2022-09-17 20:27:08 +02:00
|
|
|
*/
|
|
|
|
public httpAgent: http.Agent;
|
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
/**
|
|
|
|
* Get https proxy or non-proxy agent
|
|
|
|
*/
|
2022-09-17 20:27:08 +02:00
|
|
|
public httpsAgent: https.Agent;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
) {
|
2023-01-25 04:00:04 +01:00
|
|
|
const cache = new CacheableLookup({
|
2022-09-17 20:27:08 +02:00
|
|
|
maxTtl: 3600, // 1hours
|
|
|
|
errorTtl: 30, // 30secs
|
|
|
|
lookup: false, // nativeのdns.lookupにfallbackしない
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.http = new http.Agent({
|
2022-09-17 20:27:08 +02:00
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
2023-07-15 13:07:31 +02:00
|
|
|
lookup: cache.lookup as unknown as net.LookupFunction,
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.https = new https.Agent({
|
2022-09-17 20:27:08 +02:00
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
2023-07-15 13:07:31 +02:00
|
|
|
lookup: cache.lookup as unknown as net.LookupFunction,
|
|
|
|
});
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
this.httpAgent = config.proxy
|
|
|
|
? new HttpProxyAgent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
2023-01-25 04:00:04 +01:00
|
|
|
maxSockets,
|
2022-09-17 20:27:08 +02:00
|
|
|
maxFreeSockets: 256,
|
|
|
|
scheduling: 'lifo',
|
|
|
|
proxy: config.proxy,
|
|
|
|
})
|
2022-09-18 20:11:50 +02:00
|
|
|
: this.http;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
this.httpsAgent = config.proxy
|
|
|
|
? new HttpsProxyAgent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
2023-01-25 04:00:04 +01:00
|
|
|
maxSockets,
|
2022-09-17 20:27:08 +02:00
|
|
|
maxFreeSockets: 256,
|
|
|
|
scheduling: 'lifo',
|
|
|
|
proxy: config.proxy,
|
|
|
|
})
|
2022-09-18 20:11:50 +02:00
|
|
|
: this.https;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-25 04:00:04 +01:00
|
|
|
* Get agent by URL
|
2022-09-17 20:27:08 +02:00
|
|
|
* @param url URL
|
|
|
|
* @param bypassProxy Allways bypass proxy
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-01-25 04:00:04 +01:00
|
|
|
public getAgentByUrl(url: URL, bypassProxy = false): http.Agent | https.Agent {
|
2022-09-17 20:27:08 +02:00
|
|
|
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
|
2022-09-18 20:11:50 +02:00
|
|
|
return url.protocol === 'http:' ? this.http : this.https;
|
2022-09-17 20:27:08 +02:00
|
|
|
} else {
|
|
|
|
return url.protocol === 'http:' ? this.httpAgent : this.httpsAgent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-02-01 12:13:22 +01:00
|
|
|
public async getJson<T = unknown>(url: string, accept = 'application/json, */*', headers?: Record<string, string>): Promise<T> {
|
2023-01-25 04:00:04 +01:00
|
|
|
const res = await this.send(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: Object.assign({
|
|
|
|
Accept: accept,
|
|
|
|
}, headers ?? {}),
|
|
|
|
timeout: 5000,
|
|
|
|
size: 1024 * 256,
|
|
|
|
});
|
|
|
|
|
2023-02-01 12:13:22 +01:00
|
|
|
return await res.json() as T;
|
2023-01-25 04:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async getHtml(url: string, accept = 'text/html, */*', headers?: Record<string, string>): Promise<string> {
|
|
|
|
const res = await this.send(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: Object.assign({
|
|
|
|
Accept: accept,
|
|
|
|
}, headers ?? {}),
|
|
|
|
timeout: 5000,
|
|
|
|
});
|
|
|
|
|
|
|
|
return await res.text();
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
@bindThis
|
|
|
|
public async send(url: string, args: {
|
|
|
|
method?: string,
|
|
|
|
body?: string,
|
|
|
|
headers?: Record<string, string>,
|
|
|
|
timeout?: number,
|
|
|
|
size?: number,
|
|
|
|
} = {}, extra: {
|
|
|
|
throwErrorWhenResponseNotOk: boolean;
|
|
|
|
} = {
|
|
|
|
throwErrorWhenResponseNotOk: true,
|
|
|
|
}): Promise<Response> {
|
|
|
|
const timeout = args.timeout ?? 5000;
|
|
|
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: args.method ?? 'GET',
|
2023-02-14 05:08:56 +01:00
|
|
|
headers: {
|
|
|
|
'User-Agent': this.config.userAgent,
|
2023-07-15 13:07:31 +02:00
|
|
|
...(args.headers ?? {}),
|
2023-02-14 05:08:56 +01:00
|
|
|
},
|
2023-01-25 04:00:04 +01:00
|
|
|
body: args.body,
|
|
|
|
size: args.size ?? 10 * 1024 * 1024,
|
|
|
|
agent: (url) => this.getAgentByUrl(url),
|
|
|
|
signal: controller.signal,
|
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
if (!res.ok && extra.throwErrorWhenResponseNotOk) {
|
|
|
|
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-25 04:00:04 +01:00
|
|
|
return res;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|