-
-
Notifications
You must be signed in to change notification settings - Fork 757
feat: add SOCKS5 proxy support to ProxyAgent #4385
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
58e1ad9
feat: add SOCKS5 implementation plan
mcollina 7d0e4d4
feat: implement SOCKS5 proxy support (Phase 1)
mcollina 43ae9be
fix: update SOCKS5 implementation plan to require Pool instead of Client
mcollina c779f19
feat: complete SOCKS5 ProxyAgent integration (Phase 2)
mcollina b8c7d72
fix: refactor SOCKS5 proxy to use Pool instead of Client
mcollina 0a2da5e
feat: add TypeScript definitions and comprehensive tests for SOCKS5 p…
mcollina abdbb19
docs: add comprehensive documentation for SOCKS5 proxy support
mcollina 2634ca1
style: apply linting fixes for SOCKS5 implementation
mcollina edbaa59
feat: add IPv6 support to SOCKS5 client
mcollina 6eedb92
fix: move docker-compose to fixtures and fix ProxyAgent close for SOCKS5
mcollina ce3478e
chore: remove PLAN.md
mcollina 0658e46
refactor: rename Socks5ProxyWrapper to Socks5Agent and add experiment…
mcollina f5353b3
fix: remove redundant method declarations from Socks5Agent types
mcollina c7c82eb
refactor: move TestSocks5Server to fixtures
mcollina 489c414
refactor: address PR review feedback for SOCKS5 support
mcollina 1b8b506
fix: lazy load TLS module in SOCKS5 agent for --without-ssl builds
mcollina 9426fae
fix: add defensive guard in ProxyAgent connect for SOCKS5 proxies
mcollina 84470b3
refactor: address second round of PR review feedback
mcollina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| # Class: Socks5ProxyAgent | ||
|
|
||
| Extends: `undici.Dispatcher` | ||
|
|
||
| A SOCKS5 proxy wrapper class that implements the Dispatcher API. It enables HTTP requests to be routed through a SOCKS5 proxy server, providing connection tunneling and authentication support. | ||
|
|
||
| ## `new Socks5ProxyAgent(proxyUrl[, options])` | ||
|
|
||
| Arguments: | ||
|
|
||
| * **proxyUrl** `string | URL` (required) - The SOCKS5 proxy server URL. Must use `socks5://` or `socks://` protocol. | ||
| * **options** `Socks5ProxyAgent.Options` (optional) - Additional configuration options. | ||
|
|
||
| Returns: `Socks5ProxyAgent` | ||
|
|
||
| ### Parameter: `Socks5ProxyAgent.Options` | ||
|
|
||
| Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions) | ||
|
|
||
| * **headers** `IncomingHttpHeaders` (optional) - Additional headers to send with proxy connections. | ||
| * **username** `string` (optional) - SOCKS5 proxy username for authentication. Can also be provided in the proxy URL. | ||
| * **password** `string` (optional) - SOCKS5 proxy password for authentication. Can also be provided in the proxy URL. | ||
| * **connect** `Function` (optional) - Custom connector function for the proxy connection. | ||
| * **proxyTls** `BuildOptions` (optional) - TLS options for the proxy connection (when using SOCKS5 over TLS). | ||
|
|
||
| Examples: | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
| // or with authentication | ||
| const socks5ProxyWithAuth = new Socks5ProxyAgent('socks5://user:pass@localhost:1080') | ||
| // or with options | ||
| const socks5ProxyWithOptions = new Socks5ProxyAgent('socks5://localhost:1080', { | ||
| username: 'user', | ||
| password: 'pass', | ||
| connections: 10 | ||
| }) | ||
| ``` | ||
|
|
||
| #### Example - Basic SOCKS5 Proxy instantiation | ||
|
|
||
| This will instantiate the Socks5ProxyAgent. It will not do anything until registered as the dispatcher to use with requests. | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
| ``` | ||
|
|
||
| #### Example - Basic SOCKS5 Proxy Request with global dispatcher | ||
|
|
||
| ```js | ||
| import { setGlobalDispatcher, request, Socks5ProxyAgent } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
| setGlobalDispatcher(socks5Proxy) | ||
|
|
||
| const { statusCode, body } = await request('http://localhost:3000/foo') | ||
|
|
||
| console.log('response received', statusCode) // response received 200 | ||
|
|
||
| for await (const data of body) { | ||
| console.log('data', data.toString('utf8')) // data foo | ||
| } | ||
| ``` | ||
|
|
||
| #### Example - Basic SOCKS5 Proxy Request with local dispatcher | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent, request } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
|
|
||
| const { | ||
| statusCode, | ||
| body | ||
| } = await request('http://localhost:3000/foo', { dispatcher: socks5Proxy }) | ||
|
|
||
| console.log('response received', statusCode) // response received 200 | ||
|
|
||
| for await (const data of body) { | ||
| console.log('data', data.toString('utf8')) // data foo | ||
| } | ||
| ``` | ||
|
|
||
| #### Example - SOCKS5 Proxy Request with authentication | ||
|
|
||
| ```js | ||
| import { setGlobalDispatcher, request, Socks5ProxyAgent } from 'undici' | ||
|
|
||
| // Authentication via URL | ||
| const socks5Proxy = new Socks5ProxyAgent('socks5://username:password@localhost:1080') | ||
|
|
||
| // Or authentication via options | ||
| // const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080', { | ||
| // username: 'username', | ||
| // password: 'password' | ||
| // }) | ||
|
|
||
| setGlobalDispatcher(socks5Proxy) | ||
|
|
||
| const { statusCode, body } = await request('http://localhost:3000/foo') | ||
|
|
||
| console.log('response received', statusCode) // response received 200 | ||
|
|
||
| for await (const data of body) { | ||
| console.log('data', data.toString('utf8')) // data foo | ||
| } | ||
| ``` | ||
|
|
||
| #### Example - SOCKS5 Proxy with HTTPS requests | ||
|
|
||
| SOCKS5 proxy supports both HTTP and HTTPS requests through tunneling: | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent, request } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
|
|
||
| const response = await request('https://api.example.com/data', { | ||
| dispatcher: socks5Proxy, | ||
| method: 'GET' | ||
| }) | ||
|
|
||
| console.log('Response status:', response.statusCode) | ||
| console.log('Response data:', await response.body.json()) | ||
| ``` | ||
|
|
||
| #### Example - SOCKS5 Proxy with Fetch | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent, fetch } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
|
|
||
| const response = await fetch('http://localhost:3000/api/users', { | ||
| dispatcher: socks5Proxy, | ||
| method: 'GET' | ||
| }) | ||
|
|
||
| console.log('Response status:', response.status) | ||
| console.log('Response data:', await response.text()) | ||
| ``` | ||
|
|
||
| #### Example - Connection Pooling | ||
|
|
||
| SOCKS5ProxyWrapper automatically manages connection pooling for better performance: | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent, request } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080', { | ||
| connections: 10, // Allow up to 10 concurrent connections | ||
| pipelining: 1 // Enable HTTP/1.1 pipelining | ||
| }) | ||
|
|
||
| // Multiple requests will reuse connections through the SOCKS5 tunnel | ||
| const responses = await Promise.all([ | ||
| request('http://api.example.com/endpoint1', { dispatcher: socks5Proxy }), | ||
| request('http://api.example.com/endpoint2', { dispatcher: socks5Proxy }), | ||
| request('http://api.example.com/endpoint3', { dispatcher: socks5Proxy }) | ||
| ]) | ||
|
|
||
| console.log('All requests completed through the same SOCKS5 proxy') | ||
| ``` | ||
|
|
||
| ### `Socks5ProxyAgent.close()` | ||
|
|
||
| Closes the SOCKS5 proxy wrapper and waits for all underlying pools and connections to close before resolving. | ||
|
|
||
| Returns: `Promise<void>` | ||
|
|
||
| #### Example - clean up after tests are complete | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent, setGlobalDispatcher } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
| setGlobalDispatcher(socks5Proxy) | ||
|
|
||
| // ... make requests | ||
|
|
||
| await socks5Proxy.close() | ||
| ``` | ||
|
|
||
| ### `Socks5ProxyAgent.destroy([err])` | ||
|
|
||
| Destroys the SOCKS5 proxy wrapper and all underlying connections immediately. | ||
|
|
||
| Arguments: | ||
| * **err** `Error` (optional) - The error that caused the destruction. | ||
|
|
||
| Returns: `Promise<void>` | ||
|
|
||
| #### Example - force close all connections | ||
|
|
||
| ```js | ||
| import { Socks5ProxyAgent } from 'undici' | ||
|
|
||
| const socks5Proxy = new Socks5ProxyAgent('socks5://localhost:1080') | ||
|
|
||
| // Force close all connections | ||
| await socks5Proxy.destroy() | ||
| ``` | ||
|
|
||
| ### `Socks5ProxyAgent.dispatch(options, handlers)` | ||
|
|
||
| Implements [`Dispatcher.dispatch(options, handlers)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handlers). | ||
|
|
||
| ### `Socks5ProxyAgent.request(options[, callback])` | ||
|
|
||
| See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback). | ||
|
|
||
| ## Debugging | ||
|
|
||
| SOCKS5 proxy connections can be debugged using Node.js diagnostics: | ||
|
|
||
| ```sh | ||
| NODE_DEBUG=undici:socks5 node script.js | ||
| ``` | ||
|
|
||
| This will output detailed information about the SOCKS5 handshake, authentication, and connection establishment. | ||
|
|
||
| ## SOCKS5 Protocol Support | ||
|
|
||
| The Socks5ProxyAgent supports the following SOCKS5 features: | ||
|
|
||
| ### Authentication Methods | ||
|
|
||
| - **No Authentication** (`0x00`) - For public or internal proxies | ||
| - **Username/Password** (`0x02`) - RFC 1929 authentication | ||
|
|
||
| ### Address Types | ||
|
|
||
| - **IPv4** (`0x01`) - Standard IPv4 addresses | ||
| - **Domain Name** (`0x03`) - Domain names (recommended for flexibility) | ||
| - **IPv6** (`0x04`) - IPv6 addresses (full support for standard and compressed notation) | ||
|
|
||
| ### Commands | ||
|
|
||
| - **CONNECT** (`0x01`) - Establish TCP connection (primary use case for HTTP) | ||
|
|
||
| ### Error Handling | ||
|
|
||
| The wrapper handles various SOCKS5 error conditions: | ||
|
|
||
| - Connection refused by proxy | ||
| - Authentication failures | ||
| - Network unreachable | ||
| - Host unreachable | ||
| - Unsupported address types or commands | ||
|
|
||
| ## Performance Considerations | ||
|
|
||
| - **Connection Pooling**: Automatically pools connections through the SOCKS5 tunnel for better performance | ||
| - **HTTP/1.1 Pipelining**: Supports pipelining when enabled | ||
| - **DNS Resolution**: Domain names are resolved by the SOCKS5 proxy, reducing local DNS queries | ||
| - **TLS Termination**: HTTPS connections are encrypted end-to-end, with the SOCKS5 proxy only handling the TCP tunnel | ||
|
|
||
| ## Security Notes | ||
|
|
||
| 1. **Authentication**: Credentials are sent to the SOCKS5 proxy in plaintext unless using SOCKS5 over TLS | ||
| 2. **DNS Leaks**: All DNS resolution happens on the proxy server, preventing DNS leaks | ||
| 3. **End-to-end Encryption**: HTTPS traffic remains encrypted between client and final destination | ||
| 4. **Connection Security**: Consider using authenticated proxies and secure networks | ||
|
|
||
| ## Compatibility | ||
|
|
||
| - **Protocol**: SOCKS5 (RFC 1928) with Username/Password Authentication (RFC 1929) | ||
| - **Transport**: TCP only (UDP support not implemented) | ||
| - **Node.js**: Compatible with all supported Node.js versions | ||
| - **HTTP Versions**: Works with HTTP/1.1 and HTTP/2 over the tunnel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.