Determine IP to which HTTP request was addressed in http.Handler

Viewed 123

I have the following setup:

The request is made to the server using one of its IP addresses - let's say either 192.168.0.1(on eth0) or 192.168.0.2(on eth1). The server is listening on all addresses on the local machine (e.g. http.ListenAndServe(":8080", nil)).

How can I determine in the server code from the request which network interface (or IP address) it was sent to?

req.Host contains the host name for the server. Moreover, I can't see anything in the request headers.

1 Answers

ok, I figured out this for http library source code - incoming address is stored in request context under http.LocalAddrContextKey key. So to extract incomming IP address you need to do

r.Context().Value(http.LocalAddrContextKey)
Related