我正在使用Google Cloud Run的Go中的智能链接服务原型。我需要知道哪个域名/主机名被用来调用我的云运行服务(例如,默认的。run.app,或许多自定义映射域之一)。
我创建了一个检查端点(/__hitcheck
),它以JSON形式返回当前HTTP请求的引用者、标头和URL。
我已经检查了字段,我希望显示当前调用的域,徒劳的:
r.URL.String() = "/__hitcheck"
(我的测试端点)r.URL.Host = ""
请求头中也没有任何内容。
那么,我怎么知道是否有人直接从云运行服务URL或许多自定义域之一调用/__hitcheck
?
编辑后的JSON有效负载:
{
"r.Header": {
"Accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"Accept-Language": [
"en-US,en;q=0.9"
],
"Cookie": [
"...some stuff..."
],
"Early-Data": [
"1"
],
"Forwarded": [
"for="MY IP";proto=https"
],
"Referer": [
"https://example.com/some-blog-post/"
],
"Sec-Ch-Ua": [
""Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24""
],
"Sec-Ch-Ua-Mobile": [
"?0"
],
"Sec-Ch-Ua-Platform": [
""macOS""
],
"Sec-Fetch-Dest": [
"document"
],
"Sec-Fetch-Mode": [
"navigate"
],
"Sec-Fetch-Site": [
"cross-site"
],
"Sec-Fetch-User": [
"?1"
],
"Traceparent": [
"some request ID"
],
"Upgrade-Insecure-Requests": [
"1"
],
"User-Agent": [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
],
"X-Cloud-Trace-Context": [
"some request ID"
],
"X-Forwarded-For": [
"MY IP"
],
"X-Forwarded-Proto": [
"https"
]
},
"r.Referer()": "https://example.com/some-blog-post/",
"r.RequestURI": "/__hitcheck",
"r.URL": {
"Scheme": "",
"Opaque": "",
"User": null,
"Host": "",
"Path": "/__hitcheck",
"RawPath": "",
"OmitHost": false,
"ForceQuery": false,
"RawQuery": "",
"Fragment": "",
"RawFragment": ""
},
"r.URL.String()": "/__hitcheck"
}
HTTPHost
报头指定发送请求的服务器的主机和端口号。
我用Swift Cloud Run实例验证了这一点。点击两个链接并向下滚动到host
标题。
Cloud Run URL
云运行自定义域
Swift代码在GitHub上。类似的技术也适用于Cloud Run Go应用程序。
在Go中,主机似乎是在http.Request.Host
中指定的,而不是在标题或http.Request.URL.Host
中指定的。