diff --git a/_examples/delegation/delegation.go b/_examples/delegation/delegation.go index fcf397a..61e3716 100644 --- a/_examples/delegation/delegation.go +++ b/_examples/delegation/delegation.go @@ -11,9 +11,11 @@ import ( func main() { opts := xmpp.ComponentOptions{ - Domain: "service.localhost", - Secret: "mypass", - Address: "localhost:9999", + TransportConfiguration: xmpp.TransportConfiguration{ + Address: "localhost:9999", + }, + Domain: "service.localhost", + Secret: "mypass", // TODO: Move that part to a component discovery handler Name: "Test Component", diff --git a/_examples/xmpp_component/xmpp_component.go b/_examples/xmpp_component/xmpp_component.go index 77a3099..fc07f05 100644 --- a/_examples/xmpp_component/xmpp_component.go +++ b/_examples/xmpp_component/xmpp_component.go @@ -10,9 +10,11 @@ import ( func main() { opts := xmpp.ComponentOptions{ + TransportConfiguration: xmpp.TransportConfiguration{ + Address: "localhost:8888", + }, Domain: "service2.localhost", Secret: "mypass", - Address: "localhost:8888", Name: "Test Component", Category: "gateway", Type: "service", diff --git a/_examples/xmpp_echo/xmpp_echo.go b/_examples/xmpp_echo/xmpp_echo.go index 3ee9b2e..5654a2b 100644 --- a/_examples/xmpp_echo/xmpp_echo.go +++ b/_examples/xmpp_echo/xmpp_echo.go @@ -15,7 +15,9 @@ import ( func main() { config := xmpp.Config{ - Address: "localhost:5222", + TransportConfiguration: xmpp.TransportConfiguration{ + Address: "localhost:5222", + }, Jid: "test@localhost", Credential: xmpp.Password("test"), StreamLogger: os.Stdout, diff --git a/_examples/xmpp_jukebox/xmpp_jukebox.go b/_examples/xmpp_jukebox/xmpp_jukebox.go index 51a401d..10e5dfc 100644 --- a/_examples/xmpp_jukebox/xmpp_jukebox.go +++ b/_examples/xmpp_jukebox/xmpp_jukebox.go @@ -32,7 +32,9 @@ func main() { // 2. Prepare XMPP client config := xmpp.Config{ - Address: *address, + TransportConfiguration: xmpp.TransportConfiguration{ + Address: *address, + }, Jid: *jid, Credential: xmpp.Password(*password), // StreamLogger: os.Stdout, diff --git a/_examples/xmpp_oauth2/xmpp_oauth2.go b/_examples/xmpp_oauth2/xmpp_oauth2.go index a5cace5..f322447 100644 --- a/_examples/xmpp_oauth2/xmpp_oauth2.go +++ b/_examples/xmpp_oauth2/xmpp_oauth2.go @@ -15,12 +15,14 @@ import ( func main() { config := xmpp.Config{ - Address: "localhost:5222", + TransportConfiguration: xmpp.TransportConfiguration{ + Address: "localhost:5222", + // TLSConfig: tls.Config{InsecureSkipVerify: true}, + }, Jid: "test@localhost", Credential: xmpp.OAuthToken("OdAIsBlY83SLBaqQoClAn7vrZSHxixT8"), StreamLogger: os.Stdout, // Insecure: true, - // TLSConfig: tls.Config{InsecureSkipVerify: true}, } router := xmpp.NewRouter() diff --git a/client.go b/client.go index e783c0f..cd7640b 100644 --- a/client.go +++ b/client.go @@ -160,7 +160,7 @@ func (c *Client) Connect() error { func (c *Client) Resume(state SMState) error { var err error - err = c.transport.Connect(c.config.Address) + err = c.transport.Connect() if err != nil { return err } diff --git a/client_test.go b/client_test.go index 13bbb36..2636f29 100644 --- a/client_test.go +++ b/client_test.go @@ -25,7 +25,13 @@ func TestClient_Connect(t *testing.T) { mock.Start(t, testXMPPAddress, handlerConnectSuccess) // 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 err error @@ -47,7 +53,13 @@ func TestClient_NoInsecure(t *testing.T) { mock.Start(t, testXMPPAddress, handlerAbortTLS) // 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 err error @@ -71,7 +83,13 @@ func TestClient_FeaturesTracking(t *testing.T) { mock.Start(t, testXMPPAddress, handlerAbortTLS) // 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 err error @@ -94,7 +112,14 @@ func TestClient_RFC3921Session(t *testing.T) { mock.Start(t, testXMPPAddress, handlerConnectWithSession) // 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 err error diff --git a/cmd/fluuxmpp/send.go b/cmd/fluuxmpp/send.go index 5760d8d..0942928 100644 --- a/cmd/fluuxmpp/send.go +++ b/cmd/fluuxmpp/send.go @@ -32,8 +32,10 @@ func sendxmpp(cmd *cobra.Command, args []string) { var err error client, err := xmpp.NewClient(xmpp.Config{ + TransportConfiguration: xmpp.TransportConfiguration{ + Address: viper.GetString("addr"), + }, Jid: viper.GetString("jid"), - Address: viper.GetString("addr"), Credential: xmpp.Password(viper.GetString("password")), }, xmpp.NewRouter()) diff --git a/component.go b/component.go index 0a1000e..4a94389 100644 --- a/component.go +++ b/component.go @@ -23,9 +23,6 @@ type ComponentOptions struct { Domain string // Secret is the "password" used by the XMPP server to secure component access 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 @@ -71,7 +68,7 @@ func (c *Component) Connect() error { func (c *Component) Resume(sm SMState) error { var err error 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 } c.updateState(StateConnected) diff --git a/config.go b/config.go index c4ff97f..178feae 100644 --- a/config.go +++ b/config.go @@ -10,7 +10,6 @@ type Config struct { // changes made after connecting are ignored. TransportConfiguration - Address string Jid string parsedJid *Jid // For easier manipulation Credential Credential diff --git a/transport.go b/transport.go index 30bcfca..e435633 100644 --- a/transport.go +++ b/transport.go @@ -5,6 +5,9 @@ import ( ) 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 // tls.Config must not be modified after having been passed to NewClient. Any // changes made after connecting are ignored. @@ -12,7 +15,7 @@ type TransportConfiguration struct { } type Transport interface { - Connect(address string) error + Connect() error DoesStartTLS() bool StartTLS(domain string) error @@ -20,3 +23,8 @@ type Transport interface { Write(p []byte) (n int, err error) Close() error } + +func NewTransport(config TransportConfiguration) Transport { + return &XMPPTransport{Config: config} + +} diff --git a/websocket_transport.go b/websocket_transport.go index 3b16622..9363cf8 100644 --- a/websocket_transport.go +++ b/websocket_transport.go @@ -11,21 +11,22 @@ import ( ) type WebsocketTransport struct { + Config TransportConfiguration wsConn *websocket.Conn netConn net.Conn ctx context.Context } -func (t *WebsocketTransport) Connect(address string, c Config) error { +func (t *WebsocketTransport) Connect() error { 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() if !c.Insecure && strings.HasPrefix(address, "wss:") { 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 { t.wsConn = wsConn t.netConn = websocket.NetConn(t.ctx, t.wsConn, websocket.MessageText) diff --git a/xmpp_transport.go b/xmpp_transport.go index fcbaa3b..6f526db 100644 --- a/xmpp_transport.go +++ b/xmpp_transport.go @@ -14,10 +14,10 @@ type XMPPTransport struct { conn net.Conn } -func (t *XMPPTransport) Connect(address string) error { +func (t *XMPPTransport) Connect() 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 }