如何知道使用Cloud Run进行请求时使用的自定义域名?

huangapple go评论82阅读模式
英文:

How to know which custom domain a request was made to with Cloud Run?

问题

我正在使用Google Cloud Run在Go中原型化一个智能链接服务。我需要知道调用我的Cloud Run服务时使用了哪个域名/主机名(例如,默认的.run.app,或多个自定义映射的域名)。

我创建了一个检查端点(/__hitcheck),它以JSON格式返回当前HTTP请求的引用者、头部和URL。

我已经检查了我期望显示当前调用域的字段,但没有成功:

  • r.URL.String() = "/__hitcheck"(我的测试端点)
  • r.URL.Host = ""

请求头中也没有任何内容。

那么,我如何知道有人是直接从Cloud Run服务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": [
			"...一些内容..."
		],
		"Early-Data": [
			"1"
		],
		"Forwarded": [
			"for=\"我的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": [
			"一些请求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": [
			"一些请求ID"
		],
		"X-Forwarded-For": [
			"我的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"
}
英文:

I am prototyping a smart link service in Go using Google Cloud Run. I need to know which domain/hostname has been used to call my Cloud Run service (e.g., the default .run.app, or one of many custom mapped domains).

I created a check endpoint (/__hitcheck) that returns the current HTTP request's referer, headers, and URL, as JSON.

I've checked fields I'd expect to show the current domain called, in vain:

  • r.URL.String() = "/__hitcheck" (my test endpoint)
  • r.URL.Host = ""

And nothing's in the request headers either.

So, how can I know if somebody called /__hitcheck from the Cloud Run service URL directly or one of many custom domains?

The redacted JSON payload:

{
"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"
}

答案1

得分: 2

HTTP的Host头部字段指定了请求发送到的服务器的主机和端口号。

我通过一个Swift Cloud Run实例进行了验证。点击下面的链接并向下滚动到host头部字段。

Cloud Run URL

Cloud Run自定义域名

Swift代码在GitHub上。类似的技术也适用于Cloud Run Go应用程序。

英文:

The HTTP Host header specifies the host and port number of the server to which the request is being sent.

I verified that with a Swift Cloud Run instance. Click both links and scroll down to the host header.

Cloud Run URL

Cloud Run Custom Domain

The Swift code is on GitHub. Similar techniques will work for a Cloud Run Go application.

答案2

得分: 0

在Go语言中,似乎是通过http.Request.Host来指定主机,而不是通过头部或http.Request.URL.Host来指定。

英文:

In Go, it seems like the host is specified in http.Request.Host, rather than the headers or http.Request.URL.Host.

huangapple
  • 本文由 发表于 2022年11月28日 21:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74601451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定