Move address into transport config

This makes it possible to use a factory function to create a transport of the right type and not having to repeat the address when calling Transport.Connect()
This commit is contained in:
Wichert Akkerman 2019-10-11 06:41:15 +02:00 committed by Mickaël Rémond
parent f8d0e99696
commit 7fa4b06705
13 changed files with 67 additions and 25 deletions

View File

@ -11,9 +11,11 @@ import (
func main() { func main() {
opts := xmpp.ComponentOptions{ opts := xmpp.ComponentOptions{
TransportConfiguration: xmpp.TransportConfiguration{
Address: "localhost:9999",
},
Domain: "service.localhost", Domain: "service.localhost",
Secret: "mypass", Secret: "mypass",
Address: "localhost:9999",
// TODO: Move that part to a component discovery handler // TODO: Move that part to a component discovery handler
Name: "Test Component", Name: "Test Component",

View File

@ -10,9 +10,11 @@ import (
func main() { func main() {
opts := xmpp.ComponentOptions{ opts := xmpp.ComponentOptions{
TransportConfiguration: xmpp.TransportConfiguration{
Address: "localhost:8888",
},
Domain: "service2.localhost", Domain: "service2.localhost",
Secret: "mypass", Secret: "mypass",
Address: "localhost:8888",
Name: "Test Component", Name: "Test Component",
Category: "gateway", Category: "gateway",
Type: "service", Type: "service",

View File

@ -15,7 +15,9 @@ import (
func main() { func main() {
config := xmpp.Config{ config := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: "localhost:5222", Address: "localhost:5222",
},
Jid: "test@localhost", Jid: "test@localhost",
Credential: xmpp.Password("test"), Credential: xmpp.Password("test"),
StreamLogger: os.Stdout, StreamLogger: os.Stdout,

View File

@ -32,7 +32,9 @@ func main() {
// 2. Prepare XMPP client // 2. Prepare XMPP client
config := xmpp.Config{ config := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: *address, Address: *address,
},
Jid: *jid, Jid: *jid,
Credential: xmpp.Password(*password), Credential: xmpp.Password(*password),
// StreamLogger: os.Stdout, // StreamLogger: os.Stdout,

View File

@ -15,12 +15,14 @@ import (
func main() { func main() {
config := xmpp.Config{ config := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: "localhost:5222", Address: "localhost:5222",
// TLSConfig: tls.Config{InsecureSkipVerify: true},
},
Jid: "test@localhost", Jid: "test@localhost",
Credential: xmpp.OAuthToken("OdAIsBlY83SLBaqQoClAn7vrZSHxixT8"), Credential: xmpp.OAuthToken("OdAIsBlY83SLBaqQoClAn7vrZSHxixT8"),
StreamLogger: os.Stdout, StreamLogger: os.Stdout,
// Insecure: true, // Insecure: true,
// TLSConfig: tls.Config{InsecureSkipVerify: true},
} }
router := xmpp.NewRouter() router := xmpp.NewRouter()

View File

@ -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) err = c.transport.Connect()
if err != nil { if err != nil {
return err return err
} }

View File

@ -25,7 +25,13 @@ func TestClient_Connect(t *testing.T) {
mock.Start(t, testXMPPAddress, handlerConnectSuccess) mock.Start(t, testXMPPAddress, handlerConnectSuccess)
// Test / Check result // Test / Check result
config := Config{Address: testXMPPAddress, Jid: "test@localhost", Credential: Password("test"), Insecure: true} config := Config{
TransportConfiguration: TransportConfiguration{
Address: testXMPPAddress,
},
Jid: "test@localhost",
Credential: Password("test"),
Insecure: true}
var client *Client var client *Client
var err error var err error
@ -47,7 +53,13 @@ func TestClient_NoInsecure(t *testing.T) {
mock.Start(t, testXMPPAddress, handlerAbortTLS) mock.Start(t, testXMPPAddress, handlerAbortTLS)
// Test / Check result // Test / Check result
config := Config{Address: testXMPPAddress, Jid: "test@localhost", Credential: Password("test")} config := Config{
TransportConfiguration: TransportConfiguration{
Address: testXMPPAddress,
},
Jid: "test@localhost",
Credential: Password("test"),
}
var client *Client var client *Client
var err error var err error
@ -71,7 +83,13 @@ func TestClient_FeaturesTracking(t *testing.T) {
mock.Start(t, testXMPPAddress, handlerAbortTLS) mock.Start(t, testXMPPAddress, handlerAbortTLS)
// Test / Check result // Test / Check result
config := Config{Address: testXMPPAddress, Jid: "test@localhost", Credential: Password("test")} config := Config{
TransportConfiguration: TransportConfiguration{
Address: testXMPPAddress,
},
Jid: "test@localhost",
Credential: Password("test"),
}
var client *Client var client *Client
var err error var err error
@ -94,7 +112,14 @@ func TestClient_RFC3921Session(t *testing.T) {
mock.Start(t, testXMPPAddress, handlerConnectWithSession) mock.Start(t, testXMPPAddress, handlerConnectWithSession)
// Test / Check result // Test / Check result
config := Config{Address: testXMPPAddress, Jid: "test@localhost", Credential: Password("test"), Insecure: true} config := Config{
TransportConfiguration: TransportConfiguration{
Address: testXMPPAddress,
},
Jid: "test@localhost",
Credential: Password("test"),
Insecure: true,
}
var client *Client var client *Client
var err error var err error

View File

@ -32,8 +32,10 @@ func sendxmpp(cmd *cobra.Command, args []string) {
var err error var err error
client, err := xmpp.NewClient(xmpp.Config{ client, err := xmpp.NewClient(xmpp.Config{
Jid: viper.GetString("jid"), TransportConfiguration: xmpp.TransportConfiguration{
Address: viper.GetString("addr"), Address: viper.GetString("addr"),
},
Jid: viper.GetString("jid"),
Credential: xmpp.Password(viper.GetString("password")), Credential: xmpp.Password(viper.GetString("password")),
}, xmpp.NewRouter()) }, xmpp.NewRouter())

View File

@ -23,9 +23,6 @@ type ComponentOptions struct {
Domain string Domain string
// Secret is the "password" used by the XMPP server to secure component access // Secret is the "password" used by the XMPP server to secure component access
Secret string Secret string
// Address is the XMPP Host and port to connect to. Host is of
// the form 'serverhost:port' i.e "localhost:8888"
Address string
// ================================= // =================================
// Component discovery // Component discovery
@ -71,7 +68,7 @@ func (c *Component) Connect() error {
func (c *Component) Resume(sm SMState) error { func (c *Component) Resume(sm SMState) error {
var err error var err error
c.transport = &XMPPTransport{Config: c.ComponentOptions.TransportConfiguration} c.transport = &XMPPTransport{Config: c.ComponentOptions.TransportConfiguration}
if err = c.transport.Connect(c.Address); err != nil { if err = c.transport.Connect(); err != nil {
return err return err
} }
c.updateState(StateConnected) c.updateState(StateConnected)

View File

@ -10,7 +10,6 @@ type Config struct {
// changes made after connecting are ignored. // changes made after connecting are ignored.
TransportConfiguration TransportConfiguration
Address string
Jid string Jid string
parsedJid *Jid // For easier manipulation parsedJid *Jid // For easier manipulation
Credential Credential Credential Credential

View File

@ -5,6 +5,9 @@ import (
) )
type TransportConfiguration struct { type TransportConfiguration struct {
// Address is the XMPP Host and port to connect to. Host is of
// the form 'serverhost:port' i.e "localhost:8888"
Address string
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 // tls.Config must not be modified after having been passed to NewClient. Any
// changes made after connecting are ignored. // changes made after connecting are ignored.
@ -12,7 +15,7 @@ type TransportConfiguration struct {
} }
type Transport interface { type Transport interface {
Connect(address string) error Connect() error
DoesStartTLS() bool DoesStartTLS() bool
StartTLS(domain string) error StartTLS(domain string) error
@ -20,3 +23,8 @@ type Transport interface {
Write(p []byte) (n int, err error) Write(p []byte) (n int, err error)
Close() error Close() error
} }
func NewTransport(config TransportConfiguration) Transport {
return &XMPPTransport{Config: config}
}

View File

@ -11,21 +11,22 @@ import (
) )
type WebsocketTransport struct { type WebsocketTransport struct {
Config TransportConfiguration
wsConn *websocket.Conn wsConn *websocket.Conn
netConn net.Conn netConn net.Conn
ctx context.Context ctx context.Context
} }
func (t *WebsocketTransport) Connect(address string, c Config) error { func (t *WebsocketTransport) Connect() error {
t.ctx = context.Background() t.ctx = context.Background()
ctx, cancel := context.WithTimeout(t.ctx, time.Duration(c.ConnectTimeout)*time.Second) ctx, cancel := context.WithTimeout(t.ctx, time.Duration(t.Config.ConnectTimeout)*time.Second)
defer cancel() defer cancel()
if !c.Insecure && strings.HasPrefix(address, "wss:") { if !c.Insecure && strings.HasPrefix(address, "wss:") {
return errors.New("Websocket address is not secure") return errors.New("Websocket address is not secure")
} }
wsConn, _, err := websocket.Dial(ctx, address, nil) wsConn, _, err := websocket.Dial(ctx, t.Config.Address, nil)
if err != nil { if err != nil {
t.wsConn = wsConn t.wsConn = wsConn
t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText) t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText)

View File

@ -14,10 +14,10 @@ type XMPPTransport struct {
conn net.Conn conn net.Conn
} }
func (t *XMPPTransport) Connect(address string) error { func (t *XMPPTransport) Connect() error {
var err error var err error
t.conn, err = net.DialTimeout("tcp", address, time.Duration(t.Config.ConnectTimeout)*time.Second) t.conn, err = net.DialTimeout("tcp", t.Config.Address, time.Duration(t.Config.ConnectTimeout)*time.Second)
return err return err
} }