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/dendrite/userapi"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
) )
type DendriteMonolith struct { type DendriteMonolith struct {
@ -162,32 +161,6 @@ func (m *DendriteMonolith) Start() {
base.UseHTTPAPIs, 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. // Build both ends of a HTTP multiplex.
httpServer := &http.Server{ httpServer := &http.Server{
Addr: ":0", Addr: ":0",
@ -209,6 +182,14 @@ func (m *DendriteMonolith) Start() {
logger.Info("Listening on ", m.BaseURL()) logger.Info("Listening on ", m.BaseURL())
logger.Fatal(httpServer.Serve(m.listener)) 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() { func (m *DendriteMonolith) Stop() {

View file

@ -32,6 +32,7 @@ import (
"github.com/matrix-org/dendrite/eduserver" "github.com/matrix-org/dendrite/eduserver"
"github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/eduserver/cache"
"github.com/matrix-org/dendrite/federationsender" "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"
"github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/config"
"github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/internal/httputil"
@ -174,6 +175,14 @@ func main() {
logrus.Info("Listening on ", httpBindAddr) logrus.Info("Listening on ", httpBindAddr)
logrus.Fatal(http.ListenAndServe(httpBindAddr, base.BaseMux)) 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 {} select {}
} }

View file

@ -1,35 +1,13 @@
package yggconn package yggconn
import ( import (
"context"
"crypto/ed25519"
"encoding/hex"
"fmt"
"net"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/convert"
"github.com/matrix-org/dendrite/internal/setup" "github.com/matrix-org/dendrite/internal/setup"
"github.com/matrix-org/gomatrixserverlib" "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 { type yggroundtripper struct {
inner *http.Transport inner *http.Transport
} }
@ -48,7 +26,7 @@ func (n *Node) CreateClient(
inner: &http.Transport{ inner: &http.Transport{
ResponseHeaderTimeout: 15 * time.Second, ResponseHeaderTimeout: 15 * time.Second,
IdleConnTimeout: 60 * time.Second, IdleConnTimeout: 60 * time.Second,
DialContext: n.yggdialerctx, DialContext: n.DialerContext,
}, },
}, },
) )
@ -64,7 +42,7 @@ func (n *Node) CreateFederationClient(
inner: &http.Transport{ inner: &http.Transport{
ResponseHeaderTimeout: 15 * time.Second, ResponseHeaderTimeout: 15 * time.Second,
IdleConnTimeout: 60 * time.Second, IdleConnTimeout: 60 * time.Second,
DialContext: n.yggdialerctx, DialContext: n.DialerContext,
TLSClientConfig: n.tlsConfig, TLSClientConfig: n.tlsConfig,
}, },
}, },

View file

@ -20,6 +20,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -53,15 +54,10 @@ type Node struct {
incoming chan QUICStream 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) { 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, ":") tokens := strings.Split(address, ":")
raw, err := hex.DecodeString(tokens[0]) raw, err := hex.DecodeString(tokens[0])
if err != nil { if err != nil {
@ -85,7 +81,6 @@ func Setup(instanceName, storageDirectory string) (*Node, error) {
log: gologme.New(os.Stdout, "YGG ", log.Flags()), log: gologme.New(os.Stdout, "YGG ", log.Flags()),
incoming: make(chan QUICStream), incoming: make(chan QUICStream),
} }
//n.core.SetBuildInfo(n)
yggfile := fmt.Sprintf("%s/%s-yggdrasil.conf", storageDirectory, instanceName) yggfile := fmt.Sprintf("%s/%s-yggdrasil.conf", storageDirectory, instanceName)
if _, err := os.Stat(yggfile); !os.IsNotExist(err) { if _, err := os.Stat(yggfile); !os.IsNotExist(err) {

View file

@ -52,8 +52,8 @@ func (n *Node) listenFromYgg() {
return return
} }
if len(session.ConnectionState().PeerCertificates) != 1 { if len(session.ConnectionState().PeerCertificates) != 1 {
session.CloseWithError(0, "expected a peer certificate") _ = session.CloseWithError(0, "expected a peer certificate")
return continue
} }
address := session.ConnectionState().PeerCertificates[0].Subject.CommonName address := session.ConnectionState().PeerCertificates[0].Subject.CommonName
n.log.Infoln("Accepted connection from", address) n.log.Infoln("Accepted connection from", address)