optimize: 捕获所有文件保存失败的异常,并记录日志
parent
17cc3db33d
commit
f44dc790c7
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
},
|
||||
},
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 ,避免影响代理服务的启动速度
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue