fix(api): 仅信任配置的反向代理转发客户端 IP (#39)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package clientip
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Resolver resolves client addresses using configured trusted proxy ranges.
|
||||
type Resolver struct {
|
||||
trusted []*net.IPNet
|
||||
}
|
||||
|
||||
var defaultResolver = &Resolver{trusted: []*net.IPNet{
|
||||
mustCIDR("127.0.0.0/8"),
|
||||
mustCIDR("::1/128"),
|
||||
mustCIDR("10.0.0.0/8"),
|
||||
mustCIDR("172.16.0.0/12"),
|
||||
mustCIDR("192.168.0.0/16"),
|
||||
mustCIDR("fc00::/7"),
|
||||
}}
|
||||
|
||||
// Parse creates a resolver from comma-separated CIDRs or bare IP addresses.
|
||||
func Parse(spec string) (*Resolver, error) {
|
||||
spec = strings.TrimSpace(spec)
|
||||
if spec == "" {
|
||||
return Default(), nil
|
||||
}
|
||||
if strings.EqualFold(spec, "none") {
|
||||
return &Resolver{}, nil
|
||||
}
|
||||
|
||||
resolver := &Resolver{}
|
||||
for _, item := range strings.Split(spec, ",") {
|
||||
item = strings.TrimSpace(item)
|
||||
if item == "" {
|
||||
return nil, fmt.Errorf("empty trusted proxy entry")
|
||||
}
|
||||
if ip := net.ParseIP(item); ip != nil {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
resolver.trusted = append(resolver.trusted, &net.IPNet{
|
||||
IP: ipv4,
|
||||
Mask: net.CIDRMask(32, 32),
|
||||
})
|
||||
} else {
|
||||
resolver.trusted = append(resolver.trusted, &net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.CIDRMask(128, 128),
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
_, network, err := net.ParseCIDR(item)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid trusted proxy entry %q: %w", item, err)
|
||||
}
|
||||
resolver.trusted = append(resolver.trusted, network)
|
||||
}
|
||||
return resolver, nil
|
||||
}
|
||||
|
||||
// Default trusts loopback, private IPv4, and unique-local IPv6 proxies.
|
||||
func Default() *Resolver {
|
||||
return defaultResolver
|
||||
}
|
||||
|
||||
// ClientIP returns the resolved client IP without a port.
|
||||
func (r *Resolver) ClientIP(req *http.Request) string {
|
||||
if req == nil {
|
||||
return ""
|
||||
}
|
||||
if r == nil {
|
||||
r = Default()
|
||||
}
|
||||
|
||||
peer, peerIP := remoteIP(req.RemoteAddr)
|
||||
if peerIP == nil || !r.isTrusted(peerIP) {
|
||||
return peer
|
||||
}
|
||||
if ip := net.ParseIP(strings.TrimSpace(req.Header.Get("X-Real-IP"))); ip != nil {
|
||||
return ip.String()
|
||||
}
|
||||
|
||||
var forwarded []string
|
||||
for _, value := range req.Header.Values("X-Forwarded-For") {
|
||||
forwarded = append(forwarded, strings.Split(value, ",")...)
|
||||
}
|
||||
for i := len(forwarded) - 1; i >= 0; i-- {
|
||||
ip := net.ParseIP(strings.TrimSpace(forwarded[i]))
|
||||
if ip != nil && !r.isTrusted(ip) {
|
||||
return ip.String()
|
||||
}
|
||||
}
|
||||
return peer
|
||||
}
|
||||
|
||||
// Middleware normalizes RemoteAddr to the resolved client IP.
|
||||
func (r *Resolver) Middleware(next http.Handler) http.Handler {
|
||||
if r == nil {
|
||||
r = Default()
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
req.RemoteAddr = r.ClientIP(req)
|
||||
next.ServeHTTP(w, req)
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Resolver) isTrusted(ip net.IP) bool {
|
||||
for _, network := range r.trusted {
|
||||
if network.Contains(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func remoteIP(remoteAddr string) (string, net.IP) {
|
||||
host, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
host = remoteAddr
|
||||
}
|
||||
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
|
||||
host = host[1 : len(host)-1]
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return host, nil
|
||||
}
|
||||
return ip.String(), ip
|
||||
}
|
||||
|
||||
func mustCIDR(value string) *net.IPNet {
|
||||
_, network, err := net.ParseCIDR(value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return network
|
||||
}
|
||||
Reference in New Issue
Block a user