2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-08-28 09:03:50 -04:00
|
|
|
// Used for benchmarking Deno's tcp proxy performance.
|
2021-07-12 13:44:42 -04:00
|
|
|
import { copy } from "../../test_util/std/io/util.ts";
|
|
|
|
|
2020-01-09 13:37:01 -05:00
|
|
|
const addr = Deno.args[0] || "127.0.0.1:4500";
|
|
|
|
const originAddr = Deno.args[1] || "127.0.0.1:4501";
|
2019-06-06 22:46:18 -04:00
|
|
|
|
2019-09-20 18:32:18 -04:00
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const [originHostname, originPort] = originAddr.split(":");
|
|
|
|
|
|
|
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
2019-06-06 22:46:18 -04:00
|
|
|
|
|
|
|
async function handle(conn: Deno.Conn): Promise<void> {
|
2020-01-18 12:35:12 -05:00
|
|
|
const origin = await Deno.connect({
|
2019-09-20 18:32:18 -04:00
|
|
|
hostname: originHostname,
|
2020-03-28 13:03:49 -04:00
|
|
|
port: Number(originPort),
|
2019-09-20 18:32:18 -04:00
|
|
|
});
|
2019-06-06 22:46:18 -04:00
|
|
|
try {
|
2021-07-12 13:44:42 -04:00
|
|
|
await Promise.all([copy(conn, origin), copy(origin, conn)]);
|
2021-04-08 12:04:02 -04:00
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
!(e instanceof Deno.errors.BrokenPipe) &&
|
|
|
|
!(e instanceof Deno.errors.ConnectionReset)
|
|
|
|
) {
|
|
|
|
throw e;
|
2019-06-06 22:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 12:04:02 -04:00
|
|
|
conn.close();
|
|
|
|
origin.close();
|
2019-06-06 22:46:18 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:58:35 -04:00
|
|
|
console.log(`Proxy listening on http://${addr}/`);
|
|
|
|
for await (const conn of listener) {
|
|
|
|
handle(conn);
|
2019-06-06 22:46:18 -04:00
|
|
|
}
|