The*_*nix 5 ssh haproxy stunnel
我正在尝试使用 haproxy 通过 https 连接设置 ssh。
我目前正在寻找一种让 SSHD 从 haproxy 获取源 ip 的方法,类似于读取X-Forwarded-For
或X-Real-IP
标头。
客户端配置;
~$ cat ~/.stunnel/stunnel.conf
pid=
client=yes
foreground=yes
[ssh]
accept=4444
connect=ssh.example.com:443
Run Code Online (Sandbox Code Playgroud)
客户端输出;
~$ 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
Run Code Online (Sandbox Code Playgroud)
服务器配置;
~$ 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
Run Code Online (Sandbox Code Playgroud)
服务器输出;
~$ 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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我希望 ssh 知道源 IP。
在中mode tcp
,你需要tproxy将原始客户端IP传递给后面的服务器haproxy
。
链接的文章有很多技术背景,其中大部分不再是问题 - 最新版本的 Linux 并且haproxy
很可能支持开箱即用的 tproxy。
TL;DR 在后端部分,尝试一下
source 0.0.0.0 usesrc clientip
Run Code Online (Sandbox Code Playgroud)