2020-06-12 13:55:57 +00: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 12:02:41 +00:00
|
|
|
"encoding/json"
|
2023-03-01 21:57:30 +00:00
|
|
|
"errors"
|
2020-06-02 20:02:24 +00:00
|
|
|
"fmt"
|
2023-01-26 15:25:17 +00:00
|
|
|
"html/template"
|
2018-01-02 10:26:56 +00:00
|
|
|
"io"
|
2023-03-01 21:57:30 +00:00
|
|
|
"io/fs"
|
2020-10-20 16:13:12 +00:00
|
|
|
"net"
|
2018-01-02 10:26:56 +00:00
|
|
|
"net/http"
|
2022-04-27 13:05:49 +00:00
|
|
|
_ "net/http/pprof"
|
2021-01-26 12:56:20 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-08-30 11:59:13 +00:00
|
|
|
"sync"
|
2021-01-26 12:56:20 +00:00
|
|
|
"syscall"
|
2020-04-03 10:40:50 +00:00
|
|
|
"time"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2018-01-02 10:26:56 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-08-13 11:16:37 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-01-26 12:56:20 +00:00
|
|
|
"go.uber.org/atomic"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2022-09-27 16:06:49 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2022-08-30 11:59:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-04-27 12:36:40 +00:00
|
|
|
"github.com/kardianos/minwinsvc"
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2022-08-30 11:59:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-09-27 16:06:49 +00: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
|
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// BaseDendrite is a base for creating new instances of dendrite. It parses
|
|
|
|
// command line flags and config, and exposes methods for creating various
|
|
|
|
// resources. All errors are handled by logging then exiting, so all methods
|
|
|
|
// should only be used during start up.
|
|
|
|
// Must be closed when shutting down.
|
|
|
|
type BaseDendrite struct {
|
2021-01-26 12:56:20 +00:00
|
|
|
*process.ProcessContext
|
2023-03-17 11:09:45 +00:00
|
|
|
tracerCloser io.Closer
|
|
|
|
Routers httputil.Routers
|
|
|
|
NATS *jetstream.NATSInstance
|
|
|
|
Cfg *config.Dendrite
|
|
|
|
DNSCache *gomatrixserverlib.DNSCache
|
|
|
|
ConnectionManager sqlutil.Connections
|
|
|
|
EnableMetrics bool
|
|
|
|
startupLock sync.Mutex
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 16:42:34 +00:00
|
|
|
const HTTPServerTimeout = time.Minute * 5
|
|
|
|
|
2021-11-24 10:45:23 +00:00
|
|
|
type BaseDendriteOptions int
|
|
|
|
|
|
|
|
const (
|
2022-05-09 16:23:02 +00:00
|
|
|
DisableMetrics BaseDendriteOptions = iota
|
2021-11-24 10:45:23 +00:00
|
|
|
)
|
2020-08-13 11:16:37 +00:00
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
// NewBaseDendrite creates a new instance to be used by a component.
|
2023-02-14 11:47:47 +00:00
|
|
|
func NewBaseDendrite(cfg *config.Dendrite, options ...BaseDendriteOptions) *BaseDendrite {
|
2022-03-11 17:27:12 +00:00
|
|
|
platformSanityChecks()
|
2022-05-09 16:23:02 +00:00
|
|
|
enableMetrics := true
|
2021-11-24 10:45:23 +00:00
|
|
|
for _, opt := range options {
|
|
|
|
switch opt {
|
2022-05-09 16:23:02 +00:00
|
|
|
case DisableMetrics:
|
|
|
|
enableMetrics = false
|
2021-11-24 10:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 13:18:04 +00:00
|
|
|
configErrors := &config.ConfigErrors{}
|
2023-02-14 11:47:47 +00:00
|
|
|
cfg.Verify(configErrors)
|
2020-08-10 13:18:04 +00:00
|
|
|
if len(*configErrors) > 0 {
|
|
|
|
for _, err := range *configErrors {
|
|
|
|
logrus.Errorf("Configuration error: %s", err)
|
|
|
|
}
|
|
|
|
logrus.Fatalf("Failed to start due to configuration errors")
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
internal.SetupStdLogging()
|
2023-02-14 11:47:47 +00:00
|
|
|
internal.SetupHookLogging(cfg.Logging)
|
2020-05-21 13:40:13 +00:00
|
|
|
internal.SetupPprof()
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2020-09-02 15:18:08 +00:00
|
|
|
logrus.Infof("Dendrite version %s", internal.VersionString())
|
|
|
|
|
2022-04-29 07:31:11 +00:00
|
|
|
if !cfg.ClientAPI.RegistrationDisabled && cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled {
|
|
|
|
logrus.Warn("Open registration is enabled")
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
closer, err := cfg.SetupTracing()
|
2018-01-02 10:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to start opentracing")
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
if cfg.Global.Sentry.Enabled {
|
|
|
|
logrus.Info("Setting up Sentry for debugging...")
|
|
|
|
err = sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: cfg.Global.Sentry.DSN,
|
|
|
|
Environment: cfg.Global.Sentry.Environment,
|
|
|
|
Debug: true,
|
|
|
|
ServerName: string(cfg.Global.ServerName),
|
|
|
|
Release: "dendrite@" + internal.VersionString(),
|
|
|
|
AttachStacktrace: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to start Sentry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:16:59 +00:00
|
|
|
var dnsCache *gomatrixserverlib.DNSCache
|
|
|
|
if cfg.Global.DNSCache.Enabled {
|
|
|
|
dnsCache = gomatrixserverlib.NewDNSCache(
|
|
|
|
cfg.Global.DNSCache.CacheSize,
|
2021-07-16 18:42:53 +00:00
|
|
|
cfg.Global.DNSCache.CacheLifetime,
|
2021-01-22 14:16:59 +00:00
|
|
|
)
|
|
|
|
logrus.Infof(
|
|
|
|
"DNS cache enabled (size %d, lifetime %s)",
|
|
|
|
cfg.Global.DNSCache.CacheSize,
|
2021-07-16 18:42:53 +00:00
|
|
|
cfg.Global.DNSCache.CacheLifetime,
|
2021-01-22 14:16:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:35:06 +00:00
|
|
|
// If we're in monolith mode, we'll set up a global pool of database
|
|
|
|
// connections. A component is welcome to use this pool if they don't
|
|
|
|
// have a separate database config of their own.
|
2023-03-17 11:09:45 +00:00
|
|
|
cm := sqlutil.NewConnectionManager()
|
2022-05-03 15:35:06 +00:00
|
|
|
if cfg.Global.DatabaseOptions.ConnectionString != "" {
|
|
|
|
if cfg.Global.DatabaseOptions.ConnectionString.IsSQLite() {
|
|
|
|
logrus.Panic("Using a global database connection pool is not supported with SQLite databases")
|
|
|
|
}
|
2023-03-17 11:09:45 +00:00
|
|
|
_, _, err := cm.Connection(&cfg.Global.DatabaseOptions)
|
|
|
|
if err != nil {
|
2022-05-03 15:35:06 +00:00
|
|
|
logrus.WithError(err).Panic("Failed to set up global database connections")
|
|
|
|
}
|
|
|
|
logrus.Debug("Using global database connection pool")
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:14:08 +00:00
|
|
|
// Ideally we would only use SkipClean on routes which we know can allow '/' but due to
|
|
|
|
// https://github.com/gorilla/mux/issues/460 we have to attach this at the top router.
|
|
|
|
// When used in conjunction with UseEncodedPath() we get the behaviour we want when parsing
|
|
|
|
// path parameters:
|
|
|
|
// /foo/bar%2Fbaz == [foo, bar%2Fbaz] (from UseEncodedPath)
|
|
|
|
// /foo/bar%2F%2Fbaz == [foo, bar%2F%2Fbaz] (from SkipClean)
|
|
|
|
// In particular, rooms v3 event IDs are not urlsafe and can include '/' and because they
|
|
|
|
// are randomly generated it results in flakey tests.
|
|
|
|
// We need to be careful with media APIs if they read from a filesystem to make sure they
|
|
|
|
// are not inadvertently reading paths without cleaning, else this could introduce a
|
|
|
|
// directory traversal attack e.g /../../../etc/passwd
|
2021-01-26 12:56:20 +00:00
|
|
|
|
2018-01-02 10:26:56 +00:00
|
|
|
return &BaseDendrite{
|
2023-03-17 11:09:45 +00:00
|
|
|
ProcessContext: process.NewProcessContext(),
|
|
|
|
tracerCloser: closer,
|
|
|
|
Cfg: cfg,
|
|
|
|
DNSCache: dnsCache,
|
|
|
|
Routers: httputil.NewRouters(),
|
|
|
|
NATS: &jetstream.NATSInstance{},
|
|
|
|
ConnectionManager: cm,
|
|
|
|
EnableMetrics: enableMetrics,
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements io.Closer
|
|
|
|
func (b *BaseDendrite) Close() error {
|
2023-01-20 11:45:56 +00:00
|
|
|
b.ProcessContext.ShutdownDendrite()
|
|
|
|
b.ProcessContext.WaitForShutdown()
|
2018-01-02 10:26:56 +00:00
|
|
|
return b.tracerCloser.Close()
|
|
|
|
}
|
|
|
|
|
2020-10-01 10:55:17 +00:00
|
|
|
// CreateClient creates a new client (normally used for media fetch requests).
|
|
|
|
// Should only be called once per component.
|
|
|
|
func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
|
2020-12-02 15:10:03 +00:00
|
|
|
if b.Cfg.Global.DisableFederation {
|
2021-01-22 16:09:05 +00:00
|
|
|
return gomatrixserverlib.NewClient(
|
|
|
|
gomatrixserverlib.WithTransport(noOpHTTPTransport),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
opts := []gomatrixserverlib.ClientOption{
|
2021-11-24 10:45:23 +00:00
|
|
|
gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
|
2022-05-05 13:06:05 +00:00
|
|
|
gomatrixserverlib.WithWellKnownSRVLookups(true),
|
2020-12-02 15:10:03 +00:00
|
|
|
}
|
2021-01-22 14:16:59 +00:00
|
|
|
if b.Cfg.Global.DNSCache.Enabled {
|
2021-01-22 16:09:05 +00:00
|
|
|
opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
|
2021-01-22 14:16:59 +00:00
|
|
|
}
|
2021-01-22 16:09:05 +00:00
|
|
|
client := gomatrixserverlib.NewClient(opts...)
|
2020-10-01 10:55:17 +00: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.
|
|
|
|
func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
|
2022-11-15 15:05:23 +00:00
|
|
|
identities := b.Cfg.Global.SigningIdentities()
|
2020-12-02 15:10:03 +00:00
|
|
|
if b.Cfg.Global.DisableFederation {
|
2021-01-22 16:09:05 +00:00
|
|
|
return gomatrixserverlib.NewFederationClient(
|
2022-11-15 15:05:23 +00:00
|
|
|
identities, gomatrixserverlib.WithTransport(noOpHTTPTransport),
|
2020-12-02 15:10:03 +00:00
|
|
|
)
|
|
|
|
}
|
2021-01-22 16:09:05 +00:00
|
|
|
opts := []gomatrixserverlib.ClientOption{
|
|
|
|
gomatrixserverlib.WithTimeout(time.Minute * 5),
|
2021-11-24 10:45:23 +00:00
|
|
|
gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
|
2022-09-20 16:17:44 +00:00
|
|
|
gomatrixserverlib.WithKeepAlives(!b.Cfg.FederationAPI.DisableHTTPKeepalives),
|
2021-01-22 16:09:05 +00:00
|
|
|
}
|
2021-01-22 14:16:59 +00:00
|
|
|
if b.Cfg.Global.DNSCache.Enabled {
|
2021-01-22 16:09:05 +00:00
|
|
|
opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
|
2021-01-22 14:16:59 +00:00
|
|
|
}
|
2021-01-22 16:09:05 +00:00
|
|
|
client := gomatrixserverlib.NewFederationClient(
|
2022-11-15 15:05:23 +00:00
|
|
|
identities, opts...,
|
2018-01-02 10:26:56 +00:00
|
|
|
)
|
2020-10-01 10:55:17 +00:00
|
|
|
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
|
|
|
return client
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:45:15 +00:00
|
|
|
func (b *BaseDendrite) ConfigureAdminEndpoints() {
|
2023-03-17 11:09:45 +00:00
|
|
|
b.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-17 11:09:45 +00:00
|
|
|
b.Routers.DendriteAdmin.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
|
2022-12-01 10:45:15 +00:00
|
|
|
if isDegraded, reasons := b.ProcessContext.IsDegraded(); isDegraded {
|
|
|
|
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.
|
2020-08-13 11:16:37 +00:00
|
|
|
func (b *BaseDendrite) SetupAndServeHTTP(
|
2023-03-01 21:57:30 +00:00
|
|
|
externalHTTPAddr config.ServerAddress,
|
2020-08-13 11:16:37 +00:00
|
|
|
certFile, keyFile *string,
|
|
|
|
) {
|
2022-08-30 11:59:13 +00:00
|
|
|
// Manually unlocked right before actually serving requests,
|
|
|
|
// as we don't return from this method (defer doesn't work).
|
|
|
|
b.startupLock.Lock()
|
2020-08-13 11:16:37 +00:00
|
|
|
|
2020-10-07 15:59:22 +00:00
|
|
|
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2020-08-13 11:16:37 +00:00
|
|
|
|
2020-10-07 15:59:22 +00:00
|
|
|
externalServ := &http.Server{
|
2023-03-01 21:57:30 +00:00
|
|
|
Addr: externalHTTPAddr.Address,
|
2020-04-20 16:42:34 +00:00
|
|
|
WriteTimeout: HTTPServerTimeout,
|
2020-10-07 15:59:22 +00:00
|
|
|
Handler: externalRouter,
|
2022-04-27 14:01:57 +00:00
|
|
|
BaseContext: func(_ net.Listener) context.Context {
|
|
|
|
return b.ProcessContext.Context()
|
|
|
|
},
|
2020-08-13 11:16:37 +00: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)
|
|
|
|
})
|
|
|
|
|
2020-08-13 11:16:37 +00:00
|
|
|
if b.Cfg.Global.Metrics.Enabled {
|
2023-02-14 11:47:47 +00:00
|
|
|
externalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), b.Cfg.Global.Metrics.BasicAuth))
|
2020-08-13 11:16:37 +00:00
|
|
|
}
|
2018-01-02 10:26:56 +00:00
|
|
|
|
2022-12-01 10:45:15 +00:00
|
|
|
b.ConfigureAdminEndpoints()
|
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-17 11:09:45 +00:00
|
|
|
b.Routers.Static.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-01-26 15:25:17 +00:00
|
|
|
_, _ = w.Write(landingPage.Bytes())
|
|
|
|
})
|
|
|
|
|
2021-03-24 10:25:24 +00:00
|
|
|
var clientHandler http.Handler
|
2023-03-17 11:09:45 +00:00
|
|
|
clientHandler = b.Routers.Client
|
2021-03-24 10:25:24 +00:00
|
|
|
if b.Cfg.Global.Sentry.Enabled {
|
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-17 11:09:45 +00:00
|
|
|
clientHandler = sentryHandler.Handle(b.Routers.Client)
|
2021-03-24 10:25:24 +00:00
|
|
|
}
|
|
|
|
var federationHandler http.Handler
|
2023-03-17 11:09:45 +00:00
|
|
|
federationHandler = b.Routers.Federation
|
2021-03-24 10:25:24 +00:00
|
|
|
if b.Cfg.Global.Sentry.Enabled {
|
|
|
|
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
2023-03-17 11:09:45 +00:00
|
|
|
federationHandler = sentryHandler.Handle(b.Routers.Federation)
|
2021-03-24 10:25:24 +00:00
|
|
|
}
|
2023-03-17 11:09:45 +00:00
|
|
|
externalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(b.Routers.DendriteAdmin)
|
2021-03-24 10:25:24 +00:00
|
|
|
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(clientHandler)
|
2020-12-02 15:10:03 +00:00
|
|
|
if !b.Cfg.Global.DisableFederation {
|
2023-03-17 11:09:45 +00:00
|
|
|
externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(b.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-17 11:09:45 +00:00
|
|
|
externalRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(b.Routers.SynapseAdmin)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.Routers.Media)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.Routers.WellKnown)
|
|
|
|
externalRouter.PathPrefix(httputil.PublicStaticPath).Handler(b.Routers.Static)
|
2020-08-13 11:16:37 +00:00
|
|
|
|
2022-08-30 11:59:13 +00:00
|
|
|
b.startupLock.Unlock()
|
2020-08-13 11:16:37 +00: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 11:16:37 +00: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)
|
2021-01-26 12:56:20 +00:00
|
|
|
b.ProcessContext.ComponentStarted()
|
|
|
|
externalServ.RegisterOnShutdown(func() {
|
2022-09-20 14:01:19 +00:00
|
|
|
if externalShutdown.CompareAndSwap(false, true) {
|
2021-01-26 12:56:20 +00:00
|
|
|
b.ProcessContext.ComponentFinished()
|
|
|
|
logrus.Infof("Stopped external HTTP listener")
|
|
|
|
}
|
|
|
|
})
|
2020-08-13 11:16:37 +00:00
|
|
|
if certFile != nil && keyFile != nil {
|
2020-08-13 17:27:19 +00: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 11:16:37 +00: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 11:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 11:47:47 +00:00
|
|
|
logrus.Infof("Stopped external listener on %s", externalServ.Addr)
|
2020-08-13 11:16:37 +00:00
|
|
|
}()
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:36:40 +00:00
|
|
|
minwinsvc.SetOnExit(b.ProcessContext.ShutdownDendrite)
|
2022-04-27 14:29:49 +00:00
|
|
|
<-b.ProcessContext.WaitForShutdown()
|
2022-04-27 15:04:11 +00:00
|
|
|
|
2022-04-27 14:29:49 +00:00
|
|
|
logrus.Infof("Stopping HTTP listeners")
|
|
|
|
_ = externalServ.Shutdown(context.Background())
|
2021-01-26 12:56:20 +00:00
|
|
|
logrus.Infof("Stopped HTTP listeners")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseDendrite) WaitForShutdown() {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2022-04-27 12:36:40 +00:00
|
|
|
select {
|
|
|
|
case <-sigs:
|
|
|
|
case <-b.ProcessContext.WaitForShutdown():
|
|
|
|
}
|
2021-01-26 12:56:20 +00:00
|
|
|
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
logrus.Warnf("Shutdown signal received")
|
|
|
|
|
|
|
|
b.ProcessContext.ShutdownDendrite()
|
|
|
|
b.ProcessContext.WaitForComponentsToFinish()
|
2021-03-24 10:25:24 +00:00
|
|
|
if b.Cfg.Global.Sentry.Enabled {
|
|
|
|
if !sentry.Flush(time.Second * 5) {
|
|
|
|
logrus.Warnf("failed to flush all Sentry events!")
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 12:56:20 +00:00
|
|
|
|
|
|
|
logrus.Warnf("Dendrite is exiting now")
|
2018-01-02 10:26:56 +00:00
|
|
|
}
|