mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-12-17 14:02:04 -08:00
Move XMPP transport to separate file
This commit is contained in:
parent
e97d290e2b
commit
f8d0e99696
56
transport.go
56
transport.go
@ -2,8 +2,6 @@ package xmpp
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TransportConfiguration struct {
|
||||
@ -22,57 +20,3 @@ type Transport interface {
|
||||
Write(p []byte) (n int, err error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// 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
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
func (t *XMPPTransport) Connect(address string) error {
|
||||
var err error
|
||||
|
||||
t.conn, err = net.DialTimeout("tcp", address, time.Duration(t.Config.ConnectTimeout)*time.Second)
|
||||
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{}
|
||||
}
|
||||
|
||||
if t.Config.TLSConfig.ServerName == "" {
|
||||
t.Config.TLSConfig.ServerName = domain
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
61
xmpp_transport.go
Normal file
61
xmpp_transport.go
Normal file
@ -0,0 +1,61 @@
|
||||
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
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
func (t *XMPPTransport) Connect(address string) error {
|
||||
var err error
|
||||
|
||||
t.conn, err = net.DialTimeout("tcp", address, time.Duration(t.Config.ConnectTimeout)*time.Second)
|
||||
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{}
|
||||
}
|
||||
|
||||
if t.Config.TLSConfig.ServerName == "" {
|
||||
t.Config.TLSConfig.ServerName = domain
|
||||
}
|
||||
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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user