AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 788856
Accepted
kvz
kvz
Asked: 2016-07-10 01:20:51 +0800 CST2016-07-10 01:20:51 +0800 CST 2016-07-10 01:20:51 +0800 CST

我可以使用 HAProxy 的新“捕获”功能将远程地址保存在 TCP 前端,并将其用作 HTTP 后端的“X-Forwarded-For”标头吗?

  • 772

使用 HAProxy 1.6 和一个聪明的 hack,我现在有一个 HAProxy tcp 模式前端,它检测浏览器是否支持 SNI,并基于此路由到强加密 SSL 终止后端,或者更弱的后端。这确保了 SSL 实验室的 A+ 评级,同时仍然允许除 IE6 之外的所有浏览器使用 SSL。

这是我的配置。它有一些模板变量应该是不言自明的,但与我的问题无关:

frontend https_incoming
 bind 0.0.0.0:443
 mode tcp
 option tcplog
 tcp-request inspect-delay 5s
 tcp-request content accept if { req.ssl_hello_type 1 }
 use_backend https_strong if { req.ssl_sni -m end .transloadit.com }
 default_backend https_weak

backend https_strong
 mode tcp
 option tcplog
 server https_strong 127.0.0.1:1665

frontend https_strong
 bind 127.0.0.1:1665 ssl crt ${DM_ROOT_DIR}/envs/ssl/haproxy-dh2048.pem no-sslv3 no-tls-tickets ciphers ${strongCiphers}
 mode http
 option httplog
 option httpclose
 option forwardfor if-none except 127.0.0.1
 http-response add-header Strict-Transport-Security max-age=31536000
 reqadd X-Forwarded-Proto:\ https
 reqadd FRONT_END_HTTPS:\ on
 use_backend http_incoming

backend https_weak
 mode tcp
 option tcplog
 server https_weak 127.0.0.1:1667

frontend https_weak
 bind 127.0.0.1:1667 ssl crt ${DM_ROOT_DIR}/envs/ssl/haproxy.pem no-sslv3 ciphers ${weakCiphers}
 mode http
 option httplog
 option httpclose
 option forwardfor if-none except 127.0.0.1
 http-response add-header Strict-Transport-Security max-age=31536000
 reqadd X-Forwarded-Proto:\ https
 reqadd FRONT_END_HTTPS:\ on
 use_backend http_incoming

问题:https_incoming前端知道客户端IP,但是由于它在mode tcp,它不能把这个信息保存在一个mode http X-Forwarded-Forheader中。option forwardfor在 TCP 模式下无效。

从另一个关于 serverfault 的问题 我已经发现我可以使用:

  • LVS
  • 代理协议

因此,据X-Forwarded-For我了解,在 LVS 的情况下,甚至不再需要标头:数据包被欺骗,因此源成为客户端 IP,而在代理的情况下:数据包被封装以携带客户端 IP。

这两个似乎都可以工作。然而,LVS 对我们来说似乎是一场可能有副作用的心脏手术,而 PROXY 的缺点是代理/应用程序上游/下游可能还不完全兼容。

我真的希望有更轻量级的东西,就在那时我发现了HAProxy 1.6 的新“捕获”功能,因为它提到:

您可以声明捕获槽,在其中存储数据并在会话期间随时使用它。

它继续显示以下示例:

defaults 
 mode http

frontend f_myapp
 bind :9001
 declare capture request len 32 # id=0 to store Host header
 declare capture request len 64 # id=1 to store User-Agent header
 http-request capture req.hdr(Host) id 0
 http-request capture req.hdr(User-Agent) id 1
 default_backend b_myapp

backend b_myapp
 http-response set-header Your-Host %[capture.req.hdr(0)]
 http-response set-header Your-User-Agent %[capture.req.hdr(1)]
 server s1 10.0.0.3:4444 check

在我看来,信息存储在前端,然后在后端使用,所以也许我可以在 TCP 模式下获取客户端 IP,保存它,然后在以后使用它,可能像这样:

http-response set-header X-Forwarded-For %[capture.req.hdr(0)]

我查看了捕获文档,似乎捕获更面向 http 模式标头,但后来我还看到一个邮件列表对话成功地演示了tcp-request capture.

我尝试了几件事,其中:

tcp-request capture req.hdr(RemoteAddr) id 0
# or
tcp-request content capture req.hdr(RemoteHost) id 0

但是正如您所看到的,我不知道语法应该是什么以及在哪个键下可以使用此信息,我也无法在(我认为)相关文档中找到它。

X-Forwarded-For问题:是否可以在 TCP 模式下捕获客户端 IP,然后再以 HTTP 模式将此信息写入标头?如果是这样,这将是什么语法?

ssl haproxy packet-capture
  • 2 2 个回答
  • 4484 Views

2 个回答

  • Voted
  1. Best Answer
    kvz
    2016-07-11T23:31:28+08:002016-07-11T23:31:28+08:00

    要回答我自己的问题,这似乎是不可能的,因为流量在这里“离开”HAProxy:

           TCP                             HTTP
    frontend->backend (->leaving->) frontend->backend
    

    因此上下文丢失并且无法保留捕获。相反,正如“PiBa-NL”昨天在 IRC 在 Freenode 的 #haproxy 上建议的那样:

    [5:29pm] PiBa-NL: kvz, use proxy-protocol between back and front
    [5:54pm] kvz: PiBa-NL: Thanks, does this mean my app also needs to understand 
             the proxy protocol, or will it be 'stripped' once it reaches the 
             backend. I don't think my node.js server could handle it without  
             significant changes to its stack
    [6:07pm] kvz: Or let me rephrase: could I enable the proxy protocol on the first 
             frontend, then 'unwrap' it in the second frontend, taking the client ip 
             and putting it into the http header - so that my app would not have to 
             be proxy protocol compatible, and it would just be means to carry the 
             client ip from first frontend to the second?
    [6:49pm] PiBa-NL: kvz, the last part you can still use the x-forwarded-for header
    [6:50pm] PiBa-NL: but between haproxy backend and frontend you would use the 
             proxyprotocol to make the second frontent 'know' the original client ip
    [6:50pm] PiBa-NL: server https_strong 127.0.0.1:1665 send-proxy
    [6:50pm] PiBa-NL: bind 127.0.0.1:1665 ssl crt .... accept-proxy
    [6:52pm] PiBa-NL: the second frontend can then still use the  
             'option forwardfor', and it should insert the wanted header
    [6:53pm] PiBa-NL: so basically 'yes' 
    

    这意味着 PROXY 协议仅用于将两个前端粘合在一起,封装 Cient IP,但第二个前端将其解包并通过 保存在X-Forwarded-For标头中option forwardfor,以便其后端可以向我的应用程序发送无代理协议的请求服务器,这意味着我不必担心上游/下游的兼容性问题。

    • 3
  2. BillThor
    2016-07-10T08:16:10+08:002016-07-10T08:16:10+08:00

    HAProxy 应该已经添加了 X-Forwarded-For 标头。如果其中任何一个是非标准的,您可能需要添加协议和/或端口。

    我通常用一个回显请求标头的页面来测试这种行为。这样可以很容易地查看哪些标题可用,以及它们的内容是什么。

    X-Forward-For 包含地址列表并不罕见。这表明请求已通过多个代理,或者有人在欺骗标头。最右边的地址将是由处理请求的最后一个代理 (ha-proxy) 添加的地址。

    某些 Web 服务器可以配置为从标头而不是连接记录 IP 地址。这对于您的访问日志以及您希望根据传入连接 IP 地址生成标头的情况很有用。

    在支持除 IE 6 之外的所有列出的浏览器的同时,无需使用两个不同的堆栈即可获得 A+ 评级。

    • 对于 Java 6,至少禁用 DHE-RSA-AES128-SHA 和 AES128-SHA。它可能是所有没有前向保密的密码。
    • 对于 WinXP/IE8 和 Java6,启用 DES-CBC3-SHA 和/或 EDH-DSS-DES-CBC3-SHA。这些应该是最不受欢迎的密码,因为它们不支持前向保密。

    WinXP/IE8 和 Java 6 不支持使用安全协议的前向保密,因此测试不会因失败而惩罚您。如果您强制执行服务器排序,所有其他浏览器都将使用前向保密。(如果你不这样做,Win Phones 会失败。)

    A+ 的评级要求设置 Strict-Transport-Security 的时间段至少为 180 天。

    • 0

相关问题

  • 如何使用 Tomcat 5.5 更新 SSL 证书

  • 为 IIS6 自行生成 SSL 证书?

  • plesk 上的域和子域 ssl 访问

  • 如何设置 SSL 邮件服务器?

  • 如何通过 SVN 命令行接受 SSL 证书?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve