From 19fde43f8b2c1c1d108c4f29c6c3f1e23463ca3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=98=89=E4=BC=9F?= <8473136@qq.com> Date: Wed, 8 Jan 2025 11:22:09 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Improve=20logging=20and=20error=20h?= =?UTF-8?q?andling=20in=20API=20modules:=20Refactored=20common,=20file,=20?= =?UTF-8?q?and=20logger=20APIs=20to=20utilize=20a=20new=20logging=20utilit?= =?UTF-8?q?y=20for=20better=20traceability.=20Enhanced=20error=20handling?= =?UTF-8?q?=20for=20URL=20opening=20and=20file=20dialog=20interactions,=20?= =?UTF-8?q?ensuring=20informative=20logs=20for=20success=20and=20failure?= =?UTF-8?q?=20cases.=20Introduced=20a=20utility=20to=20mask=20sensitive=20?= =?UTF-8?q?data=20in=20configuration=20logs,=20improving=20security=20duri?= =?UTF-8?q?ng=20application=20initialization.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/api/common.ts | 18 ++++++++++++++---- electron/api/file.ts | 22 +++++++++++++++------- electron/api/logger.ts | 15 ++++++++++++--- electron/main/index.ts | 23 ++++++++++++++--------- electron/utils/desensitize.ts | 14 ++++++++++++++ 5 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 electron/utils/desensitize.ts diff --git a/electron/api/common.ts b/electron/api/common.ts index 1325b71..5a06025 100644 --- a/electron/api/common.ts +++ b/electron/api/common.ts @@ -1,16 +1,26 @@ import { app, ipcMain, shell } from "electron"; -import log from "electron-log"; +import { logError, logInfo, LogModule, logWarn } from "../utils/log"; export const initCommonApi = () => { - // 打开链接 ipcMain.on("common.openUrl", async (event, args) => { if (args) { - log.info(`打开链接:${args}`); - shell.openExternal(args).then(() => {}); + logInfo(LogModule.APP, `Attempting to open URL: ${args}`); + try { + await shell.openExternal(args); + logInfo(LogModule.APP, `Successfully opened URL: ${args}`); + } catch (error) { + logError( + LogModule.APP, + `Failed to open URL: ${args}. Error: ${error.message}` + ); + } + } else { + logWarn(LogModule.APP, "No URL provided to open."); } }); ipcMain.on("common.relaunch", () => { + logInfo(LogModule.APP, "Application is relaunching."); app.relaunch(); app.quit(); }); diff --git a/electron/api/file.ts b/electron/api/file.ts index e546f66..2180366 100644 --- a/electron/api/file.ts +++ b/electron/api/file.ts @@ -1,13 +1,21 @@ import {dialog, ipcMain} from "electron"; +import { logInfo, logError, LogModule } from "../utils/log"; export const initFileApi = () => { ipcMain.handle("file.selectFile", async (event, args) => { - const result = dialog.showOpenDialogSync({ - properties: ['openFile'], - filters: [ - {name: 'Text Files', extensions: args}, - ] - }) - return result; + logInfo(LogModule.APP, `Attempting to open file dialog with filters: ${JSON.stringify(args)}`); + try { + const result = dialog.showOpenDialogSync({ + properties: ['openFile'], + filters: [ + { name: 'Text Files', extensions: args }, + ] + }); + logInfo(LogModule.APP, `File dialog result: ${JSON.stringify(result)}`); + return result; + } catch (error) { + logError(LogModule.APP, `Error opening file dialog: ${error.message}`); + return null; + } }); } diff --git a/electron/api/logger.ts b/electron/api/logger.ts index 33be0b4..b350a15 100644 --- a/electron/api/logger.ts +++ b/electron/api/logger.ts @@ -1,4 +1,5 @@ import { app, ipcMain, shell } from "electron"; +import { logInfo, logError, LogModule } from "../utils/log"; const fs = require("fs"); const path = require("path"); @@ -8,35 +9,43 @@ export const initLoggerApi = () => { const readLogger = (callback: (content: string) => void) => { fs.readFile(logPath, "utf-8", (error, data) => { if (!error) { + logInfo(LogModule.LOGGER, "Log file read successfully."); callback(data); + } else { + logError(LogModule.LOGGER, `Error reading log file: ${error.message}`); } }); }; ipcMain.on("logger.getLog", async (event, args) => { + logInfo(LogModule.LOGGER, "Received request to get log."); readLogger(content => { event.reply("Logger.getLog.hook", content); + logInfo(LogModule.LOGGER, "Log data sent to client."); }); }); ipcMain.on("logger.update", (event, args) => { + logInfo(LogModule.LOGGER, "Watching log file for changes."); fs.watch(logPath, (eventType, filename) => { if (eventType === "change") { + logInfo(LogModule.LOGGER, "Log file changed, reading new content."); readLogger(content => { event.reply("Logger.update.hook", content); + logInfo(LogModule.LOGGER, "Updated log data sent to client."); }); } }); }); ipcMain.on("logger.openLog", (event, args) => { - console.log('正在打开日志'); + logInfo(LogModule.LOGGER, "Attempting to open log file."); shell.openPath(logPath).then((errorMessage) => { if (errorMessage) { - console.error('Failed to open Logger:', errorMessage); + logError(LogModule.LOGGER, `Failed to open Logger: ${errorMessage}`); event.reply("Logger.openLog.hook", false); } else { - console.log('Logger opened successfully'); + logInfo(LogModule.LOGGER, "Logger opened successfully."); event.reply("Logger.openLog.hook", true); } }); diff --git a/electron/main/index.ts b/electron/main/index.ts index b6b06ed..af23442 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -24,6 +24,7 @@ import { getConfig } from "../storage/config"; import { initCommonApi } from "../api/common"; import { initLocalApi } from "../api/local"; import { logError, logInfo, LogModule } from "../utils/log"; +import { maskSensitiveData } from "../utils/desensitize"; process.env.DIST_ELECTRON = join(__dirname, ".."); process.env.DIST = join(process.env.DIST_ELECTRON, "../dist"); @@ -151,11 +152,7 @@ export const createTray = (config: FrpConfig) => { win.show(); }); - if (config) { - if (config.systemStartupConnect) { - startFrpWorkerProcess(config); - } - } + logInfo(LogModule.APP, `Tray created successfully.`); }; app.whenReady().then(() => { logInfo( @@ -171,15 +168,23 @@ app.whenReady().then(() => { return; } - const { serverAddr, serverPort, authToken, ...sensitiveData } = config; - logInfo(LogModule.APP, `Config retrieved: ${JSON.stringify({ ...sensitiveData, serverAddr: '**', serverPort: '**', authToken: '**' })}`); - createWindow(config) .then(r => { logInfo(LogModule.APP, `Window created successfully.`); createTray(config); - logInfo(LogModule.APP, `Tray created successfully.`); + if (config) { + logInfo( + LogModule.APP, + `Config retrieved: ${JSON.stringify( + maskSensitiveData(config, ["serverAddr", "serverPort", "authToken", "user", "metaToken"]) + )}` + ); + + if (config.systemStartupConnect) { + startFrpWorkerProcess(config); + } + } // Initialize APIs try { initGitHubApi(); diff --git a/electron/utils/desensitize.ts b/electron/utils/desensitize.ts new file mode 100644 index 0000000..a77f0f4 --- /dev/null +++ b/electron/utils/desensitize.ts @@ -0,0 +1,14 @@ +export const maskSensitiveData = ( + obj: Record, + keysToMask: string[] +) => { + console.log("obj", obj); + const maskedObj = JSON.parse(JSON.stringify(obj)); + console.log("masked", maskedObj); + keysToMask.forEach(key => { + if (maskedObj.hasOwnProperty(key)) { + maskedObj[key] = "***"; + } + }); + return maskedObj; +};