0

proxy multiple proxies by net.createServer

here is my all code

var proxies='127.0.0.1:4444,127.0.0.1:4443,127.0.0.1:4442'.split(',');
var net = require('net');
function randgetir(){
    d=proxies[Math.floor(Math.random() * proxies.length)].split(':');
    return [d[0],d[1]];
}
var cre=net.createServer(function(from) {
    var prgs=randgetir();
    var to = net.createConnection({host: prgs[0],
        port: prgs[1]
    }).on('error', function(error) {
    });
    from.pipe(to);
    to.pipe(from);
}).on('error', function(error) {
});
cre.listen("56666");

when i run the code sometimes i get

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:162:27)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:98:12)
    at Socket.onerror (_stream_readable.js:720:7)
    at Socket.emit (events.js:197:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

error and my app just crash and exit but as you see i handle all the error events,

what may i be doing wrong?

Arya Same
  • 397
  • 1
  • 3
  • 12

1 Answers1

0

The problem is that there is no error handler attached to the incoming message stream. You could attach a handler like this:

from.on('error', function(e){ ...}))
.pipe(to);

Maybe check out this post on stackoverflow for more information regarding error handling with streams/pipes: Error handling with node.js streams

eol
  • 23,236
  • 5
  • 46
  • 64