Skip to content

Commit

Permalink
Allow filtering via the network_source
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouzierinverse committed Mar 9, 2023
1 parent 6f5b415 commit 4287047
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion go/plugin/coredns/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"crypto/tls"
"errors"
"net"
"sync/atomic"
"time"

Expand Down Expand Up @@ -42,6 +43,7 @@ type Forward struct {
maxfails uint32
expire time.Duration
maxConcurrent int64
sourceNetwork net.IPNet

opts options // also here for testing

Expand Down Expand Up @@ -190,13 +192,17 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}

func (f *Forward) match(state request.Request) bool {
if !plugin.Name(f.from).Matches(state.Name()) || !f.isAllowedDomain(state.Name()) {
if !plugin.Name(f.from).Matches(state.Name()) || !f.isAllowedDomain(state.Name()) || !f.isIpAllowed(net.ParseIP(state.IP())) {
return false
}

return true
}

func (f *Forward) isIpAllowed(ip net.IP) bool {
return len(f.sourceNetwork.IP) != len(ip) || f.sourceNetwork.Contains(ip)
}

func (f *Forward) isAllowedDomain(name string) bool {
if dns.Name(name) == dns.Name(f.from) {
return true
Expand Down
11 changes: 11 additions & 0 deletions go/plugin/coredns/forward/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"net"
"strconv"
"time"

Expand Down Expand Up @@ -281,6 +282,16 @@ func parseBlock(c *caddy.Controller, f *Forward) error {
}
f.ErrLimitExceeded = errors.New("concurrent queries exceeded maximum " + c.Val())
f.maxConcurrent = int64(n)
case "network_source":
if !c.NextArg() {
return c.ArgErr()
}
_, ipNet, err := net.ParseCIDR(c.Val())
if err != nil {
return c.Err("Unable to parse network_source configuration parameter")
}

f.sourceNetwork = *ipNet

default:
return c.Errf("unknown property '%s'", c.Val())
Expand Down

0 comments on commit 4287047

Please sign in to comment.