2018-08-30 14:25:33 +00:00
|
|
|
package ratelimit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
2018-11-20 20:51:18 +00:00
|
|
|
"sort"
|
2018-08-30 14:25:33 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
// ratelimiting and per-ip buckets
|
|
|
|
"github.com/beefsack/go-rate"
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
|
|
|
|
// coredns plugin
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2018-11-25 20:11:36 +00:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
2018-08-30 14:25:33 +00:00
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2018-11-25 20:32:28 +00:00
|
|
|
const defaultRatelimit = 30
|
2018-11-25 20:11:36 +00:00
|
|
|
const defaultResponseSize = 1000
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
tokenBuckets = cache.New(time.Hour, time.Hour)
|
|
|
|
)
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
// ServeDNS handles the DNS request and refuses if it's an beyind specified ratelimit
|
|
|
|
func (p *plug) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2018-08-30 14:25:33 +00:00
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
ip := state.IP()
|
|
|
|
allow, err := p.allowRequest(ip)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !allow {
|
|
|
|
ratelimited.Inc()
|
|
|
|
return 0, nil
|
|
|
|
}
|
2018-11-25 20:11:36 +00:00
|
|
|
|
|
|
|
// Record response to get status code and size of the reply.
|
|
|
|
rw := dnstest.NewRecorder(w)
|
|
|
|
status, err := plugin.NextOrFailure(p.Name(), p.Next, ctx, rw, r)
|
|
|
|
|
|
|
|
size := rw.Len
|
|
|
|
|
|
|
|
if size > defaultResponseSize && state.Proto() == "udp" {
|
|
|
|
// For large UDP responses we call allowRequest more times
|
|
|
|
// The exact number of times depends on the response size
|
|
|
|
for i := 0; i < size/defaultResponseSize; i++ {
|
|
|
|
p.allowRequest(ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status, err
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
func (p *plug) allowRequest(ip string) (bool, error) {
|
2018-11-20 20:51:18 +00:00
|
|
|
|
2018-11-20 21:18:13 +00:00
|
|
|
if len(p.whitelist) > 0 {
|
|
|
|
i := sort.SearchStrings(p.whitelist, ip)
|
|
|
|
|
|
|
|
if i < len(p.whitelist) && p.whitelist[i] == ip {
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-11-20 20:51:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
if _, found := tokenBuckets.Get(ip); !found {
|
|
|
|
tokenBuckets.Set(ip, rate.New(p.ratelimit, time.Second), time.Hour)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, found := tokenBuckets.Get(ip)
|
|
|
|
if !found {
|
|
|
|
// should not happen since we've just inserted it
|
|
|
|
text := "SHOULD NOT HAPPEN: just-inserted ratelimiter disappeared"
|
|
|
|
log.Println(text)
|
|
|
|
err := errors.New(text)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rl, ok := value.(*rate.RateLimiter)
|
2018-09-14 13:50:56 +00:00
|
|
|
if !ok {
|
2018-08-30 14:25:33 +00:00
|
|
|
text := "SHOULD NOT HAPPEN: non-bool entry found in safebrowsing lookup cache"
|
|
|
|
log.Println(text)
|
|
|
|
err := errors.New(text)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allow, _ := rl.Try()
|
|
|
|
return allow, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterPlugin("ratelimit", caddy.Plugin{
|
|
|
|
ServerType: "dns",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
type plug struct {
|
2018-08-30 14:25:33 +00:00
|
|
|
Next plugin.Handler
|
|
|
|
|
|
|
|
// configuration for creating above
|
2018-11-20 20:51:18 +00:00
|
|
|
ratelimit int // in requests per second per IP
|
|
|
|
whitelist []string // a list of whitelisted IP addresses
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 20:51:18 +00:00
|
|
|
func setupPlugin(c *caddy.Controller) (*plug, error) {
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
p := &plug{ratelimit: defaultRatelimit}
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
2018-11-20 20:51:18 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
ratelimit, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
p.ratelimit = ratelimit
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
2018-11-20 20:51:18 +00:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "whitelist":
|
|
|
|
p.whitelist = c.RemainingArgs()
|
|
|
|
|
|
|
|
if len(p.whitelist) > 0 {
|
|
|
|
sort.Strings(p.whitelist)
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:51:18 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
p, err := setupPlugin(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := dnsserver.GetConfig(c)
|
2018-08-30 14:25:33 +00:00
|
|
|
config.AddPlugin(func(next plugin.Handler) plugin.Handler {
|
|
|
|
p.Next = next
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
|
|
|
|
c.OnStartup(func() error {
|
2018-09-14 13:50:56 +00:00
|
|
|
m := dnsserver.GetConfig(c).Handler("prometheus")
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if x, ok := m.(*metrics.Metrics); ok {
|
|
|
|
x.MustRegister(ratelimited)
|
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
func newDNSCounter(name string, help string) prometheus.Counter {
|
2018-08-30 14:25:33 +00:00
|
|
|
return prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: plugin.Namespace,
|
|
|
|
Subsystem: "ratelimit",
|
|
|
|
Name: name,
|
|
|
|
Help: help,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-09-14 13:50:56 +00:00
|
|
|
ratelimited = newDNSCounter("dropped_total", "Count of requests that have been dropped because of rate limit")
|
2018-08-30 14:25:33 +00:00
|
|
|
)
|
|
|
|
|
2018-09-14 13:50:56 +00:00
|
|
|
// Name returns name of the plugin as seen in Corefile and plugin.cfg
|
|
|
|
func (p *plug) Name() string { return "ratelimit" }
|