From c4130e200d1282c1637c6f79332593026c87993d Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 13 Jun 2019 23:55:23 -0400 Subject: refactored tls listener --- svc/svc.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'svc') diff --git a/svc/svc.go b/svc/svc.go index a7786d9..d8b4128 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -1,6 +1,7 @@ package svc // import "github.com/getwtxt/getwtxt/svc" import ( + "crypto/tls" "fmt" "log" "net/http" @@ -34,12 +35,21 @@ func Start() { confObj.Mu.RUnlock() server := newServer(portnum, index) - log.Printf("*** Listening on %v\n", portnum) - log.Printf("*** getwtxt %v Startup finished at %v, took %v\n\n", Vers, time.Now().Format(time.RFC3339), time.Since(before)) if TLS { - errLog("", server.ListenAndServeTLS(TLSCert, TLSKey)) + cert, err := tls.LoadX509KeyPair(TLSCert, TLSKey) + errFatal("", err) + + cfg := &tls.Config{Certificates: []tls.Certificate{cert}} + lstnr, err := tls.Listen("tcp", portnum, cfg) + errFatal("", err) + + server.TLSConfig = cfg + startAnnounce(portnum, before) + errLog("", server.ServeTLS(lstnr, "", "")) + } else { + startAnnounce(portnum, before) errLog("", server.ListenAndServe()) } @@ -50,6 +60,11 @@ func Start() { close(closeLog) } +func startAnnounce(portnum string, before time.Time) { + log.Printf("*** Listening on %v\n", portnum) + log.Printf("*** getwtxt %v Startup finished at %v, took %v\n\n", Vers, time.Now().Format(time.RFC3339), time.Since(before)) +} + func newServer(port string, index *mux.Router) *http.Server { // handlers.CompressHandler gzips all responses. // ipMiddleware passes the request IP along. -- cgit 1.4.1-2-gfad0