fix param of tlsconn.VerifyHostname

This commit is contained in:
Jesse Kuang 2019-01-09 15:35:32 +08:00
parent 113d9c0420
commit 2c5079ea28

52
xmpp.go
View File

@ -79,34 +79,10 @@ func containsIgnoreCase(s, substr string) bool {
func connect(host, user, passwd string) (net.Conn, error) { func connect(host, user, passwd string) (net.Conn, error) {
addr := host addr := host
if strings.TrimSpace(host) == "" { a := strings.SplitN(addr, ":", 2)
a := strings.SplitN(user, "@", 2)
if len(a) == 2 {
if cna, addrs, err := net.LookupSRV("xmpp-client", "tcp", a[1]); err == nil {
fmt.Printf("LookupSRV got: %s, %d SRVs\n", cna, len(addrs))
if len(addrs) > 0 {
// default to first record
addr = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
defP := addrs[0].Priority
for _, adr := range addrs {
if adr.Priority < defP {
addr = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
defP = adr.Priority
}
}
} else {
addr = a[1]
}
} else {
addr = a[1]
}
}
} else {
a := strings.SplitN(host, ":", 2)
if len(a) == 1 { if len(a) == 1 {
addr += ":5222" addr += ":5222"
} }
}
proxy := os.Getenv("HTTP_PROXY") proxy := os.Getenv("HTTP_PROXY")
if proxy == "" { if proxy == "" {
@ -217,13 +193,35 @@ type Options struct {
// NewClient establishes a new Client connection based on a set of Options. // NewClient establishes a new Client connection based on a set of Options.
func (o Options) NewClient() (*Client, error) { func (o Options) NewClient() (*Client, error) {
host := o.Host host := o.Host
if strings.TrimSpace(host) == "" {
a := strings.SplitN(o.User, "@", 2)
if len(a) == 2 {
if _, addrs, err := net.LookupSRV("xmpp-client", "tcp", a[1]); err == nil {
if len(addrs) > 0 {
// default to first record
host = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
defP := addrs[0].Priority
for _, adr := range addrs {
if adr.Priority < defP {
host = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
defP = adr.Priority
}
}
} else {
host = a[1]
}
} else {
host = a[1]
}
}
}
c, err := connect(host, o.User, o.Password) c, err := connect(host, o.User, o.Password)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if strings.LastIndex(o.Host, ":") > 0 { if strings.LastIndex(host, ":") > 0 {
host = host[:strings.LastIndex(o.Host, ":")] host = host[:strings.LastIndex(host, ":")]
} }
client := new(Client) client := new(Client)