go-xmpp/transport.go

79 lines
1.7 KiB
Go
Raw Normal View History

2019-10-06 17:37:56 +00:00
package xmpp
import (
"crypto/tls"
"net"
"time"
)
type TransportConfiguration struct {
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 17:37:56 +00:00
type Transport interface {
Connect(address string) error
2019-10-06 17:37:56 +00:00
DoesStartTLS() bool
StartTLS(domain string) 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
}
// XMPPTransport implements the XMPP native TCP transport
type XMPPTransport struct {
Config TransportConfiguration
2019-10-06 17:37:56 +00:00
TLSConfig *tls.Config
// TCP level connection / can be replaced by a TLS session after starttls
conn net.Conn
}
func (t *XMPPTransport) Connect(address string) error {
2019-10-06 17:37:56 +00:00
var err error
t.conn, err = net.DialTimeout("tcp", address, time.Duration(t.Config.ConnectTimeout)*time.Second)
2019-10-06 17:37:56 +00:00
return err
}
func (t XMPPTransport) DoesStartTLS() bool {
return true
}
func (t *XMPPTransport) StartTLS(domain string) error {
if t.Config.TLSConfig == nil {
t.Config.TLSConfig = &tls.Config{}
2019-10-06 17:37:56 +00:00
}
if t.Config.TLSConfig.ServerName == "" {
t.Config.TLSConfig.ServerName = domain
2019-10-06 17:37:56 +00: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
}
}
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()
}