From 6d57101b2fd7db429aaeeac7b1ab3a8d8c2aae45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com>
Date: Wed, 22 Jan 2025 16:29:19 +0800
Subject: [PATCH] =?UTF-8?q?optimize:=20=E6=8D=95=E8=8E=B7=E5=BA=94?=
 =?UTF-8?q?=E7=94=A8=E5=90=AF=E5=8A=A8=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=9A=84?=
 =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=B9=B6=E8=AE=B0=E5=BD=95=E6=97=A5?=
 =?UTF-8?q?=E5=BF=97=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/core/src/config.js                   |   2 +-
 packages/gui/src/background.js                | 242 ++++++++++--------
 packages/mitmproxy/src/index.js               |   2 +-
 packages/mitmproxy/src/lib/dns/ipaddress.js   |   2 +-
 .../src/lib/interceptor/impl/req/baiduOcr.js  |   2 +-
 5 files changed, 133 insertions(+), 117 deletions(-)

diff --git a/packages/core/src/config.js b/packages/core/src/config.js
index c05ad47592..729f18477c 100644
--- a/packages/core/src/config.js
+++ b/packages/core/src/config.js
@@ -173,7 +173,7 @@ const configApi = {
         log.info('读取远程配置文件内容成功:', path)
         return file.toString()
       } else {
-        log.warn('远程配置文件不存在:', path)
+        log.info('远程配置文件不存在:', path)
       }
     } catch (e) {
       log.error('读取远程配置文件内容失败:', e)
diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js
index 512791296d..a0a5c8962d 100644
--- a/packages/gui/src/background.js
+++ b/packages/gui/src/background.js
@@ -30,7 +30,13 @@ let winIsHidden = false
 
 let tray // 防止被内存清理
 let forceClose = false
-DevSidecar.api.config.reload()
+
+try {
+  DevSidecar.api.config.reload()
+} catch (e) {
+  log.error('配置加载失败:', e)
+}
+
 let hideDockWhenWinClose = DevSidecar.api.config.get().app.dock.hideWhenWinClose || false
 // Scheme must be registered before the app is ready
 protocol.registerSchemesAsPrivileged([
@@ -362,129 +368,139 @@ function initApp () {
 }
 
 // -------------执行开始---------------
-app.disableHardwareAcceleration() // 禁用gpu
-
-// 开启后是否默认隐藏window
-let startHideWindow = !DevSidecar.api.config.get().app.startShowWindow
-if (app.getLoginItemSettings().wasOpenedAsHidden) {
-  startHideWindow = true
-} else if (process.argv) {
-  const args = minimist(process.argv)
-  log.info('start args:', args)
-
-  // 通过启动参数,判断是否隐藏窗口
-  const hideWindowArg = `${args.hideWindow}`
-  if (hideWindowArg === 'true' || hideWindowArg === '1') {
+try {
+  app.disableHardwareAcceleration() // 禁用gpu
+
+  // 开启后是否默认隐藏window
+  let startHideWindow = !DevSidecar.api.config.get().app.startShowWindow
+  if (app.getLoginItemSettings().wasOpenedAsHidden) {
     startHideWindow = true
-  } else if (hideWindowArg === 'false' || hideWindowArg === '0') {
-    startHideWindow = false
-  }
-}
-log.info('startHideWindow = ', startHideWindow, ', app.getLoginItemSettings() = ', jsonApi.stringify2(app.getLoginItemSettings()))
-
-// 禁止双开
-const isFirstInstance = app.requestSingleInstanceLock()
-if (!isFirstInstance) {
-  log.info('is second instance')
-  setTimeout(() => {
-    app.quit()
-  }, 1000)
-} else {
-  app.on('before-quit', async () => {
-    log.info('before-quit')
-    if (process.platform === 'darwin') {
-      quit()
+  } else if (process.argv) {
+    const args = minimist(process.argv)
+    log.info('start args:', args)
+
+    // 通过启动参数,判断是否隐藏窗口
+    const hideWindowArg = `${args.hideWindow}`
+    if (hideWindowArg === 'true' || hideWindowArg === '1') {
+      startHideWindow = true
+    } else if (hideWindowArg === 'false' || hideWindowArg === '0') {
+      startHideWindow = false
     }
-  })
-  app.on('will-quit', () => {
-    log.info('应用关闭,注销所有快捷键')
-    globalShortcut.unregisterAll()
-  })
-  app.on('second-instance', (event, commandLine) => {
-    log.info('new app started, command:', commandLine)
-    if (win) {
-      showWin()
-      win.focus()
-    }
-  })
+  }
+  log.info('startHideWindow = ', startHideWindow, ', app.getLoginItemSettings() = ', jsonApi.stringify2(app.getLoginItemSettings()))
 
-  // Quit when all windows are closed.
-  app.on('window-all-closed', () => {
-    log.info('window-all-closed')
-    // On macOS it is common for applications and their menu bar
-    // to stay active until the user quits explicitly with Cmd + Q
-    if (process.platform !== 'darwin') {
-      quit()
-    }
-  })
+  // 禁止双开
+  const isFirstInstance = app.requestSingleInstanceLock()
+  if (!isFirstInstance) {
+    log.info('is second instance')
+    setTimeout(() => {
+      app.quit()
+    }, 1000)
+  } else {
+    app.on('before-quit', async () => {
+      log.info('before-quit')
+      if (process.platform === 'darwin') {
+        quit()
+      }
+    })
+    app.on('will-quit', () => {
+      log.info('应用关闭,注销所有快捷键')
+      globalShortcut.unregisterAll()
+    })
+    app.on('second-instance', (event, commandLine) => {
+      log.info('new app started, command:', commandLine)
+      if (win) {
+        showWin()
+        win.focus()
+      }
+    })
 
-  app.on('activate', () => {
-    // On macOS it's common to re-create a window in the app when the
-    // dock icon is clicked and there are no other windows open.
-    if (win == null) {
-      createWindow(false)
-    } else {
-      showWin()
-    }
-  })
+    // Quit when all windows are closed.
+    app.on('window-all-closed', () => {
+      log.info('window-all-closed')
+      // On macOS it is common for applications and their menu bar
+      // to stay active until the user quits explicitly with Cmd + Q
+      if (process.platform !== 'darwin') {
+        quit()
+      }
+    })
 
-  // initApp()
-
-  // This method will be called when Electron has finished
-  // initialization and is ready to create browser windows.
-  // Some APIs can only be used after this event occurs.
-  app.on('ready', async () => {
-    if (isDevelopment && !process.env.IS_TEST) {
-      // Install Vue Devtools
-      // try {
-      //   await installExtension(VUEJS_DEVTOOLS)
-      // } catch (e) {
-      //   log.error('Vue Devtools failed to install:', e.toString())
-      // }
-    }
-    try {
-      createWindow(startHideWindow)
-      const context = { win, app, beforeQuit, quit, ipcMain, dialog, log, api: DevSidecar.api, changeAppConfig }
-      backend.install(context) // 模块安装
-    } catch (err) {
-      log.info('error:', err)
-    }
+    app.on('activate', () => {
+      // On macOS it's common to re-create a window in the app when the
+      // dock icon is clicked and there are no other windows open.
+      if (win == null) {
+        createWindow(false)
+      } else {
+        showWin()
+      }
+    })
 
-    try {
-      // 最小化到托盘
-      tray = setTray()
-    } catch (err) {
-      log.info('error:', err)
-    }
+    // initApp()
+
+    // This method will be called when Electron has finished
+    // initialization and is ready to create browser windows.
+    // Some APIs can only be used after this event occurs.
+    app.on('ready', async () => {
+      if (isDevelopment && !process.env.IS_TEST) {
+        // Install Vue Devtools
+        // try {
+        //   await installExtension(VUEJS_DEVTOOLS)
+        // } catch (e) {
+        //   log.error('Vue Devtools failed to install:', e.toString())
+        // }
+      }
 
-    _powerMonitor.on('shutdown', async (e) => {
-      if (e) {
-        e.preventDefault()
+      try {
+        createWindow(startHideWindow)
+      } catch (err) {
+        log.error('createWindow error:', err)
       }
-      log.info('系统关机,恢复代理设置')
-      await quit()
-    })
-  })
-}
 
-initApp()
+      try {
+        const context = { win, app, beforeQuit, quit, ipcMain, dialog, log, api: DevSidecar.api, changeAppConfig }
+        backend.install(context) // 模块安装
+      } catch (err) {
+        log.error('install modules error:', err)
+      }
 
-// Exit cleanly on request from parent process in development mode.
-if (isDevelopment) {
-  if (process.platform === 'win32') {
-    process.on('message', (data) => {
-      if (data === 'graceful-exit') {
-        quit()
+      try {
+        // 最小化到托盘
+        tray = setTray()
+      } catch (err) {
+        log.error('setTray error:', err)
       }
+
+      _powerMonitor.on('shutdown', async (e) => {
+        if (e) {
+          e.preventDefault()
+        }
+        log.info('系统关机,恢复代理设置')
+        await quit()
+      })
     })
-  } else {
-    process.on('SIGINT', () => {
-      quit()
-    })
   }
+
+  initApp()
+
+  // Exit cleanly on request from parent process in development mode.
+  if (isDevelopment) {
+    if (process.platform === 'win32') {
+      process.on('message', (data) => {
+        if (data === 'graceful-exit') {
+          quit()
+        }
+      })
+    } else {
+      process.on('SIGINT', () => {
+        quit()
+      })
+    }
+  }
+  // 系统关机和重启时的操作
+  process.on('exit', () => {
+    log.info('进程结束,退出app')
+    quit()
+  })
+} catch (e) {
+  log.error('应用启动过程中,出现未知异常:', e)
 }
-// 系统关机和重启时的操作
-process.on('exit', () => {
-  log.info('进程结束,退出app')
-  quit()
-})
diff --git a/packages/mitmproxy/src/index.js b/packages/mitmproxy/src/index.js
index e46bd798c7..06d601392b 100644
--- a/packages/mitmproxy/src/index.js
+++ b/packages/mitmproxy/src/index.js
@@ -38,7 +38,7 @@ const api = {
         }
       })
       newServer.on('error', (e) => {
-        log.info('server error', e)
+        log.error('server error', e)
         // newServer = null
         fireError(e)
       })
diff --git a/packages/mitmproxy/src/lib/dns/ipaddress.js b/packages/mitmproxy/src/lib/dns/ipaddress.js
index 900995ed0a..3be81eec22 100644
--- a/packages/mitmproxy/src/lib/dns/ipaddress.js
+++ b/packages/mitmproxy/src/lib/dns/ipaddress.js
@@ -23,7 +23,7 @@ module.exports = class DNSOverIpAddress extends BaseDNS {
       log.info(`[dns] get ${hostname} ipaddress:${ip}`)
       return [ip]
     }
-    log.info(`[dns] get ${hostname} ipaddress: error`)
+    log.warn(`[dns] get ${hostname} ipaddress: error`)
     return null
 
     // const { answers } = await dnstls.query(hostname)
diff --git a/packages/mitmproxy/src/lib/interceptor/impl/req/baiduOcr.js b/packages/mitmproxy/src/lib/interceptor/impl/req/baiduOcr.js
index dcce58773b..063b81196e 100644
--- a/packages/mitmproxy/src/lib/interceptor/impl/req/baiduOcr.js
+++ b/packages/mitmproxy/src/lib/interceptor/impl/req/baiduOcr.js
@@ -175,7 +175,7 @@ module.exports = {
         next() // 异步执行完继续next
       }
     }).catch((err) => {
-      log.info('baiduOcr error:', err)
+      log.error('baiduOcr error:', err)
       res.writeHead(200, headers)
       res.write(`{"error_code": 999500, "error_msg": "${err}"}`) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
       res.end()