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"
|
|
|
|
|
2019-05-04 14:13:07 -05:00
|
|
|
import "math/rand"
|
|
|
|
import "net"
|
|
|
|
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"
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
// this is the king of dns libraries
|
2019-05-04 16:25:23 -05:00
|
|
|
import "github.com/miekg/dns"
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
// this is awesome for debugging
|
2019-05-04 16:25:23 -05:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
// this is our dnssec IPv6 socket library
|
|
|
|
import "git.wit.com/jcarr/dnssecsocket"
|
2019-05-04 14:13:07 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
CONN_HOST = "localhost"
|
|
|
|
CONN_PORT = "3333"
|
|
|
|
CONN_TYPE = "tcp"
|
|
|
|
)
|
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
/*
|
|
|
|
go func(c net.Conn) {
|
|
|
|
defer c.Close()
|
|
|
|
io.Copy(os.Stdout, c)
|
|
|
|
}(conn)
|
|
|
|
*/
|
|
|
|
|
2019-05-04 14:13:07 -05:00
|
|
|
func main() {
|
2019-05-09 22:22:44 -05:00
|
|
|
// redirect all this output to systemd
|
|
|
|
dnssecsocket.UseJournalctl()
|
|
|
|
|
2019-05-04 16:25:23 -05:00
|
|
|
hostname := fqdn.Get()
|
|
|
|
log.Println("FQDN hostname is", hostname)
|
|
|
|
|
|
|
|
// lookup the IP address from DNS
|
2019-05-05 02:00:39 -05:00
|
|
|
dnsRR := dnssecsocket.Dnstrace(hostname, "AAAA")
|
2019-05-04 16:25:23 -05:00
|
|
|
spew.Dump(dnsRR)
|
|
|
|
ipaddr := dns.Field(dnsRR, 1)
|
|
|
|
log.Println("ipaddr", ipaddr)
|
|
|
|
|
|
|
|
listenstr := "[" + ipaddr + "]:" + CONN_PORT
|
|
|
|
log.Println("listenstr", listenstr)
|
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
myTCPAddr, err := net.ResolveTCPAddr("tcp", listenstr)
|
|
|
|
|
2019-05-04 16:25:23 -05:00
|
|
|
// // Listen for incoming connections on the IPv6 address only
|
2019-05-09 08:20:57 -05:00
|
|
|
l, err := net.ListenTCP("tcp", myTCPAddr)
|
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.
|
2019-05-09 08:20:57 -05:00
|
|
|
conn, err := l.AcceptTCP()
|
2019-05-04 14:13:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error accepting: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:22:44 -05:00
|
|
|
log.Println("Accepted new connection from: " + conn.RemoteAddr().String())
|
|
|
|
|
2019-05-04 14:13:07 -05:00
|
|
|
// Handle connections in a new goroutine.
|
2019-05-05 02:00:39 -05:00
|
|
|
go dnssecsocket.HandleConnection(conn)
|
2019-05-04 14:13:07 -05:00
|
|
|
}
|
|
|
|
}
|