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

Time:11-29

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related