telebit/websocket.go

222 lines
6.4 KiB
Go
Raw Normal View History

2020-05-22 10:41:24 +00:00
package telebit
2020-05-21 10:29:05 +00:00
import (
2020-05-21 18:28:33 +00:00
"context"
2020-05-21 10:29:05 +00:00
"fmt"
"io"
"net"
2020-05-21 18:28:33 +00:00
"net/http"
2020-05-21 10:29:05 +00:00
"os"
2020-05-21 18:28:33 +00:00
"strings"
2020-05-21 10:29:05 +00:00
"time"
2020-07-20 22:20:59 +00:00
"git.rootprojects.org/root/telebit/dbg"
2020-05-21 10:29:05 +00:00
"github.com/gorilla/websocket"
)
var defaultReadWait = 20 * time.Second
var defaultWriteWait = 20 * time.Second
2020-05-21 18:28:33 +00:00
// WebsocketTunnel wraps a websocket.Conn instance to behave like net.Conn.
type WebsocketTunnel struct {
wsconn WSConn
readWait time.Duration
writeWait time.Duration
tmpr io.Reader
2020-05-21 10:29:05 +00:00
//w io.WriteCloser
//pingCh chan struct{}
}
// WSConn defines a interface for gorilla websockets for the purpose of testing
type WSConn interface {
NextReader() (messageType int, r io.Reader, err error)
NextWriter(messageType int) (io.WriteCloser, error)
WriteControl(messageType int, data []byte, deadline time.Time) error
WriteMessage(messageType int, data []byte) error
SetPongHandler(h func(appData string) error)
2020-05-21 10:29:05 +00:00
SetReadDeadline(t time.Time) error
2020-05-21 18:28:33 +00:00
SetWriteDeadline(t time.Time) error
2020-05-21 10:29:05 +00:00
Close() error
RemoteAddr() net.Addr
// LocalAddr() net.Addr
}
2020-05-21 18:28:33 +00:00
// NewWebsocketTunnel allocates a new websocket connection wrapper
func NewWebsocketTunnel(wsconn WSConn) net.Conn {
// TODO only set ping when SetReadDeadline would otherwise fail
// See https://github.com/gorilla/websocket/blob/a6870891/examples/chat/conn.go#L86
writeWait := defaultWriteWait
readWait := defaultReadWait
go func() {
// Ping every 15 seconds, or stop listening
for {
time.Sleep(15 * time.Second)
deadline := time.Now().Add(writeWait)
// https://www.gorillatoolkit.org/pkg/websocket
// "The Close and WriteControl methods can be called concurrently with all other methods."
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] sending ping (set write deadline %s)\n", writeWait)
}
if err := wsconn.WriteControl(websocket.PingMessage, []byte(""), deadline); nil != err {
wsconn.Close()
fmt.Fprintf(os.Stderr, "failed to write ping message to websocket: %s\n", err)
break
}
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] sent ping (cleared write deadline)\n")
}
}
}()
wsconn.SetPongHandler(func(pong string) error {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] received pong (reset read deadline %s): %q\n", readWait, pong)
}
wsconn.SetReadDeadline(time.Now().Add(readWait))
return nil
})
2020-05-21 18:28:33 +00:00
return &WebsocketTunnel{
wsconn: wsconn,
readWait: readWait,
writeWait: writeWait,
tmpr: nil,
2020-05-21 11:09:49 +00:00
}
}
2020-05-21 18:28:33 +00:00
// DialWebsocketTunnel connects to the given websocket relay as wraps it as net.Conn
func DialWebsocketTunnel(ctx context.Context, relay, authz string) (net.Conn, error) {
wsd := websocket.Dialer{}
headers := http.Header{}
headers.Set("Authorization", fmt.Sprintf("Bearer %s", authz))
// *http.Response
sep := "?"
if strings.Contains(relay, sep) {
sep = "&"
}
wsconn, _, err := wsd.DialContext(ctx, relay+sep+"access_token="+authz+"&versions=v1", headers)
2020-07-15 09:33:23 +00:00
if nil != err {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] simple dial failed %q %v %v\n", err, wsconn, ctx)
}
2020-07-15 09:33:23 +00:00
}
2020-05-21 18:28:33 +00:00
return NewWebsocketTunnel(wsconn), err
}
func (wsw *WebsocketTunnel) Read(b []byte) (int, error) {
wsw.wsconn.SetReadDeadline(time.Now().Add(wsw.readWait))
2020-05-21 10:29:05 +00:00
if nil == wsw.tmpr {
_, msgr, err := wsw.wsconn.NextReader()
if nil != err {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] NextReader err: %q\n", err)
}
2020-05-21 10:29:05 +00:00
return 0, err
}
wsw.tmpr = msgr
}
n, err := wsw.tmpr.Read(b)
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] Read %d %v\n", n, dbg.Trunc(b, n))
}
2020-05-21 10:29:05 +00:00
if nil != err {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] Read (EOF=WS packet complete) err: %q\n", err)
}
2020-05-21 10:29:05 +00:00
if io.EOF == err {
wsw.tmpr = nil
// ignore the message EOF because it's not the websocket EOF
err = nil
}
}
return n, err
}
2020-05-21 18:28:33 +00:00
func (wsw *WebsocketTunnel) Write(b []byte) (int, error) {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] Write %d\n", len(b))
}
2020-05-21 10:29:05 +00:00
// TODO create or reset ping deadline
// TODO document that more complete writes are preferred?
wsw.wsconn.SetWriteDeadline(time.Now().Add(wsw.writeWait))
2020-05-21 10:29:05 +00:00
msgw, err := wsw.wsconn.NextWriter(websocket.BinaryMessage)
if nil != err {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] NextWriter err: %q\n", err)
}
2020-05-21 10:29:05 +00:00
return 0, err
}
n, err := msgw.Write(b)
if nil != err {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] Write err: %q\n", err)
}
2020-05-21 10:29:05 +00:00
return n, err
}
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] Write n %d = %d\n", n, len(b))
}
2020-05-21 10:29:05 +00:00
// if the message error fails, we can assume the websocket is damaged
return n, msgw.Close()
}
// Close will close the websocket with a control message
2020-05-21 18:28:33 +00:00
func (wsw *WebsocketTunnel) Close() error {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] closing the websocket.Conn\n")
}
2020-05-21 10:29:05 +00:00
// TODO handle EOF as websocket.CloseNormal?
message := websocket.FormatCloseMessage(websocket.CloseGoingAway, "closing connection")
deadline := time.Now().Add(10 * time.Second)
err := wsw.wsconn.WriteControl(websocket.CloseMessage, message, deadline)
if nil != err {
fmt.Fprintf(os.Stderr, "failed to write close message to websocket: %s\n", err)
}
_ = wsw.wsconn.Close()
return err
}
2020-05-21 11:09:49 +00:00
2020-05-21 18:28:33 +00:00
// LocalAddr is not implemented and will panic
func (wsw *WebsocketTunnel) LocalAddr() net.Addr {
// TODO do we reverse this since the "local" address is that of the relay?
// return wsw.wsconn.RemoteAddr()
panic("no LocalAddr() implementation")
2020-05-21 11:09:49 +00:00
}
2020-05-21 18:28:33 +00:00
// RemoteAddr is not implemented and will panic. Additionally, it wouldn't mean anything useful anyway.
func (wsw *WebsocketTunnel) RemoteAddr() net.Addr {
// TODO do we reverse this since the "remote" address means nothing / is that of one of the clients?
// return wsw.wsconn.LocalAddr()
panic("no RemoteAddr() implementation")
2020-05-21 11:09:49 +00:00
}
// SetDeadline sets the read and write deadlines associated
2020-05-21 18:28:33 +00:00
func (wsw *WebsocketTunnel) SetDeadline(t time.Time) error {
err := wsw.SetReadDeadline(t)
if nil == err {
err = wsw.SetWriteDeadline(t)
}
return err
2020-05-21 11:09:49 +00:00
}
// SetReadDeadline sets the deadline for future Read calls
2020-05-21 18:28:33 +00:00
func (wsw *WebsocketTunnel) SetReadDeadline(t time.Time) error {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] read deadline\n")
}
2020-05-21 18:28:33 +00:00
return wsw.wsconn.SetReadDeadline(t)
2020-05-21 11:09:49 +00:00
}
// SetWriteDeadline sets the deadline for future Write calls
2020-05-21 18:28:33 +00:00
func (wsw *WebsocketTunnel) SetWriteDeadline(t time.Time) error {
if dbg.Debug {
fmt.Fprintf(os.Stderr, "[debug] [wstun] write deadline\n")
}
2020-05-21 18:28:33 +00:00
return wsw.wsconn.SetWriteDeadline(t)
2020-05-21 11:09:49 +00:00
}