package web import ( "net" "net/http" "strings" "sync" "time" ) // A person subscribes once; a script posting the form for other people's addresses is what this // slows down: at most subscribeMax POSTs per subscribeWindow from one client. A client is an IPv4 // address or an IPv6 /64 (one machine usually holds a whole /64). The window starts at the first // request of a client and everything about it is forgotten when it ends. At most maxClients are // remembered: past that, new clients are refused until a window ends, rather than the map growing. const ( subscribeMax = 10 subscribeWindow = time.Hour sweepEvery = time.Minute maxClients = 100000 ) type visits struct { count int first time.Time } type subscribeLimiter struct { mu sync.Mutex now func() time.Time byIP map[string]*visits lastSweep time.Time max int } func newSubscribeLimiter(now func() time.Time) *subscribeLimiter { return &subscribeLimiter{now: now, byIP: map[string]*visits{}, lastSweep: now(), max: maxClients} } // allow counts a request from ip. When the address is over its limit, or it is new and the map is // full, it returns false and how long until it may try again; a refused request is not counted. func (l *subscribeLimiter) allow(ip string) (retryAfter time.Duration, ok bool) { l.mu.Lock() defer l.mu.Unlock() now := l.now() l.sweep(now) v := l.byIP[ip] if v == nil && len(l.byIP) >= l.max { l.lastSweep = now.Add(-sweepEvery) // force it if wait := l.sweep(now); len(l.byIP) >= l.max { return wait, false } } if v == nil || now.Sub(v.first) >= subscribeWindow { v = &visits{first: now} l.byIP[ip] = v } if v.count >= subscribeMax { return v.first.Add(subscribeWindow).Sub(now), false } v.count++ return 0, true } // sweep forgets addresses whose window is over, at most once every sweepEvery. It returns how long // until the next window of a kept address ends (zero when it did not run). func (l *subscribeLimiter) sweep(now time.Time) (next time.Duration) { if now.Sub(l.lastSweep) < sweepEvery { return 0 } l.lastSweep = now next = subscribeWindow for ip, v := range l.byIP { if left := v.first.Add(subscribeWindow).Sub(now); left <= 0 { delete(l.byIP, ip) } else { next = min(next, left) } } return next } func (l *subscribeLimiter) size() int { l.mu.Lock() defer l.mu.Unlock() return len(l.byIP) } // clientKey names the client of r for the limiter: an IPv4 address, or the /64 of an IPv6 one. // // The client is the TCP peer. X-Forwarded-For is read only when the peer is one of the trusted // proxies: anyone can send the header, so trusting it from anybody would let a script pick a fresh // identity for every request. Even then only the right-most entry that is not itself a trusted // proxy is taken, since the entries on its left were written by the client. An empty or malformed // header, or one listing only trusted proxies, leaves the peer. func clientKey(r *http.Request, trusted []*net.IPNet) string { host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { host = r.RemoteAddr } ip := net.ParseIP(host) if ip == nil { return host } if inAny(ip, trusted) { ip = forwardedClient(r, trusted, ip) } if ip4 := ip.To4(); ip4 != nil { return ip4.String() } return ip.Mask(net.CIDRMask(64, 128)).String() + "/64" } // forwardedClient walks X-Forwarded-For (all its lines, in order) from the right. func forwardedClient(r *http.Request, trusted []*net.IPNet, peer net.IP) net.IP { entries := strings.Split(strings.Join(r.Header.Values("X-Forwarded-For"), ","), ",") for i := len(entries) - 1; i >= 0; i-- { ip := net.ParseIP(strings.TrimSpace(entries[i])) if ip == nil { return peer } if !inAny(ip, trusted) { return ip } } return peer } func inAny(ip net.IP, nets []*net.IPNet) bool { for _, n := range nets { if n.Contains(ip) { return true } } return false }