481 lines
15 KiB
TypeScript
481 lines
15 KiB
TypeScript
import { createClient, MatrixClient, IndexedDBStore, IndexedDBCryptoStore } from 'matrix-js-sdk';
|
|
|
|
import { cryptoCallbacks } from './secretStorageKeys';
|
|
import { clearNavToActivePathStore } from '../app/state/navToActivePath';
|
|
import { Session, getSessionStoreName } from '../app/state/sessions';
|
|
import { getSettings } from '../app/state/settings';
|
|
import { recordPacket } from '../app/state/debugPackets';
|
|
import { promptDialog } from '../app/state/dialog';
|
|
|
|
const MAX_BODY = 8192;
|
|
|
|
const jsonField = (text: string | undefined, key: string) => {
|
|
if (!text) return undefined;
|
|
try {
|
|
const parsed = JSON.parse(text);
|
|
return typeof parsed?.[key] === 'string' ? parsed[key] : undefined;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
};
|
|
|
|
// Bodies are only read from a clone, so the SDK still gets the response it asked for
|
|
const readBody = async (res: Response) => {
|
|
const type = res.headers.get('content-type') ?? '';
|
|
if (!type.includes('json') && !type.includes('text')) return undefined;
|
|
try {
|
|
return (await res.clone().text()).slice(0, MAX_BODY);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
};
|
|
|
|
const debugFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
if (!getSettings().debugPackets) return globalThis.fetch(input, init);
|
|
|
|
// The SDK passes `URL | string`, and a URL instance has href, not url
|
|
const url = input instanceof URL ? input : new URL(typeof input === 'string' ? input : input.url);
|
|
const method = init?.method ?? (input instanceof Request ? input.method : 'GET');
|
|
const req = typeof init?.body === 'string' ? init.body.slice(0, MAX_BODY) : undefined;
|
|
const txnId = jsonField(req, 'transaction_id');
|
|
const start = Date.now();
|
|
|
|
recordPacket({
|
|
ts: start,
|
|
phase: 'start',
|
|
method,
|
|
url: url.toString(),
|
|
path: url.pathname,
|
|
txnId,
|
|
req,
|
|
});
|
|
|
|
let res: Response;
|
|
try {
|
|
res = await globalThis.fetch(input, init);
|
|
} catch (error) {
|
|
recordPacket({
|
|
ts: Date.now(),
|
|
phase: 'end',
|
|
method,
|
|
url: url.toString(),
|
|
path: url.pathname,
|
|
ms: Date.now() - start,
|
|
txnId,
|
|
req,
|
|
error: String(error),
|
|
});
|
|
throw error;
|
|
}
|
|
|
|
const body = await readBody(res);
|
|
recordPacket({
|
|
ts: Date.now(),
|
|
phase: 'end',
|
|
method,
|
|
url: url.toString(),
|
|
path: url.pathname,
|
|
status: res.status,
|
|
ms: Date.now() - start,
|
|
txnId,
|
|
eventId: jsonField(body, 'event_id'),
|
|
req,
|
|
res: body,
|
|
});
|
|
|
|
return res;
|
|
};
|
|
|
|
// Track if we're currently handling a key conflict to prevent loops
|
|
let handlingKeyConflict = false;
|
|
|
|
const deleteIndexedDB = (dbName: string): Promise<void> =>
|
|
new Promise((resolve, reject) => {
|
|
const request = globalThis.indexedDB.deleteDatabase(dbName);
|
|
request.onsuccess = () => {
|
|
console.log(`[initMatrix] Deleted IndexedDB database: ${dbName}`);
|
|
resolve();
|
|
};
|
|
request.onerror = () => {
|
|
console.error(`[initMatrix] Failed to delete IndexedDB database: ${dbName}`, request.error);
|
|
reject(request.error);
|
|
};
|
|
request.onblocked = () => {
|
|
console.warn(`[initMatrix] Delete blocked for IndexedDB database: ${dbName}`);
|
|
// Resolve anyway as the delete might succeed later
|
|
resolve();
|
|
};
|
|
});
|
|
|
|
const getAllIndexedDBNames = async (): Promise<string[]> => {
|
|
try {
|
|
if ('databases' in globalThis.indexedDB) {
|
|
// Add timeout to prevent hanging
|
|
const timeoutPromise = new Promise<string[]>((resolve) => {
|
|
setTimeout(() => {
|
|
console.warn('[initMatrix] indexedDB.databases() timed out after 2s');
|
|
resolve([]);
|
|
}, 2000);
|
|
});
|
|
|
|
const databasesPromise = globalThis.indexedDB
|
|
.databases()
|
|
.then((dbs) => dbs.map((db) => db.name).filter((name) => typeof name === 'string'));
|
|
|
|
return await Promise.race([databasesPromise, timeoutPromise]);
|
|
}
|
|
} catch (error) {
|
|
console.warn('[initMatrix] Error getting IndexedDB names:', error);
|
|
}
|
|
// Fallback for browsers that don't support indexedDB.databases()
|
|
return [];
|
|
};
|
|
|
|
/**
|
|
* Delete the Rust crypto database to fix one-time key conflicts.
|
|
* This happens when the local crypto state has keys that conflict with server state.
|
|
*/
|
|
const deleteRustCryptoDatabase = async (session: Session): Promise<boolean> => {
|
|
console.log('[initMatrix] 🔑 Attempting to delete Rust crypto database to fix key conflicts...');
|
|
|
|
const dbNames = await getAllIndexedDBNames();
|
|
const storagePrefix = `${session.userId}@${session.deviceId}`;
|
|
|
|
// Find crypto databases for this session
|
|
const cryptoDbNames = dbNames.filter(
|
|
(name) => name.includes('matrix-sdk-crypto') || name.includes(storagePrefix),
|
|
);
|
|
|
|
if (cryptoDbNames.length === 0) {
|
|
console.log('[initMatrix] No crypto databases found to delete');
|
|
return false;
|
|
}
|
|
|
|
console.log('[initMatrix] 🗑️ Deleting crypto databases:', cryptoDbNames);
|
|
|
|
for (let i = 0; i < cryptoDbNames.length; i += 1) {
|
|
const dbName = cryptoDbNames[i];
|
|
try {
|
|
await deleteIndexedDB(dbName);
|
|
console.log(`[initMatrix] ✅ Deleted: ${dbName}`);
|
|
} catch (error) {
|
|
console.error(`[initMatrix] ❌ Failed to delete ${dbName}:`, error);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Setup error listener to detect one-time key conflicts and auto-fix them.
|
|
*/
|
|
const setupKeyConflictHandler = (mx: MatrixClient, session: Session): void => {
|
|
// Listen for HTTP errors from the client
|
|
let keyConflictCount = 0;
|
|
const KEY_CONFLICT_THRESHOLD = 3;
|
|
|
|
// Patch console.log/error to detect the key conflict errors
|
|
const keyConflictPattern = /One time key.*already exists/;
|
|
|
|
// We need to use a different approach - listen to the crypto module
|
|
// Since we can't directly intercept, we'll use a MutationObserver-style approach
|
|
// by checking for repeated key upload failures
|
|
|
|
const checkKeyConflict = (args: unknown[]): boolean => {
|
|
const message = args.map((a) => String(a)).join(' ');
|
|
return keyConflictPattern.test(message);
|
|
};
|
|
|
|
// Override console methods temporarily to detect the errors
|
|
const originalError = console.error;
|
|
const originalWarn = console.warn;
|
|
|
|
const errorHandler = (...args: unknown[]) => {
|
|
if (checkKeyConflict(args)) {
|
|
keyConflictCount += 1;
|
|
console.log(
|
|
`[initMatrix] ⚠️ Detected key conflict (${keyConflictCount}/${KEY_CONFLICT_THRESHOLD})`,
|
|
);
|
|
|
|
if (keyConflictCount >= KEY_CONFLICT_THRESHOLD && !handlingKeyConflict) {
|
|
handlingKeyConflict = true;
|
|
console.log('[initMatrix] 🔄 Too many key conflicts detected, attempting auto-fix...');
|
|
|
|
// Schedule the fix for next page load to avoid issues during active session
|
|
localStorage.setItem(
|
|
'cinny_fix_key_conflict',
|
|
JSON.stringify({
|
|
userId: session.userId,
|
|
deviceId: session.deviceId,
|
|
timestamp: Date.now(),
|
|
}),
|
|
);
|
|
|
|
// Restore console methods
|
|
console.error = originalError;
|
|
console.warn = originalWarn;
|
|
|
|
// Notify user and reload
|
|
setTimeout(() => {
|
|
promptDialog(
|
|
'Detected encryption key conflict. The app needs to reset your encryption session. You may need to re-verify your devices after this. Continue?',
|
|
true,
|
|
).then((continueFix) => {
|
|
if (continueFix) {
|
|
deleteRustCryptoDatabase(session).then(() => {
|
|
window.location.reload();
|
|
});
|
|
} else {
|
|
handlingKeyConflict = false;
|
|
}
|
|
});
|
|
}, 100);
|
|
}
|
|
}
|
|
originalError.apply(console, args);
|
|
};
|
|
|
|
console.error = errorHandler;
|
|
console.warn = (...args: unknown[]) => {
|
|
if (checkKeyConflict(args)) {
|
|
keyConflictCount += 1;
|
|
}
|
|
originalWarn.apply(console, args);
|
|
};
|
|
};
|
|
|
|
export const initClient = async (session: Session): Promise<MatrixClient> => {
|
|
const storeName = getSessionStoreName(session);
|
|
|
|
// Check if we need to fix a key conflict from a previous session
|
|
const pendingFix = localStorage.getItem('cinny_fix_key_conflict');
|
|
if (pendingFix) {
|
|
try {
|
|
const fixData = JSON.parse(pendingFix);
|
|
if (fixData.userId === session.userId && fixData.deviceId === session.deviceId) {
|
|
console.log('[initMatrix] 🔧 Found pending key conflict fix, clearing crypto database...');
|
|
await deleteRustCryptoDatabase(session);
|
|
localStorage.removeItem('cinny_fix_key_conflict');
|
|
console.log('[initMatrix] ✅ Crypto database cleared');
|
|
}
|
|
} catch {
|
|
localStorage.removeItem('cinny_fix_key_conflict');
|
|
}
|
|
}
|
|
|
|
console.log('[initMatrix] ========================================');
|
|
console.log('[initMatrix] Initializing Matrix client for session:');
|
|
console.log('[initMatrix] User ID:', session.userId);
|
|
console.log('[initMatrix] Device ID:', session.deviceId);
|
|
console.log('[initMatrix] Using fallback stores:', session.fallbackSdkStores || false);
|
|
console.log('[initMatrix] Sync store name:', storeName.sync);
|
|
console.log('[initMatrix] Crypto store name:', storeName.crypto);
|
|
console.log('[initMatrix] ========================================');
|
|
|
|
console.log('[initMatrix] Creating Matrix client...');
|
|
|
|
try {
|
|
const indexedDBStore = new IndexedDBStore({
|
|
indexedDB: globalThis.indexedDB,
|
|
localStorage: globalThis.localStorage,
|
|
dbName: storeName.sync,
|
|
});
|
|
|
|
const legacyCryptoStore = new IndexedDBCryptoStore(globalThis.indexedDB, storeName.crypto);
|
|
|
|
const mx = createClient({
|
|
baseUrl: session.baseUrl,
|
|
accessToken: session.accessToken,
|
|
userId: session.userId,
|
|
store: indexedDBStore,
|
|
cryptoStore: legacyCryptoStore,
|
|
deviceId: session.deviceId,
|
|
timelineSupport: true,
|
|
cryptoCallbacks: cryptoCallbacks as any,
|
|
verificationMethods: ['m.sas.v1'],
|
|
fetchFn: debugFetch,
|
|
});
|
|
|
|
await indexedDBStore.startup();
|
|
|
|
// Initialize Rust crypto with a unique store prefix for this account
|
|
// This ensures each account has its own isolated crypto database
|
|
console.log(
|
|
'[initMatrix] Initializing Rust crypto with prefix:',
|
|
`${session.userId}@${session.deviceId}`,
|
|
);
|
|
await mx.initRustCrypto({
|
|
storagePrefix: `${session.userId}@${session.deviceId}`,
|
|
});
|
|
|
|
console.log('[initMatrix] ✅ Client initialized successfully!');
|
|
|
|
mx.setMaxListeners(50);
|
|
|
|
// Increase max listeners for User objects to prevent warnings when many components
|
|
// subscribe to presence events (e.g., in member lists)
|
|
const originalGetUser = mx.getUser.bind(mx);
|
|
mx.getUser = (userId: string) => {
|
|
const user = originalGetUser(userId);
|
|
if (user) {
|
|
user.setMaxListeners(50);
|
|
}
|
|
return user;
|
|
};
|
|
|
|
// Setup handler to detect and auto-fix one-time key conflicts
|
|
setupKeyConflictHandler(mx, session);
|
|
|
|
// Register this as the global MatrixClient so auth utils can access the latest token
|
|
const { setGlobalMatrixClient } = await import('../app/utils/auth');
|
|
setGlobalMatrixClient(mx);
|
|
console.log('[initMatrix] Registered global MatrixClient for auth utils');
|
|
|
|
return mx;
|
|
} catch (error) {
|
|
// Handle crypto store account mismatch error
|
|
if (error instanceof Error && error.message.includes("account in the store doesn't match")) {
|
|
console.error('[initMatrix] ❌ Crypto store account mismatch detected!');
|
|
console.error('[initMatrix] Error details:', error.message);
|
|
console.error('[initMatrix] Expected account:', session.userId, session.deviceId);
|
|
|
|
// This shouldn't happen anymore since we delete the shared db before init
|
|
// If it does happen, it means there's a different issue
|
|
throw new Error(
|
|
`❌ Crypto store conflict detected.\n\n` +
|
|
`This account's crypto store contains data from a different account.\n` +
|
|
`Please clear site data in DevTools (F12 → Application → Storage → Clear site data)`,
|
|
{ cause: error },
|
|
);
|
|
}
|
|
|
|
// Re-throw other errors
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const startClient = async (mx: MatrixClient) => {
|
|
await mx.startClient({
|
|
lazyLoadMembers: true,
|
|
});
|
|
};
|
|
|
|
export const clearCacheAndReload = async (mx: MatrixClient) => {
|
|
mx.stopClient();
|
|
clearNavToActivePathStore(mx.getSafeUserId());
|
|
await mx.store.deleteAllData();
|
|
window.location.reload();
|
|
};
|
|
|
|
/**
|
|
* Fix one-time key conflicts by clearing the Rust crypto database.
|
|
* Call this when you see repeated "One time key already exists" errors.
|
|
* The user will need to re-verify their devices after this.
|
|
*/
|
|
export const fixKeyConflict = async (mx: MatrixClient): Promise<void> => {
|
|
const userId = mx.getSafeUserId();
|
|
const deviceId = mx.getDeviceId();
|
|
|
|
if (!userId || !deviceId) {
|
|
console.error('[initMatrix] Cannot fix key conflict: missing userId or deviceId');
|
|
return;
|
|
}
|
|
|
|
console.log('[initMatrix] 🔧 Fixing key conflict for', userId, deviceId);
|
|
|
|
// Stop the client first
|
|
mx.stopClient();
|
|
|
|
// Delete the crypto databases
|
|
const dbNames = await getAllIndexedDBNames();
|
|
const storagePrefix = `${userId}@${deviceId}`;
|
|
|
|
const cryptoDbNames = dbNames.filter(
|
|
(name) =>
|
|
name.includes('matrix-sdk-crypto') || name.includes(storagePrefix) || name.includes('crypto'),
|
|
);
|
|
|
|
console.log('[initMatrix] 🗑️ Deleting crypto databases:', cryptoDbNames);
|
|
|
|
for (let i = 0; i < cryptoDbNames.length; i += 1) {
|
|
const dbName = cryptoDbNames[i];
|
|
try {
|
|
await deleteIndexedDB(dbName);
|
|
console.log(`[initMatrix] ✅ Deleted: ${dbName}`);
|
|
} catch (error) {
|
|
console.error(`[initMatrix] ❌ Failed to delete ${dbName}:`, error);
|
|
}
|
|
}
|
|
|
|
// Reload the page
|
|
console.log('[initMatrix] 🔄 Reloading...');
|
|
window.location.reload();
|
|
};
|
|
|
|
export const logoutClient = async (mx: MatrixClient) => {
|
|
mx.stopClient();
|
|
|
|
// Clear the global MatrixClient reference
|
|
const { setGlobalMatrixClient } = await import('../app/utils/auth');
|
|
setGlobalMatrixClient(null);
|
|
|
|
try {
|
|
await mx.logout();
|
|
} catch {
|
|
// ignore if failed to logout
|
|
}
|
|
await mx.clearStores();
|
|
|
|
const { getLocalStorageItem, setLocalStorageItem } =
|
|
await import('../app/state/utils/atomWithLocalStorage');
|
|
|
|
const userId = mx.getSafeUserId();
|
|
const allSessions = getLocalStorageItem('matrixSessions', []);
|
|
const currentUserId = getLocalStorageItem('currentSessionUserId', undefined);
|
|
|
|
// Remove the current session
|
|
const remainingSessions = allSessions.filter((s: Session) => s.userId !== userId);
|
|
setLocalStorageItem('matrixSessions', remainingSessions);
|
|
|
|
// If we're logging out the current session and there are other sessions, switch to the first one
|
|
if (remainingSessions.length > 0 && currentUserId === userId) {
|
|
setLocalStorageItem('currentSessionUserId', remainingSessions[0].userId);
|
|
window.location.reload();
|
|
} else if (remainingSessions.length === 0) {
|
|
// No more sessions, clear everything
|
|
window.localStorage.clear();
|
|
window.location.reload();
|
|
} else {
|
|
// We logged out a non-current session, just reload
|
|
window.location.reload();
|
|
}
|
|
};
|
|
|
|
export const logoutAllSessions = async () => {
|
|
window.localStorage.clear();
|
|
|
|
// Clear all IndexedDB databases
|
|
const dbs = await window.indexedDB.databases();
|
|
dbs.forEach((idbInfo) => {
|
|
if (idbInfo.name) {
|
|
window.indexedDB.deleteDatabase(idbInfo.name);
|
|
}
|
|
});
|
|
|
|
window.location.reload();
|
|
};
|
|
|
|
export const clearLoginData = async () => {
|
|
const dbs = await window.indexedDB.databases();
|
|
|
|
dbs.forEach((idbInfo) => {
|
|
const { name } = idbInfo;
|
|
if (name) {
|
|
window.indexedDB.deleteDatabase(name);
|
|
}
|
|
});
|
|
|
|
window.localStorage.clear();
|
|
window.location.reload();
|
|
};
|