mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-23 02:52:00 -08:00
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:
parent
f8d0e99696
commit
7fa4b06705
@ -11,9 +11,11 @@ import (
|
||||
|
||||
func main() {
|
||||
opts := xmpp.ComponentOptions{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
Domain: "service.localhost",
|
||||
Secret: "mypass",
|
||||
Address: "localhost:9999",
|
||||
|
||||
// TODO: Move that part to a component discovery handler
|
||||
Name: "Test Component",
|
||||
|
@ -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",
|
||||
|
@ -15,7 +15,9 @@ import (
|
||||
|
||||
func main() {
|
||||
config := xmpp.Config{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: "localhost:5222",
|
||||
},
|
||||
Jid: "test@localhost",
|
||||
Credential: xmpp.Password("test"),
|
||||
StreamLogger: os.Stdout,
|
||||
|
@ -32,7 +32,9 @@ func main() {
|
||||
|
||||
// 2. Prepare XMPP client
|
||||
config := xmpp.Config{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: *address,
|
||||
},
|
||||
Jid: *jid,
|
||||
Credential: xmpp.Password(*password),
|
||||
// StreamLogger: os.Stdout,
|
||||
|
@ -15,12 +15,14 @@ import (
|
||||
|
||||
func main() {
|
||||
config := xmpp.Config{
|
||||
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()
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -32,8 +32,10 @@ func sendxmpp(cmd *cobra.Command, args []string) {
|
||||
|
||||
var err error
|
||||
client, err := xmpp.NewClient(xmpp.Config{
|
||||
Jid: viper.GetString("jid"),
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: viper.GetString("addr"),
|
||||
},
|
||||
Jid: viper.GetString("jid"),
|
||||
Credential: xmpp.Password(viper.GetString("password")),
|
||||
}, xmpp.NewRouter())
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
10
transport.go
10
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}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user