For the most part, migrating from v1 takes minimal effort as v2 still supports factory creation of proxy connections with callback support.
For informational purposes, here is the original getting started example from v1 converted to work with v2.
var Socks = require('socks');
var options = {
proxy: {
ipaddress: "202.101.228.108",
port: 1080,
type: 5
},
target: {
host: "google.com",
port: 80
},
command: 'connect'
};
Socks.createConnection(options, function(err, socket, info) {
if (err)
console.log(err);
else {
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...
}
});
const SocksClient = require('socks').SocksClient;
let options = {
proxy: {
ipaddress: "202.101.228.108",
port: 1080,
type: 5
},
destination: {
host: "google.com",
port: 80
},
command: 'connect'
};
SocksClient.createConnection(options, function(err, result) {
if (err)
console.log(err);
else {
result.socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
result.socket.on('data', function(data) {
console.log(data.length);
console.log(data);
});
// 569
// <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65...
}
});