反向SSH隧道

chr*_*ris 8 ssh nginx tunneling iptables ssh-tunnel

我正在尝试将 Web 流量从远程服务器转发到我的本地计算机,以测试一些 API 集成(tropo、paypal 等)。基本上,我正在尝试设置类似于 tunnlr.com 提供的内容。

我已经使用命令启动了 ssh 隧道

$ssh –nNT –R :7777:localhost:5000 user@server

然后我可以看到服务器现在正在侦听端口 7777

user@server:$netstat -ant | grep 7777

tcp        0      0 127.0.0.1:7777          0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:7777                :::*                    LISTEN  


$user@server:curl localhost:7777
Hello from local machine
Run Code Online (Sandbox Code Playgroud)

所以这很好用。curl 请求实际上是从本地机器提供的。

现在,我如何启用 server.com:8888 以通过该隧道路由?

我试过像这样使用 nginx:

upstream tunnel {
    server 0.0.0.0:7777;
}
server {
  listen 8888;
  server_name server.com;
  location / {
    access_log /var/log/nginx/tunnel-access.log;
    error_log /var/log/nginx/tunnel-error.log;
    proxy_pass http://tunnel;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
  }
}
Run Code Online (Sandbox Code Playgroud)

从nginx错误日志我看到:

[error] 11389#0: *1 connect() failed (111: Connection refused)

我一直在考虑尝试使用iptables,但没有取得任何进展。 iptables似乎比仅为隧道运行 nginx 更优雅的解决方案。任何帮助是极大的赞赏。谢谢!

编辑(添加服务器信息)

服务器详细信息:Ubuntu 10.10 Maverick(标准安装)

编辑(nginx 选项)

正如@Marcel G 建议的那样,nginx 选项通过将 0.0.0.0:7777 更改为 127.0.0.1:7777 起作用。尽管如此,仍在寻找非 nginx 解决方案。

编辑(更新最终解决方案)

正如@sciurus 指出的那样,请确保GatewayPorts yes在您的 sshd_config 文件中。我最初在配置文件中有这一行,但我需要发出一个/etc/init.d/ssh reload而不是restart(至少在 ubuntu 上看起来是这样)。

在本地机器上使用的最终命令: ssh -nNT -R '*:8888:localhost:5000' user@server

然后服务器应该表明它正在侦听 *:8888 lsof -i tcp:888

user@server:~$ sudo lsof -i tcp:8888
COMMAND   PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
sshd    14711 user    8u  IPv4 1791013      0t0  TCP *:8888 (LISTEN)
sshd    14711 user    9u  IPv6 1791014      0t0  TCP *:8888 (LISTEN)
Run Code Online (Sandbox Code Playgroud)

sci*_*rus 9

没有必要使用nginx。

在您的 ssh 守护程序配置(它应该是/etc/ssh/sshd_config)中,将GatewayPorts设置为clientspecified并重新加载它。这是其他系统能够连接到您的隧道所必需的。没有它,只有在您的服务器上运行的程序才能使用它。

现在您需要做的就是修改您的 ssh 命令以侦听端口 8888 而不是端口 7777。这看起来像

ssh -nNT -R '*:8888:localhost:5000' user@server
Run Code Online (Sandbox Code Playgroud)

星号告诉 sshd 在所有接口上侦听端口 8888,而不仅仅是环回接口。如果您没有更改GatewayPorts ,这将失败。