2019-10-10 21:32:26 -07:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
2019-11-01 13:57:38 -07:00
|
|
|
"bufio"
|
2019-10-10 21:32:26 -07:00
|
|
|
"crypto/tls"
|
2019-10-18 11:29:54 -07:00
|
|
|
"encoding/xml"
|
2019-10-15 11:56:11 -07:00
|
|
|
"errors"
|
2019-10-18 11:29:54 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-10 21:32:26 -07:00
|
|
|
"net"
|
|
|
|
"time"
|
2019-10-18 11:29:54 -07:00
|
|
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-10-10 21:32:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// XMPPTransport implements the XMPP native TCP transport
|
2019-11-22 06:07:40 -08:00
|
|
|
// The decoder is expected to be initialized after connecting to a server.
|
2019-10-10 21:32:26 -07:00
|
|
|
type XMPPTransport struct {
|
2019-10-25 06:22:01 -07:00
|
|
|
openStatement string
|
|
|
|
Config TransportConfiguration
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
decoder *xml.Decoder
|
|
|
|
conn net.Conn
|
|
|
|
readWriter io.ReadWriter
|
|
|
|
logFile io.Writer
|
|
|
|
isSecure bool
|
2019-12-26 05:47:02 -08:00
|
|
|
closeChan chan stanza.StreamClosePacket
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-25 06:22:01 -07:00
|
|
|
var componentStreamOpen = fmt.Sprintf("<?xml version='1.0'?><stream:stream to='%%s' xmlns='%s' xmlns:stream='%s'>", stanza.NSComponent, stanza.NSStream)
|
|
|
|
|
|
|
|
var clientStreamOpen = fmt.Sprintf("<?xml version='1.0'?><stream:stream to='%%s' xmlns='%s' xmlns:stream='%s' version='1.0'>", stanza.NSClient, stanza.NSStream)
|
2019-10-18 11:29:54 -07:00
|
|
|
|
|
|
|
func (t *XMPPTransport) Connect() (string, error) {
|
2019-10-10 21:32:26 -07:00
|
|
|
var err error
|
|
|
|
|
2019-10-10 21:41:15 -07:00
|
|
|
t.conn, err = net.DialTimeout("tcp", t.Config.Address, time.Duration(t.Config.ConnectTimeout)*time.Second)
|
2019-10-12 08:46:53 -07:00
|
|
|
if err != nil {
|
2019-10-18 11:29:54 -07:00
|
|
|
return "", NewConnError(err, true)
|
2019-10-12 08:46:53 -07:00
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
t.closeChan = make(chan stanza.StreamClosePacket)
|
2019-10-21 01:10:18 -07:00
|
|
|
t.readWriter = newStreamLogger(t.conn, t.logFile)
|
2019-11-01 13:57:38 -07:00
|
|
|
t.decoder = xml.NewDecoder(bufio.NewReaderSize(t.readWriter, maxPacketSize))
|
2019-10-25 06:55:27 -07:00
|
|
|
t.decoder.CharsetReader = t.Config.CharsetReader
|
|
|
|
return t.StartStream()
|
2019-10-25 06:22:01 -07:00
|
|
|
}
|
2019-10-21 01:10:18 -07:00
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) StartStream() (string, error) {
|
2019-10-25 06:55:27 -07:00
|
|
|
if _, err := fmt.Fprintf(t, t.openStatement, t.Config.Domain); err != nil {
|
|
|
|
t.Close()
|
2019-10-18 11:29:54 -07:00
|
|
|
return "", NewConnError(err, true)
|
|
|
|
}
|
|
|
|
|
2019-10-25 06:55:27 -07:00
|
|
|
sessionID, err := stanza.InitStream(t.GetDecoder())
|
2019-10-18 11:29:54 -07:00
|
|
|
if err != nil {
|
2019-10-25 06:55:27 -07:00
|
|
|
t.Close()
|
2019-10-18 11:29:54 -07:00
|
|
|
return "", NewConnError(err, false)
|
|
|
|
}
|
2019-10-21 01:10:18 -07:00
|
|
|
return sessionID, nil
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) DoesStartTLS() bool {
|
2019-10-10 21:32:26 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) GetDomain() string {
|
2019-10-25 06:55:27 -07:00
|
|
|
return t.Config.Domain
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) GetDecoder() *xml.Decoder {
|
2019-10-18 11:29:54 -07:00
|
|
|
return t.decoder
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) IsSecure() bool {
|
2019-10-10 22:15:47 -07:00
|
|
|
return t.isSecure
|
|
|
|
}
|
|
|
|
|
2019-10-25 06:55:27 -07:00
|
|
|
func (t *XMPPTransport) StartTLS() error {
|
2019-10-10 21:32:26 -07:00
|
|
|
if t.Config.TLSConfig == nil {
|
2019-10-12 11:47:16 -07:00
|
|
|
t.TLSConfig = &tls.Config{}
|
|
|
|
} else {
|
|
|
|
t.TLSConfig = t.Config.TLSConfig.Clone()
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-12 11:47:16 -07:00
|
|
|
if t.TLSConfig.ServerName == "" {
|
2019-10-18 11:29:54 -07:00
|
|
|
t.TLSConfig.ServerName = t.Config.Domain
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
tlsConn := tls.Client(t.conn, t.TLSConfig)
|
|
|
|
// We convert existing connection to TLS
|
|
|
|
if err := tlsConn.Handshake(); err != nil {
|
2019-10-25 06:55:27 -07:00
|
|
|
return err
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 02:02:23 -07:00
|
|
|
t.conn = tlsConn
|
|
|
|
t.readWriter = newStreamLogger(tlsConn, t.logFile)
|
2019-11-01 13:57:38 -07:00
|
|
|
t.decoder = xml.NewDecoder(bufio.NewReaderSize(t.readWriter, maxPacketSize))
|
2019-10-21 02:02:23 -07:00
|
|
|
t.decoder.CharsetReader = t.Config.CharsetReader
|
|
|
|
|
2019-10-10 21:32:26 -07:00
|
|
|
if !t.TLSConfig.InsecureSkipVerify {
|
2019-10-18 11:29:54 -07:00
|
|
|
if err := tlsConn.VerifyHostname(t.Config.Domain); err != nil {
|
2019-10-25 06:55:27 -07:00
|
|
|
return err
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:15:47 -07:00
|
|
|
t.isSecure = true
|
2019-10-25 06:55:27 -07:00
|
|
|
return nil
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) Ping() error {
|
2019-10-15 11:56:11 -07:00
|
|
|
n, err := t.conn.Write([]byte("\n"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != 1 {
|
2020-01-31 02:48:03 -08:00
|
|
|
return errors.New("could not write ping")
|
2019-10-15 11:56:11 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) Read(p []byte) (n int, err error) {
|
2019-11-08 03:07:55 -08:00
|
|
|
if t.readWriter == nil {
|
|
|
|
return 0, errors.New("cannot read: not connected, no readwriter")
|
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
return t.readWriter.Read(p)
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) Write(p []byte) (n int, err error) {
|
2019-11-08 03:07:55 -08:00
|
|
|
if t.readWriter == nil {
|
|
|
|
return 0, errors.New("cannot write: not connected, no readwriter")
|
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
return t.readWriter.Write(p)
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-12-26 05:47:02 -08:00
|
|
|
func (t *XMPPTransport) Close() error {
|
2019-11-08 03:07:55 -08:00
|
|
|
if t.readWriter != nil {
|
2019-12-26 05:47:02 -08:00
|
|
|
_, _ = t.readWriter.Write([]byte(stanza.StreamClose))
|
2019-11-08 03:07:55 -08:00
|
|
|
}
|
2019-12-26 05:47:02 -08:00
|
|
|
|
|
|
|
// Try to wait for the stream close tag from the server. After a timeout, disconnect anyway.
|
|
|
|
select {
|
|
|
|
case <-t.closeChan:
|
|
|
|
case <-time.After(time.Duration(t.Config.ConnectTimeout) * time.Second):
|
|
|
|
}
|
|
|
|
|
2019-11-08 03:07:55 -08:00
|
|
|
if t.conn != nil {
|
|
|
|
return t.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
|
|
|
|
func (t *XMPPTransport) LogTraffic(logFile io.Writer) {
|
2019-10-21 01:10:18 -07:00
|
|
|
t.logFile = logFile
|
2019-10-18 11:29:54 -07:00
|
|
|
}
|
2019-12-26 05:47:02 -08:00
|
|
|
|
|
|
|
func (t *XMPPTransport) ReceivedStreamClose() {
|
|
|
|
t.closeChan <- stanza.StreamClosePacket{}
|
|
|
|
}
|