Go package that can be used to get client's real public IP from Fast HTTP request, which usually useful for logging HTTP server.
This is fork from realip for Fast HTTP with some imporvements.
- Follows the rule of X-Real-IP
- Follows the rule of X-Forwarded-For
- Exclude local or private address
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-Original-Forwarded-For
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-Forwarded
,Forwarded-For
andForwarded
(Variations of #2)ctx.RemoteAddr().String()
go get -u github.com/valyala/fasthttp
package main
import (
"log"
"github.com/valyala/fasthttp"
"github.com/ferluci/fast-realip"
)
func main() {
if err := fasthttp.ListenAndServe(":8080", realipHandler); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func realipHandler(ctx *fasthttp.RequestCtx) {
clientIP := realip.FromRequest(ctx)
log.Println("GET / from", clientIP)
}
Commited code must pass: