2019-10-06 18:15:26 +00:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
2019-11-01 20:57:38 +00:00
|
|
|
"bufio"
|
2019-10-06 18:15:26 +00:00
|
|
|
"context"
|
2019-10-18 18:29:54 +00:00
|
|
|
"encoding/xml"
|
2019-10-16 12:44:22 +00:00
|
|
|
"errors"
|
2019-10-18 18:29:54 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-06 18:15:26 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-10-06 18:15:26 +00:00
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
const maxPacketSize = 32768
|
|
|
|
|
2019-10-15 18:56:11 +00:00
|
|
|
const pingTimeout = time.Duration(5) * time.Second
|
|
|
|
|
2019-10-16 12:44:22 +00:00
|
|
|
var ServerDoesNotSupportXmppOverWebsocket = errors.New("The websocket server does not support the xmpp subprotocol")
|
|
|
|
|
2019-11-22 14:07:40 +00:00
|
|
|
// The decoder is expected to be initialized after connecting to a server.
|
2019-10-06 18:15:26 +00:00
|
|
|
type WebsocketTransport struct {
|
2019-10-11 04:41:15 +00:00
|
|
|
Config TransportConfiguration
|
2019-10-18 18:29:54 +00:00
|
|
|
decoder *xml.Decoder
|
2019-10-06 18:15:26 +00:00
|
|
|
wsConn *websocket.Conn
|
2019-10-21 06:57:44 +00:00
|
|
|
queue chan []byte
|
2019-10-18 18:29:54 +00:00
|
|
|
logFile io.Writer
|
2019-10-21 06:57:44 +00:00
|
|
|
|
|
|
|
closeCtx context.Context
|
|
|
|
closeFunc context.CancelFunc
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
func (t *WebsocketTransport) Connect() (string, error) {
|
2019-10-21 06:57:44 +00:00
|
|
|
t.queue = make(chan []byte, 256)
|
|
|
|
t.closeCtx, t.closeFunc = context.WithCancel(context.Background())
|
2019-10-06 18:15:26 +00:00
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
var ctx context.Context
|
|
|
|
ctx = context.Background()
|
2019-10-12 15:50:00 +00:00
|
|
|
if t.Config.ConnectTimeout > 0 {
|
2019-10-21 06:57:44 +00:00
|
|
|
var cancelConnect context.CancelFunc
|
|
|
|
ctx, cancelConnect = context.WithTimeout(t.closeCtx, time.Duration(t.Config.ConnectTimeout)*time.Second)
|
|
|
|
defer cancelConnect()
|
2019-10-12 15:50:00 +00:00
|
|
|
}
|
2019-10-06 18:15:26 +00:00
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
wsConn, response, err := websocket.Dial(ctx, t.Config.Address, &websocket.DialOptions{
|
2019-10-16 12:44:22 +00:00
|
|
|
Subprotocols: []string{"xmpp"},
|
|
|
|
})
|
2019-10-12 15:46:53 +00:00
|
|
|
if err != nil {
|
2019-10-18 18:29:54 +00:00
|
|
|
return "", NewConnError(err, true)
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
2019-10-16 12:44:22 +00:00
|
|
|
if response.Header.Get("Sec-WebSocket-Protocol") != "xmpp" {
|
2019-10-21 06:57:44 +00:00
|
|
|
t.cleanup(websocket.StatusBadGateway)
|
2019-10-18 18:29:54 +00:00
|
|
|
return "", NewConnError(ServerDoesNotSupportXmppOverWebsocket, true)
|
2019-10-16 12:44:22 +00:00
|
|
|
}
|
2019-10-18 18:29:54 +00:00
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
wsConn.SetReadLimit(maxPacketSize)
|
2019-10-12 15:46:53 +00:00
|
|
|
t.wsConn = wsConn
|
2019-10-21 06:57:44 +00:00
|
|
|
t.startReader()
|
2019-10-18 18:29:54 +00:00
|
|
|
|
2019-11-01 20:57:38 +00:00
|
|
|
t.decoder = xml.NewDecoder(bufio.NewReaderSize(t, maxPacketSize))
|
2019-10-25 13:55:27 +00:00
|
|
|
t.decoder.CharsetReader = t.Config.CharsetReader
|
2019-10-18 18:29:54 +00:00
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
return t.StartStream()
|
|
|
|
}
|
2019-10-21 06:57:44 +00:00
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
func (t WebsocketTransport) StartStream() (string, error) {
|
|
|
|
if _, err := fmt.Fprintf(t, `<open xmlns="urn:ietf:params:xml:ns:xmpp-framing" to="%s" version="1.0" />`, t.Config.Domain); err != nil {
|
|
|
|
t.cleanup(websocket.StatusBadGateway)
|
|
|
|
return "", NewConnError(err, true)
|
2019-10-18 18:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
sessionID, err := stanza.InitStream(t.GetDecoder())
|
|
|
|
if err != nil {
|
|
|
|
t.Close()
|
2019-10-18 18:29:54 +00:00
|
|
|
return "", NewConnError(err, false)
|
|
|
|
}
|
2019-10-25 13:55:27 +00:00
|
|
|
return sessionID, nil
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
// startReader runs a go function that keeps reading from the websocket. This
|
|
|
|
// is required to allow Ping() to work: Ping requires a Reader to be running
|
|
|
|
// to process incoming control frames.
|
|
|
|
func (t WebsocketTransport) startReader() {
|
|
|
|
go func() {
|
|
|
|
buffer := make([]byte, maxPacketSize)
|
|
|
|
for {
|
|
|
|
_, reader, err := t.wsConn.Reader(t.closeCtx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := reader.Read(buffer)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
// We need to make a copy, otherwise we will overwrite the slice content
|
|
|
|
// on the next iteration of the for loop.
|
2019-10-21 08:05:43 +00:00
|
|
|
tmp := make([]byte, n)
|
2019-10-21 06:57:44 +00:00
|
|
|
copy(tmp, buffer)
|
|
|
|
t.queue <- tmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
func (t WebsocketTransport) StartTLS() error {
|
|
|
|
return ErrTLSNotSupported
|
2019-10-11 05:15:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 18:15:26 +00:00
|
|
|
func (t WebsocketTransport) DoesStartTLS() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
func (t WebsocketTransport) GetDomain() string {
|
|
|
|
return t.Config.Domain
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
func (t WebsocketTransport) GetDecoder() *xml.Decoder {
|
|
|
|
return t.decoder
|
|
|
|
}
|
|
|
|
|
2019-10-11 05:15:47 +00:00
|
|
|
func (t WebsocketTransport) IsSecure() bool {
|
|
|
|
return strings.HasPrefix(t.Config.Address, "wss:")
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:56:11 +00:00
|
|
|
func (t WebsocketTransport) Ping() error {
|
2019-10-21 06:57:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(t.closeCtx, pingTimeout)
|
2019-10-15 18:56:11 +00:00
|
|
|
defer cancel()
|
2019-10-18 18:29:54 +00:00
|
|
|
return t.wsConn.Ping(ctx)
|
2019-10-15 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
func (t *WebsocketTransport) Read(p []byte) (int, error) {
|
|
|
|
select {
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
return 0, t.closeCtx.Err()
|
|
|
|
case data := <-t.queue:
|
|
|
|
if t.logFile != nil && len(data) > 0 {
|
|
|
|
_, _ = fmt.Fprintf(t.logFile, "RECV:\n%s\n\n", data)
|
|
|
|
}
|
|
|
|
copy(p, data)
|
|
|
|
return len(data), nil
|
2019-10-18 18:29:54 +00:00
|
|
|
}
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 06:57:44 +00:00
|
|
|
func (t WebsocketTransport) Write(p []byte) (int, error) {
|
2019-10-18 18:29:54 +00:00
|
|
|
if t.logFile != nil {
|
|
|
|
_, _ = fmt.Fprintf(t.logFile, "SEND:\n%s\n\n", p)
|
|
|
|
}
|
2019-10-21 06:57:44 +00:00
|
|
|
return len(p), t.wsConn.Write(t.closeCtx, websocket.MessageText, p)
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t WebsocketTransport) Close() error {
|
2019-10-18 18:29:54 +00:00
|
|
|
t.Write([]byte("<close xmlns=\"urn:ietf:params:xml:ns:xmpp-framing\" />"))
|
2019-10-25 13:55:27 +00:00
|
|
|
return t.cleanup(websocket.StatusGoingAway)
|
2019-10-06 18:15:26 +00:00
|
|
|
}
|
2019-10-18 18:29:54 +00:00
|
|
|
|
|
|
|
func (t *WebsocketTransport) LogTraffic(logFile io.Writer) {
|
|
|
|
t.logFile = logFile
|
|
|
|
}
|
2019-10-21 06:57:44 +00:00
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
func (t *WebsocketTransport) cleanup(code websocket.StatusCode) error {
|
|
|
|
var err error
|
2019-10-21 06:57:44 +00:00
|
|
|
if t.queue != nil {
|
|
|
|
close(t.queue)
|
|
|
|
t.queue = nil
|
|
|
|
}
|
|
|
|
if t.wsConn != nil {
|
2019-10-25 13:55:27 +00:00
|
|
|
err = t.wsConn.Close(websocket.StatusGoingAway, "Done")
|
2019-10-21 06:57:44 +00:00
|
|
|
t.wsConn = nil
|
|
|
|
}
|
|
|
|
if t.closeFunc != nil {
|
|
|
|
t.closeFunc()
|
|
|
|
t.closeFunc = nil
|
|
|
|
t.closeCtx = nil
|
|
|
|
}
|
2019-10-25 13:55:27 +00:00
|
|
|
return err
|
2019-10-21 06:57:44 +00:00
|
|
|
}
|