reqip
A simple tool for retrieving a request's IP address on the server. Inspired from request-ip
Installation
Via go get
go get github.com/mo7zayed/reqip
How to use
package main
import (
"fmt"
"net/http"
"github.com/mo7zayed/reqip"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(
w,
fmt.SprintF("your ip is %s", reqip.GetClientIP(r)) // reqip.GetClientIP receives a *http.Request var type
)
})
fmt.Println("server started on: http://127.0.1.1:8000")
http.ListenAndServe(":8000", nil)
}
How It Works
It looks for specific headers in the request and falls back to some defaults if they do not exist.
The user ip is determined by the following order:
X-Client-IP
X-Forwarded-For
(Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the the first one.)CF-Connecting-IP
(Cloudflare)Fastly-Client-Ip
(Fastly CDN and Firebase hosting header when forwared to a cloud function)True-Client-Ip
(Akamai and Cloudflare)X-Real-IP
(Nginx proxy/FastCGI)X-Cluster-Client-IP
(Rackspace LB, Riverbed Stingray)X-Forwarded
,Forwarded-For
andForwarded
(Variations of #2)http.Request.RemoteAddr
License
The MIT License (MIT) - 2020