2026-02-01 15:07:02 +08:00
|
|
|
const { app, BrowserWindow, ipcMain, clipboard, Menu, desktopCapturer, screen } = require('electron');
|
2026-01-28 14:56:28 +08:00
|
|
|
const path = require('path');
|
2026-02-01 15:07:02 +08:00
|
|
|
const fs = require('fs');
|
2026-01-28 14:56:28 +08:00
|
|
|
const Database = require('./src/db/database');
|
|
|
|
|
|
2026-02-01 15:07:02 +08:00
|
|
|
// Force userData path to project root/data folder
|
|
|
|
|
// Prevents Electron cache/localStorage/logs from writing to C drive
|
2026-01-28 14:56:28 +08:00
|
|
|
app.setPath('userData', path.join(process.cwd(), 'data'));
|
|
|
|
|
|
|
|
|
|
let mainWindow;
|
|
|
|
|
let db;
|
2026-02-01 15:07:02 +08:00
|
|
|
let backupInterval;
|
|
|
|
|
|
|
|
|
|
// Auto backup feature
|
|
|
|
|
function startAutoBackup() {
|
|
|
|
|
const backupDir = path.join(process.cwd(), 'DB');
|
|
|
|
|
const dbPath = path.join(process.cwd(), 'accounts.db');
|
|
|
|
|
|
|
|
|
|
// Ensure backup directory exists
|
|
|
|
|
if (!fs.existsSync(backupDir)) {
|
|
|
|
|
fs.mkdirSync(backupDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Backup every 30 minutes
|
|
|
|
|
backupInterval = setInterval(() => {
|
|
|
|
|
try {
|
|
|
|
|
if (fs.existsSync(dbPath)) {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const timestamp = now.getFullYear().toString() +
|
|
|
|
|
(now.getMonth() + 1).toString().padStart(2, '0') +
|
|
|
|
|
now.getDate().toString().padStart(2, '0') + '_' +
|
|
|
|
|
now.getHours().toString().padStart(2, '0') +
|
|
|
|
|
now.getMinutes().toString().padStart(2, '0') +
|
|
|
|
|
now.getSeconds().toString().padStart(2, '0');
|
|
|
|
|
|
|
|
|
|
const backupPath = path.join(backupDir, `accounts_${timestamp}.db`);
|
|
|
|
|
fs.copyFileSync(dbPath, backupPath);
|
|
|
|
|
console.log(`[Backup] Auto backup success: ${backupPath}`);
|
|
|
|
|
|
|
|
|
|
// Clean old backups, keep last 10
|
|
|
|
|
cleanOldBackups(backupDir, 10);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('[Backup] Backup failed:', e);
|
|
|
|
|
}
|
|
|
|
|
}, 30 * 60 * 1000); // 30 minutes
|
|
|
|
|
|
|
|
|
|
console.log('[Backup] Auto backup started, interval 30 minutes');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clean old backups
|
|
|
|
|
function cleanOldBackups(backupDir, keepCount) {
|
|
|
|
|
try {
|
|
|
|
|
const files = fs.readdirSync(backupDir)
|
|
|
|
|
.filter(f => f.startsWith('accounts_') && f.endsWith('.db'))
|
|
|
|
|
.map(f => ({ name: f, path: path.join(backupDir, f), time: fs.statSync(path.join(backupDir, f)).mtime }))
|
|
|
|
|
.sort((a, b) => b.time - a.time);
|
|
|
|
|
|
|
|
|
|
if (files.length > keepCount) {
|
|
|
|
|
files.slice(keepCount).forEach(f => {
|
|
|
|
|
fs.unlinkSync(f.path);
|
|
|
|
|
console.log(`[Backup] Deleted old backup: ${f.name}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('[Backup] Clean old backups failed:', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-28 14:56:28 +08:00
|
|
|
|
|
|
|
|
function createWindow() {
|
2026-02-01 15:07:02 +08:00
|
|
|
// Remove application menu bar
|
2026-01-28 14:56:28 +08:00
|
|
|
Menu.setApplicationMenu(null);
|
|
|
|
|
|
|
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
|
width: 1200,
|
|
|
|
|
height: 800,
|
|
|
|
|
minWidth: 900,
|
|
|
|
|
minHeight: 600,
|
|
|
|
|
webPreferences: {
|
|
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
|
contextIsolation: true,
|
2026-02-01 15:07:02 +08:00
|
|
|
nodeIntegration: false,
|
|
|
|
|
sandbox: false
|
2026-01-28 14:56:28 +08:00
|
|
|
},
|
|
|
|
|
backgroundColor: '#0a0a0b',
|
|
|
|
|
titleBarStyle: 'default',
|
|
|
|
|
show: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mainWindow.loadFile('src/index.html');
|
|
|
|
|
|
|
|
|
|
mainWindow.once('ready-to-show', () => {
|
|
|
|
|
mainWindow.show();
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-01 15:07:02 +08:00
|
|
|
// DevTools: opt-in only (prevents auto-opening on startup)
|
|
|
|
|
if (process.env.ELECTRON_DEVTOOLS === '1') {
|
|
|
|
|
mainWindow.webContents.openDevTools();
|
|
|
|
|
}
|
2026-01-28 14:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
|
db = new Database();
|
|
|
|
|
createWindow();
|
2026-02-01 15:07:02 +08:00
|
|
|
startAutoBackup();
|
2026-01-28 14:56:28 +08:00
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
|
createWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
2026-02-01 15:07:02 +08:00
|
|
|
if (backupInterval) {
|
|
|
|
|
clearInterval(backupInterval);
|
|
|
|
|
}
|
2026-01-28 14:56:28 +08:00
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ==================== Vault IPC Handlers ====================
|
|
|
|
|
ipcMain.handle('db:getVaults', async () => {
|
|
|
|
|
return db.getVaults();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:addVault', async (event, vault) => {
|
|
|
|
|
return db.addVault(vault);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:updateVault', async (event, id, vault) => {
|
|
|
|
|
return db.updateVault(id, vault);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:deleteVault', async (event, id) => {
|
|
|
|
|
return db.deleteVault(id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ==================== Account IPC Handlers ====================
|
|
|
|
|
ipcMain.handle('db:getAccounts', async (event, page, limit, vaultId) => {
|
|
|
|
|
return db.getAccounts(page, limit, vaultId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:getAccountCount', async (event, vaultId) => {
|
|
|
|
|
return db.getAccountCount(vaultId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:addAccount', async (event, account) => {
|
|
|
|
|
return db.addAccount(account);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:updateAccount', async (event, id, account) => {
|
|
|
|
|
return db.updateAccount(id, account);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:deleteAccount', async (event, id) => {
|
|
|
|
|
return db.deleteAccount(id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:moveAccountToVault', async (event, accountId, vaultId) => {
|
|
|
|
|
return db.moveAccountToVault(accountId, vaultId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('db:searchAccounts', async (event, query, vaultId) => {
|
|
|
|
|
return db.searchAccounts(query, vaultId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('clipboard:write', async (event, text) => {
|
|
|
|
|
clipboard.writeText(text);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-02-01 15:07:02 +08:00
|
|
|
|
|
|
|
|
// Screen capture feature
|
|
|
|
|
ipcMain.handle('screen:capture', async () => {
|
|
|
|
|
try {
|
|
|
|
|
const primaryDisplay = screen.getPrimaryDisplay();
|
|
|
|
|
const { width, height } = primaryDisplay.size;
|
|
|
|
|
const scaleFactor = primaryDisplay.scaleFactor;
|
|
|
|
|
|
|
|
|
|
const sources = await desktopCapturer.getSources({
|
|
|
|
|
types: ['screen'],
|
|
|
|
|
thumbnailSize: { width: width * scaleFactor, height: height * scaleFactor }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (sources.length > 0) {
|
|
|
|
|
const thumbnail = sources[0].thumbnail;
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
image: thumbnail.toDataURL(),
|
|
|
|
|
width: thumbnail.getSize().width,
|
|
|
|
|
height: thumbnail.getSize().height
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return { success: false, error: 'Failed to capture screen' };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Screen capture failed:', e);
|
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
|
}
|
|
|
|
|
});
|