refactor: 1
parent
8f03430f62
commit
767b8a196a
|
@ -0,0 +1 @@
|
||||||
|
sysproxy.exe set 1
|
|
@ -11,6 +11,7 @@ const log = require('../../../utils/util.log')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const childProcess = require('child_process')
|
const childProcess = require('child_process')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
|
const fs = require('fs')
|
||||||
const _exec = util.promisify(childProcess.exec)
|
const _exec = util.promisify(childProcess.exec)
|
||||||
const _lanIP = [
|
const _lanIP = [
|
||||||
'localhost',
|
'localhost',
|
||||||
|
@ -35,35 +36,98 @@ const _lanIP = [
|
||||||
'192.168.*'
|
'192.168.*'
|
||||||
]
|
]
|
||||||
|
|
||||||
async function _winUnsetProxy (exec) {
|
const logoffScriptPath = 'C:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\scripts.ini'
|
||||||
// eslint-disable-next-line no-constant-condition
|
|
||||||
if (true) {
|
function buildScriptBody (addHeader = true, index = 0) {
|
||||||
const proxyPath = getProxyExePath()
|
const batPath = getClearBatPath()
|
||||||
await execFile(proxyPath, ['set', '1'])
|
const header = addHeader ? '[Logoff]\r\n' : ''
|
||||||
|
return `${header}${index}CmdLine=${batPath}
|
||||||
|
${index}Parameters=`
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addClearScriptIni () {
|
||||||
|
const batPath = getClearBatPath()
|
||||||
|
let body
|
||||||
|
if (fs.existsSync(logoffScriptPath)) {
|
||||||
|
body = fs.readFileSync(logoffScriptPath)
|
||||||
|
if (body.indexOf(batPath) >= 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let index = 0
|
||||||
|
// 要加新的
|
||||||
|
if (body.indexOf('[Logoff]') >= 0) {
|
||||||
|
// 如果有logoff了
|
||||||
|
const list = body.trim().split('\n')
|
||||||
|
let lastParameters = -1
|
||||||
|
for (const string of list) {
|
||||||
|
if (string.indexOf('Parameters') >= 0) {
|
||||||
|
lastParameters = parseInt(string.trim().charAt(0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index = lastParameters + 1
|
||||||
|
body += '\r\n' + buildScriptBody(false, index)
|
||||||
|
} else {
|
||||||
|
body += '\r\n' + buildScriptBody(true, 0)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body = buildScriptBody(true, 0)
|
||||||
|
}
|
||||||
|
fs.writeFileSync(logoffScriptPath, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeClearScriptIni () {
|
||||||
|
if (!fs.existsSync(logoffScriptPath)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const batPath = getClearBatPath()
|
||||||
|
let body = fs.readFileSync(logoffScriptPath)
|
||||||
|
if (body.indexOf(batPath) === -1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const regKey = new Registry({
|
|
||||||
hive: Registry.HKCU,
|
|
||||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
|
||||||
})
|
|
||||||
|
|
||||||
await Promise.all([
|
// 有就要删除
|
||||||
_winAsyncRegSet(regKey, 'ProxyEnable', Registry.REG_DWORD, 0),
|
body = body.trim()
|
||||||
_winAsyncRegSet(regKey, 'ProxyServer', Registry.REG_SZ, '')
|
const list = body.split('\n')
|
||||||
])
|
let lastParameters = -1
|
||||||
log.info('代理关闭成功,等待refresh')
|
for (const string of list) {
|
||||||
await exec(['echo "do refresh"', refreshInternetPs], { type: 'ps' })
|
if (string.indexOf(batPath) >= 0) {
|
||||||
log.info('代理关闭refresh完成')
|
lastParameters = parseInt(string.trim().charAt(0))
|
||||||
return true
|
}
|
||||||
|
}
|
||||||
|
body = body.replace(`${lastParameters}CmdLine=${batPath}`, '')
|
||||||
|
body = body.replace(`${lastParameters}Parameters=`, '')
|
||||||
|
body = body.trim()
|
||||||
|
fs.writeFileSync(logoffScriptPath, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getExtraPath () {
|
||||||
|
let extraPath = process.env.DS_EXTRA_PATH
|
||||||
|
log.info('extraPath', extraPath)
|
||||||
|
if (!extraPath) {
|
||||||
|
extraPath = __dirname
|
||||||
|
}
|
||||||
|
return extraPath
|
||||||
}
|
}
|
||||||
|
|
||||||
function getProxyExePath () {
|
function getProxyExePath () {
|
||||||
const proxyPath = process.env.DS_SYSPROXY_PATH
|
const extraPath = getExtraPath()
|
||||||
log.info('proxyPath', proxyPath)
|
return path.join(extraPath, 'sysproxy.exe')
|
||||||
if (proxyPath) {
|
}
|
||||||
return proxyPath
|
|
||||||
|
function getClearBatPath () {
|
||||||
|
const extraPath = getExtraPath()
|
||||||
|
return path.join(extraPath, 'clear.bat')
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _winUnsetProxy (exec) {
|
||||||
|
// eslint-disable-next-line no-constant-condition
|
||||||
|
const proxyPath = getProxyExePath()
|
||||||
|
await execFile(proxyPath, ['set', '1'])
|
||||||
|
try {
|
||||||
|
// await removeClearScriptIni()
|
||||||
|
} catch (e) {
|
||||||
|
log.error(e)
|
||||||
}
|
}
|
||||||
return path.join(__dirname, './sysproxy.exe')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _winSetProxy (exec, ip, port) {
|
async function _winSetProxy (exec, ip, port) {
|
||||||
|
@ -71,43 +135,17 @@ async function _winSetProxy (exec, ip, port) {
|
||||||
for (const string of _lanIP) {
|
for (const string of _lanIP) {
|
||||||
lanIpStr += string + ';'
|
lanIpStr += string + ';'
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line no-constant-condition
|
const proxyPath = getProxyExePath()
|
||||||
if (true) {
|
await execFile(proxyPath, ['global', `${ip}:${port}`, lanIpStr])
|
||||||
const proxyPath = getProxyExePath()
|
try {
|
||||||
await execFile(proxyPath, ['global', `${ip}:${port}`, lanIpStr])
|
// await addClearScriptIni()
|
||||||
return
|
} catch (e) {
|
||||||
|
log.error(e)
|
||||||
}
|
}
|
||||||
const regKey = new Registry({
|
|
||||||
hive: Registry.HKCU,
|
|
||||||
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
|
|
||||||
})
|
|
||||||
|
|
||||||
// log.info('lanIps:', lanIpStr, ip, port)
|
|
||||||
await Promise.all([
|
|
||||||
_winAsyncRegSet(regKey, 'MigrateProxy', Registry.REG_DWORD, 1),
|
|
||||||
_winAsyncRegSet(regKey, 'ProxyEnable', Registry.REG_DWORD, 1),
|
|
||||||
_winAsyncRegSet(regKey, 'ProxyHttp1.1', Registry.REG_DWORD, 0),
|
|
||||||
_winAsyncRegSet(regKey, 'ProxyServer', Registry.REG_SZ, `${ip}:${port}`),
|
|
||||||
_winAsyncRegSet(regKey, 'ProxyOverride', Registry.REG_SZ, lanIpStr)
|
|
||||||
])
|
|
||||||
log.info('代理设置成功,等待refresh')
|
|
||||||
await exec(['echo "do refresh"', refreshInternetPs], { type: 'ps' })
|
|
||||||
log.info('代理设置refresh完成')
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function _winAsyncRegSet (regKey, name, type, value) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
regKey.set(name, type, value, e => {
|
|
||||||
if (e) {
|
|
||||||
reject(e)
|
|
||||||
} else {
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const executor = {
|
const executor = {
|
||||||
async windows (exec, params) {
|
async windows (exec, params) {
|
||||||
if (params == null) {
|
if (params == null) {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
sysproxy.exe set 1
|
|
@ -160,12 +160,17 @@ function createWindow () {
|
||||||
hideWin()
|
hideWin()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
win.on('session-end', async (e) => {
|
||||||
|
log.info('session-end', e)
|
||||||
|
await quit(app)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function beforeQuit () {
|
async function beforeQuit () {
|
||||||
return DevSidecar.api.shutdown()
|
return DevSidecar.api.shutdown()
|
||||||
}
|
}
|
||||||
async function quit (app, callback) {
|
async function quit (app) {
|
||||||
if (tray) {
|
if (tray) {
|
||||||
tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' })
|
tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' })
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import JSON5 from 'json5'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
const pk = require('../../../package.json')
|
const pk = require('../../../package.json')
|
||||||
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
|
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
|
||||||
process.env.DS_SYSPROXY_PATH = path.join(__dirname, '../extra/sysproxy.exe')
|
process.env.DS_EXTRA_PATH = path.join(__dirname, '../extra/')
|
||||||
const log = require('../../utils/util.log')
|
const log = require('../../utils/util.log')
|
||||||
const getDefaultConfigBasePath = function () {
|
const getDefaultConfigBasePath = function () {
|
||||||
return DevSidecar.api.config.get().server.setting.userBasePath
|
return DevSidecar.api.config.get().server.setting.userBasePath
|
||||||
|
|
Loading…
Reference in New Issue