| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import { app, BrowserWindow, session, ipcMain } from 'electron';
- import { fileURLToPath } from 'url';
- import path from 'path';
- import { exec } from 'child_process';
- import { promisify } from 'util';
- const execAsync = promisify(exec);
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
- // 设置内容安全策略(CSP),防止 XSS 攻击
- function setContentSecurityPolicy() {
- session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
- const csp = isDev
- ? "default-src 'self'; script-src 'self' 'unsafe-inline' http://localhost:*; style-src 'self' 'unsafe-inline'; connect-src 'self' http://localhost:* ws://localhost:*; img-src 'self' data: https: blob:; font-src 'self' data:; worker-src 'self' blob:;"
- : "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data: https:; font-src 'self' data:;";
-
- const responseHeaders = Object.assign({}, details.responseHeaders);
- responseHeaders['Content-Security-Policy'] = [csp];
-
- callback({ responseHeaders });
- });
- }
- // 创建主窗口,根据环境加载不同内容源
- function createWindow() {
- const mainWindow = new BrowserWindow({
- width: 1200,
- height: 800,
- webPreferences: {
- preload: path.join(__dirname, 'preload.cjs'),
- nodeIntegration: false,
- contextIsolation: true
- }
- });
- if (isDev) {
- mainWindow.loadURL('http://localhost:5173');
- mainWindow.webContents.openDevTools();
- } else {
- mainWindow.loadFile(path.join(__dirname, 'dist/index.html'));
- }
- }
- // 获取已连接的 ADB 设备列表
- async function getADBDevices() {
- try {
- const { stdout } = await execAsync('adb devices');
- const lines = stdout.split('\n').slice(1);
- const devices = [];
-
- for (const line of lines) {
- const parts = line.trim().split(/\s+/);
- if (parts.length >= 2 && parts[0] && parts[1] === 'device') {
- devices.push({
- id: parts[0],
- status: parts[1]
- });
- }
- }
- return devices;
- } catch (error) {
- console.error('获取设备列表失败:', error);
- return [];
- }
- }
- // IPC 处理程序:获取 ADB 设备列表
- ipcMain.handle('get-adb-devices', async () => {
- return await getADBDevices();
- });
- // IPC 处理程序:连接 ADB 设备
- ipcMain.handle('connect-adb-device', async (event, ipPort) => {
- try {
- await execAsync(`adb connect ${ipPort}`);
- return { success: true };
- } catch (error) {
- console.error('连接设备失败:', error);
- return { success: false, error: error.message };
- }
- });
- // 应用启动逻辑:设置 CSP、创建窗口、监听激活事件
- app.whenReady().then(() => {
- setContentSecurityPolicy();
- createWindow();
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
- });
- // 应用关闭逻辑:macOS 保持运行,其他平台退出
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
|