3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 03:02:48 +01:00

server: Let websocket listeners also be TLS

This commit is contained in:
Daniel Oaks 2016-04-28 20:41:26 +10:00
parent 2fb6acb92a
commit 3a9a643d30

View File

@ -103,7 +103,7 @@ func NewServer(config *Config) *Server {
} }
if config.Server.Wslisten != "" { if config.Server.Wslisten != "" {
server.wslisten(config.Server.Wslisten) server.wslisten(config.Server.Wslisten, config.Server.TLSListeners)
} }
signal.Notify(server.signals, SERVER_SIGNALS...) signal.Notify(server.signals, SERVER_SIGNALS...)
@ -273,8 +273,7 @@ func (s *Server) listen(addr string, tlsMap map[Name]*tls.Config) {
// websocket listen goroutine // websocket listen goroutine
// //
func (s *Server) wslisten(addr string) { func (s *Server) wslisten(addr string, tlsMap map[string]*TLSListenConfig) {
//TODO(dan): open a https websocket here if ssl/tls details are setup in the config for the wslistener
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { if r.Method != "GET" {
Log.error.Printf("%s method not allowed", s) Log.error.Printf("%s method not allowed", s)
@ -297,10 +296,22 @@ func (s *Server) wslisten(addr string) {
s.newConns <- WSContainer{ws} s.newConns <- WSContainer{ws}
}) })
go func() { go func() {
Log.info.Printf("%s listening on %s", s, addr) config, listenTLS := tlsMap[addr]
err := http.ListenAndServe(addr, nil)
tlsString := "plaintext"
var err error
if listenTLS {
tlsString = "TLS"
}
Log.info.Printf("%s websocket listening on %s using %s.", s, addr, tlsString)
if listenTLS {
err = http.ListenAndServeTLS(addr, config.Cert, config.Key, nil)
} else {
err = http.ListenAndServe(addr, nil)
}
if err != nil { if err != nil {
Log.error.Printf("%s listenAndServe error: %s", s, err) Log.error.Printf("%s listenAndServe (%s) error: %s", s, tlsString, err)
} }
}() }()
} }