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"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
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
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/userutil"
|
2023-01-31 17:10:48 -06:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conduit"
|
2023-01-31 18:35:21 -06:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/monolith"
|
2023-01-31 16:51:08 -06:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/relay"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
2022-11-17 18:29:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2021-05-06 06:00:42 -05:00
|
|
|
userapiAPI "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-01-31 18:35:21 -06:00
|
|
|
"github.com/matrix-org/pinecone/types"
|
2021-05-06 06:00:42 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
pineconeMulticast "github.com/matrix-org/pinecone/multicast"
|
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
2021-05-25 03:49:02 -05:00
|
|
|
|
|
|
|
_ "golang.org/x/mobile/bind"
|
2021-05-06 06:00:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-31 16:51:08 -06:00
|
|
|
PeerTypeRemote = pineconeRouter.PeerTypeRemote
|
|
|
|
PeerTypeMulticast = pineconeRouter.PeerTypeMulticast
|
|
|
|
PeerTypeBluetooth = pineconeRouter.PeerTypeBluetooth
|
|
|
|
PeerTypeBonjour = pineconeRouter.PeerTypeBonjour
|
2021-05-06 06:00:42 -05:00
|
|
|
)
|
|
|
|
|
2023-02-01 14:41:27 -06:00
|
|
|
// Re-export Conduit in this package for bindings.
|
|
|
|
type Conduit struct {
|
|
|
|
conduit.Conduit
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
type DendriteMonolith struct {
|
2023-01-31 18:35:21 -06:00
|
|
|
logger logrus.Logger
|
|
|
|
p2pMonolith monolith.P2PMonolith
|
|
|
|
StorageDirectory string
|
2023-02-01 14:41:27 -06:00
|
|
|
CacheDirectory string
|
2023-01-31 18:35:21 -06:00
|
|
|
listener net.Listener
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:06:55 -05:00
|
|
|
func (m *DendriteMonolith) PublicKey() string {
|
2023-01-31 18:35:21 -06:00
|
|
|
return m.p2pMonolith.Router.PublicKey().String()
|
2022-09-29 07:06:55 -05:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
func (m *DendriteMonolith) BaseURL() string {
|
2023-02-01 14:41:27 -06:00
|
|
|
return fmt.Sprintf("http://%s", m.p2pMonolith.Addr())
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) PeerCount(peertype int) int {
|
2023-01-31 18:35:21 -06:00
|
|
|
return m.p2pMonolith.Router.PeerCount(peertype)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SessionCount() int {
|
2023-02-01 14:41:27 -06:00
|
|
|
return len(m.p2pMonolith.Sessions.Protocol(monolith.SessionProtocol).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
|
|
|
|
}
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Multicast.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 {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Multicast.Start()
|
2021-05-06 06:00:42 -05:00
|
|
|
} else {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Multicast.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) {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.ConnManager.RemovePeers()
|
2022-10-03 08:43:38 -05:00
|
|
|
for _, uri := range strings.Split(uri, ",") {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.ConnManager.AddPeer(strings.TrimSpace(uri))
|
2022-10-03 08:43:38 -05:00
|
|
|
}
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2023-01-28 17:27:53 -06: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)
|
2023-02-01 14:41:27 -06:00
|
|
|
m.p2pMonolith.RelayRetriever.SetRelayServers(relays)
|
2023-01-28 17:27:53 -06:00
|
|
|
} else {
|
2023-01-31 16:51:08 -06:00
|
|
|
relay.UpdateNodeRelayServers(
|
2023-01-28 17:27:53 -06:00
|
|
|
gomatrixserverlib.ServerName(nodeKey),
|
|
|
|
relays,
|
2023-02-01 14:41:27 -06:00
|
|
|
m.p2pMonolith.BaseDendrite.Context(),
|
|
|
|
m.p2pMonolith.GetFederationAPI(),
|
2023-01-28 17:27:53 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2023-02-01 14:41:27 -06:00
|
|
|
relays := m.p2pMonolith.RelayRetriever.GetRelayServers()
|
2023-01-28 17:27:53 -06:00
|
|
|
|
|
|
|
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{}
|
2023-02-01 14:41:27 -06:00
|
|
|
err := m.p2pMonolith.GetFederationAPI().P2PQueryRelayServers(m.p2pMonolith.BaseDendrite.Context(), &request, &response)
|
2023-01-28 17:27:53 -06:00
|
|
|
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 13:26:16 -06:00
|
|
|
func (m *DendriteMonolith) RelayingEnabled() bool {
|
2023-02-01 14:41:27 -06:00
|
|
|
return m.p2pMonolith.GetRelayAPI().RelayingEnabled()
|
2023-01-29 13:26:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) SetRelayingEnabled(enabled bool) {
|
2023-02-01 14:41:27 -06:00
|
|
|
m.p2pMonolith.GetRelayAPI().SetRelayingEnabled(enabled)
|
2023-01-29 13:26:16 -06:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
func (m *DendriteMonolith) DisconnectType(peertype int) {
|
2023-01-31 18:35:21 -06:00
|
|
|
for _, p := range m.p2pMonolith.Router.Peers() {
|
2021-11-25 03:46:26 -06:00
|
|
|
if int(peertype) == p.PeerType {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Router.Disconnect(types.SwitchPortID(p.Port), nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) DisconnectZone(zone string) {
|
2023-01-31 18:35:21 -06:00
|
|
|
for _, p := range m.p2pMonolith.Router.Peers() {
|
2021-05-06 06:00:42 -05:00
|
|
|
if zone == p.Zone {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Router.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) {
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith.Router.Disconnect(types.SwitchPortID(port), nil)
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:41:27 -06:00
|
|
|
func (m *DendriteMonolith) Conduit(zone string, peertype int) (*Conduit, error) {
|
2021-05-06 06:00:42 -05:00
|
|
|
l, r := net.Pipe()
|
2023-02-01 14:41:27 -06:00
|
|
|
newConduit := Conduit{conduit.NewConduit(r, 0)}
|
2021-05-06 06:00:42 -05:00
|
|
|
go func() {
|
2022-09-29 07:06:55 -05:00
|
|
|
logrus.Errorf("Attempting authenticated connect")
|
2023-01-31 17:10:48 -06:00
|
|
|
var port types.SwitchPortID
|
2022-09-29 07:06:55 -05:00
|
|
|
var err error
|
2023-01-31 18:35:21 -06:00
|
|
|
if port, err = m.p2pMonolith.Router.Connect(
|
2022-09-29 07:06:55 -05:00
|
|
|
l,
|
|
|
|
pineconeRouter.ConnectionZone(zone),
|
|
|
|
pineconeRouter.ConnectionPeerType(peertype),
|
|
|
|
); err != nil {
|
|
|
|
logrus.Errorf("Authenticated connect failed: %s", err)
|
|
|
|
_ = l.Close()
|
|
|
|
_ = r.Close()
|
2023-01-31 17:10:48 -06:00
|
|
|
_ = newConduit.Close()
|
2022-09-29 07:06:55 -05:00
|
|
|
return
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
2023-01-31 17:10:48 -06:00
|
|
|
newConduit.SetPort(port)
|
|
|
|
logrus.Infof("Authenticated connect succeeded (port %d)", newConduit.Port())
|
2021-05-06 06:00:42 -05:00
|
|
|
}()
|
2023-01-31 17:10:48 -06:00
|
|
|
return &newConduit, nil
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) RegisterUser(localpart, password string) (string, error) {
|
2023-01-31 18:35:21 -06:00
|
|
|
pubkey := m.p2pMonolith.Router.PublicKey()
|
2021-05-06 06:00:42 -05:00
|
|
|
userID := userutil.MakeUserID(
|
|
|
|
localpart,
|
|
|
|
gomatrixserverlib.ServerName(hex.EncodeToString(pubkey[:])),
|
|
|
|
)
|
|
|
|
userReq := &userapiAPI.PerformAccountCreationRequest{
|
|
|
|
AccountType: userapiAPI.AccountTypeUser,
|
|
|
|
Localpart: localpart,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
userRes := &userapiAPI.PerformAccountCreationResponse{}
|
2023-02-01 14:41:27 -06:00
|
|
|
if err := m.p2pMonolith.GetUserAPI().PerformAccountCreation(context.Background(), userReq, userRes); err != nil {
|
2021-05-06 06:00:42 -05:00
|
|
|
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{}
|
2023-02-01 14:41:27 -06:00
|
|
|
if err := m.p2pMonolith.GetUserAPI().PerformDeviceCreation(context.Background(), loginReq, loginRes); err != nil {
|
2021-05-06 06:00:42 -05:00
|
|
|
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 11:12:27 -05:00
|
|
|
keyfile := filepath.Join(m.StorageDirectory, "p2p.pem")
|
2023-01-31 17:27:51 -06:00
|
|
|
oldKeyfile := filepath.Join(m.StorageDirectory, "p2p.key")
|
2023-01-31 18:35:21 -06:00
|
|
|
sk, pk := monolith.GetOrCreateKey(keyfile, oldKeyfile)
|
2022-09-12 04:19:02 -05:00
|
|
|
|
2021-05-06 06:00:42 -05:00
|
|
|
m.logger = logrus.Logger{
|
|
|
|
Out: BindLogger{},
|
|
|
|
}
|
|
|
|
m.logger.SetOutput(BindLogger{})
|
|
|
|
logrus.SetOutput(BindLogger{})
|
|
|
|
|
2023-01-31 18:35:21 -06:00
|
|
|
m.p2pMonolith = monolith.P2PMonolith{}
|
|
|
|
m.p2pMonolith.SetupPinecone(sk)
|
2021-05-06 06:00:42 -05:00
|
|
|
|
|
|
|
prefix := hex.EncodeToString(pk)
|
2023-02-01 14:41:27 -06:00
|
|
|
cfg := monolith.GenerateDefaultConfig(sk, m.StorageDirectory, m.CacheDirectory, prefix)
|
2021-05-06 06:00:42 -05:00
|
|
|
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
|
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)
|
2022-10-03 08:35:10 -05:00
|
|
|
cfg.Global.JetStream.InMemory = false
|
2021-05-06 06:00:42 -05:00
|
|
|
|
2023-02-01 14:41:27 -06:00
|
|
|
enableRelaying := false
|
2023-02-01 15:11:48 -06:00
|
|
|
enableMetrics := false
|
|
|
|
enableWebsockets := false
|
2023-02-01 14:41:27 -06:00
|
|
|
m.p2pMonolith.SetupDendrite(cfg, 65432, enableRelaying, enableMetrics, enableWebsockets)
|
2023-02-01 15:11:48 -06:00
|
|
|
m.p2pMonolith.StartMonolith()
|
2021-05-06 06:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DendriteMonolith) Stop() {
|
2023-02-01 14:41:27 -06:00
|
|
|
m.p2pMonolith.Stop()
|
2023-01-23 11:55:12 -06:00
|
|
|
}
|