Skip to content

Commit

Permalink
Only overwrite servername in tls connect when host is not an IP addre…
Browse files Browse the repository at this point in the history
…ss (#354)
  • Loading branch information
lukekarrys authored Dec 6, 2024
1 parent 1699a09 commit 913a49a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/lovely-boxes-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"https-proxy-agent": patch
"pac-proxy-agent": patch
"socks-proxy-agent": patch
---

Only overwrite servername in tls connect when host is not an IP address
34 changes: 25 additions & 9 deletions packages/https-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ import type { OutgoingHttpHeaders } from 'http';

const debug = createDebug('https-proxy-agent');

const setServernameFromNonIpHost = <
T extends { host?: string; servername?: string }
>(
options: T
) => {
if (
options.servername === undefined &&
options.host &&
!net.isIP(options.host)
) {
return {
...options,
servername: options.host,
};
}
return options;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;

Expand Down Expand Up @@ -92,12 +110,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
let socket: net.Socket;
if (proxy.protocol === 'https:') {
debug('Creating `tls.Socket`: %o', this.connectOpts);
const servername =
this.connectOpts.servername || this.connectOpts.host;
socket = tls.connect({
...this.connectOpts,
servername,
});
socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));
} else {
debug('Creating `net.Socket`: %o', this.connectOpts);
socket = net.connect(this.connectOpts);
Expand Down Expand Up @@ -146,11 +159,14 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
debug('Upgrading socket connection to TLS');
const servername = opts.servername || opts.host;
return tls.connect({
...omit(opts, 'host', 'path', 'port'),
...omit(
setServernameFromNonIpHost(opts),
'host',
'path',
'port'
),
socket,
servername,
});
}

Expand Down
23 changes: 18 additions & 5 deletions packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ import { getQuickJS } from '@tootallnate/quickjs-emscripten';

const debug = createDebug('pac-proxy-agent');

const setServernameFromNonIpHost = <
T extends { host?: string; servername?: string }
>(
options: T
) => {
if (
options.servername === undefined &&
options.host &&
!net.isIP(options.host)
) {
return {
...options,
servername: options.host,
};
}
return options;
};
type Protocols = keyof typeof gProtocols;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -238,11 +255,7 @@ export class PacProxyAgent<Uri extends string> extends Agent {
if (type === 'DIRECT') {
// Direct connection to the destination endpoint
if (secureEndpoint) {
const servername = opts.servername || opts.host;
socket = tls.connect({
...opts,
servername,
});
socket = tls.connect(setServernameFromNonIpHost(opts));
} else {
socket = net.connect(opts);
}
Expand Down
30 changes: 25 additions & 5 deletions packages/socks-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ import { URL } from 'url';

const debug = createDebug('socks-proxy-agent');

const setServernameFromNonIpHost = <
T extends { host?: string; servername?: string }
>(
options: T
) => {
if (
options.servername === undefined &&
options.host &&
!net.isIP(options.host)
) {
return {
...options,
servername: options.host,
};
}
return options;
};

function parseSocksURL(url: URL): { lookup: boolean; proxy: SocksProxy } {
let lookup = false;
let type: SocksProxy['type'] = 5;
Expand Down Expand Up @@ -79,8 +97,7 @@ export type SocksProxyAgentOptions = Omit<
'ipaddress' | 'host' | 'port' | 'type' | 'userId' | 'password'
> & {
socketOptions?: SocksSocketOptions;
} &
http.AgentOptions;
} & http.AgentOptions;

export class SocksProxyAgent extends Agent {
static protocols = [
Expand Down Expand Up @@ -171,11 +188,14 @@ export class SocksProxyAgent extends Agent {
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
debug('Upgrading socket connection to TLS');
const servername = opts.servername || opts.host;
const tlsSocket = tls.connect({
...omit(opts, 'host', 'path', 'port'),
...omit(
setServernameFromNonIpHost(opts),
'host',
'path',
'port'
),
socket,
servername,
});

tlsSocket.once('error', (error) => {
Expand Down

0 comments on commit 913a49a

Please sign in to comment.