【问题标题】:Get real IP from Request in server从服务器中的请求获取真实 IP
【发布时间】:2021-09-25 16:09:36
【问题描述】:

我正在尝试从 HTTP 请求中提取真实 IP。当我从部署在同一 VM 上的容器进行 CURL 调用时,我从 http 请求中获取容器的 IP,而不是通过 r.RemoteAddr 获取 VM 的 IP。

但是当我使用fmt.Printf("Request: %s", r) 打印请求时,我可以看到VM 的真实IP 在那里。 Print 语句的示例请求如下所示:

&{GET /containers/json HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[*/*] User-Agent:[curl/7.64.0]] {} %!s(func() (io.ReadCloser, error)=<nil>) %!s(int64=0) [] %!s(bool=false) 10.32.20.122:2375 map[] map[] %!s(*multipart.Form=<nil>) map[] 172.20.0.1:47102 /containers/json %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) %!s(*context.cancelCtx=&{0xc000120480 {0 0} <nil> map[] <nil>})}

在上面的输出中,10.32.20.122 是 VM IP,172.20.0.1 是 docker 网络网关 IP。我想提取VM IP。

【问题讨论】:

    标签: go docker-networking


    【解决方案1】:

    你最好在你的 Printf 中使用%+v

    fmt.Printf("Request: %+v", r)
    

    然后您可以看到该字段的名称。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多