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

[socks-proxy-agent] pass socket_options to SocksClient #302

Merged
merged 6 commits into from Mar 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-pumas-drum.md
@@ -0,0 +1,5 @@
---
"socks-proxy-agent": patch
---

Pass `socket_options` to `SocksClient`
11 changes: 10 additions & 1 deletion packages/socks-proxy-agent/src/index.ts
Expand Up @@ -70,11 +70,15 @@ function parseSocksURL(url: URL): { lookup: boolean; proxy: SocksProxy } {
return { lookup, proxy };
}

type SocksSocketOptions = Omit<net.TcpNetConnectOpts, 'port' | 'host'>;

export type SocksProxyAgentOptions = Omit<
SocksProxy,
// These come from the parsed URL
'ipaddress' | 'host' | 'port' | 'type' | 'userId' | 'password'
> &
> & {
socketOptions?: SocksSocketOptions;
} &
http.AgentOptions;

export class SocksProxyAgent extends Agent {
Expand All @@ -89,6 +93,7 @@ export class SocksProxyAgent extends Agent {
readonly shouldLookup: boolean;
readonly proxy: SocksProxy;
timeout: number | null;
socketOptions: SocksSocketOptions | null;

constructor(uri: string | URL, opts?: SocksProxyAgentOptions) {
super(opts);
Expand All @@ -99,6 +104,7 @@ export class SocksProxyAgent extends Agent {
this.shouldLookup = lookup;
this.proxy = proxy;
this.timeout = opts?.timeout ?? null;
this.socketOptions = opts?.socketOptions ?? null;
}

/**
Expand Down Expand Up @@ -140,6 +146,9 @@ export class SocksProxyAgent extends Agent {
},
command: 'connect',
timeout: timeout ?? undefined,
// @ts-expect-error the type supplied by socks for socket_options is wider
// than necessary since socks will always override the host and port
socket_options: this.socketOptions ?? undefined,
};

const cleanup = (tlsSocket?: tls.TLSSocket) => {
Expand Down
47 changes: 42 additions & 5 deletions packages/socks-proxy-agent/test/test.ts
Expand Up @@ -20,19 +20,26 @@ describe('SocksProxyAgent', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let socksServer: any;
let socksServerUrl: URL;
let socksServerHost: string | null = null;

beforeAll(async () => {
beforeEach(async () => {
// setup SOCKS proxy server
// @ts-expect-error no types for `socksv5`
socksServer = socks.createServer(function (_info, accept) {
accept();
});
await listen(socksServer);
const port = socksServer.address().port;
socksServerUrl = new URL(`socks://127.0.0.1:${port}`);
await listen(socksServer, 0, socksServerHost ?? '127.0.0.1');
const { port, family, address } = socksServer.address();
socksServerUrl = new URL(
`socks://${family === 'IPv6' ? 'localhost' : address}:${port}`
);
socksServer.useAuth(socks.auth.None());
});

afterEach(() => {
socksServer.close();
});

beforeAll(async () => {
// setup target HTTP server
httpServer = http.createServer();
Expand All @@ -54,7 +61,6 @@ describe('SocksProxyAgent', () => {
});

afterAll(() => {
socksServer.close();
httpServer.close();
httpsServer.close();
});
Expand Down Expand Up @@ -89,6 +95,37 @@ describe('SocksProxyAgent', () => {
});
});

describe('ipv6 host', () => {
beforeAll(() => {
socksServerHost = '::1';
});
afterAll(() => {
socksServerHost = null;
});

it('should connect over ipv6 socket', async () => {
httpServer.once('request', (req, res) => res.end());

const res = await req(new URL('/foo', httpServerUrl), {
agent: new SocksProxyAgent(socksServerUrl, { socketOptions: { family: 6 } }),
});
assert(res);
});

it('should refuse connection over ipv4 socket', async () => {
let err: Error | undefined;
try {
await req(new URL('/foo', httpServerUrl), {
agent: new SocksProxyAgent(socksServerUrl, { socketOptions: { family: 4 } }),
});
} catch (_err) {
err = _err as Error;
}
assert(err);
assert.equal(err.message, `connect ECONNREFUSED 127.0.0.1:${socksServerUrl.port}`);
});
});

describe('"http" module', () => {
it('should work against an HTTP endpoint', async () => {
httpServer.once('request', function (req, res) {
Expand Down