Bare QUIC demo working

This commit is contained in:
Neil Alexander 2020-07-16 13:42:10 +01:00
parent d3939b3d65
commit 8a5c2020b3
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
5 changed files with 25 additions and 62 deletions

View file

@ -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() {

View file

@ -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 {}
}

View file

@ -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,
},
},

View file

@ -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) {

View file

@ -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)