在haproxy之后的SSHD真正的IP

我试图通过https连接使用haproxy设置SSH。

我目前正在寻找一种SSHD从haproxy获取源IP的方法,类似于阅读X-Forwarded-ForX-Real-IP标头。

客户端configuration;

 ~$ cat ~/.stunnel/stunnel.conf pid= client=yes foreground=yes [ssh] accept=4444 connect=ssh.example.com:443 

客户输出;

 ~$ ssh -v -p 4444 user@localhost OpenSSH_6.6.1, OpenSSL 1.0.1i 6 Aug 2014 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Connecting to localhost [::1] port 4444. debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 pat OpenSSH_6.6.1* compat 0x04000000 ..... debug1: SSH2_MSG_KEXINIT sent Bad packet length 1349676916. Disconnecting: Packet corrupt 

服务器configuration;

 ~$ cat /etc/haproxy/haproxy.cfg frontend public mode tcp bind :80 redirect scheme https code 301 if !{ ssl_fc } bind :443 ssl crt example.pem no-tls-tickets tcp-request inspect-delay 5s tcp-request content accept if HTTP # .... use_backend ssh if { ssl_fc_sni ssh.example.com } backend ssh mode tcp server ssh 127.0.0.1:22 send-proxy timeout server 2h 

服务器输出

 ~$ tail -f /var/log/haproxy.log Aug 15 23:31:57 localhost haproxy[50379]: 115.000.000.000:51924 [15/Aug/2014:23:31:57.907] public~ ssh/ssh 2/0/8 60 SD 0/0/0/0/0 0/0 ~$ tail -f /var/log/auth.log Aug 15 23:31:57 localhost sshd[50757]: debug1: inetd sockets after dupping: 3, 3 Aug 15 23:31:57 localhost sshd[50757]: Connection from 127.0.0.1 port 36333 on 127.0.0.1 port 22 Aug 15 23:31:57 localhost sshd[50757]: Bad protocol version identification 'PROXY TCP4 115.000.000.000 192.168.000.000 51924 443' from 127.0.0.1 port 36333 

haproxy.cfg中的send-proxy行导致Bad protocol version identification

我可以连接时删除send-proxy但是这从127.0.0.1不断连接到/etc/hosts.deny

 Aug 15 23:55:22 localhost sshd[55997]: debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock 8 Aug 15 23:55:22 localhost sshd[55997]: debug1: inetd sockets after dupping: 3, 3 Aug 15 23:55:22 localhost sshd[55997]: debug1: Connection refused by tcp wrapper Aug 15 23:55:22 localhost sshd[55997]: refused connect from localhost (127.0.0.1) 

我宁愿ssh知道源IP。

mode tcp ,您可以将haproxy将原始客户端IP传递给haproxy后面的服务器。

链接的文章有很多的技术背景,其中大部分已经不是问题了 – 最近版本的Linux和haproxy很可能会支持tproxy。

TL; DR在后端部分,试试

 source 0.0.0.0 usesrc clientip 

这恐怕不可能。 与HTTP不同,SSH协议无法让代理告诉您原始源IP是什么。