bugfix: 当 `@starknt/shutdown-handler-napi` 和 `@starknt/sysproxy` 出现异常时,将异常捕获掉并尝试使用替代方案执行,尽可能的避免DS运行异常 (#407)

pull/410/head
王良 2024-12-03 11:10:38 +08:00 committed by GitHub
parent 2bcf865dd2
commit 58500d312c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 12 deletions

View File

@ -10,11 +10,17 @@ function getExtraPath () {
return extraPath
}
function getProxyExePath () {
const extraPath = getExtraPath()
return path.join(extraPath, 'sysproxy.exe')
}
function getEnableLoopbackPath () {
const extraPath = getExtraPath()
return path.join(extraPath, 'EnableLoopback.exe')
}
module.exports = {
getProxyExePath,
getEnableLoopbackPath,
}

View File

@ -7,8 +7,10 @@ const request = require('request')
const Registry = require('winreg')
const log = require('../../../utils/util.log')
const Shell = require('../../shell')
const extraPath = require('../extra-path/index')
const execute = Shell.execute
const execFile = Shell.execFile
let config = null
function loadConfig () {
@ -45,7 +47,7 @@ async function downloadDomesticDomainAllowListAsync () {
fileTxt = Buffer.from(fileTxt, 'base64').toString('utf8')
// log.debug('解析 base64 后的 domestic-domain-allowlist:', fileTxt)
}
} catch (e) {
} catch {
if (!fileTxt.includes('*.')) {
log.error(`远程 domestic-domain-allowlist.txt 文件内容即不是base64格式也不是要求的格式url: ${remoteFileUrl}body: ${body}`)
return
@ -80,7 +82,7 @@ function saveDomesticDomainAllowListFile (fileTxt) {
// 尝试解析和修改 domestic-domain-allowlist.txt 文件时间
const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt)
if (lastModifiedTime) {
fs.stat(filePath, (err, stats) => {
fs.stat(filePath, (err, _stats) => {
if (err) {
log.error('修改 domestic-domain-allowlist.txt 文件时间失败:', err)
return
@ -182,13 +184,12 @@ function getProxyExcludeIpStr (split) {
const executor = {
async windows (exec, params = {}) {
const sysproxy = require('@starknt/sysproxy')
const { ip, port, setEnv } = params
if (ip != null) { // 设置代理
// 延迟加载config
loadConfig()
log.info('设置windows系统代理:', ip, port, setEnv)
log.info('开始设置windows系统代理:', ip, port, setEnv)
// https
let proxyAddr = `https=http://${ip}:${port}`
@ -200,7 +201,22 @@ const executor = {
// 读取排除域名
const excludeIpStr = getProxyExcludeIpStr(';')
// 设置代理,同时设置排除域名
sysproxy.triggerManualProxyByUrl(true, proxyAddr, excludeIpStr)
try {
require('@starknt/sysproxy').triggerManualProxyByUrl(true, proxyAddr, excludeIpStr)
log.info(`设置windows系统代理成功: ${proxyAddr} ......(省略排除IP列表)`)
} catch (e1) {
log.warn('设置windows系统代理失败执行 `@starknt/sysproxy` 失败,现尝试通过执行 `sysproxy.exe global ...` 来设置系统代理!\r\n捕获的异常:', e1)
const proxyPath = extraPath.getProxyExePath()
const execFun = 'global'
try {
await execFile(proxyPath, [execFun, proxyAddr, excludeIpStr])
log.info(`设置windows系统代理成功执行的命令${proxyPath} ${execFun} ${proxyAddr} ......(省略排除IP列表)`)
} catch (e2) {
log.error(`设置windows系统代理失败执行的命令${proxyPath} ${execFun} ${proxyAddr} ......(省略排除IP列表), error:`, e2)
throw e1 // 将上面的异常抛出
}
}
if (setEnv) {
// 设置全局代理所需的环境变量
@ -223,8 +239,22 @@ const executor = {
return true
} else { // 关闭代理
log.info('关闭windows系统代理')
sysproxy.triggerManualProxy(false, '', 0, '')
try {
log.info('开始关闭windows系统代理')
require('@starknt/sysproxy').triggerManualProxy(false, '', 0, '')
log.info('关闭windows系统代理成功')
} catch (e1) {
log.error('关闭windows系统代理失败执行 `@starknt/sysproxy` 失败,现尝试通过执行 `sysproxy.exe set 1` 来关闭系统代理!\r\n捕获的异常:', e1)
try {
const proxyPath = extraPath.getProxyExePath()
await execFile(proxyPath, ['set', '1'])
log.info('关闭windows系统代理成功执行的命令sysproxy.exe set 1')
} catch (e2) {
log.error('关闭windows系统代理失败执行的命令sysproxy.exe set 1, error:', e2)
throw e1 // 将上面的异常抛出
}
}
try {
await exec('echo \'删除环境变量 HTTPS_PROXY、HTTP_PROXY\'')

Binary file not shown.

View File

@ -14,7 +14,14 @@ const isMac = process.platform === 'darwin'
const isDevelopment = process.env.NODE_ENV !== 'production'
// 避免其他系统出现异常,只有 Windows 使用 './background/powerMonitor'
const _powerMonitor = isWindows ? require('./background/powerMonitor').powerMonitor : powerMonitor
let _powerMonitor = powerMonitor
if (isWindows) {
try {
_powerMonitor = require('./background/powerMonitor').powerMonitor
} catch (e) {
log.error(`加载 './background/powerMonitor' 失败,现捕获异常并使用默认的 powerMonitor。\r\n目前启动着DS重启电脑时将无法正常关闭系统代理届时请自行关闭系统代理\r\n捕获的异常信息:`, e)
}
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
@ -191,7 +198,7 @@ function createWindow (startHideWindow) {
win.setMenu(null)
// !!IMPORTANT
if (isWindows) {
if (isWindows && typeof _powerMonitor.setupMainWindow === 'function') {
_powerMonitor.setupMainWindow(win)
}
@ -261,6 +268,7 @@ function createWindow (startHideWindow) {
event.preventDefault()
// 切换开发者工具显示状态
switchDevTools()
// eslint-disable-next-line style/brace-style
}
// 按 F5刷新页面
else if (input.key === 'F5') {
@ -454,7 +462,6 @@ if (!isFirstInstance) {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')
await quit()
})
})

View File

@ -1,5 +1,5 @@
packages:
# all packages in subdirs of packages/ and components/
# all packages in subdirectories of packages/ and components/
- packages/*
# exclude packages that are inside test directories
- '!**/test/**'