Skip to content

Commit

Permalink
Merge pull request #2 from kelchy/main
Browse files Browse the repository at this point in the history
Release v1.0.1
  • Loading branch information
kelchy authored Oct 28, 2021
2 parents 37bb9c4 + a79872d commit bb29583
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

golint
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

golint:
export GOBIN=$${PWD};\
go get -u golang.org/x/lint/golint

lint: golint
./golint -set_exit_status ./...
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/kelchy/matchrelay

go 1.13

require (
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/tools v0.1.7 // indirect
)
29 changes: 29 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
34 changes: 32 additions & 2 deletions matchrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"net"
"time"
"strings"
"crypto/md5"

"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/forward"
"github.com/coredns/coredns/request"
"github.com/coredns/coredns/plugin/pkg/log"

"github.com/infobloxopen/go-trees/iptree"
"github.com/miekg/dns"
Expand All @@ -24,7 +26,8 @@ type MatchRelay struct{
zones []string
domains map[string]string
interval time.Duration
filename string
files []string
md5sum map[string][16]byte
}

type rule struct {
Expand All @@ -36,17 +39,19 @@ type policy struct {
filter *iptree.Tree
}

// New - function which creates a module instance on coredns
func New() MatchRelay {
mr := MatchRelay{}
mr.fwd = forward.New()
return mr
}

// SetProxy - function which sets forwarding relay
func (mr MatchRelay) SetProxy(proxy string) {
mr.fwd.SetProxy(forward.NewProxy(proxy, "dns"))
}

// ServeDNS implements the plugin.Handler interface.
// ServeDNS - function which implements the plugin.Handler interface.
func (mr *MatchRelay) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}

Expand All @@ -66,6 +71,7 @@ func (mr *MatchRelay) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns
}
base = str
}
return plugin.NextOrFailure(state.Name(), mr.Next, ctx, w, r)
}

for _, rule := range mr.rules {
Expand All @@ -84,6 +90,30 @@ func (mr *MatchRelay) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns
return plugin.NextOrFailure(state.Name(), mr.Next, ctx, w, r)
}

func (mr *MatchRelay) pushMatch() error {
var buf []byte
changed := false
for _, file := range mr.files {
s, e := fileOpen(file)
if e != nil {
log.Errorf("pushMatch error opening matchrelay file %s", file)
return e
}
md5sum := md5.Sum(s)
if mr.md5sum[file] != md5sum {
log.Infof("Matchrelay new config %s MD5 = %x\n", file, md5sum)
changed = true
mr.md5sum[file] = md5sum
}
// insert a new line character (10) in between files just to be sure
buf = append(buf, append(s, 10)...)
}
if changed {
mr.Reload(buf)
}
return nil
}

// matchWithPolicies matches the DNS query with a list of Match polices and returns boolean
func matchWithPolicies(policies []policy, w dns.ResponseWriter, r *dns.Msg) bool {
state := request.Request{W: w, Req: r}
Expand Down
18 changes: 16 additions & 2 deletions reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ package matchrelay
import (
"io/ioutil"
"strings"
"strconv"
"bufio"
"bytes"

"github.com/coredns/coredns/plugin/pkg/log"
)

// Reload - function which reloads the rules
func (mr *MatchRelay) Reload(buf []byte) {
mr.rules = nil
lines := strings.Split(string(buf), "\n")
mr.domains = make(map[string]string)

r := rule{}
for _, line := range lines {
scanner := bufio.NewScanner(bytes.NewReader(buf))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, " ")
if fields[0] == "net" {
id := fields[0]
Expand All @@ -22,8 +29,15 @@ func (mr *MatchRelay) Reload(buf []byte) {
r.policies = append(r.policies, p)

}
} else if fields[0] == "domain" {
if fields[1] != "" {
mr.domains[fields[1]] = strconv.Itoa(len(buf))
}
}
}
if err := scanner.Err(); err != nil {
log.Errorf("read line error %v", err)
}
if len(r.policies) > 0 {
mr.rules = append(mr.rules, r)
}
Expand Down
28 changes: 6 additions & 22 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"time"
"path/filepath"
"crypto/md5"

"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
Expand Down Expand Up @@ -36,16 +35,9 @@ func setup(c *caddy.Controller) error {

loop := make(chan bool)
c.OnStartup(func() error {
if mr.interval == 0 || mr.filename == "" {
if mr.interval == 0 {
return nil
}
s, e := fileOpen(mr.filename)
if e != nil {
log.Errorf("error opening matchrelay file %s", mr.filename)
return e
}
md5sum := md5.Sum(s)
mr.Reload(s)

go func() {
ticker := time.NewTicker(mr.interval)
Expand All @@ -54,17 +46,7 @@ func setup(c *caddy.Controller) error {
case <-loop:
return
case <-ticker.C:
s, e := fileOpen(mr.filename)
if e != nil {
log.Errorf("error opening matchrelay file %s", mr.filename)
return
}
ms := md5.Sum(s)
if md5sum != ms {
log.Infof("Matchrelay new config MD5 = %x\n", ms)
md5sum = ms
mr.Reload(s)
}
mr.pushMatch()
}
}
}()
Expand All @@ -89,6 +71,7 @@ func parse(c *caddy.Controller) (MatchRelay, error) {
// matchrelay takes zone details from server block, not on config block
mr.zones = make([]string, len(c.ServerBlockKeys))
mr.domains = make(map[string]string)
mr.md5sum = make(map[string][16]byte)
copy(mr.zones, c.ServerBlockKeys)
for i := range mr.zones {
mr.zones[i] = plugin.Host(mr.zones[i]).Normalize()
Expand Down Expand Up @@ -130,12 +113,12 @@ func parse(c *caddy.Controller) (MatchRelay, error) {
}
case "match":
// file based rules with own reload mechanism compatible with static rules above
fileName := strings.ToLower(remainingTokens[0])
fileName := remainingTokens[0]
config := dnsserver.GetConfig(c)
if !filepath.IsAbs(fileName) && config.Root != "" {
fileName = filepath.Join(config.Root, fileName)
}
mr.filename = fileName
mr.files = append(mr.files, fileName)
default:
return mr, c.Errf("unexpected token %q; expect 'net', 'match', 'reload' or 'relay'", id)
}
Expand All @@ -144,6 +127,7 @@ func parse(c *caddy.Controller) (MatchRelay, error) {
mr.rules = append(mr.rules, r)
}
}
mr.pushMatch()
return mr, nil
}

Expand Down

0 comments on commit bb29583

Please sign in to comment.