3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00

delete websocket support as per discussion on #139

This commit is contained in:
Shivaram Lingamneni 2017-09-28 02:53:08 -04:00
parent e8b1870067
commit 3b0f1ff1cc
4 changed files with 2 additions and 133 deletions

View File

@ -196,7 +196,6 @@ type Config struct {
Password string
Name string
Listen []string
Wslisten string `yaml:"ws-listen"`
TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
STS STSConfig
RestAPI RestAPIConfig `yaml:"rest-api"`

View File

@ -38,8 +38,8 @@ var (
)
const (
// when shutting down the REST and websocket servers, wait this long
// before killing active non-WS connections. TODO: this might not be
// when shutting down the REST server, wait this long
// before killing active connections. TODO: this might not be
// necessary at all? but it seems prudent to avoid potential resource
// leaks
httpShutdownTimeout = time.Second
@ -124,7 +124,6 @@ type Server struct {
store *buntdb.DB
stsEnabled bool
whoWas *WhoWasList
wsServer *http.Server
}
var (
@ -382,75 +381,6 @@ func (server *Server) createListener(addr string, tlsConfig *tls.Config) *Listen
return &wrapper
}
//
// websocket listen goroutine
//
func (server *Server) setupWSListener(config *Config) {
// unconditionally shut down the old listener because we can't tell
// whether we need to reload the TLS certificate
if server.wsServer != nil {
ctx, _ := context.WithTimeout(context.Background(), httpShutdownTimeout)
server.wsServer.Shutdown(ctx)
server.wsServer.Close()
}
if config.Server.Wslisten == "" {
server.wsServer = nil
return
}
addr := config.Server.Wslisten
tlsConfig := config.Server.TLSListeners[addr]
handler := http.NewServeMux()
wsServer := http.Server{
Addr: addr,
Handler: handler,
}
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
server.logger.Error("ws", addr, fmt.Sprintf("%s method not allowed", r.Method))
return
}
// We don't have any subprotocols, so if someone attempts to `new
// WebSocket(server, "subprotocol")` they'll break here, instead of
// getting the default, ambiguous, response from gorilla.
if v, ok := r.Header["Sec-Websocket-Protocol"]; ok {
http.Error(w, fmt.Sprintf("WebSocket subprocotols (e.g. %s) not supported", v), 400)
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
server.logger.Error("ws", addr, fmt.Sprintf("%s websocket upgrade error: %s", server.name, err))
return
}
newConn := clientConn{
Conn: WSContainer{ws},
IsTLS: tlsConfig != nil,
}
server.newConns <- newConn
})
go func() {
var err error
server.logger.Info("listeners", fmt.Sprintf("websocket listening on %s, tls=%t.", addr, tlsConfig != nil))
if tlsConfig != nil {
err = wsServer.ListenAndServeTLS(tlsConfig.Cert, tlsConfig.Key)
} else {
err = wsServer.ListenAndServe()
}
if err != nil {
server.logger.Error("listeners", fmt.Sprintf("websocket ListenAndServe error: %s", err))
}
}()
server.wsServer = &wsServer
}
// generateMessageID returns a network-unique message ID.
func (server *Server) generateMessageID() string {
return fmt.Sprintf("%s-%s", strconv.FormatInt(time.Now().UTC().UnixNano(), 10), strconv.FormatInt(rand.Int63(), 10))
@ -1497,7 +1427,6 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
// we are now open for business
server.setupListeners(config)
server.setupWSListener(config)
server.setupRestAPI(config)
return nil

View File

@ -1,56 +0,0 @@
// Copyright (c) 2015 Niels Freier
// Copyright (c) 2015 Edmund Huber
// released under the MIT license
package irc
import (
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 2048,
WriteBufferSize: 2048,
// If a WS session contains sensitive information, and you choose to use
// cookies for authentication (during the HTTP(S) upgrade request), then
// you should check that Origin is a domain under your control. If it
// isn't, then it is possible for users of your site, visiting a naughty
// Origin, to have a WS opened using their credentials. See
// http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
// We don't care about Origin because the (IRC) authentication is contained
// in the WS stream -- the WS session is not privileged when it is opened.
CheckOrigin: func(r *http.Request) bool { return true },
}
// WSContainer holds the websocket.
type WSContainer struct {
*websocket.Conn
}
// Read reads new incoming messages.
func (ws WSContainer) Read(msg []byte) (int, error) {
ty, bytes, err := ws.ReadMessage()
if ty == websocket.TextMessage {
n := copy(msg, []byte(string(bytes)+"\r\n\r\n"))
return n, err
}
// Binary, and other kinds of messages, are thrown away.
return 0, nil
}
// Write writes lines out to the websocket.
func (ws WSContainer) Write(msg []byte) (int, error) {
err := ws.WriteMessage(websocket.TextMessage, msg)
return len(msg), err
}
// SetDeadline sets the read and write deadline on this websocket.
func (ws WSContainer) SetDeadline(t time.Time) error {
if err := ws.SetWriteDeadline(t); err != nil {
return err
}
return ws.SetReadDeadline(t)
}

View File

@ -17,9 +17,6 @@ server:
- "[::1]:6668"
- ":6697" # ssl port
# websocket listening port
ws-listen: ":8080"
# tls listeners
tls-listeners:
# listener on ":6697"