feishin/src/main/features/core/settings/index.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
import Store from 'electron-store';
import { ipcMain, safeStorage } from 'electron';
2022-12-19 15:59:14 -08:00
export const store = new Store();
ipcMain.handle('settings-get', (_event, data: { property: string }) => {
2023-07-01 19:10:05 -07:00
return store.get(`${data.property}`);
2022-12-19 15:59:14 -08:00
});
ipcMain.on('settings-set', (__event, data: { property: string; value: any }) => {
2023-07-01 19:10:05 -07:00
store.set(`${data.property}`, data.value);
2022-12-19 15:59:14 -08:00
});
ipcMain.handle('password-get', (_event, server: string): string | null => {
2023-07-01 19:10:05 -07:00
if (safeStorage.isEncryptionAvailable()) {
const servers = store.get('server') as Record<string, string> | undefined;
2023-07-01 19:10:05 -07:00
if (!servers) {
return null;
}
2023-07-01 19:10:05 -07:00
const encrypted = servers[server];
if (!encrypted) return null;
2023-07-01 19:10:05 -07:00
const decrypted = safeStorage.decryptString(Buffer.from(encrypted, 'hex'));
return decrypted;
}
2023-07-01 19:10:05 -07:00
return null;
});
ipcMain.on('password-remove', (_event, server: string) => {
2023-07-01 19:10:05 -07:00
const passwords = store.get('server', {}) as Record<string, string>;
if (server in passwords) {
delete passwords[server];
}
store.set({ server: passwords });
});
ipcMain.handle('password-set', (_event, password: string, server: string) => {
2023-07-01 19:10:05 -07:00
if (safeStorage.isEncryptionAvailable()) {
const encrypted = safeStorage.encryptString(password);
const passwords = store.get('server', {}) as Record<string, string>;
passwords[server] = encrypted.toString('hex');
store.set({ server: passwords });
2023-07-01 19:10:05 -07:00
return true;
}
return false;
});