Skip to content

Commit

Permalink
[bug] Fix http proxy not working
Browse files Browse the repository at this point in the history
[bug] Fix dns lookup resolve not working
  • Loading branch information
01101sam committed Sep 16, 2023
1 parent 8011111 commit 358ec31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
33 changes: 25 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface BaseSocksProxyAgentOptions {
auth?: string | null;
username?: string | null;
password?: string | null;
// DNS options
lookup?: typeof dns.lookup;
}

interface BaseHttpsProxyAgentOptions { // extend BaseSocksProxyAgentOptions
Expand All @@ -36,6 +38,8 @@ interface BaseHttpsProxyAgentOptions { // extend BaseSocksProxyAgentOptions
auth?: string | null;
username?: string | null;
password?: string | null;
// DNS options
lookup?: typeof dns.lookup;
}

export interface HttpsProxyAgentOptions extends AgentOptions, BaseHttpsProxyAgentOptions, Partial<Omit<URL & net.NetConnectOpts & tls.ConnectionOptions, keyof BaseHttpsProxyAgentOptions>> {
Expand Down Expand Up @@ -74,6 +78,7 @@ class HttpsProxyAgent extends Agent {
private readonly tlsSecureContext: tls.SecureContext | undefined
public readonly proxy: HttpsProxyAgentOptions
private readonly secureEndpoint: boolean
private readonly lookup: typeof dns.lookup
public timeout: number | null


Expand All @@ -87,6 +92,7 @@ class HttpsProxyAgent extends Agent {
// Defaults to `false`.
this.secureEndpoint = Boolean(this.proxy.protocol?.startsWith('https'))
this.tlsSecureContext = proxyOptions.tls ? tls.createSecureContext(proxyOptions.tls) : undefined
this.lookup = proxyOptions.lookup ?? dns.lookup
this.timeout = proxyOptions.timeout ?? null
}

Expand Down Expand Up @@ -128,17 +134,28 @@ class HttpsProxyAgent extends Agent {
opts: RequestOptions
): Promise<net.Socket> {
const {proxy, secureEndpoint} = this
let {host, port, lookup: lookupCallback} = opts

if (!host) throw new Error('No `host` defined!')

const host = await new Promise<string>((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
// Client-side DNS resolution for http / https proxy.
opts.host = await new Promise<string>((resolve, reject) => {
const lookup = lookupCallback ?? this.lookup
try {
dns.lookup(proxy.hostname!, {}, (err, res) => err ? reject(err) : resolve(res))
lookup(host!, {}, (err, res) => err ? reject(err) : resolve(res))
} catch (e) {
reject(e)
}
});

proxy.host = host;
proxy.host = await new Promise<string>((resolve, reject) => {
const lookup = lookupCallback ?? this.lookup
try {
lookup(proxy.hostname!, {}, (err, res) => err ? reject(err) : resolve(res))
} catch (e) {
reject(e)
}
});

// Create a socket connection to the proxy server.
debug(`Creating \`${secureEndpoint ? 'tls' : 'net'}.Socket\`: %o`, proxy)
Expand Down Expand Up @@ -239,6 +256,7 @@ class HttpsProxyAgent extends Agent {
class SocksProxyAgent extends Agent {
private readonly tlsConnectionOptions: tls.ConnectionOptions | undefined
public readonly proxy: SocksProxy
private readonly lookup: typeof dns.lookup
public timeout: number | null

constructor(input: string | SocksProxyAgentOptions, options?: SocksProxyAgentOptions) {
Expand All @@ -248,6 +266,7 @@ class SocksProxyAgent extends Agent {

this.proxy = SocksProxyAgent._parseSocksProxy(proxyOptions)
this.tlsConnectionOptions = proxyOptions.tls != null ? proxyOptions.tls : {}
this.lookup = proxyOptions.lookup ?? dns.lookup
this.timeout = proxyOptions.timeout ?? null
}

Expand Down Expand Up @@ -317,11 +336,9 @@ class SocksProxyAgent extends Agent {

// Client-side DNS resolution for "4" and "5" socks proxy versions.
host = await new Promise<string>((resolve, reject) => {
// Use the request's custom lookup, if one was configured:
const lookupFn = lookupCallback ?? dns.lookup
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const lookup = lookupCallback ?? this.lookup
try {
lookupFn(host!, {}, (err, res) => err ? reject(err) : resolve(res))
lookup(host!, {}, (err, res) => err ? reject(err) : resolve(res))
} catch (e) {
reject(e)
}
Expand Down
10 changes: 6 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('HttpsProxyAgent', () => {
serverPort = parseInt(server.address().port)
done()
})
server.on('error', err => console.error(err) && process.exit(1))
})

before(done => {
Expand Down Expand Up @@ -155,6 +156,7 @@ describe('HttpsProxyAgent', () => {
res.setEncoding('utf8')
res.on('data', b => data += b)
res.once('end', () => {
assert.strictEqual(200, res.statusCode)
data = JSON.parse(data)
assert.strictEqual(`localhost:${serverPort}`, data.host)
done()
Expand All @@ -171,10 +173,10 @@ describe('HttpsProxyAgent', () => {
port: serverPort,
hostname: 'localhost',
path: '/',
agent: new HttpsProxyAgent({
hostname: 'localhost',
port: sslProxyPort,
protocol: 'https',
agent: new HttpsProxyAgent(`https://localhost:${sslProxyPort}`,{
// hostname: 'localhost',
// port: sslProxyPort,
// protocol: 'https',
tls: {
ca: fs.readFileSync(`${__dirname}/cacert.pem`)
},
Expand Down

0 comments on commit 358ec31

Please sign in to comment.