Look up SRV records for xmpp- and xmpps-client

srvlookup
Martin Dosch 3 years ago
parent afcbe65bc1
commit 702ce9ff47

@ -0,0 +1,53 @@
package main
import (
"fmt"
"net"
"strings"
"github.com/mattn/go-xmpp" // BSD-3-Clause
)
func connect(options xmpp.Options, directTLS bool) (*xmpp.Client, error) {
// Lookup SRV records if server is not specified manually
if options.Host == "" {
server := options.User[strings.LastIndex(options.User, "@")+1:]
// Lookup xmpp-client SRV records if direct TLS is not required
if !directTLS {
// TODO: Fallback to 5222 if no xmpp-client SRV records exist
if _, addrs, err := net.LookupSRV("xmpp-client", "tcp", server); err == nil {
if len(addrs) > 0 {
for _, adr := range addrs {
options.Host = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
// Connect to server
client, err := options.NewClient()
if err == nil {
return client, err
}
}
}
}
// TODO: Quit with error if NoTLS is set
// Unset STartTLS as we try direct TLS now
options.NoTLS = false
options.StartTLS = false
}
// Lookup xmpps-client SRV records
// TODO: Fallback to 5223 and 443 if no xmpps-client SRV records exist
if _, addrs, err := net.LookupSRV("xmpps-client", "tcp", server); err == nil {
if len(addrs) > 0 {
for _, adr := range addrs {
options.Host = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
// Connect to server
client, err := options.NewClient()
if err == nil {
return client, err
}
}
}
}
}
client, err := options.NewClient()
return client, err
}

@ -8,10 +8,8 @@ import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"os/user"
@ -291,30 +289,6 @@ func main() {
server = *flagServer
}
// Determine server part if no server is specified.
if server == "" {
server = strings.Split(user, "@")[1]
// Lookup SRV record for xmpps-client if direct TLS is requested
if *flagTLS {
if _, addrs, err := net.LookupSRV("xmpps-client", "tcp", server); err == nil {
if len(addrs) > 0 {
// Default to first record
server = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
defP := addrs[0].Priority
for _, adr := range addrs {
if adr.Priority < defP {
server = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
defP = adr.Priority
}
}
}
}
}
}
// Overwrite password if specified via command line flag.
if *flagPassword != "" {
password = *flagPassword
@ -333,20 +307,23 @@ func main() {
// Use ALPN
var tlsConfig tls.Config
tlsConfig.ServerName = strings.Split(user, "@")[1]
tlsConfig.ServerName = user[strings.LastIndex(user, "@")+1:]
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "xmpp-client")
tlsConfig.InsecureSkipVerify = *flagSkipVerify
// Set XMPP connection options.
options := xmpp.Options{
Host: server,
User: user,
Resource: *flagResource,
Password: password,
NoTLS: !*flagTLS,
StartTLS: !*flagTLS,
Debug: *flagDebug,
TLSConfig: &tlsConfig,
Host: server,
User: user,
// TODO: Check whether the timeout is reasonable or maybe make
// it configurable
DialTimeout: 1 * time.Second,
Resource: *flagResource,
Password: password,
NoTLS: !*flagTLS,
StartTLS: !*flagTLS,
Debug: *flagDebug,
TLSConfig: &tlsConfig,
}
// Read message from file.
@ -358,7 +335,7 @@ func main() {
}
// Connect to server.
client, err := options.NewClient()
client, err := connect(options, *flagTLS)
if err != nil {
log.Fatal(err)
}

Loading…
Cancel
Save