Look up the client IP from the hostname

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-04 15:07:21 -07:00
parent a9a254e62d
commit d767632eed
3 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -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{}

View File

@ -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 {