2019-10-06 17:37:56 +00:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-10-18 18:29:54 +00:00
|
|
|
"encoding/xml"
|
2019-10-11 05:15:47 +00:00
|
|
|
"errors"
|
2019-10-25 13:22:01 +00:00
|
|
|
"fmt"
|
2019-10-18 18:29:54 +00:00
|
|
|
"io"
|
2019-10-11 05:19:55 +00:00
|
|
|
"strings"
|
2019-10-06 17:37:56 +00:00
|
|
|
)
|
|
|
|
|
2019-10-25 13:22:01 +00:00
|
|
|
var ErrTransportProtocolNotSupported = errors.New("Transport protocol not supported")
|
|
|
|
var ErrTLSNotSupported = errors.New("Transport does not support StartTLS")
|
2019-10-11 05:15:47 +00:00
|
|
|
|
2019-11-04 11:58:10 +00:00
|
|
|
// TODO: rename to transport config?
|
2019-10-11 04:24:27 +00:00
|
|
|
type TransportConfiguration struct {
|
2019-10-11 04:41:15 +00:00
|
|
|
// Address is the XMPP Host and port to connect to. Host is of
|
|
|
|
// the form 'serverhost:port' i.e "localhost:8888"
|
|
|
|
Address string
|
2019-10-18 18:29:54 +00:00
|
|
|
Domain string
|
2019-10-11 04:24:27 +00:00
|
|
|
ConnectTimeout int // Client timeout in seconds. Default to 15
|
|
|
|
// tls.Config must not be modified after having been passed to NewClient. Any
|
|
|
|
// changes made after connecting are ignored.
|
2019-10-18 18:29:54 +00:00
|
|
|
TLSConfig *tls.Config
|
|
|
|
CharsetReader func(charset string, input io.Reader) (io.Reader, error) // passed to xml decoder
|
2019-10-11 04:24:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 17:37:56 +00:00
|
|
|
type Transport interface {
|
2019-10-18 18:29:54 +00:00
|
|
|
Connect() (string, error)
|
2019-10-06 17:37:56 +00:00
|
|
|
DoesStartTLS() bool
|
2019-10-25 13:55:27 +00:00
|
|
|
StartTLS() error
|
2019-10-06 17:37:56 +00:00
|
|
|
|
2019-10-18 18:29:54 +00:00
|
|
|
LogTraffic(logFile io.Writer)
|
|
|
|
|
2019-10-25 13:55:27 +00:00
|
|
|
StartStream() (string, error)
|
2019-10-18 18:29:54 +00:00
|
|
|
GetDecoder() *xml.Decoder
|
2019-10-11 05:15:47 +00:00
|
|
|
IsSecure() bool
|
|
|
|
|
2019-10-15 18:56:11 +00:00
|
|
|
Ping() error
|
2019-10-06 17:37:56 +00:00
|
|
|
Read(p []byte) (n int, err error)
|
|
|
|
Write(p []byte) (n int, err error)
|
|
|
|
Close() error
|
2019-12-26 13:47:02 +00:00
|
|
|
// ReceivedStreamClose signals to the transport that a </stream:stream> has been received and that the tcp connection
|
|
|
|
// should be closed.
|
|
|
|
ReceivedStreamClose()
|
2019-10-06 17:37:56 +00:00
|
|
|
}
|
2019-10-11 04:41:15 +00:00
|
|
|
|
2019-10-25 13:22:01 +00:00
|
|
|
// NewClientTransport creates a new Transport instance for clients.
|
2019-10-11 05:19:55 +00:00
|
|
|
// The type of transport is determined by the address in the configuration:
|
|
|
|
// - if the address is a URL with the `ws` or `wss` scheme WebsocketTransport is used
|
|
|
|
// - in all other cases a XMPPTransport is used
|
|
|
|
// For XMPPTransport it is mandatory for the address to have a port specified.
|
2019-10-25 13:22:01 +00:00
|
|
|
func NewClientTransport(config TransportConfiguration) Transport {
|
2019-10-11 05:19:55 +00:00
|
|
|
if strings.HasPrefix(config.Address, "ws:") || strings.HasPrefix(config.Address, "wss:") {
|
|
|
|
return &WebsocketTransport{Config: config}
|
|
|
|
}
|
2019-10-12 13:49:22 +00:00
|
|
|
|
|
|
|
config.Address = ensurePort(config.Address, 5222)
|
2019-10-25 13:22:01 +00:00
|
|
|
return &XMPPTransport{
|
|
|
|
Config: config,
|
|
|
|
openStatement: clientStreamOpen,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewComponentTransport creates a new Transport instance for components.
|
|
|
|
// Only XMPP transports are allowed. If you try to use any other protocol an error
|
|
|
|
// will be returned.
|
|
|
|
func NewComponentTransport(config TransportConfiguration) (Transport, error) {
|
|
|
|
if strings.HasPrefix(config.Address, "ws:") || strings.HasPrefix(config.Address, "wss:") {
|
|
|
|
return nil, fmt.Errorf("Components only support XMPP transport: %w", ErrTransportProtocolNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Address = ensurePort(config.Address, 5222)
|
|
|
|
return &XMPPTransport{
|
|
|
|
Config: config,
|
|
|
|
openStatement: componentStreamOpen,
|
|
|
|
}, nil
|
2019-10-11 04:41:15 +00:00
|
|
|
}
|