Documentation ¶
Overview ¶
Package realip mimics the nginx realip module. It rewrites RemoteAddr in an http.Request when the connection is from a trusted network.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RealIP ¶
type RealIP struct {
// contains filtered or unexported fields
}
RealIP is used to override RemoteAddr for net/http.
func New ¶
New creates a new RealIP that uses the given headers and networks. The first matching header is used.
Example ¶
// simple handler http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { // realip will rewrite RemoteAddr fmt.Fprintf(w, "Hello: %s\n", r.RemoteAddr) }) // we only will rewrite RemoteAddr if connection is from our "trusted" loadbalancers // we allow localhost for testing ri, err := realip.New([]string{"X-Forwarded-For", "X-Real-IP"}, []string{"8.8.0.0/16", "127.0.0.0/8"}) if err != nil { // do something with error } // this wraps all HTTP requests http.ListenAndServe(":8080", ri.Handler(http.DefaultServeMux))
Output:
Click to show internal directories.
Click to hide internal directories.