From aff7bc512d5726a020e4107690abd24ae91b1b48 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sun, 1 Jun 2014 13:21:45 +0200 Subject: Added TLS support --- main.go | 19 ++++++++++++++++++- pages.go | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f61700f..6bf55c4 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,9 @@ var ( tplpath = flag.String("tplpath", "tpls", "Path to templates") staticpath = flag.String("staticpath", "static", "Path to static page elements") perroom = flag.Int("perroom", -1, "Maximum amount of users per room (negative for unlimited)") + usetls = flag.Bool("tls", false, "Should TLS be used?") + certfile = flag.String("tlscert", "", "TLS certificate file") + keyfile = flag.String("tlskey", "", "TLS key file") ) func main() { @@ -39,5 +42,19 @@ func main() { r.HandleFunc("/chat/{chatroom:.+}/", Chatpage) r.HandleFunc("/chat/{chatroom:.+}", Chatpage) http.Handle("/", r) - http.ListenAndServe(*laddr, nil) + + var listenServe func() error + if *usetls { + listenServe = func() error { + return http.ListenAndServeTLS(*laddr, *certfile, *keyfile, nil) + } + } else { + listenServe = func() error { + return http.ListenAndServe(*laddr, nil) + } + } + + if err := listenServe(); err != nil { + log.Fatal(err) + } } diff --git a/pages.go b/pages.go index ebe284a..61ee0ae 100644 --- a/pages.go +++ b/pages.go @@ -44,5 +44,9 @@ func Home(rw http.ResponseWriter, req *http.Request) { func Chatpage(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) - TplChat.Execute(rw, ChatpageData{"ws://" + req.Host + strings.Replace(req.URL.Path+"/socket", "//", "/", -1), vars["chatroom"]}) + wsproto := "ws://" + if *usetls { + wsproto = "wss://" + } + TplChat.Execute(rw, ChatpageData{wsproto + req.Host + strings.Replace(req.URL.Path+"/socket", "//", "/", -1), vars["chatroom"]}) } -- cgit v1.2.3-54-g00ecf