try support arm
parent
6a5edf44bc
commit
9056bdc487
|
@ -16,6 +16,7 @@
|
||||||
"test": "mocha"
|
"test": "mocha"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@mihomo-party/sysproxy": "^2.0.4",
|
||||||
"fix-path": "^3.0.0",
|
"fix-path": "^3.0.0",
|
||||||
"iconv-lite": "^0.6.3",
|
"iconv-lite": "^0.6.3",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
|
|
|
@ -7,10 +7,9 @@ const request = require('request')
|
||||||
const Registry = require('winreg')
|
const Registry = require('winreg')
|
||||||
const log = require('../../../utils/util.log')
|
const log = require('../../../utils/util.log')
|
||||||
const Shell = require('../../shell')
|
const Shell = require('../../shell')
|
||||||
const extraPath = require('../extra-path/index')
|
const sysproxy = require('@mihomo-party/sysproxy')
|
||||||
|
|
||||||
const execute = Shell.execute
|
const execute = Shell.execute
|
||||||
const execFile = Shell.execFile
|
|
||||||
|
|
||||||
let config = null
|
let config = null
|
||||||
function loadConfig () {
|
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 () {
|
function getDomesticDomainAllowListTmpFilePath () {
|
||||||
return path.join(config.get().server.setting.userBasePath, '/domestic-domain-allowlist.txt')
|
return path.join(config.get().server.setting.userBasePath, '/domestic-domain-allowlist.txt')
|
||||||
}
|
}
|
||||||
|
@ -212,57 +181,68 @@ function getProxyExcludeIpStr (split) {
|
||||||
return excludeIpStr
|
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 = {
|
const executor = {
|
||||||
async windows (exec, params = {}) {
|
async windows (exec, params = {}) {
|
||||||
const { ip, port, setEnv } = params
|
const { ip, port, setEnv } = params
|
||||||
if (ip != null) { // 设置代理
|
if (ip != null) { // 设置代理
|
||||||
|
// 延迟加载config
|
||||||
|
loadConfig()
|
||||||
log.info('设置windows系统代理:', ip, port, setEnv)
|
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 { // 关闭代理
|
} else { // 关闭代理
|
||||||
log.info('关闭windows系统代理')
|
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 = {}) {
|
async linux (exec, params = {}) {
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
const childProcess = require('node:child_process')
|
const childProcess = require('node:child_process')
|
||||||
const os = require('node:os')
|
const os = require('node:os')
|
||||||
const fixPath = require('fix-path')
|
const fixPath = require('fix-path')
|
||||||
const iconv = require('iconv-lite')
|
|
||||||
const PowerShell = require('node-powershell')
|
const PowerShell = require('node-powershell')
|
||||||
const log = require('../utils/util.log')
|
const log = require('../utils/util.log')
|
||||||
|
|
||||||
const _execFile = childProcess.execFile
|
|
||||||
|
|
||||||
fixPath()
|
fixPath()
|
||||||
|
|
||||||
class SystemShell {
|
class SystemShell {
|
||||||
|
@ -64,7 +61,7 @@ class WindowsSystemShell extends SystemShell {
|
||||||
ps.dispose()
|
ps.dispose()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let compose = 'echo "test" ' // 'chcp 65001 '
|
let compose = 'chcp 65001' // 'chcp 65001 '
|
||||||
for (const cmd of cmds) {
|
for (const cmd of cmds) {
|
||||||
compose += ` && ${cmd}`
|
compose += ` && ${cmd}`
|
||||||
}
|
}
|
||||||
|
@ -76,9 +73,8 @@ class WindowsSystemShell extends SystemShell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function _childExec (composeCmds, options = {}) {
|
function childExec (composeCmds, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const childProcess = require('node:child_process')
|
|
||||||
log.info('shell:', composeCmds)
|
log.info('shell:', composeCmds)
|
||||||
childProcess.exec(composeCmds, options, (error, stdout, stderr) => {
|
childProcess.exec(composeCmds, options, (error, stdout, stderr) => {
|
||||||
if (error) {
|
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 () {
|
function getSystemShell () {
|
||||||
switch (getSystemPlatform()) {
|
switch (getSystemPlatform()) {
|
||||||
case 'mac':
|
case 'mac':
|
||||||
|
@ -135,6 +105,7 @@ function getSystemShell () {
|
||||||
throw new Error(`UNKNOWN OS TYPE ${os.platform()}`)
|
throw new Error(`UNKNOWN OS TYPE ${os.platform()}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSystemPlatform () {
|
function getSystemPlatform () {
|
||||||
switch (os.platform()) {
|
switch (os.platform()) {
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
|
@ -158,7 +129,7 @@ async function execute (executor, args) {
|
||||||
async function execFile (file, args, options) {
|
async function execFile (file, args, options) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
_execFile(file, args, options, (err, stdout) => {
|
childProcess.execFile(file, args, options, (err, stdout) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('文件执行出错:', file, err)
|
log.error('文件执行出错:', file, err)
|
||||||
reject(err)
|
reject(err)
|
||||||
|
|
|
@ -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)
|
|
||||||
})
|
|
|
@ -211,8 +211,8 @@ function createWindow (startHideWindow) {
|
||||||
hideWin()
|
hideWin()
|
||||||
}
|
}
|
||||||
|
|
||||||
win.on('closed', async () => {
|
win.on('closed', async (...args) => {
|
||||||
log.info('win closed:', arguments)
|
log.info('win closed:', ...args)
|
||||||
win = null
|
win = null
|
||||||
tray = null
|
tray = null
|
||||||
})
|
})
|
||||||
|
@ -225,8 +225,8 @@ function createWindow (startHideWindow) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
win.on('close', (e) => {
|
win.on('close', (e, ...args) => {
|
||||||
log.info('win close:', arguments)
|
log.info('win close:', e, ...args)
|
||||||
if (forceClose) {
|
if (forceClose) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -249,8 +249,8 @@ function createWindow (startHideWindow) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
win.on('session-end', async (e) => {
|
win.on('session-end', async (e, ...args) => {
|
||||||
log.info('win session-end:', arguments)
|
log.info('win session-end:', e, ...args)
|
||||||
await quit()
|
await quit()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -289,8 +289,8 @@ function createWindow (startHideWindow) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// 监听渲染进程发送过来的消息
|
// 监听渲染进程发送过来的消息
|
||||||
win.webContents.on('ipc-message', (event, channel, message) => {
|
win.webContents.on('ipc-message', (event, channel, message, ...args) => {
|
||||||
console.info('win ipc-message:', arguments)
|
console.info('win ipc-message:', event, channel, message, ...args)
|
||||||
if (channel === 'change-showHideShortcut') {
|
if (channel === 'change-showHideShortcut') {
|
||||||
registerShowHideShortcut(message)
|
registerShowHideShortcut(message)
|
||||||
}
|
}
|
||||||
|
@ -454,10 +454,7 @@ if (!isFirstInstance) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
}
|
}
|
||||||
log.info('系统关机,恢复代理设置')
|
log.info('系统关机,恢复代理设置')
|
||||||
if (isWindows) {
|
|
||||||
const Sysproxy = require('@mihomo-party/sysproxy')
|
|
||||||
Sysproxy.triggerManualProxy(false, '', 0, '')
|
|
||||||
}
|
|
||||||
await quit()
|
await quit()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue