Use xmppsrv for SRV lookups.

v0.1
Martin Dosch 3 years ago
parent 57892a8901
commit ca8dec5ca1

@ -2,7 +2,8 @@
## [Unreleased]
### Changed
- Use xml.Marshal to safely build HTTP Upload request
- Use xml.Marshal to safely build HTTP Upload request.
- Use salsa.debian.org/mdosch/xmppsrv for SRV lookups.
## [v0.1.1]
### Changed

@ -7,79 +7,36 @@ package main
import (
"fmt"
"net"
"sort"
"strings"
"github.com/mattn/go-xmpp" // BSD-3-Clause
"github.com/mattn/go-xmpp" // BSD-3-Clause
"salsa.debian.org/mdosch/xmppsrv" // BSD-2-Clause
)
type srv struct {
host string
xmpps bool
port uint16
priority uint16
weight uint16
}
type srvMixed []srv
type byPriority []srv
func (o byPriority) Len() int { return len(o) }
func (o byPriority) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o byPriority) Less(i, j int) bool {
if o[i].priority == o[j].priority {
return o[i].weight > o[j].weight
}
return o[i].priority < o[j].priority
}
func connect(options xmpp.Options, directTLS bool) (*xmpp.Client, error) {
// Look up SRV records if server is not specified manually.
srvMixed := srvMixed{}
if options.Host == "" {
server := options.User[strings.Index(options.User, "@")+1:]
// Look up xmpp-client SRV records.
if _, addrs, err := net.LookupSRV("xmpp-client", "tcp", server); err == nil {
if len(addrs) > 0 {
for _, adr := range addrs {
srvMixed = append(srvMixed, srv{strings.TrimSuffix(adr.Target, "."),
false, adr.Port, adr.Priority, adr.Weight})
}
}
}
// Look up xmpps-client SRV records.
if _, addrs, err := net.LookupSRV("xmpps-client", "tcp", server); err == nil {
if len(addrs) > 0 {
if addrs[0].Target != "." {
for _, adr := range addrs {
srvMixed = append(srvMixed,
srv{strings.TrimSuffix(adr.Target, "."), true, adr.Port,
adr.Priority, adr.Weight})
}
}
}
}
if len(srvMixed) > 0 {
// Sort xmpp- and xmpps-client SRV records according to the priority
// and wight.
sort.Sort(byPriority(srvMixed))
srvMixed, err := xmppsrv.LookupClient(server)
if len(srvMixed) > 0 && err == nil {
for _, adr := range srvMixed {
if !directTLS && !adr.xmpps {
if !directTLS && adr.Type != "xmpps-client" {
// Use StartTLS
options.NoTLS = true
options.StartTLS = true
options.Host = net.JoinHostPort(adr.host, fmt.Sprint(adr.port))
options.Host = net.JoinHostPort(adr.Target, fmt.Sprint(adr.Port))
// Connect to server
client, err := options.NewClient()
if err == nil {
return client, err
}
} else if adr.xmpps {
} else if adr.Type == "xmpps-client" {
// Use direct TLS
options.NoTLS = false
options.StartTLS = false
options.Host = net.JoinHostPort(adr.host, fmt.Sprint(adr.port))
options.Host = net.JoinHostPort(adr.Target, fmt.Sprint(adr.Port))
// Connect to server
client, err := options.NewClient()
if err == nil {

Loading…
Cancel
Save