mscdex / socksv5

SOCKS protocol version 5 server and client implementations for node.js
MIT License
400 stars 121 forks source link

All successful client sockets are not closed #48

Open louislam opened 3 years ago

louislam commented 3 years ago

As topic.

If you use this server as a web proxy, because websites nowadays make a lot of requests, it also creates a lot of sockets on socksv5 server. As all successful sockets are not closed, it will eats up all system resources and emits EMFILE error shortly. As a result, the server is no longer working anymore.

After some investigations, in server.js, I found out that after the dstSock (the socket between proxy server and destination server) is closed, it do not close the client socket together.

To fix this, just need to add close event and destroy the client socket.

server.js

function proxySocket(socket, req) {

    ....

    dstSock
            .on('error', onError)
            //////////// Missing Part ///////////
            .on('close', () => {
              socket.end()
              socket.destroy()
            })
           //////////// Missing Part End ////////////////////
           .on('connect', function() {
             connected = true;
             if (socket.writable) {

    ....

However, it seems that this project is abandoned. My recommendation is you should not use this library.

israelsgalaxy commented 1 year ago

You're a life saver.

kzar79 commented 1 year ago

In theory piped sockets are closed automatically when the source stream emits 'end': https://nodejs.org/api/stream.html#readablepipedestination-options

Anyway, if node doesn't do what it says it does, better do it manually.