From f44dc790c7c036fb7aaa7bf4b0aa5e89e3e3e2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 24 Jan 2025 15:09:51 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20=E6=8D=95=E8=8E=B7=E6=89=80?= =?UTF-8?q?=E6=9C=89=E6=96=87=E4=BB=B6=E4=BF=9D=E5=AD=98=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=B9=B6=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config.js | 41 +++++++++++++++---- packages/core/src/modules/server/index.js | 9 +++- .../shell/scripts/set-system-proxy/index.js | 11 +++-- packages/gui/src/bridge/api/backend.js | 2 +- .../src/lib/proxy/compatible/compatible.js | 2 +- .../src/lib/proxy/middleware/overwall.js | 11 +++-- .../mitmproxy/src/lib/proxy/tls/tlsUtils.js | 27 +++++++----- 7 files changed, 74 insertions(+), 29 deletions(-) diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 7f1035a3..739f6b45 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -86,8 +86,14 @@ const configApi = { if (remoteConfig != null) { const remoteSavePath = configLoader.getRemoteConfigPath(suffix) - fs.writeFileSync(remoteSavePath, body) - log.info('保存远程配置文件成功:', remoteSavePath) + try { + fs.writeFileSync(remoteSavePath, body) + log.info('保存远程配置文件成功:', remoteSavePath) + } catch (e) { + log.error('保存远程配置文件失败:', remoteSavePath, ', error:', e) + reject(new Error(`保存远程配置文件失败: ${e.message}`)) + return + } } else { log.warn('远程配置对象为空:', remoteConfigUrl) } @@ -153,8 +159,13 @@ const configApi = { // 将差异作为用户配置保存到 config.json 中 const configPath = configLoader.getUserConfigPath() - fs.writeFileSync(configPath, jsonApi.stringify(diffConfig)) - log.info('保存 config.json 自定义配置文件成功:', configPath) + try { + fs.writeFileSync(configPath, jsonApi.stringify(diffConfig)) + log.info('保存 config.json 自定义配置文件成功:', configPath) + } catch (e) { + log.error('保存 config.json 自定义配置文件失败:', configPath, ', error:', e) + throw e + } // 重载配置 const allConfig = configApi.set(diffConfig) @@ -206,14 +217,30 @@ const configApi = { // 判断文件内容是否为空或空配置 const fileStr = fileOriginalStr.replace(/\s/g, '') if (fileStr.length < 5) { - fs.writeFileSync(configPath, '{}') + try { + fs.writeFileSync(configPath, '{}') + } catch (e) { + log.warn('简化用户配置文件失败:', configPath, ', error:', e) + } return false // config.json 内容为空,或为空json } // 备份用户自定义配置文件 - fs.writeFileSync(`${configPath}.${Date.now()}.bak.json`, fileOriginalStr) + const bakConfigPath = `${configPath}.${Date.now()}.bak.json` + try { + fs.writeFileSync(bakConfigPath, fileOriginalStr) + log.info('备份用户配置文件成功:', bakConfigPath) + } catch (e) { + log.error('备份用户配置文件失败:', bakConfigPath, ', error:', e) + throw e + } // 原配置文件内容设为空 - fs.writeFileSync(configPath, '{}') + try { + fs.writeFileSync(configPath, '{}') + } catch (e) { + log.error('初始化用户配置文件失败:', configPath, ', error:', e) + throw e + } // 重新加载配置 configApi.load(null) diff --git a/packages/core/src/modules/server/index.js b/packages/core/src/modules/server/index.js index fa0e4dbe..910c19af 100644 --- a/packages/core/src/modules/server/index.js +++ b/packages/core/src/modules/server/index.js @@ -76,8 +76,13 @@ const serverApi = { // fireStatus('ing') // 启动中 const basePath = serverConfig.setting.userBasePath const runningConfigPath = path.join(basePath, '/running.json') - fs.writeFileSync(runningConfigPath, jsonApi.stringify(serverConfig)) - log.info('保存 running.json 运行时配置文件成功:', runningConfigPath) + try { + fs.writeFileSync(runningConfigPath, jsonApi.stringify(serverConfig)) + log.info('保存 running.json 运行时配置文件成功:', runningConfigPath) + } catch (e) { + log.error('保存 running.json 运行时配置文件失败:', runningConfigPath, ', error:', e) + throw e + } const serverProcess = fork(mitmproxyPath, [runningConfigPath]) server = { id: serverProcess.pid, diff --git a/packages/core/src/shell/scripts/set-system-proxy/index.js b/packages/core/src/shell/scripts/set-system-proxy/index.js index 3dccea69..85201126 100644 --- a/packages/core/src/shell/scripts/set-system-proxy/index.js +++ b/packages/core/src/shell/scripts/set-system-proxy/index.js @@ -76,8 +76,13 @@ function loadLastModifiedTimeFromTxt (fileTxt) { // 保存 国内域名白名单 内容到 `~/domestic-domain-allowlist.txt` 文件中 function saveDomesticDomainAllowListFile (fileTxt) { const filePath = getDomesticDomainAllowListTmpFilePath() - fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n')) - log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath) + try { + fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n')) + log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath) + } catch (e) { + log.error('保存 domestic-domain-allowlist.txt 文件失败:', filePath, ', error:', e) + return + } // 尝试解析和修改 domestic-domain-allowlist.txt 文件时间 const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt) @@ -98,8 +103,6 @@ function saveDomesticDomainAllowListFile (fileTxt) { }) }) } - - return filePath } function formatDate (date) { diff --git a/packages/gui/src/bridge/api/backend.js b/packages/gui/src/bridge/api/backend.js index 379e5fdf..8d8bf950 100644 --- a/packages/gui/src/bridge/api/backend.js +++ b/packages/gui/src/bridge/api/backend.js @@ -104,7 +104,7 @@ const localApi = { fs.writeFileSync(settingPath, jsonApi.stringify(setting)) log.info('保存 setting.json 配置文件成功:', settingPath) } catch (e) { - log.error('保存 setting.json 配置文件失败:', settingPath, e) + log.error('保存 setting.json 配置文件失败:', settingPath, ', error:', e) } }, }, diff --git a/packages/mitmproxy/src/lib/proxy/compatible/compatible.js b/packages/mitmproxy/src/lib/proxy/compatible/compatible.js index 803c7301..db36757e 100644 --- a/packages/mitmproxy/src/lib/proxy/compatible/compatible.js +++ b/packages/mitmproxy/src/lib/proxy/compatible/compatible.js @@ -76,7 +76,7 @@ function _saveConfigToFile () { fs.writeFileSync(filePath, jsonApi.stringify(config)) log.info('保存 automaticCompatibleConfig.json 成功:', filePath) } catch (e) { - log.error('保存 automaticCompatibleConfig.json 失败:', filePath, e) + log.error('保存 automaticCompatibleConfig.json 失败:', filePath, ', error:', e) } } diff --git a/packages/mitmproxy/src/lib/proxy/middleware/overwall.js b/packages/mitmproxy/src/lib/proxy/middleware/overwall.js index 0b0d0700..8e53a783 100644 --- a/packages/mitmproxy/src/lib/proxy/middleware/overwall.js +++ b/packages/mitmproxy/src/lib/proxy/middleware/overwall.js @@ -68,8 +68,13 @@ function formatDate (date) { // 保存 pac 内容到 `~/pac.txt` 文件中 function savePacFile (pacTxt) { const pacFilePath = getTmpPacFilePath() - fs.writeFileSync(pacFilePath, pacTxt) - log.info('保存 pac.txt 文件成功:', pacFilePath) + try { + fs.writeFileSync(pacFilePath, pacTxt) + log.info('保存 pac.txt 文件成功:', pacFilePath) + } catch (e) { + log.error('保存 pac.txt 文件失败:', pacFilePath, ', error:', e) + return + } // 尝试解析和修改 pac.txt 文件时间 const lastModifiedTime = loadPacLastModifiedTime(pacTxt) @@ -90,8 +95,6 @@ function savePacFile (pacTxt) { }) }) } - - return pacFilePath } // 异步下载 pac.txt ,避免影响代理服务的启动速度 diff --git a/packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js b/packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js index a1738f83..a2d0d2eb 100644 --- a/packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js +++ b/packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js @@ -250,18 +250,25 @@ utils.initCA = function ({ caCertPath, caKeyPath }) { caKeyPath, create: false, } - } catch { - const caObj = utils.createCA(config.caName) + } catch (e0) { + log.info('证书文件不存在,重新生成:', e0) - const caCert = caObj.cert - const cakey = caObj.key + try { + const caObj = utils.createCA(config.caName) - const certPem = pki.certificateToPem(caCert) - const keyPem = pki.privateKeyToPem(cakey) - fs.mkdirSync(path.dirname(caCertPath), { recursive: true }) - fs.writeFileSync(caCertPath, certPem) - fs.writeFileSync(caKeyPath, keyPem) - log.info('生成证书文件成功,共2个文件:', caCertPath, caKeyPath) + const caCert = caObj.cert + const cakey = caObj.key + + const certPem = pki.certificateToPem(caCert) + const keyPem = pki.privateKeyToPem(cakey) + fs.mkdirSync(path.dirname(caCertPath), { recursive: true }) + fs.writeFileSync(caCertPath, certPem) + fs.writeFileSync(caKeyPath, keyPem) + log.info('生成证书文件成功,共2个文件:', caCertPath, caKeyPath) + } catch (e) { + log.error('生成证书文件失败:', caCertPath, caKeyPath, ', error:', e) + throw e + } } return { caCertPath,