armbian-router/middleware/middleware.go

90 lines
1.6 KiB
Go
Raw Permalink Normal View History

package middleware
2022-01-10 04:47:40 +00:00
import (
2022-01-10 04:53:23 +00:00
"net"
"net/http"
"strings"
2022-01-10 04:47:40 +00:00
)
var (
2022-01-10 04:53:23 +00:00
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
xForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Proto")
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
forwardLimit = 5
2022-01-10 04:47:40 +00:00
)
2022-01-10 04:53:23 +00:00
// RealIPMiddleware is an implementation of reverse proxy checks.
// It uses the remote address to find the originating IP, as well as protocol
func RealIPMiddleware(f http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2022-01-10 04:53:23 +00:00
// Treat unix socket as 127.0.0.1
if r.RemoteAddr == "@" {
r.RemoteAddr = "127.0.0.1:0"
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
f.ServeHTTP(w, r)
return
}
netIP := net.ParseIP(host)
if !netIP.IsLoopback() && !netIP.IsPrivate() {
2022-01-10 04:53:23 +00:00
f.ServeHTTP(w, r)
return
}
if rip := realIP(r); len(rip) > 0 {
r.RemoteAddr = net.JoinHostPort(rip, "0")
}
if rproto := realProto(r); len(rproto) > 0 {
r.URL.Scheme = rproto
}
f.ServeHTTP(w, r)
})
2022-01-10 04:47:40 +00:00
}
func realIP(r *http.Request) string {
2022-01-10 04:53:23 +00:00
var ip string
if xrip := r.Header.Get(xRealIP); xrip != "" {
ip = xrip
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
p := 0
for i := forwardLimit; i > 0; i-- {
if p > 0 {
xff = xff[:p-2]
}
p = strings.LastIndex(xff, ", ")
if p < 0 {
p = 0
break
} else {
p += 2
}
}
ip = xff[p:]
}
return ip
2022-01-10 04:47:40 +00:00
}
func realProto(r *http.Request) string {
2022-01-10 04:53:23 +00:00
proto := "http"
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
if r.TLS != nil {
proto = "https"
}
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
if xproto := r.Header.Get(xForwardedProto); xproto != "" {
proto = xproto
}
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
return proto
2022-01-10 04:47:40 +00:00
}