Skip to content

Commit

Permalink
optimize: 捕获所有文件保存失败的异常,并记录日志
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jan 24, 2025
1 parent 17cc3db commit f44dc79
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 29 deletions.
41 changes: 34 additions & 7 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/modules/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/shell/scripts/set-system-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -98,8 +103,6 @@ function saveDomesticDomainAllowListFile (fileTxt) {
})
})
}

return filePath
}

function formatDate (date) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gui/src/bridge/api/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/proxy/compatible/compatible.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/mitmproxy/src/lib/proxy/middleware/overwall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -90,8 +95,6 @@ function savePacFile (pacTxt) {
})
})
}

return pacFilePath
}

// 异步下载 pac.txt ,避免影响代理服务的启动速度
Expand Down
27 changes: 17 additions & 10 deletions packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f44dc79

Please sign in to comment.