Search code examples
node.jsconfigurationtor

Issue with HTTP request using node through TOR


I am relatively new to both node and TOR. I'm trying to send a request using a node application through TOR. Ive set up a basic app using socks5-HTTP-client. Also, TOR is installed (sudo port install tor) and I ran the server using tor..

Executing the app, node gives me the following error

Error: SOCKS connection failed. General SOCKS server failure.
    at Socket.<anonymous> (/Users/MyName/node_modules/socks5-client/lib/Socket.js:199:12)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)

The TOR server reports the following to me:

[warn] Rejecting SOCKS request for anonymous connection to private address [scrubbed].

The topic I found here does not seem to apply to this case. Also, this earlier article does not answer my question. Neither can I find any difference between the example given by the developer of the socks5 module and my case.

I am unsure whether this is an issue with my configuration in node or my TOR configuration.

Below is the JS code

var shttp = require('socks5-http-client');
var config = 
        { url: 'http://whatismyipaddress.com/', // I'd like to see a different IP on every request, that should be the end result, correct?
      socksHost: '127.0.0.1',
        socksPort: 9050
    }
shttp.get(config, function(res) {
    res.setEncoding('utf8');
    res.on('readable', function() {
        console.log(res.read());
    });
});

Also, in my torrc.sample configuration file, I made sure the following is set up:

SOCKSPort 127.0.0.1:9050 

Also, my SOCKSPolicy is set up as follows:

SOCKSPolicy accept 192.168.0.0/16
SOCKSPolicy accept6 FC00::/7
SOCKSPolicy reject *

At the time of trying, my internal IP address was 192.168.0.11, seems fine to me.

Any help would be appreciated!


Solution

  • You are getting that error because the URL isn't getting passed to the request properly so it is trying to issue a SOCKS request to localhost instead of the site you want to visit.

    URLs are parsed using url.parse.

    Try changing your code to:

    var shttp = require('socks5-http-client');
    var url = require('url');
    var config = url.parse('http://whatismyipaddress.com/');
    config.socksHost = '127.0.0.1';
    config.socksPort = 9050;
    
    shttp.get(config, function(res) {
        res.setEncoding('utf8');
        res.on('readable', function() {
            console.log(res.read());
        });
    });
    

    Also, it appears whatismyipaddress.com might be blocking Tor as I don't get a response back from them. Try ipchicken.com or some other site. You may need to set a User-Agent header to get the former site to reply, but this code change does work.