2022-03-30 09:01:22 -05:00
|
|
|
// Copyright 2022 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.
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
package gobind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-09-01 11:12:27 -05:00
|
|
|
"path/filepath"
|
2021-11-25 03:46:26 -06:00
|
|
|
"strings"
|
2021-05-06 06:00:42 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-29 10:08:00 -05:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/appservice"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/userutil"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conn"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/rooms"
|
2022-03-28 10:25:26 -05:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/users"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
2021-11-24 04:45:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/federationapi"
|
2022-11-17 18:29:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver"
|
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2021-11-24 04:45:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2022-09-01 11:12:27 -05:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/userapi"
|
|
|
|
userapiAPI "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
|
|
|
|
2022-04-08 07:55:46 -05:00
|
|
|
pineconeConnections "github.com/matrix-org/pinecone/connections"
|
2021-05-06 06:00:42 -05:00
|
|
|
pineconeMulticast "github.com/matrix-org/pinecone/multicast"
|
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
2022-11-17 18:29:23 -06:00
|
|
|
pineconeEvents "github.com/matrix-org/pinecone/router/events"
|
2021-05-06 06:00:42 -05:00
|
|
|
pineconeSessions "github.com/matrix-org/pinecone/sessions"
|
|
|
|
"github.com/matrix-org/pinecone/types"
|
2021-05-25 03:49:02 -05:00
|
|
|
|
|
|
|
_ "golang.org/x/mobile/bind"
|
2021-05-06 06:00:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PeerTypeRemote = pineconeRouter.PeerTypeRemote
|
|
|
|
PeerTypeMulticast = pineconeRouter.PeerTypeMulticast
|
|
|
|
PeerTypeBluetooth = pineconeRouter.PeerTypeBluetooth
|
2022-09-29 07:06:55 -05:00
|
|
|
PeerTypeBonjour = pineconeRouter.PeerTypeBonjour
|
2021-05-06 06:00:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DendriteMonolith struct {
|
2021-06-14 07:13:07 -05:00
|
|
|
logger logrus.Logger
|
|
|
|
PineconeRouter *pineconeRouter.Router
|
|
|
|
PineconeMulticast *pineconeMulticast.Multicast
|
|
|
|
PineconeQUIC *pineconeSessions.Sessions
|
2022-04-08 07:55:46 -05:00
|
|
|
PineconeManager *pineconeConnections.ConnectionManager
|
2021-06-14 07:13:07 -05:00
|
|
|
StorageDirectory string
|
|
|
|
CacheDirectory string
|
|
|
|
listener net.Listener
|
|
|
|
httpServer *http.Server
|
|
|
|
processContext *process.ProcessContext
|
|
|
|
userAPI userapiAPI.UserInternalAPI
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:06:55 -05:00
|
|
|
func (m *DendriteMonolith) PublicKey() string {
|
|
|
|
return m.PineconeRouter.PublicKey().String()
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
func (m *DendriteMonolith) BaseURL() string {
|
|
|
|
return fmt.Sprintf("http://%s", m.listener.Addr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) PeerCount(peertype int) int {
|
2021-11-25 05:18:01 -06:00
|
|
|
return m.PineconeRouter.PeerCount(peertype)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SessionCount() int {
|
2022-04-04 09:16:02 -05:00
|
|
|
return len(m.PineconeQUIC.Protocol("matrix").Sessions())
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:25:57 -05:00
|
|
|
type InterfaceInfo struct {
|
|
|
|
Name string
|
|
|
|
Index int
|
|
|
|
Mtu int
|
|
|
|
Up bool
|
|
|
|
Broadcast bool
|
|
|
|
Loopback bool
|
|
|
|
PointToPoint bool
|
|
|
|
Multicast bool
|
|
|
|
Addrs string
|
|
|
|
}
|
|
|
|
|
|
|
|
type InterfaceRetriever interface {
|
|
|
|
CacheCurrentInterfaces() int
|
|
|
|
GetCachedInterface(index int) *InterfaceInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) RegisterNetworkCallback(intfCallback InterfaceRetriever) {
|
|
|
|
callback := func() []pineconeMulticast.InterfaceInfo {
|
|
|
|
count := intfCallback.CacheCurrentInterfaces()
|
|
|
|
intfs := []pineconeMulticast.InterfaceInfo{}
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
iface := intfCallback.GetCachedInterface(i)
|
|
|
|
if iface != nil {
|
|
|
|
intfs = append(intfs, pineconeMulticast.InterfaceInfo{
|
|
|
|
Name: iface.Name,
|
|
|
|
Index: iface.Index,
|
|
|
|
Mtu: iface.Mtu,
|
|
|
|
Up: iface.Up,
|
|
|
|
Broadcast: iface.Broadcast,
|
|
|
|
Loopback: iface.Loopback,
|
|
|
|
PointToPoint: iface.PointToPoint,
|
|
|
|
Multicast: iface.Multicast,
|
|
|
|
Addrs: iface.Addrs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return intfs
|
|
|
|
}
|
|
|
|
m.PineconeMulticast.RegisterNetworkCallback(callback)
|
2022-09-29 11:05:16 -05:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
func (m *DendriteMonolith) SetMulticastEnabled(enabled bool) {
|
|
|
|
if enabled {
|
|
|
|
m.PineconeMulticast.Start()
|
|
|
|
} else {
|
|
|
|
m.PineconeMulticast.Stop()
|
2021-11-25 03:46:26 -06:00
|
|
|
m.DisconnectType(int(pineconeRouter.PeerTypeMulticast))
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SetStaticPeer(uri string) {
|
2022-04-08 07:55:46 -05:00
|
|
|
m.PineconeManager.RemovePeers()
|
2022-10-03 08:43:38 -05:00
|
|
|
for _, uri := range strings.Split(uri, ",") {
|
|
|
|
m.PineconeManager.AddPeer(strings.TrimSpace(uri))
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) DisconnectType(peertype int) {
|
|
|
|
for _, p := range m.PineconeRouter.Peers() {
|
2021-11-25 03:46:26 -06:00
|
|
|
if int(peertype) == p.PeerType {
|
2021-10-08 09:29:30 -05:00
|
|
|
m.PineconeRouter.Disconnect(types.SwitchPortID(p.Port), nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) DisconnectZone(zone string) {
|
|
|
|
for _, p := range m.PineconeRouter.Peers() {
|
|
|
|
if zone == p.Zone {
|
2021-10-08 09:29:30 -05:00
|
|
|
m.PineconeRouter.Disconnect(types.SwitchPortID(p.Port), nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 09:29:30 -05:00
|
|
|
func (m *DendriteMonolith) DisconnectPort(port int) {
|
|
|
|
m.PineconeRouter.Disconnect(types.SwitchPortID(port), nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) Conduit(zone string, peertype int) (*Conduit, error) {
|
|
|
|
l, r := net.Pipe()
|
|
|
|
conduit := &Conduit{conn: r, port: 0}
|
|
|
|
go func() {
|
|
|
|
conduit.portMutex.Lock()
|
|
|
|
defer conduit.portMutex.Unlock()
|
2022-09-29 07:06:55 -05:00
|
|
|
|
|
|
|
logrus.Errorf("Attempting authenticated connect")
|
|
|
|
var err error
|
|
|
|
if conduit.port, err = m.PineconeRouter.Connect(
|
|
|
|
l,
|
|
|
|
pineconeRouter.ConnectionZone(zone),
|
|
|
|
pineconeRouter.ConnectionPeerType(peertype),
|
|
|
|
); err != nil {
|
|
|
|
logrus.Errorf("Authenticated connect failed: %s", err)
|
|
|
|
_ = l.Close()
|
|
|
|
_ = r.Close()
|
|
|
|
_ = conduit.Close()
|
|
|
|
return
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
2022-09-29 07:06:55 -05:00
|
|
|
logrus.Infof("Authenticated connect succeeded (port %d)", conduit.port)
|
2021-05-06 06:00:42 -05:00
|
|
|
}()
|
|
|
|
return conduit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) RegisterUser(localpart, password string) (string, error) {
|
|
|
|
pubkey := m.PineconeRouter.PublicKey()
|
|
|
|
userID := userutil.MakeUserID(
|
|
|
|
localpart,
|
|
|
|
gomatrixserverlib.ServerName(hex.EncodeToString(pubkey[:])),
|
|
|
|
)
|
|
|
|
userReq := &userapiAPI.PerformAccountCreationRequest{
|
|
|
|
AccountType: userapiAPI.AccountTypeUser,
|
|
|
|
Localpart: localpart,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
userRes := &userapiAPI.PerformAccountCreationResponse{}
|
|
|
|
if err := m.userAPI.PerformAccountCreation(context.Background(), userReq, userRes); err != nil {
|
|
|
|
return userID, fmt.Errorf("userAPI.PerformAccountCreation: %w", err)
|
|
|
|
}
|
|
|
|
return userID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) RegisterDevice(localpart, deviceID string) (string, error) {
|
|
|
|
accessTokenBytes := make([]byte, 16)
|
|
|
|
n, err := rand.Read(accessTokenBytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("rand.Read: %w", err)
|
|
|
|
}
|
|
|
|
loginReq := &userapiAPI.PerformDeviceCreationRequest{
|
|
|
|
Localpart: localpart,
|
|
|
|
DeviceID: &deviceID,
|
|
|
|
AccessToken: hex.EncodeToString(accessTokenBytes[:n]),
|
|
|
|
}
|
|
|
|
loginRes := &userapiAPI.PerformDeviceCreationResponse{}
|
|
|
|
if err := m.userAPI.PerformDeviceCreation(context.Background(), loginReq, loginRes); err != nil {
|
|
|
|
return "", fmt.Errorf("userAPI.PerformDeviceCreation: %w", err)
|
|
|
|
}
|
|
|
|
if !loginRes.DeviceCreated {
|
|
|
|
return "", fmt.Errorf("device was not created")
|
|
|
|
}
|
|
|
|
return loginRes.Device.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint:gocyclo
|
|
|
|
func (m *DendriteMonolith) Start() {
|
|
|
|
var sk ed25519.PrivateKey
|
|
|
|
var pk ed25519.PublicKey
|
2022-09-01 11:12:27 -05:00
|
|
|
|
|
|
|
keyfile := filepath.Join(m.StorageDirectory, "p2p.pem")
|
|
|
|
if _, err := os.Stat(keyfile); os.IsNotExist(err) {
|
|
|
|
oldkeyfile := filepath.Join(m.StorageDirectory, "p2p.key")
|
|
|
|
if _, err = os.Stat(oldkeyfile); os.IsNotExist(err) {
|
|
|
|
if err = test.NewMatrixKey(keyfile); err != nil {
|
|
|
|
panic("failed to generate a new PEM key: " + err.Error())
|
|
|
|
}
|
|
|
|
if _, sk, err = config.LoadMatrixKey(keyfile, os.ReadFile); err != nil {
|
|
|
|
panic("failed to load PEM key: " + err.Error())
|
|
|
|
}
|
2022-09-12 04:19:02 -05:00
|
|
|
if len(sk) != ed25519.PrivateKeySize {
|
|
|
|
panic("the private key is not long enough")
|
|
|
|
}
|
2022-09-01 11:12:27 -05:00
|
|
|
} else {
|
|
|
|
if sk, err = os.ReadFile(oldkeyfile); err != nil {
|
|
|
|
panic("failed to read the old private key: " + err.Error())
|
|
|
|
}
|
|
|
|
if len(sk) != ed25519.PrivateKeySize {
|
|
|
|
panic("the private key is not long enough")
|
|
|
|
}
|
|
|
|
if err = test.SaveMatrixKey(keyfile, sk); err != nil {
|
|
|
|
panic("failed to convert the private key to PEM format: " + err.Error())
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
2022-09-01 11:12:27 -05:00
|
|
|
} else {
|
|
|
|
if _, sk, err = config.LoadMatrixKey(keyfile, os.ReadFile); err != nil {
|
|
|
|
panic("failed to load PEM key: " + err.Error())
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
2022-09-12 04:19:02 -05:00
|
|
|
if len(sk) != ed25519.PrivateKeySize {
|
|
|
|
panic("the private key is not long enough")
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2022-09-12 04:19:02 -05:00
|
|
|
pk = sk.Public().(ed25519.PublicKey)
|
|
|
|
|
2022-09-01 11:12:27 -05:00
|
|
|
var err error
|
2022-05-13 06:06:47 -05:00
|
|
|
m.listener, err = net.Listen("tcp", "localhost:65432")
|
2021-05-06 06:00:42 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logger = logrus.Logger{
|
|
|
|
Out: BindLogger{},
|
|
|
|
}
|
|
|
|
m.logger.SetOutput(BindLogger{})
|
|
|
|
logrus.SetOutput(BindLogger{})
|
|
|
|
|
2022-11-17 18:29:23 -06:00
|
|
|
pineconeEventChannel := make(chan pineconeEvents.Event)
|
2022-09-14 08:26:24 -05:00
|
|
|
m.PineconeRouter = pineconeRouter.NewRouter(logrus.WithField("pinecone", "router"), sk)
|
2022-11-17 18:29:23 -06:00
|
|
|
m.PineconeRouter.EnableHopLimiting()
|
|
|
|
m.PineconeRouter.EnableWakeupBroadcasts()
|
|
|
|
m.PineconeRouter.Subscribe(pineconeEventChannel)
|
|
|
|
|
2022-04-04 09:16:02 -05:00
|
|
|
m.PineconeQUIC = pineconeSessions.NewSessions(logrus.WithField("pinecone", "sessions"), m.PineconeRouter, []string{"matrix"})
|
|
|
|
m.PineconeMulticast = pineconeMulticast.NewMulticast(logrus.WithField("pinecone", "multicast"), m.PineconeRouter)
|
2022-07-08 08:54:08 -05:00
|
|
|
m.PineconeManager = pineconeConnections.NewConnectionManager(m.PineconeRouter, nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
|
|
|
prefix := hex.EncodeToString(pk)
|
|
|
|
cfg := &config.Dendrite{}
|
2022-09-01 08:15:41 -05:00
|
|
|
cfg.Defaults(config.DefaultOpts{
|
|
|
|
Generate: true,
|
|
|
|
Monolithic: true,
|
|
|
|
})
|
2021-05-06 06:00:42 -05:00
|
|
|
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
|
|
|
|
cfg.Global.PrivateKey = sk
|
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)
|
2022-10-03 08:35:10 -05:00
|
|
|
cfg.Global.JetStream.InMemory = false
|
|
|
|
cfg.Global.JetStream.StoragePath = config.Path(filepath.Join(m.CacheDirectory, prefix))
|
|
|
|
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.MediaAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mediaapi.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.SyncAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-syncapi.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.RoomServer.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-roomserver.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.KeyServer.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-keyserver.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.FederationAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-federationsender.db", filepath.Join(m.StorageDirectory, prefix)))
|
|
|
|
cfg.MediaAPI.BasePath = config.Path(filepath.Join(m.CacheDirectory, "media"))
|
|
|
|
cfg.MediaAPI.AbsBasePath = config.Path(filepath.Join(m.CacheDirectory, "media"))
|
2021-05-18 09:47:15 -05:00
|
|
|
cfg.MSCs.MSCs = []string{"msc2836", "msc2946"}
|
2022-04-29 02:31:11 -05:00
|
|
|
cfg.ClientAPI.RegistrationDisabled = false
|
|
|
|
cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
|
2022-10-03 08:35:10 -05:00
|
|
|
cfg.SyncAPI.Fulltext.Enabled = true
|
|
|
|
cfg.SyncAPI.Fulltext.IndexPath = config.Path(filepath.Join(m.CacheDirectory, "search"))
|
2022-06-07 04:46:21 -05:00
|
|
|
if err = cfg.Derive(); err != nil {
|
2021-05-06 06:00:42 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
base := base.NewBaseDendrite(cfg, "Monolith")
|
2022-12-01 04:45:15 -06:00
|
|
|
base.ConfigureAdminEndpoints()
|
2021-05-06 06:00:42 -05:00
|
|
|
defer base.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
federation := conn.CreateFederationClient(base, m.PineconeQUIC)
|
|
|
|
|
|
|
|
serverKeyAPI := &signing.YggdrasilKeys{}
|
|
|
|
keyRing := serverKeyAPI.KeyRing()
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
rsAPI := roomserver.NewInternalAPI(base)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
fsAPI := federationapi.NewInternalAPI(
|
2021-12-13 07:24:49 -06:00
|
|
|
base, federation, rsAPI, base.Caches, keyRing, true,
|
2021-05-06 06:00:42 -05:00
|
|
|
)
|
|
|
|
|
2022-12-12 01:20:59 -06:00
|
|
|
keyAPI := keyserver.NewInternalAPI(base, &base.Cfg.KeyServer, fsAPI, rsAPI)
|
2022-05-03 10:35:06 -05:00
|
|
|
m.userAPI = userapi.NewInternalAPI(base, &cfg.UserAPI, cfg.Derived.ApplicationServices, keyAPI, rsAPI, base.PushGatewayHTTPClient())
|
2021-05-06 06:00:42 -05:00
|
|
|
keyAPI.SetUserAPI(m.userAPI)
|
|
|
|
|
|
|
|
asAPI := appservice.NewInternalAPI(base, m.userAPI, rsAPI)
|
|
|
|
|
|
|
|
// The underlying roomserver implementation needs to be able to call the fedsender.
|
|
|
|
// This is different to rsAPI which can be the http client which doesn't need this dependency
|
2021-12-13 07:24:49 -06:00
|
|
|
rsAPI.SetFederationAPI(fsAPI, keyRing)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
2022-03-28 10:25:26 -05:00
|
|
|
userProvider := users.NewPineconeUserProvider(m.PineconeRouter, m.PineconeQUIC, m.userAPI, federation)
|
|
|
|
roomProvider := rooms.NewPineconeRoomProvider(m.PineconeRouter, m.PineconeQUIC, fsAPI, federation)
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
monolith := setup.Monolith{
|
|
|
|
Config: base.Cfg,
|
|
|
|
Client: conn.CreateClient(base, m.PineconeQUIC),
|
|
|
|
FedClient: federation,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
|
2022-03-28 10:25:26 -05:00
|
|
|
AppserviceAPI: asAPI,
|
|
|
|
FederationAPI: fsAPI,
|
|
|
|
RoomserverAPI: rsAPI,
|
|
|
|
UserAPI: m.userAPI,
|
|
|
|
KeyAPI: keyAPI,
|
|
|
|
ExtPublicRoomsProvider: roomProvider,
|
|
|
|
ExtUserDirectoryProvider: userProvider,
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
2022-05-03 11:17:02 -05:00
|
|
|
monolith.AddAllPublicRoutes(base)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
|
|
|
httpRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
|
|
|
httpRouter.PathPrefix(httputil.InternalPathPrefix).Handler(base.InternalAPIMux)
|
|
|
|
httpRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(base.PublicClientAPIMux)
|
|
|
|
httpRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(base.PublicMediaAPIMux)
|
2022-12-01 04:24:17 -06:00
|
|
|
httpRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(base.DendriteAdminMux)
|
|
|
|
httpRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(base.SynapseAdminMux)
|
2022-03-30 09:01:22 -05:00
|
|
|
httpRouter.HandleFunc("/pinecone", m.PineconeRouter.ManholeHandler)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
|
|
|
pMux := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2022-03-28 10:25:26 -05:00
|
|
|
pMux.PathPrefix(users.PublicURL).HandlerFunc(userProvider.FederatedUserProfiles)
|
2021-05-06 06:00:42 -05:00
|
|
|
pMux.PathPrefix(httputil.PublicFederationPathPrefix).Handler(base.PublicFederationAPIMux)
|
|
|
|
pMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(base.PublicMediaAPIMux)
|
|
|
|
|
2022-04-04 09:16:02 -05:00
|
|
|
pHTTP := m.PineconeQUIC.Protocol("matrix").HTTP()
|
2022-03-28 10:25:26 -05:00
|
|
|
pHTTP.Mux().Handle(users.PublicURL, pMux)
|
2021-05-06 06:00:42 -05:00
|
|
|
pHTTP.Mux().Handle(httputil.PublicFederationPathPrefix, pMux)
|
|
|
|
pHTTP.Mux().Handle(httputil.PublicMediaPathPrefix, pMux)
|
|
|
|
|
|
|
|
// Build both ends of a HTTP multiplex.
|
|
|
|
h2s := &http2.Server{}
|
|
|
|
m.httpServer = &http.Server{
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
Handler: h2c.NewHandler(pMux, h2s),
|
|
|
|
}
|
2021-06-14 07:13:07 -05:00
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
m.processContext = base.ProcessContext
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
m.logger.Info("Listening on ", cfg.Global.ServerName)
|
2022-06-07 04:46:21 -05:00
|
|
|
|
|
|
|
switch m.httpServer.Serve(m.PineconeQUIC.Protocol("matrix")) {
|
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
m.logger.Info("Stopped listening on ", cfg.Global.ServerName)
|
|
|
|
default:
|
|
|
|
m.logger.Fatal(err)
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
logrus.Info("Listening on ", m.listener.Addr())
|
2022-06-07 04:46:21 -05:00
|
|
|
|
|
|
|
switch http.Serve(m.listener, httpRouter) {
|
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
m.logger.Info("Stopped listening on ", cfg.Global.ServerName)
|
|
|
|
default:
|
|
|
|
m.logger.Fatal(err)
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}()
|
2022-11-17 18:29:23 -06:00
|
|
|
|
|
|
|
go func(ch <-chan pineconeEvents.Event) {
|
|
|
|
eLog := logrus.WithField("pinecone", "events")
|
|
|
|
|
|
|
|
for event := range ch {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case pineconeEvents.PeerAdded:
|
|
|
|
case pineconeEvents.PeerRemoved:
|
|
|
|
case pineconeEvents.TreeParentUpdate:
|
|
|
|
case pineconeEvents.SnakeDescUpdate:
|
|
|
|
case pineconeEvents.TreeRootAnnUpdate:
|
|
|
|
case pineconeEvents.SnakeEntryAdded:
|
|
|
|
case pineconeEvents.SnakeEntryRemoved:
|
|
|
|
case pineconeEvents.BroadcastReceived:
|
|
|
|
eLog.Info("Broadcast received from: ", e.PeerID)
|
|
|
|
|
|
|
|
req := &api.PerformWakeupServersRequest{
|
|
|
|
ServerNames: []gomatrixserverlib.ServerName{gomatrixserverlib.ServerName(e.PeerID)},
|
|
|
|
}
|
|
|
|
res := &api.PerformWakeupServersResponse{}
|
|
|
|
if err := fsAPI.PerformWakeupServers(base.Context(), req, res); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to wakeup destination", e.PeerID)
|
|
|
|
}
|
|
|
|
case pineconeEvents.BandwidthReport:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(pineconeEventChannel)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) Stop() {
|
2022-03-28 04:55:53 -05:00
|
|
|
m.processContext.ShutdownDendrite()
|
2021-05-06 06:00:42 -05:00
|
|
|
_ = m.listener.Close()
|
|
|
|
m.PineconeMulticast.Stop()
|
|
|
|
_ = m.PineconeQUIC.Close()
|
|
|
|
_ = m.PineconeRouter.Close()
|
2022-03-28 04:55:53 -05:00
|
|
|
m.processContext.WaitForComponentsToFinish()
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:32:14 -05:00
|
|
|
const MaxFrameSize = types.MaxFrameSize
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
type Conduit struct {
|
2022-09-29 07:06:55 -05:00
|
|
|
closed atomic.Bool
|
2021-05-06 06:00:42 -05:00
|
|
|
conn net.Conn
|
|
|
|
port types.SwitchPortID
|
|
|
|
portMutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conduit) Port() int {
|
|
|
|
c.portMutex.Lock()
|
|
|
|
defer c.portMutex.Unlock()
|
|
|
|
return int(c.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conduit) Read(b []byte) (int, error) {
|
2022-09-29 07:06:55 -05:00
|
|
|
if c.closed.Load() {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
return c.conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conduit) ReadCopy() ([]byte, error) {
|
2022-09-29 07:06:55 -05:00
|
|
|
if c.closed.Load() {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
var buf [65535 * 2]byte
|
|
|
|
n, err := c.conn.Read(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conduit) Write(b []byte) (int, error) {
|
2022-09-29 07:06:55 -05:00
|
|
|
if c.closed.Load() {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
return c.conn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conduit) Close() error {
|
2022-09-29 07:06:55 -05:00
|
|
|
if c.closed.Load() {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
c.closed.Store(true)
|
2021-05-06 06:00:42 -05:00
|
|
|
return c.conn.Close()
|
|
|
|
}
|