如何在 envoy 前置代理中仅重定向一个域名而非所有 on 401

现状: 我们使用 envoyproxy 过滤超时(因此未授权)的流量并重定向到所有.*\\.domain\\.de(:3000)? 上的 login.domain.tld,因此在 envoy.yaml 中,

inline_code: |
  function envoy_on_response(response_handle)
    if response_handle:headers():get(":status") == "401" then
      response_handle:headers():replace(":status", "301")
      response_handle:headers():replace("location", "https://login.domain.tld")
    end
  end

示例代码: https://github.com/envoyproxy/envoy/blob/main/examples/lua/envoy.yaml#L30-L39,YAML-path: static_resources.listeners:.address.filter_chains.filters.'envoy.http_connection_manager'.config.http_filters.'envoy.lua'config.inline_code

问题: 现在,我们想将重定向从所有子域名改为一个。 做这件事只需要将上面的代码加入一个条件,我们就可以做到。

为什么要这么做? 我们有很多子域,例如 api-{000..999}.domain.tld,它们都是 REST API,如果缺少验证就不应重定向。

尝试的解决方案: 我们假设在阅读文档时,https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter#requestedservername 可以解决此问题,因此添加了第二个条件:

inline_code: |
  function envoy_on_request(request_handle)
    local SNI = request_handle:streamInfo():requestedServerName()
  end
  function envoy_on_response(response_handle)
    local SNI_str = tostring(SNI)
    local MATCH_str = "app.domain.tld"
    if response_handle:headers():get(":status") == "401" and string.find(SNI_str,MATCH_str) then
      response_handle:headers():replace(":status", "301")
      response_handle:headers():replace("location", "https://login.domain.tld")
    end
  end

遗憾的是,上面的代码不起作用,对象 request_handle:streamInfo():requestedServerName() 似乎为空或不存在。

问题:

  • 我们可以使用哪个对象获取请求的 SNI 并将其转换为字符串类型?
  • 如果没有这个选项,我们如何区分重定向的域名?更多过滤?

版本: Docker 镜像 envoyproxy/envoy:v1.16.2

原文链接 https://stackoverflow.com/questions/68995459

点赞