This commit is contained in:
JOLIMAITRE Matthieu 2023-12-11 18:06:33 +01:00
parent b1ee328238
commit 6a77e1294e
2 changed files with 5 additions and 64 deletions

View file

@ -1,32 +0,0 @@
#!/bin/env -S deno run -A --unstable
if (import.meta.main) await main();
async function main() {
const [from_port, to_ip, to_port] = Deno.args;
if (to_port === undefined) {
console.error("[proxy] usage: ./proxy.ts <from_port> <to_ip> <to_port>");
Deno.exit(2);
}
const server = Deno.listen({ transport: "tcp", port: parseInt(from_port) });
console.log("[proxy] listening on port", from_port, "redirecting to", to_ip, "port", to_port);
for await (const connection of server) {
serve(to_ip, to_port, connection);
}
}
async function serve(to_ip: string, to_port: string, connection: Deno.Conn) {
try {
const client = await Deno.connect({ transport: "tcp", hostname: to_ip, port: parseInt(to_port) });
const promise_connection_done = connection.readable.pipeTo(client.writable);
const promise_client_done = client.readable.pipeTo(connection.writable);
await Promise.all([promise_client_done, promise_connection_done]);
} catch (_) { /* isok */ }
}
export function proxy_command(from_port: number, to_ip: string, to_port: number) {
const path = new URL("", import.meta.url).pathname;
const args = [from_port, to_ip, to_port].map((v) => v.toString());
return new Deno.Command(path, { args });
}

View file

@ -1,11 +1,10 @@
export type { Base, BaseContext } from "./lib/create.ts";
import { toText } from "https://deno.land/std@0.208.0/streams/to_text.ts";
import { lines, log_from, loop_process, LoopProcess, run, sleep } from "./lib/utils.ts";
import { lines, log_from, loop_process, run } from "./lib/utils.ts";
import { ContainerConfig } from "./lib/config.ts";
import { container_paths } from "./lib/paths.ts";
import { container_command, get_container_addresses } from "./lib/nspawn.ts";
import { proxy_command } from "./bin/proxy.ts";
import { container_command } from "./lib/nspawn.ts";
const log = log_from("lib");
@ -80,40 +79,14 @@ export function start_runner(config: ContainerConfig) {
stdout: "null",
},
});
// const proxies = [] as LoopProcess[];
const container_loop = loop_process(command, {
on_start: () => {
log("container", name, "started");
// (async () => {
// await sleep(1_000);
// const [address] = await get_container_addresses(name);
// proxies.push(...start_proxies(address, config.redirects));
// })();
},
on_stop: async () => {
log("container", name, "stopped");
// for (const p of proxies) await p.kill();
// await sleep(500);
// proxies.splice(0, proxies.length);
},
on_start: () => log("container", name, "started"),
on_stop: () => log("container", name, "stopped"),
});
return {
name,
config,
container_loop,
stop: async () => {
// for (const p of proxies) await p.kill();
await container_loop.kill();
},
stop: () => container_loop.kill(),
};
}
function start_proxies(address: string, redirects: [number, number][]) {
const redirections = redirects
.map(([from_port, to_port]) => {
return loop_process(proxy_command(from_port, address, to_port), {
on_start: () => console.log("starting proxy", from_port, "to", address, "port", to_port),
});
});
return redirections;
}