NodeJS snippet: create socks5 with ssh


NodeJS snippet: create socks5 with ssh

var socks = require('socksv5');
var Client = require('ssh2').Client;

var ssh_config = {
  host: '103.92.11.200',
  port: 22,
  username: 'root',
  password: 'xxxxxx'
};

socks.createServer(function(info, accept, deny) {

  var conn = new Client();
  conn.on('ready', function() {
    conn.forwardOut(info.srcAddr,
      info.srcPort,
      info.dstAddr,
      info.dstPort,
      function(err, stream) {
        if (err) {
          conn.end();
          return deny();
        }

        var clientSocket;
        if (clientSocket = accept(true)) {
          stream.pipe(clientSocket).pipe(stream).on('close', function() {
            conn.end();
          });
        } else
          conn.end();
      });
  }).on('error', function(err) {
    deny();
  }).connect(ssh_config);
}).listen(1080, '0.0.0.0', function() {
  console.log('SOCKSv5 proxy server started on port 1080');
}).useAuth(socks.auth.UserPassword(function(user, password, cb) {
  cb(user === 'user' && password === 'pass');
}));

Test example:

D:\cygwin64\bin>curl -i --socks5 127.0.0.1:1080 --proxy-user user:pass google.co
m --socks5-hostname localhost
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 30 Jul 2020 16:51:21 GMT
Expires: Sat, 29 Aug 2020 16:51:21 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Leave a Reply