diff --git a/README.md b/README.md index 0889c2a..b0ca321 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,11 @@ or the [Telegram group](https://t.me/+6UwIu9F4gZQxNjM0). which get through and which a firewall seems to drop (UDP can't be tested this way), and once the server is installed it checks federation the way a federation tester does: `/.well-known/matrix/server`, then the - server's signing keys over a trusted certificate. Once you've deployed, it shows what + server's signing keys over a trusted certificate. It looks at the + server as apps do too: `/.well-known/matrix/client` (with the CORS + header web apps need) and the client API it points to, and whether + Element X will work: sliding sync, OAuth 2.0 logins through MAS, and a + call server in the well-known when Matrix RTC is on. Once you've deployed, it shows what changes in `vars.yml` since the last successful deploy (`x` switches to the whole file). - **Checks after deploying.** The playbook's self-check runs at the end and diff --git a/internal/deploy/reach.go b/internal/deploy/reach.go index c934109..07acfd5 100644 --- a/internal/deploy/reach.go +++ b/internal/deploy/reach.go @@ -2,6 +2,7 @@ package deploy import ( "context" + "crypto/tls" "encoding/json" "errors" "fmt" @@ -23,7 +24,8 @@ func reachChecks(ctx context.Context, c *config.Config) []Finding { if c.Domain == "" { return nil } - return append(portChecks(ctx, c), federationChecks(ctx, c)...) + out := append(portChecks(ctx, c), federationChecks(ctx, c)...) + return append(out, clientChecks(ctx, c)...) } // portChecks connects to every TCP port Ports lists, at the homeserver's @@ -82,24 +84,9 @@ func federationChecks(ctx context.Context, c *config.Config) []Finding { add := func(name string, l Level, format string, a ...any) { out = append(out, Finding{Group: "Federation", Name: name, Level: l, Detail: fmt.Sprintf(format, a...)}) } - client := &http.Client{Timeout: 10 * time.Second, Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}} get := func(url string, v any) error { - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { - return err - } - resp, err := client.Do(req) - if err != nil { - return errors.New(netReason(err)) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return errors.New("answered " + resp.Status) - } - if json.NewDecoder(resp.Body).Decode(v) != nil { - return errors.New("didn't answer JSON") - } - return nil + _, err := getJSON(ctx, url, false, v) + return err } // shortcut: SRV records aren't looked up; the playbook delegates with .well-known. @@ -133,3 +120,99 @@ func federationChecks(ctx context.Context, c *config.Config) []Finding { } return out } + +// getJSON GETs url and decodes its JSON answer into v. +func getJSON(ctx context.Context, url string, insecure bool, v any) (http.Header, error) { + client := &http.Client{Timeout: 10 * time.Second, Transport: &http.Transport{Proxy: http.ProxyFromEnvironment, + TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}}} + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + resp, err := client.Do(req) + if err != nil { + return nil, errors.New(netReason(err)) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return resp.Header, errors.New("answered " + resp.Status) + } + if json.NewDecoder(resp.Body).Decode(v) != nil { + return resp.Header, errors.New("didn't answer JSON") + } + return resp.Header, nil +} + +// clientChecks looks at the server as apps do: /.well-known/matrix/client +// sends them to the homeserver, and Element X needs more than older apps: +// sliding sync, OAuth 2.0 logins (MAS), and for calls Matrix RTC. +func clientChecks(ctx context.Context, c *config.Config) []Finding { + if !c.Installed() { + return nil + } + var out []Finding + add := func(name string, l Level, format string, a ...any) { + out = append(out, Finding{Group: "Clients", Name: name, Level: l, Detail: fmt.Sprintf(format, a...)}) + } + scheme, insecure := "https://", c.SSL.Mode == "selfsigned" + if c.SSL.Mode == "none" { + scheme = "http://" + } + var wk struct { + Homeserver struct { + BaseURL string `json:"base_url"` + } `json:"m.homeserver"` + Auth *struct { + Issuer string `json:"issuer"` + } `json:"org.matrix.msc2965.authentication"` + Foci []json.RawMessage `json:"org.matrix.msc4143.rtc_foci"` + } + where := scheme + c.Domain + "/.well-known/matrix/client" + base := scheme + c.HostName() + switch h, err := getJSON(ctx, where, insecure, &wk); { + case err != nil: + add("Well-known", Warn, "%s %s; apps then need %s typed in instead of %s", where, err, c.HostName(), c.Domain) + case wk.Homeserver.BaseURL == "": + add("Well-known", Fail, "%s has no m.homeserver.base_url", where) + default: + base = strings.TrimRight(wk.Homeserver.BaseURL, "/") + if h.Get("Access-Control-Allow-Origin") == "" { + add("Well-known", Warn, "%s sends apps to %s, but without the CORS header web apps need", where, base) + } else { + add("Well-known", Pass, "sends apps to %s", base) + } + } + + var versions struct { + Unstable map[string]bool `json:"unstable_features"` + } + if _, err := getJSON(ctx, base+"/_matrix/client/versions", insecure, &versions); err != nil { + add("Client API", Fail, "%s/_matrix/client/versions %s", base, err) + return out + } + add("Client API", Pass, "%s answers", base) + + if versions.Unstable["org.matrix.simplified_msc3575"] { + add("Element X sync", Pass, "sliding sync is on") + } else { + add("Element X sync", Fail, "the homeserver doesn't offer sliding sync, which Element X needs") + } + var meta map[string]any + if wk.Auth != nil && wk.Auth.Issuer != "" { + add("Element X login", Pass, "OAuth 2.0 logins through %s", wk.Auth.Issuer) + } else if _, err := getJSON(ctx, base+"/_matrix/client/v1/auth_metadata", insecure, &meta); err == nil { + add("Element X login", Pass, "OAuth 2.0 logins") + } else { + add("Element X login", Warn, "no OAuth 2.0 logins: Element X needs the Matrix Authentication Service (MAS) on Components") + } + rtc := c.Components["matrix-rtc"] || c.Components["element-call"] + switch { + case len(wk.Foci) > 0: + add("Element X calls", Pass, "the well-known names a call server") + case rtc: + add("Element X calls", Fail, "Matrix RTC is on, but the well-known names no call server; deploy again") + default: + add("Element X calls", Warn, "calls need the Matrix RTC stack or Element Call on Components") + } + return out +} diff --git a/internal/deploy/reach_test.go b/internal/deploy/reach_test.go index 75be415..434280f 100644 --- a/internal/deploy/reach_test.go +++ b/internal/deploy/reach_test.go @@ -2,8 +2,17 @@ package deploy import ( "context" + "fmt" + "maps" "net" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" "testing" + + "interzone/internal/config" ) func TestDialPort(t *testing.T) { @@ -25,3 +34,33 @@ func TestDialPort(t *testing.T) { t.Errorf("open port: %v %s", lv, d) } } + +func TestClientChecks(t *testing.T) { + var srv *httptest.Server + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/.well-known/matrix/client": // no CORS header, no auth, no call server + fmt.Fprintf(w, `{"m.homeserver": {"base_url": %q}}`, srv.URL) + case "/_matrix/client/versions": + fmt.Fprint(w, `{"versions": ["v1.11"], "unstable_features": {}}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + c := config.Default() + c.Domain, c.SSL.Mode = strings.TrimPrefix(srv.URL, "http://"), "none" + c.Components = map[string]bool{"matrix-rtc": true} + c.PlaybookDir = t.TempDir() + vy := filepath.Join(c.PlaybookDir, "inventory", "host_vars", c.HostName(), "vars.yml") + _ = os.MkdirAll(filepath.Dir(vy), 0o755) + _ = os.WriteFile(vy, nil, 0o644) + got := map[string]Level{} + for _, f := range clientChecks(context.Background(), c) { + got[f.Name] = f.Level + } + want := map[string]Level{"Well-known": Warn, "Client API": Pass, "Element X sync": Fail, "Element X login": Warn, "Element X calls": Fail} + if !maps.Equal(got, want) { + t.Errorf("got %v", got) + } +} diff --git a/internal/deploy/server.go b/internal/deploy/server.go index 6a2cbdd..09130d0 100644 --- a/internal/deploy/server.go +++ b/internal/deploy/server.go @@ -25,7 +25,7 @@ const ( // Finding is the outcome of one pre-deploy check. type Finding struct { - Group string // "Server", "DNS", "Ports" or "Federation" + Group string // "Server", "DNS", "Ports", "Federation" or "Clients" Name string Service string // DNS: what the hostname serves; Ports: what the port is for Level Level diff --git a/internal/ui/screens_deploy.go b/internal/ui/screens_deploy.go index 942abd4..b815545 100644 --- a/internal/ui/screens_deploy.go +++ b/internal/ui/screens_deploy.go @@ -320,8 +320,8 @@ func (s *reviewScreen) checksView(w int) []string { dnsW = max(dnsW, min(34, len(f.Name)+2)) } } - collapsed := map[string]string{"Server": "%d server checks passed", "Ports": "%d ports get through", "Federation": "%d federation checks passed"} - for _, g := range []string{"Server", "DNS", "Ports", "Federation"} { + collapsed := map[string]string{"Server": "%d server checks passed", "Ports": "%d ports get through", "Federation": "%d federation checks passed", "Clients": "%d app checks passed"} + for _, g := range []string{"Server", "DNS", "Ports", "Federation", "Clients"} { passed := 0 var rows []string for _, f := range s.checks {