From 8d2567cb5afa15e252ec526a97225917ec521921 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 24 Jun 2022 19:19:51 -0500 Subject: [PATCH] plexer --- deno.json | 0 tcplexer.ts | 46 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 deno.json diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..e69de29 diff --git a/tcplexer.ts b/tcplexer.ts index aea60a0..2899db5 100644 --- a/tcplexer.ts +++ b/tcplexer.ts @@ -1,6 +1,40 @@ -import { copy } from "https://deno.land/std@0.145.0/streams/conversion.ts"; -const listener = Deno.listen({ port: 8080 }); -console.log("listening on 0.0.0.0:8080"); -for await (const conn of listener) { - copy(conn, conn).finally(() => conn.close()); -} +import { iterateReader } from "https://deno.land/std@0.145.0/streams/conversion.ts"; + +const clients: Set = new Set(); + +(async () => { + try { + const sourceListener = Deno.listen({ port: 8080 }); + for await (const source of sourceListener) { + console.info("Source connected from", source.remoteAddr); + for await (const packet of iterateReader(source)) { + clients.forEach((client, _v2, _set) => + client.write(packet).catch(() => { + console.log(`Client disconnected from`, client.remoteAddr); + clients.delete(client); + }) + ); + } + console.info(`Source disconnected from`, source.remoteAddr); + } + } catch (err) { + console.error("Error listening for TCP connections on port 8080:", err); + } +})(); + +(async () => { + try { + const clientsListener = Deno.listen({ port: 8081 }); + while (true) { + try { + const conn = await clientsListener.accept(); + console.log("Client connected from", conn.remoteAddr); + clients.add(conn); + } catch (err) { + console.error("Error accepting client connection:", err); + } + } + } catch (err) { + console.error("Error listening for TCP connections on port 8081:", err); + } +})();