forked from jshiffer/go-xmpp
Split out TransportConfiguration
This allows using the same transport configuration from both clients and components.
This commit is contained in:
parent
66e219844b
commit
96fccbd399
@ -88,8 +88,7 @@ type Client struct {
|
|||||||
// Store user defined options and states
|
// Store user defined options and states
|
||||||
config Config
|
config Config
|
||||||
// Session gather data that can be accessed by users of this library
|
// Session gather data that can be accessed by users of this library
|
||||||
Session *Session
|
Session *Session
|
||||||
// TCP level connection / can be replaced by a TLS session after starttls
|
|
||||||
transport Transport
|
transport Transport
|
||||||
// Router is used to dispatch packets
|
// Router is used to dispatch packets
|
||||||
router *Router
|
router *Router
|
||||||
@ -139,12 +138,13 @@ func NewClient(config Config, r *Router) (c *Client, err error) {
|
|||||||
c = new(Client)
|
c = new(Client)
|
||||||
c.config = config
|
c.config = config
|
||||||
c.router = r
|
c.router = r
|
||||||
c.transport = &XMPPTransport{}
|
|
||||||
|
|
||||||
if c.config.ConnectTimeout == 0 {
|
if c.config.ConnectTimeout == 0 {
|
||||||
c.config.ConnectTimeout = 15 // 15 second as default
|
c.config.ConnectTimeout = 15 // 15 second as default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.transport = &XMPPTransport{Config: config.TransportConfiguration}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ func (c *Client) Connect() error {
|
|||||||
func (c *Client) Resume(state SMState) error {
|
func (c *Client) Resume(state SMState) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
err = c.transport.Connect(c.config.Address, c.config)
|
err = c.transport.Connect(c.config.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package xmpp
|
package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// TransportConfiguration must not be modified after having been passed to NewClient. Any
|
||||||
|
// changes made after connecting are ignored.
|
||||||
|
TransportConfiguration
|
||||||
|
|
||||||
Address string
|
Address string
|
||||||
Jid string
|
Jid string
|
||||||
parsedJid *Jid // For easier manipulation
|
parsedJid *Jid // For easier manipulation
|
||||||
@ -14,9 +17,6 @@ type Config struct {
|
|||||||
StreamLogger *os.File // Used for debugging
|
StreamLogger *os.File // Used for debugging
|
||||||
Lang string // TODO: should default to 'en'
|
Lang string // TODO: should default to 'en'
|
||||||
ConnectTimeout int // Client timeout in seconds. Default to 15
|
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
|
|
||||||
// Insecure can be set to true to allow to open a session without TLS. If TLS
|
// Insecure can be set to true to allow to open a session without TLS. If TLS
|
||||||
// is supported on the server, we will still try to use it.
|
// is supported on the server, we will still try to use it.
|
||||||
Insecure bool
|
Insecure bool
|
||||||
|
@ -132,7 +132,7 @@ func (s *Session) startTlsIfSupported(transport Transport, domain string, o Conf
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.err = transport.StartTLS(domain, o)
|
s.err = transport.StartTLS(domain)
|
||||||
|
|
||||||
if s.err == nil {
|
if s.err == nil {
|
||||||
s.TlsEnabled = true
|
s.TlsEnabled = true
|
||||||
|
30
transport.go
30
transport.go
@ -6,10 +6,17 @@ import (
|
|||||||
"time"
|
"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
|
||||||
|
}
|
||||||
|
|
||||||
type Transport interface {
|
type Transport interface {
|
||||||
Connect(address string, c Config) error
|
Connect(address string) error
|
||||||
DoesStartTLS() bool
|
DoesStartTLS() bool
|
||||||
StartTLS(domain string, c Config) error
|
StartTLS(domain string) error
|
||||||
|
|
||||||
Read(p []byte) (n int, err error)
|
Read(p []byte) (n int, err error)
|
||||||
Write(p []byte) (n int, err error)
|
Write(p []byte) (n int, err error)
|
||||||
@ -18,15 +25,16 @@ type Transport interface {
|
|||||||
|
|
||||||
// XMPPTransport implements the XMPP native TCP transport
|
// XMPPTransport implements the XMPP native TCP transport
|
||||||
type XMPPTransport struct {
|
type XMPPTransport struct {
|
||||||
|
Config TransportConfiguration
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
// TCP level connection / can be replaced by a TLS session after starttls
|
// TCP level connection / can be replaced by a TLS session after starttls
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *XMPPTransport) Connect(address string, c Config) error {
|
func (t *XMPPTransport) Connect(address string) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
t.conn, err = net.DialTimeout("tcp", address, time.Duration(c.ConnectTimeout)*time.Second)
|
t.conn, err = net.DialTimeout("tcp", address, time.Duration(t.Config.ConnectTimeout)*time.Second)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,17 +42,13 @@ func (t XMPPTransport) DoesStartTLS() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *XMPPTransport) StartTLS(domain string, c Config) error {
|
func (t *XMPPTransport) StartTLS(domain string) error {
|
||||||
if t.TLSConfig == nil {
|
if t.Config.TLSConfig == nil {
|
||||||
if c.TLSConfig != nil {
|
t.Config.TLSConfig = &tls.Config{}
|
||||||
t.TLSConfig = c.TLSConfig
|
|
||||||
} else {
|
|
||||||
t.TLSConfig = &tls.Config{}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.TLSConfig.ServerName == "" {
|
if t.Config.TLSConfig.ServerName == "" {
|
||||||
t.TLSConfig.ServerName = domain
|
t.Config.TLSConfig.ServerName = domain
|
||||||
}
|
}
|
||||||
tlsConn := tls.Client(t.conn, t.TLSConfig)
|
tlsConn := tls.Client(t.conn, t.TLSConfig)
|
||||||
// We convert existing connection to TLS
|
// We convert existing connection to TLS
|
||||||
|
Loading…
Reference in New Issue
Block a user