Switch crypto store to SQLite
This commit is contained in:
@ -19,30 +19,36 @@
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"maunium.net/go/mautrix/crypto"
|
||||
|
||||
"maunium.net/go/gomuks/debug"
|
||||
)
|
||||
|
||||
type cryptoLogger struct{}
|
||||
type cryptoLogger struct {
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (c cryptoLogger) Error(message string, args ...interface{}) {
|
||||
debug.Printf("[Crypto/Error] "+message, args...)
|
||||
debug.Printf(fmt.Sprintf("[%s/Error] %s", c.prefix, message), args...)
|
||||
}
|
||||
|
||||
func (c cryptoLogger) Warn(message string, args ...interface{}) {
|
||||
debug.Printf("[Crypto/Warn] "+message, args...)
|
||||
debug.Printf(fmt.Sprintf("[%s/Warn] %s", c.prefix, message), args...)
|
||||
}
|
||||
|
||||
func (c cryptoLogger) Debug(message string, args ...interface{}) {
|
||||
debug.Printf("[Crypto/Debug] "+message, args...)
|
||||
debug.Printf(fmt.Sprintf("[%s/Debug] %s", c.prefix, message), args...)
|
||||
}
|
||||
|
||||
func (c cryptoLogger) Trace(message string, args ...interface{}) {
|
||||
debug.Printf("[Crypto/Trace] "+message, args...)
|
||||
debug.Printf(fmt.Sprintf("[%s/Trace] %s", c.prefix, message), args...)
|
||||
}
|
||||
|
||||
func isBadEncryptError(err error) bool {
|
||||
@ -50,11 +56,31 @@ func isBadEncryptError(err error) bool {
|
||||
}
|
||||
|
||||
func (c *Container) initCrypto() error {
|
||||
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.DataDir, "crypto.gob"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open crypto store: %w", err)
|
||||
var cryptoStore crypto.Store
|
||||
var err error
|
||||
legacyStorePath := filepath.Join(c.config.DataDir, "crypto.gob")
|
||||
if _, err = os.Stat(legacyStorePath); err == nil {
|
||||
debug.Printf("Using legacy crypto store as %s exists", legacyStorePath)
|
||||
cryptoStore, err = crypto.NewGobStore(legacyStorePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("file open: %w", err)
|
||||
}
|
||||
} else {
|
||||
debug.Printf("Using SQLite crypto store")
|
||||
newStorePath := filepath.Join(c.config.DataDir, "crypto.db")
|
||||
db, err := sql.Open("sqlite3", newStorePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sql open: %w", err)
|
||||
}
|
||||
accID := fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
|
||||
sqlStore := crypto.NewSQLCryptoStore(db, "sqlite3", accID, c.config.DeviceID, []byte("fi.mau.gomuks"), cryptoLogger{"Crypto/DB"})
|
||||
err = sqlStore.CreateTables()
|
||||
if err != nil {
|
||||
return fmt.Errorf("create table: %w", err)
|
||||
}
|
||||
cryptoStore = sqlStore
|
||||
}
|
||||
crypt := crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore, c.config.Rooms)
|
||||
crypt := crypto.NewOlmMachine(c.client, cryptoLogger{"Crypto"}, cryptoStore, c.config.Rooms)
|
||||
crypt.AllowUnverifiedDevices = !c.config.SendToVerifiedOnly
|
||||
c.crypto = crypt
|
||||
err = c.crypto.Load()
|
||||
@ -63,3 +89,12 @@ func (c *Container) initCrypto() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Container) cryptoOnLogin() {
|
||||
sqlStore, ok := c.crypto.(*crypto.OlmMachine).CryptoStore.(*crypto.SQLCryptoStore)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
sqlStore.DeviceID = c.config.DeviceID
|
||||
sqlStore.AccountID = fmt.Sprintf("%s/%s", c.config.UserID.String(), c.config.DeviceID)
|
||||
}
|
||||
|
@ -337,6 +337,7 @@ func (s StubSyncingModal) Close() {}
|
||||
|
||||
// OnLogin initializes the syncer and updates the room list.
|
||||
func (c *Container) OnLogin() {
|
||||
c.cryptoOnLogin()
|
||||
c.ui.OnLogin()
|
||||
|
||||
c.client.Store = c.config
|
||||
|
@ -11,3 +11,5 @@ func isBadEncryptError(err error) bool {
|
||||
func (c *Container) initCrypto() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Container) cryptoOnLogin() {}
|
||||
|
Reference in New Issue
Block a user