forked from digital195/unifi-protect-viewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
257 lines (221 loc) · 7.74 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('node:path')
const version = require('./src/js/modules/version')
const { URL } = require('node:url')
const utils = require('./src/js/modules/utils')
// Constants
const DEFAULT_WIDTH = 1270
const DEFAULT_HEIGHT = 750
const isDev = process.env.NODE_ENV === 'development'
// Enable hot reloading in development mode
if (isDev) {
try {
require('electron-reloader')(module, {
ignore: ['node_modules', 'builds', 'releases'],
})
utils.log('Electron hot reloading enabled')
} catch (err) {
console.error('Failed to enable hot reloading:', err)
}
}
// Store initialization
let store
const resetRequested = process.argv.includes('--reset')
// Initialize store
async function initializeStore() {
try {
const Store = (await import('electron-store')).default
store = new Store()
if (resetRequested) {
store.clear()
}
} catch (error) {
console.error('Failed to initialize store:', error)
// Create a memory-only store as fallback
store = {
get: (key) => null,
set: () => {},
clear: () => {},
store: {},
}
}
}
// Configure any custom app behavior
function configureApp() {
// Disable hardware acceleration if requested by config
if (store.get('disableHardwareAcceleration')) {
app.disableHardwareAcceleration()
utils.log('Hardware acceleration disabled')
}
}
// Create the browser window.
async function createWindow() {
const mainWindow = new BrowserWindow({
width: store.get('bounds')?.width || DEFAULT_WIDTH,
height: store.get('bounds')?.height || DEFAULT_HEIGHT,
x: store.get('bounds')?.x || undefined,
y: store.get('bounds')?.y || undefined,
webPreferences: {
preload: path.join(__dirname, '/src/js/preload.js'),
contextIsolation: true, // Enable security
nodeIntegration: false, // Disable direct access
spellcheck: false,
sandbox: false, // Needed for some functionality
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
webSecurity: true,
},
icon: path.join(__dirname, '/src/img/128.png'),
frame: true,
autoHideMenuBar: true,
})
// Set custom user agent using dynamic values from version
mainWindow.webContents.setUserAgent(version.userAgent)
// Set window title
mainWindow.setTitle(`UniFi Protect Viewer ${app.getVersion()}`)
// Open DevTools in development mode
if (isDev) {
mainWindow.webContents.openDevTools()
}
// Handle certificate errors - only bypass for configured domain
mainWindow.webContents.on('certificate-error', (event, urlString, error, certificate, callback) => {
const configUrl = store.get('url')
const ignoreCertErrors = store.get('ignoreCertErrors') || false
if (ignoreCertErrors && configUrl) {
try {
// Extract domains to compare
const configDomain = new URL(configUrl).hostname
const requestDomain = new URL(urlString).hostname
// Only bypass for matching domains
if (configDomain === requestDomain) {
utils.log(`Bypassing certificate error for configured domain: ${requestDomain}`)
event.preventDefault()
callback(true) // Trust the certificate
return
}
} catch (err) {
utils.log('Error parsing URL when handling certificate error:', err)
}
}
// Default: don't trust the certificate
callback(false) // Don't trust the certificate
})
// Save window position/size on close
mainWindow.on('close', () => {
store.set('bounds', mainWindow.getBounds())
})
// Load the initial URL
const initialUrl = store.get('url') || 'about:blank'
utils.log(`Loading initial URL: ${initialUrl}`)
mainWindow.loadURL(initialUrl)
// If no URL is set, navigate to the config page
if (initialUrl === 'about:blank') {
const configUrl = `file://${path.join(__dirname, 'src/html/config.html')}`
mainWindow.loadURL(configUrl)
}
// Handle external links securely
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
utils.log(`Window open request for ${url}`)
// Only allow navigation to URLs with expected protocols/domains
// For the UniFi Protect application, we should only allow URLs related to the Protect system
if (url.startsWith(store.get('url') || '') || url.startsWith('file://')) {
mainWindow.loadURL(url)
} else {
utils.log(`Blocked navigation to external URL: ${url}`)
}
return { action: 'deny' }
})
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
if (errorCode !== -3) {
// Ignore aborted loads (often just navigation)
const errorPage = `file://${path.join(__dirname, 'src/html/error.html')}?error=${encodeURIComponent(errorDescription)}&url=${encodeURIComponent(validatedURL)}`
mainWindow.loadURL(errorPage)
}
})
return mainWindow
}
// IPC handlers for communication between renderer and main process
function setupIpcHandlers(mainWindow) {
// Load saved configs and credentials
ipcMain.handle('configLoad', () => {
return store.store
})
// Save changes to config
ipcMain.on('configSave', (event, config) => {
// Merge incoming config changes with existing store
const updatedConfig = { ...store.store, ...config }
store.set(updatedConfig)
// Only reload the URL if this is a navigation change (has URL and credentials)
// and not just a UI preference update
if (config.url && config.username && config.password) {
mainWindow.loadURL(config.url)
}
})
// Handle partial config updates (for things like login attempts)
ipcMain.handle('configSavePartial', (event, partialConfig) => {
// Update only the specified config values
Object.entries(partialConfig).forEach(([key, value]) => {
store.set(key, value)
})
return true
})
// Handle URL loading from renderer
ipcMain.on('loadURL', (event, url) => {
utils.log(`Loading URL: ${url}`)
mainWindow.loadURL(url)
})
// Handle application restart
ipcMain.on('restart', (event) => {
utils.log('Restart requested')
app.relaunch()
app.exit()
})
// Handle reset request
ipcMain.on('reset', (event) => {
utils.log('Reset requested')
store.clear()
})
// Handle fullscreen toggle
ipcMain.on('toggleFullscreen', (event) => {
utils.log('Fullscreen toggle requested')
if (mainWindow) {
const isFullScreen = mainWindow.isFullScreen()
mainWindow.setFullScreen(!isFullScreen)
}
})
// Handle reset confirmation dialog
ipcMain.handle('showResetConfirmation', async (event) => {
const result = await dialog.showMessageBox(mainWindow, {
type: 'warning',
title: 'Reset Configuration',
message: 'Are you sure you want to reset all settings?',
detail: 'This will clear all your saved settings including credentials.',
buttons: ['Cancel', 'Reset'],
defaultId: 0,
cancelId: 0,
})
return result.response === 1 // Return true if "Reset" was clicked
})
}
// Wait until Electron app is ready
async function start() {
await app.whenReady()
await initializeStore()
configureApp()
const mainWindow = await createWindow()
setupIpcHandlers(mainWindow)
// Mac: Re-create window when dock icon is clicked and no windows are open
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
}
// Start the app
start().catch((error) => {
// Always log critical errors to console regardless of environment
console.error('Error starting app:', error)
})
// Quit when all windows are closed, except on macOS
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})