2019-05-04 14:13:07 -05:00
|
|
|
// inspired from:
|
|
|
|
// https://github.com/mactsouk/opensource.com.git
|
|
|
|
// and
|
|
|
|
// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// import "fmt"
|
2019-05-05 01:20:34 -05:00
|
|
|
// import "os"
|
|
|
|
|
|
|
|
import "bufio"
|
2019-05-04 14:13:07 -05:00
|
|
|
import "math/rand"
|
|
|
|
import "net"
|
|
|
|
import "strconv"
|
|
|
|
import "strings"
|
|
|
|
import "time"
|
|
|
|
import "log"
|
|
|
|
|
2019-05-04 16:25:23 -05:00
|
|
|
// 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"
|
|
|
|
|
2019-05-04 14:13:07 -05:00
|
|
|
const MIN = 1
|
|
|
|
const MAX = 100
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONN_HOST = "localhost"
|
|
|
|
CONN_PORT = "3333"
|
|
|
|
CONN_TYPE = "tcp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-05-04 16:25:23 -05:00
|
|
|
hostname := fqdn.Get()
|
|
|
|
log.Println("FQDN hostname is", hostname)
|
|
|
|
|
|
|
|
// lookup the IP address from DNS
|
|
|
|
dnsRR := dnstrace(hostname, "AAAA")
|
|
|
|
spew.Dump(dnsRR)
|
|
|
|
ipaddr := dns.Field(dnsRR, 1)
|
|
|
|
log.Println("ipaddr", ipaddr)
|
|
|
|
|
|
|
|
listenstr := "[" + ipaddr + "]:" + CONN_PORT
|
|
|
|
log.Println("listenstr", listenstr)
|
|
|
|
|
|
|
|
// // Listen for incoming connections on the IPv6 address only
|
|
|
|
l, err := net.Listen(CONN_TYPE, listenstr)
|
2019-05-04 14:13:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error listening:", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the listener when the application closes.
|
|
|
|
defer l.Close()
|
|
|
|
|
2019-05-04 16:25:23 -05:00
|
|
|
log.Println("Listening on " + listenstr)
|
2019-05-04 14:13:07 -05:00
|
|
|
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Listen for an incoming connection.
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error accepting: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle connections in a new goroutine.
|
|
|
|
go handleConnection(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func random() int {
|
|
|
|
return rand.Intn(MAX-MIN) + MIN
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:07:21 -05:00
|
|
|
func getRemoteAddr(c net.Conn) string {
|
|
|
|
clientAddr := c.RemoteAddr().String()
|
2019-05-05 01:20:34 -05:00
|
|
|
parts := strings.Split(clientAddr, "]")
|
|
|
|
ipv6 := parts[0]
|
|
|
|
return ipv6[2:]
|
2019-05-04 17:07:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Handle each connection
|
|
|
|
// Each client must send it's hostname as the first line
|
|
|
|
// Then each hostname is verified with DNSSEC
|
|
|
|
//
|
2019-05-04 14:13:07 -05:00
|
|
|
func handleConnection(c net.Conn) {
|
2019-05-04 17:07:21 -05:00
|
|
|
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)
|
2019-05-05 01:20:34 -05:00
|
|
|
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
|
|
|
|
}
|
2019-05-04 17:07:21 -05:00
|
|
|
|
2019-05-04 14:13:07 -05:00
|
|
|
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()
|
|
|
|
}
|