| 1 |
// Copyright Martin Dosch. |
| 2 |
// Use of this source code is governed by the BSD-2-clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"log/slog" |
| 10 |
"net" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/xmppo/go-xmpp" // BSD-3-Clause |
| 16 |
"golang.org/x/net/idna" // BSD-3-Clause |
| 17 |
"salsa.debian.org/mdosch/xmppsrv" // BSD-2-Clause |
| 18 |
) |
| 19 |
|
| 20 |
func connect(options xmpp.Options, directTLS bool, anon bool, retryWait int, retryMax int) (client *xmpp.Client, err error) { |
| 21 |
var server string |
| 22 |
proxy := os.Getenv("HTTP_PROXY") |
| 23 |
slog.Info("getting environment variable:", "HTTP_PROXY", proxy) |
| 24 |
if !anon { |
| 25 |
server = options.User[strings.Index(options.User, "@")+1:] |
| 26 |
} else { |
| 27 |
server = options.User |
| 28 |
} |
| 29 |
retry := true |
| 30 |
for i := 0; retry; i++ { |
| 31 |
// Look up SRV records or hostmeta2 if server is not specified manually |
| 32 |
// or if anon authentication is used. |
| 33 |
if options.Host == "" || anon { |
| 34 |
// Don't do SRV or hostmeta2 look ups if proxy is set. |
| 35 |
// TODO: 2024-10-30: Support hostmeta2 look ups using proxy. |
| 36 |
if proxy == "" { |
| 37 |
// Look up xmpp-client SRV records. |
| 38 |
slog.Info("looking up client SRV records for", "server", server) |
| 39 |
serverASCII, err := idna.ToASCII(server) |
| 40 |
if err != nil { |
| 41 |
return client, err |
| 42 |
} |
| 43 |
srvMixed, err := xmppsrv.LookupClient(serverASCII) |
| 44 |
if len(srvMixed) > 0 && err == nil { |
| 45 |
for _, adr := range srvMixed { |
| 46 |
switch { |
| 47 |
case directTLS && adr.Type == "xmpp-client": |
| 48 |
slog.Info("skipping as direct TLS is requested:", "type", adr.Type, |
| 49 |
"target", adr.Target, "port", adr.Port) |
| 50 |
continue |
| 51 |
case adr.Type == "xmpp-client": |
| 52 |
// Use StartTLS |
| 53 |
options.NoTLS = true |
| 54 |
slog.Info("received SRV record:", "type", adr.Type, "target", adr.Target, "port", adr.Port) |
| 55 |
slog.Info("setting xmpp connection option:", "NoTLS", options.NoTLS) |
| 56 |
slog.Info("setting xmpp connection option:", "StartTLS", options.StartTLS) |
| 57 |
options.StartTLS = true |
| 58 |
case adr.Type == "xmpps-client": |
| 59 |
// Use direct TLS |
| 60 |
options.NoTLS = false |
| 61 |
options.StartTLS = false |
| 62 |
slog.Info("received SRV record::", "type", adr.Type, "target", adr.Target, "port", adr.Port) |
| 63 |
slog.Info("setting xmpp connection option:", "NoTLS", options.NoTLS) |
| 64 |
slog.Info("setting xmpp connection option:", "StartTLS", options.StartTLS) |
| 65 |
default: |
| 66 |
continue |
| 67 |
} |
| 68 |
options.Host = net.JoinHostPort(adr.Target, fmt.Sprint(adr.Port)) |
| 69 |
// Connect to server |
| 70 |
slog.Info("connecting to", "host", options.Host) |
| 71 |
client, err = options.NewClient() |
| 72 |
if err == nil || strings.Contains(err.Error(), "not-authorized") { |
| 73 |
// Don't try to connect on other port if error contains |
| 74 |
// "not-authorized". |
| 75 |
return client, err |
| 76 |
} |
| 77 |
slog.Info("connecting failed") |
| 78 |
} |
| 79 |
} |
| 80 |
// Look up hostmeta2 file. |
| 81 |
slog.Info("looking up host meta 2 for", "server", server) |
| 82 |
hm2, httpStatus, err := xmppsrv.LookupHostmeta2(serverASCII) |
| 83 |
if httpStatus != 404 && err == nil { |
| 84 |
for _, link := range hm2.Links { |
| 85 |
if link.Rel == nsC2SdTLS { |
| 86 |
options.NoTLS = false |
| 87 |
options.StartTLS = false |
| 88 |
slog.Info("setting xmpp connection option:", "NoTLS", options.NoTLS) |
| 89 |
slog.Info("setting xmpp connection option:", "StartTLS", options.StartTLS) |
| 90 |
options.Host = net.JoinHostPort(link.Sni, fmt.Sprint(link.Port)) |
| 91 |
// Connect to server |
| 92 |
slog.Info("connecting to", "host", options.Host) |
| 93 |
client, err = options.NewClient() |
| 94 |
if err == nil || strings.Contains(err.Error(), "not-authorized") { |
| 95 |
// Don't try to connect on other port if error contains |
| 96 |
// "not-authorized". |
| 97 |
return client, err |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
_, port, _ := net.SplitHostPort(options.Host) |
| 105 |
if port == "" { |
| 106 |
if options.Host == "" { |
| 107 |
options.Host = server |
| 108 |
} |
| 109 |
// Try port 5223 if directTLS is set and no port is provided. |
| 110 |
if directTLS { |
| 111 |
options.NoTLS = false |
| 112 |
options.StartTLS = false |
| 113 |
options.Host = net.JoinHostPort(options.Host, "5223") |
| 114 |
slog.Info("trying direct TLS fallback on port 5223") |
| 115 |
} else { |
| 116 |
// Try port 5222 if no port is provided and directTLS is not set. |
| 117 |
options.NoTLS = true |
| 118 |
options.StartTLS = true |
| 119 |
options.Host = net.JoinHostPort(options.Host, "5222") |
| 120 |
slog.Info("trying StartTLS fallback on port 5222") |
| 121 |
} |
| 122 |
} |
| 123 |
// Connect to server |
| 124 |
slog.Info("connecting to", "host", options.Host) |
| 125 |
client, err = options.NewClient() |
| 126 |
if err == nil { |
| 127 |
return client, err |
| 128 |
} |
| 129 |
if retryWait == 0 { |
| 130 |
slog.Info("giving up trying to connect…") |
| 131 |
break |
| 132 |
} |
| 133 |
if retryMax != 0 && retryMax == i { |
| 134 |
retry = false |
| 135 |
} |
| 136 |
// Reset options.Host as otherwise DNS look up is skipped. |
| 137 |
options.Host = "" |
| 138 |
sleepDuration := time.Duration(retryWait) * time.Second |
| 139 |
slog.Info("connecting failed, retry after", "sleep", sleepDuration) |
| 140 |
time.Sleep(sleepDuration) |
| 141 |
slog.Info("connecting to server:", "retry", i) |
| 142 |
} |
| 143 |
return client, fmt.Errorf("connect: failed to connect to server %s: %w", options.Host, err) |
| 144 |
} |