mscdex / socksv5

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

Reading the destination address' response #10

Closed ghost closed 8 years ago

ghost commented 8 years ago

Forgive me if I'm missing anything particularly obvious, but I'm not sure how I'd go about intercepting data returned by the destination address.

I tried socket.read(), but that seems to indefinitely return null. Any tips?

mscdex commented 8 years ago

The stream you get from accept(true) is a stream to the socks client that is requesting a connection. If you want to get data from the destination, just use accept(true) and manually create the socket to the destination (and pipe the two together if you want). For example:

var net = require('net');
socks.createServer(function(info, accept, deny) {
  var clientSocket = accept(true);
  var destSocket = new net.Socket();
  destSocket.on('connect', function(d) {
    console.log('destSocket connected');
    // Also let data flow between client and its requested destination
    destSocket.pipe(clientSocket).pipe(destSocket);
  });
  destSocket.on('data', function(d) {
    console.log('destSocket data: %j', d);
  });
  destSocket.connect(info.dstPort, info.dstAddr);
});

You may also not want to accept(true) the request until destSocket has connected first (e.g. deny()ing in destSocket.on('error', ...)

ghost commented 8 years ago

Thank you! It took me a little bit, but I'm fairly comfortable with the library now. :)