diff --git a/packages/core/src/shell/scripts/extra-path/index.js b/packages/core/src/shell/scripts/extra-path/index.js index 2311609170..48ad7ffe95 100644 --- a/packages/core/src/shell/scripts/extra-path/index.js +++ b/packages/core/src/shell/scripts/extra-path/index.js @@ -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, } diff --git a/packages/core/src/shell/scripts/set-system-proxy/index.js b/packages/core/src/shell/scripts/set-system-proxy/index.js index 91df57c6c8..6002d4fc06 100644 --- a/packages/core/src/shell/scripts/set-system-proxy/index.js +++ b/packages/core/src/shell/scripts/set-system-proxy/index.js @@ -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\'') diff --git a/packages/gui/extra/sysproxy.exe b/packages/gui/extra/sysproxy.exe new file mode 100644 index 0000000000..979858ba78 Binary files /dev/null and b/packages/gui/extra/sysproxy.exe differ diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index ac7dcd1518..512791296d 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -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() }) }) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 0a66cabac8..07e62abb8c 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -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/**'