diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index 9ef515c..b7ba710 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -236,7 +236,9 @@ module.exports = { '*github.com': 'quad9', '*.vuepress.vuejs.org': 'quad9', 'gh.docmirror.top': 'quad9', - '*v2ex.com': 'quad9' + '*v2ex.com': 'quad9', + '*pypi.org':'quad9', + '*jetbrains.com':'quad9' }, speedTest: { enabled: true, diff --git a/packages/core/src/modules/proxy/index.js b/packages/core/src/modules/proxy/index.js index 56284e3..3bc05e0 100644 --- a/packages/core/src/modules/proxy/index.js +++ b/packages/core/src/modules/proxy/index.js @@ -28,6 +28,12 @@ const ProxyPlugin = function (context) { log.error('关闭系统代理失败', err) return false } + }, + + async setEnableLoopback () { + await shell.enableLoopback() + log.info('打开EnableLoopback成功') + return true } } return api diff --git a/packages/core/src/shell/index.js b/packages/core/src/shell/index.js index c7cfec5..41355c0 100644 --- a/packages/core/src/shell/index.js +++ b/packages/core/src/shell/index.js @@ -6,6 +6,8 @@ const setSystemEnv = require('./scripts/set-system-env') const getNpmEnv = require('./scripts/get-npm-env') const setNpmEnv = require('./scripts/set-npm-env') const setSystemProxy = require('./scripts/set-system-proxy/index') +const enableLoopback = require('./scripts/enable-loopback') +const extraPath = require('./scripts/extra-path') module.exports = { killByPort, setupCa, @@ -14,6 +16,8 @@ module.exports = { getNpmEnv, setNpmEnv, setSystemProxy, + enableLoopback, + extraPath, async exec (cmds, args) { return shell.getSystemShell().exec(cmds, args) }, diff --git a/packages/core/src/shell/scripts/enable-loopback.js b/packages/core/src/shell/scripts/enable-loopback.js new file mode 100644 index 0000000..42d380d --- /dev/null +++ b/packages/core/src/shell/scripts/enable-loopback.js @@ -0,0 +1,22 @@ +/** + */ +const Shell = require('../shell') +const extraPath = require('./extra-path') +const execute = Shell.execute +const executor = { + async windows (exec) { + const loopbackPath = extraPath.getEnableLoopbackPath() + const execFile = Shell.execFile + await execFile(loopbackPath) + }, + async linux (exec, { port }) { + throw Error('不支持此操作') + }, + async mac (exec, { port }) { + throw Error('不支持此操作') + } +} + +module.exports = async function (args) { + return execute(executor, args) +} diff --git a/packages/core/src/shell/scripts/extra-path/EnableLoopback.exe b/packages/core/src/shell/scripts/extra-path/EnableLoopback.exe new file mode 100644 index 0000000..1dcac71 Binary files /dev/null and b/packages/core/src/shell/scripts/extra-path/EnableLoopback.exe differ diff --git a/packages/core/src/shell/scripts/set-system-proxy/clear.bat b/packages/core/src/shell/scripts/extra-path/clear.bat similarity index 100% rename from packages/core/src/shell/scripts/set-system-proxy/clear.bat rename to packages/core/src/shell/scripts/extra-path/clear.bat diff --git a/packages/core/src/shell/scripts/extra-path/index.js b/packages/core/src/shell/scripts/extra-path/index.js new file mode 100644 index 0000000..eb890d2 --- /dev/null +++ b/packages/core/src/shell/scripts/extra-path/index.js @@ -0,0 +1,31 @@ +const log = require('../../../utils/util.log') +const path = require('path') + +function getExtraPath () { + let extraPath = process.env.DS_EXTRA_PATH + log.info('extraPath', extraPath) + if (!extraPath) { + extraPath = __dirname + } + return extraPath +} + +function getProxyExePath () { + const extraPath = getExtraPath() + return path.join(extraPath, 'sysproxy.exe') +} + +function getClearBatPath () { + const extraPath = getExtraPath() + return path.join(extraPath, 'clear.bat') +} + +function getEnableLoopbackPath () { + const extraPath = getExtraPath() + return path.join(extraPath, 'EnableLoopback.exe') +} +module.exports = { + getProxyExePath, + getEnableLoopbackPath, + getClearBatPath +} diff --git a/packages/core/src/shell/scripts/set-system-proxy/sysproxy.exe b/packages/core/src/shell/scripts/extra-path/sysproxy.exe similarity index 100% rename from packages/core/src/shell/scripts/set-system-proxy/sysproxy.exe rename to packages/core/src/shell/scripts/extra-path/sysproxy.exe 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 8cee8a8..a00713d 100644 --- a/packages/core/src/shell/scripts/set-system-proxy/index.js +++ b/packages/core/src/shell/scripts/set-system-proxy/index.js @@ -13,6 +13,7 @@ const childProcess = require('child_process') const util = require('util') const fs = require('fs') const _exec = util.promisify(childProcess.exec) +const extraPath = require('../extra-path/index') const _lanIP = [ 'localhost', '127.*', @@ -33,95 +34,13 @@ const _lanIP = [ '172.29.*', '172.30.*', '172.31.*', - '192.168.*' + '192.168.*', + '<-loopback>' ] -const logoffScriptPath = 'C:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\scripts.ini' - -function buildScriptBody (addHeader = true, index = 0) { - const batPath = getClearBatPath() - 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 - } - - // 有就要删除 - body = body.trim() - const list = body.split('\n') - let lastParameters = -1 - for (const string of list) { - if (string.indexOf(batPath) >= 0) { - lastParameters = parseInt(string.trim().charAt(0)) - } - } - 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 () { - const extraPath = getExtraPath() - return path.join(extraPath, 'sysproxy.exe') -} - -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() + const proxyPath = extraPath.getProxyExePath() await execFile(proxyPath, ['set', '1']) try { // await removeClearScriptIni() @@ -135,7 +54,7 @@ async function _winSetProxy (exec, ip, port) { for (const string of _lanIP) { lanIpStr += string + ';' } - const proxyPath = getProxyExePath() + const proxyPath = extraPath.getProxyExePath() await execFile(proxyPath, ['global', `${ip}:${port}`, lanIpStr]) try { // await addClearScriptIni() diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock index 5c88eb2..8167af4 100644 --- a/packages/core/yarn.lock +++ b/packages/core/yarn.lock @@ -93,10 +93,10 @@ lodash "^4.17.19" to-fast-properties "^2.0.0" -"@docmirror/mitmproxy@^1.5.1": - version "1.5.1" - resolved "https://registry.nlark.com/@docmirror/mitmproxy/download/@docmirror/mitmproxy-1.5.1.tgz#357142a41b89266491c3519a7528b9b83dc30c85" - integrity sha1-NXFCpBuJJmSRw1GadSi5uD3DDIU= +"@docmirror/mitmproxy@^1.6.2": + version "1.6.2" + resolved "https://registry.yarnpkg.com/@docmirror/mitmproxy/-/mitmproxy-1.6.2.tgz#51c7792d7d172820250822abbaacf148e629666c" + integrity sha512-JF/cJzHuV23TTN1oS+r32Qole/u37H8H36ycO06IjxmOfXx5pu0nqeOTMe3bWTXLI1YlnlktVNOH72xH73C1Fg== dependencies: agentkeepalive "^2.1.1" axios "^0.21.1" diff --git a/packages/gui/package.json b/packages/gui/package.json index e3a190b..9be3229 100644 --- a/packages/gui/package.json +++ b/packages/gui/package.json @@ -30,6 +30,7 @@ "electron-baidu-tongji": "^1.0.5", "electron-reload": "^1.5.0", "electron-store": "^6.0.1", + "electron-sudo": "^4.0.12", "electron-updater": "^4.3.5", "es-abstract": "^1.17.7", "extract-zip": "^2.0.1", diff --git a/packages/gui/public/loopback.png b/packages/gui/public/loopback.png new file mode 100644 index 0000000..c78eacd Binary files /dev/null and b/packages/gui/public/loopback.png differ diff --git a/packages/gui/src/bridge/api/open-enable-loopback.js b/packages/gui/src/bridge/api/open-enable-loopback.js new file mode 100644 index 0000000..666aa8d --- /dev/null +++ b/packages/gui/src/bridge/api/open-enable-loopback.js @@ -0,0 +1,12 @@ +import Sudoer from 'electron-sudo' +import DevSidecar from '@docmirror/dev-sidecar' +export default { + async open () { + const options = { name: '设置loopback' } + const sudoer = new Sudoer(options) + const exeFile = DevSidecar.api.shell.extraPath.getEnableLoopbackPath() + await sudoer.exec( + exeFile, { env: { PARAM: 'VALUE' } } + ) + } +} diff --git a/packages/gui/src/view/pages/index.vue b/packages/gui/src/view/pages/index.vue index b81b0c5..d6847b8 100644 --- a/packages/gui/src/view/pages/index.vue +++ b/packages/gui/src/view/pages/index.vue @@ -86,8 +86,8 @@ 捐赠
+