2022-03-30 15:01:22 +01:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
2021-05-06 12:00:42 +01:00
|
|
|
//
|
|
|
|
// 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-09-23 15:44:21 +01:00
|
|
|
"path/filepath"
|
2022-08-31 10:41:32 +01:00
|
|
|
"strings"
|
2021-05-06 12:00:42 +01:00
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/monolith"
|
2021-05-06 12:00:42 +01:00
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2021-05-06 12:00:42 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2021-05-06 12:00:42 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-02-01 20:41:27 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-06 12:00:42 +01:00
|
|
|
|
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-30 01:13:39 +00:00
|
|
|
instanceName = flag.String("name", "dendrite-p2p-pinecone", "the name of this P2P demo instance")
|
|
|
|
instancePort = flag.Int("port", 8008, "the port that the client API will listen on")
|
|
|
|
instancePeer = flag.String("peer", "", "the static Pinecone peers to connect to, comma separated-list")
|
|
|
|
instanceListen = flag.String("listen", ":0", "the port Pinecone peers can connect to")
|
|
|
|
instanceDir = flag.String("dir", ".", "the directory to store the databases in (if --config not specified)")
|
|
|
|
instanceRelayingEnabled = flag.Bool("relay", false, "whether to enable store & forward relaying for other nodes")
|
2021-05-06 12:00:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
internal.SetupPprof()
|
|
|
|
|
|
|
|
var pk ed25519.PublicKey
|
|
|
|
var sk ed25519.PrivateKey
|
|
|
|
|
2022-08-31 10:41:32 +01:00
|
|
|
// iterate through the cli args and check if the config flag was set
|
|
|
|
configFlagSet := false
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
if arg == "--config" || arg == "-config" {
|
|
|
|
configFlagSet = true
|
|
|
|
break
|
2021-05-06 12:00:42 +01:00
|
|
|
}
|
2022-08-31 10:41:32 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
var cfg *config.Dendrite
|
2022-08-31 10:41:32 +01:00
|
|
|
|
|
|
|
// use custom config if config flag is set
|
|
|
|
if configFlagSet {
|
|
|
|
cfg = setup.ParseFlags(true)
|
|
|
|
sk = cfg.Global.PrivateKey
|
2022-10-03 11:38:31 +01:00
|
|
|
pk = sk.Public().(ed25519.PublicKey)
|
2022-08-31 10:41:32 +01:00
|
|
|
} else {
|
2022-09-23 15:44:21 +01:00
|
|
|
keyfile := filepath.Join(*instanceDir, *instanceName) + ".pem"
|
2023-01-31 23:27:51 +00:00
|
|
|
oldKeyfile := *instanceName + ".key"
|
2023-02-01 00:35:21 +00:00
|
|
|
sk, pk = monolith.GetOrCreateKey(keyfile, oldKeyfile)
|
2023-02-01 20:41:27 +00:00
|
|
|
cfg = monolith.GenerateDefaultConfig(sk, *instanceDir, *instanceDir, *instanceName)
|
2021-05-06 12:00:42 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
cfg.Global.ServerName = spec.ServerName(hex.EncodeToString(pk))
|
2022-08-31 10:41:32 +01:00
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
p2pMonolith := monolith.P2PMonolith{}
|
|
|
|
p2pMonolith.SetupPinecone(sk)
|
|
|
|
p2pMonolith.Multicast.Start()
|
2022-11-18 00:29:23 +00:00
|
|
|
|
2022-04-08 13:55:46 +01:00
|
|
|
if instancePeer != nil && *instancePeer != "" {
|
2022-08-31 10:41:32 +01:00
|
|
|
for _, peer := range strings.Split(*instancePeer, ",") {
|
2023-02-01 00:35:21 +00:00
|
|
|
p2pMonolith.ConnManager.AddPeer(strings.Trim(peer, " \t\r\n"))
|
2022-08-31 10:41:32 +01:00
|
|
|
}
|
2022-04-08 13:55:46 +01:00
|
|
|
}
|
2021-05-06 12:00:42 +01:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
processCtx := process.NewProcessContext()
|
|
|
|
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
|
|
|
routers := httputil.NewRouters()
|
|
|
|
|
2023-02-01 20:41:27 +00:00
|
|
|
enableMetrics := true
|
|
|
|
enableWebsockets := true
|
2023-03-22 08:21:32 +00:00
|
|
|
p2pMonolith.SetupDendrite(processCtx, cfg, cm, routers, *instancePort, *instanceRelayingEnabled, enableMetrics, enableWebsockets)
|
2023-02-01 21:11:48 +00:00
|
|
|
p2pMonolith.StartMonolith()
|
2023-02-01 20:41:27 +00:00
|
|
|
p2pMonolith.WaitForShutdown()
|
|
|
|
|
2021-05-06 12:00:42 +01:00
|
|
|
go func() {
|
|
|
|
listener, err := net.Listen("tcp", *instanceListen)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Listening on", listener.Addr())
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("listener.Accept failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:35:21 +00:00
|
|
|
port, err := p2pMonolith.Router.Connect(
|
2021-11-25 11:18:01 +00:00
|
|
|
conn,
|
|
|
|
pineconeRouter.ConnectionPeerType(pineconeRouter.PeerTypeRemote),
|
|
|
|
)
|
2021-05-06 12:00:42 +01:00
|
|
|
if err != nil {
|
2021-11-25 09:46:26 +00:00
|
|
|
logrus.WithError(err).Error("pSwitch.Connect failed")
|
2021-05-06 12:00:42 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Inbound connection", conn.RemoteAddr(), "is connected to port", port)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|