Fix media API for demos and possibly Synapse (#1134)

* Fix media API for demos and possibly Synapse

* User API

* goimports
main
Neil Alexander 2020-06-16 14:29:11 +01:00 committed by GitHub
parent 9c77022513
commit fc0e74ae0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 71 additions and 25 deletions

View File

@ -80,6 +80,17 @@ func createFederationClient(
)
}
func createClient(
base *P2PDendrite,
) *gomatrixserverlib.Client {
tr := &http.Transport{}
tr.RegisterProtocol(
"matrix",
p2phttp.NewTransport(base.LibP2P, p2phttp.ProtocolOption("/matrix")),
)
return gomatrixserverlib.NewClientWithTransport(tr)
}
func main() {
instanceName := flag.String("name", "dendrite-p2p", "the name of this P2P demo instance")
instancePort := flag.Int("port", 8080, "the port that the client API will listen on")
@ -102,6 +113,7 @@ func main() {
}
cfg := config.Dendrite{}
cfg.SetDefaults()
cfg.Matrix.ServerName = "p2p"
cfg.Matrix.PrivateKey = privKey
cfg.Matrix.KeyID = gomatrixserverlib.KeyID(fmt.Sprintf("ed25519:%s", *instanceName))
@ -159,6 +171,7 @@ func main() {
Config: base.Base.Cfg,
AccountDB: accountDB,
DeviceDB: deviceDB,
Client: createClient(base),
FedClient: federation,
KeyRing: keyRing,
KafkaConsumer: base.Base.KafkaConsumer,

View File

@ -16,18 +16,14 @@ package main
import (
"context"
"crypto/ed25519"
"crypto/tls"
"encoding/hex"
"flag"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/matrix-org/dendrite/appservice"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/convert"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/embed"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/yggconn"
@ -63,26 +59,13 @@ func (y *yggroundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
func createFederationClient(
base *setup.BaseDendrite, n *yggconn.Node,
) *gomatrixserverlib.FederationClient {
yggdialer := func(_, 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)
}
yggdialerctx := func(ctx context.Context, network, address string) (net.Conn, error) {
return yggdialer(network, address)
}
tr := &http.Transport{}
tr.RegisterProtocol(
"matrix", &yggroundtripper{
inner: &http.Transport{
ResponseHeaderTimeout: 15 * time.Second,
IdleConnTimeout: 60 * time.Second,
DialContext: yggdialerctx,
DialContext: n.DialerContext,
},
},
)
@ -91,6 +74,20 @@ func createFederationClient(
)
}
func createClient(n *yggconn.Node) *gomatrixserverlib.Client {
tr := &http.Transport{}
tr.RegisterProtocol(
"matrix", &yggroundtripper{
inner: &http.Transport{
ResponseHeaderTimeout: 15 * time.Second,
IdleConnTimeout: 60 * time.Second,
DialContext: n.DialerContext,
},
},
)
return gomatrixserverlib.NewClientWithTransport(tr)
}
// nolint:gocyclo
func main() {
flag.Parse()
@ -162,6 +159,7 @@ func main() {
Config: base.Cfg,
AccountDB: accountDB,
DeviceDB: deviceDB,
Client: createClient(ygg),
FedClient: federation,
KeyRing: keyRing,
KafkaConsumer: base.KafkaConsumer,

View File

@ -15,13 +15,16 @@
package yggconn
import (
"context"
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/convert"
@ -48,6 +51,21 @@ type Node struct {
incoming chan *yamux.Stream
}
func (n *Node) Dialer(_, 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) DialerContext(ctx context.Context, network, address string) (net.Conn, error) {
return n.Dialer(network, address)
}
// nolint:gocyclo
func Setup(instanceName, instancePeer string) (*Node, error) {
n := &Node{

View File

@ -17,6 +17,7 @@ package main
import (
"github.com/matrix-org/dendrite/internal/setup"
"github.com/matrix-org/dendrite/mediaapi"
"github.com/matrix-org/gomatrixserverlib"
)
func main() {
@ -25,8 +26,9 @@ func main() {
defer base.Close() // nolint: errcheck
userAPI := base.UserAPIClient()
client := gomatrixserverlib.NewClient()
mediaapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, userAPI)
mediaapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, userAPI, client)
base.SetupAndServeHTTP(string(base.Cfg.Bind.MediaAPI), string(base.Cfg.Listen.MediaAPI))

View File

@ -31,6 +31,7 @@ import (
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/serverkeyapi"
"github.com/matrix-org/dendrite/userapi"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
)
@ -126,6 +127,7 @@ func main() {
Config: base.Cfg,
AccountDB: accountDB,
DeviceDB: deviceDB,
Client: gomatrixserverlib.NewClient(),
FedClient: federation,
KeyRing: keyRing,
KafkaConsumer: base.KafkaConsumer,

View File

@ -145,6 +145,11 @@ func createFederationClient(cfg *config.Dendrite, node *go_http_js_libp2p.P2pLoc
return fed
}
func createClient(node *go_http_js_libp2p.P2pLocalNode) *gomatrixserverlib.Client {
tr := go_http_js_libp2p.NewP2pTransport(node)
return gomatrixserverlib.NewClientWithTransport(tr)
}
func createP2PNode(privKey ed25519.PrivateKey) (serverName string, node *go_http_js_libp2p.P2pLocalNode) {
hosted := "/dns4/rendezvous.matrix.org/tcp/8443/wss/p2p-websocket-star/"
node = go_http_js_libp2p.NewP2pLocalNode("org.matrix.p2p.experiment", privKey.Seed(), []string{hosted}, "p2p")
@ -218,6 +223,7 @@ func main() {
Config: base.Cfg,
AccountDB: accountDB,
DeviceDB: deviceDB,
Client: createClient(node),
FedClient: federation,
KeyRing: &keyRing,
KafkaConsumer: base.KafkaConsumer,

View File

@ -45,6 +45,7 @@ type Monolith struct {
DeviceDB devices.Database
AccountDB accounts.Database
KeyRing *gomatrixserverlib.KeyRing
Client *gomatrixserverlib.Client
FedClient *gomatrixserverlib.FederationClient
KafkaConsumer sarama.Consumer
KafkaProducer sarama.SyncProducer
@ -80,7 +81,7 @@ func (m *Monolith) AddAllPublicRoutes(publicMux *mux.Router) {
m.KeyRing, m.RoomserverAPI, m.AppserviceAPI, m.FederationSenderAPI,
m.EDUInternalAPI,
)
mediaapi.AddPublicRoutes(publicMux, m.Config, m.UserAPI)
mediaapi.AddPublicRoutes(publicMux, m.Config, m.UserAPI, m.Client)
publicroomsapi.AddPublicRoutes(
publicMux, m.Config, m.KafkaConsumer, m.UserAPI, m.PublicRoomsDB, m.RoomserverAPI, m.FedClient,
m.ExtPublicRoomsProvider,

View File

@ -26,7 +26,9 @@ import (
// AddPublicRoutes sets up and registers HTTP handlers for the MediaAPI component.
func AddPublicRoutes(
router *mux.Router, cfg *config.Dendrite, userAPI userapi.UserInternalAPI,
router *mux.Router, cfg *config.Dendrite,
userAPI userapi.UserInternalAPI,
client *gomatrixserverlib.Client,
) {
mediaDB, err := storage.Open(string(cfg.Database.MediaAPI), cfg.DbProperties())
if err != nil {
@ -34,6 +36,6 @@ func AddPublicRoutes(
}
routing.Setup(
router, cfg, mediaDB, userAPI, gomatrixserverlib.NewClient(),
router, cfg, mediaDB, userAPI, client,
)
}

View File

@ -32,6 +32,7 @@ import (
)
const pathPrefixR0 = "/media/r0"
const pathPrefixV1 = "/media/v1" // TODO: remove when synapse is fixed
// Setup registers the media API HTTP handlers
//
@ -46,6 +47,7 @@ func Setup(
client *gomatrixserverlib.Client,
) {
r0mux := publicAPIMux.PathPrefix(pathPrefixR0).Subrouter()
v1mux := publicAPIMux.PathPrefix(pathPrefixV1).Subrouter()
activeThumbnailGeneration := &types.ActiveThumbnailGeneration{
PathToResult: map[string]*types.ThumbnailGenerationResult{},
@ -60,9 +62,11 @@ func Setup(
activeRemoteRequests := &types.ActiveRemoteRequests{
MXCToResult: map[string]*types.RemoteRequestResult{},
}
r0mux.Handle("/download/{serverName}/{mediaId}",
makeDownloadAPI("download", cfg, db, client, activeRemoteRequests, activeThumbnailGeneration),
).Methods(http.MethodGet, http.MethodOptions)
downloadHandler := makeDownloadAPI("download", cfg, db, client, activeRemoteRequests, activeThumbnailGeneration)
r0mux.Handle("/download/{serverName}/{mediaId}", downloadHandler).Methods(http.MethodGet, http.MethodOptions)
v1mux.Handle("/download/{serverName}/{mediaId}", downloadHandler).Methods(http.MethodGet, http.MethodOptions) // TODO: remove when synapse is fixed
r0mux.Handle("/thumbnail/{serverName}/{mediaId}",
makeDownloadAPI("thumbnail", cfg, db, client, activeRemoteRequests, activeThumbnailGeneration),
).Methods(http.MethodGet, http.MethodOptions)