79317803

Date: 2024-12-30 14:07:30
Score: 0.5
Natty:
Report link

I got it working. This is the final code.

wss.on("connection", async (ws, req) => {
  const CONTAINER_ID = req.url?.split("=")[1];
  try {
    const container = docker.getContainer(CONTAINER_ID as string);
    const exec = await container.exec({
      Cmd: ["/bin/sh"],
      AttachStdin: true,
      AttachStdout: true,
      User: "root",
      Tty: true,
    });
    const stream = await exec.start({
      stdin: true,
      hijack: true,
      Detach: false,
    });
    stream.on("data", (data) => {
      ws.send(data.toString());
    });
    ws.on("message", (msg) => {
      stream.write(msg);
    });
    ws.on("close", async () => {
      stream.write("exit\n");
    });
  } catch (error) {
    console.log(error);
    ws.close();
  }
});

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Anish