Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only overwrite servername in tls connect when host is not an IP address #354

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading