From 8a5c2020b3a4b705b5d5686a9e71990a49e6d471 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 16 Jul 2020 13:42:10 +0100 Subject: [PATCH] Bare QUIC demo working --- build/gobind/monolith.go | 35 +++++-------------- cmd/dendrite-demo-yggdrasil/main.go | 9 +++++ cmd/dendrite-demo-yggdrasil/yggconn/client.go | 26 ++------------ cmd/dendrite-demo-yggdrasil/yggconn/node.go | 13 +++---- .../yggconn/session.go | 4 +-- 5 files changed, 25 insertions(+), 62 deletions(-) diff --git a/build/gobind/monolith.go b/build/gobind/monolith.go index d220fabc..b4095063 100644 --- a/build/gobind/monolith.go +++ b/build/gobind/monolith.go @@ -24,7 +24,6 @@ import ( "github.com/matrix-org/dendrite/userapi" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" - "github.com/yggdrasil-network/yggdrasil-go/src/crypto" ) type DendriteMonolith struct { @@ -162,32 +161,6 @@ func (m *DendriteMonolith) Start() { base.UseHTTPAPIs, ) - ygg.NotifySessionNew(func(boxPubKey crypto.BoxPubKey) { - serv := gomatrixserverlib.ServerName(boxPubKey.String()) - req := &api.PerformServersAliveRequest{ - Servers: []gomatrixserverlib.ServerName{serv}, - } - res := &api.PerformServersAliveResponse{} - if err := fsAPI.PerformServersAlive(context.TODO(), req, res); err != nil { - logrus.WithError(err).Warnf("Failed to notify server %q alive due to new session", serv) - } else { - logrus.Infof("Notified server %q alive due to new session", serv) - } - }) - - ygg.NotifyLinkNew(func(boxPubKey crypto.BoxPubKey, linkType, remote string) { - serv := gomatrixserverlib.ServerName(boxPubKey.String()) - req := &api.PerformServersAliveRequest{ - Servers: []gomatrixserverlib.ServerName{serv}, - } - res := &api.PerformServersAliveResponse{} - if err := fsAPI.PerformServersAlive(context.TODO(), req, res); err != nil { - logrus.WithError(err).Warnf("Failed to notify server %q alive due to new peer", serv) - } else { - logrus.Infof("Notified server %q alive due to new peer", serv) - } - }) - // Build both ends of a HTTP multiplex. httpServer := &http.Server{ Addr: ":0", @@ -209,6 +182,14 @@ func (m *DendriteMonolith) Start() { logger.Info("Listening on ", m.BaseURL()) logger.Fatal(httpServer.Serve(m.listener)) }() + go func() { + logrus.Info("Sending wake-up message to known nodes") + req := &api.PerformBroadcastEDURequest{} + res := &api.PerformBroadcastEDUResponse{} + if err := fsAPI.PerformBroadcastEDU(context.TODO(), req, res); err != nil { + logrus.WithError(err).Error("Failed to send wake-up message to known nodes") + } + }() } func (m *DendriteMonolith) Stop() { diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index 55ae590c..87042a59 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -32,6 +32,7 @@ import ( "github.com/matrix-org/dendrite/eduserver" "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/federationsender" + "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/httputil" @@ -174,6 +175,14 @@ func main() { logrus.Info("Listening on ", httpBindAddr) logrus.Fatal(http.ListenAndServe(httpBindAddr, base.BaseMux)) }() + go func() { + logrus.Info("Sending wake-up message to known nodes") + req := &api.PerformBroadcastEDURequest{} + res := &api.PerformBroadcastEDUResponse{} + if err := fsAPI.PerformBroadcastEDU(context.TODO(), req, res); err != nil { + logrus.WithError(err).Error("Failed to send wake-up message to known nodes") + } + }() select {} } diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/client.go b/cmd/dendrite-demo-yggdrasil/yggconn/client.go index 02b93e0d..ad610c96 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/client.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/client.go @@ -1,35 +1,13 @@ package yggconn import ( - "context" - "crypto/ed25519" - "encoding/hex" - "fmt" - "net" "net/http" - "strings" "time" - "github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/convert" "github.com/matrix-org/dendrite/internal/setup" "github.com/matrix-org/gomatrixserverlib" ) -func (n *Node) yggdialer(_, address string) (net.Conn, error) { - tokens := strings.Split(address, ":") - raw, err := hex.DecodeString(tokens[0]) - if err != nil { - return nil, fmt.Errorf("hex.DecodeString: %w", err) - } - converted := convert.Ed25519PublicKeyToCurve25519(ed25519.PublicKey(raw)) - convhex := hex.EncodeToString(converted) - return n.Dial("curve25519", convhex) -} - -func (n *Node) yggdialerctx(ctx context.Context, network, address string) (net.Conn, error) { - return n.yggdialer(network, address) -} - type yggroundtripper struct { inner *http.Transport } @@ -48,7 +26,7 @@ func (n *Node) CreateClient( inner: &http.Transport{ ResponseHeaderTimeout: 15 * time.Second, IdleConnTimeout: 60 * time.Second, - DialContext: n.yggdialerctx, + DialContext: n.DialerContext, }, }, ) @@ -64,7 +42,7 @@ func (n *Node) CreateFederationClient( inner: &http.Transport{ ResponseHeaderTimeout: 15 * time.Second, IdleConnTimeout: 60 * time.Second, - DialContext: n.yggdialerctx, + DialContext: n.DialerContext, TLSClientConfig: n.tlsConfig, }, }, diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/node.go b/cmd/dendrite-demo-yggdrasil/yggconn/node.go index e281d8b2..795245b6 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/node.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/node.go @@ -20,6 +20,7 @@ import ( "crypto/tls" "encoding/hex" "encoding/json" + "errors" "fmt" "io/ioutil" "log" @@ -53,15 +54,10 @@ type Node struct { incoming chan QUICStream } -func (n *Node) BuildName() string { - return "dendrite" -} - -func (n *Node) BuildVersion() string { - return "dev" -} - func (n *Node) Dialer(_, address string) (net.Conn, error) { + if len(n.core.GetSwitchPeers()) == 0 { + return nil, errors.New("no peer connections available") + } tokens := strings.Split(address, ":") raw, err := hex.DecodeString(tokens[0]) if err != nil { @@ -85,7 +81,6 @@ func Setup(instanceName, storageDirectory string) (*Node, error) { log: gologme.New(os.Stdout, "YGG ", log.Flags()), incoming: make(chan QUICStream), } - //n.core.SetBuildInfo(n) yggfile := fmt.Sprintf("%s/%s-yggdrasil.conf", storageDirectory, instanceName) if _, err := os.Stat(yggfile); !os.IsNotExist(err) { diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/session.go b/cmd/dendrite-demo-yggdrasil/yggconn/session.go index 0b537584..0d231f6d 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/session.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/session.go @@ -52,8 +52,8 @@ func (n *Node) listenFromYgg() { return } if len(session.ConnectionState().PeerCertificates) != 1 { - session.CloseWithError(0, "expected a peer certificate") - return + _ = session.CloseWithError(0, "expected a peer certificate") + continue } address := session.ConnectionState().PeerCertificates[0].Subject.CommonName n.log.Infoln("Accepted connection from", address)