forked from longtao.wu/droboticflow
131 lines
2.9 KiB
Go
131 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type rateLimitEntry struct {
|
|
count int
|
|
resetAt time.Time
|
|
lastSeen time.Time
|
|
}
|
|
|
|
type rateLimiter struct {
|
|
mu sync.Mutex
|
|
limit int
|
|
window time.Duration
|
|
entries map[string]rateLimitEntry
|
|
}
|
|
|
|
func newRateLimiter(limit int, window time.Duration) *rateLimiter {
|
|
return &rateLimiter{
|
|
limit: limit,
|
|
window: window,
|
|
entries: map[string]rateLimitEntry{},
|
|
}
|
|
}
|
|
|
|
func (limiter *rateLimiter) allow(key string) bool {
|
|
if limiter == nil || limiter.limit <= 0 || limiter.window <= 0 {
|
|
return true
|
|
}
|
|
now := time.Now()
|
|
limiter.mu.Lock()
|
|
defer limiter.mu.Unlock()
|
|
for itemKey, entry := range limiter.entries {
|
|
if now.Sub(entry.lastSeen) > limiter.window*4 {
|
|
delete(limiter.entries, itemKey)
|
|
}
|
|
}
|
|
entry := limiter.entries[key]
|
|
if entry.resetAt.IsZero() || now.After(entry.resetAt) {
|
|
entry = rateLimitEntry{resetAt: now.Add(limiter.window)}
|
|
}
|
|
entry.count++
|
|
entry.lastSeen = now
|
|
limiter.entries[key] = entry
|
|
return entry.count <= limiter.limit
|
|
}
|
|
|
|
func parseTrustedProxyCIDRs(value string) []*net.IPNet {
|
|
fields := strings.FieldsFunc(value, func(r rune) bool {
|
|
return r == ',' || r == ';' || r == '\n' || r == '\t' || r == ' '
|
|
})
|
|
nets := make([]*net.IPNet, 0, len(fields))
|
|
for _, field := range fields {
|
|
trimmed := strings.TrimSpace(field)
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
if ip := net.ParseIP(trimmed); ip != nil {
|
|
bits := 32
|
|
if ip.To4() == nil {
|
|
bits = 128
|
|
}
|
|
nets = append(nets, &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)})
|
|
continue
|
|
}
|
|
_, cidr, err := net.ParseCIDR(trimmed)
|
|
if err == nil {
|
|
nets = append(nets, cidr)
|
|
}
|
|
}
|
|
return nets
|
|
}
|
|
|
|
func requestRemoteIP(request *http.Request) string {
|
|
host, _, err := net.SplitHostPort(request.RemoteAddr)
|
|
if err == nil {
|
|
return host
|
|
}
|
|
return strings.TrimSpace(request.RemoteAddr)
|
|
}
|
|
|
|
func trustedProxyRemote(request *http.Request, trustedProxyNets []*net.IPNet) bool {
|
|
if len(trustedProxyNets) == 0 {
|
|
return false
|
|
}
|
|
remoteIP := net.ParseIP(requestRemoteIP(request))
|
|
if remoteIP == nil {
|
|
return false
|
|
}
|
|
for _, trusted := range trustedProxyNets {
|
|
if trusted.Contains(remoteIP) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func forwardedClientIP(request *http.Request) string {
|
|
clientIP := strings.TrimSpace(request.Header.Get("X-Forwarded-For"))
|
|
if comma := strings.Index(clientIP, ","); comma >= 0 {
|
|
clientIP = strings.TrimSpace(clientIP[:comma])
|
|
}
|
|
if clientIP == "" {
|
|
clientIP = strings.TrimSpace(request.Header.Get("X-Real-IP"))
|
|
}
|
|
if net.ParseIP(clientIP) == nil {
|
|
return ""
|
|
}
|
|
return clientIP
|
|
}
|
|
|
|
func (server *server) clientRateLimitKey(request *http.Request, suffix string) string {
|
|
clientIP := ""
|
|
if trustedProxyRemote(request, server.trustedProxyNets) {
|
|
clientIP = forwardedClientIP(request)
|
|
}
|
|
if clientIP == "" {
|
|
clientIP = requestRemoteIP(request)
|
|
}
|
|
if clientIP == "" {
|
|
clientIP = "unknown"
|
|
}
|
|
return clientIP + "|" + suffix
|
|
}
|