Skip to content

Commit

Permalink
reverse dns
Browse files Browse the repository at this point in the history
  • Loading branch information
clinta committed Sep 14, 2016
1 parent 7e07216 commit 959ef77
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
func monDocker(dockerEndpoints []string, ca, cert, key string, verify bool) {
containers = make(map[string]dockertypes.ContainerJSON)
records = make(map[string][]net.IP)
rev_records = make(map[string]string)

for _, e := range dockerEndpoints {
log.WithField("Docker Endpoint", e).Debug("Starting docker monitor")
Expand Down
39 changes: 34 additions & 5 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

var (
records map[string][]net.IP
recordLock sync.RWMutex
records map[string][]net.IP
recordLock sync.RWMutex
rev_records map[string]string
)

const aLabel = "DKDNS_A"
Expand All @@ -30,24 +31,36 @@ func updateRecords() {
for _, es := range cjson.NetworkSettings.Networks {
if es.IPAddress != "" {
ip := net.ParseIP(es.IPAddress)
var rev string
if al, ok := cjson.Config.Labels[aLabel]; ok {
for _, l := range strings.Split(al, ",") {
ln := fullyQualify(l)
records[ln] = append(records[ln], ip)
rev = ln
}
}
if regContainerName {
cn := fullyQualify(cjson.Name)
records[cn] = append(records[cn], ip)
rev = cn
}
if regHostName {
hn := fullyQualify(cjson.Config.Hostname)
records[hn] = append(records[hn], ip)
rev = hn
}
if regContainerName {
cn := fullyQualify(cjson.Name)
records[cn] = append(records[cn], ip)
if rev != "" {
rev_ip, err := dns.ReverseAddr(ip.String())
if err != nil {
log.WithError(err).WithField("IP", ip.String()).Error("Error reversing ip for reverse dns")
}
rev_records[rev_ip] = rev
}
}
}
}
log.WithField("Records", records).Debug("Records updated")
log.WithField("Reverse", rev_records).Debug("Records updated")
}

var (
Expand Down Expand Up @@ -84,6 +97,22 @@ func handle(w dns.ResponseWriter, r *dns.Msg) {
recordLock.RLock()
defer recordLock.RUnlock()
for _, q := range r.Question {
if q.Qtype == dns.TypePTR {
if rev, ok := rev_records[q.Name]; ok {
log.Debug("Reverse DNS Response")
rr := &dns.PTR{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: ttl,
},
Ptr: rev,
}
m.Answer = append(m.Answer, rr)
}
continue
}
if q.Qtype != dns.TypeA && q.Qtype != dns.TypeAAAA {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/urfave/cli"
)

const version = "0.2.3"
const version = "0.3"

var (
compress bool
Expand Down Expand Up @@ -109,7 +109,7 @@ func Run(ctx *cli.Context) error {
log.WithField("Domain", dom).Debug("Domain set")
log.WithField("Compress", compress).Debug("Compression set")

dns.HandleFunc(dom, handle)
dns.HandleFunc(".", handle)
go serve("tcp", ctx.String("listen"))
go serve("udp", ctx.String("listen"))

Expand Down

0 comments on commit 959ef77

Please sign in to comment.