diff --git a/connection_handler.go b/connection_handler.go new file mode 100644 index 0000000..3381ed4 --- /dev/null +++ b/connection_handler.go @@ -0,0 +1,89 @@ +// inspired from: +// https://github.com/mactsouk/opensource.com.git +// and +// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go + +package dnssecsocket + +// import "fmt" +// import "os" + +import "bufio" +import "math/rand" +import "net" +import "strconv" +import "strings" +// import "time" +import "log" + +// will try to get this hosts FQDN +// import "github.com/Showmax/go-fqdn" + +import "github.com/miekg/dns" + +// import "github.com/davecgh/go-spew/spew" + +const MIN = 1 +const MAX = 100 + +func random() int { + return rand.Intn(MAX-MIN) + MIN +} + +func GetRemoteAddr(c net.Conn) string { + clientAddr := c.RemoteAddr().String() + parts := strings.Split(clientAddr, "]") + ipv6 := parts[0] + return ipv6[1:] +} + +// +// 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) { + 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) + if (ipaddr != ipv6client) { + log.Println() + log.Println("DNSSEC ERROR: client IPv6 does not work") + log.Println("DNSSEC ERROR: client IPv6 does not work") + log.Println("DNSSEC ERROR: client IPv6 does not work") + log.Println() + c.Close() + return + } + + for { + netData, err := bufio.NewReader(c).ReadString('\n') + if err != nil { + log.Println(err) + return + } + + temp := strings.TrimSpace(string(netData)) + if temp == "STOP" { + break + } + log.Println("Recieved: ", temp) + + result := strconv.Itoa(random()) + "\n" + c.Write([]byte(string(result))) + } + c.Close() +} diff --git a/server/dnstrace.go b/dnstrace.go similarity index 97% rename from server/dnstrace.go rename to dnstrace.go index e845957..e53a5f6 100644 --- a/server/dnstrace.go +++ b/dnstrace.go @@ -1,8 +1,7 @@ +package dnssecsocket + // inspired from github.com/rs/dnstrace/main.go -package main - -// import "flag" import "fmt" import "log" import "net" @@ -35,7 +34,7 @@ func colorize(s interface{}, color int, enabled bool) string { return fmt.Sprintf("\x1b[%dm%v\x1b[0m", color, s) } -func dnstrace(hostname string, qtypestr string) dns.RR { +func Dnstrace(hostname string, qtypestr string) dns.RR { // color := flag.Bool("color", true, "Enable/disable colors") color := true diff --git a/server/server.go b/server/server.go index 422cd32..52e6bf4 100644 --- a/server/server.go +++ b/server/server.go @@ -8,23 +8,22 @@ package main // import "fmt" // import "os" -import "bufio" import "math/rand" import "net" -import "strconv" -import "strings" import "time" import "log" // will try to get this hosts FQDN import "github.com/Showmax/go-fqdn" +// this is the king of dns libraries import "github.com/miekg/dns" +// this is awesome for debugging import "github.com/davecgh/go-spew/spew" -const MIN = 1 -const MAX = 100 +// this is our dnssec IPv6 socket library +import "git.wit.com/jcarr/dnssecsocket" const ( CONN_HOST = "localhost" @@ -37,7 +36,7 @@ func main() { log.Println("FQDN hostname is", hostname) // lookup the IP address from DNS - dnsRR := dnstrace(hostname, "AAAA") + dnsRR := dnssecsocket.Dnstrace(hostname, "AAAA") spew.Dump(dnsRR) ipaddr := dns.Field(dnsRR, 1) log.Println("ipaddr", ipaddr) @@ -68,69 +67,6 @@ func main() { } // Handle connections in a new goroutine. - go handleConnection(conn) + go dnssecsocket.HandleConnection(conn) } } - - -func random() int { - return rand.Intn(MAX-MIN) + MIN -} - -func getRemoteAddr(c net.Conn) string { - clientAddr := c.RemoteAddr().String() - parts := strings.Split(clientAddr, "]") - ipv6 := parts[0] - return ipv6[2:] -} - -// -// 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) { - 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) - if (ipaddr != ipv6client) { - log.Println() - log.Println("DNSSEC ERROR: client IPv6 does not work") - log.Println("DNSSEC ERROR: client IPv6 does not work") - log.Println("DNSSEC ERROR: client IPv6 does not work") - log.Println() - c.Close() - return - } - - for { - netData, err := bufio.NewReader(c).ReadString('\n') - if err != nil { - log.Println(err) - return - } - - temp := strings.TrimSpace(string(netData)) - if temp == "STOP" { - break - } - log.Println("Recieved: ", temp) - - result := strconv.Itoa(random()) + "\n" - c.Write([]byte(string(result))) - } - c.Close() -}