js_gandalf
js_gandalf

Reputation: 891

NodeJS writing a socket through a proxy

I am trying to do a TCP client connection through a proxy that I have:

IP: 12.32.492.94

port: 8081

username: acct

pass: admin123

Not exactly sure how to do it. Any ideas?

var client = new net.Socket();
client.connect(port, ip, function() {

});

client.on('connect', function() {
  console.log('connected');
});

client.on('data', function(data) {
  console.log(data);
});

Upvotes: 0

Views: 2761

Answers (1)

js_gandalf
js_gandalf

Reputation: 891

Needed to use the socks npm module to do what I was trying to do. Needed a socks proxy.

"socks": "1.1.8"

var Socks = require('socks');

var options = {
    proxy: {
        ipaddress: "202.101.228.108", // Random public proxy 
        port: 1080,
        type: 5 // type is REQUIRED. Valid types: [4, 5]  (note 4 also works for 4a) 
    },
    target: {
        host: "google.com", // can be an ip address or domain (4a and 5 only) 
        port: 80
    },
    command: 'connect'  // This defaults to connect, so it's optional if you're not using BIND or Associate. 
};

Socks.createConnection(options, function(err, socket, info) {
    if (err)
        console.log(err);
    else {
        // Connection has been established, we can start sending data now: 
        socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
        socket.on('data', function(data) {
            console.log(data.length);
            console.log(data);
        });

        // PLEASE NOTE: sockets need to be resumed before any data will come in or out as they are paused right before this callback is fired. 
        socket.resume();

        // 569 
        // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65... 
    }
});

Upvotes: 1

Related Questions