Skip to content

Commit

Permalink
bugfix: 当 @starknt/shutdown-handler-napi@starknt/sysproxy 出现异常…
Browse files Browse the repository at this point in the history
…时,将异常捕获掉并尝试使用替代方案执行,尽可能的避免DS运行异常 (#407)
  • Loading branch information
wangliang181230 authored Dec 3, 2024
1 parent 2bcf865 commit 58500d3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/shell/scripts/extra-path/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
46 changes: 38 additions & 8 deletions packages/core/src/shell/scripts/set-system-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`
Expand All @@ -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) {
// 设置全局代理所需的环境变量
Expand All @@ -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\'')
Expand Down
Binary file added packages/gui/extra/sysproxy.exe
Binary file not shown.
13 changes: 10 additions & 3 deletions packages/gui/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -191,7 +198,7 @@ function createWindow (startHideWindow) {
win.setMenu(null)

// !!IMPORTANT
if (isWindows) {
if (isWindows && typeof _powerMonitor.setupMainWindow === 'function') {
_powerMonitor.setupMainWindow(win)
}

Expand Down Expand Up @@ -261,6 +268,7 @@ function createWindow (startHideWindow) {
event.preventDefault()
// 切换开发者工具显示状态
switchDevTools()
// eslint-disable-next-line style/brace-style
}
// 按 F5,刷新页面
else if (input.key === 'F5') {
Expand Down Expand Up @@ -454,7 +462,6 @@ if (!isFirstInstance) {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')

await quit()
})
})
Expand Down
2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -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/**'

0 comments on commit 58500d3

Please sign in to comment.