From 0087bf10ffac78676f67fcdd7490ff361d40a76a Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 3 Mar 2024 23:09:37 +0800 Subject: [PATCH] fix(proxy-agent): use https proxy agent for http proxy, fix #3 --- packages/core/src/index.ts | 8 ++++---- packages/proxy-agent/src/index.ts | 32 ++++++++++++++++++------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index af65866..11a5eff 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -16,8 +16,8 @@ declare module 'cordis' { interface Events { 'http/config'(config: HTTP.Config): void - 'http/fetch-init'(init: RequestInit, config: HTTP.Config): void - 'http/websocket-init'(init: ClientOptions, config: HTTP.Config): void + 'http/fetch-init'(url: URL, init: RequestInit, config: HTTP.Config): void + 'http/websocket-init'(url: URL, init: ClientOptions, config: HTTP.Config): void } } @@ -261,7 +261,7 @@ export class HTTP extends Service { headers.append('Content-Type', type) } } - caller.emit('http/fetch-init', init, config) + caller.emit('http/fetch-init', url, init, config) const raw = await fetch(url, init).catch((cause) => { const error = new HTTP.Error(`fetch ${url} failed`) error.cause = cause @@ -330,7 +330,7 @@ export class HTTP extends Service { handshakeTimeout: config?.timeout, headers: config?.headers, } - caller.emit('http/websocket-init', options, config) + caller.emit('http/websocket-init', url, options, config) } const socket = new WebSocket(url, options as never) const dispose = caller.on('dispose', () => { diff --git a/packages/proxy-agent/src/index.ts b/packages/proxy-agent/src/index.ts index 44c6ef5..2b72417 100644 --- a/packages/proxy-agent/src/index.ts +++ b/packages/proxy-agent/src/index.ts @@ -13,8 +13,8 @@ import { SocksProxyAgent } from 'socks-proxy-agent' declare module 'cordis' { interface Events { - 'http/dispatcher'(url: URL): Dispatcher | undefined - 'http/legacy-agent'(url: URL): http.Agent | undefined + 'http/dispatcher'(proxyURL: URL, requestURL: URL): Dispatcher | undefined + 'http/legacy-agent'(proxyURL: URL, requestURL: URL): http.Agent | undefined } } @@ -74,16 +74,17 @@ function socksAgent(result: ParseResult, options: SocksDispatcherOptions = {}) { } export const name = 'undios-proxy-agent' +export const inject = ['http'] export interface Config {} export const Config: z = z.object({}) export function apply(ctx: Context, config: Config) { - ctx.on('http/fetch-init', (init, config) => { + ctx.on('http/fetch-init', (url, init, config) => { if (!config?.proxyAgent) return - const url = new URL(config.proxyAgent) - const agent = ctx.bail('http/dispatcher', url) + const proxy = new URL(config.proxyAgent) + const agent = ctx.bail('http/dispatcher', proxy, url) if (!agent) throw new Error(`Cannot resolve proxy agent ${url}`) init['dispatcher'] = agent }) @@ -97,20 +98,25 @@ export function apply(ctx: Context, config: Config) { return socksAgent(result) }) - ctx.on('http/websocket-init', (init, config) => { + ctx.on('http/websocket-init', (url, init, config) => { if (!config?.proxyAgent) return - const url = new URL(config.proxyAgent) - const agent = ctx.bail('http/legacy-agent', url) + const proxy = new URL(config.proxyAgent) + const agent = ctx.bail('http/legacy-agent', proxy, url) if (!agent) throw new Error(`Cannot resolve proxy agent ${url}`) init.agent = agent }) - ctx.on('http/legacy-agent', (url) => { - if (url.protocol === 'http:') return new HttpProxyAgent(url) - if (url.protocol === 'https:') return new HttpsProxyAgent(url) - const result = parseSocksURL(url) + ctx.on('http/legacy-agent', (proxy, url) => { + if (['http:', 'https:'].includes(proxy.protocol)) { + if (['http:', 'ws:'].includes(url.protocol)) { + return new HttpProxyAgent(proxy) + } else { + return new HttpsProxyAgent(proxy) + } + } + const result = parseSocksURL(proxy) if (!result) return - return new SocksProxyAgent(url) + return new SocksProxyAgent(proxy) }) }