From 9056bdc4874a15faf45d167ee52453195af94d24 Mon Sep 17 00:00:00 2001 From: starknt <1431880400@qq.com> Date: Fri, 22 Nov 2024 03:23:01 +0800 Subject: [PATCH] try support arm --- packages/core/package.json | 1 + .../shell/scripts/set-system-proxy/index.js | 132 ++++++++---------- packages/core/src/shell/shell.js | 37 +---- packages/core/src/shell/test.js | 46 ------ packages/gui/src/background.js | 21 ++- 5 files changed, 70 insertions(+), 167 deletions(-) delete mode 100644 packages/core/src/shell/test.js diff --git a/packages/core/package.json b/packages/core/package.json index 3d1d598..28f44b4 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -16,6 +16,7 @@ "test": "mocha" }, "dependencies": { + "@mihomo-party/sysproxy": "^2.0.4", "fix-path": "^3.0.0", "iconv-lite": "^0.6.3", "lodash": "^4.17.21", 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 015124b..45b046c 100644 --- a/packages/core/src/shell/scripts/set-system-proxy/index.js +++ b/packages/core/src/shell/scripts/set-system-proxy/index.js @@ -7,10 +7,9 @@ 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 sysproxy = require('@mihomo-party/sysproxy') const execute = Shell.execute -const execFile = Shell.execFile let config = null function loadConfig () { @@ -19,36 +18,6 @@ function loadConfig () { } } -async function _winUnsetProxy (exec, setEnv) { - const proxyPath = extraPath.getProxyExePath() - await execFile(proxyPath, ['set', '1']) - - try { - await exec('echo \'删除环境变量 HTTPS_PROXY、HTTP_PROXY\'') - const regKey = new Registry({ // new operator is optional - hive: Registry.HKCU, // open registry hive HKEY_CURRENT_USER - key: '\\Environment', // key containing autostart programs - }) - regKey.get('HTTPS_PROXY', (err) => { - if (!err) { - regKey.remove('HTTPS_PROXY', async (err) => { - log.warn('删除环境变量 HTTPS_PROXY 失败:', err) - await exec('setx DS_REFRESH "1"') - }) - } - }) - regKey.get('HTTP_PROXY', (err) => { - if (!err) { - regKey.remove('HTTP_PROXY', async (err) => { - log.warn('删除环境变量 HTTP_PROXY 失败:', err) - }) - } - }) - } catch (e) { - log.error('删除环境变量 HTTPS_PROXY、HTTP_PROXY 失败:', e) - } -} - function getDomesticDomainAllowListTmpFilePath () { return path.join(config.get().server.setting.userBasePath, '/domestic-domain-allowlist.txt') } @@ -212,57 +181,68 @@ function getProxyExcludeIpStr (split) { return excludeIpStr } -async function _winSetProxy (exec, ip, port, setEnv) { - // 延迟加载config - loadConfig() - - const proxyPath = extraPath.getProxyExePath() - const execFun = 'global' - - // https - let proxyAddr = `https=http://${ip}:${port}` - // http - if (config.get().proxy.proxyHttp) { - proxyAddr = `http=http://${ip}:${port - 1};${proxyAddr}` - } - - // 读取排除域名 - const excludeIpStr = getProxyExcludeIpStr(';') - // 设置代理,同时设置排除域名 - log.info(`执行“设置系统代理”的程序: ${proxyPath} ${execFun} ${proxyAddr} ......(省略排除IP列表)`) - await execFile(proxyPath, [execFun, proxyAddr, excludeIpStr]) - - if (setEnv) { - // 设置全局代理所需的环境变量 - try { - await exec(`echo '设置环境变量 HTTPS_PROXY${config.get().proxy.proxyHttp ? '、HTTP_PROXY' : ''}'`) - - log.info(`开启系统代理的同时设置环境变量:HTTPS_PROXY = "http://${ip}:${port}/"`) - await exec(`setx HTTPS_PROXY "http://${ip}:${port}/"`) - - if (config.get().proxy.proxyHttp) { - log.info(`开启系统代理的同时设置环境变量:HTTP_PROXY = "http://${ip}:${port - 1}/"`) - await exec(`setx HTTP_PROXY "http://${ip}:${port - 1}/"`) - } - - // await addClearScriptIni() - } catch (e) { - log.error('设置环境变量 HTTPS_PROXY、HTTP_PROXY 失败:', e) - } - } - - return true -} - const executor = { async windows (exec, params = {}) { const { ip, port, setEnv } = params if (ip != null) { // 设置代理 + // 延迟加载config + loadConfig() log.info('设置windows系统代理:', ip, port, setEnv) - return _winSetProxy(exec, ip, port, setEnv) + // 读取排除域名 + const excludeIpStr = getProxyExcludeIpStr(';') + + sysproxy.triggerAutoProxy(true, ip, port, excludeIpStr) + + if (setEnv) { + // 设置全局代理所需的环境变量 + try { + await exec(`echo '设置环境变量 HTTPS_PROXY${config.get().proxy.proxyHttp ? '、HTTP_PROXY' : ''}'`) + + log.info(`开启系统代理的同时设置环境变量:HTTPS_PROXY = "http://${ip}:${port}/"`) + await exec(`setx HTTPS_PROXY "http://${ip}:${port}/"`) + + if (config.get().proxy.proxyHttp) { + log.info(`开启系统代理的同时设置环境变量:HTTP_PROXY = "http://${ip}:${port - 1}/"`) + await exec(`setx HTTP_PROXY "http://${ip}:${port - 1}/"`) + } + + // await addClearScriptIni() + } catch (e) { + log.error('设置环境变量 HTTPS_PROXY、HTTP_PROXY 失败:', e) + } + } + + return true } else { // 关闭代理 log.info('关闭windows系统代理') - return _winUnsetProxy(exec, setEnv) + sysproxy.triggerManualProxy(false, '', 0, '') + + try { + await exec('echo \'删除环境变量 HTTPS_PROXY、HTTP_PROXY\'') + const regKey = new Registry({ // new operator is optional + hive: Registry.HKCU, // open registry hive HKEY_CURRENT_USER + key: '\\Environment', // key containing autostart programs + }) + regKey.get('HTTPS_PROXY', (err) => { + if (!err) { + regKey.remove('HTTPS_PROXY', async (err) => { + log.warn('删除环境变量 HTTPS_PROXY 失败:', err) + await exec('setx DS_REFRESH "1"') + }) + } + }) + regKey.get('HTTP_PROXY', (err) => { + if (!err) { + regKey.remove('HTTP_PROXY', async (err) => { + log.warn('删除环境变量 HTTP_PROXY 失败:', err) + }) + } + }) + } catch (e) { + log.error('删除环境变量 HTTPS_PROXY、HTTP_PROXY 失败:', e) + } + + return true } }, async linux (exec, params = {}) { diff --git a/packages/core/src/shell/shell.js b/packages/core/src/shell/shell.js index 9ab1fac..7e0ccd7 100644 --- a/packages/core/src/shell/shell.js +++ b/packages/core/src/shell/shell.js @@ -1,12 +1,9 @@ const childProcess = require('node:child_process') const os = require('node:os') const fixPath = require('fix-path') -const iconv = require('iconv-lite') const PowerShell = require('node-powershell') const log = require('../utils/util.log') -const _execFile = childProcess.execFile - fixPath() class SystemShell { @@ -64,7 +61,7 @@ class WindowsSystemShell extends SystemShell { ps.dispose() } } else { - let compose = 'echo "test" ' // 'chcp 65001 ' + let compose = 'chcp 65001' // 'chcp 65001 ' for (const cmd of cmds) { compose += ` && ${cmd}` } @@ -76,9 +73,8 @@ class WindowsSystemShell extends SystemShell { } } -function _childExec (composeCmds, options = {}) { +function childExec (composeCmds, options = {}) { return new Promise((resolve, reject) => { - const childProcess = require('node:child_process') log.info('shell:', composeCmds) childProcess.exec(composeCmds, options, (error, stdout, stderr) => { if (error) { @@ -96,32 +92,6 @@ function _childExec (composeCmds, options = {}) { }) } -function childExec (composeCmds, options = {}) { - return new Promise((resolve, reject) => { - const encoding = 'cp936' - const binaryEncoding = 'binary' - - const childProcess = require('node:child_process') - log.info('shell:', composeCmds) - childProcess.exec(composeCmds, { encoding: binaryEncoding }, (error, stdout, stderr) => { - if (error) { - // console.log('------', decoder.decode(stderr)) - const message = iconv.decode(Buffer.from(stderr, binaryEncoding), encoding) - if (options.printErrorLog !== false) { - log.error('cmd 命令执行错误:\n------------------------------\ncommands:', composeCmds, '\n message:', message, '\n error:', error, '\n------------------------------') - } - reject(new Error(message)) - } else { - // log.info('cmd 命令完成:', stdout) - const message = iconv.decode(Buffer.from(stdout, binaryEncoding), encoding) - resolve(message) - } - // log.info('关闭 cmd') - // ps.kill('SIGINT') - }) - }) -} - function getSystemShell () { switch (getSystemPlatform()) { case 'mac': @@ -135,6 +105,7 @@ function getSystemShell () { throw new Error(`UNKNOWN OS TYPE ${os.platform()}`) } } + function getSystemPlatform () { switch (os.platform()) { case 'darwin': @@ -158,7 +129,7 @@ async function execute (executor, args) { async function execFile (file, args, options) { return new Promise((resolve, reject) => { try { - _execFile(file, args, options, (err, stdout) => { + childProcess.execFile(file, args, options, (err, stdout) => { if (err) { log.error('文件执行出错:', file, err) reject(err) diff --git a/packages/core/src/shell/test.js b/packages/core/src/shell/test.js deleted file mode 100644 index af7b20b..0000000 --- a/packages/core/src/shell/test.js +++ /dev/null @@ -1,46 +0,0 @@ -// const cmd1 = require('node-cmd') -// cmd1.get('set', -// function (err, data, stderr) { -// console.log('cmd complete:', err, data, stderr) -// if (err) { -// console.error('cmd 命令执行错误:', err, stderr) -// } else { -// console.log('cmd 命令执行结果:', data) -// } -// } -// ) - -// const process = require('child_process') -// -// const cmd = 'set' -// process.exec(cmd, function (error, stdout, stderr) { -// console.log('error:' + error) -// console.log('stdout:' + stdout) -// console.log('stderr:' + stderr) -// }) - -// const HttpsProxyAgent = require('https-proxy-agent') -// const proxy = 'http://user:pass@xxx.com:port' -// const agent = new HttpsProxyAgent(proxy) -// const https = require('https') -// https.get('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', (res) => { -// console.log('状态码:', res.statusCode) -// console.log('请求头:', res.headers) -// -// res.on('data', (d) => { -// process.stdout.write(d) -// }) -// }).on('error', (e) => { -// console.error(e) -// }) - -const fs = require('node:fs') -const request = require('request') - -request({ - url: 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', - proxy: 'http://127.0.0.1:31181', - ca: fs.readFileSync('C:/Users/Administrator/.dev-sidecar/dev-sidecar.ca.crt'), -}, (err, res, body) => { - console.log(body) -}) diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index 00af78b..ac7dcd1 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -211,8 +211,8 @@ function createWindow (startHideWindow) { hideWin() } - win.on('closed', async () => { - log.info('win closed:', arguments) + win.on('closed', async (...args) => { + log.info('win closed:', ...args) win = null tray = null }) @@ -225,8 +225,8 @@ function createWindow (startHideWindow) { } }) - win.on('close', (e) => { - log.info('win close:', arguments) + win.on('close', (e, ...args) => { + log.info('win close:', e, ...args) if (forceClose) { return } @@ -249,8 +249,8 @@ function createWindow (startHideWindow) { } }) - win.on('session-end', async (e) => { - log.info('win session-end:', arguments) + win.on('session-end', async (e, ...args) => { + log.info('win session-end:', e, ...args) await quit() }) @@ -289,8 +289,8 @@ function createWindow (startHideWindow) { }) // 监听渲染进程发送过来的消息 - win.webContents.on('ipc-message', (event, channel, message) => { - console.info('win ipc-message:', arguments) + win.webContents.on('ipc-message', (event, channel, message, ...args) => { + console.info('win ipc-message:', event, channel, message, ...args) if (channel === 'change-showHideShortcut') { registerShowHideShortcut(message) } @@ -454,10 +454,7 @@ if (!isFirstInstance) { e.preventDefault() } log.info('系统关机,恢复代理设置') - if (isWindows) { - const Sysproxy = require('@mihomo-party/sysproxy') - Sysproxy.triggerManualProxy(false, '', 0, '') - } + await quit() }) })