From f14150c69df2366abcb596d4a0f431670d937eb6 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 4 Jan 2023 10:56:45 +0000 Subject: [PATCH] Use custom fetcher for ApRequest / ApResolver --- packages/backend/src/core/DownloadService.ts | 2 +- packages/backend/src/core/HttpRequestService.ts | 14 ++++++++------ .../src/core/activitypub/ApRequestService.ts | 7 +++++-- .../src/core/activitypub/ApResolverService.ts | 6 ++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/core/DownloadService.ts b/packages/backend/src/core/DownloadService.ts index 09b4d94aa9..86aff3f387 100644 --- a/packages/backend/src/core/DownloadService.ts +++ b/packages/backend/src/core/DownloadService.ts @@ -32,7 +32,7 @@ export class DownloadService { ) { this.logger = this.loggerService.getLogger('download'); - this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherConstructorOption( + this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption( { connect: process.env.NODE_ENV === 'development' ? this.httpRequestService.clientDefaults.connect diff --git a/packages/backend/src/core/HttpRequestService.ts b/packages/backend/src/core/HttpRequestService.ts index 91e06ab65a..6787dab907 100644 --- a/packages/backend/src/core/HttpRequestService.ts +++ b/packages/backend/src/core/HttpRequestService.ts @@ -58,6 +58,7 @@ export class UndiciFetcher { ...args.agentOptions, connect: (process.env.NODE_ENV !== 'production' && typeof args.agentOptions.connect !== 'function') ? (options, cb) => { + // Custom connector for debug undici.buildConnector(args.agentOptions.connect as undici.buildConnector.BuildOptions)(options, (err, socket) => { this.logger?.debug('Socket connector called', socket); if (err) { @@ -65,7 +66,7 @@ export class UndiciFetcher { cb(new Error(`Error while socket connecting\n${err}`), null); return; } - this.logger?.debug(`Socket connected: ${socket.localPort} => ${socket.remoteAddress}`); + this.logger?.debug(`Socket connected: port ${socket.localPort} => remote ${socket.remoteAddress}`); cb(null, socket); }); } : args.agentOptions.connect, @@ -80,14 +81,15 @@ export class UndiciFetcher { connect: (process.env.NODE_ENV !== 'production' && typeof (args.proxy?.options?.connect ?? args.agentOptions.connect) !== 'function') ? (options, cb) => { + // Custom connector for debug undici.buildConnector((args.proxy?.options?.connect ?? args.agentOptions.connect) as undici.buildConnector.BuildOptions)(options, (err, socket) => { - this.logger?.debug('Socket connector called', socket); + this.logger?.debug('Socket connector called (secure)', socket); if (err) { this.logger?.debug(`Socket error`, err); cb(new Error(`Error while socket connecting\n${err}`), null); return; } - this.logger?.debug(`Socket connected: ${socket.localPort} => ${socket.remoteAddress}`); + this.logger?.debug(`Socket connected (secure): port ${socket.localPort} => remote ${socket.remoteAddress}`); cb(null, socket); }); } : (args.proxy?.options?.connect ?? args.agentOptions.connect), @@ -218,12 +220,12 @@ export class HttpRequestService { this.maxSockets = Math.max(256, this.config.deliverJobConcurrency ?? 128); - this.defaultFetcher = new UndiciFetcher(this.getStandardUndiciFetcherConstructorOption(), this.logger); + this.defaultFetcher = new UndiciFetcher(this.getStandardUndiciFetcherOption(), this.logger); this.fetch = this.defaultFetcher.fetch; this.getHtml = this.defaultFetcher.getHtml; - this.defaultJsonFetcher = new UndiciFetcher(this.getStandardUndiciFetcherConstructorOption({ + this.defaultJsonFetcher = new UndiciFetcher(this.getStandardUndiciFetcherOption({ maxResponseSize: 1024 * 256, }), this.logger); @@ -272,7 +274,7 @@ export class HttpRequestService { * @param bypassProxy Allways bypass proxy */ @bindThis - public getStandardUndiciFetcherConstructorOption(opts: undici.Agent.Options = {}, proxyOpts: undici.Agent.Options = {}) { + public getStandardUndiciFetcherOption(opts: undici.Agent.Options = {}, proxyOpts: undici.Agent.Options = {}) { return { agentOptions: { ...this.clientDefaults, diff --git a/packages/backend/src/core/activitypub/ApRequestService.ts b/packages/backend/src/core/activitypub/ApRequestService.ts index 2626beaade..fe487e8fd6 100644 --- a/packages/backend/src/core/activitypub/ApRequestService.ts +++ b/packages/backend/src/core/activitypub/ApRequestService.ts @@ -5,7 +5,7 @@ import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; import type { User } from '@/models/entities/User.js'; import { UserKeypairStoreService } from '@/core/UserKeypairStoreService.js'; -import { HttpRequestService } from '@/core/HttpRequestService.js'; +import { HttpRequestService, UndiciFetcher } from '@/core/HttpRequestService.js'; import { bindThis } from '@/decorators.js'; type Request = { @@ -28,6 +28,8 @@ type PrivateKey = { @Injectable() export class ApRequestService { + private undiciFetcher: UndiciFetcher; + constructor( @Inject(DI.config) private config: Config, @@ -35,6 +37,7 @@ export class ApRequestService { private userKeypairStoreService: UserKeypairStoreService, private httpRequestService: HttpRequestService, ) { + this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption()); } @bindThis @@ -152,7 +155,7 @@ export class ApRequestService { }, }); - await this.httpRequestService.fetch( + await this.undiciFetcher.fetch( url, { method: req.request.method, diff --git a/packages/backend/src/core/activitypub/ApResolverService.ts b/packages/backend/src/core/activitypub/ApResolverService.ts index 21bff88b2e..94c053473e 100644 --- a/packages/backend/src/core/activitypub/ApResolverService.ts +++ b/packages/backend/src/core/activitypub/ApResolverService.ts @@ -4,7 +4,7 @@ import { InstanceActorService } from '@/core/InstanceActorService.js'; import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository } from '@/models/index.js'; import type { Config } from '@/config.js'; import { MetaService } from '@/core/MetaService.js'; -import { HttpRequestService } from '@/core/HttpRequestService.js'; +import { HttpRequestService, UndiciFetcher } from '@/core/HttpRequestService.js'; import { DI } from '@/di-symbols.js'; import { UtilityService } from '@/core/UtilityService.js'; import { bindThis } from '@/decorators.js'; @@ -17,6 +17,7 @@ import type { IObject, ICollection, IOrderedCollection } from './type.js'; export class Resolver { private history: Set; private user?: ILocalUser; + private undiciFetcher: UndiciFetcher; constructor( private config: Config, @@ -34,6 +35,7 @@ export class Resolver { private recursionLimit = 100, ) { this.history = new Set(); + this.undiciFetcher = new UndiciFetcher(this.httpRequestService.getStandardUndiciFetcherOption()); } @bindThis @@ -97,7 +99,7 @@ export class Resolver { const object = (this.user ? await this.apRequestService.signedGet(value, this.user) as IObject - : await this.httpRequestService.getJson(value, 'application/activity+json, application/ld+json')); + : await this.undiciFetcher.getJson(value, 'application/activity+json, application/ld+json')); if (object == null || ( Array.isArray(object['@context']) ?