简体   繁体   English

node.js-动态SSH隧道

[英]node.js - dynamic SSH tunnels

I'm trying to find a way to create a dynamic SSH tunnel to send HTTP requests through (node.js -> SSH server -> internet) however it seems that none of the nodeJS SSH libraries allow you to create dynamic tunnels. 我正在尝试找到一种创建动态SSH隧道的方法,以通过(node.js-> SSH服务器-> Internet)发送HTTP请求,但是似乎没有nodeJS SSH库允许您创建动态隧道。
https://www.npmjs.com/package/tunnel-ssh only seems to allow you to create non-dynamic tunnels. https://www.npmjs.com/package/tunnel-ssh似乎只允许您创建非动态隧道。 How can I solve this problem? 我怎么解决这个问题? Thanks. 谢谢。

Assuming you're referring to SOCKS proxying, the ssh2 module readme contains such an example that uses the socksv5 module: 假设您指的是SOCKS代理,则ssh2模块自述文件包含一个使用socksv5模块的示例:

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

var ssh_config = {
  host: '192.168.100.1',
  port: 22,
  username: 'nodejs',
  password: 'rules'
};

socks.createServer(function(info, accept, deny) {
  // NOTE: you could just use one ssh2 client connection for all forwards, but
  // you could run into server-imposed limits if you have too many forwards open
  // at any given time
  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, 'localhost', function() {
  console.log('SOCKSv5 proxy server started on port 1080');
}).useAuth(socks.auth.None());

// test with cURL:
//   curl -i --socks5 localhost:1080 google.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM