简体   繁体   English

Cloud Run 服务未获取客户端正确的 IP 地址

[英]Cloud Run Service not picking up client correct IP address

I have a service hosted on Google Cloud Run.我在 Google Cloud Run 上托管了一项服务。 The service uses socket.io whenever the service is up and running.只要服务启动并运行,该服务就会使用 socket.io。

When a socket client connects to the service I have the following function that gets the ip address of the connected client from the socket as shown below and then I am hitting this GeoPlugin Link with the retrieved IP当套接字客户端连接到服务时,我有以下 function 从套接字获取已连接客户端的 ip 地址,如下所示,然后我用检索到的 IP 点击此GeoPlugin 链接

    async getSocketIP(socket) {
        let { headers, address } = socket.handshake;
        let { origin } = headers;
        let ip = headers['x-forwarded-for'];
        let userAgent = headers['user-agent'];
        try {
            let locationPointUrl = `http://www.geoplugin.net/json.gp?ip=${ip}`;
            let { data: location } = await axios.get(locationPointUrl);
        } catch (e) {
            console.log(`Error get client online IP on Socket IO`);
        }
    }

Unfortunately, irrespective of the User's Location the IP always resolves to US.不幸的是,无论用户的位置如何,IP 总是解析为美国。

I have a custom domain mapped to the cloud run service via Domain Mapping.我有一个自定义域通过域映射映射到云运行服务。

What could be the reason the IP of the Client is always US IP? Client 的 IP 始终是 US IP 的原因可能是什么?

Please note that this same service when hosted on Heroku gets the correct IP address of the connected client.请注意,当托管在 Heroku 上时,相同的服务会获得已连接客户端的正确 IP 地址。

So, I'm very certain that it has something to do with Cloud Run.所以,我非常肯定它与Cloud Run有关。

All my services on Cloud Run are on US-CENTRAL1我在 Cloud Run 上的所有服务都在 US-CENTRAL1 上

For anyone who may experience something like this in the future.对于将来可能会遇到类似情况的任何人。

We had Cloudflare sitting in front of Cloud Run.我们让 Cloudflare 坐在 Cloud Run 前面。

So, to get the correct Client's IP address all we had to do was retrieve it from cf-connecting-ip header instead of x-forwarded-for .因此,要获得正确的客户端 IP 地址,我们所要做的就是从cf-connecting-ip header 而不是x-forwarded-for检索它。

So, the modified and working code now becomes:因此,修改后的工作代码现在变为:

   async getSocketIP(socket) {
        let { headers, address } = socket.handshake;
        let { origin } = headers;
        let ip = headers['cf-connecting-ip'] ?? headers['x-forwarded-for']; //Notice the difference
        let userAgent = headers['user-agent'];
        try {
            let locationPointUrl = `http://www.geoplugin.net/json.gp?ip=${ip}`;
            let { data: location } = await axios.get(locationPointUrl);
        } catch (e) {
           console.log(`Error get client online IP on Socket IO`);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Dataflow 可以在 Go 中没有外部 IP 地址的情况下运行吗? - Can Google Cloud Dataflow be run without an external IP address in Go? 如何在 Firebase 云 function 中获取客户端 IP 地址? - How to get client IP address in a Firebase cloud function? 如何在我的 Cloud-Run Flask 应用程序中获取用户的 IP 地址? - How can I get the user's IP-Address in my Cloud-Run Flask app? 无法访问 Kube.netes Service IP 地址的工作负载 - Unable to access workload at the IP address of the Kubernetes Service GCP,Cloud Run 外部 IP 不工作 - GCP, Cloud Run external IP is not working 是否可以在谷歌云控制台或 firebase 控制台中更改项目 ip 地址 - Is it possible to change project ip address in google cloud console or firebase console 套接字 IO 事件有时无法从托管在 Google Cloud Run 上的服务到达客户端 - Socket IO event sometimes doesn't get to client from a Service hosted on Google Cloud Run 在 Cloud Run 上部署客户端的 docker 映像 - Deploy the docker image of a client on Cloud Run SSL 使用 Google Cloud Run 进行客户端身份验证 - SSL Client Authentication with Google Cloud Run Cloud Run 服务 URL 的格式是什么? - What is the format of Cloud Run service URLs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM