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

Issue

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?

Solution

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.

Answered By – John Hanley

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.