aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-04-27 00:38:04 +0300
committerTulir Asokan <tulir@maunium.net>2020-04-27 00:38:04 +0300
commit0e8e81a6078a98803f1012882f8c176fd3b5204e (patch)
treef33433ef6629fcca4b92c8d9b50dab4c30ec4a63
parent96bb87e8ac8f45d56d487ea6c16d67f057d97e1f (diff)
Add support for decrypting messages
-rw-r--r--config/config.go7
-rw-r--r--go.mod2
-rw-r--r--go.sum15
-rw-r--r--matrix/matrix.go48
-rw-r--r--matrix/rooms/room.go2
-rw-r--r--matrix/sync.go45
6 files changed, 96 insertions, 23 deletions
diff --git a/config/config.go b/config/config.go
index 3fe961e..8234edc 100644
--- a/config/config.go
+++ b/config/config.go
@@ -54,9 +54,10 @@ type UserPreferences struct {
// Config contains the main config of gomuks.
type Config struct {
- UserID id.UserID `yaml:"mxid"`
- AccessToken string `yaml:"access_token"`
- HS string `yaml:"homeserver"`
+ UserID id.UserID `yaml:"mxid"`
+ DeviceID id.DeviceID `yaml:"device_id"`
+ AccessToken string `yaml:"access_token"`
+ HS string `yaml:"homeserver"`
RoomCacheSize int `yaml:"room_cache_size"`
RoomCacheAge int64 `yaml:"room_cache_age"`
diff --git a/go.mod b/go.mod
index 75d4a09..8564081 100644
--- a/go.mod
+++ b/go.mod
@@ -21,7 +21,7 @@ require (
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.8
- maunium.net/go/mautrix v0.2.0-beta.4
+ maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b
maunium.net/go/mauview v0.1.0
maunium.net/go/tcell v0.1.0
)
diff --git a/go.sum b/go.sum
index dfaa139..9d0874b 100644
--- a/go.sum
+++ b/go.sum
@@ -47,6 +47,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
+github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
+github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
+github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
+github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
+github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
+github.com/tidwall/pretty v1.0.1 h1:WE4RBSZ1x6McVVC8S/Md+Qse8YUv6HRObAx6ke00NY8=
+github.com/tidwall/pretty v1.0.1/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
+github.com/tidwall/sjson v1.1.1 h1:7h1vk049Jnd5EH9NyzNiEuwYW4b5qgreBbqRC19AS3U=
+github.com/tidwall/sjson v1.1.1/go.mod h1:yvVuSnpEQv5cYIrO+AT6kw4QVfd5SDZoGIS7/5+fZFs=
github.com/zyedidia/clipboard v0.0.0-20190823154308-241f98e9b197 h1:gYTNnAW6azuB3BbA6QYWO/H4F2ABSOjjw3Z03tlXd2c=
github.com/zyedidia/clipboard v0.0.0-20190823154308-241f98e9b197/go.mod h1:WDk3p8GiZV9+xFWlSo8qreeoLhW6Ik692rqXk+cNeRY=
github.com/zyedidia/poller v1.0.1/go.mod h1:vZXJOHGDcuK08GXhF6IAY0ZFd2WcgOR5DOTp84Uk5eE=
@@ -73,10 +82,8 @@ gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2/go.mod h1:s1Sn2yZos05Qfs7NK
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
-maunium.net/go/mautrix v0.2.0-beta.3 h1:pm5MQszvzYaLyCEGjyUau4DvzrOw5dT45UAvDLz5zBo=
-maunium.net/go/mautrix v0.2.0-beta.3/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
-maunium.net/go/mautrix v0.2.0-beta.4 h1:L7Jpc+8GGc+Qo0DdamACEeU1Ci9G1mergJpsTTgDOUA=
-maunium.net/go/mautrix v0.2.0-beta.4/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
+maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b h1:s3+wlMmmtpPUoOGVyS5nyR62htnwD/TEfA0NVbDk7zc=
+maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg=
maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U=
maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM=
diff --git a/matrix/matrix.go b/matrix/matrix.go
index d0fd2f4..8804103 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -37,6 +37,7 @@ import (
"github.com/pkg/errors"
"maunium.net/go/mautrix"
+ "maunium.net/go/mautrix/crypto"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
@@ -55,6 +56,7 @@ import (
// It is used for all Matrix calls from the UI and Matrix event handlers.
type Container struct {
client *mautrix.Client
+ crypto *crypto.OlmMachine
syncer *GomuksSyncer
gmx ifc.Gomuks
ui ifc.GomuksUI
@@ -88,6 +90,24 @@ func (log mxLogger) Debugfln(message string, args ...interface{}) {
debug.Printf("[Matrix] "+message, args...)
}
+type cryptoLogger struct{}
+
+func (c cryptoLogger) Error(message string, args ...interface{}) {
+ debug.Printf("[Crypto/Error] "+message, args...)
+}
+
+func (c cryptoLogger) Warn(message string, args ...interface{}) {
+ debug.Printf("[Crypto/Warn] "+message, args...)
+}
+
+func (c cryptoLogger) Debug(message string, args ...interface{}) {
+ debug.Printf("[Crypto/Debug] "+message, args...)
+}
+
+func (c cryptoLogger) Trace(message string, args ...interface{}) {
+ debug.Printf("[Crypto/Trace] "+message, args...)
+}
+
// InitClient initializes the mautrix client and connects to the homeserver specified in the config.
func (c *Container) InitClient() error {
if len(c.config.HS) == 0 {
@@ -97,6 +117,7 @@ func (c *Container) InitClient() error {
if c.client != nil {
c.Stop()
c.client = nil
+ c.crypto = nil
}
var mxid id.UserID
@@ -112,6 +133,17 @@ func (c *Container) InitClient() error {
return err
}
c.client.Logger = mxLogger{}
+ c.client.DeviceID = c.config.DeviceID
+
+ cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.CacheDir, "crypto.gob"))
+ if err != nil {
+ return err
+ }
+ c.crypto = crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore)
+ err = c.crypto.Load()
+ if err != nil {
+ return err
+ }
if c.history == nil {
c.history, err = NewHistoryManager(c.config.HistoryPath)
@@ -159,7 +191,9 @@ func (c *Container) PasswordLogin(user, password string) error {
func (c *Container) finishLogin(resp *mautrix.RespLogin) {
c.client.SetCredentials(resp.UserID, resp.AccessToken)
+ c.client.DeviceID = resp.DeviceID
c.config.UserID = resp.UserID
+ c.config.DeviceID = resp.DeviceID
c.config.AccessToken = resp.AccessToken
c.config.Save()
@@ -250,6 +284,7 @@ func (c *Container) Logout() {
c.config.DeleteSession()
c.Stop()
c.client = nil
+ c.crypto = nil
c.ui.OnLogout()
}
@@ -315,8 +350,9 @@ func (c *Container) OnLogin() {
debug.Print("Initializing syncer")
c.syncer = NewGomuksSyncer(c.config.Rooms)
+ c.syncer.OnSync(c.crypto.ProcessSyncResponse)
c.syncer.OnEventType(event.EventMessage, c.HandleMessage)
- c.syncer.OnEventType(event.EventEncrypted, c.HandleMessage)
+ c.syncer.OnEventType(event.EventEncrypted, c.HandleEncrypted)
c.syncer.OnEventType(event.EventSticker, c.HandleMessage)
c.syncer.OnEventType(event.EventReaction, c.HandleMessage)
c.syncer.OnEventType(event.EventRedaction, c.HandleRedaction)
@@ -516,6 +552,16 @@ func (c *Container) HandleReaction(room *rooms.Room, reactsTo id.EventID, reactE
}
}
+func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) {
+ evt, err := c.crypto.DecryptMegolmEvent(mxEvent)
+ if err != nil {
+ debug.Print("Failed to decrypt event:", err)
+ return
+ }
+ debug.Print("!!!!!", evt)
+ c.HandleMessage(source, evt)
+}
+
// HandleMessage is the event handler for the m.room.message timeline event.
func (c *Container) HandleMessage(source EventSource, mxEvent *event.Event) {
room := c.GetOrCreateRoom(mxEvent.RoomID)
diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go
index 0238cfb..45222e1 100644
--- a/matrix/rooms/room.go
+++ b/matrix/rooms/room.go
@@ -412,7 +412,7 @@ func (room *Room) UpdateState(evt *event.Event) {
case *event.TopicEventContent:
room.topicCache = content.Topic
case *event.EncryptionEventContent:
- if content.Algorithm == event.AlgorithmMegolmV1 {
+ if content.Algorithm == id.AlgorithmMegolmV1 {
room.Encrypted = true
}
}
diff --git a/matrix/sync.go b/matrix/sync.go
index 85de68c..415748e 100644
--- a/matrix/sync.go
+++ b/matrix/sync.go
@@ -83,9 +83,11 @@ func (es EventSource) String() string {
}
type EventHandler func(source EventSource, event *event.Event)
+type SyncHandler func(resp *mautrix.RespSync)
type GomuksSyncer struct {
rooms *rooms.RoomCache
+ globalListeners []SyncHandler
listeners map[event.Type][]EventHandler // event type to listeners array
FirstSyncDone bool
InitDoneCallback func()
@@ -96,10 +98,11 @@ type GomuksSyncer struct {
// NewGomuksSyncer returns an instantiated GomuksSyncer
func NewGomuksSyncer(rooms *rooms.RoomCache) *GomuksSyncer {
return &GomuksSyncer{
- rooms: rooms,
- listeners: make(map[event.Type][]EventHandler),
- FirstSyncDone: false,
- Progress: StubSyncingModal{},
+ rooms: rooms,
+ globalListeners: []SyncHandler{},
+ listeners: make(map[event.Type][]EventHandler),
+ FirstSyncDone: false,
+ Progress: StubSyncingModal{},
}
}
@@ -109,23 +112,26 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
s.rooms.DisableUnloading()
}
debug.Print("Received sync response")
+ s.Progress.SetMessage("Processing sync response")
steps := len(res.Rooms.Join) + len(res.Rooms.Invite) + len(res.Rooms.Leave)
- s.Progress.SetSteps(steps + 2)
- s.Progress.SetMessage("Processing global events")
- s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence)
- s.Progress.Step()
- s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData)
- s.Progress.Step()
+ s.Progress.SetSteps(steps + 2 + len(s.globalListeners))
wait := &sync.WaitGroup{}
-
- wait.Add(steps)
callback := func() {
wait.Done()
s.Progress.Step()
}
+ wait.Add(len(s.globalListeners))
+ s.notifyGlobalListeners(res, callback)
+ wait.Wait()
+
+ s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence)
+ s.Progress.Step()
+ s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData)
+ s.Progress.Step()
+
+ wait.Add(steps)
- s.Progress.SetMessage("Processing room events")
for roomID, roomData := range res.Rooms.Join {
go s.processJoinedRoom(roomID, roomData, callback)
}
@@ -152,6 +158,15 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
return
}
+func (s *GomuksSyncer) notifyGlobalListeners(res *mautrix.RespSync, callback func()) {
+ for _, listener := range s.globalListeners {
+ go func(listener SyncHandler) {
+ listener(res)
+ callback()
+ }(listener)
+ }
+}
+
func (s *GomuksSyncer) processJoinedRoom(roomID id.RoomID, roomData mautrix.SyncJoinedRoom, callback func()) {
defer debug.Recover()
room := s.rooms.GetOrCreate(roomID)
@@ -239,6 +254,10 @@ func (s *GomuksSyncer) OnEventType(eventType event.Type, callback EventHandler)
s.listeners[eventType] = append(s.listeners[eventType], callback)
}
+func (s *GomuksSyncer) OnSync(callback SyncHandler) {
+ s.globalListeners = append(s.globalListeners, callback)
+}
+
func (s *GomuksSyncer) notifyListeners(source EventSource, evt *event.Event) {
listeners, exists := s.listeners[evt.Type]
if !exists {