anwar ul hasan
anwar ul hasan

Reputation: 65

Get real IP from Request in server

I am trying to extract the real IP from the HTTP request. When I make a CURL call from a container deployed on the same VM, I get the IP of the container from the http Request instead of IP of the VM through r.RemoteAddr.

But when I print the request using fmt.Printf("Request: %s", r) I can see the real IP of the VM is there. The sample request from Print statement looks like:

&{GET /containers/json HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[*/*] User-Agent:[curl/7.64.0]] {} %!s(func() (io.ReadCloser, error)=<nil>) %!s(int64=0) [] %!s(bool=false) 10.32.20.122:2375 map[] map[] %!s(*multipart.Form=<nil>) map[] 172.20.0.1:47102 /containers/json %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) %!s(*context.cancelCtx=&{0xc000120480 {0 0} <nil> map[] <nil>})}

In the above output 10.32.20.122 is the VM IP and 172.20.0.1 is docker network gateway IP. I want to extract the VM IP.

Upvotes: 0

Views: 163

Answers (1)

Cadet
Cadet

Reputation: 354

You would be better off using %+v in your Printf

fmt.Printf("Request: %+v", r)

Then you could see what the field is called.

Upvotes: 1

Related Questions