Skip to content

Commit

Permalink
fix: Downloading does not follow redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 20, 2024
1 parent a0a2585 commit b8f754f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 46 deletions.
12 changes: 0 additions & 12 deletions xmcl-runtime/authlibInjector/AuthlibInjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down
32 changes: 6 additions & 26 deletions xmcl-runtime/network/dispatchers/NetworkAgent.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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,
)
}
}
Expand Down
1 change: 0 additions & 1 deletion xmcl-runtime/network/networkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const kNetworkInterface: InjectionKey<NetworkInterface> = Symbol('Network
export const kDownloadOptions : InjectionKey<DownloadBaseOptions> = Symbol('DownloadOptions')

export interface NetworkInterface {
registerOptionsInterceptor(interceptor: (opts: Dispatcher.DispatchOptions) => void): void
getDownloadAgentStatus(): Record<string, PoolStats>
destroyPool(origin: string): Promise<void>
}
8 changes: 1 addition & 7 deletions xmcl-runtime/network/pluginNetworkInterface.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -217,9 +214,6 @@ export const pluginNetworkInterface: LauncherAppPlugin = (app) => {
})

app.registry.register(kNetworkInterface, {
registerOptionsInterceptor(interceptor: (opts: DispatchOptions) => void | Promise<void>): void {
dispatchInterceptors.unshift(interceptor)
},
getDownloadAgentStatus: getAgentStatus,
async destroyPool(origin) {
// @ts-ignore
Expand Down

0 comments on commit b8f754f

Please sign in to comment.