如何在 Google App Engine Standard Env for Go 中获取 request.RemoteAddr 和 X-AppEngine-Country、区域等的输出?

How do I get output from request.RemoteAddr and X-AppEngine-Country, Region, etc. in Google App Engine Standard Env for Go?

我在 Google App Engine 标准环境中有一个服务 运行 用 Go 编写,配置为在部署时使用最新的运行时(api_version:go1 - 目前是去 1.8).

在此服务中,我出于各种目的检查请求 headers。

func extractHeaders(res http.ResponseWriter, req *http.Request) {
    ctx := appengine.NewContext(req)
    clientIPAddress, _, _ := net.SplitHostPort(req.RemoteAddr) // Output is blank
    country := req.Header.Get("X-AppEngine-Country") // Output: US
    region := req.Header.Get("X-AppEngine-Region") // Output: ?
    city := req.Header.Get("X-AppEngine-City") // Output: ?
    cityLatLong := req.Header.Get("X-AppEngine-CityLatLong") // Output 0.000000,0.000000
    ...
}

从我读取 RemoteAddr 字段开始的行的内联注释中可以看出,根据发现的 AppEngine 标准文档,我没有得到预期的输出 here(如何处理请求 | App Engine 标准Go 的环境 | Google 云平台)。

虽然文档指出 X-AppEngine-* 可能并不总是能够根据客户端请求的 IP 地址填写,但我从来没有看到他们填写的数据不是我列出来了。

是否需要任何配置才能在 Go 的 App Engine 标准环境中填充这些 headers(和 RemoteAddr 字段)?我只是误解了文档吗?

需要注意的一件事是 req.RemoteAddr 将以您应用程序链中最后一个代理的 IP 地址结束。在 App Engine 上,它可能最终成为 127.0.0.1。 net.SplitHostPort 可能是错误的,因为 IP 实际上可能没有端口号。

Region、City 和 CityLatLong 数据将来自您的 IP,如果该信息可用,则可能不会,因为它不是严格要求的。您可以尝试从 X-Forwarded-For header 中提取 IP 地址。这通常会为您提供完整的代理列表和原始 IP 地址(IPv4 或 IPv6,请参阅下面的示例结果)。

如果您要使用: 包裹你好

import (
    "fmt"
    "net"
    "net/http"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    clientIPAddress, _, err := net.SplitHostPort(r.RemoteAddr)
    if err != nil {
        clientIPAddress = err.Error()
    }
    rAddr := r.RemoteAddr
    forward := r.Header.Get("x-forwarded-for")
    country := r.Header.Get("X-AppEngineCountry")
    region := r.Header.Get("X-AppEngine-Region")
    city := r.Header.Get("X-AppEngine-City")
    cityLatLong := r.Header.Get("X-AppEngine-CityLatLong")
    fullHeader := r.Header
    fmt.Fprintf(w, "Hello, world!\n %v\n %v\n %v\n %v\n %v\n %v\n %v\n %v",
        clientIPAddress,
        rAddr,
        forward,
        country,
        region,
        city,
        cityLatLong,
        fullHeader
    )
}

您应该会看到类似的内容(只要 Region City、CityLatLong 可用,否则它们将是空白的):

Hello, world!
 missing port in address 127.0.0.1
 127.0.0.1
 2601:1c2:600:17b0::::, 2607:f8b0:400a:::2014
 ZZ
 or
 portland
 45.523452,-122.676207
 map[Accept-Encoding:[identity] X-Appengine-Default-Version-Hostname:
[127.0.0.1:8080] X-Appengine-User-Organization:[] X-Appengine-Citylatlong:
[45.523452,-122.676207] X-Appengine-Request-Log-Id:
[48f03baada92bbb3872a8b3a5baa3f8b0dfff413f64fbcfa] X-
Appengine-City:[portland] X-Forwarded-Proto:[https] X-Appengine-Server-Name:
[127.0.0.1] Via:[1.1 google] X-Appengine-Server-Software:[Development/2.0] 
X-Appengine-User-Id:[] X-Forwarded-For:
[2601:1c2:600:17b0:::: 2607:f8b0:400a:::2014] X-Goog-
Cloud-Shell-Target-Host:[127.0.0.1] X-Appengine-Country:[ZZ] Accept:
etc
etc

希望对您有所帮助。