Pārlūkot izejas kodu

Add command to reset the megolm session in a room

Tulir Asokan 4 gadi atpakaļ
vecāks
revīzija
0f17863708
3 mainītis faili ar 21 papildinājumiem un 0 dzēšanām
  1. 13 0
      commands.go
  2. 7 0
      crypto.go
  3. 1 0
      main.go

+ 13 - 0
commands.go

@@ -123,6 +123,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
 		handler.CommandDeletePortal(ce)
 	case "delete-all-portals":
 		handler.CommandDeleteAllPortals(ce)
+	case "discard-megolm-session", "discard-session":
+		handler.CommandDiscardMegolmSession(ce)
 	case "dev-test":
 		handler.CommandDevTest(ce)
 	case "set-pl":
@@ -163,6 +165,17 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
 	}
 }
 
+func (handler *CommandHandler) CommandDiscardMegolmSession(ce *CommandEvent) {
+	if handler.bridge.Crypto == nil {
+		ce.Reply("This bridge instance doesn't have end-to-bridge encryption enabled")
+	} else if !ce.User.Admin {
+		ce.Reply("Only the bridge admin can reset Megolm sessions")
+	} else {
+		handler.bridge.Crypto.ResetSession(ce.RoomID)
+		ce.Reply("Successfully reset Megolm session in this room. New decryption keys will be shared the next time a message is sent from WhatsApp.")
+	}
+}
+
 func (handler *CommandHandler) CommandRelaybot(ce *CommandEvent) {
 	if handler.bridge.Relaybot == nil {
 		ce.Reply("The relaybot is disabled")

+ 7 - 0
crypto.go

@@ -189,6 +189,13 @@ func (helper *CryptoHelper) WaitForSession(roomID id.RoomID, senderKey id.Sender
 	return helper.mach.WaitForSession(roomID, senderKey, sessionID, timeout)
 }
 
+func (helper *CryptoHelper) ResetSession(roomID id.RoomID) {
+	err := helper.mach.CryptoStore.RemoveOutboundGroupSession(roomID)
+	if err != nil {
+		helper.log.Debugfln("Error manually removing outbound group session in %s: %v", roomID, err)
+	}
+}
+
 func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) {
 	helper.mach.HandleMemberEvent(evt)
 }

+ 1 - 0
main.go

@@ -154,6 +154,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
+	ResetSession(id.RoomID)
 	Init() error
 	Start()
 	Stop()