Switch crypto store to SQLite
This commit is contained in:
parent
d958c372cb
commit
c2f0bb244b
1
go.mod
1
go.mod
@ -10,6 +10,7 @@ require (
|
||||
github.com/lithammer/fuzzysearch v1.1.1
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3
|
||||
github.com/mattn/go-runewidth v0.0.9
|
||||
github.com/mattn/go-sqlite3 v1.14.0
|
||||
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
|
||||
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
|
||||
github.com/rivo/uniseg v0.1.0
|
||||
|
1
go.sum
1
go.sum
@ -49,6 +49,7 @@ github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
|
||||
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
|
||||
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
|
@ -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() {}
|
||||
|
Loading…
Reference in New Issue
Block a user