Do you want to build a website clone similar to whatismyipaddress.com? It is actually easy to develop. I decided to create this tool because I was working on an automated DNS client that will check my public IP address. Perhaps, somebody might need this in the future. I already made the completed demo available online at https://ip.johnpili.com
How it works
It works by reading the HTTP header request which contains information such as IP Address, User-Agent, Scheme, Accept-Encoding, etc. If you are using a reverse proxy like Cloudflare, you can extract IP information from header keys Cf-Connecting-Ip or X-Real-Ip
Sample HTTP header
map[ Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip] Accept-Language:[en-US,en;q=0.5] Cache-Control:[no-cache] Cdn-Loop:[cloudflare] Cf-Connecting-Ip:[193.169.145.66] Cf-Ipcountry:[T1] Cf-Visitor:[{"scheme":"https"}] Connection:[upgrade] Dnt:[1] Pragma:[no-cache] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Firefox/68.0] X-Forwarded-For:[193.169.145.66, 193.169.145.66] X-Forwarded-Proto:[https] X-Real-Ip:[193.169.145.66] ]
Snippet extracting IP address from header
func (z *PageController) getIPDetails(r *http.Request) models.IPInfo { ip := "" if len(z.Configuration.Extraction.HeaderKey) > 0 { ip = r.Header.Get(z.Configuration.Extraction.HeaderKey) // Extract IP from header because we are using reverse proxy example X-Real-Ip } if len(ip) == 0 { // Fallback ip = extractIPAddress(r.RemoteAddr) } ipInfo := models.IPInfo{ IP: ip, UserAgent: r.Header.Get("User-Agent"), } return ipInfo }
You get the full source code from github https://github.com/johnpili/ip-echo We can further improve this tool by adding an IP to location mapping from MaxMind or IP2Location