-
-
Notifications
You must be signed in to change notification settings - Fork 104
proxy: support setting proxy via --proxyServer, PROXY_SERVER env var or PROXY_HOST + PROXY_PORT env vars #589
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
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0a881b9
proxy: fix proxy settings when PROXY_PORT and PROXY_PORT env vars are…
ikreymer 5354c63
remove obsolete PROXY_* vars
ikreymer eb47cb7
add support for PROXY_SERVER env var for setting custom proxy, as well
ikreymer ec07cc9
WIP 1
vnznznz ae146b1
switch to undici proxy
ikreymer 1202b52
tweak logging, hostname use
ikreymer b28a417
use global dispatcher
ikreymer b694278
add missing file
ikreymer 6162804
proxy auth:
ikreymer 4ee0ce6
ensure --proxyServer cli flag also supported by both direct fetch and…
ikreymer 9191b6c
tests fix: use docker network, persistent proxies accross tests
ikreymer 3764df3
remove logging test
ikreymer 476e639
Merge branch 'main' into fix-proxy
tw4l 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
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
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,60 @@ | ||
import { Dispatcher, ProxyAgent, setGlobalDispatcher } from "undici"; | ||
|
||
import { socksDispatcher } from "fetch-socks"; | ||
import type { SocksProxyType } from "socks/typings/common/constants.js"; | ||
|
||
export function getEnvProxyUrl() { | ||
if (process.env.PROXY_SERVER) { | ||
return process.env.PROXY_SERVER; | ||
} | ||
|
||
// for backwards compatibility with 0.x proxy settings | ||
if (process.env.PROXY_HOST && process.env.PROXY_PORT) { | ||
return `http://${process.env.PROXY_HOST}:${process.env.PROXY_PORT}`; | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
export function initProxy(proxy?: string): string { | ||
if (!proxy) { | ||
proxy = getEnvProxyUrl(); | ||
} | ||
if (proxy) { | ||
const dispatcher = createDispatcher(proxy); | ||
if (dispatcher) { | ||
setGlobalDispatcher(dispatcher); | ||
return proxy; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
export function createDispatcher(proxyUrl: string): Dispatcher | undefined { | ||
if (proxyUrl.startsWith("http://") || proxyUrl.startsWith("https://")) { | ||
// HTTP PROXY does not support auth, as it's not supported in the browser | ||
// so must drop username/password for consistency | ||
const url = new URL(proxyUrl); | ||
url.username = ""; | ||
url.password = ""; | ||
return new ProxyAgent({ uri: url.href }); | ||
} else if ( | ||
proxyUrl.startsWith("socks://") || | ||
proxyUrl.startsWith("socks5://") || | ||
proxyUrl.startsWith("socks4://") | ||
) { | ||
// support auth as SOCKS5 auth *is* supported in Brave (though not in Chromium) | ||
const url = new URL(proxyUrl); | ||
const type: SocksProxyType = url.protocol === "socks4:" ? 4 : 5; | ||
const params = { | ||
type, | ||
host: url.hostname, | ||
port: parseInt(url.port), | ||
userId: url.username || undefined, | ||
password: url.password || undefined, | ||
}; | ||
return socksDispatcher(params); | ||
} else { | ||
return undefined; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { execSync, exec } from "child_process"; | ||
|
||
const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); | ||
|
||
const PROXY_IMAGE = "tarampampam/3proxy:1.9.1"; | ||
const SOCKS_PORT = "1080"; | ||
const HTTP_PORT = "3128"; | ||
const WRONG_PORT = "33130"; | ||
|
||
const PDF = "https://specs.webrecorder.net/wacz/1.1.1/wacz-2021.pdf"; | ||
const HTML = "https://webrecorder.net/"; | ||
|
||
const extraArgs = "--limit 1 --failOnFailedSeed --timeout 10 --logging debug"; | ||
|
||
let proxyAuthId; | ||
let proxyNoAuthId; | ||
|
||
beforeAll(() => { | ||
execSync("docker network create proxy-test-net"); | ||
|
||
proxyAuthId = execSync(`docker run -e PROXY_LOGIN=user -e PROXY_PASSWORD=passw0rd -d --rm --network=proxy-test-net --name proxy-with-auth ${PROXY_IMAGE}`, {encoding: "utf-8"}); | ||
|
||
proxyNoAuthId = execSync(`docker run -d --rm --network=proxy-test-net --name proxy-no-auth ${PROXY_IMAGE}`, {encoding: "utf-8"}); | ||
}); | ||
|
||
afterAll(async () => { | ||
execSync(`docker kill -s SIGINT ${proxyAuthId}`); | ||
execSync(`docker kill -s SIGINT ${proxyNoAuthId}`); | ||
await sleep(3000); | ||
execSync("docker network rm proxy-test-net"); | ||
}); | ||
|
||
describe("socks5 + https proxy tests", () => { | ||
for (const scheme of ["socks5", "http"]) { | ||
const port = scheme === "socks5" ? SOCKS_PORT : HTTP_PORT; | ||
|
||
for (const type of ["HTML page", "PDF"]) { | ||
Comment on lines
+34
to
+37
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. nice homegrown parametrization here |
||
|
||
const url = type === "PDF" ? PDF : HTML; | ||
|
||
test(`${scheme} proxy, ${type}, no auth`, () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_SERVER=${scheme}://proxy-no-auth:${port} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${url} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(0); | ||
}); | ||
|
||
test(`${scheme} proxy, ${type}, with auth`, () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_SERVER=${scheme}://user:passw0rd@proxy-with-auth:${port} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${url} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
// auth supported only for SOCKS5 | ||
expect(status).toBe(scheme === "socks5" ? 0 : 1); | ||
}); | ||
|
||
test(`${scheme} proxy, ${type}, wrong auth`, () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_SERVER=${scheme}://user:passw1rd@proxy-with-auth:${port} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${url} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(1); | ||
}); | ||
|
||
test(`${scheme} proxy, ${type}, wrong protocol`, () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_SERVER=${scheme}://user:passw1rd@proxy-with-auth:${scheme === "socks5" ? HTTP_PORT : SOCKS_PORT} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${url} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(1); | ||
}); | ||
} | ||
|
||
test(`${scheme} proxy, proxy missing error`, () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_SERVER=${scheme}://proxy-no-auth:${WRONG_PORT} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${HTML} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(1); | ||
}); | ||
} | ||
}); | ||
|
||
|
||
test("http proxy, PDF, separate env vars", () => { | ||
execSync(`docker run -e PROXY_HOST=proxy-no-auth -e PROXY_PORT=${HTTP_PORT} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${PDF} ${extraArgs}`, {encoding: "utf-8"}); | ||
}); | ||
|
||
test("http proxy set, but not running, separate env vars", () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run -e PROXY_HOST=proxy-no-auth -e PROXY_PORT=${WRONG_PORT} --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --url ${PDF} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(1); | ||
}); | ||
|
||
test("http proxy set, but not running, cli arg", () => { | ||
let status = 0; | ||
|
||
try { | ||
execSync(`docker run --rm --network=proxy-test-net webrecorder/browsertrix-crawler crawl --proxyServer http://proxy-no-auth:${WRONG_PORT} --url ${PDF} ${extraArgs}`, {encoding: "utf-8"}); | ||
} catch (e) { | ||
status = e.status; | ||
} | ||
expect(status).toBe(1); | ||
}); | ||
|
||
|
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.