Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dns wildcarding #615

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions dns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"sync"

"github.com/miekg/dns"
Expand All @@ -20,23 +21,39 @@ var dnsAddr string

type dnsRecords struct {
sync.RWMutex
dnsMap map[string]string
hostMap *HostMap
dnsMap map[string]string
hostMap *HostMap
dnsWildcardEnabled bool
dnsWildcardLimit int
}

func newDnsRecords(hostMap *HostMap) *dnsRecords {
return &dnsRecords{
dnsMap: make(map[string]string),
hostMap: hostMap,
dnsMap: make(map[string]string),
hostMap: hostMap,
dnsWildcardEnabled: false,
dnsWildcardLimit: 5,
}
}

func (d *dnsRecords) Query(data string) string {
d.RLock()
if r, ok := d.dnsMap[data]; ok {
d.RUnlock()
return r

if d.dnsWildcardEnabled {
hostParts := strings.SplitN(data, ".", d.dnsWildcardLimit+1)
for i := range hostParts {
if r, ok := d.dnsMap[strings.Join(hostParts[i:], ".")]; ok {
d.RUnlock()
return r
}
}
} else {
if r, ok := d.dnsMap[data]; ok {
d.RUnlock()
return r
}
}

d.RUnlock()
return ""
}
Expand Down Expand Up @@ -113,6 +130,8 @@ func handleDnsRequest(l *logrus.Logger, w dns.ResponseWriter, r *dns.Msg) {

func dnsMain(l *logrus.Logger, hostMap *HostMap, c *config.C) func() {
dnsR = newDnsRecords(hostMap)
dnsR.dnsWildcardEnabled = c.GetBool("dns.wildcard", false)
dnsR.dnsWildcardLimit = c.GetInt("dns.wildcard_limit", 5)
aaronstillwell marked this conversation as resolved.
Show resolved Hide resolved

// attach request handler func
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
Expand Down
43 changes: 43 additions & 0 deletions dns_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)

func TestParsequery(t *testing.T) {
Expand All @@ -17,3 +18,45 @@ func TestParsequery(t *testing.T) {

//parseQuery(m)
}

func TestDnsWildcardLookup(t *testing.T) {
hostMap := &HostMap{}
ds := newDnsRecords(hostMap)
ds.Add("test.com.com", "1.2.3.4")
ds.dnsWildcardEnabled = true

result := ds.Query("test.com.com")
assert.Equal(t, "1.2.3.4", result)

result = ds.Query("foo.test.com.com")
assert.Equal(t, "1.2.3.4", result)
}

func TestDnsWildcardDisabled(t *testing.T) {
hostMap := &HostMap{}
ds := newDnsRecords(hostMap)
ds.Add("test.com.com", "1.2.3.4")

result := ds.Query("test.com.com")
assert.Equal(t, "1.2.3.4", result)

result = ds.Query("foo.test.com.com")
assert.Equal(t, "", result)
}

func TestDnsWildcardLookupLimit(t *testing.T) {
hostMap := &HostMap{}
ds := newDnsRecords(hostMap)
ds.Add("test.com.com", "1.2.3.4")
ds.dnsWildcardEnabled = true
ds.dnsWildcardLimit = 1

result := ds.Query("test.com.com")
assert.Equal(t, "1.2.3.4", result)

result = ds.Query("foo.test.com.com")
assert.Equal(t, "1.2.3.4", result)

result = ds.Query("foo.bar.test.com.com")
assert.Equal(t, "", result)
}