Skip to content

Commit

Permalink
add support for domain matching
Browse files Browse the repository at this point in the history
  • Loading branch information
kelchy committed Feb 23, 2021
1 parent 0086650 commit 957f65a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 19 additions & 1 deletion matchrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"net"
"time"
"strings"

"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/forward"
Expand All @@ -21,6 +22,7 @@ type MatchRelay struct{
fwd *forward.Forward
rules []rule
zones []string
domains map[string]string
interval time.Duration
filename string
}
Expand Down Expand Up @@ -48,13 +50,29 @@ func (mr MatchRelay) SetProxy(proxy string) {
func (mr *MatchRelay) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}

if len(mr.domains) > 0 {
// state.Name() will always have a trailing .
sArr := strings.Split(state.Name(), ".")
l := len(sArr)
if l > 2 {
base := sArr[l - 3] + "." + sArr[l - 2] + "."
for i := l - 3; i > 0; i = i - 1 {
str := sArr[i - 1] + "." + base
if _, ok := mr.domains[str]; ok {
mr.fwd.ServeDNS(ctx, w, r)
return 0, nil
}
base = str
}
}
}

for _, rule := range mr.rules {
// check zone.
zone := plugin.Zones(mr.zones).Matches(state.Name())
if zone == "" {
continue
}

ipMatch := matchWithPolicies(rule.policies, w, r)
if ipMatch {
mr.fwd.ServeDNS(ctx, w, r)
Expand Down
4 changes: 4 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func parse(c *caddy.Controller) (MatchRelay, error) {
mr := New()
// matchrelay takes zone details from server block, not on config block
mr.zones = make([]string, len(c.ServerBlockKeys))
mr.domains = make(map[string]string)
copy(mr.zones, c.ServerBlockKeys)
for i := range mr.zones {
mr.zones[i] = plugin.Host(mr.zones[i]).Normalize()
Expand All @@ -101,6 +102,9 @@ func parse(c *caddy.Controller) (MatchRelay, error) {
return mr, c.Errf("empty token")
}
switch id {
case "domain":
// we don't support any options for now so set it to empty string
mr.domains[remainingTokens[0]] = ""
case "net":
// static rules
p := makePolicy(remainingTokens)
Expand Down

0 comments on commit 957f65a

Please sign in to comment.