在Google Cloud服务上如何找到IPv4地址?

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

How do I find the IPv4 address on a Google Cloud service?

问题

我有一个部署在Heroku上的Go服务,成功地从请求头中获取IPv4地址。

ip := net.ParseIP(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]).String()

我将完全相同的代码部署为Google Cloud的服务,大约25%的时间里IP地址经常是IPv6。在检查完整的请求头之后,没有找到任何IPv4地址,只有IPv6。

Heroku的请求头X-Forwarded-For始终包含IPv4地址,但Google Cloud没有。有人知道如何在Google Cloud中强制使用IPv4格式的请求头吗?

英文:

I have a Go service, deployed on Heroku, which pulls the IPv4 address from the request header successfully.

ip := net.ParseIP(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]).String()

I have deployed the identical code as a service to Google Cloud, and the IP addresses are frequently IPv6 in about 25% of the time. After examining the full Request Header, there is no IPv4 address available anywhere, only IPv6.

Heroku's Request Header X-Forwarded-For ALWAYS contains the IPv4 address, yet Google Cloud doesn't. Does anyone know a way to force the IPv4 format for Request Headers in Google Cloud?

答案1

得分: 3

客户端可以通过IPv4或IPv6连接,但不能同时使用两者。客户端将只使用一个地址族,并且代理服务器将只记录一个IP地址。

附加信息:

1)Heroku不支持IPv6,因此客户端被强制使用IPv4进行连接。参考链接

2)如果您只想要IPv4连接,请不要启用IPv6前端。然而,我建议在可能的情况下使用IPv6。

英文:

Clients can connect via IPv4 or IPv6 but not both. Only one address family will be used by the client and only one IP address will be recorded by the proxy.

Additional information:

  1. Heroku does not support IPv6 so clients are forced to connect using IPv4. reference

  2. If you only want IPv4 connections, do not enable the IPv6 frontends. However, I recommend using IPv6 where possible.

huangapple
  • 本文由 发表于 2022年3月24日 05:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/71594388.html
匿名

发表评论

匿名网友

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

确定