From 280e8218ae07aaac369681180490eb1008e07e5e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 25 May 2019 11:37:47 -0700 Subject: [PATCH] another example Signed-off-by: Jeff Carr --- emaild/Makefile | 8 +++-- emaild/main.go | 4 +++ emaild/server.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 emaild/server.go diff --git a/emaild/Makefile b/emaild/Makefile index 493f4a7..627f0ea 100644 --- a/emaild/Makefile +++ b/emaild/Makefile @@ -1,6 +1,8 @@ +# do this + +run: + go run server.go + build: go build ./emaild - -run: - go run *.go diff --git a/emaild/main.go b/emaild/main.go index 18a22c6..74a7571 100644 --- a/emaild/main.go +++ b/emaild/main.go @@ -219,6 +219,8 @@ func sendProtobuf(conn *websocket.Conn, msg *pb.Event) { */ func eventHandler(w http.ResponseWriter, r *http.Request) { + log.Println("Starting http.ListenAndServe() on port 9000") + spew.Dump(r) log.Println("echoHandler() START") log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent")) log.Println("echoHandler() ATTEMPT r.Header", r.Header) @@ -256,6 +258,8 @@ func eventHandler(w http.ResponseWriter, r *http.Request) { } func serveHome(w http.ResponseWriter, r *http.Request) { + log.Println("Starting http.ListenAndServe() on port 9000") + spew.Dump(r) if r.URL.Path != "/" { http.Error(w, "Not found.", http.StatusNotFound) return diff --git a/emaild/server.go b/emaild/server.go new file mode 100644 index 0000000..93becc2 --- /dev/null +++ b/emaild/server.go @@ -0,0 +1,88 @@ +// 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" +// import "os" + +import "math/rand" +import "net" +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" + +// this is our dnssec IPv6 socket library +import "git.wit.com/jcarr/dnssecsocket" + +const ( + CONN_HOST = "localhost" + CONN_PORT = "3333" + CONN_TYPE = "tcp" +) + +/* +go func(c net.Conn) { + defer c.Close() + io.Copy(os.Stdout, c) +}(conn) +*/ + +func main() { +/* + // redirect all this output to systemd + dnssecsocket.UseJournalctl() + + hostname := fqdn.Get() + log.Println("FQDN hostname is", hostname) + + // lookup the IP address from DNS + dnsRR := dnssecsocket.Dnstrace(hostname, "AAAA") + spew.Dump(dnsRR) + ipaddr := dns.Field(dnsRR, 1) + log.Println("ipaddr", ipaddr) +*/ + + listenstr := "[" + "localhost" + "]:" + CONN_PORT + log.Println("listenstr", listenstr) + + myTCPAddr, err := net.ResolveTCPAddr("tcp", listenstr) + + // // Listen for incoming connections on the IPv6 address only + l, err := net.ListenTCP("tcp", myTCPAddr) + if err != nil { + log.Println("Error listening:", err.Error()) + return + } + + // Close the listener when the application closes. + defer l.Close() + + log.Println("Listening on " + listenstr) + + rand.Seed(time.Now().Unix()) + + for { + // Listen for an incoming connection. + conn, err := l.AcceptTCP() + if err != nil { + log.Println("Error accepting: ", err.Error()) + return + } + + log.Println("Accepted new connection from: " + conn.RemoteAddr().String()) + + // Handle connections in a new goroutine. + go dnssecsocket.HandleConnection(conn) + } +}