Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #1426

Open
ghost opened this issue Oct 10, 2024 · 3 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 10, 2024

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?

@mscdex
Copy link
Owner

mscdex commented Oct 10, 2024

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

@ghost
Copy link
Author

ghost commented Oct 11, 2024

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.

@mscdex
Copy link
Owner

mscdex commented Oct 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant