DenoDDeno
Powered by
TaxBusbyT
Denoβ€’16mo agoβ€’
4 replies
TaxBusby

Piping two TCP connections to each other

Hey, I'm messing around trying to create a proxy in deno. I want to take an incoming connection, use some logic to decide how to route it, and then create an outgoing connection to pass it to.

I have some code like this right now:

const listener = Deno.listen({
  hostname: "127.0.0.1",
  port: 8445,
  transport: "tcp",
});
for await (const inConn of listener) {
  const buf = new Uint8Array(4096);

  await inConn.read(buf);
  // TODO Routing logic
  console.log("Connecting to local server (OUT)...");
  const outConn = await Deno.connect({
    hostname: "127.0.0.1",
    port: 8443,
    transport: "tcp",
  });
  console.log("Flushing buffer to OUT");
  await outConn.write(buf);

  console.log("Send all other data from IN to OUT");
  inConn.readable.pipeTo(outConn.writable);

  console.log("Send all other data from OUT to IN");
  outConn.readable.pipeTo(inConn.writable);

  console.log("Finished establishing connetion");
}
const listener = Deno.listen({
  hostname: "127.0.0.1",
  port: 8445,
  transport: "tcp",
});
for await (const inConn of listener) {
  const buf = new Uint8Array(4096);

  await inConn.read(buf);
  // TODO Routing logic
  console.log("Connecting to local server (OUT)...");
  const outConn = await Deno.connect({
    hostname: "127.0.0.1",
    port: 8443,
    transport: "tcp",
  });
  console.log("Flushing buffer to OUT");
  await outConn.write(buf);

  console.log("Send all other data from IN to OUT");
  inConn.readable.pipeTo(outConn.writable);

  console.log("Send all other data from OUT to IN");
  outConn.readable.pipeTo(inConn.writable);

  console.log("Finished establishing connetion");
}


Which seems right to me in theory, but when I run it I get some streaming error after these commands all run:
Connecting to local server (OUT)...
Flushing buffer to OUT
Send all other data from IN to OUT
Send all other data from OUT to IN
Finished establishing connetion
error: Uncaught (in promise) Interrupted: operation canceled
    at async Object.pull (ext:deno_web/06_streams.js:991:27)
Connecting to local server (OUT)...
Flushing buffer to OUT
Send all other data from IN to OUT
Send all other data from OUT to IN
Finished establishing connetion
error: Uncaught (in promise) Interrupted: operation canceled
    at async Object.pull (ext:deno_web/06_streams.js:991:27)

Any idea if I'm using these APIs wrong, or if this might be a bug? I noticed
pipeTo
pipeTo
returns a promise, but if I await these it seems to stall the program indefinitely (like it's waiting for the pipe to close)
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Reuse Deno.serve for other TCP connections
EsenteEEsente / help
3y ago
Deno Subprocess piping is slow when piping large data between processes
phoenisxPphoenisx / help
4y ago
piping for async sub process
dan.the.discloserDdan.the.discloser / help
4y ago
TLS connections
mscmls.xMmscmls.x / help
15mo ago