2019-10-06 10:37:56 -07:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-10-10 22:15:47 -07:00
|
|
|
"errors"
|
2019-10-10 22:19:55 -07:00
|
|
|
"strings"
|
2019-10-06 10:37:56 -07:00
|
|
|
)
|
|
|
|
|
2019-10-10 22:15:47 -07:00
|
|
|
var TLSNotSupported = errors.New("Transport does not support StartTLS")
|
|
|
|
|
2019-10-10 21:24:27 -07:00
|
|
|
type TransportConfiguration struct {
|
2019-10-10 21:41:15 -07: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-10 21:24:27 -07: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.
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:37:56 -07:00
|
|
|
type Transport interface {
|
2019-10-10 21:41:15 -07:00
|
|
|
Connect() error
|
2019-10-06 10:37:56 -07:00
|
|
|
DoesStartTLS() bool
|
2019-10-10 21:24:27 -07:00
|
|
|
StartTLS(domain string) error
|
2019-10-06 10:37:56 -07:00
|
|
|
|
2019-10-10 22:15:47 -07:00
|
|
|
IsSecure() bool
|
|
|
|
|
2019-10-06 10:37:56 -07:00
|
|
|
Read(p []byte) (n int, err error)
|
|
|
|
Write(p []byte) (n int, err error)
|
|
|
|
Close() error
|
|
|
|
}
|
2019-10-10 21:41:15 -07:00
|
|
|
|
2019-10-10 22:19:55 -07:00
|
|
|
// NewTransport creates a new Transport instance.
|
|
|
|
// 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-10 21:41:15 -07:00
|
|
|
func NewTransport(config TransportConfiguration) Transport {
|
2019-10-10 22:19:55 -07:00
|
|
|
if strings.HasPrefix(config.Address, "ws:") || strings.HasPrefix(config.Address, "wss:") {
|
|
|
|
return &WebsocketTransport{Config: config}
|
|
|
|
}
|
2019-10-12 06:49:22 -07:00
|
|
|
|
|
|
|
config.Address = ensurePort(config.Address, 5222)
|
2019-10-10 21:41:15 -07:00
|
|
|
return &XMPPTransport{Config: config}
|
|
|
|
}
|