mirror of
https://github.com/adnanh/webhook.git
synced 2025-06-28 07:18:31 +00:00
* Update go-chi dependency to v5 * Update gofrs/uuid dependency to v5 * Update gorilla/mux dependency to v1.8.1 * Update go-humanize dependency to v1.0.1 * Update mxj dependency to v2.7.0 * Update fsnotify dependency to v1.7.0 * Update Go versions in GH build workflow * Update gopkg.in/yaml.v2 indirect dependency to v2.4.0 * Bump GH actions
60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package middleware
|
|
|
|
// Ported from Goji's middleware, source:
|
|
// https://github.com/zenazn/goji/tree/master/web/middleware
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
|
|
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
|
|
// of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
|
|
// (in that order).
|
|
//
|
|
// This middleware should be inserted fairly early in the middleware stack to
|
|
// ensure that subsequent layers (e.g., request loggers) which examine the
|
|
// RemoteAddr will see the intended value.
|
|
//
|
|
// You should only use this middleware if you can trust the headers passed to
|
|
// you (in particular, the two headers this middleware uses), for example
|
|
// because you have placed a reverse proxy like HAProxy or nginx in front of
|
|
// chi. If your reverse proxies are configured to pass along arbitrary header
|
|
// values from the client, or if you use this middleware without a reverse
|
|
// proxy, malicious clients will be able to make you very sad (or, depending on
|
|
// how you're using RemoteAddr, vulnerable to an attack of some sort).
|
|
func RealIP(h http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
if rip := realIP(r); rip != "" {
|
|
r.RemoteAddr = rip
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
|
|
func realIP(r *http.Request) string {
|
|
var ip string
|
|
|
|
if tcip := r.Header.Get(trueClientIP); tcip != "" {
|
|
ip = tcip
|
|
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
|
|
ip = xrip
|
|
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
|
|
i := strings.Index(xff, ",")
|
|
if i == -1 {
|
|
i = len(xff)
|
|
}
|
|
ip = xff[:i]
|
|
}
|
|
if ip == "" || net.ParseIP(ip) == nil {
|
|
return ""
|
|
}
|
|
return ip
|
|
}
|