Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): pass DNS behind a proxy and retry discovery (#3564)

Hampusfc8b6e4abff1
diff --git a/fluxer_rexctl/internal/checks/discovery.go b/fluxer_rexctl/internal/checks/discovery.go
index 2ad830e1d..b28680066 100644
--- a/fluxer_rexctl/internal/checks/discovery.go
+++ b/fluxer_rexctl/internal/checks/discovery.go
@@ -8,6 +8,12 @@ import (
"fmt"
"net/url"
"strings"
+ "time"
+)
+
+const (
+ discoveryAttempts = 6
+ discoveryRetry = 2 * time.Second
)
func init() {
@@ -25,7 +31,15 @@ func runDiscovery(ctx context.Context, e *Env) Result {
if e.Instance == nil || e.Domain == "" || !e.running(ctx, "edge") || !e.running(ctx, "api") {
return skip("")
}
- out, err := e.exec(ctx, "edge", "", "wget", "-q", "-O", "-", "http://127.0.0.1:8088/.well-known/fluxer")
+ out, err := e.discoveryDoc(ctx)
+ for attempt := 1; err != nil && attempt < discoveryAttempts; attempt++ {
+ select {
+ case <-ctx.Done():
+ return skip("")
+ case <-time.After(discoveryRetry):
+ }
+ out, err = e.discoveryDoc(ctx)
+ }
if err != nil {
r := fail("Apps can't load your instance's details.", "Run rexctl logs api to see why.")
r.Details = []string{out}
@@ -47,6 +61,10 @@ func runDiscovery(ctx context.Context, e *Env) Result {
return pass(fmt.Sprintf("Apps are told to connect to %s.", e.Domain))
}
+func (e *Env) discoveryDoc(ctx context.Context) (string, error) {
+ return e.exec(ctx, "edge", "", "wget", "-q", "-O", "-", "http://127.0.0.1:8088/.well-known/fluxer")
+}
+
func endpointHost(s string) string {
u, err := url.Parse(strings.TrimSpace(s))
if err != nil {
diff --git a/fluxer_rexctl/internal/checks/dns.go b/fluxer_rexctl/internal/checks/dns.go
index 5241d8159..41ba9dfaa 100644
--- a/fluxer_rexctl/internal/checks/dns.go
+++ b/fluxer_rexctl/internal/checks/dns.go
@@ -4,9 +4,13 @@ package checks
import (
"context"
+ "encoding/json"
"errors"
"fmt"
+ "io"
"net"
+ "net/http"
+ "strings"
"time"
)
@@ -48,6 +52,9 @@ func runDNS(ctx context.Context, e *Env) Result {
}
return pass(fmt.Sprintf("%s points to this machine (%s).", e.Domain, shown))
}
+ if e.forwardsHere(ctx) {
+ return pass(fmt.Sprintf("%s points to %s, which forwards to this machine.", e.Domain, addrs[0].IP))
+ }
if perr != nil {
return skip(fmt.Sprintf("Couldn't find this machine's public IP address to compare with %s.", e.Domain))
}
@@ -75,3 +82,47 @@ func pointsHere(addrs []net.IPAddr, public string) bool {
}
return false
}
+
+func (e *Env) forwardsHere(ctx context.Context) bool {
+ if e.Instance == nil || !e.running(ctx, "edge") {
+ return false
+ }
+ local, err := e.discoveryDoc(ctx)
+ if err != nil {
+ return false
+ }
+ return servesInstance(ctx, e.Domain, local)
+}
+
+func servesInstance(ctx context.Context, domain, local string) bool {
+ ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
+ defer cancel()
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+domain+"/.well-known/fluxer", nil)
+ if err != nil {
+ return false
+ }
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return false
+ }
+ defer resp.Body.Close()
+ remote, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
+ if err != nil || resp.StatusCode != http.StatusOK {
+ return false
+ }
+ key := instanceKey(local)
+ return key != "" && key == instanceKey(string(remote))
+}
+
+func instanceKey(doc string) string {
+ var d struct {
+ Push struct {
+ Key string `json:"public_vapid_key"`
+ } `json:"push"`
+ }
+ start := strings.Index(doc, "{")
+ if start < 0 || json.Unmarshal([]byte(doc[start:]), &d) != nil {
+ return ""
+ }
+ return d.Push.Key
+}