From 6a77e1294ef6b5c4bc5faa515fe94c92d1b478d6 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Mon, 11 Dec 2023 18:06:33 +0100 Subject: [PATCH] cleanup --- instance/src/bin/proxy.ts | 32 -------------------------------- instance/src/lib.ts | 37 +++++-------------------------------- 2 files changed, 5 insertions(+), 64 deletions(-) delete mode 100755 instance/src/bin/proxy.ts diff --git a/instance/src/bin/proxy.ts b/instance/src/bin/proxy.ts deleted file mode 100755 index 16611d1..0000000 --- a/instance/src/bin/proxy.ts +++ /dev/null @@ -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 "); - 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 }); -} diff --git a/instance/src/lib.ts b/instance/src/lib.ts index 84382c1..be1c432 100644 --- a/instance/src/lib.ts +++ b/instance/src/lib.ts @@ -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; -}