mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-13 09:03:10 +01:00
Re-request Megolm sessions if they don't arrive on time
This commit is contained in:
parent
7bb629d199
commit
6cb5301996
5 changed files with 20 additions and 9 deletions
|
@ -201,6 +201,15 @@ func (helper *CryptoHelper) WaitForSession(roomID id.RoomID, senderKey id.Sender
|
|||
return helper.mach.WaitForSession(roomID, senderKey, sessionID, timeout)
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) RequestSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID, userID id.UserID, deviceID id.DeviceID) {
|
||||
err := helper.mach.SendRoomKeyRequest(roomID, senderKey, sessionID, "", map[id.UserID][]id.DeviceID{userID: {deviceID}})
|
||||
if err != nil {
|
||||
helper.log.Warnfln("Failed to send key request to %s/%s for %s in %s: %v", userID, deviceID, sessionID, roomID, err)
|
||||
} else {
|
||||
helper.log.Debugfln("Sent key request to %s/%s for %s in %s", userID, deviceID, sessionID, roomID)
|
||||
}
|
||||
}
|
||||
|
||||
func (helper *CryptoHelper) ResetSession(roomID id.RoomID) {
|
||||
err := helper.mach.CryptoStore.RemoveOutboundGroupSession(roomID)
|
||||
if err != nil {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -14,7 +14,7 @@ require (
|
|||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
maunium.net/go/mauflag v1.0.0
|
||||
maunium.net/go/maulogger/v2 v2.3.1
|
||||
maunium.net/go/mautrix v0.10.3
|
||||
maunium.net/go/mautrix v0.10.5-0.20211203161903-f556aff31562
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
4
go.sum
4
go.sum
|
@ -221,5 +221,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
|
|||
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
|
||||
maunium.net/go/maulogger/v2 v2.3.1 h1:fwBYJne0pHvJrrIPHK+TAPfyxxbBEz46oVGez2x0ODE=
|
||||
maunium.net/go/maulogger/v2 v2.3.1/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A=
|
||||
maunium.net/go/mautrix v0.10.3 h1:TNRhlpXnYcvPEDo0tnAvLoewQ4Zc4mhFHU4k4AipsOc=
|
||||
maunium.net/go/mautrix v0.10.3/go.mod h1:k4Ng5oci83MEbqPL6KOjPdbU7f8v01KlMjR/zTQ+7mA=
|
||||
maunium.net/go/mautrix v0.10.5-0.20211203161903-f556aff31562 h1:SjfM0TBdClR/RIFPCY3Ufz+QCbvC/qwjiHzN/Z2JuaM=
|
||||
maunium.net/go/mautrix v0.10.5-0.20211203161903-f556aff31562/go.mod h1:k4Ng5oci83MEbqPL6KOjPdbU7f8v01KlMjR/zTQ+7mA=
|
||||
|
|
1
main.go
1
main.go
|
@ -194,6 +194,7 @@ type Crypto interface {
|
|||
Decrypt(*event.Event) (*event.Event, error)
|
||||
Encrypt(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error)
|
||||
WaitForSession(id.RoomID, id.SenderKey, id.SessionID, time.Duration) bool
|
||||
RequestSession(id.RoomID, id.SenderKey, id.SessionID, id.UserID, id.DeviceID)
|
||||
ResetSession(id.RoomID)
|
||||
Init() error
|
||||
Start()
|
||||
|
|
13
matrix.go
13
matrix.go
|
@ -342,7 +342,7 @@ func (mx *MatrixHandler) HandleEncrypted(evt *event.Event) {
|
|||
mx.log.Debugfln("Got session %s after waiting, trying to decrypt %s again", content.SessionID, evt.ID)
|
||||
decrypted, err = mx.bridge.Crypto.Decrypt(evt)
|
||||
} else {
|
||||
mx.as.SendErrorMessageSendCheckpoint(evt, appservice.StepDecrypted, err, false)
|
||||
mx.as.SendErrorMessageSendCheckpoint(evt, appservice.StepDecrypted, fmt.Errorf("didn't receive encryption keys"), false)
|
||||
go mx.waitLongerForSession(evt)
|
||||
return
|
||||
}
|
||||
|
@ -360,12 +360,14 @@ func (mx *MatrixHandler) HandleEncrypted(evt *event.Event) {
|
|||
}
|
||||
|
||||
func (mx *MatrixHandler) waitLongerForSession(evt *event.Event) {
|
||||
const extendedTimeout = sessionWaitTimeout * 2
|
||||
const extendedTimeout = sessionWaitTimeout * 3
|
||||
|
||||
content := evt.Content.AsEncrypted()
|
||||
mx.log.Debugfln("Couldn't find session %s trying to decrypt %s, waiting %d more seconds...",
|
||||
content.SessionID, evt.ID, int(extendedTimeout.Seconds()))
|
||||
|
||||
go mx.bridge.Crypto.RequestSession(evt.RoomID, content.SenderKey, content.SessionID, evt.Sender, content.DeviceID)
|
||||
|
||||
resp, err := mx.bridge.Bot.SendNotice(evt.RoomID, fmt.Sprintf(
|
||||
"\u26a0 Your message was not bridged: the bridge hasn't received the decryption keys. "+
|
||||
"The bridge will retry for %d seconds. If this error keeps happening, try restarting your client.",
|
||||
|
@ -388,11 +390,10 @@ func (mx *MatrixHandler) waitLongerForSession(evt *event.Event) {
|
|||
mx.as.SendErrorMessageSendCheckpoint(evt, appservice.StepDecrypted, err, true)
|
||||
update.Body = fmt.Sprintf("\u26a0 Your message was not bridged: %v", err)
|
||||
} else {
|
||||
errMsg := fmt.Sprintf("Didn't get %s, giving up on %s", content.SessionID, evt.ID)
|
||||
mx.log.Debugfln(errMsg)
|
||||
mx.as.SendErrorMessageSendCheckpoint(evt, appservice.StepDecrypted, fmt.Errorf(errMsg), true)
|
||||
mx.log.Debugfln("Didn't get %s, giving up on %s", content.SessionID, evt.ID)
|
||||
mx.as.SendErrorMessageSendCheckpoint(evt, appservice.StepDecrypted, fmt.Errorf("didn't receive encryption keys"), true)
|
||||
update.Body = "\u26a0 Your message was not bridged: the bridge hasn't received the decryption keys. " +
|
||||
"If this keeps happening, try restarting your client."
|
||||
"If this error keeps happening, try restarting your client."
|
||||
}
|
||||
|
||||
newContent := update
|
||||
|
|
Loading…
Reference in a new issue