Files
mirror-3x-ui/internal/util/clientip/clientip.go
T
amirim f9e15a6b9b feat(sub): opt-in per-client rate limit for the subscription server (#6966)
A loop probing subscription ids, or one misbehaving app, could turn the subscription server into a database load generator, and there was nothing to cap it.

Add a setting, off by default, that limits how many subscription fetches one client address may make per minute (60 by default, 1 to 1000); requests over it get 429 with Retry-After. Static assets stay outside the guard. The client address comes from the same resolution the panel's login limiter uses, now shared in internal/util/clientip, so trustedProxyCIDRs decides it in one place for the panel and the subscription server.
2026-11-24 20:51:32 +03:30

100 lines
2.8 KiB
Go

// Package clientip resolves the address a request came from behind trusted reverse proxies.
package clientip
import (
"net"
"net/netip"
"strings"
)
// Resolve returns the address rate limiters key on: forwarded headers count only
// when remoteAddr is inside trustedCIDRs, and never when they contradict each other.
func Resolve(remoteAddr, xRealIP, xForwardedFor, trustedCIDRs string) string {
remoteIP, ok := Parse(remoteAddr)
if !ok {
return "unknown"
}
if !Trusted(remoteIP, trustedCIDRs) {
return remoteIP.String()
}
realIP, hasRealIP := Parse(xRealIP)
forwardedIP, hasForwarded := forwardedClient(xForwardedFor, trustedCIDRs)
switch {
case hasRealIP && hasForwarded && realIP != forwardedIP:
// A proxy rewrites at most one of the two, so the other is the client's
// own claim; neither may pick the identity a limiter keys on.
return remoteIP.String()
case hasForwarded:
return forwardedIP.String()
case hasRealIP:
return realIP.String()
}
return remoteIP.String()
}
// forwardedClient walks X-Forwarded-For from the proxy end: entries a client sent
// come first, so the first hop that is not a trusted proxy is the address one saw.
func forwardedClient(xForwardedFor, trustedCIDRs string) (netip.Addr, bool) {
parts := strings.Split(xForwardedFor, ",")
var client netip.Addr
found := false
for i := len(parts) - 1; i >= 0; i-- {
ip, ok := Parse(parts[i])
if !ok {
break
}
client, found = ip, true
if !Trusted(ip, trustedCIDRs) {
break
}
}
return client, found
}
// Trusted reports whether addr is inside the comma-separated CIDR or address list;
// an entry that parses as neither is skipped.
func Trusted(addr netip.Addr, trustedCIDRs string) bool {
addr = addr.Unmap()
for value := range strings.SplitSeq(trustedCIDRs, ",") {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if prefix, err := netip.ParsePrefix(value); err == nil {
if prefix.Contains(addr) {
return true
}
continue
}
if proxyIP, err := netip.ParseAddr(value); err == nil && proxyIP.Unmap() == addr {
return true
}
}
return false
}
// Parse reads an address from a header value or a RemoteAddr, with or without a port.
func Parse(value string) (netip.Addr, bool) {
candidate := strings.TrimSpace(value)
if candidate == "" {
return netip.Addr{}, false
}
if ip, err := netip.ParseAddr(candidate); err == nil {
return ip.Unmap(), true
}
if host, _, err := net.SplitHostPort(candidate); err == nil {
if ip, err := netip.ParseAddr(strings.TrimSpace(host)); err == nil {
return ip.Unmap(), true
}
}
if strings.Count(candidate, ":") == 1 {
if host, _, err := net.SplitHostPort("[" + candidate + "]"); err == nil {
if ip, err := netip.ParseAddr(strings.TrimSpace(host)); err == nil {
return ip.Unmap(), true
}
}
}
return netip.Addr{}, false
}