-
Notifications
You must be signed in to change notification settings - Fork 12
/
srv.go
41 lines (37 loc) · 850 Bytes
/
srv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"errors"
"fmt"
"net"
"strconv"
)
func getSRVService(protocols []string) string {
if len(protocols) == 0 {
return ""
}
switch protocols[0] {
case "xmpp-client":
return "xmpps-client"
case "xmpp-server":
return "xmpps-server"
}
return ""
}
func dialSRV(dialer net.Dialer, network string, hostname string, service string) (net.Conn, error) {
_, addrs, err := net.LookupSRV(service, "tcp", hostname)
if err != nil {
return nil, err
}
if len(addrs) == 0 {
return nil, fmt.Errorf("no SRV records exist for %s on %s", service, hostname)
}
var errs []error
for _, addr := range addrs {
conn, err := dialer.Dial(network, net.JoinHostPort(addr.Target, strconv.FormatUint(uint64(addr.Port), 10)))
if err == nil {
return conn, nil
}
errs = append(errs, err)
}
return nil, errors.Join(errs...)
}