From 30ec9e14fb788c4a918aca189c4bbc7a80d28ecc Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 23 Jul 2016 11:59:21 +0200 Subject: [PATCH] [fix] Add domain name in Stream to get TLS working --- src/xmpp/client.go | 2 +- src/xmpp/dns.go | 13 ++++++++++++- src/xmpp/stream.go | 4 +++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/xmpp/client.go b/src/xmpp/client.go index 34941d8..8e5b817 100644 --- a/src/xmpp/client.go +++ b/src/xmpp/client.go @@ -115,7 +115,7 @@ func startTLS(stream *Stream, config *ClientConfig) error { return err } - tlsConfig := tls.Config{InsecureSkipVerify: config.InsecureSkipVerify} + tlsConfig := tls.Config{InsecureSkipVerify: config.InsecureSkipVerify, ServerName: stream.connDomain} return stream.UpgradeTLS(&tlsConfig) } diff --git a/src/xmpp/dns.go b/src/xmpp/dns.go index 4b1d136..757ba38 100644 --- a/src/xmpp/dns.go +++ b/src/xmpp/dns.go @@ -27,7 +27,18 @@ func HomeServerAddrs(jid JID) (addr []string, err error) { // Build list of "host:port" strings. for _, a := range addrs { - addr = append(addr, fmt.Sprintf("%s:%d", a.Target, a.Port)) + target := parseTargetDomainName(a.Target) + addr = append(addr, fmt.Sprintf("%s:%d", target, a.Port)) + } + return +} + +// Remove the last dot in the domain name if exist +func parseTargetDomainName(domainName string) (ret string) { + if domainName[len(domainName)-1] == '.' { + ret = parseTargetDomainName(domainName[:len(domainName)-1]) + } else { + ret = domainName } return } diff --git a/src/xmpp/stream.go b/src/xmpp/stream.go index 31bc7f2..b1cd8d5 100644 --- a/src/xmpp/stream.go +++ b/src/xmpp/stream.go @@ -7,6 +7,7 @@ import ( "io" "log" "net" + "strings" ) // Stream configuration. @@ -24,6 +25,7 @@ type Stream struct { config *StreamConfig stanzaBuf string incomingNamespace nsMap + connDomain string } // Create a XML stream connection. A Steam is used by an XMPP instance to @@ -41,7 +43,7 @@ func NewStream(addr string, config *StreamConfig) (*Stream, error) { return nil, err } - stream := &Stream{conn: conn, dec: xml.NewDecoder(conn), config: config} + stream := &Stream{conn: conn, dec: xml.NewDecoder(conn), config: config, connDomain: strings.SplitN(addr, ":", 2)[0]} if err := stream.send([]byte("")); err != nil { return nil, err