2020-08-28 09:03:50 -04:00
|
|
|
// Used for benchmarking Deno's tcp proxy performance.
|
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 {
|
|
|
|
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.message !== "read error" && err.message !== "write error") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
conn.close();
|
|
|
|
origin.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|