perf: 优化win7代理开关的速度
parent
c782d11816
commit
f830b0f457
|
@ -3,11 +3,12 @@
|
||||||
*/
|
*/
|
||||||
const Shell = require('../../shell')
|
const Shell = require('../../shell')
|
||||||
const execute = Shell.execute
|
const execute = Shell.execute
|
||||||
|
const execFile = Shell.execFile
|
||||||
const Registry = require('winreg')
|
const Registry = require('winreg')
|
||||||
// const cmd = require('node-cmd')
|
|
||||||
const refreshInternetPs = require('./refresh-internet')
|
const refreshInternetPs = require('./refresh-internet')
|
||||||
const PowerShell = require('node-powershell')
|
const PowerShell = require('node-powershell')
|
||||||
const log = require('../../../utils/util.log')
|
const log = require('../../../utils/util.log')
|
||||||
|
const path = require('path')
|
||||||
const _lanIP = [
|
const _lanIP = [
|
||||||
'localhost',
|
'localhost',
|
||||||
'127.*',
|
'127.*',
|
||||||
|
@ -33,6 +34,12 @@ const _lanIP = [
|
||||||
]
|
]
|
||||||
|
|
||||||
async function _winUnsetProxy (exec) {
|
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({
|
const regKey = new Registry({
|
||||||
hive: Registry.HKCU,
|
hive: Registry.HKCU,
|
||||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
||||||
|
@ -48,16 +55,30 @@ async function _winUnsetProxy (exec) {
|
||||||
return true
|
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) {
|
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({
|
const regKey = new Registry({
|
||||||
hive: Registry.HKCU,
|
hive: Registry.HKCU,
|
||||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
||||||
})
|
})
|
||||||
|
|
||||||
let lanIpStr = ''
|
|
||||||
for (const string of _lanIP) {
|
|
||||||
lanIpStr += string + ';'
|
|
||||||
}
|
|
||||||
// log.info('lanIps:', lanIpStr, ip, port)
|
// log.info('lanIps:', lanIpStr, ip, port)
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
_winAsyncRegSet(regKey, 'MigrateProxy', Registry.REG_DWORD, 1),
|
_winAsyncRegSet(regKey, 'MigrateProxy', Registry.REG_DWORD, 1),
|
||||||
|
|
Binary file not shown.
|
@ -2,6 +2,7 @@ const util = require('util')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
const childProcess = require('child_process')
|
const childProcess = require('child_process')
|
||||||
const _exec = childProcess.exec
|
const _exec = childProcess.exec
|
||||||
|
const _execFile = childProcess.execFile
|
||||||
const exec = util.promisify(_exec)
|
const exec = util.promisify(_exec)
|
||||||
const PowerShell = require('node-powershell')
|
const PowerShell = require('node-powershell')
|
||||||
const log = require('../utils/util.log')
|
const log = require('../utils/util.log')
|
||||||
|
@ -119,8 +120,23 @@ async function execute (executor, args) {
|
||||||
return executor[getSystemPlatform()](getSystemShell().exec, 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 = {
|
module.exports = {
|
||||||
getSystemShell,
|
getSystemShell,
|
||||||
getSystemPlatform,
|
getSystemPlatform,
|
||||||
execute
|
execute,
|
||||||
|
execFile
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue