2020-06-12 14:55:57 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-01-02 10:26:56 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-11-24 10:45:23 +00:00
|
|
|
package base
|
2018-01-02 10:26:56 +00:00
|
|
|
|
|
|
|
import (
|
2023-01-26 15:25:17 +00:00
|
|
|
"bytes"
|
2021-01-26 12:56:20 +00:00
|
|
|
"context"
|
2023-01-26 15:25:17 +00:00
|
|
|
"embed"
|
2022-10-04 13:02:41 +01:00
|
|
|
"encoding/json"
|
2023-03-01 21:57:30 +00:00
|
|
|
"errors"
|
2020-06-02 21:02:24 +01:00
|
|
|
"fmt"
|
2023-01-26 15:25:17 +00:00
|
|
|
"html/template"
|
2023-03-01 21:57:30 +00:00
|
|
|
"io/fs"
|
2020-10-20 17:13:12 +01:00
|
|
|
"net"
|
2018-01-02 10:26:56 +00:00
|
|
|
"net/http"
|
2022-04-27 14:05:49 +01:00
|
|
|
_ "net/http/pprof"
|
2021-01-26 12:56:20 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-05-01 01:38:36 +01:00
|
|
|
"sync/atomic"
|
2021-01-26 12:56:20 +00:00
|
|
|
"syscall"
|
2020-04-03 11:40:50 +01:00
|
|
|
"time"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2020-08-13 12:16:37 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-04-27 13:36:40 +01:00
|
|
|
"github.com/kardianos/minwinsvc"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2022-08-30 12:59:13 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-09-27 17:06:49 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2018-01-02 10:26:56 +00:00
|
|
|
)
|
|
|
|
|
2023-01-26 15:25:17 +00:00
|
|
|
//go:embed static/*.gotmpl
|
|
|
|
var staticContent embed.FS
|
|
|
|
|
2024-01-17 16:08:57 +00:00
|
|
|
//go:embed static/client/login
|
|
|
|
var loginFallback embed.FS
|
|
|
|
|
2020-04-20 17:42:34 +01:00
|
|
|
const HTTPServerTimeout = time.Minute * 5
|
|
|
|
|
2020-10-01 11:55:17 +01:00
|
|
|
// CreateClient creates a new client (normally used for media fetch requests).
|
|
|
|
// Should only be called once per component.
|
2023-04-06 09:55:01 +01:00
|
|
|
func CreateClient(cfg *config.Dendrite, dnsCache *fclient.DNSCache) *fclient.Client {
|
2023-03-22 08:21:32 +00:00
|
|
|
if cfg.Global.DisableFederation {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.NewClient(
|
|
|
|
fclient.WithTransport(noOpHTTPTransport),
|
2021-01-22 16:09:05 +00:00
|
|
|
)
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
opts := []fclient.ClientOption{
|
|
|
|
fclient.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
|
|
|
|
fclient.WithWellKnownSRVLookups(true),
|
2020-12-02 15:10:03 +00:00
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
if cfg.Global.DNSCache.Enabled && dnsCache != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
opts = append(opts, fclient.WithDNSCache(dnsCache))
|
2021-01-22 14:16:59 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
client := fclient.NewClient(opts...)
|
2020-10-01 11:55:17 +01:00
|
|
|
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// CreateFederationClient creates a new federation client. Should only be called
|
|
|
|
// once per component.
|
2023-04-24 17:23:25 +01:00
|
|
|
func CreateFederationClient(cfg *config.Dendrite, dnsCache *fclient.DNSCache) fclient.FederationClient {
|
2023-03-22 08:21:32 +00:00
|
|
|
identities := cfg.Global.SigningIdentities()
|
|
|
|
if cfg.Global.DisableFederation {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.NewFederationClient(
|
|
|
|
identities, fclient.WithTransport(noOpHTTPTransport),
|
2020-12-02 15:10:03 +00:00
|
|
|
)
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
opts := []fclient.ClientOption{
|
|
|
|
fclient.WithTimeout(time.Minute * 5),
|
|
|
|
fclient.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
|
|
|
|
fclient.WithKeepAlives(!cfg.FederationAPI.DisableHTTPKeepalives),
|
2023-04-24 17:23:25 +01:00
|
|
|
fclient.WithUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString())),
|
2021-01-22 16:09:05 +00:00
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
if cfg.Global.DNSCache.Enabled {
|
2023-04-06 09:55:01 +01:00
|
|
|
opts = append(opts, fclient.WithDNSCache(dnsCache))
|
2021-01-22 14:16:59 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
client := fclient.NewFederationClient(
|
2022-11-15 15:05:23 +00:00
|
|
|
identities, opts...,
|
2018-01-02 10:26:56 +00:00
|
|
|
)
|
2020-10-01 11:55:17 +01:00
|
|
|
return client
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
func ConfigureAdminEndpoints(processContext *process.ProcessContext, routers httputil.Routers) {
|
|
|
|
routers.DendriteAdmin.HandleFunc("/monitor/up", func(w http.ResponseWriter, r *http.Request) {
|
2022-12-01 10:45:15 +00:00
|
|
|
w.WriteHeader(200)
|
|
|
|
})
|
2023-03-22 08:21:32 +00:00
|
|
|
routers.DendriteAdmin.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if isDegraded, reasons := processContext.IsDegraded(); isDegraded {
|
2022-12-01 10:45:15 +00:00
|
|
|
w.WriteHeader(503)
|
|
|
|
_ = json.NewEncoder(w).Encode(struct {
|
|
|
|
Warnings []string `json:"warnings"`
|
|
|
|
}{
|
|
|
|
Warnings: reasons,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
// SetupAndServeHTTP sets up the HTTP server to serve client & federation APIs
|
|
|
|
// and adds a prometheus handler under /_dendrite/metrics.
|
2023-03-22 08:21:32 +00:00
|
|
|
func SetupAndServeHTTP(
|
|
|
|
processContext *process.ProcessContext,
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
routers httputil.Routers,
|
2023-03-01 21:57:30 +00:00
|
|
|
externalHTTPAddr config.ServerAddress,
|
2020-08-13 12:16:37 +01:00
|
|
|
certFile, keyFile *string,
|
|
|
|
) {
|
2020-10-07 16:59:22 +01:00
|
|
|
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2020-08-13 12:16:37 +01:00
|
|
|
|
2020-10-07 16:59:22 +01:00
|
|
|
externalServ := &http.Server{
|
2023-03-01 21:57:30 +00:00
|
|
|
Addr: externalHTTPAddr.Address,
|
2020-04-20 17:42:34 +01:00
|
|
|
WriteTimeout: HTTPServerTimeout,
|
2020-10-07 16:59:22 +01:00
|
|
|
Handler: externalRouter,
|
2022-04-27 15:01:57 +01:00
|
|
|
BaseContext: func(_ net.Listener) context.Context {
|
2023-03-22 08:21:32 +00:00
|
|
|
return processContext.Context()
|
2022-04-27 15:01:57 +01:00
|
|
|
},
|
2020-08-13 12:16:37 +01:00
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2023-01-26 15:25:17 +00:00
|
|
|
//Redirect for Landing Page
|
|
|
|
externalRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, httputil.PublicStaticPath, http.StatusFound)
|
|
|
|
})
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
if cfg.Global.Metrics.Enabled {
|
|
|
|
externalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Global.Metrics.BasicAuth))
|
2020-08-13 12:16:37 +01:00
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
ConfigureAdminEndpoints(processContext, routers)
|
2022-03-25 13:25:15 +00:00
|
|
|
|
2023-01-26 15:25:17 +00:00
|
|
|
// Parse and execute the landing page template
|
|
|
|
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
|
|
|
|
landingPage := &bytes.Buffer{}
|
|
|
|
if err := tmpl.ExecuteTemplate(landingPage, "index.gotmpl", map[string]string{
|
|
|
|
"Version": internal.VersionString(),
|
|
|
|
}); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("failed to execute landing page template")
|
|
|
|
}
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
routers.Static.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-01-26 15:25:17 +00:00
|
|
|
_, _ = w.Write(landingPage.Bytes())
|
|
|
|
})
|
|
|
|
|
2024-01-17 16:08:57 +00:00
|
|
|
// We only need the files beneath the static/client/login folder.
|
|
|
|
sub, err := fs.Sub(loginFallback, "static/client/login")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Panicf("unable to read embedded files, this should never happen: %s", err)
|
|
|
|
}
|
|
|
|
// Serve a static page for login fallback
|
|
|
|
routers.Static.PathPrefix("/client/login/").Handler(http.StripPrefix("/_matrix/static/client/login/", http.FileServer(http.FS(sub))))
|
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
var clientHandler http.Handler
|
2023-03-22 08:21:32 +00:00
|
|
|
clientHandler = routers.Client
|
|
|
|
if cfg.Global.Sentry.Enabled {
|
2021-03-24 10:25:24 +00:00
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-22 08:21:32 +00:00
|
|
|
clientHandler = sentryHandler.Handle(routers.Client)
|
2021-03-24 10:25:24 +00:00
|
|
|
}
|
|
|
|
var federationHandler http.Handler
|
2023-03-22 08:21:32 +00:00
|
|
|
federationHandler = routers.Federation
|
|
|
|
if cfg.Global.Sentry.Enabled {
|
2021-03-24 10:25:24 +00:00
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-22 08:21:32 +00:00
|
|
|
federationHandler = sentryHandler.Handle(routers.Federation)
|
2021-03-24 10:25:24 +00:00
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
externalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(routers.DendriteAdmin)
|
2021-03-24 10:25:24 +00:00
|
|
|
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(clientHandler)
|
2023-03-22 08:21:32 +00:00
|
|
|
if !cfg.Global.DisableFederation {
|
|
|
|
externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(routers.Keys)
|
2021-03-24 10:25:24 +00:00
|
|
|
externalRouter.PathPrefix(httputil.PublicFederationPathPrefix).Handler(federationHandler)
|
2020-12-02 15:10:03 +00:00
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
externalRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(routers.SynapseAdmin)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(routers.WellKnown)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicStaticPath).Handler(routers.Static)
|
2020-08-13 12:16:37 +01:00
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler
|
|
|
|
externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler
|
|
|
|
|
2023-03-01 21:57:30 +00:00
|
|
|
if externalHTTPAddr.Enabled() {
|
2020-08-13 12:16:37 +01:00
|
|
|
go func() {
|
2021-01-26 12:56:20 +00:00
|
|
|
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
|
2023-02-14 11:47:47 +00:00
|
|
|
logrus.Infof("Starting external listener on %s", externalServ.Addr)
|
2023-03-22 08:21:32 +00:00
|
|
|
processContext.ComponentStarted()
|
2021-01-26 12:56:20 +00:00
|
|
|
externalServ.RegisterOnShutdown(func() {
|
2022-09-20 15:01:19 +01:00
|
|
|
if externalShutdown.CompareAndSwap(false, true) {
|
2023-03-22 08:21:32 +00:00
|
|
|
processContext.ComponentFinished()
|
2021-01-26 12:56:20 +00:00
|
|
|
logrus.Infof("Stopped external HTTP listener")
|
|
|
|
}
|
|
|
|
})
|
2020-08-13 12:16:37 +01:00
|
|
|
if certFile != nil && keyFile != nil {
|
2020-08-13 18:27:19 +01:00
|
|
|
if err := externalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
2021-01-26 12:56:20 +00:00
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
|
|
|
}
|
2020-08-13 12:16:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-01 21:57:30 +00:00
|
|
|
if externalHTTPAddr.IsUnixSocket() {
|
|
|
|
err := os.Remove(externalHTTPAddr.Address)
|
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
logrus.WithError(err).Fatal("failed to remove existing unix socket")
|
|
|
|
}
|
|
|
|
listener, err := net.Listen(externalHTTPAddr.Network(), externalHTTPAddr.Address)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve unix socket")
|
|
|
|
}
|
|
|
|
err = os.Chmod(externalHTTPAddr.Address, externalHTTPAddr.UnixSocketPermission)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("failed to set unix socket permissions")
|
|
|
|
}
|
|
|
|
if err := externalServ.Serve(listener); err != nil {
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve unix socket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if err := externalServ.ListenAndServe(); err != nil {
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTP")
|
|
|
|
}
|
2021-01-26 12:56:20 +00:00
|
|
|
}
|
2020-08-13 12:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 11:47:47 +00:00
|
|
|
logrus.Infof("Stopped external listener on %s", externalServ.Addr)
|
2020-08-13 12:16:37 +01:00
|
|
|
}()
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
minwinsvc.SetOnExit(processContext.ShutdownDendrite)
|
|
|
|
<-processContext.WaitForShutdown()
|
2022-04-27 16:04:11 +01:00
|
|
|
|
2022-04-27 15:29:49 +01:00
|
|
|
logrus.Infof("Stopping HTTP listeners")
|
|
|
|
_ = externalServ.Shutdown(context.Background())
|
2021-01-26 12:56:20 +00:00
|
|
|
logrus.Infof("Stopped HTTP listeners")
|
|
|
|
}
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
func WaitForShutdown(processCtx *process.ProcessContext) {
|
2021-01-26 12:56:20 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2022-04-27 13:36:40 +01:00
|
|
|
select {
|
|
|
|
case <-sigs:
|
2023-03-22 08:21:32 +00:00
|
|
|
case <-processCtx.WaitForShutdown():
|
2022-04-27 13:36:40 +01:00
|
|
|
}
|
2021-01-26 12:56:20 +00:00
|
|
|
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
logrus.Warnf("Shutdown signal received")
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
processCtx.ShutdownDendrite()
|
|
|
|
processCtx.WaitForComponentsToFinish()
|
2021-01-26 12:56:20 +00:00
|
|
|
|
|
|
|
logrus.Warnf("Dendrite is exiting now")
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|