2023-02-01 00:35:21 +00:00
|
|
|
// Copyright 2023 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 monolith
|
|
|
|
|
|
|
|
import (
|
2023-02-01 20:41:27 +00:00
|
|
|
"context"
|
2023-02-01 00:35:21 +00:00
|
|
|
"crypto/ed25519"
|
2023-02-01 20:41:27 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
2023-02-01 00:35:21 +00:00
|
|
|
"fmt"
|
2023-02-01 20:41:27 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-02-01 00:35:21 +00:00
|
|
|
"path/filepath"
|
2023-03-22 08:21:32 +00:00
|
|
|
"sync"
|
2023-02-01 20:41:27 +00:00
|
|
|
"time"
|
2023-02-01 00:35:21 +00:00
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/matrix-org/dendrite/appservice"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conn"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/embed"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/relay"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/rooms"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/users"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi"
|
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/matrix-org/dendrite/relayapi"
|
|
|
|
relayAPI "github.com/matrix-org/dendrite/relayapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver"
|
|
|
|
"github.com/matrix-org/dendrite/setup"
|
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2023-02-01 00:35:21 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi"
|
|
|
|
userAPI "github.com/matrix-org/dendrite/userapi/api"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-02-01 00:35:21 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
pineconeConnections "github.com/matrix-org/pinecone/connections"
|
|
|
|
pineconeMulticast "github.com/matrix-org/pinecone/multicast"
|
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
|
|
|
pineconeEvents "github.com/matrix-org/pinecone/router/events"
|
|
|
|
pineconeSessions "github.com/matrix-org/pinecone/sessions"
|
|
|
|
)
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
const SessionProtocol = "matrix"
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
type P2PMonolith struct {
|
2023-02-01 20:41:27 +00:00
|
|
|
Sessions *pineconeSessions.Sessions
|
|
|
|
Multicast *pineconeMulticast.Multicast
|
|
|
|
ConnManager *pineconeConnections.ConnectionManager
|
|
|
|
Router *pineconeRouter.Router
|
|
|
|
EventChannel chan pineconeEvents.Event
|
|
|
|
RelayRetriever relay.RelayServerRetriever
|
2023-03-22 08:21:32 +00:00
|
|
|
ProcessCtx *process.ProcessContext
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-02-24 22:41:47 +00:00
|
|
|
dendrite setup.Monolith
|
|
|
|
port int
|
|
|
|
httpMux *mux.Router
|
|
|
|
pineconeMux *mux.Router
|
|
|
|
httpServer *http.Server
|
|
|
|
listener net.Listener
|
|
|
|
httpListenAddr string
|
|
|
|
stopHandlingEvents chan bool
|
2023-03-22 08:21:32 +00:00
|
|
|
httpServerMu sync.Mutex
|
2023-02-01 00:35:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
func GenerateDefaultConfig(sk ed25519.PrivateKey, storageDir string, cacheDir string, dbPrefix string) *config.Dendrite {
|
2023-02-01 00:35:21 +00:00
|
|
|
cfg := config.Dendrite{}
|
|
|
|
cfg.Defaults(config.DefaultOpts{
|
2023-02-14 11:47:47 +00:00
|
|
|
Generate: true,
|
|
|
|
SingleDatabase: true,
|
2023-02-01 00:35:21 +00:00
|
|
|
})
|
|
|
|
cfg.Global.PrivateKey = sk
|
2023-02-01 20:41:27 +00:00
|
|
|
cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", filepath.Join(cacheDir, dbPrefix)))
|
2023-02-01 00:35:21 +00:00
|
|
|
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.MediaAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mediaapi.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.SyncAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-syncapi.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.RoomServer.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-roomserver.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.KeyServer.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-keyserver.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.FederationAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-federationsender.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.RelayAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-relayapi.db", filepath.Join(storageDir, dbPrefix)))
|
2023-07-20 14:06:05 +00:00
|
|
|
cfg.MSCs.MSCs = []string{"msc2836"}
|
2023-02-01 00:35:21 +00:00
|
|
|
cfg.MSCs.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mscs.db", filepath.Join(storageDir, dbPrefix)))
|
|
|
|
cfg.ClientAPI.RegistrationDisabled = false
|
|
|
|
cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
|
2023-02-01 20:41:27 +00:00
|
|
|
cfg.MediaAPI.BasePath = config.Path(filepath.Join(cacheDir, "media"))
|
|
|
|
cfg.MediaAPI.AbsBasePath = config.Path(filepath.Join(cacheDir, "media"))
|
2023-02-01 00:35:21 +00:00
|
|
|
cfg.SyncAPI.Fulltext.Enabled = true
|
2023-02-01 20:41:27 +00:00
|
|
|
cfg.SyncAPI.Fulltext.IndexPath = config.Path(filepath.Join(cacheDir, "search"))
|
2023-02-01 00:35:21 +00:00
|
|
|
if err := cfg.Derive(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) SetupPinecone(sk ed25519.PrivateKey) {
|
|
|
|
p.EventChannel = make(chan pineconeEvents.Event)
|
|
|
|
p.Router = pineconeRouter.NewRouter(logrus.WithField("pinecone", "router"), sk)
|
|
|
|
p.Router.EnableHopLimiting()
|
|
|
|
p.Router.EnableWakeupBroadcasts()
|
|
|
|
p.Router.Subscribe(p.EventChannel)
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
p.Sessions = pineconeSessions.NewSessions(logrus.WithField("pinecone", "sessions"), p.Router, []string{SessionProtocol})
|
2023-02-01 00:35:21 +00:00
|
|
|
p.Multicast = pineconeMulticast.NewMulticast(logrus.WithField("pinecone", "multicast"), p.Router)
|
|
|
|
p.ConnManager = pineconeConnections.NewConnectionManager(p.Router, nil)
|
|
|
|
}
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
func (p *P2PMonolith) SetupDendrite(
|
2023-07-19 11:37:04 +00:00
|
|
|
processCtx *process.ProcessContext, cfg *config.Dendrite, cm *sqlutil.Connections, routers httputil.Routers,
|
2023-03-22 08:21:32 +00:00
|
|
|
port int, enableRelaying bool, enableMetrics bool, enableWebsockets bool) {
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
p.port = port
|
2023-03-22 08:21:32 +00:00
|
|
|
base.ConfigureAdminEndpoints(processCtx, routers)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
federation := conn.CreateFederationClient(cfg, p.Sessions)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
|
|
|
serverKeyAPI := &signing.YggdrasilKeys{}
|
|
|
|
keyRing := serverKeyAPI.KeyRing()
|
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
caches := caching.NewRistrettoCache(cfg.Global.Cache.EstimatedMaxSize, cfg.Global.Cache.MaxAge, enableMetrics)
|
2023-03-22 08:21:32 +00:00
|
|
|
natsInstance := jetstream.NATSInstance{}
|
|
|
|
rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, caches, enableMetrics)
|
2023-02-01 20:41:27 +00:00
|
|
|
fsAPI := federationapi.NewInternalAPI(
|
2023-03-22 08:21:32 +00:00
|
|
|
processCtx, cfg, cm, &natsInstance, federation, rsAPI, caches, keyRing, true,
|
2023-02-01 20:41:27 +00:00
|
|
|
)
|
2023-07-20 14:06:05 +00:00
|
|
|
rsAPI.SetFederationAPI(fsAPI, keyRing)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-11-09 07:43:27 +00:00
|
|
|
userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, federation, enableMetrics, fsAPI.IsBlacklistedOrBackingOff)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
asAPI := appservice.NewInternalAPI(processCtx, cfg, &natsInstance, userAPI, rsAPI)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
|
|
|
userProvider := users.NewPineconeUserProvider(p.Router, p.Sessions, userAPI, federation)
|
|
|
|
roomProvider := rooms.NewPineconeRoomProvider(p.Router, p.Sessions, fsAPI, federation)
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
js, _ := natsInstance.Prepare(processCtx, &cfg.Global.JetStream)
|
2023-02-01 20:41:27 +00:00
|
|
|
producer := &producers.SyncAPIProducer{
|
|
|
|
JetStream: js,
|
2023-03-22 08:21:32 +00:00
|
|
|
TopicReceiptEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputReceiptEvent),
|
|
|
|
TopicSendToDeviceEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputSendToDeviceEvent),
|
|
|
|
TopicTypingEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputTypingEvent),
|
|
|
|
TopicPresenceEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputPresenceEvent),
|
|
|
|
TopicDeviceListUpdate: cfg.Global.JetStream.Prefixed(jetstream.InputDeviceListUpdate),
|
|
|
|
TopicSigningKeyUpdate: cfg.Global.JetStream.Prefixed(jetstream.InputSigningKeyUpdate),
|
|
|
|
Config: &cfg.FederationAPI,
|
2023-02-01 20:41:27 +00:00
|
|
|
UserAPI: userAPI,
|
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
relayAPI := relayapi.NewRelayInternalAPI(cfg, cm, federation, rsAPI, keyRing, producer, enableRelaying, caches)
|
2023-02-01 20:41:27 +00:00
|
|
|
logrus.Infof("Relaying enabled: %v", relayAPI.RelayingEnabled())
|
|
|
|
|
|
|
|
p.dendrite = setup.Monolith{
|
2023-03-22 08:21:32 +00:00
|
|
|
Config: cfg,
|
|
|
|
Client: conn.CreateClient(p.Sessions),
|
2023-02-01 20:41:27 +00:00
|
|
|
FedClient: federation,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
|
|
|
|
AppserviceAPI: asAPI,
|
|
|
|
FederationAPI: fsAPI,
|
|
|
|
RoomserverAPI: rsAPI,
|
|
|
|
UserAPI: userAPI,
|
|
|
|
RelayAPI: relayAPI,
|
|
|
|
ExtPublicRoomsProvider: roomProvider,
|
|
|
|
ExtUserDirectoryProvider: userProvider,
|
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
p.ProcessCtx = processCtx
|
|
|
|
p.dendrite.AddAllPublicRoutes(processCtx, cfg, routers, cm, &natsInstance, caches, enableMetrics)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
p.setupHttpServers(userProvider, routers, enableWebsockets)
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) GetFederationAPI() federationAPI.FederationInternalAPI {
|
|
|
|
return p.dendrite.FederationAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) GetRelayAPI() relayAPI.RelayInternalAPI {
|
|
|
|
return p.dendrite.RelayAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) GetUserAPI() userAPI.UserInternalAPI {
|
|
|
|
return p.dendrite.UserAPI
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:11:48 +00:00
|
|
|
func (p *P2PMonolith) StartMonolith() {
|
|
|
|
p.startHTTPServers()
|
2023-02-01 20:41:27 +00:00
|
|
|
p.startEventHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) Stop() {
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Stopping monolith")
|
2023-03-22 08:21:32 +00:00
|
|
|
p.ProcessCtx.ShutdownDendrite()
|
2023-02-01 20:41:27 +00:00
|
|
|
p.WaitForShutdown()
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Stopped monolith")
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) WaitForShutdown() {
|
2023-03-27 23:19:53 +00:00
|
|
|
base.WaitForShutdown(p.ProcessCtx)
|
2023-02-01 20:41:27 +00:00
|
|
|
p.closeAllResources()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) closeAllResources() {
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Closing monolith resources")
|
2023-03-22 08:21:32 +00:00
|
|
|
p.httpServerMu.Lock()
|
2023-02-24 22:41:47 +00:00
|
|
|
if p.httpServer != nil {
|
2023-02-24 22:49:51 +00:00
|
|
|
_ = p.httpServer.Shutdown(context.Background())
|
2023-02-24 22:41:47 +00:00
|
|
|
}
|
2023-08-11 14:29:48 +00:00
|
|
|
p.httpServerMu.Unlock()
|
2023-02-24 22:41:47 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p.stopHandlingEvents <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
if p.listener != nil {
|
|
|
|
_ = p.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Multicast != nil {
|
|
|
|
p.Multicast.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Sessions != nil {
|
|
|
|
_ = p.Sessions.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Router != nil {
|
|
|
|
_ = p.Router.Close()
|
|
|
|
}
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Monolith resources closed")
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) Addr() string {
|
|
|
|
return p.httpListenAddr
|
|
|
|
}
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider, routers httputil.Routers, enableWebsockets bool) {
|
2023-02-01 20:41:27 +00:00
|
|
|
p.httpMux = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2023-03-22 08:21:32 +00:00
|
|
|
p.httpMux.PathPrefix(httputil.PublicClientPathPrefix).Handler(routers.Client)
|
|
|
|
p.httpMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
|
|
|
|
p.httpMux.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(routers.DendriteAdmin)
|
|
|
|
p.httpMux.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(routers.SynapseAdmin)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
|
|
|
if enableWebsockets {
|
|
|
|
wsUpgrader := websocket.Upgrader{
|
|
|
|
CheckOrigin: func(_ *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.httpMux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c, err := wsUpgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to upgrade WebSocket connection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn := conn.WrapWebSocketConn(c)
|
|
|
|
if _, err = p.Router.Connect(
|
|
|
|
conn,
|
|
|
|
pineconeRouter.ConnectionZone("websocket"),
|
|
|
|
pineconeRouter.ConnectionPeerType(pineconeRouter.PeerTypeRemote),
|
|
|
|
); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to connect WebSocket peer to Pinecone switch")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
p.httpMux.HandleFunc("/pinecone", p.Router.ManholeHandler)
|
|
|
|
|
|
|
|
if enableWebsockets {
|
|
|
|
embed.Embed(p.httpMux, p.port, "Pinecone Demo")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.pineconeMux = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
|
|
|
p.pineconeMux.PathPrefix(users.PublicURL).HandlerFunc(userProvider.FederatedUserProfiles)
|
2023-03-22 08:21:32 +00:00
|
|
|
p.pineconeMux.PathPrefix(httputil.PublicFederationPathPrefix).Handler(routers.Federation)
|
|
|
|
p.pineconeMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
|
2023-02-01 20:41:27 +00:00
|
|
|
|
|
|
|
pHTTP := p.Sessions.Protocol(SessionProtocol).HTTP()
|
|
|
|
pHTTP.Mux().Handle(users.PublicURL, p.pineconeMux)
|
|
|
|
pHTTP.Mux().Handle(httputil.PublicFederationPathPrefix, p.pineconeMux)
|
|
|
|
pHTTP.Mux().Handle(httputil.PublicMediaPathPrefix, p.pineconeMux)
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:11:48 +00:00
|
|
|
func (p *P2PMonolith) startHTTPServers() {
|
2023-02-01 20:41:27 +00:00
|
|
|
go func() {
|
2023-03-22 08:21:32 +00:00
|
|
|
p.httpServerMu.Lock()
|
2023-02-01 20:41:27 +00:00
|
|
|
// Build both ends of a HTTP multiplex.
|
2023-02-24 22:41:47 +00:00
|
|
|
p.httpServer = &http.Server{
|
2023-02-01 20:41:27 +00:00
|
|
|
Addr: ":0",
|
|
|
|
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 30 * time.Second,
|
|
|
|
BaseContext: func(_ net.Listener) context.Context {
|
|
|
|
return context.Background()
|
|
|
|
},
|
2023-02-01 21:11:48 +00:00
|
|
|
Handler: p.pineconeMux,
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
2023-03-22 08:21:32 +00:00
|
|
|
p.httpServerMu.Unlock()
|
2023-02-01 20:41:27 +00:00
|
|
|
pubkey := p.Router.PublicKey()
|
|
|
|
pubkeyString := hex.EncodeToString(pubkey[:])
|
|
|
|
logrus.Info("Listening on ", pubkeyString)
|
|
|
|
|
2023-02-24 22:41:47 +00:00
|
|
|
switch p.httpServer.Serve(p.Sessions.Protocol(SessionProtocol)) {
|
2023-02-01 20:41:27 +00:00
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
logrus.Info("Stopped listening on ", pubkeyString)
|
|
|
|
default:
|
|
|
|
logrus.Error("Stopped listening on ", pubkeyString)
|
|
|
|
}
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Stopped goroutine listening on ", pubkeyString)
|
2023-02-01 20:41:27 +00:00
|
|
|
}()
|
|
|
|
|
2023-02-01 21:11:48 +00:00
|
|
|
p.httpListenAddr = fmt.Sprintf(":%d", p.port)
|
2023-02-01 20:41:27 +00:00
|
|
|
go func() {
|
|
|
|
logrus.Info("Listening on ", p.httpListenAddr)
|
2023-02-01 21:11:48 +00:00
|
|
|
switch http.ListenAndServe(p.httpListenAddr, p.httpMux) {
|
2023-02-01 20:41:27 +00:00
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
logrus.Info("Stopped listening on ", p.httpListenAddr)
|
|
|
|
default:
|
|
|
|
logrus.Error("Stopped listening on ", p.httpListenAddr)
|
|
|
|
}
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Stopped goroutine listening on ", p.httpListenAddr)
|
2023-02-01 20:41:27 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P2PMonolith) startEventHandler() {
|
2023-02-24 22:41:47 +00:00
|
|
|
p.stopHandlingEvents = make(chan bool)
|
2023-02-01 20:41:27 +00:00
|
|
|
stopRelayServerSync := make(chan bool)
|
|
|
|
eLog := logrus.WithField("pinecone", "events")
|
|
|
|
p.RelayRetriever = relay.NewRelayServerRetriever(
|
|
|
|
context.Background(),
|
2023-04-19 14:50:33 +00:00
|
|
|
spec.ServerName(p.Router.PublicKey().String()),
|
2023-02-01 20:41:27 +00:00
|
|
|
p.dendrite.FederationAPI,
|
|
|
|
p.dendrite.RelayAPI,
|
|
|
|
stopRelayServerSync,
|
|
|
|
)
|
|
|
|
p.RelayRetriever.InitializeRelayServers(eLog)
|
|
|
|
|
|
|
|
go func(ch <-chan pineconeEvents.Event) {
|
2023-02-24 22:41:47 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-ch:
|
|
|
|
switch e := event.(type) {
|
|
|
|
case pineconeEvents.PeerAdded:
|
|
|
|
p.RelayRetriever.StartSync()
|
|
|
|
case pineconeEvents.PeerRemoved:
|
|
|
|
if p.RelayRetriever.IsRunning() && p.Router.TotalPeerCount() == 0 {
|
|
|
|
// NOTE: Don't block on channel
|
|
|
|
select {
|
|
|
|
case stopRelayServerSync <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case pineconeEvents.BroadcastReceived:
|
|
|
|
// eLog.Info("Broadcast received from: ", e.PeerID)
|
|
|
|
|
|
|
|
req := &federationAPI.PerformWakeupServersRequest{
|
2023-04-19 14:50:33 +00:00
|
|
|
ServerNames: []spec.ServerName{spec.ServerName(e.PeerID)},
|
2023-02-24 22:41:47 +00:00
|
|
|
}
|
|
|
|
res := &federationAPI.PerformWakeupServersResponse{}
|
2023-03-22 08:21:32 +00:00
|
|
|
if err := p.dendrite.FederationAPI.PerformWakeupServers(p.ProcessCtx.Context(), req, res); err != nil {
|
2023-02-24 22:41:47 +00:00
|
|
|
eLog.WithError(err).Error("Failed to wakeup destination", e.PeerID)
|
|
|
|
}
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
2023-02-24 22:41:47 +00:00
|
|
|
case <-p.stopHandlingEvents:
|
|
|
|
logrus.Info("Stopping processing pinecone events")
|
|
|
|
// NOTE: Don't block on channel
|
|
|
|
select {
|
|
|
|
case stopRelayServerSync <- true:
|
|
|
|
default:
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
2023-02-24 22:41:47 +00:00
|
|
|
logrus.Info("Stopped processing pinecone events")
|
|
|
|
return
|
2023-02-01 20:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(p.EventChannel)
|
|
|
|
}
|