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

标签 google-app-engine go http-headers google-cloud-platform google-app-engine-go

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

在这项服务中,我出于各种目的检查请求 header 。

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 (如何处理请求 | 适用于 Go 的 App Engine 标准环境 | Google Cloud Platform)。

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

是否需要任何配置才能在 Go 的 App Engine 标准环境中填充这些 header (和 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

希望对您有所帮助。

关于google-app-engine - 如何在 Google App Engine Standard Env for Go 中获取 request.RemoteAddr 和 X-AppEngine-Country、Region 等的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48345409/

相关文章:

go - 如何在golang中体现struct recursive

postgresql - Google Go 数据存储接口(interface)

go - 如何在处理结果时正确关闭 Goroutines 中的共享 channel

python - 如何更新 NDB 模型的架构

python appengine unicodeencodeerror on search api snippeted results

google-app-engine - 谷歌云 sdks 不启动 Docker 图像 Mac OS 的预览

python - 应用程序引擎超时错误 : Serving a third-party API with an image stored on App Engine

javascript - 通过 jQuery 检测浏览器缓存

python - 恢复文件的 HTTP 下载和 HTTP 压缩

Spring - 在浏览器中显示 PDF 文件而不是下载