Files
backone-client/main.js
2025-12-23 22:01:01 +07:00

126 lines
3.7 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');
//console.log(app.getPath('userData'))
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 = { url_address: 'https://us.manage.backone.cloud/login/',
app_name: 'Client',
user_agent: 'BackOne-Client'
};
const loadValues = loadConfig() ?? config
config.url_address = loadValues.url_address ?? config.url_address
config.app_name = loadValues.app_name ?? config.app_name
config.user_agent = loadValues.user_agent ?? config.user_agent
const filter = {
urls: ['<all_urls>']
}
// Browser Specific
Menu.setApplicationMenu(null);
const createWindow = () => {
const win = new BrowserWindow({
width: 1440,
height: 900,
icon: path.join(__dirname, 'assets/client.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
})
win.setTitle(`${config.app_name} ${app.getVersion()}`);
// Custom Headers
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
details.requestHeaders['User-Agent'] = config.user_agent;
details.requestHeaders['Device-Id'] = device_id;
callback({ requestHeaders: details.requestHeaders })
})
win.loadURL(config.url_address)
win.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
console.log(`Failed to load: ${validatedURL}. Error: ${errorDescription} (${errorCode})`);
win.loadFile(path.join(__dirname, 'error.html'));
});
}
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
const urlObject = new URL(config.url_address)
//if (url.startsWith(config.url_address)) {
//console.log(`${error}`)
//console.log(`${urlObject.protocol}//${urlObject.hostname}`)
//if (url.startsWith(`${urlObject.protocol}//${urlObject.hostname}`)) {
if (url.startsWith(`${urlObject.protocol}//`)) {
// 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();
}
});
});