Documentation
¶
Overview ¶
Example ¶
var serverAddress = "127.0.0.1:8080"
myParse, err := NewParse([]string{"127.0.0.1"})
if err != nil {
log.Println(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Set the global trusted proxy addresses,
// If not set, the default priority is used
if err := SetTrustedProxies([]string{"127.0.0.1"}); err != nil {
log.Println(err)
}
// Use the global default configuration
ip := ClientIP(r)
// Use custom configuration
_ = myParse.ClientIP(r)
// Get the ip address from X-Appengine-Remote-Addr first.
// If the IP address cannot be obtained, try to get it from another lower priority
_ = ClientIP(r, XAppEngineRemoteAddr)
_ = myParse.ClientIP(r, XAppEngineRemoteAddr)
// Use the custom configuration
_ = ClientIP(r, XHeader("X-My-IP"))
_ = myParse.ClientIP(r)
fmt.Fprintf(w, "Your IP address is %s", ip)
})
go func() {
log.Println(http.ListenAndServe(serverAddress, nil))
}()
// execute http request
req, err := http.NewRequest("GET", "http://"+serverAddress, nil)
if err != nil {
panic(err)
}
// simulated proxy server added address information
req.Header.Set("X-Forwarded-For", "123.123.0,1, 123.123.0.2")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
io.Copy(os.Stdout, resp.Body)
Output: Your IP address is 123.123.0.2
Index ¶
Examples ¶
Constants ¶
View Source
const Forwarded = "Forwarded"
Forwarded standard RFC-7239 https://www.rfc-editor.org/rfc/rfc7239.txt
Variables ¶
This section is empty.
Functions ¶
func SetTrustedProxies ¶
SetTrustedProxies Set a global trusted proxy server address.
Types ¶
type ForwardedHeader ¶
ForwardedHeader represents the structure of a parsed Forwarded header standard RFC-7239 https://www.rfc-editor.org/rfc/rfc7239.txt section 5 .
func ParseForwardedHeader ¶
func ParseForwardedHeader(headerValue string) ([]ForwardedHeader, error)
ParseForwardedHeader parses the Forwarded header value and returns a slice of ForwardedHeader e.g: - Forwarded: for=192.0.2.43, for=198.51.100.17;by=203.0.113.60;proto=http;host=example.com - Forwarded: for="[2001:db8:cafe::17]", for=unknown
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func (*Parser) SetTrustedProxies ¶
SetTrustedProxies Set a trusted proxy server address. like SetTrustedProxies().
type XHeader ¶
type XHeader string
const ( // XForwardedFor X-Forwarded-For. XForwardedFor XHeader = "X-Forwarded-For" // XRealIP commonly used for Nginx、Apache HTTP Server, etc. XRealIP XHeader = "X-Real-IP" // XClientIP used by Amazon EC2, Heroku, and others. XClientIP XHeader = "X-Client-IP" // CFConnectingIP used by Cloudflare. // - https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip CFConnectingIP XHeader = "CF-Connecting-IP" // FastlyClientIp Fastly CDN and Firebase hosting header when forward to a cloud function. FastlyClientIp XHeader = "Fastly-Client-Ip" // TrueClientIp Akamai and Cloudflare(enterprise plan only). TrueClientIp XHeader = "True-Client-Ip" // XAppEngineRemoteAddr Google App Engine(GAE). XAppEngineRemoteAddr XHeader = "X-Appengine-Remote-Addr" // FlyClientIP when running on Fly.io. FlyClientIP XHeader = "Fly-Client-IP" )
Click to show internal directories.
Click to hide internal directories.