2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-01-02 08:04:40 -05:00
|
|
|
const addr = Deno.args[1] || "localhost:4555";
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2024-07-25 01:30:28 -04:00
|
|
|
function proxyServer() {
|
2022-01-02 08:04:40 -05:00
|
|
|
const [hostname, p] = addr.split(":");
|
|
|
|
const port = parseInt(p ?? 4555);
|
2024-07-25 01:30:28 -04:00
|
|
|
Deno.serve({ hostname, port }, handler);
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2022-01-02 08:04:40 -05:00
|
|
|
async function handler(req: Request): Promise<Response> {
|
2019-09-24 18:52:01 -04:00
|
|
|
console.log(`Proxy request to: ${req.url}`);
|
2022-01-02 08:04:40 -05:00
|
|
|
const headers = new Headers(req.headers);
|
|
|
|
const proxyAuthorization = headers.get("proxy-authorization");
|
2021-06-21 23:21:57 -04:00
|
|
|
if (proxyAuthorization) {
|
|
|
|
console.log(`proxy-authorization: ${proxyAuthorization}`);
|
2022-01-02 08:04:40 -05:00
|
|
|
headers.delete("proxy-authorization");
|
2021-06-21 23:21:57 -04:00
|
|
|
}
|
2019-09-24 18:52:01 -04:00
|
|
|
const resp = await fetch(req.url, {
|
|
|
|
method: req.method,
|
2022-01-02 08:04:40 -05:00
|
|
|
headers: headers,
|
2019-09-24 18:52:01 -04:00
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
return new Response(await resp.bytes(), {
|
2020-07-11 12:29:55 -04:00
|
|
|
status: resp.status,
|
|
|
|
headers: resp.headers,
|
|
|
|
});
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2024-05-14 12:47:57 -04:00
|
|
|
function assertSuccessOutput(output: Deno.CommandOutput) {
|
|
|
|
if (output.code !== 0) {
|
|
|
|
console.error("STDOUT", new TextDecoder().decode(output.stdout));
|
|
|
|
console.error("STDERR", new TextDecoder().decode(output.stderr));
|
|
|
|
throw new Error(`Expected exit code 0, was ${output.code}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetch() {
|
2024-05-14 12:47:57 -04:00
|
|
|
const output = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-07-11 12:29:55 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
2024-05-14 12:47:57 -04:00
|
|
|
"proxy_client.ts",
|
2020-07-11 12:29:55 -04:00
|
|
|
],
|
2019-09-24 18:52:01 -04:00
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2024-05-14 12:47:57 -04:00
|
|
|
assertSuccessOutput(output);
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testModuleDownload() {
|
2024-05-14 12:47:57 -04:00
|
|
|
const output = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-04-07 11:24:47 -04:00
|
|
|
"cache",
|
2020-07-11 12:29:55 -04:00
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/run/045_mod.ts",
|
2019-09-24 18:52:01 -04:00
|
|
|
],
|
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2024-05-14 12:47:57 -04:00
|
|
|
assertSuccessOutput(output);
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetchNoProxy() {
|
2024-05-14 12:47:57 -04:00
|
|
|
const output = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-08-15 09:48:29 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
2024-05-14 12:47:57 -04:00
|
|
|
"proxy_client.ts",
|
2020-08-15 09:48:29 -04:00
|
|
|
],
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2020-08-15 09:48:29 -04:00
|
|
|
|
2024-05-14 12:47:57 -04:00
|
|
|
assertSuccessOutput(output);
|
2020-08-15 09:48:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testModuleDownloadNoProxy() {
|
2024-05-14 12:47:57 -04:00
|
|
|
const output = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-08-15 09:48:29 -04:00
|
|
|
"cache",
|
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/run/045_mod.ts",
|
2020-08-15 09:48:29 -04:00
|
|
|
],
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2020-08-15 09:48:29 -04:00
|
|
|
|
2024-05-14 12:47:57 -04:00
|
|
|
assertSuccessOutput(output);
|
2020-08-15 09:48:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetchProgrammaticProxy() {
|
2024-05-14 12:47:57 -04:00
|
|
|
const output = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2021-06-21 23:21:57 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net=localhost:4545,localhost:4555",
|
2024-05-14 12:47:57 -04:00
|
|
|
"programmatic_proxy_client.ts",
|
2021-06-21 23:21:57 -04:00
|
|
|
],
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2024-05-14 12:47:57 -04:00
|
|
|
|
|
|
|
assertSuccessOutput(output);
|
2021-06-21 23:21:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 09:04:42 -04:00
|
|
|
proxyServer();
|
|
|
|
await testFetch();
|
|
|
|
await testModuleDownload();
|
2020-08-15 09:48:29 -04:00
|
|
|
await testFetchNoProxy();
|
|
|
|
await testModuleDownloadNoProxy();
|
2021-06-21 23:21:57 -04:00
|
|
|
await testFetchProgrammaticProxy();
|
2019-10-27 09:04:42 -04:00
|
|
|
Deno.exit(0);
|