optimize: 捕获所有文件保存失败的异常,并记录日志
parent
17cc3db33d
commit
f44dc790c7
|
@ -86,8 +86,14 @@ const configApi = {
|
||||||
|
|
||||||
if (remoteConfig != null) {
|
if (remoteConfig != null) {
|
||||||
const remoteSavePath = configLoader.getRemoteConfigPath(suffix)
|
const remoteSavePath = configLoader.getRemoteConfigPath(suffix)
|
||||||
|
try {
|
||||||
fs.writeFileSync(remoteSavePath, body)
|
fs.writeFileSync(remoteSavePath, body)
|
||||||
log.info('保存远程配置文件成功:', remoteSavePath)
|
log.info('保存远程配置文件成功:', remoteSavePath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存远程配置文件失败:', remoteSavePath, ', error:', e)
|
||||||
|
reject(new Error(`保存远程配置文件失败: ${e.message}`))
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn('远程配置对象为空:', remoteConfigUrl)
|
log.warn('远程配置对象为空:', remoteConfigUrl)
|
||||||
}
|
}
|
||||||
|
@ -153,8 +159,13 @@ const configApi = {
|
||||||
|
|
||||||
// 将差异作为用户配置保存到 config.json 中
|
// 将差异作为用户配置保存到 config.json 中
|
||||||
const configPath = configLoader.getUserConfigPath()
|
const configPath = configLoader.getUserConfigPath()
|
||||||
|
try {
|
||||||
fs.writeFileSync(configPath, jsonApi.stringify(diffConfig))
|
fs.writeFileSync(configPath, jsonApi.stringify(diffConfig))
|
||||||
log.info('保存 config.json 自定义配置文件成功:', configPath)
|
log.info('保存 config.json 自定义配置文件成功:', configPath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存 config.json 自定义配置文件失败:', configPath, ', error:', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
// 重载配置
|
// 重载配置
|
||||||
const allConfig = configApi.set(diffConfig)
|
const allConfig = configApi.set(diffConfig)
|
||||||
|
@ -206,14 +217,30 @@ const configApi = {
|
||||||
// 判断文件内容是否为空或空配置
|
// 判断文件内容是否为空或空配置
|
||||||
const fileStr = fileOriginalStr.replace(/\s/g, '')
|
const fileStr = fileOriginalStr.replace(/\s/g, '')
|
||||||
if (fileStr.length < 5) {
|
if (fileStr.length < 5) {
|
||||||
|
try {
|
||||||
fs.writeFileSync(configPath, '{}')
|
fs.writeFileSync(configPath, '{}')
|
||||||
|
} catch (e) {
|
||||||
|
log.warn('简化用户配置文件失败:', configPath, ', error:', e)
|
||||||
|
}
|
||||||
return false // config.json 内容为空,或为空json
|
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
|
||||||
|
}
|
||||||
// 原配置文件内容设为空
|
// 原配置文件内容设为空
|
||||||
|
try {
|
||||||
fs.writeFileSync(configPath, '{}')
|
fs.writeFileSync(configPath, '{}')
|
||||||
|
} catch (e) {
|
||||||
|
log.error('初始化用户配置文件失败:', configPath, ', error:', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
// 重新加载配置
|
// 重新加载配置
|
||||||
configApi.load(null)
|
configApi.load(null)
|
||||||
|
|
|
@ -76,8 +76,13 @@ const serverApi = {
|
||||||
// fireStatus('ing') // 启动中
|
// fireStatus('ing') // 启动中
|
||||||
const basePath = serverConfig.setting.userBasePath
|
const basePath = serverConfig.setting.userBasePath
|
||||||
const runningConfigPath = path.join(basePath, '/running.json')
|
const runningConfigPath = path.join(basePath, '/running.json')
|
||||||
|
try {
|
||||||
fs.writeFileSync(runningConfigPath, jsonApi.stringify(serverConfig))
|
fs.writeFileSync(runningConfigPath, jsonApi.stringify(serverConfig))
|
||||||
log.info('保存 running.json 运行时配置文件成功:', runningConfigPath)
|
log.info('保存 running.json 运行时配置文件成功:', runningConfigPath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存 running.json 运行时配置文件失败:', runningConfigPath, ', error:', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
const serverProcess = fork(mitmproxyPath, [runningConfigPath])
|
const serverProcess = fork(mitmproxyPath, [runningConfigPath])
|
||||||
server = {
|
server = {
|
||||||
id: serverProcess.pid,
|
id: serverProcess.pid,
|
||||||
|
|
|
@ -76,8 +76,13 @@ function loadLastModifiedTimeFromTxt (fileTxt) {
|
||||||
// 保存 国内域名白名单 内容到 `~/domestic-domain-allowlist.txt` 文件中
|
// 保存 国内域名白名单 内容到 `~/domestic-domain-allowlist.txt` 文件中
|
||||||
function saveDomesticDomainAllowListFile (fileTxt) {
|
function saveDomesticDomainAllowListFile (fileTxt) {
|
||||||
const filePath = getDomesticDomainAllowListTmpFilePath()
|
const filePath = getDomesticDomainAllowListTmpFilePath()
|
||||||
|
try {
|
||||||
fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n'))
|
fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n'))
|
||||||
log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath)
|
log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存 domestic-domain-allowlist.txt 文件失败:', filePath, ', error:', e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 尝试解析和修改 domestic-domain-allowlist.txt 文件时间
|
// 尝试解析和修改 domestic-domain-allowlist.txt 文件时间
|
||||||
const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt)
|
const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt)
|
||||||
|
@ -98,8 +103,6 @@ function saveDomesticDomainAllowListFile (fileTxt) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return filePath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDate (date) {
|
function formatDate (date) {
|
||||||
|
|
|
@ -104,7 +104,7 @@ const localApi = {
|
||||||
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
|
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
|
||||||
log.info('保存 setting.json 配置文件成功:', settingPath)
|
log.info('保存 setting.json 配置文件成功:', settingPath)
|
||||||
} catch (e) {
|
} 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))
|
fs.writeFileSync(filePath, jsonApi.stringify(config))
|
||||||
log.info('保存 automaticCompatibleConfig.json 成功:', filePath)
|
log.info('保存 automaticCompatibleConfig.json 成功:', filePath)
|
||||||
} catch (e) {
|
} 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` 文件中
|
// 保存 pac 内容到 `~/pac.txt` 文件中
|
||||||
function savePacFile (pacTxt) {
|
function savePacFile (pacTxt) {
|
||||||
const pacFilePath = getTmpPacFilePath()
|
const pacFilePath = getTmpPacFilePath()
|
||||||
|
try {
|
||||||
fs.writeFileSync(pacFilePath, pacTxt)
|
fs.writeFileSync(pacFilePath, pacTxt)
|
||||||
log.info('保存 pac.txt 文件成功:', pacFilePath)
|
log.info('保存 pac.txt 文件成功:', pacFilePath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存 pac.txt 文件失败:', pacFilePath, ', error:', e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 尝试解析和修改 pac.txt 文件时间
|
// 尝试解析和修改 pac.txt 文件时间
|
||||||
const lastModifiedTime = loadPacLastModifiedTime(pacTxt)
|
const lastModifiedTime = loadPacLastModifiedTime(pacTxt)
|
||||||
|
@ -90,8 +95,6 @@ function savePacFile (pacTxt) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return pacFilePath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 异步下载 pac.txt ,避免影响代理服务的启动速度
|
// 异步下载 pac.txt ,避免影响代理服务的启动速度
|
||||||
|
|
|
@ -250,7 +250,10 @@ utils.initCA = function ({ caCertPath, caKeyPath }) {
|
||||||
caKeyPath,
|
caKeyPath,
|
||||||
create: false,
|
create: false,
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e0) {
|
||||||
|
log.info('证书文件不存在,重新生成:', e0)
|
||||||
|
|
||||||
|
try {
|
||||||
const caObj = utils.createCA(config.caName)
|
const caObj = utils.createCA(config.caName)
|
||||||
|
|
||||||
const caCert = caObj.cert
|
const caCert = caObj.cert
|
||||||
|
@ -262,6 +265,10 @@ utils.initCA = function ({ caCertPath, caKeyPath }) {
|
||||||
fs.writeFileSync(caCertPath, certPem)
|
fs.writeFileSync(caCertPath, certPem)
|
||||||
fs.writeFileSync(caKeyPath, keyPem)
|
fs.writeFileSync(caKeyPath, keyPem)
|
||||||
log.info('生成证书文件成功,共2个文件:', caCertPath, caKeyPath)
|
log.info('生成证书文件成功,共2个文件:', caCertPath, caKeyPath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('生成证书文件失败:', caCertPath, caKeyPath, ', error:', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
caCertPath,
|
caCertPath,
|
||||||
|
|
Loading…
Reference in New Issue