From 8a52f2afdc3e8002948853c05893caad458c05f6 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 25 Jan 2020 13:32:37 -0700 Subject: [PATCH] first cut, untested, for hooking dendrite into go-http-js-libp2p --- build.sh | 2 +- cmd/dendrite-monolith-server/main.go | 43 +++++++++++++++++++++------- common/basecomponent/base.go | 27 +++++++++++++++-- go.mod | 3 +- go.sum | 2 ++ 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/build.sh b/build.sh index 9a8050f3c..326b9e6ab 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/sh -GOBIN=$PWD/`dirname $0`/bin go install -v ./cmd/... +GOOS=js GOARCH=wasm GOBIN=$PWD/`dirname $0`/bin go build -v $PWD/`dirname $0`/cmd/dendrite-monolith-server diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 5ea6b154a..c35251286 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -33,6 +33,8 @@ import ( "github.com/matrix-org/dendrite/typingserver" "github.com/matrix-org/dendrite/typingserver/cache" + "github.com/matrix-org/go-http-js-libp2p/go_http_js_libp2p" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/sirupsen/logrus" ) @@ -80,17 +82,38 @@ func main() { http.Handle("/", httpHandler) // Expose the matrix APIs directly rather than putting them under a /api path. - go func() { - logrus.Info("Listening on ", *httpBindAddr) - logrus.Fatal(http.ListenAndServe(*httpBindAddr, nil)) - }() - // Handle HTTPS if certificate and key are provided - go func() { - if *certFile != "" && *keyFile != "" { - logrus.Info("Listening on ", *httpsBindAddr) - logrus.Fatal(http.ListenAndServeTLS(*httpsBindAddr, *certFile, *keyFile, nil)) + // go func() { + // logrus.Info("Listening on ", *httpBindAddr) + // logrus.Fatal(http.ListenAndServe(*httpBindAddr, nil)) + // }() + + // Expose the matrix APIs via libp2p-js + if base.P2PLocalNode != nil { + go func() { + logrus.Info("Listening on libp2p-js host ID ", base.P2PLocalNode.Id) + + listener := go_http_js_libp2p.NewP2pListener(base.P2PLocalNode) + defer listener.Close() + s := &http.Server{} + s.Serve(listener) + }() + + go func() { + logrus.Info("Listening for service-worker fetch traffic") + + listener := go_http_js_libp2p.NewFetchListener() + s := &http.Server{} + go s.Serve(listener) } - }() + } + + // Handle HTTPS if certificate and key are provided + // go func() { + // if *certFile != "" && *keyFile != "" { + // logrus.Info("Listening on ", *httpsBindAddr) + // logrus.Fatal(http.ListenAndServeTLS(*httpsBindAddr, *certFile, *keyFile, nil)) + // } + // }() // We want to block forever to let the HTTP and HTTPS handler serve the APIs select {} diff --git a/common/basecomponent/base.go b/common/basecomponent/base.go index 50fc2d5c6..3884b90fb 100644 --- a/common/basecomponent/base.go +++ b/common/basecomponent/base.go @@ -27,6 +27,8 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/go-http-js-libp2p/go_http_js_libp2p" + "github.com/gorilla/mux" sarama "gopkg.in/Shopify/sarama.v1" @@ -52,6 +54,8 @@ type BaseDendrite struct { Cfg *config.Dendrite KafkaConsumer sarama.Consumer KafkaProducer sarama.SyncProducer + + P2pLocalNode *go_http_js_libp2p.P2pLocalNode } // NewBaseDendrite creates a new instance to be used by a component. @@ -68,6 +72,12 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string) *BaseDendrite { kafkaConsumer, kafkaProducer := setupKafka(cfg) + var node *go_http_js_libp2p.P2pLocalNode + if cfg.Matrix.ServerName == "p2p-js" { + node = go_http_js_libp2p.NewP2pLocalNode("org.matrix.p2p.experiment") + cfg.Matrix.ServerName = gomatrixserverlib.ServerName(node.Id) + } + return &BaseDendrite{ componentName: componentName, tracerCloser: closer, @@ -75,6 +85,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string) *BaseDendrite { APIMux: mux.NewRouter().UseEncodedPath(), KafkaConsumer: kafkaConsumer, KafkaProducer: kafkaProducer, + P2pLocalNode: node, } } @@ -150,9 +161,19 @@ func (b *BaseDendrite) CreateKeyDB() keydb.Database { // CreateFederationClient creates a new federation client. Should only be called // once per component. func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient { - return gomatrixserverlib.NewFederationClient( - b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey, - ) + if b.P2PLocalNode != nil { + fmt.Println("Running in js-libp2p federation mode") + fmt.Println("Warning: Federation with non-libp2p homeservers will not work in this mode yet!") + tr := go_http_js_libp2p.NewP2pTransport(b.P2PLocalNode) + return gomatrixserverlib.NewFederationClientWithTransport( + b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey, tr, + ) + } else { + fmt.Println("Running in regular federation mode") + return gomatrixserverlib.NewFederationClient( + b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey, + ) + } } // SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on diff --git a/go.mod b/go.mod index d2cb80bb8..6c160044e 100644 --- a/go.mod +++ b/go.mod @@ -23,13 +23,12 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/lib/pq v0.0.0-20170918175043-23da1db4f16d github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5 + github.com/matrix-org/go-http-js-libp2p v0.0.0-20200125063821-6eb06b102bda github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 github.com/matrix-org/gomatrixserverlib v0.0.0-20190814163046-d6285a18401f github.com/matrix-org/naffka v0.0.0-20171115094957-662bfd0841d0 github.com/matrix-org/util v0.0.0-20171127121716-2e2df66af2f5 github.com/matttproud/golang_protobuf_extensions v1.0.1 - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 github.com/nicksnyder/go-i18n v1.8.1 github.com/opentracing/opentracing-go v0.0.0-20170806192116-8ebe5d4e236e diff --git a/go.sum b/go.sum index 962d0f5bb..10b61df96 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/lib/pq v0.0.0-20170918175043-23da1db4f16d h1:Hdtccv31GWxWoCzWsIhZXy5N github.com/lib/pq v0.0.0-20170918175043-23da1db4f16d/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5 h1:nMX2t7hbGF0NYDYySx0pCqEKGKAeZIiSqlWSspetlhY= github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5/go.mod h1:NgPCr+UavRGH6n5jmdX8DuqFZ4JiCWIJoZiuhTRLSUg= +github.com/matrix-org/go-http-js-libp2p v0.0.0-20200125063821-6eb06b102bda h1:e8QGrSxjAaQ2W5ak33taR2yhQAVBQzjrKJ2zW9/jOIw= +github.com/matrix-org/go-http-js-libp2p v0.0.0-20200125063821-6eb06b102bda/go.mod h1:3WluEZ9QXSwU30tWYqktnpC1x9mwZKx1r8uAv8Iq+a4= github.com/matrix-org/gomatrix v0.0.0-20171003113848-a7fc80c8060c h1:aZap604NyBGhAUE0CyNHz6+Pryye5A5mHnYyO4KPPW8= github.com/matrix-org/gomatrix v0.0.0-20171003113848-a7fc80c8060c/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0= github.com/matrix-org/gomatrix v0.0.0-20190130130140-385f072fe9af h1:piaIBNQGIHnni27xRB7VKkEwoWCgAmeuYf8pxAyG0bI=