【问题标题】:How to get HAProxy to route TCP based on SNI (using openssl s_client to test)?如何让HAProxy基于SNI路由TCP(使用openssl s_client进行测试)?
【发布时间】:2019-12-13 14:49:29
【问题描述】:

我想使用 HAProxy 来终止 TLS 加密的 TCP 连接,并根据用于启动 TLS 连接的服务器名称指示将未加密的 TCP 流量传递到各种后端。

我有 3 个服务在后端服务器上运行,每个服务都在不同的端口(5001、5002、5003)上。 HAProxy 绑定到端口 5000。如果没有匹配,我想按名称将连接路由到前 2 个服务或第三个。我正在使用openssl s_client 启动与 HAProxy 的连接。但是,在日志中我可以看到连接只路由到默认服务器,即 SNI 似乎被忽略了。

DNS:

A record     demo.sni.example.com     1.2.3.4
CNAME        1.sni.example.com        pointing to demo.sni.example.com   
CNAME        2.sni.example.com        pointing to demo.sni.example.com   

即我希望发生以下路由:

SNI = 1.sni.example.com:5000 -> 1.2.3.4:5001
SNI = 2.sni.example.com:5000 -> 1.2.3.4:5002
anything else on port 5000 -> 1.2.3.4:5003

haproxy.cfg:

global
  log stdout  format raw  local0 info

defaults
  timeout client 30s
  timeout server 30s
  timeout connect 5s
  option tcplog

frontend tcp-proxy
  bind :5000 ssl crt combined-cert-key.pem
  mode tcp
  log global
  tcp-request inspect-delay 5s
  tcp-request content accept if { req_ssl_hello_type 1 }

  use_backend bk_sni_1 if { req.ssl_sni -i 1.sni.example.com }
  use_backend bk_sni_2 if { req.ssl_sni -i 2.sni.example.com }
  default_backend bk_default

backend bk_sni_1
  mode tcp
  log global
  balance roundrobin
  server server1 1.2.3.4:5001 check

backend bk_sni_2
  mode tcp
  log global
  balance roundrobin
  server server1 1.2.3.4:5002 check

backend bk_default
  mode tcp
  log global
  balance roundrobin
  server server1 1.2.3.4:5003 check

combined-cert-key.pem 是一个自签名证书文件加密钥,其中 CN 是服务器的 IP (1.2.3.4),并且有所有 DNS 值和 IP 的 SAN。

使用openssl s_client发起的连接:

我已尝试通过 DNS(A 和 CNAME 记录,以及 IP)进行连接:

echo test | openssl s_client -connect demo.sni.example.com:5000 -servername 1.sni.example.com
echo test | openssl s_client -connect 1.sni.example.com:5000 -servername 1.sni.example.com
echo test | openssl s_client -connect 1.2.3.4:5000 -servername 1.sni.example.com

但是,所有连接都路由到默认后端bk_default

是什么导致 HAProxy 无法识别 SNI 服务器名? (我使用的是最新的 HAProxy docker 镜像:https://hub.docker.com/_/haproxy

【问题讨论】:

    标签: openssl haproxy


    【解决方案1】:

    答案是使用ssl_fc-sni,而不是req.ssl_sni。前者用于 SSL 终止的会话,而后者用于 TCP 直接通过的会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多