2023-01-11 06:38:48 +00:00
|
|
|
package barertc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/config"
|
2023-01-11 06:38:48 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/log"
|
2023-08-14 02:21:27 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/messages"
|
2023-02-09 04:01:06 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/util"
|
2023-01-11 06:38:48 +00:00
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
// WebSocket handles the /ws websocket connection endpoint.
|
2023-01-11 06:38:48 +00:00
|
|
|
func (s *Server) WebSocket() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-02-09 04:01:06 +00:00
|
|
|
ip := util.IPAddress(r)
|
|
|
|
log.Info("WebSocket connection from %s - %s", ip, r.Header.Get("User-Agent"))
|
|
|
|
log.Debug("Headers: %+v", r.Header)
|
2023-03-31 19:40:55 +00:00
|
|
|
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
|
|
|
CompressionMode: websocket.CompressionDisabled,
|
|
|
|
})
|
2023-01-11 06:38:48 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-02-09 04:01:06 +00:00
|
|
|
fmt.Fprintf(w, "Could not accept websocket connection: %s", err)
|
2023-01-11 06:38:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close(websocket.StatusInternalError, "the sky is falling")
|
|
|
|
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Debug("WebSocket: %s has connected", ip)
|
2023-03-22 04:29:24 +00:00
|
|
|
c.SetReadLimit(config.Current.WebSocketReadLimit)
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
// CloseRead starts a goroutine that will read from the connection
|
|
|
|
// until it is closed.
|
|
|
|
// ctx := c.CloseRead(r.Context())
|
2023-02-05 05:00:01 +00:00
|
|
|
ctx, cancel := context.WithCancel(r.Context())
|
2023-01-11 06:38:48 +00:00
|
|
|
|
2023-12-11 02:43:18 +00:00
|
|
|
sub := s.NewWebSocketSubscriber(ctx, c, cancel)
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
s.AddSubscriber(sub)
|
2023-02-09 04:01:06 +00:00
|
|
|
defer s.DeleteSubscriber(sub)
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
go sub.ReadLoop(s)
|
2023-02-05 05:00:01 +00:00
|
|
|
pinger := time.NewTicker(PingInterval)
|
2023-01-11 06:38:48 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-sub.messages:
|
2023-09-30 19:46:45 +00:00
|
|
|
err = writeTimeout(ctx, time.Second*time.Duration(config.Current.WebSocketSendTimeout), c, msg)
|
2023-01-11 06:38:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-11 06:46:39 +00:00
|
|
|
case <-pinger.C:
|
2023-04-20 02:55:39 +00:00
|
|
|
// Send a ping, and a refreshed JWT token if the user sent one.
|
|
|
|
var token string
|
|
|
|
if sub.JWTClaims != nil {
|
|
|
|
if jwt, err := sub.JWTClaims.ReSign(); err != nil {
|
2023-10-24 02:05:02 +00:00
|
|
|
log.Error("ReSign JWT token for %s#%d: %s", sub.Username, sub.ID, err)
|
2023-04-20 02:55:39 +00:00
|
|
|
} else {
|
|
|
|
token = jwt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 02:21:27 +00:00
|
|
|
sub.SendJSON(messages.Message{
|
|
|
|
Action: messages.ActionPing,
|
2023-04-20 02:55:39 +00:00
|
|
|
JWTToken: token,
|
2023-02-05 05:00:01 +00:00
|
|
|
})
|
2023-01-11 06:38:48 +00:00
|
|
|
case <-ctx.Done():
|
2023-02-05 05:00:01 +00:00
|
|
|
pinger.Stop()
|
2023-01-11 06:38:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
return c.Write(ctx, websocket.MessageText, msg)
|
|
|
|
}
|