2019-10-10 21:32:26 -07:00
|
|
|
package xmpp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// XMPPTransport implements the XMPP native TCP transport
|
|
|
|
type XMPPTransport struct {
|
|
|
|
Config TransportConfiguration
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
// TCP level connection / can be replaced by a TLS session after starttls
|
2019-10-10 22:15:47 -07:00
|
|
|
conn net.Conn
|
|
|
|
isSecure bool
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-10 21:41:15 -07:00
|
|
|
func (t *XMPPTransport) Connect() 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 {
|
|
|
|
return NewConnError(err, true)
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-10 21:32:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t XMPPTransport) DoesStartTLS() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:15:47 -07:00
|
|
|
func (t XMPPTransport) IsSecure() bool {
|
|
|
|
return t.isSecure
|
|
|
|
}
|
|
|
|
|
2019-10-10 21:32:26 -07:00
|
|
|
func (t *XMPPTransport) StartTLS(domain string) error {
|
|
|
|
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 == "" {
|
|
|
|
t.TLSConfig.ServerName = 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !t.TLSConfig.InsecureSkipVerify {
|
|
|
|
if err := tlsConn.VerifyHostname(domain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:15:47 -07:00
|
|
|
t.isSecure = true
|
2019-10-10 21:32:26 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t XMPPTransport) Read(p []byte) (n int, err error) {
|
|
|
|
return t.conn.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t XMPPTransport) Write(p []byte) (n int, err error) {
|
|
|
|
return t.conn.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t XMPPTransport) Close() error {
|
|
|
|
return t.conn.Close()
|
|
|
|
}
|