2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-12-07 13:31:39 +01:00
|
|
|
|
2021-05-30 13:12:00 +02:00
|
|
|
package server
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-02 13:10:50 +01:00
|
|
|
"errors"
|
2021-03-04 14:38:18 +01:00
|
|
|
"fmt"
|
2023-01-02 13:10:50 +01:00
|
|
|
"net/http"
|
2021-03-04 14:38:18 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api"
|
2023-01-02 13:10:50 +01:00
|
|
|
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/middleware"
|
2023-05-25 10:37:38 +02:00
|
|
|
tlprocessor "github.com/superseriousbusiness/gotosocial/internal/processing/timeline"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/timeline"
|
2023-05-09 19:19:48 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/tracing"
|
2023-05-25 10:37:38 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/visibility"
|
2023-01-17 21:59:04 +01:00
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2023-01-02 13:10:50 +01:00
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-10-31 15:46:23 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/email"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
2021-05-27 16:06:24 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
|
2021-05-30 13:12:00 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2022-05-15 11:16:43 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-07-23 10:36:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2021-05-30 13:12:00 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/router"
|
2022-12-08 18:35:14 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2022-07-03 12:08:30 +02:00
|
|
|
gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-06-21 19:46:10 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/web"
|
2023-01-17 21:59:04 +01:00
|
|
|
|
|
|
|
// Inherit memory limit if set from cgroup
|
|
|
|
_ "github.com/KimMachineGun/automemlimit"
|
2021-03-04 14:38:18 +01:00
|
|
|
)
|
|
|
|
|
2021-05-30 13:12:00 +02:00
|
|
|
// Start creates and starts a gotosocial server
|
2021-12-07 13:31:39 +01:00
|
|
|
var Start action.GTSAction = func(ctx context.Context) error {
|
2023-04-08 11:51:24 +02:00
|
|
|
if _, err := maxprocs.Set(maxprocs.Logger(nil)); err != nil {
|
|
|
|
log.Infof(ctx, "could not set CPU limits from cgroup: %s", err)
|
2023-01-17 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
var state state.State
|
|
|
|
|
|
|
|
// Initialize caches
|
|
|
|
state.Caches.Init()
|
2023-02-03 21:03:05 +01:00
|
|
|
state.Caches.Start()
|
|
|
|
defer state.Caches.Stop()
|
2022-12-08 18:35:14 +01:00
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
// Initialize Tracing
|
|
|
|
if err := tracing.Initialize(); err != nil {
|
|
|
|
return fmt.Errorf("error initializing tracing: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
// Open connection to the database
|
|
|
|
dbService, err := bundb.NewBunDBService(ctx, &state)
|
2021-03-04 14:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
// Set the state DB connection
|
|
|
|
state.DB = dbService
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbService.CreateInstanceAccount(ctx); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error creating instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbService.CreateInstanceInstance(ctx); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error creating instance instance: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-11 21:18:06 +02:00
|
|
|
// Open the storage backend
|
2022-07-03 12:08:30 +02:00
|
|
|
storage, err := gtsstorage.AutoConfig()
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
2022-07-03 12:08:30 +02:00
|
|
|
return fmt.Errorf("error creating storage backend: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:40:48 +01:00
|
|
|
// Set the state storage driver
|
|
|
|
state.Storage = storage
|
|
|
|
|
2023-07-07 16:17:39 +02:00
|
|
|
// Build HTTP client
|
|
|
|
client := httpclient.New(httpclient.Config{
|
|
|
|
AllowRanges: config.MustParseIPPrefixes(config.GetHTTPClientAllowIPs()),
|
|
|
|
BlockRanges: config.MustParseIPPrefixes(config.GetHTTPClientBlockIPs()),
|
|
|
|
Timeout: config.GetHTTPClientTimeout(),
|
|
|
|
})
|
2022-05-15 11:16:43 +02:00
|
|
|
|
2023-02-13 19:40:48 +01:00
|
|
|
// Initialize workers.
|
|
|
|
state.Workers.Start()
|
|
|
|
defer state.Workers.Stop()
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Build handlers used in later initializations.
|
2023-02-13 19:40:48 +01:00
|
|
|
mediaManager := media.NewManager(&state)
|
2021-10-11 14:37:33 +02:00
|
|
|
oauthServer := oauth.New(ctx, dbService)
|
2023-02-13 19:40:48 +01:00
|
|
|
typeConverter := typeutils.NewConverter(dbService)
|
2023-05-25 10:37:38 +02:00
|
|
|
filter := visibility.NewFilter(&state)
|
2023-03-01 19:26:53 +01:00
|
|
|
federatingDB := federatingdb.New(&state, typeConverter)
|
2023-03-08 13:57:41 +01:00
|
|
|
transportController := transport.NewController(&state, federatingDB, &federation.Clock{}, client)
|
2023-05-12 11:15:54 +02:00
|
|
|
federator := federation.NewFederator(&state, federatingDB, transportController, typeConverter, mediaManager)
|
2021-10-31 15:46:23 +01:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Decide whether to create a noop email
|
|
|
|
// sender (won't send emails) or a real one.
|
2021-10-31 15:46:23 +01:00
|
|
|
var emailSender email.Sender
|
2022-05-30 14:41:24 +02:00
|
|
|
if smtpHost := config.GetSMTPHost(); smtpHost != "" {
|
2023-05-25 10:37:38 +02:00
|
|
|
// Host is defined; create a proper sender.
|
2021-12-07 13:31:39 +01:00
|
|
|
emailSender, err = email.NewSender()
|
2021-10-31 15:46:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating email sender: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-25 10:37:38 +02:00
|
|
|
// No host is defined; create a noop sender.
|
2021-12-07 13:31:39 +01:00
|
|
|
emailSender, err = email.NewNoopSender(nil)
|
2021-10-31 15:46:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating noop email sender: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Initialize timelines.
|
|
|
|
state.Timelines.Home = timeline.NewManager(
|
|
|
|
tlprocessor.HomeTimelineGrab(&state),
|
|
|
|
tlprocessor.HomeTimelineFilter(&state, filter),
|
|
|
|
tlprocessor.HomeTimelineStatusPrepare(&state, typeConverter),
|
|
|
|
tlprocessor.SkipInsert(),
|
|
|
|
)
|
|
|
|
if err := state.Timelines.Home.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting home timeline: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Timelines.List = timeline.NewManager(
|
|
|
|
tlprocessor.ListTimelineGrab(&state),
|
|
|
|
tlprocessor.ListTimelineFilter(&state, filter),
|
|
|
|
tlprocessor.ListTimelineStatusPrepare(&state, typeConverter),
|
|
|
|
tlprocessor.SkipInsert(),
|
|
|
|
)
|
|
|
|
if err := state.Timelines.List.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting list timeline: %s", err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Create the processor using all the other services we've created so far.
|
|
|
|
processor := processing.NewProcessor(typeConverter, federator, oauthServer, mediaManager, &state, emailSender)
|
|
|
|
|
2023-03-01 19:26:53 +01:00
|
|
|
// Set state client / federator worker enqueue functions
|
|
|
|
state.Workers.EnqueueClientAPI = processor.EnqueueClientAPI
|
|
|
|
state.Workers.EnqueueFederator = processor.EnqueueFederator
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
/*
|
|
|
|
HTTP router initialization
|
|
|
|
*/
|
|
|
|
|
|
|
|
router, err := router.New(ctx)
|
2021-07-23 10:36:28 +02:00
|
|
|
if err != nil {
|
2023-01-02 13:10:50 +01:00
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
2021-07-23 10:36:28 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
middlewares := []gin.HandlerFunc{
|
|
|
|
middleware.AddRequestID(config.GetRequestIDHeader()), // requestID middleware must run before tracing
|
|
|
|
}
|
|
|
|
if config.GetTracingEnabled() {
|
|
|
|
middlewares = append(middlewares, tracing.InstrumentGin())
|
|
|
|
}
|
|
|
|
middlewares = append(middlewares, []gin.HandlerFunc{
|
2023-02-17 12:02:29 +01:00
|
|
|
// note: hooks adding ctx fields must be ABOVE
|
|
|
|
// the logger, otherwise won't be accessible.
|
2023-05-21 17:12:47 +02:00
|
|
|
middleware.Logger(config.GetLogClientIP()),
|
2023-01-02 13:10:50 +01:00
|
|
|
middleware.UserAgent(),
|
|
|
|
middleware.CORS(),
|
|
|
|
middleware.ExtraHeaders(),
|
2023-05-09 19:19:48 +02:00
|
|
|
}...)
|
|
|
|
|
|
|
|
// attach global middlewares which are used for every request
|
|
|
|
router.AttachGlobalMiddleware(middlewares...)
|
2023-01-02 13:10:50 +01:00
|
|
|
|
|
|
|
// attach global no route / 404 handler to the router
|
|
|
|
router.AttachNoRouteHandler(func(c *gin.Context) {
|
2023-02-02 14:08:13 +01:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorNotFound(errors.New(http.StatusText(http.StatusNotFound))), processor.InstanceGetV1)
|
2023-01-02 13:10:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// build router modules
|
|
|
|
var idp oidc.IDP
|
|
|
|
if config.GetOIDCEnabled() {
|
|
|
|
idp, err = oidc.NewIDP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating oidc idp: %w", err)
|
|
|
|
}
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
routerSession, err := dbService.GetSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error retrieving router session for session middleware: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
sessionName, err := middleware.SessionName()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating session name for session middleware: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
authModule = api.NewAuth(dbService, processor, idp, routerSession, sessionName) // auth/oauth paths
|
|
|
|
clientModule = api.NewClient(dbService, processor) // api client endpoints
|
|
|
|
fileserverModule = api.NewFileserver(processor) // fileserver endpoints
|
|
|
|
wellKnownModule = api.NewWellKnown(processor) // .well-known endpoints
|
|
|
|
nodeInfoModule = api.NewNodeInfo(processor) // nodeinfo endpoint
|
|
|
|
activityPubModule = api.NewActivityPub(dbService, processor) // ActivityPub endpoints
|
2023-02-07 14:57:09 +01:00
|
|
|
webModule = web.New(dbService, processor) // web pages + user profiles + settings panels etc
|
2023-01-02 13:10:50 +01:00
|
|
|
)
|
|
|
|
|
2023-01-03 11:50:59 +01:00
|
|
|
// create required middleware
|
2023-01-04 11:57:59 +01:00
|
|
|
// rate limiting
|
2023-01-03 11:50:59 +01:00
|
|
|
limit := config.GetAdvancedRateLimitRequests()
|
|
|
|
clLimit := middleware.RateLimit(limit) // client api
|
|
|
|
s2sLimit := middleware.RateLimit(limit) // server-to-server (AP)
|
|
|
|
fsLimit := middleware.RateLimit(limit) // fileserver / web templates
|
|
|
|
|
2023-01-04 11:57:59 +01:00
|
|
|
// throttling
|
|
|
|
cpuMultiplier := config.GetAdvancedThrottlingMultiplier()
|
2023-02-10 21:16:01 +01:00
|
|
|
retryAfter := config.GetAdvancedThrottlingRetryAfter()
|
|
|
|
clThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // client api
|
|
|
|
s2sThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // server-to-server (AP)
|
|
|
|
fsThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // fileserver / web templates
|
|
|
|
pkThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // throttle public key endpoint separately
|
2023-01-04 11:57:59 +01:00
|
|
|
|
|
|
|
gzip := middleware.Gzip() // applied to all except fileserver
|
|
|
|
|
|
|
|
// these should be routed in order;
|
|
|
|
// apply throttling *after* rate limiting
|
|
|
|
authModule.Route(router, clLimit, clThrottle, gzip)
|
|
|
|
clientModule.Route(router, clLimit, clThrottle, gzip)
|
|
|
|
fileserverModule.Route(router, fsLimit, fsThrottle)
|
|
|
|
wellKnownModule.Route(router, gzip, s2sLimit, s2sThrottle)
|
|
|
|
nodeInfoModule.Route(router, s2sLimit, s2sThrottle, gzip)
|
|
|
|
activityPubModule.Route(router, s2sLimit, s2sThrottle, gzip)
|
2023-02-08 15:10:56 +01:00
|
|
|
activityPubModule.RoutePublicKey(router, s2sLimit, pkThrottle, gzip)
|
2023-01-04 11:57:59 +01:00
|
|
|
webModule.Route(router, fsLimit, fsThrottle, gzip)
|
2023-01-02 13:10:50 +01:00
|
|
|
|
2022-01-10 18:36:09 +01:00
|
|
|
gts, err := gotosocial.NewServer(dbService, router, federator, mediaManager)
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating gotosocial service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gts.Start(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error starting gotosocial service: %s", err)
|
|
|
|
}
|
2021-03-05 18:31:12 +01:00
|
|
|
|
2021-03-04 14:38:18 +01:00
|
|
|
// catch shutdown signals from the operating system
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2023-01-03 11:50:59 +01:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs // block until signal received
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Infof(ctx, "received signal %s, shutting down", sig)
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
// close down all running services in order
|
2021-04-01 20:46:45 +02:00
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 14:38:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Info(ctx, "done! exiting...")
|
2021-03-04 14:38:18 +01:00
|
|
|
return nil
|
|
|
|
}
|