add neurone - wip
This commit is contained in:
parent
c88cb4bca9
commit
c27f45161e
313 changed files with 15684 additions and 8101 deletions
85
src/client/decryption/DecryptionManager.ts
Normal file
85
src/client/decryption/DecryptionManager.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import {
|
||||
ClientEventWithoutRoomId,
|
||||
MessageEventWithoutRoomId,
|
||||
StateEventWithoutRoomId,
|
||||
} from 'neurone/lib/api/types';
|
||||
import { Machine, RoomKeyInfo, RoomKeyWithheldInfo } from 'neurone/lib/crypto/machine';
|
||||
import { ObjectReader } from 'neurone/lib/utils';
|
||||
import { Disposable, Disposer } from 'neurone/lib/utils/disposable';
|
||||
import { EncryptedEvent } from './EncryptedEvent';
|
||||
import { EncryptedEventIndex } from './EncryptedEventIndex';
|
||||
|
||||
export class DecryptionManager implements Disposable {
|
||||
private readonly machine: Machine;
|
||||
|
||||
private readonly disposer = new Disposer();
|
||||
|
||||
private readonly stateEvents = new EncryptedEventIndex<StateEventWithoutRoomId>();
|
||||
|
||||
private readonly messageEvents = new EncryptedEventIndex<MessageEventWithoutRoomId>();
|
||||
|
||||
constructor(machine: Machine) {
|
||||
this.machine = machine;
|
||||
|
||||
this.disposer.collect(
|
||||
this.machine.events.onRoomKeysUpdated.subscribe(this.handleRoomKeysUpdated.bind(this)),
|
||||
this.machine.events.onRoomKeysWithheld.subscribe(this.handleRoomKeysWithheld.bind(this))
|
||||
);
|
||||
}
|
||||
|
||||
private handleRoomKeysUpdated(keysInfo: RoomKeyInfo[]): void {
|
||||
keysInfo.forEach((keyInfo) => this.decryptSession(keyInfo.roomId, keyInfo.sessionId));
|
||||
}
|
||||
|
||||
private handleRoomKeysWithheld(keysInfo: RoomKeyWithheldInfo[]): void {
|
||||
keysInfo.forEach((keyInfo) => this.decryptSession(keyInfo.roomId, keyInfo.sessionId));
|
||||
}
|
||||
|
||||
private decryptSession(roomId: string, sessionId: string): void {
|
||||
const m = this.messageEvents.getEventsForSession(roomId, sessionId);
|
||||
m.forEach((event) => event.decrypt());
|
||||
|
||||
const s = this.stateEvents.getEventsForSession(roomId, sessionId);
|
||||
s.forEach((event) => event.decrypt());
|
||||
}
|
||||
|
||||
private registerEvent<T extends ClientEventWithoutRoomId>(
|
||||
eventIndex: EncryptedEventIndex<T>,
|
||||
roomId: string,
|
||||
event: T
|
||||
): EncryptedEvent<T> {
|
||||
const { content } = event;
|
||||
const sessionId = ObjectReader.string(content, 'session_id');
|
||||
|
||||
if (!sessionId) {
|
||||
throw new Error('Malformed event! Missing session_id');
|
||||
}
|
||||
|
||||
const existingEvent = eventIndex.get(roomId, sessionId, event.event_id);
|
||||
if (existingEvent) return existingEvent;
|
||||
|
||||
const encryptedEvent = new EncryptedEvent(this.machine.roomEncryption, roomId, event);
|
||||
|
||||
eventIndex.store(roomId, sessionId, encryptedEvent);
|
||||
|
||||
return encryptedEvent;
|
||||
}
|
||||
|
||||
public decryptableMessageEvent(
|
||||
roomId: string,
|
||||
event: MessageEventWithoutRoomId
|
||||
): EncryptedEvent<MessageEventWithoutRoomId> {
|
||||
return this.registerEvent(this.messageEvents, roomId, event);
|
||||
}
|
||||
|
||||
public decryptableStateEvent(
|
||||
roomId: string,
|
||||
event: StateEventWithoutRoomId
|
||||
): EncryptedEvent<StateEventWithoutRoomId> {
|
||||
return this.registerEvent(this.stateEvents, roomId, event);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.disposer.run();
|
||||
}
|
||||
}
|
||||
93
src/client/decryption/EncryptedEvent.ts
Normal file
93
src/client/decryption/EncryptedEvent.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import {
|
||||
ClientEventWithoutRoomId,
|
||||
MessageEventWithoutRoomId,
|
||||
StateEventWithoutRoomId,
|
||||
} from 'neurone/lib/api/types';
|
||||
import { MegolmDecryptionError, RoomEncryption } from 'neurone/lib/crypto/machine';
|
||||
import { ObservableValue } from 'neurone/lib/utils/observable';
|
||||
|
||||
export class EventDecryptionIdle {}
|
||||
|
||||
export class EventDecryptionPending {}
|
||||
|
||||
export class EventDecryptionError {
|
||||
public readonly error?: MegolmDecryptionError;
|
||||
|
||||
public readonly cause?: unknown;
|
||||
|
||||
constructor(error?: MegolmDecryptionError, cause?: unknown) {
|
||||
this.error = error;
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
|
||||
export class EventDecryptionSuccess<T extends ClientEventWithoutRoomId> {
|
||||
public readonly event: T extends StateEventWithoutRoomId
|
||||
? StateEventWithoutRoomId
|
||||
: MessageEventWithoutRoomId;
|
||||
|
||||
constructor(
|
||||
event: T extends StateEventWithoutRoomId ? StateEventWithoutRoomId : MessageEventWithoutRoomId
|
||||
) {
|
||||
this.event = event;
|
||||
}
|
||||
}
|
||||
|
||||
export class EncryptedEvent<T extends ClientEventWithoutRoomId> {
|
||||
private readonly roomEncryption: RoomEncryption;
|
||||
|
||||
private readonly publishKey = crypto.randomUUID();
|
||||
|
||||
private decryptPromise: Promise<void> | undefined;
|
||||
|
||||
public readonly roomId: string;
|
||||
|
||||
public readonly event: T;
|
||||
|
||||
public readonly decryption: ObservableValue<
|
||||
EventDecryptionPending | EventDecryptionError | EventDecryptionSuccess<T>
|
||||
>;
|
||||
|
||||
constructor(roomEncryption: RoomEncryption, roomId: string, event: T) {
|
||||
this.roomEncryption = roomEncryption;
|
||||
|
||||
this.roomId = roomId;
|
||||
this.event = event;
|
||||
|
||||
this.decryption = new ObservableValue(new EventDecryptionIdle(), this.publishKey);
|
||||
}
|
||||
|
||||
get eventId(): string {
|
||||
return this.event.event_id;
|
||||
}
|
||||
|
||||
public decrypt(): Promise<void> {
|
||||
if (this.decryptPromise) {
|
||||
return this.decryptPromise;
|
||||
}
|
||||
|
||||
this.decryptPromise = this.runDecryption().finally(() => {
|
||||
this.decryptPromise = undefined;
|
||||
});
|
||||
|
||||
return this.decryptPromise;
|
||||
}
|
||||
|
||||
private async runDecryption(): Promise<void> {
|
||||
this.decryption.publish(this.publishKey, new EventDecryptionPending());
|
||||
|
||||
try {
|
||||
const [err, decryptedEvent] = await this.roomEncryption.decryptEvent<T>(
|
||||
this.roomId,
|
||||
this.event
|
||||
);
|
||||
|
||||
this.decryption.publish(
|
||||
this.publishKey,
|
||||
err ? new EventDecryptionError(err) : new EventDecryptionSuccess<T>(decryptedEvent)
|
||||
);
|
||||
} catch (e) {
|
||||
this.decryption.publish(this.publishKey, new EventDecryptionError(undefined, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
52
src/client/decryption/EncryptedEventIndex.ts
Normal file
52
src/client/decryption/EncryptedEventIndex.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import { ClientEventWithoutRoomId } from 'neurone/lib/api/types';
|
||||
import { EncryptedEvent } from './EncryptedEvent';
|
||||
|
||||
/**
|
||||
* Indexes encrypted events by room, Megolm session, and event ID.
|
||||
*
|
||||
* This index is used to efficiently locate encrypted events that depend on a
|
||||
* particular Megolm session. When new room keys become available, all events
|
||||
* associated with the corresponding room and session can be retrieved and
|
||||
* retried for decryption without scanning every tracked event.
|
||||
*
|
||||
* Structure:
|
||||
*
|
||||
* room_id -> session_id -> event_id -> EncryptedEvent
|
||||
*/
|
||||
export class EncryptedEventIndex<T extends ClientEventWithoutRoomId> {
|
||||
private readonly rooms = new Map<string, Map<string, Map<string, EncryptedEvent<T>>>>();
|
||||
|
||||
get(roomId: string, sessionId: string, eventId: string): EncryptedEvent<T> | undefined {
|
||||
return this.rooms.get(roomId)?.get(sessionId)?.get(eventId);
|
||||
}
|
||||
|
||||
store(roomId: string, sessionId: string, encryptedEvent: EncryptedEvent<T>): void {
|
||||
let sessions = this.rooms.get(roomId);
|
||||
if (!sessions) {
|
||||
sessions = new Map();
|
||||
this.rooms.set(roomId, sessions);
|
||||
}
|
||||
|
||||
let events = sessions.get(sessionId);
|
||||
if (!events) {
|
||||
events = new Map();
|
||||
sessions.set(sessionId, events);
|
||||
}
|
||||
|
||||
events.set(encryptedEvent.eventId, encryptedEvent);
|
||||
}
|
||||
|
||||
getEventsForSession(roomId: string, sessionId: string): EncryptedEvent<T>[] {
|
||||
const events = this.rooms.get(roomId)?.get(sessionId);
|
||||
|
||||
if (!events) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Array.from(events.values());
|
||||
}
|
||||
|
||||
public remove(roomId: string): boolean {
|
||||
return this.rooms.delete(roomId);
|
||||
}
|
||||
}
|
||||
3
src/client/decryption/index.ts
Normal file
3
src/client/decryption/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export * from './DecryptionManager';
|
||||
export * from './EncryptedEvent';
|
||||
export * from './EncryptedEventIndex';
|
||||
Loading…
Reference in a new issue