Files
backone-client/main.js
2025-12-23 12:21:30 +07:00

110 lines
3.1 KiB
JavaScript

const { app, BrowserWindow, Menu, ipcMain, session } = require('electron/main')
const fs = require('fs')
const path = require('path')
//const { session } = require('electron')
const {machineId, machineIdSync} = require('node-machine-id')
const { loadEnvFile } = require('node:process');
// run this as early in the main process as possible
if (require('electron-squirrel-startup')) app.quit();
let device_id = machineIdSync()
// Define the path to your config file (e.g., in the user's app data directory)
const configPath = path.join(app.getPath('userData'), 'config.json');
function loadConfig() {
if (fs.existsSync(configPath)) {
try {
const data = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(data);
} catch (error) {
console.error('Error reading or parsing config file:', error);
// Handle potential errors (e.g., corrupted JSON)
return null;
}
}
return null; // Return null or default config if the file doesn't exist
}
const config = loadConfig() || { url_address: 'https://nexus.manage.backone.cloud/' };
const filter = {
urls: [`${config.url_address}*`]
}
// Browser Sepcific
Menu.setApplicationMenu(null);
const createWindow = () => {
const win = new BrowserWindow({
width: 1440,
height: 900,
title: 'Client',
//titleBarStyle: 'hidden',
//tabbingIdentifier: 'clientTabs',
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
//...(process.platform !== 'darwin' ? { titleBarOverlay: true } : {})
})
//win.setMenu(null)
//win.setTitle(`Client - Version ${app.getVersion()}`);
win.setTitle(`Client ${app.getVersion()}`);
// Custom Headers
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
details.requestHeaders['User-Agent'] = 'BackOne-Client';
details.requestHeaders['Device-Id'] = device_id;
callback({ requestHeaders: details.requestHeaders })
})
//win.loadFile('index.html')
win.loadURL(config.url_address)
}
app.on('ready', async () => {
// Clear the default session's cache before the window is ready to load content
try {
await session.defaultSession.clearCache();
console.log('Cache cleared successfully on app start.');
} catch (err) {
console.error('Failed to clear cache:', err);
}
createWindow();
app.on('certificate-error', (event, webContents, url, error, cert, callback) => {
// Verify the URL to ensure you only allow your intended self-signed certificate
if (url.startsWith(config.url_address)) {
// Prevent the default behavior (which is to reject the cert)
event.preventDefault();
// Accept the certificate
callback(true);
} else {
// For all other URLs, let Chromium handle the error normally
callback(false);
}
});
// Quit app when all windows are closed, except on macOS (common pattern)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// Recreate a window in the app when the dock icon is clicked (macOS)
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});