ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

how to handle Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:218:20) at TCP.callbackTrampoline (node:internal/async_hooks:130:17) in sock5

Open ghost opened this issue 1 year ago • 3 comments

how to handle Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:218:20) at TCP.callbackTrampoline (node:internal/async_hooks:130:17) in sock5

const sshConfig = {
  host: commander.program.opts().ip,
  port: 22,
  username: 'ubuntu',
  privateKey:await fs.readFile(path.join(os.homedir(), '.ssh', 'id_ed25519'), 'utf8')
};

socks.createServer((info, accept, deny) => {
  const conn = new ssh2.Client();
  conn.on('ready', () => {
    conn.forwardOut(info.srcAddr,
                    info.srcPort,
                    info.dstAddr,
                    info.dstPort,
                    (err, stream) => {
      if (err) {
	try{
        conn.end();
	}catch(e) {console.log(e)}
        return deny();
      }

      const clientSocket = accept(true);
      if (clientSocket) {
        stream.pipe(clientSocket).pipe(stream).on('close', () => {
          try{conn.end();}catch(e){console.log(e)}
        });
      } else {
	try{
        conn.end();
	}catch(e){console.log(e)}
      }
    });
  }).on('error', (err) => {
    deny();
  }).connect(sshConfig);
}).listen(9000, 'localhost', () => {
  console.log('SOCKSv5 proxy server started on port  9000');
}).useAuth(socks.auth.None());

I always get Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:218:20) at TCP.callbackTrampoline (node:internal/async_hooks:130:17) but the information is not very useful. I still have no idea where to throw the ECONNRESET error

I see other issues, they say always add error handler to connection. I already add the .on('error', (err) => { deny(); }) But the ECONNRESET still exist. I also try to console.log all the err in the above code. But None of them provide any useful information. I want to ask how to suppress ECONNRESET error?

ghost avatar Oct 10 '24 03:10 ghost

You're missing 'error' event handlers on stream and clientSocket.

mscdex avatar Oct 10 '24 11:10 mscdex

const server = socks.createServer(async (info, accept, deny) =>
{
    const conn = new ssh2.Client()
    conn.on('ready', () =>
    {
        conn.forwardOut(info.srcAddr, info.srcPort, info.dstAddr, info.dstPort, (err, stream) =>
	    {
            if (err)
	        {
                conn.end()
                return deny()
            }
	        const clientSocket = accept(true).on('error', _ => conn.end())
            if (clientSocket) stream.pipe(clientSocket).pipe(stream).on('error', _ => conn.end()).on('close', () => conn.end())
            else conn.end()
	    })
    }).on('error', _ => deny()).connect({host:commander.program.opts().ip, username:'ubuntu', privateKey:await fs.readFile(path.join(os.homedir(), '.ssh', 'id_ed25519'), 'utf8'), keepaliveInterval:60 * 1000})
}).listen().useAuth(socks.auth.None())

this is the code I use, I am not sure how to add the error handler to stream and clientSocket.

it is .on('error', _ => conn.end()) or .on('error', _ => {conn.end(); deny()}), I am not familiar with the internal impletemetation of ssh. Please help.

ghost avatar Oct 11 '24 06:10 ghost

The 'error' event handlers you've added for stream and clientSocket there seem fine to me.

mscdex avatar Oct 11 '24 14:10 mscdex