From d767632eedfeabf37283092e7ee0a35229122f6b Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 4 May 2019 15:07:21 -0700 Subject: [PATCH] Look up the client IP from the hostname Signed-off-by: Jeff Carr --- Makefile | 2 +- server/dnstrace.go | 9 ++++++--- server/server.go | 28 +++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 7f18058..8803001 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ DNSNAME = $(shell hostname -f) all: # 'gaper' is a simple and smart golang tool that just rebuilds every time you change a file # go get -u github.com/maxcnunes/gaper - gaper + # gaper push: git pull diff --git a/server/dnstrace.go b/server/dnstrace.go index d4a42c5..e845957 100644 --- a/server/dnstrace.go +++ b/server/dnstrace.go @@ -2,7 +2,7 @@ package main -import "flag" +// import "flag" import "fmt" import "log" import "net" @@ -13,6 +13,8 @@ import "time" import "github.com/miekg/dns" import "github.com/rs/dnstrace/client" +// import "github.com/davecgh/go-spew/spew" + const ( cReset = 0 cBold = 1 @@ -34,14 +36,15 @@ func colorize(s interface{}, color int, enabled bool) string { } func dnstrace(hostname string, qtypestr string) dns.RR { - color := flag.Bool("color", true, "Enable/disable colors") + // color := flag.Bool("color", true, "Enable/disable colors") + color := true qname := dns.Fqdn(hostname) // qtype := dns.TypeA qtype := dns.StringToType[qtypestr] col := func(s interface{}, c int) string { - return colorize(s, c, *color) + return colorize(s, c, color) } m := &dns.Msg{} diff --git a/server/server.go b/server/server.go index 8dbfc3c..7ab7005 100644 --- a/server/server.go +++ b/server/server.go @@ -76,8 +76,34 @@ func random() int { return rand.Intn(MAX-MIN) + MIN } +func getRemoteAddr(c net.Conn) string { + clientAddr := c.RemoteAddr().String() + return clientAddr +} + +// +// Handle each connection +// Each client must send it's hostname as the first line +// Then each hostname is verified with DNSSEC +// func handleConnection(c net.Conn) { - log.Printf("Serving %s\n", c.RemoteAddr().String()) + ipv6client := getRemoteAddr(c) + log.Println("Serving to %s as the IPv6 client", ipv6client) + + log.Println("Waiting for the client to tell me its name") + netData, err := bufio.NewReader(c).ReadString('\n') + if err != nil { + log.Println(err) + return + } + clientHostname := strings.TrimSpace(netData) + log.Println("Recieved client hostname as:", clientHostname) + + dnsRR := dnstrace(clientHostname, "AAAA") + ipaddr := dns.Field(dnsRR, 1) + log.Println("Client claims to be: ", ipaddr) + log.Println("Serving to IPv6 client:", ipv6client) + for { netData, err := bufio.NewReader(c).ReadString('\n') if err != nil {