2020-12-09 21:46:53 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2021-03-09 15:57:04 +00:00
|
|
|
"bytes"
|
2020-12-09 21:46:53 +00:00
|
|
|
"context"
|
2021-10-19 19:01:17 +00:00
|
|
|
"errors"
|
2021-03-09 15:57:04 +00:00
|
|
|
"fmt"
|
2020-12-09 21:46:53 +00:00
|
|
|
"io"
|
2021-10-19 19:01:17 +00:00
|
|
|
"sync"
|
2020-12-09 21:46:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
gobwas "github.com/gobwas/ws"
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
|
|
|
"github.com/gorilla/websocket"
|
2021-03-09 15:57:04 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-12-09 21:46:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
2021-09-22 16:33:05 +00:00
|
|
|
defaultPongWait = 60 * time.Second
|
2020-12-09 21:46:53 +00:00
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
2021-09-22 16:33:05 +00:00
|
|
|
defaultPingPeriod = (defaultPongWait * 9) / 10
|
|
|
|
|
|
|
|
PingPeriodContextKey = PingPeriodContext("pingPeriod")
|
2020-12-09 21:46:53 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 16:33:05 +00:00
|
|
|
type PingPeriodContext string
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// GorillaConn is a wrapper around the standard gorilla websocket but implements a ReadWriter
|
|
|
|
// This is still used by access carrier
|
|
|
|
type GorillaConn struct {
|
|
|
|
*websocket.Conn
|
2021-03-09 15:57:04 +00:00
|
|
|
log *zerolog.Logger
|
|
|
|
readBuf bytes.Buffer
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read will read messages from the websocket connection
|
|
|
|
func (c *GorillaConn) Read(p []byte) (int, error) {
|
2021-03-09 15:57:04 +00:00
|
|
|
// Intermediate buffer may contain unread bytes from the last read, start there before blocking on a new frame
|
|
|
|
if c.readBuf.Len() > 0 {
|
|
|
|
return c.readBuf.Read(p)
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
_, message, err := c.Conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:57:04 +00:00
|
|
|
copied := copy(p, message)
|
|
|
|
|
|
|
|
// Write unread bytes to readBuf; if everything was read this is a no-op
|
|
|
|
// Write returns a nil error always and grows the buffer; everything is always written or panic
|
|
|
|
c.readBuf.Write(message[copied:])
|
|
|
|
|
|
|
|
return copied, nil
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write will write messages to the websocket connection
|
|
|
|
func (c *GorillaConn) Write(p []byte) (int, error) {
|
|
|
|
if err := c.Conn.WriteMessage(websocket.BinaryMessage, p); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:57:04 +00:00
|
|
|
// SetDeadline sets both read and write deadlines, as per net.Conn interface docs:
|
|
|
|
// "It is equivalent to calling both SetReadDeadline and SetWriteDeadline."
|
|
|
|
// Note there is no synchronization here, but the gorilla implementation isn't thread safe anyway
|
|
|
|
func (c *GorillaConn) SetDeadline(t time.Time) error {
|
|
|
|
if err := c.Conn.SetReadDeadline(t); err != nil {
|
|
|
|
return fmt.Errorf("error setting read deadline: %w", err)
|
|
|
|
}
|
|
|
|
if err := c.Conn.SetWriteDeadline(t); err != nil {
|
|
|
|
return fmt.Errorf("error setting write deadline: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
type Conn struct {
|
|
|
|
rw io.ReadWriter
|
|
|
|
log *zerolog.Logger
|
2021-10-19 19:01:17 +00:00
|
|
|
// writeLock makes sure
|
|
|
|
// 1. Only one write at a time. The pinger and Stream function can both call write.
|
|
|
|
// 2. Close only returns after in progress Write is finished, and no more Write will succeed after calling Close.
|
|
|
|
writeLock sync.Mutex
|
|
|
|
done bool
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func NewConn(ctx context.Context, rw io.ReadWriter, log *zerolog.Logger) *Conn {
|
|
|
|
c := &Conn{
|
2021-10-19 19:01:17 +00:00
|
|
|
rw: rw,
|
|
|
|
log: log,
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2021-02-02 18:27:50 +00:00
|
|
|
go c.pinger(ctx)
|
|
|
|
return c
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read will read messages from the websocket connection
|
|
|
|
func (c *Conn) Read(reader []byte) (int, error) {
|
|
|
|
data, err := wsutil.ReadClientBinary(c.rw)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return copy(reader, data), nil
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
// Write will write messages to the websocket connection.
|
|
|
|
// It will not write to the connection after Close is called to fix TUN-5184
|
2020-12-09 21:46:53 +00:00
|
|
|
func (c *Conn) Write(p []byte) (int, error) {
|
2021-10-19 19:01:17 +00:00
|
|
|
c.writeLock.Lock()
|
|
|
|
defer c.writeLock.Unlock()
|
|
|
|
if c.done {
|
|
|
|
return 0, errors.New("write to closed websocket connection")
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
if err := wsutil.WriteServerBinary(c.rw, p); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-10-19 19:01:17 +00:00
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:27:50 +00:00
|
|
|
func (c *Conn) pinger(ctx context.Context) {
|
2020-12-09 21:46:53 +00:00
|
|
|
pongMessge := wsutil.Message{
|
|
|
|
OpCode: gobwas.OpPong,
|
|
|
|
Payload: []byte{},
|
|
|
|
}
|
2021-09-22 16:33:05 +00:00
|
|
|
|
|
|
|
ticker := time.NewTicker(c.pingPeriod(ctx))
|
2020-12-09 21:46:53 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2021-10-19 19:01:17 +00:00
|
|
|
done, err := c.ping()
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-09-22 16:33:05 +00:00
|
|
|
c.log.Debug().Err(err).Msgf("failed to write ping message")
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2021-10-25 18:51:52 +00:00
|
|
|
if err := wsutil.HandleClientControlMessage(c.rw, pongMessge); err != nil {
|
2021-09-22 16:33:05 +00:00
|
|
|
c.log.Debug().Err(err).Msgf("failed to write pong message")
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 16:33:05 +00:00
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
func (c *Conn) ping() (bool, error) {
|
|
|
|
c.writeLock.Lock()
|
|
|
|
defer c.writeLock.Unlock()
|
|
|
|
|
|
|
|
if c.done {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, wsutil.WriteServerMessage(c.rw, gobwas.OpPing, []byte{})
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:33:05 +00:00
|
|
|
func (c *Conn) pingPeriod(ctx context.Context) time.Duration {
|
|
|
|
if val := ctx.Value(PingPeriodContextKey); val != nil {
|
|
|
|
if period, ok := val.(time.Duration); ok {
|
|
|
|
return period
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultPingPeriod
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
// Close waits for the current write to finish. Further writes will return error
|
|
|
|
func (c *Conn) Close() {
|
|
|
|
c.writeLock.Lock()
|
|
|
|
defer c.writeLock.Unlock()
|
|
|
|
c.done = true
|
2021-09-22 16:33:05 +00:00
|
|
|
}
|