Skip to content

Commit f31fdc4

Browse files
committed
refactor: simplify socksDispatcher parameters
1 parent ee2b5e1 commit f31fdc4

File tree

3 files changed

+66
-82
lines changed

3 files changed

+66
-82
lines changed

README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@ Socks proxy for Node builtin (also [undici](https://github.com/nodejs/undici)) `
1010
npm install fetch-socks
1111
```
1212

13-
# Usage Example
13+
# Usage Examples
1414

1515
fetch `http://example.com` through `socks5://[::1]:1080`.
1616

1717
```javascript
1818
import { socksDispatcher } from "fetch-socks";
1919

2020
const dispatcher = socksDispatcher({
21-
proxy: {
22-
type: 5,
23-
host: "::1",
24-
port: 1080,
25-
},
21+
type: 5,
22+
host: "::1",
23+
port: 1080,
2624
});
2725

2826
const response = await fetch("https://example.com", { dispatcher });
2927
console.log(response.status);
3028
console.log(await response.text());
3129
```
3230

33-
fetch through proxy chain with two SOCKS proxies.
34-
35-
```javascript
36-
import { socksDispatcher } from "fetch-socks";
37-
38-
const dispatcher = socksDispatcher({
39-
proxy: [{
40-
type: 5,
41-
host: "::1",
42-
port: 1080,
43-
}, {
44-
type: 5,
45-
host: "::1",
46-
port: 1081,
47-
//userId: "foo",
48-
//password: "bar",
49-
}],
50-
// set some TLS options
31+
TypeScript example, fetch through proxy chain with two SOCKS proxies.
32+
33+
```typescript
34+
import { fetch } from "undici";
35+
import { socksDispatcher, SocksProxies } from "fetch-socks";
36+
37+
const proxyConfig: SocksProxies = [{
38+
type: 5,
39+
host: "::1",
40+
port: 1080,
41+
}, {
42+
type: 5,
43+
host: "127.0.0.1",
44+
port: 1081,
45+
//userId: "foo",
46+
//password: "bar",
47+
}];
48+
49+
const dispatcher = socksDispatcher(proxyConfig, {
5150
connect: {
51+
// set some TLS options
5252
rejectUnauthorized: false,
5353
},
5454
});

index.spec.ts

+34-48
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,11 @@ it("should throw error if proxy connect timeout", async () => {
6666
const addr = blackHole.address() as net.AddressInfo;
6767

6868
const dispatcher = socksDispatcher({
69-
proxy: {
70-
type: 5,
71-
host: addr.address,
72-
port: addr.port,
73-
},
74-
connect: {
75-
timeout: 500,
76-
},
69+
type: 5,
70+
host: addr.address,
71+
port: addr.port,
72+
}, {
73+
connect: { timeout: 500 },
7774
});
7875
const promise = fetch("https://example.com", { dispatcher });
7976
await expect(promise).rejects.toThrow(new TypeError("fetch failed"));
@@ -83,11 +80,9 @@ it("should throw error if proxy connect timeout", async () => {
8380

8481
it("should throw error if the socks server is unreachable", async () => {
8582
const dispatcher = socksDispatcher({
86-
proxy: {
87-
type: 5,
88-
host: "::1",
89-
port: 111,
90-
},
83+
type: 5,
84+
host: "::1",
85+
port: 111,
9186
});
9287
await httpServer.forGet("/foobar").thenReply(200, "__RESPONSE_DATA__");
9388

@@ -98,13 +93,11 @@ it("should throw error if the socks server is unreachable", async () => {
9893

9994
it("should throw error if the target is unreachable", async () => {
10095
const dispatcher = socksDispatcher({
101-
proxy: {
102-
type: 5,
103-
host: secureProxy.address,
104-
port: secureProxy.port,
105-
userId: "foo",
106-
password: "_INVALID_",
107-
},
96+
type: 5,
97+
host: secureProxy.address,
98+
port: secureProxy.port,
99+
userId: "foo",
100+
password: "_INVALID_",
108101
});
109102
await httpServer.forGet("/foobar").thenReply(200, "__RESPONSE_DATA__");
110103
const promise = fetch(httpServer.urlFor("/foobar"), { dispatcher });
@@ -113,23 +106,19 @@ it("should throw error if the target is unreachable", async () => {
113106

114107
it("should throw error if authenticate failed", async () => {
115108
const dispatcher = socksDispatcher({
116-
proxy: {
117-
type: 5,
118-
host: plainProxy.address,
119-
port: plainProxy.port,
120-
},
109+
type: 5,
110+
host: plainProxy.address,
111+
port: plainProxy.port,
121112
});
122113
const promise = fetch("http://[::1]:111", { dispatcher });
123114
await expect(promise).rejects.toThrow(new TypeError("fetch failed"));
124115
});
125116

126117
it("should connect target through socks", async () => {
127118
const dispatcher = socksDispatcher({
128-
proxy: {
129-
type: 5,
130-
host: plainProxy.address,
131-
port: plainProxy.port,
132-
},
119+
type: 5,
120+
host: plainProxy.address,
121+
port: plainProxy.port,
133122
});
134123
const ep = await httpServer.forGet("/foobar")
135124
.thenReply(200, "__RESPONSE_DATA__");
@@ -143,19 +132,17 @@ it("should connect target through socks", async () => {
143132
});
144133

145134
it("should support proxy chain", async () => {
146-
const dispatcher = socksDispatcher({
147-
proxy: [{
148-
type: 5,
149-
host: secureProxy.address,
150-
port: secureProxy.port,
151-
userId: "foo",
152-
password: "bar",
153-
}, {
154-
type: 5,
155-
host: plainProxy.address,
156-
port: plainProxy.port,
157-
}],
158-
});
135+
const dispatcher = socksDispatcher([{
136+
type: 5,
137+
host: secureProxy.address,
138+
port: secureProxy.port,
139+
userId: "foo",
140+
password: "bar",
141+
}, {
142+
type: 5,
143+
host: plainProxy.address,
144+
port: plainProxy.port,
145+
}]);
159146
const ep = await httpServer.forGet("/foobar")
160147
.thenReply(200, "__RESPONSE_DATA__");
161148

@@ -171,11 +158,10 @@ it("should support proxy chain", async () => {
171158

172159
it("should support TLS over socks", async () => {
173160
const dispatcher = socksDispatcher({
174-
proxy: {
175-
type: 5,
176-
host: plainProxy.address,
177-
port: plainProxy.port,
178-
},
161+
type: 5,
162+
host: plainProxy.address,
163+
port: plainProxy.port,
164+
}, {
179165
connect: {
180166
rejectUnauthorized: false,
181167
},

index.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import TLSOptions = buildConnector.BuildOptions;
55

66
type onEstablished = Parameters<typeof SocksClient.createConnection>[1];
77

8-
type Proxies = SocksProxy | SocksProxy[];
8+
export type SocksProxies = SocksProxy | SocksProxy[];
99

1010
/**
1111
* Since socks does not guess HTTP ports, we need to do that.
@@ -23,7 +23,7 @@ function resolvePort(protocol: string, port: string) {
2323
* @param proxies The proxy server to use or the list of proxy servers to chain.
2424
* @param tlsOpts TLS upgrade options.
2525
*/
26-
export function socksConnector(proxies: Proxies, tlsOpts: TLSOptions = {}): Connector {
26+
export function socksConnector(proxies: SocksProxies, tlsOpts: TLSOptions = {}): Connector {
2727
const { timeout = 10e3 } = tlsOpts;
2828
const tlsUpgrade = buildConnector(tlsOpts);
2929

@@ -63,11 +63,6 @@ export function socksConnector(proxies: Proxies, tlsOpts: TLSOptions = {}): Conn
6363

6464
export interface SocksDispatcherOptions extends Agent.Options {
6565

66-
/**
67-
* The socks proxy server to use or the list of proxy servers to chain.
68-
*/
69-
proxy: Proxies;
70-
7166
/**
7267
* TLS upgrade options, see:
7368
* https://undici.nodejs.org/#/docs/api/Client?id=parameter-connectoptions
@@ -80,8 +75,11 @@ export interface SocksDispatcherOptions extends Agent.Options {
8075

8176
/**
8277
* Create a undici Agent with socks connector.
78+
*
79+
* @param proxies The proxy server to use or the list of proxy servers to chain.
80+
* @param options Additional options passed to the Agent constructor.
8381
*/
84-
export function socksDispatcher(options: SocksDispatcherOptions) {
85-
const { connect, proxy, ...rest } = options;
86-
return new Agent({ ...rest, connect: socksConnector(proxy, connect) });
82+
export function socksDispatcher(proxies: SocksProxies, options: SocksDispatcherOptions = {}) {
83+
const { connect, ...rest } = options;
84+
return new Agent({ ...rest, connect: socksConnector(proxies, connect) });
8785
}

0 commit comments

Comments
 (0)