Skip to content

Commit

Permalink
fix(proxy-agent): use https proxy agent for http proxy, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 3, 2024
1 parent 3ca4816 commit 0087bf1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ export class HTTP extends Service<HTTP.Config> {
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
Expand Down Expand Up @@ -330,7 +330,7 @@ export class HTTP extends Service<HTTP.Config> {
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', () => {
Expand Down
32 changes: 19 additions & 13 deletions packages/proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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<Config> = 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
})
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 0087bf1

Please sign in to comment.