-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
343 lines (304 loc) · 9.51 KB
/
main.js
File metadata and controls
343 lines (304 loc) · 9.51 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const { app, BrowserWindow, ipcMain, shell } = require('electron');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
// 配置文件路径
const configPath = path.join(app.getPath('userData'), 'config.json');
// 加密配置
const ENCRYPTION_KEY = crypto.scryptSync('bt-watcher-secret-2024', 'salt', 32);
const IV_LENGTH = 16;
// 加密函数
function encrypt(text) {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-cbc', ENCRYPTION_KEY, iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
// 解密函数
function decrypt(text) {
const textParts = text.split(':');
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', ENCRYPTION_KEY, iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
// 默认配置
const defaultConfig = {
refreshInterval: 5,
uaFilter: '',
uaBlacklist: '',
ipBlacklist: '',
maxDisplayRows: 1000,
windowBounds: {
width: 1400,
height: 900
},
// API配置(加密存储)
apiUrl: null,
apiKey: null,
apiEncrypted: false
};
// 检查是否需要初始化
function needsInitialization() {
try {
const config = loadConfig();
if (!config.apiUrl || !config.apiKey) {
return true;
}
// 如果是加密配置,尝试解密来验证
if (config.apiEncrypted) {
try {
const apiUrl = decrypt(config.apiUrl);
const apiKey = decrypt(config.apiKey);
return !apiUrl || !apiKey;
} catch {
return true;
}
}
return false;
} catch {
return true;
}
}
// 保存API配置到主配置文件
function saveApiConfig(apiUrl, apiKey) {
try {
config.apiUrl = encrypt(apiUrl);
config.apiKey = encrypt(apiKey);
config.apiEncrypted = true;
saveConfig(config);
// 同时更新环境变量
process.env.BT_URL = apiUrl;
process.env.BT_KEY = apiKey;
return true;
} catch (error) {
console.error('保存API配置失败:', error);
return false;
}
}
// 加载API配置
function loadApiConfig() {
try {
const config = loadConfig();
if (config.apiUrl && config.apiKey) {
if (config.apiEncrypted) {
// 解密配置
const apiUrl = decrypt(config.apiUrl);
const apiKey = decrypt(config.apiKey);
process.env.BT_URL = apiUrl;
process.env.BT_KEY = apiKey;
return { apiUrl, apiKey };
} else {
// 兼容旧版本未加密的配置,转换为加密格式
process.env.BT_URL = config.apiUrl;
process.env.BT_KEY = config.apiKey;
saveApiConfig(config.apiUrl, config.apiKey);
return { apiUrl: config.apiUrl, apiKey: config.apiKey };
}
}
} catch (error) {
console.error('加载API配置失败:', error);
}
return null;
}
// 读取配置
function loadConfig() {
try {
if (fs.existsSync(configPath)) {
const data = fs.readFileSync(configPath, 'utf8');
return { ...defaultConfig, ...JSON.parse(data) };
}
} catch (error) {
console.error('加载配置失败:', error);
}
return defaultConfig;
}
// 保存配置
function saveConfig(config) {
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
} catch (error) {
console.error('保存配置失败:', error);
}
}
let mainWindow;
// 迁移旧版本API配置文件
function migrateOldApiConfig() {
const oldApiConfigPath = path.join(app.getPath('userData'), 'api-config.json');
try {
if (fs.existsSync(oldApiConfigPath)) {
const oldConfig = JSON.parse(fs.readFileSync(oldApiConfigPath, 'utf8'));
// 加载当前配置
const currentConfig = loadConfig();
// 迁移API配置
if (oldConfig.encrypted || oldConfig.apiEncrypted) {
currentConfig.apiUrl = oldConfig.apiUrl;
currentConfig.apiKey = oldConfig.apiKey;
currentConfig.apiEncrypted = true;
} else if (oldConfig.apiUrl && oldConfig.apiKey) {
// 旧版本未加密的配置,重新加密保存
currentConfig.apiUrl = encrypt(oldConfig.apiUrl);
currentConfig.apiKey = encrypt(oldConfig.apiKey);
currentConfig.apiEncrypted = true;
}
// 保存合并后的配置
saveConfig(currentConfig);
// 删除旧配置文件
fs.unlinkSync(oldApiConfigPath);
console.log('已成功迁移旧版本API配置');
}
} catch (error) {
console.error('迁移旧版本API配置失败:', error);
}
}
// 启动时先进行迁移
migrateOldApiConfig();
let config = loadConfig();
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
width: config.windowBounds.width || 1400,
height: config.windowBounds.height || 900,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
},
icon: path.join(__dirname, 'icon.png'),
title: 'BT Watcher'
});
// 检查是否需要初始化
if (needsInitialization()) {
mainWindow.loadFile('init.html');
} else {
// 加载API配置
loadApiConfig();
mainWindow.loadFile('index.html');
}
// 保存窗口大小
mainWindow.on('resize', () => {
const bounds = mainWindow.getBounds();
config.windowBounds = bounds;
saveConfig(config);
});
// 窗口关闭时的处理
mainWindow.on('closed', () => {
mainWindow = null;
});
// 开发者工具
// 需要调试时可以取消注释下面这行
// mainWindow.webContents.openDevTools();
}
// IPC通信处理
ipcMain.handle('load-config', () => {
return config;
});
ipcMain.handle('save-config', async (event, newConfig) => {
// 如果是API配置
if (newConfig.apiUrl !== undefined && newConfig.apiKey !== undefined) {
// 先验证连接
const btApi = require('./lib/bt-api');
process.env.BT_URL = newConfig.apiUrl;
process.env.BT_KEY = newConfig.apiKey;
try {
// 尝试获取站点列表来验证连接
await btApi.getSiteList();
// 保存配置
if (saveApiConfig(newConfig.apiUrl, newConfig.apiKey)) {
return { success: true };
} else {
return { success: false, error: '保存配置失败' };
}
} catch (error) {
return { success: false, error: '连接失败: ' + error.message };
}
} else {
// 普通配置
config = { ...config, ...newConfig };
saveConfig(config);
return { success: true };
}
});
// 清空API配置
ipcMain.handle('clear-api-config', async () => {
try {
// 只清空API相关字段,保留其他配置
config.apiUrl = null;
config.apiKey = null;
config.apiEncrypted = false;
saveConfig(config);
// 清除环境变量
delete process.env.BT_URL;
delete process.env.BT_KEY;
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
// 打开外部链接
ipcMain.handle('open-external', async (event, url) => {
try {
await shell.openExternal(url);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
// API调用转发
ipcMain.handle('api-request', async (event, { endpoint, method, data }) => {
const btApi = require('./lib/bt-api');
try {
switch (endpoint) {
case 'getSiteList':
return { success: true, data: await btApi.getSiteList() };
case 'getSiteLogs':
return { success: true, data: await btApi.getSiteLogs(data.siteName) };
case 'getRedirectList':
return { success: true, data: await btApi.getRedirectList(data.siteName) };
case 'modifyRedirect':
return { success: true, data: await btApi.modifyRedirect(data) };
case 'getFileList':
return { success: true, data: await btApi.getFileList(data.path) };
case 'getFileContent':
return { success: true, data: await btApi.getFileContent(data.path) };
case 'saveFileContent':
return { success: true, data: await btApi.saveFileContent(data.path, data.content, data.encoding) };
case 'createFile':
return { success: true, data: await btApi.createFile(data.path) };
case 'createFolder':
return { success: true, data: await btApi.createFolder(data.path) };
case 'deleteFile':
return { success: true, data: await btApi.deleteFile(data.path) };
case 'uploadFile':
return { success: true, data: await btApi.uploadFile(data.path, data.fileData) };
case 'addFirewallRule':
return { success: true, data: await btApi.addFirewallRule(data.ip, data.brief) };
case 'getFirewallRules':
return { success: true, data: await btApi.getFirewallRules(data.page, data.pageSize) };
case 'removeFirewallRule':
return { success: true, data: await btApi.removeFirewallRule(data.ip) };
default:
throw new Error('未知的API端点');
}
} catch (error) {
console.error('API调用失败:', error);
return { success: false, error: error.message };
}
});
// 应用就绪
app.whenReady().then(createWindow);
// 所有窗口关闭时退出
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// 在macOS上重新创建窗口
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});