2020-06-12 15:55:57 +02:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-01-02 11:26:56 +01: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 11:45:23 +01:00
|
|
|
package base
|
2018-01-02 11:26:56 +01:00
|
|
|
|
|
|
|
import (
|
2023-01-26 16:25:17 +01:00
|
|
|
"bytes"
|
2021-01-26 13:56:20 +01:00
|
|
|
"context"
|
2023-01-26 16:25:17 +01:00
|
|
|
"embed"
|
2022-10-04 14:02:41 +02:00
|
|
|
"encoding/json"
|
2023-03-01 22:57:30 +01:00
|
|
|
"errors"
|
2020-06-02 22:02:24 +02:00
|
|
|
"fmt"
|
2023-01-26 16:25:17 +01:00
|
|
|
"html/template"
|
2023-03-01 22:57:30 +01:00
|
|
|
"io/fs"
|
2020-10-20 18:13:12 +02:00
|
|
|
"net"
|
2018-01-02 11:26:56 +01:00
|
|
|
"net/http"
|
2022-04-27 15:05:49 +02:00
|
|
|
_ "net/http/pprof"
|
2021-01-26 13:56:20 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-05-01 02:38:36 +02:00
|
|
|
"sync/atomic"
|
2021-01-26 13:56:20 +01:00
|
|
|
"syscall"
|
2020-04-03 12:40:50 +02:00
|
|
|
"time"
|
2018-01-02 11:26:56 +01:00
|
|
|
|
2021-03-24 11:25:24 +01:00
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2023-04-06 10:55:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2020-08-13 13:16:37 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-01-02 11:26:56 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-04-27 14:36:40 +02:00
|
|
|
"github.com/kardianos/minwinsvc"
|
2023-03-22 09:21:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2018-01-02 11:26:56 +01:00
|
|
|
|
2022-08-30 13:59:13 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-09-27 18:06:49 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2018-01-02 11:26:56 +01:00
|
|
|
)
|
|
|
|
|
2023-01-26 16:25:17 +01:00
|
|
|
//go:embed static/*.gotmpl
|
|
|
|
var staticContent embed.FS
|
|
|
|
|
2024-01-17 17:08:57 +01:00
|
|
|
//go:embed static/client/login
|
|
|
|
var loginFallback embed.FS
|
|
|
|
|
2020-04-20 18:42:34 +02:00
|
|
|
const HTTPServerTimeout = time.Minute * 5
|
|
|
|
|
2020-10-01 12:55:17 +02:00
|
|
|
// CreateClient creates a new client (normally used for media fetch requests).
|
|
|
|
// Should only be called once per component.
|
2023-04-06 10:55:01 +02:00
|
|
|
func CreateClient(cfg *config.Dendrite, dnsCache *fclient.DNSCache) *fclient.Client {
|
2023-03-22 09:21:32 +01:00
|
|
|
if cfg.Global.DisableFederation {
|
2023-04-06 10:55:01 +02:00
|
|
|
return fclient.NewClient(
|
|
|
|
fclient.WithTransport(noOpHTTPTransport),
|
2021-01-22 17:09:05 +01:00
|
|
|
)
|
|
|
|
}
|
2023-04-06 10:55:01 +02:00
|
|
|
opts := []fclient.ClientOption{
|
|
|
|
fclient.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
|
|
|
|
fclient.WithWellKnownSRVLookups(true),
|
2020-12-02 16:10:03 +01:00
|
|
|
}
|
2023-03-22 09:21:32 +01:00
|
|
|
if cfg.Global.DNSCache.Enabled && dnsCache != nil {
|
2023-04-06 10:55:01 +02:00
|
|
|
opts = append(opts, fclient.WithDNSCache(dnsCache))
|
2021-01-22 15:16:59 +01:00
|
|
|
}
|
2023-04-06 10:55:01 +02:00
|
|
|
client := fclient.NewClient(opts...)
|
2020-10-01 12:55:17 +02:00
|
|
|
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2018-01-02 11:26:56 +01:00
|
|
|
// CreateFederationClient creates a new federation client. Should only be called
|
|
|
|
// once per component.
|
2023-04-24 18:23:25 +02:00
|
|
|
func CreateFederationClient(cfg *config.Dendrite, dnsCache *fclient.DNSCache) fclient.FederationClient {
|
2023-03-22 09:21:32 +01:00
|
|
|
identities := cfg.Global.SigningIdentities()
|
|
|
|
if cfg.Global.DisableFederation {
|
2023-04-06 10:55:01 +02:00
|
|
|
return fclient.NewFederationClient(
|
|
|
|
identities, fclient.WithTransport(noOpHTTPTransport),
|
2020-12-02 16:10:03 +01:00
|
|
|
)
|
|
|
|
}
|
2023-04-06 10:55:01 +02:00
|
|
|
opts := []fclient.ClientOption{
|
|
|
|
fclient.WithTimeout(time.Minute * 5),
|
|
|
|
fclient.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
|
|
|
|
fclient.WithKeepAlives(!cfg.FederationAPI.DisableHTTPKeepalives),
|
2023-04-24 18:23:25 +02:00
|
|
|
fclient.WithUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString())),
|
2021-01-22 17:09:05 +01:00
|
|
|
}
|
2023-03-22 09:21:32 +01:00
|
|
|
if cfg.Global.DNSCache.Enabled {
|
2023-04-06 10:55:01 +02:00
|
|
|
opts = append(opts, fclient.WithDNSCache(dnsCache))
|
2021-01-22 15:16:59 +01:00
|
|
|
}
|
2023-04-06 10:55:01 +02:00
|
|
|
client := fclient.NewFederationClient(
|
2022-11-15 16:05:23 +01:00
|
|
|
identities, opts...,
|
2018-01-02 11:26:56 +01:00
|
|
|
)
|
2020-10-01 12:55:17 +02:00
|
|
|
return client
|
2018-01-02 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
func ConfigureAdminEndpoints(processContext *process.ProcessContext, routers httputil.Routers) {
|
|
|
|
routers.DendriteAdmin.HandleFunc("/monitor/up", func(w http.ResponseWriter, r *http.Request) {
|
2022-12-01 11:45:15 +01:00
|
|
|
w.WriteHeader(200)
|
|
|
|
})
|
2023-03-22 09:21:32 +01:00
|
|
|
routers.DendriteAdmin.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if isDegraded, reasons := processContext.IsDegraded(); isDegraded {
|
2022-12-01 11:45:15 +01:00
|
|
|
w.WriteHeader(503)
|
|
|
|
_ = json.NewEncoder(w).Encode(struct {
|
|
|
|
Warnings []string `json:"warnings"`
|
|
|
|
}{
|
|
|
|
Warnings: reasons,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:47:47 +01:00
|
|
|
// SetupAndServeHTTP sets up the HTTP server to serve client & federation APIs
|
|
|
|
// and adds a prometheus handler under /_dendrite/metrics.
|
2023-03-22 09:21:32 +01:00
|
|
|
func SetupAndServeHTTP(
|
|
|
|
processContext *process.ProcessContext,
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
routers httputil.Routers,
|
2023-03-01 22:57:30 +01:00
|
|
|
externalHTTPAddr config.ServerAddress,
|
2020-08-13 13:16:37 +02:00
|
|
|
certFile, keyFile *string,
|
|
|
|
) {
|
2020-10-07 17:59:22 +02:00
|
|
|
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2020-08-13 13:16:37 +02:00
|
|
|
|
2020-10-07 17:59:22 +02:00
|
|
|
externalServ := &http.Server{
|
2023-03-01 22:57:30 +01:00
|
|
|
Addr: externalHTTPAddr.Address,
|
2020-04-20 18:42:34 +02:00
|
|
|
WriteTimeout: HTTPServerTimeout,
|
2020-10-07 17:59:22 +02:00
|
|
|
Handler: externalRouter,
|
2022-04-27 16:01:57 +02:00
|
|
|
BaseContext: func(_ net.Listener) context.Context {
|
2023-03-22 09:21:32 +01:00
|
|
|
return processContext.Context()
|
2022-04-27 16:01:57 +02:00
|
|
|
},
|
2020-08-13 13:16:37 +02:00
|
|
|
}
|
2018-01-02 11:26:56 +01:00
|
|
|
|
2023-01-26 16:25:17 +01: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 09:21:32 +01:00
|
|
|
if cfg.Global.Metrics.Enabled {
|
|
|
|
externalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Global.Metrics.BasicAuth))
|
2020-08-13 13:16:37 +02:00
|
|
|
}
|
2018-01-02 11:26:56 +01:00
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
ConfigureAdminEndpoints(processContext, routers)
|
2022-03-25 14:25:15 +01:00
|
|
|
|
2023-01-26 16:25:17 +01: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 09:21:32 +01:00
|
|
|
routers.Static.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-01-26 16:25:17 +01:00
|
|
|
_, _ = w.Write(landingPage.Bytes())
|
|
|
|
})
|
|
|
|
|
2024-01-17 17:08:57 +01: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 11:25:24 +01:00
|
|
|
var clientHandler http.Handler
|
2023-03-22 09:21:32 +01:00
|
|
|
clientHandler = routers.Client
|
|
|
|
if cfg.Global.Sentry.Enabled {
|
2021-03-24 11:25:24 +01:00
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-22 09:21:32 +01:00
|
|
|
clientHandler = sentryHandler.Handle(routers.Client)
|
2021-03-24 11:25:24 +01:00
|
|
|
}
|
|
|
|
var federationHandler http.Handler
|
2023-03-22 09:21:32 +01:00
|
|
|
federationHandler = routers.Federation
|
|
|
|
if cfg.Global.Sentry.Enabled {
|
2021-03-24 11:25:24 +01:00
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-22 09:21:32 +01:00
|
|
|
federationHandler = sentryHandler.Handle(routers.Federation)
|
2021-03-24 11:25:24 +01:00
|
|
|
}
|
2023-03-22 09:21:32 +01:00
|
|
|
externalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(routers.DendriteAdmin)
|
2021-03-24 11:25:24 +01:00
|
|
|
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(clientHandler)
|
2023-03-22 09:21:32 +01:00
|
|
|
if !cfg.Global.DisableFederation {
|
|
|
|
externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(routers.Keys)
|
2021-03-24 11:25:24 +01:00
|
|
|
externalRouter.PathPrefix(httputil.PublicFederationPathPrefix).Handler(federationHandler)
|
2020-12-02 16:10:03 +01:00
|
|
|
}
|
2023-03-22 09:21:32 +01: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 13:16:37 +02:00
|
|
|
|
2023-03-17 12:09:45 +01:00
|
|
|
externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler
|
|
|
|
externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler
|
|
|
|
|
2023-03-01 22:57:30 +01:00
|
|
|
if externalHTTPAddr.Enabled() {
|
2020-08-13 13:16:37 +02:00
|
|
|
go func() {
|
2021-01-26 13:56:20 +01:00
|
|
|
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
|
2023-02-14 12:47:47 +01:00
|
|
|
logrus.Infof("Starting external listener on %s", externalServ.Addr)
|
2023-03-22 09:21:32 +01:00
|
|
|
processContext.ComponentStarted()
|
2021-01-26 13:56:20 +01:00
|
|
|
externalServ.RegisterOnShutdown(func() {
|
2022-09-20 16:01:19 +02:00
|
|
|
if externalShutdown.CompareAndSwap(false, true) {
|
2023-03-22 09:21:32 +01:00
|
|
|
processContext.ComponentFinished()
|
2021-01-26 13:56:20 +01:00
|
|
|
logrus.Infof("Stopped external HTTP listener")
|
|
|
|
}
|
|
|
|
})
|
2020-08-13 13:16:37 +02:00
|
|
|
if certFile != nil && keyFile != nil {
|
2020-08-13 19:27:19 +02:00
|
|
|
if err := externalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
2021-01-26 13:56:20 +01:00
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
|
|
|
}
|
2020-08-13 13:16:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-01 22:57:30 +01: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 13:56:20 +01:00
|
|
|
}
|
2020-08-13 13:16:37 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 12:47:47 +01:00
|
|
|
logrus.Infof("Stopped external listener on %s", externalServ.Addr)
|
2020-08-13 13:16:37 +02:00
|
|
|
}()
|
2018-01-02 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
minwinsvc.SetOnExit(processContext.ShutdownDendrite)
|
|
|
|
<-processContext.WaitForShutdown()
|
2022-04-27 17:04:11 +02:00
|
|
|
|
2022-04-27 16:29:49 +02:00
|
|
|
logrus.Infof("Stopping HTTP listeners")
|
|
|
|
_ = externalServ.Shutdown(context.Background())
|
2021-01-26 13:56:20 +01:00
|
|
|
logrus.Infof("Stopped HTTP listeners")
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
func WaitForShutdown(processCtx *process.ProcessContext) {
|
2021-01-26 13:56:20 +01:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2022-04-27 14:36:40 +02:00
|
|
|
select {
|
|
|
|
case <-sigs:
|
2023-03-22 09:21:32 +01:00
|
|
|
case <-processCtx.WaitForShutdown():
|
2022-04-27 14:36:40 +02:00
|
|
|
}
|
2021-01-26 13:56:20 +01:00
|
|
|
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
logrus.Warnf("Shutdown signal received")
|
|
|
|
|
2023-03-22 09:21:32 +01:00
|
|
|
processCtx.ShutdownDendrite()
|
|
|
|
processCtx.WaitForComponentsToFinish()
|
2021-01-26 13:56:20 +01:00
|
|
|
|
|
|
|
logrus.Warnf("Dendrite is exiting now")
|
2018-01-02 11:26:56 +01:00
|
|
|
}
|