fix connection to ipv6 address + use fallback to jid domain

This commit is contained in:
genofire 2019-06-26 09:04:03 +02:00 committed by Mickaël Rémond
parent 7a386ec8d0
commit fde524ef98

View File

@ -85,8 +85,9 @@ Setting up the client / Checking the parameters
// Default the port to 5222. // Default the port to 5222.
// TODO: better config checks // TODO: better config checks
func NewClient(config Config, r *Router) (c *Client, err error) { func NewClient(config Config, r *Router) (c *Client, err error) {
// TODO: If option address is nil, use the Jid domain to compose the address // Parse JID
if config.Address, err = checkAddress(config.Address); err != nil { if config.parsedJid, err = NewJid(config.Jid); err != nil {
err = errors.New("missing jid")
return nil, NewConnError(err, true) return nil, NewConnError(err, true)
} }
@ -95,10 +96,13 @@ func NewClient(config Config, r *Router) (c *Client, err error) {
return nil, NewConnError(err, true) return nil, NewConnError(err, true)
} }
// Parse JID // fallback to jid domain
if config.parsedJid, err = NewJid(config.Jid); err != nil { if config.Address == "" {
err = errors.New("missing jid") config.Address = config.parsedJid.Domain
return nil, NewConnError(err, true) }
// if address has no port (behind his ipv6 address) - add default port
if strings.LastIndex(config.Address, ":") <= strings.LastIndex(config.Address, "]") {
config.Address += ":5222"
} }
c = new(Client) c = new(Client)
@ -112,24 +116,6 @@ func NewClient(config Config, r *Router) (c *Client, err error) {
return return
} }
// TODO Pass JID to be able to add default address based on JID, if addr is empty
func checkAddress(addr string) (string, error) {
var err error
hostport := strings.Split(addr, ":")
if len(hostport) > 2 {
err = errors.New("too many colons in xmpp server address")
return addr, err
}
// Address is composed of two parts, we are good
if len(hostport) == 2 && hostport[1] != "" {
return addr, err
}
// Port was not passed, we append XMPP default port:
return strings.Join([]string{hostport[0], "5222"}, ":"), err
}
// Connect triggers actual TCP connection, based on previously defined parameters. // Connect triggers actual TCP connection, based on previously defined parameters.
func (c *Client) Connect() error { func (c *Client) Connect() error {
var err error var err error