1
- import { logger as rootLogger } from "../logger.ts" ;
1
+ import { type Logger , logger as rootLogger } from "../logger.ts" ;
2
2
import { type EncryptionConfig } from "./MatrixRTCSession.ts" ;
3
3
import { secureRandomBase64Url } from "../randomstring.ts" ;
4
4
import { decodeBase64 , encodeUnpaddedBase64 } from "../base64.ts" ;
@@ -7,8 +7,6 @@ import { type CallMembership } from "./CallMembership.ts";
7
7
import { type KeyTransportEventListener , KeyTransportEvents , type IKeyTransport } from "./IKeyTransport.ts" ;
8
8
import { isMyMembership , type Statistics } from "./types.ts" ;
9
9
10
- const logger = rootLogger . getChild ( "MatrixRTCSession" ) ;
11
-
12
10
/**
13
11
* This interface is for testing and for making it possible to interchange the encryption manager.
14
12
* @internal
@@ -83,7 +81,7 @@ export class EncryptionManager implements IEncryptionManager {
83
81
private currentEncryptionKeyIndex = - 1 ;
84
82
85
83
private joinConfig : EncryptionConfig | undefined ;
86
-
84
+ private logger : Logger ;
87
85
public constructor (
88
86
private userId : string ,
89
87
private deviceId : string ,
@@ -95,7 +93,10 @@ export class EncryptionManager implements IEncryptionManager {
95
93
encryptionKeyIndex : number ,
96
94
participantId : string ,
97
95
) => void ,
98
- ) { }
96
+ parentLogger ?: Logger ,
97
+ ) {
98
+ this . logger = ( parentLogger ?? rootLogger ) . getChild ( `[EncryptionManager]` ) ;
99
+ }
99
100
100
101
public getEncryptionKeys ( ) : Map < string , Array < { key : Uint8Array ; timestamp : number } > > {
101
102
return this . encryptionKeys ;
@@ -163,11 +164,11 @@ export class EncryptionManager implements IEncryptionManager {
163
164
if ( this . makeNewKeyTimeout ) {
164
165
// existing rotation in progress, so let it complete
165
166
} else {
166
- logger . debug ( `Member(s) have left: queueing sender key rotation` ) ;
167
+ this . logger . debug ( `Member(s) have left: queueing sender key rotation` ) ;
167
168
this . makeNewKeyTimeout = setTimeout ( this . onRotateKeyTimeout , this . makeKeyDelay ) ;
168
169
}
169
170
} else if ( anyJoined ) {
170
- logger . debug ( `New member(s) have joined: re-sending keys` ) ;
171
+ this . logger . debug ( `New member(s) have joined: re-sending keys` ) ;
171
172
this . requestSendCurrentKey ( ) ;
172
173
} else if ( oldFingerprints ) {
173
174
// does it look like any of the members have updated their memberships?
@@ -179,7 +180,7 @@ export class EncryptionManager implements IEncryptionManager {
179
180
Array . from ( oldFingerprints ) . some ( ( x ) => ! newFingerprints . has ( x ) ) ||
180
181
Array . from ( newFingerprints ) . some ( ( x ) => ! oldFingerprints . has ( x ) ) ;
181
182
if ( candidateUpdates ) {
182
- logger . debug ( `Member(s) have updated/reconnected: re-sending keys to everyone` ) ;
183
+ this . logger . debug ( `Member(s) have updated/reconnected: re-sending keys to everyone` ) ;
183
184
this . requestSendCurrentKey ( ) ;
184
185
}
185
186
}
@@ -195,7 +196,7 @@ export class EncryptionManager implements IEncryptionManager {
195
196
private makeNewSenderKey ( delayBeforeUse = false ) : number {
196
197
const encryptionKey = secureRandomBase64Url ( 16 ) ;
197
198
const encryptionKeyIndex = this . getNewEncryptionKeyIndex ( ) ;
198
- logger . info ( "Generated new key at index " + encryptionKeyIndex ) ;
199
+ this . logger . info ( "Generated new key at index " + encryptionKeyIndex ) ;
199
200
this . setEncryptionKey (
200
201
this . userId ,
201
202
this . deviceId ,
@@ -218,7 +219,7 @@ export class EncryptionManager implements IEncryptionManager {
218
219
this . lastEncryptionKeyUpdateRequest &&
219
220
this . lastEncryptionKeyUpdateRequest + this . updateEncryptionKeyThrottle > Date . now ( )
220
221
) {
221
- logger . info ( "Last encryption key event sent too recently: postponing" ) ;
222
+ this . logger . info ( "Last encryption key event sent too recently: postponing" ) ;
222
223
if ( this . keysEventUpdateTimeout === undefined ) {
223
224
this . keysEventUpdateTimeout = setTimeout (
224
225
( ) => void this . sendEncryptionKeysEvent ( ) ,
@@ -254,17 +255,17 @@ export class EncryptionManager implements IEncryptionManager {
254
255
255
256
if ( ! this . joined ) return ;
256
257
257
- logger . info ( `Sending encryption keys event. indexToSend=${ indexToSend } ` ) ;
258
+ this . logger . info ( `Sending encryption keys event. indexToSend=${ indexToSend } ` ) ;
258
259
259
260
const myKeys = this . getKeysForParticipant ( this . userId , this . deviceId ) ;
260
261
261
262
if ( ! myKeys ) {
262
- logger . warn ( "Tried to send encryption keys event but no keys found!" ) ;
263
+ this . logger . warn ( "Tried to send encryption keys event but no keys found!" ) ;
263
264
return ;
264
265
}
265
266
266
267
if ( typeof indexToSend !== "number" && this . currentEncryptionKeyIndex === - 1 ) {
267
- logger . warn ( "Tried to send encryption keys event but no current key index found!" ) ;
268
+ this . logger . warn ( "Tried to send encryption keys event but no current key index found!" ) ;
268
269
return ;
269
270
}
270
271
@@ -274,17 +275,17 @@ export class EncryptionManager implements IEncryptionManager {
274
275
try {
275
276
this . statistics . counters . roomEventEncryptionKeysSent += 1 ;
276
277
await this . transport . sendKey ( encodeUnpaddedBase64 ( keyToSend ) , keyIndexToSend , this . getMemberships ( ) ) ;
277
- logger . debug (
278
+ this . logger . debug (
278
279
`Embedded-E2EE-LOG updateEncryptionKeyEvent participantId=${ this . userId } :${ this . deviceId } numKeys=${ myKeys . length } currentKeyIndex=${ this . currentEncryptionKeyIndex } keyIndexToSend=${ keyIndexToSend } ` ,
279
280
this . encryptionKeys ,
280
281
) ;
281
282
} catch ( error ) {
282
283
if ( this . keysEventUpdateTimeout === undefined ) {
283
284
const resendDelay = safeGetRetryAfterMs ( error , 5000 ) ;
284
- logger . warn ( `Failed to send m.call.encryption_key, retrying in ${ resendDelay } ` , error ) ;
285
+ this . logger . warn ( `Failed to send m.call.encryption_key, retrying in ${ resendDelay } ` , error ) ;
285
286
this . keysEventUpdateTimeout = setTimeout ( ( ) => void this . sendEncryptionKeysEvent ( ) , resendDelay ) ;
286
287
} else {
287
- logger . info ( "Not scheduling key resend as another re-send is already pending" ) ;
288
+ this . logger . info ( "Not scheduling key resend as another re-send is already pending" ) ;
288
289
}
289
290
}
290
291
} ;
@@ -332,7 +333,7 @@ export class EncryptionManager implements IEncryptionManager {
332
333
timestamp : number ,
333
334
delayBeforeUse = false ,
334
335
) : void {
335
- logger . debug ( `Setting encryption key for ${ userId } :${ deviceId } at index ${ encryptionKeyIndex } ` ) ;
336
+ this . logger . debug ( `Setting encryption key for ${ userId } :${ deviceId } at index ${ encryptionKeyIndex } ` ) ;
336
337
const keyBin = decodeBase64 ( encryptionKeyString ) ;
337
338
338
339
const participantId = getParticipantId ( userId , deviceId ) ;
@@ -345,7 +346,7 @@ export class EncryptionManager implements IEncryptionManager {
345
346
346
347
if ( existingKeyAtIndex ) {
347
348
if ( existingKeyAtIndex . timestamp > timestamp ) {
348
- logger . info (
349
+ this . logger . info (
349
350
`Ignoring new key at index ${ encryptionKeyIndex } for ${ participantId } as it is older than existing known key` ,
350
351
) ;
351
352
return ;
@@ -365,7 +366,7 @@ export class EncryptionManager implements IEncryptionManager {
365
366
if ( delayBeforeUse ) {
366
367
const useKeyTimeout = setTimeout ( ( ) => {
367
368
this . setNewKeyTimeouts . delete ( useKeyTimeout ) ;
368
- logger . info ( `Delayed-emitting key changed event for ${ participantId } idx ${ encryptionKeyIndex } ` ) ;
369
+ this . logger . info ( `Delayed-emitting key changed event for ${ participantId } idx ${ encryptionKeyIndex } ` ) ;
369
370
if ( userId === this . userId && deviceId === this . deviceId ) {
370
371
this . currentEncryptionKeyIndex = encryptionKeyIndex ;
371
372
}
@@ -384,7 +385,7 @@ export class EncryptionManager implements IEncryptionManager {
384
385
if ( ! this . manageMediaKeys ) return ;
385
386
386
387
this . makeNewKeyTimeout = undefined ;
387
- logger . info ( "Making new sender key for key rotation" ) ;
388
+ this . logger . info ( "Making new sender key for key rotation" ) ;
388
389
const newKeyIndex = this . makeNewSenderKey ( true ) ;
389
390
// send immediately: if we're about to start sending with a new key, it's
390
391
// important we get it out to others as soon as we can.
0 commit comments