diff --git a/xmcl-runtime/authlibInjector/AuthlibInjectorService.ts b/xmcl-runtime/authlibInjector/AuthlibInjectorService.ts index 4fb9d2e69..db9bc04f0 100644 --- a/xmcl-runtime/authlibInjector/AuthlibInjectorService.ts +++ b/xmcl-runtime/authlibInjector/AuthlibInjectorService.ts @@ -26,20 +26,8 @@ export class AuthlibInjectorService extends AbstractService implements IAuthlibI @Inject(kGameDataPath) private getPath: PathResolver, @Inject(kTaskExecutor) private submit: TaskFn, @Inject(kGFW) gfw: GFW, - @Inject(kNetworkInterface) networkInterface: NetworkInterface, ) { super(app) - - networkInterface.registerOptionsInterceptor((options) => { - const origin = options.origin instanceof URL ? options.origin : new URL(options.origin! as any) - if (origin.hostname === 'authlib-injector.yushi.moe') { - if (shouldOverrideApiSet(settings, gfw.inside)) { - const api = settings.apiSets.find(a => a.name === settings.apiSetsPreference) || settings.apiSets[0] - options.origin = new URL(api.url).origin - options.path = `/mirrors/authlib-injector${options.path}` - } - } - }) } async abortAuthlibInjectorInstall(): Promise { diff --git a/xmcl-runtime/network/dispatchers/NetworkAgent.ts b/xmcl-runtime/network/dispatchers/NetworkAgent.ts index 30d69aff4..14a839b3f 100644 --- a/xmcl-runtime/network/dispatchers/NetworkAgent.ts +++ b/xmcl-runtime/network/dispatchers/NetworkAgent.ts @@ -1,5 +1,5 @@ import { Socket } from 'net' -import { Agent, Client, Dispatcher, RetryHandler, buildConnector, errors, util } from 'undici' +import { Agent, Client, Dispatcher, RetryHandler, buildConnector, errors, interceptors, util } from 'undici' type DispatchHandlers = Dispatcher.DispatchHandler const { InvalidArgumentError, RequestAbortedError } = errors @@ -49,9 +49,9 @@ export class NetworkAgent extends Dispatcher { private requestTls?: buildConnector.BuildOptions private proxyTls?: buildConnector.BuildOptions + #dispatcher: Dispatcher #userAgent: string #retryOptions: RetryHandler.RetryOptions - #dispatchInterceptors?: Array<(opts: Dispatcher.DispatchOptions) => void> async setProxy(uri: URL, auth?: string) { const oldClient = this.proxyClient @@ -86,7 +86,6 @@ export class NetworkAgent extends Dispatcher { constructor(opts: { userAgent: string retryOptions: RetryHandler.RetryOptions - dispatchInterceptors?: Array<(opts: Dispatcher.DispatchOptions) => void> factory: (connect: buildConnector.connector) => Agent requestTls?: buildConnector.BuildOptions proxyTls?: buildConnector.BuildOptions @@ -95,7 +94,6 @@ export class NetworkAgent extends Dispatcher { this.#retryOptions = opts.retryOptions this.#userAgent = opts.userAgent - this.#dispatchInterceptors = opts.dispatchInterceptors this.requestTls = opts.requestTls this.proxyTls = opts.proxyTls @@ -140,43 +138,25 @@ export class NetworkAgent extends Dispatcher { } this.agent = opts.factory(connect as any) + this.#dispatcher = this.agent.compose(interceptors.redirect(), interceptors.retry(this.#retryOptions)) } dispatch(opts: Agent.DispatchOptions, handler: DispatchHandlers) { - const { host } = new URL(opts.origin as string) + // const { host } = new URL(opts.origin as string) const headers = opts.headers ? opts.headers instanceof Array ? util.parseHeaders(opts.headers as any) : opts.headers as any : {} if (!headers['user-agent']) { headers['user-agent'] = this.#userAgent } - if (this.#dispatchInterceptors) { - for (const interceptor of this.#dispatchInterceptors) { - interceptor(opts) - } - } - throwIfProxyAuthIsSent(headers) - const retry = new RetryHandler({ - ...opts, - method: opts.method, - headers: { - ...headers, - host, - }, - retryOptions: this.#retryOptions, - }, { - dispatch: this.agent.dispatch.bind(this.agent), - handler, - }) - - return this.agent.dispatch( + return this.#dispatcher.dispatch( { ...opts, method: opts.method, headers, }, - retry, + handler, ) } } diff --git a/xmcl-runtime/network/networkInterface.ts b/xmcl-runtime/network/networkInterface.ts index 70f9ae05e..86dcbc298 100644 --- a/xmcl-runtime/network/networkInterface.ts +++ b/xmcl-runtime/network/networkInterface.ts @@ -7,7 +7,6 @@ export const kNetworkInterface: InjectionKey = Symbol('Network export const kDownloadOptions : InjectionKey = Symbol('DownloadOptions') export interface NetworkInterface { - registerOptionsInterceptor(interceptor: (opts: Dispatcher.DispatchOptions) => void): void getDownloadAgentStatus(): Record destroyPool(origin: string): Promise } diff --git a/xmcl-runtime/network/pluginNetworkInterface.ts b/xmcl-runtime/network/pluginNetworkInterface.ts index eda366817..7ce9107e7 100644 --- a/xmcl-runtime/network/pluginNetworkInterface.ts +++ b/xmcl-runtime/network/pluginNetworkInterface.ts @@ -1,7 +1,7 @@ import { DefaultRangePolicy } from '@xmcl/file-transfer' import { PoolStats } from '@xmcl/runtime-api' import { setTimeout as timeout } from 'timers/promises' -import { Agent, Dispatcher, Pool, buildConnector } from 'undici' +import { Agent, Dispatcher, Pool, buildConnector, interceptors } from 'undici' import { kClients, kRunning } from 'undici/lib/core/symbols' import { LauncherAppPlugin } from '~/app' import { kSettings } from '~/settings' @@ -14,8 +14,6 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => { const logger = app.getLogger('NetworkInterface') const userAgent = app.userAgent - const dispatchInterceptors: Array<(opts: DispatchOptions) => void> = [] - let maxConnection = 64 const connectorOptions: buildConnector.BuildOptions = { timeout: 25_000, @@ -71,7 +69,6 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => { retryOptions: { maxTimeout: 60_000, maxRetries: 30, - // @ts-ignore retry: (err, { state, opts }, cb) => { const { statusCode, code, headers } = err as any const { method, retryOptions } = opts @@ -217,9 +214,6 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => { }) app.registry.register(kNetworkInterface, { - registerOptionsInterceptor(interceptor: (opts: DispatchOptions) => void | Promise): void { - dispatchInterceptors.unshift(interceptor) - }, getDownloadAgentStatus: getAgentStatus, async destroyPool(origin) { // @ts-ignore