mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
* Correctly redact events over federation (#2526) * Ensure we check powerlevel/origin before redacting an event * Add passing test * Use pl.UserLevel * Make check more readable, also check for the sender * Add new next steps page to the documentation * Highlighting in docs * Rename the page to "Optimise your installation" * Attempt to raise the file descriptor limit at startup (#2527) * Add `--difference` to `resolve-state` tool * Make the linter happy again * generic CaddyFile in front of Dendrite (monolith) (#2531) for Caddy 2.5.x Co-authored-by: emanuele.aliberti <emanuele.aliberti@mtka.eu> * Handle state before, send history visibility in output (#2532) * Check state before event * Tweaks * Refactor a bit, include in output events * Don't waste time if soft failed either * Tweak control flow, comments, use GMSL history visibility type * Fix rare panic when returning user devices over federation (#2534) * Add `InputDeviceListUpdate` to the keyserver, remove old input API (#2536) * Add `InputDeviceListUpdate` to the keyserver, remove old input API * Fix copyright * Log more information when a device list update fails * Fix nats.go commit (#2540) Signed-off-by: Jean Lucas <jean@4ray.co> * Don't return `end` if there are not more messages (#2542) * Be more spec compliant * Move lazyLoadMembers to own method * Return an error if trying to invite a malformed user ID (#2543) * Add `evacuateUser` endpoint, use it when deactivating accounts (#2545) * Add `evacuateUser` endpoint, use it when deactivating accounts * Populate the API * Clean up user devices when deactivating * Include invites, delete pushers * Silence presence logs (#2547) * Blacklist `Guest users can join guest_access rooms` test until it can be investigated * Disable WebAssembly builds for now * Try to fix backfilling (#2548) * Try to fix backfilling * Return start/end to not confuse clients * Update GMSL * Update GMSL * Roomserver producers package (#2546) * Give the roomserver a producers package * Change init point * Populate ACLs API * Fix build issues * `RoomEventProducer` naming * Version 0.8.9 (#2549) * Version 0.8.9 * Update changelog * feat+fix: Ignore unknown keys and verify required fields are present in appservice registration files (#2550) * fix: ignore unknown keys in appservice configs fixes matrix-org/dendrite#1567 * feat: verify required fields in appservice configs * Use new testrig for key changes tests (#2552) * Use new testrig for tests * Log the error message * Fix QuerySharedUsers for the SyncAPI keychange consumer (#2554) * Make more use of base.BaseDendrite * Fix QuerySharedUsers if no UserIDs are supplied * Return clearer error when no state NID exists for an event (#2555) * Wrap error from `SnapshotNIDFromEventID` * Hopefully fix read receipts timestamps (#2557) This should avoid coercions between signed and unsigned ints which might fix problems like `sql: converting argument $5 type: uint64 values with high bit set are not supported`. * Fix nil pointer access when redacting events (#2560) * Fix issue `uint64 values with high bit are not supported` in presence (#2562) * Fix issue #2528 * Use gomatrixserverlib.Timestamp * Use ParseUint instead of ParseInt * Update Pinecone to matrix-org/pinecone@1ce778f * Ristretto cache (#2563) * Try Ristretto cache * Tweak * It's beautiful * Update GMSL * More strict keyable interface * Fix that some more * Make less panicky * Don't enforce mutability checks for now * Determine mutability using deep equality * Tweaks * Namespace keys * Make federation caches mutable * Update cost estimation, add metric * Update GMSL * Estimate cost for metrics better * Reduce counters a bit * Try caching events * Some guards * Try again * Try this * Use separate caches for hopefully better hash distribution * Fix bug with admitting events into cache * Try to fix bugs * Check nil * Try that again * Preserve order jeezo this is messy * thanks VS Code for doing exactly the wrong thing * Try this again * Be more specific * aaaaargh * One more time * That might be better * Stronger sorting * Cache expiries, async publishing of EDUs * Put it back * Use a shared cache again * Cost estimation fixes * Update ristretto * Reduce counters a bit * Clean up a bit * Update GMSL * 1GB * Configurable cache sizees * Tweaks * Add `config.DataUnit` for specifying friendly cache sizes * Various tweaks * Update GMSL * Add back some lazy loading caching * Include key in cost * Include key in cost * Tweak max age handling, config key name * Only register prometheus metrics if requested * Review comments @S7evinK * Don't return errors when creating caches (it is better just to crash since otherwise we'll `nil`-pointer exception everywhere) * Review comments * Update sample configs * Update GHA Workflow * Update Complement images to Go 1.18 * Remove the cache test from the federation API as we no longer guarantee immediate cache admission * Don't check the caches in the renewal test * Possibly fix the upgrade tests * Update to matrix-org/gomatrixserverlib#322 * Update documentation to refer to Go 1.18 * Minor SendToDevice fix (#2565) * Avoid unnecessary marshalling if sending to the local server * Fix ordering of ToDevice messages * Revive SendToDevice test * Use `/v3` to request media from remote servers (update to matrix-org/gomatrixserverlib#324) * Pointerise `types.RoomInfo` in the cache so we can update it in-place in the latest events updater * Add a Troubleshooting page * Update `sytest-whitelist` * Use sync API database in `filterSharedUsers` (#2572) * Add function to the sync API storage package for filtering shared users * Use the database instead of asking the RS API * Fix unit tests * Fix map handling in `filterSharedUsers` * Update 1_createusers.md (#2571) * Update 1_createusers.md Added description on how to create user accounts when running in docker. * Update 1_createusers.md Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Fix connection_string format in dendrite-sample.polylith.yaml (#2574) * History visibility database changes (#2533) * Add new history_visibility column * Update SQL queries to include history_visibility * Store the history visibilty calculated by the roomserver * Update GMSL * Update migrations * Fix migration * Update GMSL * Fix `go.sum` * Update GMSL to use sql.Scanner & sql.Valuer * Re-order migration/table creation * Update gomatrixserverlib * Add history_visibility column to current_room_state * Fix migrations * Return error instead of Fatal log Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Tweak cache counters (#2575) * Tweak cache counters This makes the number of counters relative to the maximum cache size. Since the counters effectively manage the size of the bloom filter, larger caches need more counters and smaller caches need less. 10 counters per 1KB data means that the default cache size of 1GB should result in a bloom filter and TinyLRU admission set of about 16MB estimated. * Remove line left by accident * Set historyVisibility in rowsToStreamEvents * Update FAQ * Add event state key cache (#2576) * Explain how SRV works in Matrix and discourage using it (#2577) * Explain how SRV works in Matrix and discourage using it * Minor tweaks to formatting Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Fix issue with membership event_nid being 0 (#2580) * docs: Add build page; correct proxy info; fix Caddy example (#2579) * Add build page; correct proxy info; fix Caddy example * Improve Caddyfile example * Apply review comments; add polylith Caddyfile * Bump tzinfo from 1.2.9 to 1.2.10 in /docs (#2584) Bumps [tzinfo](https://github.com/tzinfo/tzinfo) from 1.2.9 to 1.2.10. - [Release notes](https://github.com/tzinfo/tzinfo/releases) - [Changelog](https://github.com/tzinfo/tzinfo/blob/master/CHANGES.md) - [Commits](https://github.com/tzinfo/tzinfo/compare/v1.2.9...v1.2.10) --- updated-dependencies: - dependency-name: tzinfo dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Membership updater refactoring (#2541) * Membership updater refactoring * Pass in membership state * Use membership check rather than referring to state directly * Delete irrelevant membership states * We don't need the leave event after all * Tweaks * Put a log entry in that I might stand a chance of finding * Be less panicky * Tweak invite handling * Don't freak if we can't find the event NID * Use event NID from `types.Event` * Clean up * Better invite handling * Placate the almighty linter * Blacklist a Sytest which is otherwise fine under Complement for reasons I don't understand * Fix the sytest after all (thanks @S7evinK for the spot) * Try to fix HTTP 500s on `/members` (#2581) * Update database migrations, remove goose (#2264) * Add new db migration * Update migrations Remove goose * Add possibility to test direct upgrades * Try to fix WASM test * Add checks for specific migrations * Remove AddMigration Use WithTransaction Add Dendrite version to table * Fix linter issues * Update tests * Update comments, outdent if * Namespace migrations * Add direct upgrade tests, skipping over one version * Split migrations * Update go version in CI * Fix copy&paste mistake * Use contexts in migrations Co-authored-by: kegsay <kegan@matrix.org> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Add .well-known/matrix/client to clientapi (#2551) Signed-off-by: Jonathan Bartlett <jonathan@jonnobrow.co.uk> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Remove `room_id` field from MSC2946 stripped events (closes #2588) * Remove `goose` from Dockerfiles * Make the User API responsible for sending account data output events (#2592) * Make the User API responsible for sending account data output events * Clean up producer * Review comments * Update NATS Server and nats.go to use upstream * Set CORS headers for HTTP 404 and 405 errors (#2599) * Set CORS headers for the 404s * Use custom handlers, plus one for HTTP 405 too * Tweak setup * Add to muxes too * Tidy up some more * Use built-in HTTP 404 handler * Don't bother setting it for federation-facing * Optimise checking other servers allowed to see events (#2596) * Try optimising checking if server is allowed to see event * Fix error * Handle case where snapshot NID is 0 * Fix query * Update SQL * Clean up `CheckServerAllowedToSeeEvent` * Not supported on SQLite * Maybe placate the unit tests * Review comments * De-race `types.RoomInfo` (#2600) * De-race `CompleteSync` (#2601) The `err` was coming from outside of the goroutine and being written to by concurrent goroutines. * Version 0.9.0 (#2602) Co-authored-by: Till <2353100+S7evinK@users.noreply.github.com> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> Co-authored-by: Till Faelligen <davidf@element.io> Co-authored-by: Emanuele Aliberti <dev@mtka.eu> Co-authored-by: emanuele.aliberti <emanuele.aliberti@mtka.eu> Co-authored-by: Jean Lucas <jean@4ray.co> Co-authored-by: Kabir Kwatra <kabir@kwatra.me> Co-authored-by: andreever <52261463+andreever@users.noreply.github.com> Co-authored-by: Maximilian Gaedig <38767445+MaximilianGaedig@users.noreply.github.com> Co-authored-by: Tulir Asokan <tulir@maunium.net> Co-authored-by: Matt Holt <mholt@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: kegsay <kegan@matrix.org> Co-authored-by: Jonathan Bartlett <34320158+Jonnobrow@users.noreply.github.com>
560 lines
21 KiB
Go
560 lines
21 KiB
Go
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
||
//
|
||
// 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.
|
||
|
||
package base
|
||
|
||
import (
|
||
"context"
|
||
"crypto/tls"
|
||
"database/sql"
|
||
"fmt"
|
||
"io"
|
||
"net"
|
||
"net/http"
|
||
_ "net/http/pprof"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
"time"
|
||
|
||
"github.com/getsentry/sentry-go"
|
||
sentryhttp "github.com/getsentry/sentry-go/http"
|
||
"github.com/matrix-org/dendrite/internal/caching"
|
||
"github.com/matrix-org/dendrite/internal/httputil"
|
||
"github.com/matrix-org/dendrite/internal/pushgateway"
|
||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||
"github.com/matrix-org/gomatrixserverlib"
|
||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||
"go.uber.org/atomic"
|
||
"golang.org/x/net/http2"
|
||
"golang.org/x/net/http2/h2c"
|
||
|
||
"github.com/matrix-org/dendrite/internal"
|
||
"github.com/matrix-org/dendrite/setup/jetstream"
|
||
"github.com/matrix-org/dendrite/setup/process"
|
||
|
||
"github.com/gorilla/mux"
|
||
"github.com/kardianos/minwinsvc"
|
||
|
||
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
||
asinthttp "github.com/matrix-org/dendrite/appservice/inthttp"
|
||
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
||
federationIntHTTP "github.com/matrix-org/dendrite/federationapi/inthttp"
|
||
keyserverAPI "github.com/matrix-org/dendrite/keyserver/api"
|
||
keyinthttp "github.com/matrix-org/dendrite/keyserver/inthttp"
|
||
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||
rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp"
|
||
"github.com/matrix-org/dendrite/setup/config"
|
||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||
userapiinthttp "github.com/matrix-org/dendrite/userapi/inthttp"
|
||
"github.com/sirupsen/logrus"
|
||
)
|
||
|
||
// 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 {
|
||
*process.ProcessContext
|
||
componentName string
|
||
tracerCloser io.Closer
|
||
PublicClientAPIMux *mux.Router
|
||
PublicFederationAPIMux *mux.Router
|
||
PublicKeyAPIMux *mux.Router
|
||
PublicMediaAPIMux *mux.Router
|
||
PublicWellKnownAPIMux *mux.Router
|
||
InternalAPIMux *mux.Router
|
||
DendriteAdminMux *mux.Router
|
||
SynapseAdminMux *mux.Router
|
||
NATS *jetstream.NATSInstance
|
||
UseHTTPAPIs bool
|
||
apiHttpClient *http.Client
|
||
Cfg *config.Dendrite
|
||
Caches *caching.Caches
|
||
DNSCache *gomatrixserverlib.DNSCache
|
||
Database *sql.DB
|
||
DatabaseWriter sqlutil.Writer
|
||
EnableMetrics bool
|
||
}
|
||
|
||
const NoListener = ""
|
||
|
||
const HTTPServerTimeout = time.Minute * 5
|
||
const HTTPClientTimeout = time.Second * 30
|
||
|
||
type BaseDendriteOptions int
|
||
|
||
const (
|
||
DisableMetrics BaseDendriteOptions = iota
|
||
UseHTTPAPIs
|
||
PolylithMode
|
||
)
|
||
|
||
// NewBaseDendrite creates a new instance to be used by a component.
|
||
// The componentName is used for logging purposes, and should be a friendly name
|
||
// of the compontent running, e.g. "SyncAPI"
|
||
func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...BaseDendriteOptions) *BaseDendrite {
|
||
platformSanityChecks()
|
||
useHTTPAPIs := false
|
||
enableMetrics := true
|
||
isMonolith := true
|
||
for _, opt := range options {
|
||
switch opt {
|
||
case DisableMetrics:
|
||
enableMetrics = false
|
||
case UseHTTPAPIs:
|
||
useHTTPAPIs = true
|
||
case PolylithMode:
|
||
isMonolith = false
|
||
useHTTPAPIs = true
|
||
}
|
||
}
|
||
|
||
configErrors := &config.ConfigErrors{}
|
||
cfg.Verify(configErrors, isMonolith)
|
||
if len(*configErrors) > 0 {
|
||
for _, err := range *configErrors {
|
||
logrus.Errorf("Configuration error: %s", err)
|
||
}
|
||
logrus.Fatalf("Failed to start due to configuration errors")
|
||
}
|
||
|
||
internal.SetupHookLogging(cfg.Logging, componentName)
|
||
internal.SetupPprof()
|
||
|
||
logrus.Infof("Dendrite version %s", internal.VersionString())
|
||
|
||
if !cfg.ClientAPI.RegistrationDisabled && cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled {
|
||
logrus.Warn("Open registration is enabled")
|
||
}
|
||
|
||
closer, err := cfg.SetupTracing("Dendrite" + componentName)
|
||
if err != nil {
|
||
logrus.WithError(err).Panicf("failed to start opentracing")
|
||
}
|
||
|
||
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")
|
||
}
|
||
}
|
||
|
||
var dnsCache *gomatrixserverlib.DNSCache
|
||
if cfg.Global.DNSCache.Enabled {
|
||
dnsCache = gomatrixserverlib.NewDNSCache(
|
||
cfg.Global.DNSCache.CacheSize,
|
||
cfg.Global.DNSCache.CacheLifetime,
|
||
)
|
||
logrus.Infof(
|
||
"DNS cache enabled (size %d, lifetime %s)",
|
||
cfg.Global.DNSCache.CacheSize,
|
||
cfg.Global.DNSCache.CacheLifetime,
|
||
)
|
||
}
|
||
|
||
apiClient := http.Client{
|
||
Timeout: time.Minute * 10,
|
||
Transport: &http2.Transport{
|
||
AllowHTTP: true,
|
||
DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
|
||
// Ordinarily HTTP/2 would expect TLS, but the remote listener is
|
||
// H2C-enabled (HTTP/2 without encryption). Overriding the DialTLS
|
||
// function with a plain Dial allows us to trick the HTTP client
|
||
// into establishing a HTTP/2 connection without TLS.
|
||
// TODO: Eventually we will want to look at authenticating and
|
||
// encrypting these internal HTTP APIs, at which point we will have
|
||
// to reconsider H2C and change all this anyway.
|
||
return net.Dial(network, addr)
|
||
},
|
||
},
|
||
}
|
||
|
||
// 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.
|
||
var db *sql.DB
|
||
var writer sqlutil.Writer
|
||
if cfg.Global.DatabaseOptions.ConnectionString != "" {
|
||
if !isMonolith {
|
||
logrus.Panic("Using a global database connection pool is not supported in polylith deployments")
|
||
}
|
||
if cfg.Global.DatabaseOptions.ConnectionString.IsSQLite() {
|
||
logrus.Panic("Using a global database connection pool is not supported with SQLite databases")
|
||
}
|
||
writer = sqlutil.NewDummyWriter()
|
||
if db, err = sqlutil.Open(&cfg.Global.DatabaseOptions, writer); err != nil {
|
||
logrus.WithError(err).Panic("Failed to set up global database connections")
|
||
}
|
||
logrus.Debug("Using global database connection pool")
|
||
}
|
||
|
||
// 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
|
||
|
||
return &BaseDendrite{
|
||
ProcessContext: process.NewProcessContext(),
|
||
componentName: componentName,
|
||
UseHTTPAPIs: useHTTPAPIs,
|
||
tracerCloser: closer,
|
||
Cfg: cfg,
|
||
Caches: caching.NewRistrettoCache(cfg.Global.Cache.EstimatedMaxSize, cfg.Global.Cache.MaxAge, enableMetrics),
|
||
DNSCache: dnsCache,
|
||
PublicClientAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicClientPathPrefix).Subrouter().UseEncodedPath(),
|
||
PublicFederationAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath(),
|
||
PublicKeyAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicKeyPathPrefix).Subrouter().UseEncodedPath(),
|
||
PublicMediaAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicMediaPathPrefix).Subrouter().UseEncodedPath(),
|
||
PublicWellKnownAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicWellKnownPrefix).Subrouter().UseEncodedPath(),
|
||
InternalAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.InternalPathPrefix).Subrouter().UseEncodedPath(),
|
||
DendriteAdminMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.DendriteAdminPathPrefix).Subrouter().UseEncodedPath(),
|
||
SynapseAdminMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.SynapseAdminPathPrefix).Subrouter().UseEncodedPath(),
|
||
NATS: &jetstream.NATSInstance{},
|
||
apiHttpClient: &apiClient,
|
||
Database: db, // set if monolith with global connection pool only
|
||
DatabaseWriter: writer, // set if monolith with global connection pool only
|
||
EnableMetrics: enableMetrics,
|
||
}
|
||
}
|
||
|
||
// Close implements io.Closer
|
||
func (b *BaseDendrite) Close() error {
|
||
return b.tracerCloser.Close()
|
||
}
|
||
|
||
// DatabaseConnection assists in setting up a database connection. It accepts
|
||
// the database properties and a new writer for the given component. If we're
|
||
// running in monolith mode with a global connection pool configured then we
|
||
// will return that connection, along with the global writer, effectively
|
||
// ignoring the options provided. Otherwise we'll open a new database connection
|
||
// using the supplied options and writer. Note that it's possible for the pointer
|
||
// receiver to be nil here – that's deliberate as some of the unit tests don't
|
||
// have a BaseDendrite and just want a connection with the supplied config
|
||
// without any pooling stuff.
|
||
func (b *BaseDendrite) DatabaseConnection(dbProperties *config.DatabaseOptions, writer sqlutil.Writer) (*sql.DB, sqlutil.Writer, error) {
|
||
if dbProperties.ConnectionString != "" || b == nil {
|
||
// Open a new database connection using the supplied config.
|
||
db, err := sqlutil.Open(dbProperties, writer)
|
||
return db, writer, err
|
||
}
|
||
if b.Database != nil && b.DatabaseWriter != nil {
|
||
// Ignore the supplied config and return the global pool and
|
||
// writer.
|
||
return b.Database, b.DatabaseWriter, nil
|
||
}
|
||
return nil, nil, fmt.Errorf("no database connections configured")
|
||
}
|
||
|
||
// AppserviceHTTPClient returns the AppServiceInternalAPI for hitting the appservice component over HTTP.
|
||
func (b *BaseDendrite) AppserviceHTTPClient() appserviceAPI.AppServiceInternalAPI {
|
||
a, err := asinthttp.NewAppserviceClient(b.Cfg.AppServiceURL(), b.apiHttpClient)
|
||
if err != nil {
|
||
logrus.WithError(err).Panic("CreateHTTPAppServiceAPIs failed")
|
||
}
|
||
return a
|
||
}
|
||
|
||
// RoomserverHTTPClient returns RoomserverInternalAPI for hitting the roomserver over HTTP.
|
||
func (b *BaseDendrite) RoomserverHTTPClient() roomserverAPI.RoomserverInternalAPI {
|
||
rsAPI, err := rsinthttp.NewRoomserverClient(b.Cfg.RoomServerURL(), b.apiHttpClient, b.Caches)
|
||
if err != nil {
|
||
logrus.WithError(err).Panic("RoomserverHTTPClient failed", b.apiHttpClient)
|
||
}
|
||
return rsAPI
|
||
}
|
||
|
||
// UserAPIClient returns UserInternalAPI for hitting the userapi over HTTP.
|
||
func (b *BaseDendrite) UserAPIClient() userapi.UserInternalAPI {
|
||
userAPI, err := userapiinthttp.NewUserAPIClient(b.Cfg.UserAPIURL(), b.apiHttpClient)
|
||
if err != nil {
|
||
logrus.WithError(err).Panic("UserAPIClient failed", b.apiHttpClient)
|
||
}
|
||
return userAPI
|
||
}
|
||
|
||
// FederationAPIHTTPClient returns FederationInternalAPI for hitting
|
||
// the federation API server over HTTP
|
||
func (b *BaseDendrite) FederationAPIHTTPClient() federationAPI.FederationInternalAPI {
|
||
f, err := federationIntHTTP.NewFederationAPIClient(b.Cfg.FederationAPIURL(), b.apiHttpClient, b.Caches)
|
||
if err != nil {
|
||
logrus.WithError(err).Panic("FederationAPIHTTPClient failed", b.apiHttpClient)
|
||
}
|
||
return f
|
||
}
|
||
|
||
// KeyServerHTTPClient returns KeyInternalAPI for hitting the key server over HTTP
|
||
func (b *BaseDendrite) KeyServerHTTPClient() keyserverAPI.KeyInternalAPI {
|
||
f, err := keyinthttp.NewKeyServerClient(b.Cfg.KeyServerURL(), b.apiHttpClient)
|
||
if err != nil {
|
||
logrus.WithError(err).Panic("KeyServerHTTPClient failed", b.apiHttpClient)
|
||
}
|
||
return f
|
||
}
|
||
|
||
// PushGatewayHTTPClient returns a new client for interacting with (external) Push Gateways.
|
||
func (b *BaseDendrite) PushGatewayHTTPClient() pushgateway.Client {
|
||
return pushgateway.NewHTTPClient(b.Cfg.UserAPI.PushGatewayDisableTLSValidation)
|
||
}
|
||
|
||
// CreateClient creates a new client (normally used for media fetch requests).
|
||
// Should only be called once per component.
|
||
func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
|
||
if b.Cfg.Global.DisableFederation {
|
||
return gomatrixserverlib.NewClient(
|
||
gomatrixserverlib.WithTransport(noOpHTTPTransport),
|
||
)
|
||
}
|
||
opts := []gomatrixserverlib.ClientOption{
|
||
gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
|
||
gomatrixserverlib.WithWellKnownSRVLookups(true),
|
||
}
|
||
if b.Cfg.Global.DNSCache.Enabled {
|
||
opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
|
||
}
|
||
client := gomatrixserverlib.NewClient(opts...)
|
||
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
||
return client
|
||
}
|
||
|
||
// CreateFederationClient creates a new federation client. Should only be called
|
||
// once per component.
|
||
func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
|
||
if b.Cfg.Global.DisableFederation {
|
||
return gomatrixserverlib.NewFederationClient(
|
||
b.Cfg.Global.ServerName, b.Cfg.Global.KeyID, b.Cfg.Global.PrivateKey,
|
||
gomatrixserverlib.WithTransport(noOpHTTPTransport),
|
||
)
|
||
}
|
||
opts := []gomatrixserverlib.ClientOption{
|
||
gomatrixserverlib.WithTimeout(time.Minute * 5),
|
||
gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
|
||
}
|
||
if b.Cfg.Global.DNSCache.Enabled {
|
||
opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
|
||
}
|
||
client := gomatrixserverlib.NewFederationClient(
|
||
b.Cfg.Global.ServerName, b.Cfg.Global.KeyID,
|
||
b.Cfg.Global.PrivateKey, opts...,
|
||
)
|
||
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
|
||
return client
|
||
}
|
||
|
||
func (b *BaseDendrite) configureHTTPErrors() {
|
||
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||
_, _ = w.Write([]byte(fmt.Sprintf("405 %s not allowed on this endpoint", r.Method)))
|
||
}
|
||
|
||
notFoundCORSHandler := httputil.WrapHandlerInCORS(http.NotFoundHandler())
|
||
notAllowedCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
|
||
|
||
for _, router := range []*mux.Router{
|
||
b.PublicClientAPIMux, b.PublicMediaAPIMux,
|
||
b.DendriteAdminMux, b.SynapseAdminMux,
|
||
b.PublicWellKnownAPIMux,
|
||
} {
|
||
router.NotFoundHandler = notFoundCORSHandler
|
||
router.MethodNotAllowedHandler = notAllowedCORSHandler
|
||
}
|
||
}
|
||
|
||
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on
|
||
// ApiMux under /api/ and adds a prometheus handler under /metrics.
|
||
func (b *BaseDendrite) SetupAndServeHTTP(
|
||
internalHTTPAddr, externalHTTPAddr config.HTTPAddress,
|
||
certFile, keyFile *string,
|
||
) {
|
||
internalAddr, _ := internalHTTPAddr.Address()
|
||
externalAddr, _ := externalHTTPAddr.Address()
|
||
|
||
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
||
internalRouter := externalRouter
|
||
|
||
externalServ := &http.Server{
|
||
Addr: string(externalAddr),
|
||
WriteTimeout: HTTPServerTimeout,
|
||
Handler: externalRouter,
|
||
BaseContext: func(_ net.Listener) context.Context {
|
||
return b.ProcessContext.Context()
|
||
},
|
||
}
|
||
internalServ := externalServ
|
||
|
||
if internalAddr != NoListener && externalAddr != internalAddr {
|
||
// H2C allows us to accept HTTP/2 connections without TLS
|
||
// encryption. Since we don't currently require any form of
|
||
// authentication or encryption on these internal HTTP APIs,
|
||
// H2C gives us all of the advantages of HTTP/2 (such as
|
||
// stream multiplexing and avoiding head-of-line blocking)
|
||
// without enabling TLS.
|
||
internalH2S := &http2.Server{}
|
||
internalRouter = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
||
internalServ = &http.Server{
|
||
Addr: string(internalAddr),
|
||
Handler: h2c.NewHandler(internalRouter, internalH2S),
|
||
BaseContext: func(_ net.Listener) context.Context {
|
||
return b.ProcessContext.Context()
|
||
},
|
||
}
|
||
}
|
||
|
||
b.configureHTTPErrors()
|
||
|
||
internalRouter.PathPrefix(httputil.InternalPathPrefix).Handler(b.InternalAPIMux)
|
||
if b.Cfg.Global.Metrics.Enabled {
|
||
internalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), b.Cfg.Global.Metrics.BasicAuth))
|
||
}
|
||
|
||
b.DendriteAdminMux.HandleFunc("/monitor/up", func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(200)
|
||
})
|
||
b.DendriteAdminMux.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
|
||
if b.ProcessContext.IsDegraded() {
|
||
w.WriteHeader(503)
|
||
return
|
||
}
|
||
w.WriteHeader(200)
|
||
})
|
||
|
||
var clientHandler http.Handler
|
||
clientHandler = b.PublicClientAPIMux
|
||
if b.Cfg.Global.Sentry.Enabled {
|
||
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
||
Repanic: true,
|
||
})
|
||
clientHandler = sentryHandler.Handle(b.PublicClientAPIMux)
|
||
}
|
||
var federationHandler http.Handler
|
||
federationHandler = b.PublicFederationAPIMux
|
||
if b.Cfg.Global.Sentry.Enabled {
|
||
sentryHandler := sentryhttp.New(sentryhttp.Options{
|
||
Repanic: true,
|
||
})
|
||
federationHandler = sentryHandler.Handle(b.PublicFederationAPIMux)
|
||
}
|
||
internalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(b.DendriteAdminMux)
|
||
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(clientHandler)
|
||
if !b.Cfg.Global.DisableFederation {
|
||
externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(b.PublicKeyAPIMux)
|
||
externalRouter.PathPrefix(httputil.PublicFederationPathPrefix).Handler(federationHandler)
|
||
}
|
||
externalRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(b.SynapseAdminMux)
|
||
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux)
|
||
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux)
|
||
|
||
if internalAddr != NoListener && internalAddr != externalAddr {
|
||
go func() {
|
||
var internalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
|
||
logrus.Infof("Starting internal %s listener on %s", b.componentName, internalServ.Addr)
|
||
b.ProcessContext.ComponentStarted()
|
||
internalServ.RegisterOnShutdown(func() {
|
||
if internalShutdown.CAS(false, true) {
|
||
b.ProcessContext.ComponentFinished()
|
||
logrus.Infof("Stopped internal HTTP listener")
|
||
}
|
||
})
|
||
if certFile != nil && keyFile != nil {
|
||
if err := internalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
||
if err != http.ErrServerClosed {
|
||
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
||
}
|
||
}
|
||
} else {
|
||
if err := internalServ.ListenAndServe(); err != nil {
|
||
if err != http.ErrServerClosed {
|
||
logrus.WithError(err).Fatal("failed to serve HTTP")
|
||
}
|
||
}
|
||
}
|
||
logrus.Infof("Stopped internal %s listener on %s", b.componentName, internalServ.Addr)
|
||
}()
|
||
}
|
||
|
||
if externalAddr != NoListener {
|
||
go func() {
|
||
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
|
||
logrus.Infof("Starting external %s listener on %s", b.componentName, externalServ.Addr)
|
||
b.ProcessContext.ComponentStarted()
|
||
externalServ.RegisterOnShutdown(func() {
|
||
if externalShutdown.CAS(false, true) {
|
||
b.ProcessContext.ComponentFinished()
|
||
logrus.Infof("Stopped external HTTP listener")
|
||
}
|
||
})
|
||
if certFile != nil && keyFile != nil {
|
||
if err := externalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
|
||
if err != http.ErrServerClosed {
|
||
logrus.WithError(err).Fatal("failed to serve HTTPS")
|
||
}
|
||
}
|
||
} else {
|
||
if err := externalServ.ListenAndServe(); err != nil {
|
||
if err != http.ErrServerClosed {
|
||
logrus.WithError(err).Fatal("failed to serve HTTP")
|
||
}
|
||
}
|
||
}
|
||
logrus.Infof("Stopped external %s listener on %s", b.componentName, externalServ.Addr)
|
||
}()
|
||
}
|
||
|
||
minwinsvc.SetOnExit(b.ProcessContext.ShutdownDendrite)
|
||
<-b.ProcessContext.WaitForShutdown()
|
||
|
||
logrus.Infof("Stopping HTTP listeners")
|
||
_ = internalServ.Shutdown(context.Background())
|
||
_ = externalServ.Shutdown(context.Background())
|
||
logrus.Infof("Stopped HTTP listeners")
|
||
}
|
||
|
||
func (b *BaseDendrite) WaitForShutdown() {
|
||
sigs := make(chan os.Signal, 1)
|
||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||
select {
|
||
case <-sigs:
|
||
case <-b.ProcessContext.WaitForShutdown():
|
||
}
|
||
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
|
||
|
||
logrus.Warnf("Shutdown signal received")
|
||
|
||
b.ProcessContext.ShutdownDendrite()
|
||
b.ProcessContext.WaitForComponentsToFinish()
|
||
if b.Cfg.Global.Sentry.Enabled {
|
||
if !sentry.Flush(time.Second * 5) {
|
||
logrus.Warnf("failed to flush all Sentry events!")
|
||
}
|
||
}
|
||
|
||
logrus.Warnf("Dendrite is exiting now")
|
||
}
|