-
Notifications
You must be signed in to change notification settings - Fork 149
Add HTTPS upstream proxy support with option to ignore proxy cert errors #577
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32dd094
Add HTTPS proxy support with option to ignore proxy cert errors
patrickkfkan 93cf448
Fix `createTunnel()` error when no options specified
patrickkfkan 26ab339
Merge branch 'apify:master' into ignore-proxy-cert
patrickkfkan 660b098
chore: lint
patrickkfkan 276c153
Refactor
patrickkfkan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,14 +68,18 @@ const server = new ProxyChain.Server({ | |
// requiring Basic authentication. Here you can verify user credentials. | ||
requestAuthentication: username !== 'bob' || password !== 'TopSecret', | ||
|
||
// Sets up an upstream HTTP/SOCKS proxy to which all the requests are forwarded. | ||
// Sets up an upstream HTTP/HTTPS/SOCKS proxy to which all the requests are forwarded. | ||
// If null, the proxy works in direct mode, i.e. the connection is forwarded directly | ||
// to the target server. This field is ignored if "requestAuthentication" is true. | ||
// The username and password must be URI-encoded. | ||
upstreamProxyUrl: `http://username:[email protected]:3128`, | ||
// Or use SOCKS4/5 proxy, e.g. | ||
// upstreamProxyUrl: `socks://username:[email protected]:1080`, | ||
|
||
// Applies to HTTPS upstream proxy. If set to true, requests made to the proxy will | ||
// ignore certificate errors. Useful when upstream proxy uses self-signed certificate. By default "false". | ||
ignoreUpstreamProxyCertificate: true | ||
|
||
// If "requestAuthentication" is true, you can use the following property | ||
// to define a custom error message to return to the client instead of the default "Proxy credentials required" | ||
failMsg: 'Bad username or password, please try again.', | ||
|
@@ -368,10 +372,13 @@ The package also provides several utility functions. | |
|
||
### `anonymizeProxy({ url, port }, callback)` | ||
|
||
Parses and validates a HTTP proxy URL. If the proxy requires authentication, | ||
Parses and validates a HTTP/HTTPS proxy URL. If the proxy requires authentication, | ||
then the function starts an open local proxy server that forwards to the proxy. | ||
The port (on which the local proxy server will start) can be set via the `port` property of the first argument, if not provided, it will be chosen randomly. | ||
|
||
For HTTPS proxy with self-signed certificate, set `ignoreProxyCertificate` property of the first argument to `true` to ignore certificate errors in | ||
proxy requests. | ||
|
||
The function takes an optional callback that receives the anonymous proxy URL. | ||
If no callback is supplied, the function returns a promise that resolves to a String with | ||
anonymous proxy URL or the original URL if it was already anonymous. | ||
|
@@ -420,13 +427,14 @@ If callback is not provided, the function returns a promise instead. | |
|
||
### `createTunnel(proxyUrl, targetHost, options, callback)` | ||
|
||
Creates a TCP tunnel to `targetHost` that goes through a HTTP proxy server | ||
Creates a TCP tunnel to `targetHost` that goes through a HTTP/HTTPS proxy server | ||
specified by the `proxyUrl` parameter. | ||
|
||
The optional `options` parameter is an object with the following properties: | ||
- `port: Number` - Enables specifying the local port to listen at. By default `0`, | ||
which means a random port will be selected. | ||
- `hostname: String` - Local hostname to listen at. By default `localhost`. | ||
- `ignoreProxyCertificate` - For HTTPS proxy, ignore certificate errors in proxy requests. Useful for proxy with self-signed certificate. By default `false`. | ||
- `verbose: Boolean` - If `true`, the functions logs a lot. By default `false`. | ||
|
||
The result of the function is a local endpoint in a form of `hostname:port`. | ||
|
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
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
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 |
---|---|---|
|
@@ -19,14 +19,15 @@ | |
export async function createTunnel( | ||
proxyUrl: string, | ||
targetHost: string, | ||
options: { | ||
options?: { | ||
verbose?: boolean; | ||
ignoreProxyCertificate?: boolean; | ||
}, | ||
callback?: (error: Error | null, result?: string) => void, | ||
): Promise<string> { | ||
const parsedProxyUrl = new URL(proxyUrl); | ||
if (parsedProxyUrl.protocol !== 'http:') { | ||
throw new Error(`The proxy URL must have the "http" protocol (was "${proxyUrl}")`); | ||
if (parsedProxyUrl.protocol !== 'http:' && parsedProxyUrl.protocol !== 'https:') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please do something with an array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
throw new Error(`The proxy URL must have the "http" or "https" protocol (was "${proxyUrl}")`); | ||
} | ||
|
||
const url = new URL(`connect://${targetHost || ''}`); | ||
|
@@ -44,7 +45,7 @@ | |
const server: net.Server & { log?: (...args: unknown[]) => void } = net.createServer(); | ||
|
||
const log = (...args: unknown[]): void => { | ||
if (verbose) console.log(...args); | ||
}; | ||
|
||
server.log = log; | ||
|
@@ -67,7 +68,10 @@ | |
chain({ | ||
request: { url: targetHost }, | ||
sourceSocket, | ||
handlerOpts: { upstreamProxyUrlParsed: parsedProxyUrl }, | ||
handlerOpts: { | ||
upstreamProxyUrlParsed: parsedProxyUrl, | ||
ignoreUpstreamProxyCertificate: options?.ignoreProxyCertificate ?? false, | ||
}, | ||
server: server as net.Server & { log: typeof log }, | ||
isPlain: true, | ||
}); | ||
|
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.