2022-03-30 14:01:22 +00: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 11:00:42 +00:00
|
|
|
package gobind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-09-01 16:12:27 +00:00
|
|
|
"path/filepath"
|
2021-11-25 09:46:26 +00:00
|
|
|
"strings"
|
2021-05-06 11:00:42 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/appservice"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/userutil"
|
2023-01-31 23:10:48 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conduit"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conn"
|
2023-02-01 00:35:21 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/monolith"
|
2023-01-31 22:51:08 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/relay"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/rooms"
|
2022-03-28 15:25:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/users"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
2021-11-24 10:45:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi"
|
2022-11-18 00:29:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/matrix-org/dendrite/relayapi"
|
|
|
|
relayServerAPI "github.com/matrix-org/dendrite/relayapi/api"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver"
|
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2021-11-24 10:45:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi"
|
|
|
|
userapiAPI "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-02-01 00:35:21 +00:00
|
|
|
"github.com/matrix-org/pinecone/types"
|
2021-05-06 11:00:42 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
|
|
|
|
|
|
|
pineconeMulticast "github.com/matrix-org/pinecone/multicast"
|
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
2022-11-18 00:29:23 +00:00
|
|
|
pineconeEvents "github.com/matrix-org/pinecone/router/events"
|
2021-05-25 08:49:02 +00:00
|
|
|
|
|
|
|
_ "golang.org/x/mobile/bind"
|
2021-05-06 11:00:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-31 22:51:08 +00:00
|
|
|
PeerTypeRemote = pineconeRouter.PeerTypeRemote
|
|
|
|
PeerTypeMulticast = pineconeRouter.PeerTypeMulticast
|
|
|
|
PeerTypeBluetooth = pineconeRouter.PeerTypeBluetooth
|
|
|
|
PeerTypeBonjour = pineconeRouter.PeerTypeBonjour
|
2021-05-06 11:00:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DendriteMonolith struct {
|
2023-02-01 00:35:21 +00:00
|
|
|
logger logrus.Logger
|
|
|
|
baseDendrite *base.BaseDendrite
|
|
|
|
p2pMonolith monolith.P2PMonolith
|
|
|
|
StorageDirectory string
|
|
|
|
listener net.Listener
|
|
|
|
httpServer *http.Server
|
|
|
|
userAPI userapiAPI.UserInternalAPI
|
|
|
|
federationAPI api.FederationInternalAPI
|
|
|
|
relayAPI relayServerAPI.RelayInternalAPI
|
|
|
|
relayRetriever relay.RelayServerRetriever
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 12:06:55 +00:00
|
|
|
func (m *DendriteMonolith) PublicKey() string {
|
2023-02-01 00:35:21 +00:00
|
|
|
return m.p2pMonolith.Router.PublicKey().String()
|
2022-09-29 12:06:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:00:42 +00:00
|
|
|
func (m *DendriteMonolith) BaseURL() string {
|
|
|
|
return fmt.Sprintf("http://%s", m.listener.Addr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) PeerCount(peertype int) int {
|
2023-02-01 00:35:21 +00:00
|
|
|
return m.p2pMonolith.Router.PeerCount(peertype)
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SessionCount() int {
|
2023-02-01 00:35:21 +00:00
|
|
|
return len(m.p2pMonolith.Sessions.Protocol("matrix").Sessions())
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:25:57 +00: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
|
|
|
|
}
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Multicast.RegisterNetworkCallback(callback)
|
2022-09-29 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:00:42 +00:00
|
|
|
func (m *DendriteMonolith) SetMulticastEnabled(enabled bool) {
|
|
|
|
if enabled {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Multicast.Start()
|
2021-05-06 11:00:42 +00:00
|
|
|
} else {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Multicast.Stop()
|
2021-11-25 09:46:26 +00:00
|
|
|
m.DisconnectType(int(pineconeRouter.PeerTypeMulticast))
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SetStaticPeer(uri string) {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.ConnManager.RemovePeers()
|
2022-10-03 13:43:38 +00:00
|
|
|
for _, uri := range strings.Split(uri, ",") {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.ConnManager.AddPeer(strings.TrimSpace(uri))
|
2022-10-03 13:43:38 +00:00
|
|
|
}
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:27:53 +00:00
|
|
|
func getServerKeyFromString(nodeID string) (gomatrixserverlib.ServerName, error) {
|
|
|
|
var nodeKey gomatrixserverlib.ServerName
|
|
|
|
if userID, err := gomatrixserverlib.NewUserID(nodeID, false); err == nil {
|
|
|
|
hexKey, decodeErr := hex.DecodeString(string(userID.Domain()))
|
|
|
|
if decodeErr != nil || len(hexKey) != ed25519.PublicKeySize {
|
|
|
|
return "", fmt.Errorf("UserID domain is not a valid ed25519 public key: %v", userID.Domain())
|
|
|
|
} else {
|
|
|
|
nodeKey = userID.Domain()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hexKey, decodeErr := hex.DecodeString(nodeID)
|
|
|
|
if decodeErr != nil || len(hexKey) != ed25519.PublicKeySize {
|
|
|
|
return "", fmt.Errorf("Relay server uri is not a valid ed25519 public key: %v", nodeID)
|
|
|
|
} else {
|
|
|
|
nodeKey = gomatrixserverlib.ServerName(nodeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SetRelayServers(nodeID string, uris string) {
|
|
|
|
relays := []gomatrixserverlib.ServerName{}
|
|
|
|
for _, uri := range strings.Split(uris, ",") {
|
|
|
|
uri = strings.TrimSpace(uri)
|
|
|
|
if len(uri) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey, err := getServerKeyFromString(uri)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
relays = append(relays, nodeKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey, err := getServerKeyFromString(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(nodeKey) == m.PublicKey() {
|
|
|
|
logrus.Infof("Setting own relay servers to: %v", relays)
|
|
|
|
m.relayRetriever.SetRelayServers(relays)
|
|
|
|
} else {
|
2023-01-31 22:51:08 +00:00
|
|
|
relay.UpdateNodeRelayServers(
|
2023-01-28 23:27:53 +00:00
|
|
|
gomatrixserverlib.ServerName(nodeKey),
|
|
|
|
relays,
|
|
|
|
m.baseDendrite.Context(),
|
|
|
|
m.federationAPI,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) GetRelayServers(nodeID string) string {
|
|
|
|
nodeKey, err := getServerKeyFromString(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
relaysString := ""
|
|
|
|
if string(nodeKey) == m.PublicKey() {
|
|
|
|
relays := m.relayRetriever.GetRelayServers()
|
|
|
|
|
|
|
|
for i, relay := range relays {
|
|
|
|
if i != 0 {
|
|
|
|
// Append a comma to the previous entry if there is one.
|
|
|
|
relaysString += ","
|
|
|
|
}
|
|
|
|
relaysString += string(relay)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request := api.P2PQueryRelayServersRequest{Server: gomatrixserverlib.ServerName(nodeKey)}
|
|
|
|
response := api.P2PQueryRelayServersResponse{}
|
|
|
|
err := m.federationAPI.P2PQueryRelayServers(m.baseDendrite.Context(), &request, &response)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed obtaining list of this node's relay servers: %s", err.Error())
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, relay := range response.RelayServers {
|
|
|
|
if i != 0 {
|
|
|
|
// Append a comma to the previous entry if there is one.
|
|
|
|
relaysString += ","
|
|
|
|
}
|
|
|
|
relaysString += string(relay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return relaysString
|
|
|
|
}
|
|
|
|
|
2023-01-29 19:26:16 +00:00
|
|
|
func (m *DendriteMonolith) RelayingEnabled() bool {
|
|
|
|
return m.relayAPI.RelayingEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SetRelayingEnabled(enabled bool) {
|
|
|
|
m.relayAPI.SetRelayingEnabled(enabled)
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:00:42 +00:00
|
|
|
func (m *DendriteMonolith) DisconnectType(peertype int) {
|
2023-02-01 00:35:21 +00:00
|
|
|
for _, p := range m.p2pMonolith.Router.Peers() {
|
2021-11-25 09:46:26 +00:00
|
|
|
if int(peertype) == p.PeerType {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Router.Disconnect(types.SwitchPortID(p.Port), nil)
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) DisconnectZone(zone string) {
|
2023-02-01 00:35:21 +00:00
|
|
|
for _, p := range m.p2pMonolith.Router.Peers() {
|
2021-05-06 11:00:42 +00:00
|
|
|
if zone == p.Zone {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Router.Disconnect(types.SwitchPortID(p.Port), nil)
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:29:30 +00:00
|
|
|
func (m *DendriteMonolith) DisconnectPort(port int) {
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Router.Disconnect(types.SwitchPortID(port), nil)
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 23:10:48 +00:00
|
|
|
func (m *DendriteMonolith) Conduit(zone string, peertype int) (*conduit.Conduit, error) {
|
2021-05-06 11:00:42 +00:00
|
|
|
l, r := net.Pipe()
|
2023-01-31 23:10:48 +00:00
|
|
|
newConduit := conduit.NewConduit(r, 0)
|
2021-05-06 11:00:42 +00:00
|
|
|
go func() {
|
2022-09-29 12:06:55 +00:00
|
|
|
logrus.Errorf("Attempting authenticated connect")
|
2023-01-31 23:10:48 +00:00
|
|
|
var port types.SwitchPortID
|
2022-09-29 12:06:55 +00:00
|
|
|
var err error
|
2023-02-01 00:35:21 +00:00
|
|
|
if port, err = m.p2pMonolith.Router.Connect(
|
2022-09-29 12:06:55 +00:00
|
|
|
l,
|
|
|
|
pineconeRouter.ConnectionZone(zone),
|
|
|
|
pineconeRouter.ConnectionPeerType(peertype),
|
|
|
|
); err != nil {
|
|
|
|
logrus.Errorf("Authenticated connect failed: %s", err)
|
|
|
|
_ = l.Close()
|
|
|
|
_ = r.Close()
|
2023-01-31 23:10:48 +00:00
|
|
|
_ = newConduit.Close()
|
2022-09-29 12:06:55 +00:00
|
|
|
return
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
2023-01-31 23:10:48 +00:00
|
|
|
newConduit.SetPort(port)
|
|
|
|
logrus.Infof("Authenticated connect succeeded (port %d)", newConduit.Port())
|
2021-05-06 11:00:42 +00:00
|
|
|
}()
|
2023-01-31 23:10:48 +00:00
|
|
|
return &newConduit, nil
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) RegisterUser(localpart, password string) (string, error) {
|
2023-02-01 00:35:21 +00:00
|
|
|
pubkey := m.p2pMonolith.Router.PublicKey()
|
2021-05-06 11:00:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) Start() {
|
2022-09-01 16:12:27 +00:00
|
|
|
keyfile := filepath.Join(m.StorageDirectory, "p2p.pem")
|
2023-01-31 23:27:51 +00:00
|
|
|
oldKeyfile := filepath.Join(m.StorageDirectory, "p2p.key")
|
2023-02-01 00:35:21 +00:00
|
|
|
sk, pk := monolith.GetOrCreateKey(keyfile, oldKeyfile)
|
2022-09-12 09:19:02 +00:00
|
|
|
|
2022-09-01 16:12:27 +00:00
|
|
|
var err error
|
2022-05-13 11:06:47 +00:00
|
|
|
m.listener, err = net.Listen("tcp", "localhost:65432")
|
2021-05-06 11:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logger = logrus.Logger{
|
|
|
|
Out: BindLogger{},
|
|
|
|
}
|
|
|
|
m.logger.SetOutput(BindLogger{})
|
|
|
|
logrus.SetOutput(BindLogger{})
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith = monolith.P2PMonolith{}
|
|
|
|
m.p2pMonolith.SetupPinecone(sk)
|
2021-05-06 11:00:42 +00:00
|
|
|
|
|
|
|
prefix := hex.EncodeToString(pk)
|
2023-02-01 00:35:21 +00:00
|
|
|
cfg := monolith.GenerateDefaultConfig(sk, m.StorageDirectory, prefix)
|
2021-05-06 11:00:42 +00:00
|
|
|
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
|
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)
|
2022-10-03 13:35:10 +00:00
|
|
|
cfg.Global.JetStream.InMemory = false
|
2021-05-06 11:00:42 +00:00
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
base := base.NewBaseDendrite(cfg, "Monolith", base.DisableMetrics)
|
|
|
|
m.baseDendrite = base
|
2022-12-01 10:45:15 +00:00
|
|
|
base.ConfigureAdminEndpoints()
|
2021-05-06 11:00:42 +00:00
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
federation := conn.CreateFederationClient(base, m.p2pMonolith.Sessions)
|
2021-05-06 11:00:42 +00:00
|
|
|
|
|
|
|
serverKeyAPI := &signing.YggdrasilKeys{}
|
|
|
|
keyRing := serverKeyAPI.KeyRing()
|
|
|
|
|
2021-11-24 10:45:23 +00:00
|
|
|
rsAPI := roomserver.NewInternalAPI(base)
|
2021-05-06 11:00:42 +00:00
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
m.federationAPI = federationapi.NewInternalAPI(
|
2021-12-13 13:24:49 +00:00
|
|
|
base, federation, rsAPI, base.Caches, keyRing, true,
|
2021-05-06 11:00:42 +00:00
|
|
|
)
|
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
keyAPI := keyserver.NewInternalAPI(base, &base.Cfg.KeyServer, m.federationAPI, rsAPI)
|
2022-05-03 15:35:06 +00:00
|
|
|
m.userAPI = userapi.NewInternalAPI(base, &cfg.UserAPI, cfg.Derived.ApplicationServices, keyAPI, rsAPI, base.PushGatewayHTTPClient())
|
2021-05-06 11:00:42 +00: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
|
2023-01-23 17:55:12 +00:00
|
|
|
rsAPI.SetFederationAPI(m.federationAPI, keyRing)
|
2021-05-06 11:00:42 +00:00
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
userProvider := users.NewPineconeUserProvider(m.p2pMonolith.Router, m.p2pMonolith.Sessions, m.userAPI, federation)
|
|
|
|
roomProvider := rooms.NewPineconeRoomProvider(m.p2pMonolith.Router, m.p2pMonolith.Sessions, m.federationAPI, federation)
|
2023-01-23 17:55:12 +00:00
|
|
|
|
|
|
|
js, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
|
|
|
producer := &producers.SyncAPIProducer{
|
|
|
|
JetStream: js,
|
|
|
|
TopicReceiptEvent: base.Cfg.Global.JetStream.Prefixed(jetstream.OutputReceiptEvent),
|
|
|
|
TopicSendToDeviceEvent: base.Cfg.Global.JetStream.Prefixed(jetstream.OutputSendToDeviceEvent),
|
|
|
|
TopicTypingEvent: base.Cfg.Global.JetStream.Prefixed(jetstream.OutputTypingEvent),
|
|
|
|
TopicPresenceEvent: base.Cfg.Global.JetStream.Prefixed(jetstream.OutputPresenceEvent),
|
|
|
|
TopicDeviceListUpdate: base.Cfg.Global.JetStream.Prefixed(jetstream.InputDeviceListUpdate),
|
|
|
|
TopicSigningKeyUpdate: base.Cfg.Global.JetStream.Prefixed(jetstream.InputSigningKeyUpdate),
|
|
|
|
Config: &base.Cfg.FederationAPI,
|
|
|
|
UserAPI: m.userAPI,
|
|
|
|
}
|
2023-01-29 19:26:16 +00:00
|
|
|
m.relayAPI = relayapi.NewRelayInternalAPI(base, federation, rsAPI, keyRing, producer, false)
|
2022-03-28 15:25:26 +00:00
|
|
|
|
2021-05-06 11:00:42 +00:00
|
|
|
monolith := setup.Monolith{
|
|
|
|
Config: base.Cfg,
|
2023-02-01 00:35:21 +00:00
|
|
|
Client: conn.CreateClient(base, m.p2pMonolith.Sessions),
|
2021-05-06 11:00:42 +00:00
|
|
|
FedClient: federation,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
|
2022-03-28 15:25:26 +00:00
|
|
|
AppserviceAPI: asAPI,
|
2023-01-23 17:55:12 +00:00
|
|
|
FederationAPI: m.federationAPI,
|
2022-03-28 15:25:26 +00:00
|
|
|
RoomserverAPI: rsAPI,
|
|
|
|
UserAPI: m.userAPI,
|
|
|
|
KeyAPI: keyAPI,
|
2023-01-29 19:26:16 +00:00
|
|
|
RelayAPI: m.relayAPI,
|
2022-03-28 15:25:26 +00:00
|
|
|
ExtPublicRoomsProvider: roomProvider,
|
|
|
|
ExtUserDirectoryProvider: userProvider,
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
2022-05-03 16:17:02 +00:00
|
|
|
monolith.AddAllPublicRoutes(base)
|
2021-05-06 11:00:42 +00: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 10:24:17 +00:00
|
|
|
httpRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(base.DendriteAdminMux)
|
|
|
|
httpRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(base.SynapseAdminMux)
|
2023-02-01 00:35:21 +00:00
|
|
|
httpRouter.HandleFunc("/pinecone", m.p2pMonolith.Router.ManholeHandler)
|
2021-05-06 11:00:42 +00:00
|
|
|
|
|
|
|
pMux := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2022-03-28 15:25:26 +00:00
|
|
|
pMux.PathPrefix(users.PublicURL).HandlerFunc(userProvider.FederatedUserProfiles)
|
2021-05-06 11:00:42 +00:00
|
|
|
pMux.PathPrefix(httputil.PublicFederationPathPrefix).Handler(base.PublicFederationAPIMux)
|
|
|
|
pMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(base.PublicMediaAPIMux)
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
pHTTP := m.p2pMonolith.Sessions.Protocol("matrix").HTTP()
|
2022-03-28 15:25:26 +00:00
|
|
|
pHTTP.Mux().Handle(users.PublicURL, pMux)
|
2021-05-06 11:00:42 +00: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 12:13:07 +00:00
|
|
|
|
2021-05-06 11:00:42 +00:00
|
|
|
go func() {
|
|
|
|
m.logger.Info("Listening on ", cfg.Global.ServerName)
|
2022-06-07 09:46:21 +00:00
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
switch m.httpServer.Serve(m.p2pMonolith.Sessions.Protocol("matrix")) {
|
2022-06-07 09:46:21 +00:00
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
m.logger.Info("Stopped listening on ", cfg.Global.ServerName)
|
|
|
|
default:
|
2023-01-23 17:55:12 +00:00
|
|
|
m.logger.Error("Stopped listening on ", cfg.Global.ServerName)
|
2022-06-07 09:46:21 +00:00
|
|
|
}
|
2021-05-06 11:00:42 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
logrus.Info("Listening on ", m.listener.Addr())
|
2022-06-07 09:46:21 +00:00
|
|
|
|
|
|
|
switch http.Serve(m.listener, httpRouter) {
|
|
|
|
case net.ErrClosed, http.ErrServerClosed:
|
|
|
|
m.logger.Info("Stopped listening on ", cfg.Global.ServerName)
|
|
|
|
default:
|
2023-01-23 17:55:12 +00:00
|
|
|
m.logger.Error("Stopped listening on ", cfg.Global.ServerName)
|
2022-06-07 09:46:21 +00:00
|
|
|
}
|
2021-05-06 11:00:42 +00:00
|
|
|
}()
|
2022-11-18 00:29:23 +00:00
|
|
|
|
2023-01-28 23:27:53 +00:00
|
|
|
stopRelayServerSync := make(chan bool)
|
|
|
|
eLog := logrus.WithField("pinecone", "events")
|
2023-01-31 22:51:08 +00:00
|
|
|
m.relayRetriever = relay.NewRelayServerRetriever(
|
|
|
|
context.Background(),
|
2023-02-01 00:35:21 +00:00
|
|
|
gomatrixserverlib.ServerName(m.p2pMonolith.Router.PublicKey().String()),
|
2023-01-31 22:51:08 +00:00
|
|
|
m.federationAPI,
|
|
|
|
monolith.RelayAPI,
|
|
|
|
stopRelayServerSync,
|
|
|
|
)
|
2023-01-28 23:27:53 +00:00
|
|
|
m.relayRetriever.InitializeRelayServers(eLog)
|
|
|
|
|
2022-11-18 00:29:23 +00:00
|
|
|
go func(ch <-chan pineconeEvents.Event) {
|
|
|
|
for event := range ch {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case pineconeEvents.PeerAdded:
|
2023-01-28 23:27:53 +00:00
|
|
|
m.relayRetriever.StartSync()
|
2022-11-18 00:29:23 +00:00
|
|
|
case pineconeEvents.PeerRemoved:
|
2023-02-01 00:35:21 +00:00
|
|
|
if m.relayRetriever.IsRunning() && m.p2pMonolith.Router.TotalPeerCount() == 0 {
|
2023-01-23 17:55:12 +00:00
|
|
|
stopRelayServerSync <- true
|
|
|
|
}
|
2022-11-18 00:29:23 +00:00
|
|
|
case pineconeEvents.BroadcastReceived:
|
2023-01-23 17:55:12 +00:00
|
|
|
// eLog.Info("Broadcast received from: ", e.PeerID)
|
2022-11-18 00:29:23 +00:00
|
|
|
|
|
|
|
req := &api.PerformWakeupServersRequest{
|
|
|
|
ServerNames: []gomatrixserverlib.ServerName{gomatrixserverlib.ServerName(e.PeerID)},
|
|
|
|
}
|
|
|
|
res := &api.PerformWakeupServersResponse{}
|
2023-01-23 17:55:12 +00:00
|
|
|
if err := m.federationAPI.PerformWakeupServers(base.Context(), req, res); err != nil {
|
|
|
|
eLog.WithError(err).Error("Failed to wakeup destination", e.PeerID)
|
2022-11-18 00:29:23 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 00:35:21 +00:00
|
|
|
}(m.p2pMonolith.EventChannel)
|
2021-05-06 11:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) Stop() {
|
2023-01-28 23:27:53 +00:00
|
|
|
_ = m.baseDendrite.Close()
|
2023-01-23 17:55:12 +00:00
|
|
|
m.baseDendrite.WaitForShutdown()
|
2021-05-06 11:00:42 +00:00
|
|
|
_ = m.listener.Close()
|
2023-02-01 00:35:21 +00:00
|
|
|
m.p2pMonolith.Multicast.Stop()
|
|
|
|
_ = m.p2pMonolith.Sessions.Close()
|
|
|
|
_ = m.p2pMonolith.Router.Close()
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|