605 lines
17 KiB
TypeScript
605 lines
17 KiB
TypeScript
import Redis from 'ioredis';
|
|
import CryptoJS from 'crypto-js';
|
|
|
|
// Initialize Redis client
|
|
let redisClient: Redis | null = null;
|
|
let isConnecting = false;
|
|
let connectionAttempts = 0;
|
|
const MAX_RECONNECT_ATTEMPTS = 5;
|
|
|
|
/**
|
|
* Get a Redis client instance (singleton pattern) with improved connection management
|
|
*/
|
|
export function getRedisClient(): Redis {
|
|
if (redisClient && redisClient.status === 'ready') {
|
|
return redisClient;
|
|
}
|
|
|
|
if (isConnecting) {
|
|
// If we're already trying to connect, return the existing client
|
|
// This prevents multiple simultaneous connection attempts
|
|
if (redisClient) return redisClient;
|
|
|
|
// This is a fallback in case we're connecting but don't have a client yet
|
|
console.warn('Redis connection in progress, creating temporary client');
|
|
}
|
|
|
|
if (!redisClient) {
|
|
isConnecting = true;
|
|
connectionAttempts = 0;
|
|
|
|
// Set Redis connection parameters from environment variables only
|
|
const redisOptions = {
|
|
host: process.env.REDIS_HOST,
|
|
port: process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT) : undefined,
|
|
password: process.env.REDIS_PASSWORD,
|
|
retryStrategy: (times: number) => {
|
|
connectionAttempts = times;
|
|
if (times > MAX_RECONNECT_ATTEMPTS) {
|
|
console.error(`Redis connection failed after ${times} attempts, giving up`);
|
|
return null; // Stop trying to reconnect
|
|
}
|
|
const delay = Math.min(times * 100, 5000);
|
|
console.log(`Redis reconnect attempt ${times}, retrying in ${delay}ms`);
|
|
return delay;
|
|
},
|
|
maxRetriesPerRequest: 5,
|
|
enableOfflineQueue: true,
|
|
connectTimeout: 10000, // 10 seconds
|
|
disconnectTimeout: 2000, // 2 seconds
|
|
keepAlive: 10000, // 10 seconds
|
|
keyPrefix: '' // No prefix to keep keys clean
|
|
};
|
|
|
|
console.log('Connecting to Redis using environment variables');
|
|
redisClient = new Redis(redisOptions);
|
|
|
|
redisClient.on('error', (err) => {
|
|
console.error('Redis connection error:', err);
|
|
|
|
// Only set to null if we've exceeded max attempts
|
|
if (connectionAttempts > MAX_RECONNECT_ATTEMPTS) {
|
|
console.error('Redis connection failed permanently, will create new client on next request');
|
|
redisClient = null;
|
|
isConnecting = false;
|
|
}
|
|
});
|
|
|
|
redisClient.on('connect', () => {
|
|
console.log('Successfully connected to Redis');
|
|
isConnecting = false;
|
|
connectionAttempts = 0;
|
|
});
|
|
|
|
redisClient.on('reconnecting', () => {
|
|
console.log('Reconnecting to Redis...');
|
|
isConnecting = true;
|
|
});
|
|
|
|
redisClient.on('ready', () => {
|
|
console.log('Redis connection warmed up');
|
|
isConnecting = false;
|
|
});
|
|
|
|
redisClient.on('end', () => {
|
|
console.log('Redis connection ended');
|
|
// Don't set to null here - let the error handler decide
|
|
});
|
|
}
|
|
|
|
return redisClient;
|
|
}
|
|
|
|
/**
|
|
* Close Redis connection (useful for serverless environments)
|
|
*/
|
|
export async function closeRedisConnection(): Promise<void> {
|
|
if (redisClient) {
|
|
await redisClient.quit();
|
|
redisClient = null;
|
|
}
|
|
}
|
|
|
|
// Encryption key from environment variable or fallback
|
|
const getEncryptionKey = () => {
|
|
return process.env.REDIS_ENCRYPTION_KEY || 'default-encryption-key-change-in-production';
|
|
};
|
|
|
|
/**
|
|
* Encrypt sensitive data before storing in Redis
|
|
*/
|
|
export function encryptData(data: string): string {
|
|
return CryptoJS.AES.encrypt(data, getEncryptionKey()).toString();
|
|
}
|
|
|
|
/**
|
|
* Decrypt sensitive data retrieved from Redis
|
|
*/
|
|
export function decryptData(encryptedData: string): string {
|
|
const bytes = CryptoJS.AES.decrypt(encryptedData, getEncryptionKey());
|
|
return bytes.toString(CryptoJS.enc.Utf8);
|
|
}
|
|
|
|
// Cache key definitions
|
|
export const KEYS = {
|
|
CREDENTIALS: (userId: string, accountId: string) => `email:credentials:${userId}:${accountId}`,
|
|
SESSION: (userId: string) => `email:session:${userId}`,
|
|
EMAIL_LIST: (userId: string, accountId: string, folder: string, page: number, perPage: number) =>
|
|
`email:list:${userId}:${accountId}:${folder}:${page}:${perPage}`,
|
|
EMAIL_CONTENT: (userId: string, accountId: string, emailId: string) =>
|
|
`email:content:${userId}:${accountId}:${emailId}`
|
|
};
|
|
|
|
// TTL constants in seconds
|
|
export const TTL = {
|
|
CREDENTIALS: 60 * 60 * 24, // 24 hours
|
|
SESSION: 60 * 60 * 4, // 4 hours (increased from 30 minutes)
|
|
EMAIL_LIST: 60 * 5, // 5 minutes
|
|
EMAIL_CONTENT: 60 * 15 // 15 minutes
|
|
};
|
|
|
|
interface EmailCredentials {
|
|
email: string;
|
|
password?: string;
|
|
host: string;
|
|
port: number;
|
|
secure?: boolean;
|
|
encryptedPassword?: string;
|
|
smtp_host?: string;
|
|
smtp_port?: number;
|
|
smtp_secure?: boolean;
|
|
display_name?: string;
|
|
color?: string;
|
|
useOAuth?: boolean;
|
|
accessToken?: string;
|
|
refreshToken?: string;
|
|
tokenExpiry?: number;
|
|
}
|
|
|
|
interface ImapSessionData {
|
|
connectionId?: string;
|
|
lastActive: number;
|
|
mailboxes?: string[];
|
|
lastVisit?: number;
|
|
defaultAccountId?: string;
|
|
}
|
|
|
|
/**
|
|
* Cache email credentials in Redis
|
|
*/
|
|
export async function cacheEmailCredentials(
|
|
userId: string,
|
|
accountId: string,
|
|
credentials: EmailCredentials
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.CREDENTIALS(userId, accountId);
|
|
|
|
// Validate credentials before caching
|
|
if (!credentials.email || !credentials.host || (!credentials.password && !credentials.useOAuth)) {
|
|
console.error(`Cannot cache incomplete credentials for user ${userId}`);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
console.log(`Caching credentials for user ${userId}`);
|
|
|
|
// Create a copy without the password to store
|
|
const secureCredentials: EmailCredentials = {
|
|
email: credentials.email,
|
|
host: credentials.host,
|
|
port: credentials.port,
|
|
secure: credentials.secure ?? true,
|
|
// Include the extended fields
|
|
...(credentials.smtp_host && { smtp_host: credentials.smtp_host }),
|
|
...(credentials.smtp_port && { smtp_port: credentials.smtp_port }),
|
|
...(credentials.smtp_secure !== undefined && { smtp_secure: credentials.smtp_secure }),
|
|
...(credentials.display_name && { display_name: credentials.display_name }),
|
|
...(credentials.color && { color: credentials.color }),
|
|
// Include OAuth fields
|
|
...(credentials.useOAuth !== undefined && { useOAuth: credentials.useOAuth }),
|
|
...(credentials.accessToken && { accessToken: credentials.accessToken }),
|
|
...(credentials.refreshToken && { refreshToken: credentials.refreshToken }),
|
|
...(credentials.tokenExpiry && { tokenExpiry: credentials.tokenExpiry })
|
|
};
|
|
|
|
// Encrypt password if provided
|
|
if (credentials.password) {
|
|
try {
|
|
const encrypted = encryptData(credentials.password);
|
|
console.log(`Successfully encrypted password for user ${userId}`);
|
|
secureCredentials.encryptedPassword = encrypted;
|
|
} catch (encryptError) {
|
|
console.error(`Failed to encrypt password for user ${userId}:`, encryptError);
|
|
// Continue anyway since we might have OAuth tokens
|
|
}
|
|
}
|
|
|
|
await redis.set(key, JSON.stringify(secureCredentials), 'EX', TTL.CREDENTIALS);
|
|
console.log(`Credentials cached for user ${userId}`);
|
|
} catch (error) {
|
|
console.error(`Error caching credentials for user ${userId}:`, error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get email credentials from Redis
|
|
*/
|
|
export async function getEmailCredentials(
|
|
userId: string,
|
|
accountId: string
|
|
): Promise<EmailCredentials | null> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.CREDENTIALS(userId, accountId);
|
|
|
|
try {
|
|
const credStr = await redis.get(key);
|
|
|
|
if (!credStr) {
|
|
return null;
|
|
}
|
|
|
|
const creds = JSON.parse(credStr) as EmailCredentials;
|
|
|
|
let password: string | undefined;
|
|
|
|
// Handle OAuth accounts (they might not have a password)
|
|
if (creds.encryptedPassword) {
|
|
try {
|
|
// Decrypt the password
|
|
password = decryptData(creds.encryptedPassword);
|
|
} catch (decryptError) {
|
|
console.error(`Failed to decrypt password for user ${userId}:`, decryptError);
|
|
// For OAuth accounts, we can continue without a password
|
|
if (!creds.useOAuth) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Return the full credentials with decrypted password if available
|
|
const result: EmailCredentials = {
|
|
email: creds.email,
|
|
host: creds.host,
|
|
port: creds.port,
|
|
secure: creds.secure ?? true,
|
|
...(password && { password }),
|
|
...(creds.smtp_host && { smtp_host: creds.smtp_host }),
|
|
...(creds.smtp_port && { smtp_port: creds.smtp_port }),
|
|
...(creds.smtp_secure !== undefined && { smtp_secure: creds.smtp_secure }),
|
|
...(creds.display_name && { display_name: creds.display_name }),
|
|
...(creds.color && { color: creds.color }),
|
|
// Include OAuth fields
|
|
...(creds.useOAuth !== undefined && { useOAuth: creds.useOAuth }),
|
|
...(creds.accessToken && { accessToken: creds.accessToken }),
|
|
...(creds.refreshToken && { refreshToken: creds.refreshToken }),
|
|
...(creds.tokenExpiry && { tokenExpiry: creds.tokenExpiry })
|
|
};
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error(`Error getting credentials for user ${userId}:`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cache IMAP session data for quick reconnection
|
|
*/
|
|
export async function cacheImapSession(
|
|
userId: string,
|
|
sessionData: ImapSessionData
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.SESSION(userId);
|
|
|
|
// Always update the lastActive timestamp
|
|
sessionData.lastActive = Date.now();
|
|
|
|
await redis.set(key, JSON.stringify(sessionData), 'EX', TTL.SESSION);
|
|
}
|
|
|
|
/**
|
|
* Get cached IMAP session data
|
|
*/
|
|
export async function getCachedImapSession(
|
|
userId: string
|
|
): Promise<ImapSessionData | null> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.SESSION(userId);
|
|
|
|
const cachedData = await redis.get(key);
|
|
if (!cachedData) return null;
|
|
|
|
return JSON.parse(cachedData) as ImapSessionData;
|
|
}
|
|
|
|
/**
|
|
* Cache email list in Redis
|
|
*/
|
|
export async function cacheEmailList(
|
|
userId: string,
|
|
accountId: string,
|
|
folder: string,
|
|
page: number,
|
|
perPage: number,
|
|
data: any
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.EMAIL_LIST(userId, accountId, folder, page, perPage);
|
|
|
|
await redis.set(key, JSON.stringify(data), 'EX', TTL.EMAIL_LIST);
|
|
}
|
|
|
|
/**
|
|
* Get cached email list from Redis
|
|
*/
|
|
export async function getCachedEmailList(
|
|
userId: string,
|
|
accountId: string,
|
|
folder: string,
|
|
page: number,
|
|
perPage: number
|
|
): Promise<any | null> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.EMAIL_LIST(userId, accountId, folder, page, perPage);
|
|
|
|
const cachedData = await redis.get(key);
|
|
if (!cachedData) return null;
|
|
|
|
return JSON.parse(cachedData);
|
|
}
|
|
|
|
/**
|
|
* Cache email content in Redis
|
|
*/
|
|
export async function cacheEmailContent(
|
|
userId: string,
|
|
accountId: string,
|
|
emailId: string,
|
|
data: any
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.EMAIL_CONTENT(userId, accountId, emailId);
|
|
|
|
await redis.set(key, JSON.stringify(data), 'EX', TTL.EMAIL_CONTENT);
|
|
}
|
|
|
|
/**
|
|
* Get cached email content from Redis
|
|
*/
|
|
export async function getCachedEmailContent(
|
|
userId: string,
|
|
accountId: string,
|
|
emailId: string
|
|
): Promise<any | null> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.EMAIL_CONTENT(userId, accountId, emailId);
|
|
|
|
const cachedData = await redis.get(key);
|
|
if (!cachedData) return null;
|
|
|
|
return JSON.parse(cachedData);
|
|
}
|
|
|
|
/**
|
|
* Invalidate all email caches for a folder
|
|
*/
|
|
export async function invalidateFolderCache(
|
|
userId: string,
|
|
accountId: string,
|
|
folder: string
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const pattern = `email:list:${userId}:${accountId}:${folder}:*`;
|
|
|
|
// Use SCAN to find and delete keys matching the pattern
|
|
let cursor = '0';
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
|
|
cursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
await redis.del(...keys);
|
|
}
|
|
} while (cursor !== '0');
|
|
}
|
|
|
|
/**
|
|
* Invalidate email content cache
|
|
*/
|
|
export async function invalidateEmailContentCache(
|
|
userId: string,
|
|
accountId: string,
|
|
emailId: string
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
const key = KEYS.EMAIL_CONTENT(userId, accountId, emailId);
|
|
|
|
await redis.del(key);
|
|
}
|
|
|
|
/**
|
|
* Warm up Redis connection to avoid cold starts
|
|
*/
|
|
export async function warmupRedisCache(): Promise<boolean> {
|
|
try {
|
|
// Ping Redis to establish connection early
|
|
const redis = getRedisClient();
|
|
await redis.ping();
|
|
console.log('Redis connection warmed up');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error warming up Redis:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Redis connection status
|
|
*/
|
|
export async function getRedisStatus(): Promise<{
|
|
status: 'connected' | 'error';
|
|
ping?: string;
|
|
error?: string;
|
|
}> {
|
|
try {
|
|
const redis = getRedisClient();
|
|
const pong = await redis.ping();
|
|
return {
|
|
status: 'connected',
|
|
ping: pong
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
status: 'error',
|
|
error: error instanceof Error ? error.message : String(error)
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Invalidate all user email caches (email lists and content)
|
|
*/
|
|
export async function invalidateUserEmailCache(
|
|
userId: string
|
|
): Promise<void> {
|
|
const redis = getRedisClient();
|
|
|
|
// Patterns to delete
|
|
const patterns = [
|
|
`email:list:${userId}:*`,
|
|
`email:content:${userId}:*`
|
|
];
|
|
|
|
for (const pattern of patterns) {
|
|
let cursor = '0';
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
|
|
cursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
await redis.del(...keys);
|
|
}
|
|
} while (cursor !== '0');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get cached email credentials from Redis
|
|
* @deprecated Use getEmailCredentials instead
|
|
*/
|
|
export async function getCachedEmailCredentials(
|
|
userId: string,
|
|
accountId: string
|
|
): Promise<EmailCredentials | null> {
|
|
return getEmailCredentials(userId, accountId);
|
|
}
|
|
|
|
/**
|
|
* Cleans up all Redis data related to a user during logout
|
|
* This is critical to ensure proper session cleanup and prevent auth conflicts
|
|
*/
|
|
export async function cleanupUserSessions(userId: string): Promise<void> {
|
|
if (!userId) {
|
|
console.error('Cannot cleanup sessions: Missing userId');
|
|
return;
|
|
}
|
|
|
|
console.log(`Performing complete Redis cleanup for user ${userId}`);
|
|
const redis = getRedisClient();
|
|
|
|
try {
|
|
// 1. First get all the user's email accounts to ensure we clean everything
|
|
const userAccountPattern = `email:credentials:${userId}:*`;
|
|
const accountKeys: string[] = [];
|
|
|
|
// Use SCAN to find all account keys
|
|
let cursor = '0';
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(cursor, 'MATCH', userAccountPattern, 'COUNT', 100);
|
|
cursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
accountKeys.push(...keys);
|
|
}
|
|
} while (cursor !== '0');
|
|
|
|
console.log(`Found ${accountKeys.length} email accounts for user ${userId}`);
|
|
|
|
// Extract accountIds from the keys
|
|
const accountIds = accountKeys.map(key => {
|
|
const parts = key.split(':');
|
|
return parts[3]; // Format is email:credentials:userId:accountId
|
|
});
|
|
|
|
// 2. Remove email content and email list caches for each account
|
|
for (const accountId of accountIds) {
|
|
// Delete email lists
|
|
const emailListPattern = `email:list:${userId}:${accountId}:*`;
|
|
let listCursor = '0';
|
|
let deletedEmailLists = 0;
|
|
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(listCursor, 'MATCH', emailListPattern, 'COUNT', 100);
|
|
listCursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
await redis.del(...keys);
|
|
deletedEmailLists += keys.length;
|
|
}
|
|
} while (listCursor !== '0');
|
|
|
|
// Delete email content
|
|
const emailContentPattern = `email:content:${userId}:${accountId}:*`;
|
|
let contentCursor = '0';
|
|
let deletedEmailContent = 0;
|
|
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(contentCursor, 'MATCH', emailContentPattern, 'COUNT', 100);
|
|
contentCursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
await redis.del(...keys);
|
|
deletedEmailContent += keys.length;
|
|
}
|
|
} while (contentCursor !== '0');
|
|
|
|
console.log(`Cleaned up ${deletedEmailLists} email lists and ${deletedEmailContent} email content items for account ${accountId}`);
|
|
}
|
|
|
|
// 3. Remove credential entries
|
|
if (accountKeys.length > 0) {
|
|
await redis.del(...accountKeys);
|
|
console.log(`Removed ${accountKeys.length} credential entries`);
|
|
}
|
|
|
|
// 4. Remove IMAP session
|
|
const sessionKey = KEYS.SESSION(userId);
|
|
await redis.del(sessionKey);
|
|
console.log(`Removed IMAP session data for user ${userId}`);
|
|
|
|
// 5. Remove any other user-specific data that might be present
|
|
const otherUserDataPattern = `*:${userId}:*`;
|
|
let otherCursor = '0';
|
|
let deletedOtherData = 0;
|
|
|
|
do {
|
|
const [nextCursor, keys] = await redis.scan(otherCursor, 'MATCH', otherUserDataPattern, 'COUNT', 100);
|
|
otherCursor = nextCursor;
|
|
|
|
if (keys.length > 0) {
|
|
await redis.del(...keys);
|
|
deletedOtherData += keys.length;
|
|
}
|
|
} while (otherCursor !== '0');
|
|
|
|
if (deletedOtherData > 0) {
|
|
console.log(`Removed ${deletedOtherData} additional user-specific entries`);
|
|
}
|
|
|
|
console.log(`Successfully completed Redis cleanup for user ${userId}`);
|
|
} catch (error) {
|
|
console.error(`Error during Redis cleanup for user ${userId}:`, error);
|
|
}
|
|
}
|