2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2017-01-12 08:40:01 +01:00
|
|
|
// released under the MIT license
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
package connection_limits
|
2017-01-12 08:40:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-10-09 07:47:04 +02:00
|
|
|
"sync"
|
2017-01-12 08:40:01 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
// ThrottlerConfig controls the automated connection throttling.
|
|
|
|
type ThrottlerConfig struct {
|
2017-10-09 07:58:57 +02:00
|
|
|
Enabled bool
|
|
|
|
CidrLenIPv4 int `yaml:"cidr-len-ipv4"`
|
|
|
|
CidrLenIPv6 int `yaml:"cidr-len-ipv6"`
|
|
|
|
ConnectionsPerCidr int `yaml:"max-connections"`
|
|
|
|
DurationString string `yaml:"duration"`
|
|
|
|
Duration time.Duration `yaml:"duration-time"`
|
|
|
|
BanDurationString string `yaml:"ban-duration"`
|
|
|
|
BanDuration time.Duration
|
|
|
|
BanMessage string `yaml:"ban-message"`
|
|
|
|
Exempted []string
|
|
|
|
}
|
|
|
|
|
2017-01-12 08:40:01 +01:00
|
|
|
// ThrottleDetails holds the connection-throttling details for a subnet/IP.
|
|
|
|
type ThrottleDetails struct {
|
|
|
|
Start time.Time
|
|
|
|
ClientCount int
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
// Throttler manages automated client connection throttling.
|
|
|
|
type Throttler struct {
|
2017-10-09 07:47:04 +02:00
|
|
|
sync.RWMutex
|
|
|
|
|
2017-01-12 08:40:01 +01:00
|
|
|
enabled bool
|
|
|
|
ipv4Mask net.IPMask
|
|
|
|
ipv6Mask net.IPMask
|
|
|
|
subnetLimit int
|
|
|
|
duration time.Duration
|
|
|
|
population map[string]ThrottleDetails
|
|
|
|
|
|
|
|
// used by the server to ban clients that go over this limit
|
2017-10-09 07:47:04 +02:00
|
|
|
banDuration time.Duration
|
|
|
|
banMessage string
|
2017-01-12 08:40:01 +01:00
|
|
|
|
|
|
|
// exemptedIPs holds IPs that are exempt from limits
|
|
|
|
exemptedIPs map[string]bool
|
|
|
|
// exemptedNets holds networks that are exempt from limits
|
|
|
|
exemptedNets []net.IPNet
|
|
|
|
}
|
|
|
|
|
|
|
|
// maskAddr masks the given IPv4/6 address with our cidr limit masks.
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) maskAddr(addr net.IP) net.IP {
|
2017-01-12 08:40:01 +01:00
|
|
|
if addr.To4() == nil {
|
|
|
|
// IPv6 addr
|
|
|
|
addr = addr.Mask(ct.ipv6Mask)
|
|
|
|
} else {
|
|
|
|
// IPv4 addr
|
|
|
|
addr = addr.Mask(ct.ipv4Mask)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetFor removes any existing count for the given address.
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) ResetFor(addr net.IP) {
|
2017-10-09 07:47:04 +02:00
|
|
|
ct.Lock()
|
|
|
|
defer ct.Unlock()
|
|
|
|
|
2017-01-12 08:40:01 +01:00
|
|
|
if !ct.enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
|
|
|
ct.maskAddr(addr)
|
|
|
|
addrString := addr.String()
|
|
|
|
delete(ct.population, addrString)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddClient introduces a new client connection if possible. If we can't, throws an error instead.
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) AddClient(addr net.IP) error {
|
2017-10-09 07:47:04 +02:00
|
|
|
ct.Lock()
|
|
|
|
defer ct.Unlock()
|
|
|
|
|
2017-01-12 08:40:01 +01:00
|
|
|
if !ct.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check exempted lists
|
|
|
|
if ct.exemptedIPs[addr.String()] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, ex := range ct.exemptedNets {
|
|
|
|
if ex.Contains(addr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check throttle
|
|
|
|
ct.maskAddr(addr)
|
|
|
|
addrString := addr.String()
|
|
|
|
|
|
|
|
details, exists := ct.population[addrString]
|
|
|
|
if !exists || details.Start.Add(ct.duration).Before(time.Now()) {
|
|
|
|
details = ThrottleDetails{
|
|
|
|
Start: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if details.ClientCount+1 > ct.subnetLimit {
|
|
|
|
return errTooManyClients
|
|
|
|
}
|
|
|
|
|
|
|
|
details.ClientCount++
|
|
|
|
ct.population[addrString] = details
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) BanDuration() time.Duration {
|
2017-10-09 07:47:04 +02:00
|
|
|
ct.RLock()
|
|
|
|
defer ct.RUnlock()
|
|
|
|
|
|
|
|
return ct.banDuration
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) BanMessage() string {
|
2017-10-09 07:47:04 +02:00
|
|
|
ct.RLock()
|
|
|
|
defer ct.RUnlock()
|
|
|
|
|
|
|
|
return ct.banMessage
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:37:13 +02:00
|
|
|
// NewThrottler returns a new client connection throttler.
|
2017-10-09 07:47:04 +02:00
|
|
|
// The throttler is functional, but disabled; it can be enabled via `ApplyConfig`.
|
2017-10-09 23:37:13 +02:00
|
|
|
func NewThrottler() *Throttler {
|
|
|
|
var ct Throttler
|
2017-01-12 08:40:01 +01:00
|
|
|
|
2017-10-09 07:47:04 +02:00
|
|
|
// initialize empty population; all other state is configurable
|
2017-01-12 08:40:01 +01:00
|
|
|
ct.population = make(map[string]ThrottleDetails)
|
|
|
|
|
2017-10-09 07:47:04 +02:00
|
|
|
return &ct
|
|
|
|
}
|
2017-01-12 08:40:01 +01:00
|
|
|
|
2017-10-09 07:47:04 +02:00
|
|
|
// ApplyConfig atomically applies a config update to a throttler
|
2017-10-09 23:37:13 +02:00
|
|
|
func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error {
|
2017-01-12 08:40:01 +01:00
|
|
|
// assemble exempted nets
|
2017-10-09 07:47:04 +02:00
|
|
|
exemptedIPs := make(map[string]bool)
|
|
|
|
var exemptedNets []net.IPNet
|
2017-01-12 08:40:01 +01:00
|
|
|
for _, cidr := range config.Exempted {
|
|
|
|
ipaddr := net.ParseIP(cidr)
|
|
|
|
_, netaddr, err := net.ParseCIDR(cidr)
|
|
|
|
|
|
|
|
if ipaddr == nil && err != nil {
|
2017-10-09 07:47:04 +02:00
|
|
|
return fmt.Errorf("Could not parse exempted IP/network [%s]", cidr)
|
2017-01-12 08:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ipaddr != nil {
|
2017-10-09 07:47:04 +02:00
|
|
|
exemptedIPs[ipaddr.String()] = true
|
2017-01-12 08:40:01 +01:00
|
|
|
} else {
|
2017-10-09 07:47:04 +02:00
|
|
|
exemptedNets = append(exemptedNets, *netaddr)
|
2017-01-12 08:40:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 07:47:04 +02:00
|
|
|
ct.Lock()
|
|
|
|
defer ct.Unlock()
|
|
|
|
|
|
|
|
ct.enabled = config.Enabled
|
|
|
|
ct.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32)
|
|
|
|
ct.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128)
|
|
|
|
ct.subnetLimit = config.ConnectionsPerCidr
|
|
|
|
ct.duration = config.Duration
|
|
|
|
ct.banDuration = config.BanDuration
|
|
|
|
ct.banMessage = config.BanMessage
|
|
|
|
ct.exemptedIPs = exemptedIPs
|
|
|
|
ct.exemptedNets = exemptedNets
|
|
|
|
|
|
|
|
return nil
|
2017-01-12 08:40:01 +01:00
|
|
|
}
|