Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridge/crypto: add ShareKeys function to CryptoHelper #140

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bridge

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -219,6 +220,7 @@ type Crypto interface {
Stop()
Reset(startAfterReset bool)
Client() *mautrix.Client
ShareKeys(context.Context) error
}

func (br *Bridge) GenerateRegistration() {
Expand Down
5 changes: 5 additions & 0 deletions bridge/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) {
helper.mach.HandleMemberEvent(0, evt)
}

// ShareKeys uploads the given number of one-time-keys to the server.
func (helper *CryptoHelper) ShareKeys(ctx context.Context) error {
return helper.mach.ShareKeys(ctx, -1)
}

type cryptoSyncer struct {
*crypto.OlmMachine
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ func (mach *OlmMachine) ShareKeys(ctx context.Context, currentOTKCount int) erro
start := time.Now()
mach.otkUploadLock.Lock()
defer mach.otkUploadLock.Unlock()
if mach.lastOTKUpload.Add(1 * time.Minute).After(start) {
log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests")
if mach.lastOTKUpload.Add(1*time.Minute).After(start) || currentOTKCount < 0 {
log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests or negative OTK count")
resp, err := mach.Client.UploadKeys(&mautrix.ReqUploadKeys{})
if err != nil {
return fmt.Errorf("failed to check current OTK counts: %w", err)
Expand Down