mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-05 18:03:41 +00:00
parent
3f96db8af5
commit
dba7604a43
|
@ -9,6 +9,7 @@ import (
|
|||
)
|
||||
|
||||
var commonMiddlewares = middleware.Middlewares{
|
||||
middleware.XFF,
|
||||
middleware.Cors,
|
||||
middleware.Logger,
|
||||
middleware.Tracing(nextRequestID),
|
||||
|
|
27
pkg/middleware/xff.go
Normal file
27
pkg/middleware/xff.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const xForwardedFor = "X-Forwarded-For"
|
||||
|
||||
func getIP(req *http.Request) string {
|
||||
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
return req.RemoteAddr
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
// XFF is a middleware to identifying the originating IP address using X-Forwarded-For header
|
||||
func XFF(inner http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
xff := r.Header.Get(xForwardedFor)
|
||||
if xff == "" {
|
||||
r.Header.Set(xForwardedFor, getIP(r))
|
||||
}
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user