-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
222 lines (194 loc) · 6.8 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
const { app, BrowserWindow, session, Tray, Menu, globalShortcut } = require('electron');
const Store = require('electron-store');
const path = require('path');
const store = new Store();
let tray = null;
let mainWindow = null;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
webSecurity: true,
partition: 'persist:main'
},
icon: path.join(__dirname, 'icons', 'image-256.png')
});
// Set up request interceptor for ALL requests
session.fromPartition('persist:main').webRequest.onBeforeSendHeaders(
{ urls: ['*://*/*'] },
async (details, callback) => {
// Skip OPTIONS requests as they need special handling
if (details.method === 'OPTIONS') {
callback({ requestHeaders: details.requestHeaders });
return;
}
// Always add Cloudflare Access headers
const modifiedHeaders = {
...details.requestHeaders,
'CF-Access-Client-Id': process.env.CF_ACCESS_CLIENT_ID,
'CF-Access-Client-Secret': process.env.CF_ACCESS_CLIENT_SECRET
};
// Add browser-like headers for all requests
if (!details.url.startsWith('file://')) {
modifiedHeaders['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
modifiedHeaders['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
}
console.log('\n=== REQUEST ===');
console.log('URL:', details.url);
console.log('Headers:', JSON.stringify(modifiedHeaders, null, 2));
callback({ requestHeaders: modifiedHeaders });
}
);
// Handle redirects
session.fromPartition('persist:main').webRequest.onBeforeRedirect((details) => {
console.log('\n=== REDIRECT ===');
console.log('From:', details.url);
console.log('To:', details.redirectURL);
});
// Handle responses
session.fromPartition('persist:main').webRequest.onHeadersReceived(
{ urls: ['*://*/*'] },
(details, callback) => {
// Handle authentication errors
if (details.statusCode === 403) {
console.error('\n=== AUTHENTICATION ERROR ===');
console.error('Failed to authenticate with Cloudflare Access');
console.error('URL:', details.url);
// Retry the request after a short delay
setTimeout(() => {
win.reload();
}, 1000);
}
console.log('\n=== RESPONSE ===');
console.log('URL:', details.url);
console.log('Status:', details.statusCode);
console.log('Headers:', JSON.stringify(details.responseHeaders, null, 2));
// For Cloudflare domains, ensure CORS headers are present
if (!details.url.startsWith('file://')) {
const responseHeaders = {
...details.responseHeaders,
'Access-Control-Allow-Origin': ['*'],
'Access-Control-Allow-Headers': ['*'],
'Access-Control-Allow-Methods': ['GET, POST, OPTIONS'],
'Access-Control-Allow-Credentials': ['true']
};
callback({ responseHeaders });
} else {
callback({ responseHeaders: details.responseHeaders });
}
}
);
// Start at the main URL
mainWindow.loadURL(`${process.env.HOMEBOX_URL}`);
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
console.log('\n=== LOAD ERROR ===');
console.error('Failed:', errorDescription);
console.error('Code:', errorCode);
// Retry on connection errors
if (errorCode === -102 || errorCode === -106) {
console.log('Retrying connection...');
setTimeout(() => {
mainWindow.reload();
}, 2000);
}
});
mainWindow.webContents.on('did-navigate', (event, url) => {
console.log('\n=== NAVIGATION ===');
console.log('Navigated to:', url);
mainWindow.webContents.executeJavaScript(`
const urlDisplay = document.getElementById('url-display');
if (urlDisplay) {
urlDisplay.textContent = window.location.href;
detectAssetId();
}
`);
});
// Handle window minimize to tray
mainWindow.on('minimize', (event) => {
event.preventDefault();
mainWindow.hide();
});
// Handle window close to tray
mainWindow.on('close', (event) => {
if (!app.isQuitting) {
event.preventDefault();
mainWindow.hide();
return false;
}
return true;
});
}
function createTray() {
tray = new Tray(path.join(__dirname, 'icons', 'image-256.png'));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show Homebox',
click: () => {
mainWindow.show();
}
},
{
label: 'Quit',
click: () => {
app.isQuitting = true;
app.quit();
}
}
]);
tray.setToolTip('Homebox Desktop');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
}
function registerShortcuts() {
// Toggle window visibility
globalShortcut.register('Alt+H', () => {
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
mainWindow.show();
mainWindow.focus();
}
});
// Reload page
globalShortcut.register('Alt+R', () => {
if (mainWindow.isFocused()) {
mainWindow.reload();
}
});
// Toggle DevTools
globalShortcut.register('Alt+D', () => {
if (mainWindow.isFocused()) {
mainWindow.webContents.toggleDevTools();
}
});
// Quit application
globalShortcut.register('Alt+Q', () => {
app.isQuitting = true;
app.quit();
});
}
app.whenReady().then(() => {
createWindow();
createTray();
registerShortcuts();
});
app.on('will-quit', () => {
// Unregister all shortcuts
globalShortcut.unregisterAll();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});