This commit is contained in:
parent
49da90289a
commit
80f9a81ac4
|
@ -4,6 +4,7 @@ import type { UsersRepository } from '@/models/index.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
import { fetch } from 'undici';
|
||||||
|
|
||||||
type CaptchaResponse = {
|
type CaptchaResponse = {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
|
@ -35,7 +36,7 @@ export class CaptchaService {
|
||||||
},
|
},
|
||||||
// TODO
|
// TODO
|
||||||
//timeout: 10 * 1000,
|
//timeout: 10 * 1000,
|
||||||
agent: (url, bypassProxy) => this.httpRequestService.getAgentByUrl(url, bypassProxy),
|
dispatcher: this.httpRequestService.getAgentByUrl(new URL(url)),
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
throw `${err.message ?? err}`;
|
throw `${err.message ?? err}`;
|
||||||
});
|
});
|
||||||
|
|
|
@ -34,57 +34,29 @@ export class DownloadService {
|
||||||
@bindThis
|
@bindThis
|
||||||
public async downloadUrl(url: string, path: string): Promise<void> {
|
public async downloadUrl(url: string, path: string): Promise<void> {
|
||||||
this.logger.info(`Downloading ${chalk.cyan(url)} to ${chalk.cyanBright(path)} ...`);
|
this.logger.info(`Downloading ${chalk.cyan(url)} to ${chalk.cyanBright(path)} ...`);
|
||||||
|
|
||||||
const timeout = 30 * 1000;
|
const timeout = 30 * 1000;
|
||||||
const operationTimeout = 60 * 1000;
|
const operationTimeout = 60 * 1000;
|
||||||
const maxSize = this.config.maxFileSize ?? 262144000;
|
const maxSize = this.config.maxFileSize ?? 262144000;
|
||||||
|
|
||||||
const req = got.stream(url, {
|
const response = await this.httpRequestService.fetch({
|
||||||
|
method: 'GET',
|
||||||
|
url,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': this.config.userAgent,
|
'User-Agent': this.config.userAgent,
|
||||||
},
|
},
|
||||||
timeout: {
|
timeout,
|
||||||
lookup: timeout,
|
size: maxSize,
|
||||||
connect: timeout,
|
ipCheckers:
|
||||||
secureConnect: timeout,
|
(process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') &&
|
||||||
socket: timeout, // read timeout
|
!this.config.proxy ?
|
||||||
response: timeout,
|
[{ type: 'black', fn: this.isPrivateIp }] :
|
||||||
send: timeout,
|
undefined,
|
||||||
request: operationTimeout, // whole operation timeout
|
// http2: false, // default
|
||||||
},
|
|
||||||
agent: {
|
|
||||||
http: this.httpRequestService.httpAgent,
|
|
||||||
https: this.httpRequestService.httpsAgent,
|
|
||||||
},
|
|
||||||
http2: false, // default
|
|
||||||
retry: {
|
|
||||||
limit: 0,
|
|
||||||
},
|
|
||||||
}).on('response', (res: Got.Response) => {
|
|
||||||
if ((process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') && !this.config.proxy && res.ip) {
|
|
||||||
if (this.isPrivateIp(res.ip)) {
|
|
||||||
this.logger.warn(`Blocked address: ${res.ip}`);
|
|
||||||
req.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const contentLength = res.headers['content-length'];
|
|
||||||
if (contentLength != null) {
|
|
||||||
const size = Number(contentLength);
|
|
||||||
if (size > maxSize) {
|
|
||||||
this.logger.warn(`maxSize exceeded (${size} > ${maxSize}) on response`);
|
|
||||||
req.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).on('downloadProgress', (progress: Got.Progress) => {
|
|
||||||
if (progress.transferred > maxSize) {
|
|
||||||
this.logger.warn(`maxSize exceeded (${progress.transferred} > ${maxSize}) on downloadProgress`);
|
|
||||||
req.destroy();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await pipeline(req, fs.createWriteStream(path));
|
await pipeline(stream.Readable.fromWeb(streaming), fs.createWriteStream(path));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Got.HTTPError) {
|
if (e instanceof Got.HTTPError) {
|
||||||
throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage);
|
throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage);
|
||||||
|
@ -124,6 +96,6 @@ export class DownloadService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return PrivateIp(ip);
|
return PrivateIp(ip) ?? false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { JSDOM } from 'jsdom';
|
import { JSDOM } from 'jsdom';
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import tinycolor from 'tinycolor2';
|
import tinycolor from 'tinycolor2';
|
||||||
import type { Instance } from '@/models/entities/Instance.js';
|
import type { Instance } from '@/models/entities/Instance.js';
|
||||||
import type { InstancesRepository } from '@/models/index.js';
|
import type { InstancesRepository } from '@/models/index.js';
|
||||||
|
@ -191,10 +190,8 @@ export class FetchInstanceMetadataService {
|
||||||
|
|
||||||
const faviconUrl = url + '/favicon.ico';
|
const faviconUrl = url + '/favicon.ico';
|
||||||
|
|
||||||
const favicon = await fetch(faviconUrl, {
|
const favicon = await this.httpRequestService.fetch({
|
||||||
// TODO
|
url: faviconUrl,
|
||||||
//timeout: 10000,
|
|
||||||
agent: url => this.httpRequestService.getAgentByUrl(url),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (favicon.ok) {
|
if (favicon.ok) {
|
||||||
|
|
|
@ -1,178 +0,0 @@
|
||||||
import CacheableLookup from 'cacheable-lookup';
|
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
|
||||||
import { DI } from '@/di-symbols.js';
|
|
||||||
import type { Config } from '@/config.js';
|
|
||||||
import { StatusError } from '@/misc/status-error.js';
|
|
||||||
import { bindThis } from '@/decorators.js';
|
|
||||||
import * as undici from 'undici';
|
|
||||||
import { LookupFunction } from 'node:net';
|
|
||||||
import { TransformStream } from 'node:stream/web';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class HttpRequestService {
|
|
||||||
/**
|
|
||||||
* Get http non-proxy agent
|
|
||||||
*/
|
|
||||||
private agent: undici.Agent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get http proxy or non-proxy agent
|
|
||||||
*/
|
|
||||||
public proxiedAgent: undici.ProxyAgent | undici.Agent;
|
|
||||||
|
|
||||||
public readonly clientDefaults: undici.Agent.Options;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
@Inject(DI.config)
|
|
||||||
private config: Config,
|
|
||||||
) {
|
|
||||||
const cache = new CacheableLookup({
|
|
||||||
maxTtl: 3600, // 1hours
|
|
||||||
errorTtl: 30, // 30secs
|
|
||||||
lookup: false, // nativeのdns.lookupにfallbackしない
|
|
||||||
});
|
|
||||||
|
|
||||||
this.clientDefaults = {
|
|
||||||
keepAliveTimeout: 4 * 1000,
|
|
||||||
keepAliveMaxTimeout: 10 * 60 * 1000,
|
|
||||||
keepAliveTimeoutThreshold: 1 * 1000,
|
|
||||||
strictContentLength: true,
|
|
||||||
connect: {
|
|
||||||
maxCachedSessions: 100, // TLSセッションのキャッシュ数 https://github.com/nodejs/undici/blob/v5.14.0/lib/core/connect.js#L80
|
|
||||||
lookup: cache.lookup as LookupFunction, // https://github.com/nodejs/undici/blob/v5.14.0/lib/core/connect.js#L98
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
this.agent = new undici.Agent({
|
|
||||||
...this.clientDefaults,
|
|
||||||
});
|
|
||||||
|
|
||||||
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
|
|
||||||
|
|
||||||
this.proxiedAgent = config.proxy
|
|
||||||
? new undici.ProxyAgent({
|
|
||||||
...this.clientDefaults,
|
|
||||||
connections: maxSockets,
|
|
||||||
|
|
||||||
uri: config.proxy,
|
|
||||||
})
|
|
||||||
: this.agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get agent by URL
|
|
||||||
* @param url URL
|
|
||||||
* @param bypassProxy Allways bypass proxy
|
|
||||||
*/
|
|
||||||
@bindThis
|
|
||||||
public getAgentByUrl(url: URL, bypassProxy = false): undici.Agent | undici.ProxyAgent {
|
|
||||||
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
|
|
||||||
return this.agent;
|
|
||||||
} else {
|
|
||||||
return this.proxiedAgent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@bindThis
|
|
||||||
public async getJson(url: string, accept = 'application/json, */*', timeout = 10000, headers?: Record<string, string>): Promise<unknown> {
|
|
||||||
const res = await this.getResponse({
|
|
||||||
url,
|
|
||||||
method: 'GET',
|
|
||||||
headers: Object.assign({
|
|
||||||
'User-Agent': this.config.userAgent,
|
|
||||||
Accept: accept,
|
|
||||||
}, headers ?? {}),
|
|
||||||
timeout,
|
|
||||||
size: 1024 * 256,
|
|
||||||
});
|
|
||||||
|
|
||||||
return await res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
@bindThis
|
|
||||||
public async getHtml(url: string, accept = 'text/html, */*', timeout = 10000, headers?: Record<string, string>): Promise<string> {
|
|
||||||
const res = await this.getResponse({
|
|
||||||
url,
|
|
||||||
method: 'GET',
|
|
||||||
headers: Object.assign({
|
|
||||||
'User-Agent': this.config.userAgent,
|
|
||||||
Accept: accept,
|
|
||||||
}, headers ?? {}),
|
|
||||||
timeout,
|
|
||||||
});
|
|
||||||
|
|
||||||
return await res.text();
|
|
||||||
}
|
|
||||||
|
|
||||||
@bindThis
|
|
||||||
public async getResponse(args: {
|
|
||||||
url: string,
|
|
||||||
method: string,
|
|
||||||
body?: string,
|
|
||||||
headers: Record<string, string>,
|
|
||||||
timeout?: number,
|
|
||||||
size?: number,
|
|
||||||
redirect?: RequestRedirect | undefined,
|
|
||||||
dispatcher?: undici.Dispatcher,
|
|
||||||
}): Promise<undici.Response> {
|
|
||||||
const timeout = args.timeout ?? 10 * 1000;
|
|
||||||
|
|
||||||
const controller = new AbortController();
|
|
||||||
setTimeout(() => {
|
|
||||||
controller.abort();
|
|
||||||
}, timeout * 6);
|
|
||||||
|
|
||||||
const res = await Promise.race([
|
|
||||||
undici.fetch(args.url, {
|
|
||||||
method: args.method,
|
|
||||||
headers: args.headers,
|
|
||||||
body: args.body,
|
|
||||||
redirect: args.redirect,
|
|
||||||
dispatcher: args.dispatcher ?? this.getAgentByUrl(new URL(args.url)),
|
|
||||||
keepalive: true,
|
|
||||||
signal: controller.signal,
|
|
||||||
}),
|
|
||||||
new Promise<null>((res) => setTimeout(() => res(null)))
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (res == null) {
|
|
||||||
throw new StatusError(`Request Timeout`, 408, 'Request Timeout');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ({
|
|
||||||
...res,
|
|
||||||
body: this.fetchLimiter(res, args.size),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch body limiter
|
|
||||||
* @param res undici.Response
|
|
||||||
* @param size number of Max size (Bytes) (default: 10MiB)
|
|
||||||
* @returns ReadableStream<Uint8Array> (provided by node:stream/web)
|
|
||||||
*/
|
|
||||||
@bindThis
|
|
||||||
private fetchLimiter(res: undici.Response, size: number = 10 * 1024 * 1024) {
|
|
||||||
if (res.body == null) return null;
|
|
||||||
|
|
||||||
let total = 0;
|
|
||||||
return res.body.pipeThrough(new TransformStream({
|
|
||||||
start() {},
|
|
||||||
transform(chunk, controller) {
|
|
||||||
// TypeScirptグローバルの定義はUnit8ArrayだがundiciはReadableStream<any>を渡してくるので一応変換
|
|
||||||
const uint8 = new Uint8Array(chunk);
|
|
||||||
total += uint8.length;
|
|
||||||
if (total > size) {
|
|
||||||
controller.error(new StatusError(`Payload Too Large`, 413, 'Payload Too Large'));
|
|
||||||
} else {
|
|
||||||
controller.enqueue(uint8);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
flush() {},
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +1,31 @@
|
||||||
import * as http from 'node:http';
|
import * as http from 'node:http';
|
||||||
import * as https from 'node:https';
|
import * as https from 'node:https';
|
||||||
import CacheableLookup from 'cacheable-lookup';
|
import CacheableLookup from 'cacheable-lookup';
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import { StatusError } from '@/misc/status-error.js';
|
import { StatusError } from '@/misc/status-error.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
import type { Response } from 'node-fetch';
|
import * as undici from 'undici';
|
||||||
import type { URL } from 'node:url';
|
import { LookupFunction } from 'node:net';
|
||||||
|
import { TransformStream } from 'node:stream/web';
|
||||||
|
import * as dns from 'node:dns';
|
||||||
|
|
||||||
|
export type IpChecker = (ip: string) => boolean;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HttpRequestService {
|
export class HttpRequestService {
|
||||||
|
/**
|
||||||
|
* Get http non-proxy agent (undici)
|
||||||
|
*/
|
||||||
|
private agent: undici.Agent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get http proxy or non-proxy agent (undici)
|
||||||
|
*/
|
||||||
|
public proxiedAgent: undici.ProxyAgent | undici.Agent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get http non-proxy agent
|
* Get http non-proxy agent
|
||||||
*/
|
*/
|
||||||
|
@ -33,30 +46,57 @@ export class HttpRequestService {
|
||||||
*/
|
*/
|
||||||
public httpsAgent: https.Agent;
|
public httpsAgent: https.Agent;
|
||||||
|
|
||||||
|
public readonly dnsCache: CacheableLookup;
|
||||||
|
public readonly clientDefaults: undici.Agent.Options;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DI.config)
|
@Inject(DI.config)
|
||||||
private config: Config,
|
private config: Config,
|
||||||
) {
|
) {
|
||||||
const cache = new CacheableLookup({
|
this.dnsCache = new CacheableLookup({
|
||||||
maxTtl: 3600, // 1hours
|
maxTtl: 3600, // 1hours
|
||||||
errorTtl: 30, // 30secs
|
errorTtl: 30, // 30secs
|
||||||
lookup: false, // nativeのdns.lookupにfallbackしない
|
lookup: false, // nativeのdns.lookupにfallbackしない
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.clientDefaults = {
|
||||||
|
keepAliveTimeout: 4 * 1000,
|
||||||
|
keepAliveMaxTimeout: 10 * 60 * 1000,
|
||||||
|
keepAliveTimeoutThreshold: 1 * 1000,
|
||||||
|
strictContentLength: true,
|
||||||
|
connect: {
|
||||||
|
maxCachedSessions: 100, // TLSセッションのキャッシュ数 https://github.com/nodejs/undici/blob/v5.14.0/lib/core/connect.js#L80
|
||||||
|
lookup: this.dnsCache.lookup as LookupFunction, // https://github.com/nodejs/undici/blob/v5.14.0/lib/core/connect.js#L98
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
this.agent = new undici.Agent({
|
||||||
|
...this.clientDefaults,
|
||||||
|
});
|
||||||
|
|
||||||
this.http = new http.Agent({
|
this.http = new http.Agent({
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
keepAliveMsecs: 30 * 1000,
|
keepAliveMsecs: 30 * 1000,
|
||||||
lookup: cache.lookup,
|
lookup: this.dnsCache.lookup,
|
||||||
} as http.AgentOptions);
|
} as http.AgentOptions);
|
||||||
|
|
||||||
this.https = new https.Agent({
|
this.https = new https.Agent({
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
keepAliveMsecs: 30 * 1000,
|
keepAliveMsecs: 30 * 1000,
|
||||||
lookup: cache.lookup,
|
lookup: this.dnsCache.lookup,
|
||||||
} as https.AgentOptions);
|
} as https.AgentOptions);
|
||||||
|
|
||||||
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
|
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
|
||||||
|
|
||||||
|
this.proxiedAgent = config.proxy
|
||||||
|
? new undici.ProxyAgent({
|
||||||
|
...this.clientDefaults,
|
||||||
|
connections: maxSockets,
|
||||||
|
|
||||||
|
uri: config.proxy,
|
||||||
|
})
|
||||||
|
: this.agent;
|
||||||
|
|
||||||
this.httpAgent = config.proxy
|
this.httpAgent = config.proxy
|
||||||
? new HttpProxyAgent({
|
? new HttpProxyAgent({
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
|
@ -86,19 +126,49 @@ export class HttpRequestService {
|
||||||
* @param bypassProxy Allways bypass proxy
|
* @param bypassProxy Allways bypass proxy
|
||||||
*/
|
*/
|
||||||
@bindThis
|
@bindThis
|
||||||
public getAgentByUrl(url: URL, bypassProxy = false): http.Agent | https.Agent {
|
public getAgentByUrl(url: URL, bypassProxy = false): undici.Agent | undici.ProxyAgent {
|
||||||
|
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
|
||||||
|
return this.agent;
|
||||||
|
} else {
|
||||||
|
return this.proxiedAgent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get agent by URL
|
||||||
|
* @param url URL
|
||||||
|
* @param bypassProxy Allways bypass proxy
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
public getHttpAgentByUrl(url: URL, bypassProxy = false): http.Agent | https.Agent {
|
||||||
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
|
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
|
||||||
return url.protocol === 'http:' ? this.http : this.https;
|
return url.protocol === 'http:' ? this.http : this.https;
|
||||||
} else {
|
} else {
|
||||||
return url.protocol === 'http:' ? this.httpAgent : this.httpsAgent;
|
return url.protocol === 'http:' ? this.httpAgent : this.httpsAgent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* check ip
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
public checkIp(url: URL, fn: IpChecker): Promise<boolean> {
|
||||||
|
const lookup = this.dnsCache.lookup as LookupFunction || dns.lookup;
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
lookup(url.hostname, {}, (err, ip) => {
|
||||||
|
if (err) {
|
||||||
|
resolve(false);
|
||||||
|
} else {
|
||||||
|
resolve(fn(ip));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async getJson(url: string, accept = 'application/json, */*', timeout = 10000, headers?: Record<string, string>): Promise<unknown> {
|
public async getJson<T extends unknown>(url: string, accept = 'application/json, */*', timeout = 10000, headers?: Record<string, string>): Promise<T> {
|
||||||
const res = await this.getResponse({
|
const res = await this.fetch({
|
||||||
url,
|
url,
|
||||||
method: 'GET',
|
|
||||||
headers: Object.assign({
|
headers: Object.assign({
|
||||||
'User-Agent': this.config.userAgent,
|
'User-Agent': this.config.userAgent,
|
||||||
Accept: accept,
|
Accept: accept,
|
||||||
|
@ -112,9 +182,8 @@ export class HttpRequestService {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async getHtml(url: string, accept = 'text/html, */*', timeout = 10000, headers?: Record<string, string>): Promise<string> {
|
public async getHtml(url: string, accept = 'text/html, */*', timeout = 10000, headers?: Record<string, string>): Promise<string> {
|
||||||
const res = await this.getResponse({
|
const res = await this.fetch({
|
||||||
url,
|
url,
|
||||||
method: 'GET',
|
|
||||||
headers: Object.assign({
|
headers: Object.assign({
|
||||||
'User-Agent': this.config.userAgent,
|
'User-Agent': this.config.userAgent,
|
||||||
Accept: accept,
|
Accept: accept,
|
||||||
|
@ -126,14 +195,35 @@ export class HttpRequestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async getResponse(args: {
|
public async fetch(args: {
|
||||||
url: string,
|
url: string,
|
||||||
method: string,
|
method?: string,
|
||||||
body?: string,
|
body?: string,
|
||||||
headers: Record<string, string>,
|
headers?: Record<string, string>,
|
||||||
timeout?: number,
|
timeout?: number,
|
||||||
size?: number,
|
size?: number,
|
||||||
}): Promise<Response> {
|
redirect?: RequestRedirect | undefined,
|
||||||
|
dispatcher?: undici.Dispatcher,
|
||||||
|
ipCheckers?: {
|
||||||
|
type: 'black' | 'white',
|
||||||
|
fn: IpChecker,
|
||||||
|
}[],
|
||||||
|
noOkError?: boolean,
|
||||||
|
}): Promise<undici.Response> {
|
||||||
|
const url = new URL(args.url);
|
||||||
|
|
||||||
|
if (args.ipCheckers) {
|
||||||
|
for (const check of args.ipCheckers) {
|
||||||
|
const result = await this.checkIp(url, check.fn);
|
||||||
|
if (
|
||||||
|
(check.type === 'black' && result === true) ||
|
||||||
|
(check.type === 'white' && result === false)
|
||||||
|
) {
|
||||||
|
throw new StatusError('IP is not allowed', 403, 'IP is not allowed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const timeout = args.timeout ?? 10 * 1000;
|
const timeout = args.timeout ?? 10 * 1000;
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
@ -141,20 +231,57 @@ export class HttpRequestService {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
}, timeout * 6);
|
}, timeout * 6);
|
||||||
|
|
||||||
const res = await fetch(args.url, {
|
const res = await Promise.race([
|
||||||
method: args.method,
|
undici.fetch(args.url, {
|
||||||
headers: args.headers,
|
method: args.method ?? 'GET',
|
||||||
body: args.body,
|
headers: args.headers,
|
||||||
timeout,
|
body: args.body,
|
||||||
size: args.size ?? 10 * 1024 * 1024,
|
redirect: args.redirect,
|
||||||
agent: (url) => this.getAgentByUrl(url),
|
dispatcher: args.dispatcher ?? this.getAgentByUrl(url),
|
||||||
signal: controller.signal,
|
keepalive: true,
|
||||||
});
|
signal: controller.signal,
|
||||||
|
}),
|
||||||
|
new Promise<null>((res) => setTimeout(() => res(null)))
|
||||||
|
]);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (res == null) {
|
||||||
|
throw new StatusError(`Request Timeout`, 408, 'Request Timeout');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!res.ok && !args.noOkError) {
|
||||||
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
|
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return ({
|
||||||
|
...res,
|
||||||
|
body: this.fetchLimiter(res, args.size),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch body limiter
|
||||||
|
* @param res undici.Response
|
||||||
|
* @param size number of Max size (Bytes) (default: 10MiB)
|
||||||
|
* @returns ReadableStream<Uint8Array> (provided by node:stream/web)
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
private fetchLimiter(res: undici.Response, size: number = 10 * 1024 * 1024) {
|
||||||
|
if (res.body == null) return null;
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
return res.body.pipeThrough(new TransformStream({
|
||||||
|
start() {},
|
||||||
|
transform(chunk, controller) {
|
||||||
|
// TypeScirptグローバルの定義はUnit8ArrayだがundiciはReadableStream<any>を渡してくるので一応変換
|
||||||
|
const uint8 = new Uint8Array(chunk);
|
||||||
|
total += uint8.length;
|
||||||
|
if (total > size) {
|
||||||
|
controller.error(new StatusError(`Payload Too Large`, 413, 'Payload Too Large'));
|
||||||
|
} else {
|
||||||
|
controller.enqueue(uint8);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
flush() {},
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ export class S3Service {
|
||||||
? false
|
? false
|
||||||
: meta.objectStorageS3ForcePathStyle,
|
: meta.objectStorageS3ForcePathStyle,
|
||||||
httpOptions: {
|
httpOptions: {
|
||||||
agent: this.httpRequestService.getAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
|
agent: this.httpRequestService.getHttpAgentByUrl(new URL(u), !meta.objectStorageUseProxy),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ export class WebfingerService {
|
||||||
public async webfinger(query: string): Promise<IWebFinger> {
|
public async webfinger(query: string): Promise<IWebFinger> {
|
||||||
const url = this.genUrl(query);
|
const url = this.genUrl(query);
|
||||||
|
|
||||||
return await this.httpRequestService.getJson(url, 'application/jrd+json, application/json') as IWebFinger;
|
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
|
|
|
@ -152,7 +152,7 @@ export class ApRequestService {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.httpRequestService.getResponse({
|
await this.httpRequestService.fetch({
|
||||||
url,
|
url,
|
||||||
method: req.request.method,
|
method: req.request.method,
|
||||||
headers: req.request.headers,
|
headers: req.request.headers,
|
||||||
|
@ -180,7 +180,7 @@ export class ApRequestService {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await this.httpRequestService.getResponse({
|
const res = await this.httpRequestService.fetch({
|
||||||
url,
|
url,
|
||||||
method: req.request.method,
|
method: req.request.method,
|
||||||
headers: req.request.headers,
|
headers: req.request.headers,
|
||||||
|
|
|
@ -96,8 +96,8 @@ export class Resolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
const object = (this.user
|
const object = (this.user
|
||||||
? await this.apRequestService.signedGet(value, this.user)
|
? await this.apRequestService.signedGet(value, this.user) as IObject
|
||||||
: await this.httpRequestService.getJson(value, 'application/activity+json, application/ld+json')) as IObject;
|
: await this.httpRequestService.getJson<IObject>(value, 'application/activity+json, application/ld+json'));
|
||||||
|
|
||||||
if (object == null || (
|
if (object == null || (
|
||||||
Array.isArray(object['@context']) ?
|
Array.isArray(object['@context']) ?
|
||||||
|
|
|
@ -116,13 +116,14 @@ class LdSignature {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
private async fetchDocument(url: string) {
|
private async fetchDocument(url: string) {
|
||||||
const json = await fetch(url, {
|
const json = await this.httpRequestService.fetch({
|
||||||
|
url,
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/ld+json, application/json',
|
Accept: 'application/ld+json, application/json',
|
||||||
},
|
},
|
||||||
|
noOkError: true,
|
||||||
// TODO
|
// TODO
|
||||||
//timeout: this.loderTimeout,
|
//timeout: this.loderTimeout,
|
||||||
agent: u => u.protocol === 'http:' ? this.httpRequestService.httpAgent : this.httpRequestService.httpsAgent,
|
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw `${res.status} ${res.statusText}`;
|
throw `${res.status} ${res.statusText}`;
|
||||||
|
|
|
@ -33,7 +33,7 @@ export class WebhookDeliverProcessorService {
|
||||||
try {
|
try {
|
||||||
this.logger.debug(`delivering ${job.data.webhookId}`);
|
this.logger.debug(`delivering ${job.data.webhookId}`);
|
||||||
|
|
||||||
const res = await this.httpRequestService.getResponse({
|
const res = await this.httpRequestService.fetch({
|
||||||
url: job.data.to,
|
url: job.data.to,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
@ -33,7 +33,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||||
private httpRequestService: HttpRequestService,
|
private httpRequestService: HttpRequestService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps, me) => {
|
||||||
const res = await this.httpRequestService.getResponse({
|
const res = await this.httpRequestService.fetch({
|
||||||
url: ps.url,
|
url: ps.url,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: Object.assign({
|
headers: Object.assign({
|
||||||
|
|
|
@ -84,17 +84,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||||
|
|
||||||
const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
|
const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
|
||||||
|
|
||||||
const res = await fetch(endpoint, {
|
const res = await this.httpRequestService.fetch({
|
||||||
|
url: endpoint,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
'User-Agent': config.userAgent,
|
'User-Agent': config.userAgent,
|
||||||
Accept: 'application/json, */*',
|
Accept: 'application/json, */*',
|
||||||
},
|
},
|
||||||
body: params,
|
body: params.toString(),
|
||||||
// TODO
|
// TODO
|
||||||
//timeout: 10000,
|
//timeout: 10000,
|
||||||
agent: (url) => this.httpRequestService.getAgentByUrl(url),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const json = (await res.json()) as {
|
const json = (await res.json()) as {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import summaly from 'summaly';
|
import * as summaly from 'summaly';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import type { UsersRepository } from '@/models/index.js';
|
import type { UsersRepository } from '@/models/index.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
|
@ -65,7 +65,7 @@ export class UrlPreviewService {
|
||||||
: `Getting preview of ${url}@${lang} ...`);
|
: `Getting preview of ${url}@${lang} ...`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const summary = meta.summalyProxy ? await this.httpRequestService.getJson(`${meta.summalyProxy}?${query({
|
const summary = meta.summalyProxy ? await this.httpRequestService.getJson<ReturnType<typeof summaly.default>>(`${meta.summalyProxy}?${query({
|
||||||
url: url,
|
url: url,
|
||||||
lang: lang ?? 'ja-JP',
|
lang: lang ?? 'ja-JP',
|
||||||
})}`) : await summaly.default(url, {
|
})}`) : await summaly.default(url, {
|
||||||
|
|
Loading…
Reference in New Issue