大网数据广告活动
服务器运维问答

SSHD real-ip behind haproxy

· 52CE 我爱测

I'm trying to setup an ssh over https connection using haproxy. I'm currently looking for a way for SSHD to get the source ip from haproxy, similar to reading X-Forwarded-For or X-Real-IP headers. client config; ~$ cat ~/.stunnel…

问题原文

原帖链接 · Thermionix · CC BY-SA 3.0

I'm trying to setup an ssh over https connection using haproxy.

I'm currently looking for a way for SSHD to get the source ip from haproxy, similar to reading X-Forwarded-For or X-Real-IP headers.

client config;

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

client output;

~$ 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

server config;

~$ 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

server output;

~$ 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

The send-proxy line in haproxy.cfg is causing the Bad protocol version identification

I can connect when removing send-proxy however this connects from 127.0.0.1 which continuously gets appended to /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)

I would prefer that ssh knows the source ip.

已采纳解答

原帖链接 · Felix Frank · CC BY-SA 4.0

In mode tcp, you neet tproxy to pass the original client IP to the server behind haproxy.

The linked article has a lot of technical background, most of which is not a problem anymore - recent versions of Linux and haproxy will most likely support tproxy out of the box.

TL;DR in the backend section, try

source 0.0.0.0 usesrc clientip

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-08-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。