0

I'm new to NodeJS. The existing net.socket pipe need to filter by condition to not to connect to "con2", my existing code as follow.

I found Transform and PipeLine methods and, so far I tried, the sample code not working for my scenario yet.

The condition is in "con1" read stream data have some keyword. e.g. "Output" Then, not to connect or transform data as empty to "con2". So that, "con2" not to process.

Start.js

import Proxy from "./Proxy.js";

const proxy = Proxy();
  proxy.listen('4000');

Proxy.js

import net from "net";

export default () =>
    net.createServer((con1) => {
        const con2 = net.connect(
          '1234',
          '127.0.0.1'
        );
        
        con1.pipe(con2).pipe(con1);
    
        con2.on("data", async (data) => {
          try {
                console.log("sent from con2:", data);
               }
          }
        }
        con1.on("data", async (data) => {
          try {
                console.log("sent from con1:", data);
               }
          }
        }

Please help advise. Thanks a lot in advance.

Thet Thet
  • 13
  • 5

1 Answers1

0

I have put something together:

const net = require("net");

const { Transform, pipeline } = require("stream");


const spy = (client) => {
    return new Transform({
        transform(chunk, encoding, cb) {
            if (chunk.toString("utf-8").includes("output")) {

                cb(new Error("ABORTED!"));

            } else {

                cb(null, chunk);

            }
        }
    });
};


let server = net.createServer((socket) => {

    const client = net.connect('1234', '127.0.0.1');

    pipeline(socket, spy(client), client, (err) => {
        console.log("pipeline closed", err);
    });

});

server.listen(1235, "127.0.0.1", () => {
    console.log("Go and send something to the tcp socket, tcp://127.0.0.1:1235")
});

Start a simple netcat server to see what we send there:

while true; do nc -l -p 1234; done;

When we now connect with another netcat, we can send stuff over the proxy to the other netcat instance:

nc 127.0.0.1 1235

When we now send the "output" keyword, the connection gets aborted/terminated.

The spy functions simple inspects the chunk that pass through the transform stream in the pipeline and checks for the string "output", if its found, the pipeline is closed and both client sockets/connections are terminated.

Marc
  • 2,920
  • 3
  • 14
  • 30
  • Thanks for your full sample code, I'm trying to use your sample and test running. – Thet Thet Jan 13 '22 at 10:15
  • @ThetThet If its helpfull or the solution you are looking for please upvote/mark as solution, to help others with the same issue. – Marc Jan 13 '22 at 10:33
  • Sure, will do, so far I tried, the stream pass through "spy(client)" transform, but, "client" not received the stream. Let me find out on it. – Thet Thet Jan 13 '22 at 11:37
  • @ThetThet When you run my commands, you can clearly see that the netcat dummy server receices everything that goes throught the proxy. Can you post a reproducible example? (preferable with netcat) – Marc Jan 13 '22 at 13:03
  • I'm sorry, I don't know how to use netcat and will check I'm running on Microsoft Window and I'll try to post again. – Thet Thet Jan 14 '22 at 01:28
  • I make it as answer first, don't want to waste your effort and thank you so much – Thet Thet Jan 14 '22 at 02:40
  • You can use Linux commands/related stuff with WSL on Windows. U can try/play with netcat there. Just copy & paste the commands ;) – Marc Jan 14 '22 at 07:06
  • Thks noted, I'll try. – Thet Thet Jan 14 '22 at 08:11
  • D:\Installer\nc>nc -l -p 1234 hello testing ---- D:\Installer\nc>nc 127.0.0.1 1235 hello testing output Yes !! It's working, but, don't know why my project not working yet. :( Thks for your guidance. – Thet Thet Jan 14 '22 at 09:09
  • My project also working already. :D Thanks a lot, Marc. – Thet Thet Jan 17 '22 at 08:36
  • @ThetThet Its me a pleasure :) What was the issue? – Marc Jan 17 '22 at 09:58
  • Actually no issue in code, in my project, server using different SDK to get handshake with it. When I tried connect, after 2-3 times failed then can connect successfully. – Thet Thet Jan 31 '22 at 04:08
  • Dear Marc, pls help my another post, please. Thanks a lot in advance. https://stackoverflow.com/q/75112060/2990432 – Thet Thet Jan 13 '23 at 16:44