2021-06-25 13:20:36 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-10-08 14:33:03 +00:00
|
|
|
//go:build wasm
|
2021-06-25 13:20:36 +00:00
|
|
|
// +build wasm
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
2021-06-25 14:59:59 +00:00
|
|
|
"encoding/hex"
|
2021-06-25 13:20:36 +00:00
|
|
|
"fmt"
|
|
|
|
"syscall/js"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/appservice"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/conn"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/rooms"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
2021-11-24 10:45:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi"
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2021-06-25 13:20:36 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver"
|
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2021-11-24 10:45:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2021-06-25 13:20:36 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/userapi"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
_ "github.com/matrix-org/go-sqlite3-js"
|
|
|
|
|
2022-04-08 12:55:46 +00:00
|
|
|
pineconeConnections "github.com/matrix-org/pinecone/connections"
|
2021-06-25 13:20:36 +00:00
|
|
|
pineconeRouter "github.com/matrix-org/pinecone/router"
|
|
|
|
pineconeSessions "github.com/matrix-org/pinecone/sessions"
|
|
|
|
)
|
|
|
|
|
|
|
|
var GitCommit string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fmt.Printf("[%s] dendrite.js starting...\n", GitCommit)
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicPeer = "wss://pinecone.matrix.org/public"
|
|
|
|
const keyNameEd25519 = "_go_ed25519_key"
|
|
|
|
|
|
|
|
func readKeyFromLocalStorage() (key ed25519.PrivateKey, err error) {
|
|
|
|
localforage := js.Global().Get("localforage")
|
|
|
|
if !localforage.Truthy() {
|
|
|
|
err = fmt.Errorf("readKeyFromLocalStorage: no localforage")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// https://localforage.github.io/localForage/
|
|
|
|
item, ok := await(localforage.Call("getItem", keyNameEd25519))
|
|
|
|
if !ok || !item.Truthy() {
|
|
|
|
err = fmt.Errorf("readKeyFromLocalStorage: no key in localforage")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Found key in localforage")
|
|
|
|
// extract []byte and make an ed25519 key
|
|
|
|
seed := make([]byte, 32, 32)
|
|
|
|
js.CopyBytesToGo(seed, item)
|
|
|
|
|
|
|
|
return ed25519.NewKeyFromSeed(seed), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeKeyToLocalStorage(key ed25519.PrivateKey) error {
|
|
|
|
localforage := js.Global().Get("localforage")
|
|
|
|
if !localforage.Truthy() {
|
|
|
|
return fmt.Errorf("writeKeyToLocalStorage: no localforage")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a Uint8Array from the key's seed
|
|
|
|
seed := key.Seed()
|
|
|
|
jsSeed := js.Global().Get("Uint8Array").New(len(seed))
|
|
|
|
js.CopyBytesToJS(jsSeed, seed)
|
|
|
|
// write it
|
|
|
|
localforage.Call("setItem", keyNameEd25519, jsSeed)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// taken from https://go-review.googlesource.com/c/go/+/150917
|
|
|
|
|
|
|
|
// await waits until the promise v has been resolved or rejected and returns the promise's result value.
|
|
|
|
// The boolean value ok is true if the promise has been resolved, false if it has been rejected.
|
|
|
|
// If v is not a promise, v itself is returned as the value and ok is true.
|
|
|
|
func await(v js.Value) (result js.Value, ok bool) {
|
|
|
|
if v.Type() != js.TypeObject || v.Get("then").Type() != js.TypeFunction {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
|
|
onResolve := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
result = args[0]
|
|
|
|
ok = true
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer onResolve.Release()
|
|
|
|
onReject := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
result = args[0]
|
|
|
|
ok = false
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer onReject.Release()
|
|
|
|
v.Call("then", onResolve, onReject)
|
|
|
|
<-done
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateKey() ed25519.PrivateKey {
|
|
|
|
// attempt to look for a seed in JS-land and if it exists use it.
|
|
|
|
priv, err := readKeyFromLocalStorage()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Read key from localStorage")
|
|
|
|
return priv
|
|
|
|
}
|
|
|
|
// generate a new key
|
|
|
|
fmt.Println(err, " : Generating new ed25519 key")
|
|
|
|
_, priv, err = ed25519.GenerateKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Failed to generate ed25519 key: %s", err)
|
|
|
|
}
|
|
|
|
if err := writeKeyToLocalStorage(priv); err != nil {
|
|
|
|
fmt.Println("failed to write key to localStorage: ", err)
|
|
|
|
// non-fatal, we'll just have amnesia for a while
|
|
|
|
}
|
|
|
|
return priv
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-07-20 11:14:58 +00:00
|
|
|
startup()
|
|
|
|
|
|
|
|
// We want to block forever to let the fetch and libp2p handler serve the APIs
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startup() {
|
2021-06-25 13:20:36 +00:00
|
|
|
sk := generateKey()
|
|
|
|
pk := sk.Public().(ed25519.PublicKey)
|
|
|
|
|
2022-04-04 16:26:01 +00:00
|
|
|
pRouter := pineconeRouter.NewRouter(logrus.WithField("pinecone", "router"), sk, false)
|
|
|
|
pSessions := pineconeSessions.NewSessions(logrus.WithField("pinecone", "sessions"), pRouter, []string{"matrix"})
|
2022-04-08 12:55:46 +00:00
|
|
|
pManager := pineconeConnections.NewConnectionManager(pRouter)
|
|
|
|
pManager.AddPeer("wss://pinecone.matrix.org/public")
|
2021-06-25 13:20:36 +00:00
|
|
|
|
|
|
|
cfg := &config.Dendrite{}
|
2021-11-24 11:57:39 +00:00
|
|
|
cfg.Defaults(true)
|
2021-06-25 13:20:36 +00:00
|
|
|
cfg.UserAPI.AccountDatabase.ConnectionString = "file:/idb/dendritejs_account.db"
|
|
|
|
cfg.AppServiceAPI.Database.ConnectionString = "file:/idb/dendritejs_appservice.db"
|
2021-11-24 10:45:23 +00:00
|
|
|
cfg.FederationAPI.Database.ConnectionString = "file:/idb/dendritejs_fedsender.db"
|
2021-06-25 13:20:36 +00:00
|
|
|
cfg.MediaAPI.Database.ConnectionString = "file:/idb/dendritejs_mediaapi.db"
|
|
|
|
cfg.RoomServer.Database.ConnectionString = "file:/idb/dendritejs_roomserver.db"
|
|
|
|
cfg.SyncAPI.Database.ConnectionString = "file:/idb/dendritejs_syncapi.db"
|
|
|
|
cfg.KeyServer.Database.ConnectionString = "file:/idb/dendritejs_e2ekey.db"
|
2022-01-05 17:44:49 +00:00
|
|
|
cfg.Global.JetStream.StoragePath = "file:/idb/dendritejs/"
|
2021-06-25 13:20:36 +00:00
|
|
|
cfg.Global.TrustedIDServers = []string{}
|
|
|
|
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)
|
|
|
|
cfg.Global.PrivateKey = sk
|
2021-06-25 14:59:59 +00:00
|
|
|
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
|
2022-04-29 07:31:11 +00:00
|
|
|
cfg.ClientAPI.RegistrationDisabled = false
|
|
|
|
cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
|
2021-06-25 13:20:36 +00:00
|
|
|
|
|
|
|
if err := cfg.Derive(); err != nil {
|
|
|
|
logrus.Fatalf("Failed to derive values from config: %s", err)
|
|
|
|
}
|
2023-02-14 11:47:47 +00:00
|
|
|
base := base.NewBaseDendrite(cfg)
|
2021-06-25 13:20:36 +00:00
|
|
|
defer base.Close() // nolint: errcheck
|
|
|
|
|
2022-12-12 07:20:59 +00:00
|
|
|
rsAPI := roomserver.NewInternalAPI(base)
|
|
|
|
|
2021-06-25 13:20:36 +00:00
|
|
|
federation := conn.CreateFederationClient(base, pSessions)
|
|
|
|
|
|
|
|
serverKeyAPI := &signing.YggdrasilKeys{}
|
|
|
|
keyRing := serverKeyAPI.KeyRing()
|
|
|
|
|
2023-02-20 13:58:03 +00:00
|
|
|
userAPI := userapi.NewInternalAPI(base, rsAPI, federation)
|
2022-03-03 11:40:53 +00:00
|
|
|
|
2021-06-25 13:20:36 +00:00
|
|
|
asQuery := appservice.NewInternalAPI(
|
|
|
|
base, userAPI, rsAPI,
|
|
|
|
)
|
|
|
|
rsAPI.SetAppserviceAPI(asQuery)
|
2023-03-17 11:09:45 +00:00
|
|
|
caches := caching.NewRistrettoCache(base.Cfg.Global.Cache.EstimatedMaxSize, base.Cfg.Global.Cache.MaxAge, caching.EnableMetrics)
|
|
|
|
fedSenderAPI := federationapi.NewInternalAPI(base, federation, rsAPI, caches, keyRing, true)
|
2021-12-13 13:24:49 +00:00
|
|
|
rsAPI.SetFederationAPI(fedSenderAPI, keyRing)
|
2021-06-25 13:20:36 +00:00
|
|
|
|
|
|
|
monolith := setup.Monolith{
|
|
|
|
Config: base.Cfg,
|
|
|
|
Client: conn.CreateClient(base, pSessions),
|
|
|
|
FedClient: federation,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
|
2022-03-29 12:14:35 +00:00
|
|
|
AppserviceAPI: asQuery,
|
|
|
|
FederationAPI: fedSenderAPI,
|
|
|
|
RoomserverAPI: rsAPI,
|
|
|
|
UserAPI: userAPI,
|
2021-06-25 13:20:36 +00:00
|
|
|
//ServerKeyAPI: serverKeyAPI,
|
|
|
|
ExtPublicRoomsProvider: rooms.NewPineconeRoomProvider(pRouter, pSessions, fedSenderAPI, federation),
|
|
|
|
}
|
2023-03-17 11:09:45 +00:00
|
|
|
monolith.AddAllPublicRoutes(base, caches)
|
2021-06-25 13:20:36 +00:00
|
|
|
|
|
|
|
httpRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
2023-03-17 11:09:45 +00:00
|
|
|
httpRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(base.Routers.Client)
|
|
|
|
httpRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(base.Routers.Media)
|
2021-06-25 13:20:36 +00:00
|
|
|
|
2022-04-04 16:26:01 +00:00
|
|
|
p2pRouter := pSessions.Protocol("matrix").HTTP().Mux()
|
2023-03-17 11:09:45 +00:00
|
|
|
p2pRouter.Handle(httputil.PublicFederationPathPrefix, base.Routers.Federation)
|
|
|
|
p2pRouter.Handle(httputil.PublicMediaPathPrefix, base.Routers.Media)
|
2021-06-25 13:20:36 +00:00
|
|
|
|
|
|
|
// Expose the matrix APIs via fetch - for local traffic
|
|
|
|
go func() {
|
|
|
|
logrus.Info("Listening for service-worker fetch traffic")
|
|
|
|
s := JSServer{
|
|
|
|
Mux: httpRouter,
|
|
|
|
}
|
|
|
|
s.ListenAndServe("fetch")
|
|
|
|
}()
|
|
|
|
}
|