violet/proxy/websocket/server.go

121 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-08-17 14:38:00 +01:00
package websocket
import (
"github.com/gorilla/websocket"
"log"
"net/http"
"sync"
"time"
)
var upgrader = websocket.Upgrader{
HandshakeTimeout: time.Second * 5,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// allow requests from any origin
// the internal service can decide what origins to allow
return true
},
}
type Server struct {
connLock *sync.RWMutex
connStop bool
conns map[string]*websocket.Conn
}
func NewServer() *Server {
return &Server{
connLock: new(sync.RWMutex),
conns: make(map[string]*websocket.Conn),
}
}
func (s *Server) Upgrade(rw http.ResponseWriter, req *http.Request) {
req.URL.Scheme = "ws"
log.Printf("[Websocket] Upgrading request to '%s' from '%s'\n", req.URL.String(), req.Header.Get("Origin"))
2023-08-17 14:38:00 +01:00
c, err := upgrader.Upgrade(rw, req, nil)
if err != nil {
return
}
2023-09-10 21:49:57 +01:00
defer c.Close()
2023-08-17 14:38:00 +01:00
s.connLock.Lock()
// no more connections allowed
if s.connStop {
2023-08-17 14:57:41 +01:00
s.connLock.Unlock()
2023-08-17 14:38:00 +01:00
return
}
// save connection for shutdown
s.conns[c.RemoteAddr().String()] = c
2023-08-17 14:57:41 +01:00
s.connLock.Unlock()
2023-08-17 14:38:00 +01:00
log.Printf("[Websocket] Dialing: '%s'\n", req.URL.String())
// dial for internal connection
ic, _, err := websocket.DefaultDialer.DialContext(req.Context(), req.URL.String(), nil)
2023-08-17 14:38:00 +01:00
if err != nil {
log.Printf("[Websocket] Failed to dial '%s': %s\n", req.URL.String(), err)
2023-08-17 14:38:00 +01:00
s.Remove(c)
return
}
2023-09-10 21:49:57 +01:00
defer ic.Close()
2023-08-28 23:09:29 +01:00
d1 := make(chan struct{}, 1)
d2 := make(chan struct{}, 1)
2023-08-17 14:38:00 +01:00
// relay messages each way
2023-08-28 23:09:29 +01:00
go s.wsRelay(d1, c, ic)
go s.wsRelay(d2, ic, c)
2023-08-17 14:38:00 +01:00
// wait for done signal and close both connections
log.Println("[Websocket] Completed websocket hijacking")
2023-09-10 21:49:57 +01:00
// waiting until d1 or d2 close then automatically defer close both connections
select {
case <-d1:
case <-d2:
}
2023-08-17 14:38:00 +01:00
}
func (s *Server) wsRelay(done chan struct{}, a, b *websocket.Conn) {
defer func() {
2023-08-28 23:09:29 +01:00
close(done)
2023-08-17 14:38:00 +01:00
}()
for {
mt, message, err := a.ReadMessage()
if err != nil {
log.Println("Websocket read message error: ", err)
2023-08-17 14:38:00 +01:00
return
}
if b.WriteMessage(mt, message) != nil {
return
}
}
}
func (s *Server) Remove(c *websocket.Conn) {
s.connLock.Lock()
delete(s.conns, c.RemoteAddr().String())
s.connLock.Unlock()
_ = c.Close()
}
func (s *Server) Shutdown() {
s.connLock.Lock()
defer s.connLock.Unlock()
// flag shutdown and close all open connections
s.connStop = true
for _, i := range s.conns {
_ = i.Close()
}
// clear connections, not required but do it anyway
s.conns = make(map[string]*websocket.Conn)
}