perf: 优化win7代理开关的速度
parent
c782d11816
commit
f830b0f457
|
@ -3,11 +3,12 @@
|
|||
*/
|
||||
const Shell = require('../../shell')
|
||||
const execute = Shell.execute
|
||||
const execFile = Shell.execFile
|
||||
const Registry = require('winreg')
|
||||
// const cmd = require('node-cmd')
|
||||
const refreshInternetPs = require('./refresh-internet')
|
||||
const PowerShell = require('node-powershell')
|
||||
const log = require('../../../utils/util.log')
|
||||
const path = require('path')
|
||||
const _lanIP = [
|
||||
'localhost',
|
||||
'127.*',
|
||||
|
@ -33,6 +34,12 @@ const _lanIP = [
|
|||
]
|
||||
|
||||
async function _winUnsetProxy (exec) {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
if (true) {
|
||||
const proxyPath = path.resolve(__dirname, './extra/sysproxy.exe')
|
||||
await execFile(proxyPath, ['off'])
|
||||
return
|
||||
}
|
||||
const regKey = new Registry({
|
||||
hive: Registry.HKCU,
|
||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
||||
|
@ -48,16 +55,30 @@ async function _winUnsetProxy (exec) {
|
|||
return true
|
||||
}
|
||||
|
||||
function getProxyExePath () {
|
||||
const proxyPath = process.env.DS_SYSPROXY_PATH
|
||||
if (proxyPath) {
|
||||
return proxyPath
|
||||
}
|
||||
return path.join(__dirname, './sysproxy.exe')
|
||||
}
|
||||
|
||||
async function _winSetProxy (exec, ip, port) {
|
||||
let lanIpStr = ''
|
||||
for (const string of _lanIP) {
|
||||
lanIpStr += string + ';'
|
||||
}
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
if (true) {
|
||||
const proxyPath = getProxyExePath()
|
||||
await execFile(proxyPath, ['global', `${ip}:${port}`, lanIpStr])
|
||||
return
|
||||
}
|
||||
const regKey = new Registry({
|
||||
hive: Registry.HKCU,
|
||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
||||
})
|
||||
|
||||
let lanIpStr = ''
|
||||
for (const string of _lanIP) {
|
||||
lanIpStr += string + ';'
|
||||
}
|
||||
// log.info('lanIps:', lanIpStr, ip, port)
|
||||
await Promise.all([
|
||||
_winAsyncRegSet(regKey, 'MigrateProxy', Registry.REG_DWORD, 1),
|
||||
|
|
Binary file not shown.
|
@ -2,6 +2,7 @@ const util = require('util')
|
|||
const os = require('os')
|
||||
const childProcess = require('child_process')
|
||||
const _exec = childProcess.exec
|
||||
const _execFile = childProcess.execFile
|
||||
const exec = util.promisify(_exec)
|
||||
const PowerShell = require('node-powershell')
|
||||
const log = require('../utils/util.log')
|
||||
|
@ -119,8 +120,23 @@ async function execute (executor, args) {
|
|||
return executor[getSystemPlatform()](getSystemShell().exec, args)
|
||||
}
|
||||
|
||||
async function execFile (file, args, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
_execFile(file, args, options, (err, stdout) => {
|
||||
if (err) {
|
||||
log.error('文件执行出错:', file, err)
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
log.debug('执行成功:', stdout)
|
||||
resolve(stdout)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSystemShell,
|
||||
getSystemPlatform,
|
||||
execute
|
||||
execute,
|
||||
execFile
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue