diff --git a/package.json b/package.json index 4508701..b9b4ec8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-ssr", - "version": "0.3.0-alpha.5", + "version": "0.3.0-alpha.6", "description": "Cross platform ShadowsocksR GUI client built with electron", "author": { "name": "The Electron-SSR Authors", diff --git a/src/main/client.js b/src/main/client.js index 83a6161..a83baaf 100644 --- a/src/main/client.js +++ b/src/main/client.js @@ -9,7 +9,7 @@ import { isConfigEqual } from '../shared/utils' import { showNotification } from './notification' import { toggleEnable } from './tray-handler' import * as i18n from './locales' -import { isWin } from '@/shared/env' +import { isWin, pythonName } from '@/shared/env' const $t = i18n.default let quitByCommand = false /** @@ -22,7 +22,7 @@ let pythonSSRInstance * @param {string[]} params 待执行的shell命令 */ function runPythonSSR (params) { - let command = 'python' + let command = pythonName const commandStr = `${command} ${params.join(' ')}` logger.info('run command: %s', commandStr.replace(/-k [\d\w]* /, '-k ****** ')) if (isWin) { diff --git a/src/main/http-proxy.js b/src/main/http-proxy.js index fd15c36..47f4403 100644 --- a/src/main/http-proxy.js +++ b/src/main/http-proxy.js @@ -18,7 +18,7 @@ let socks2httpInstance * @param {Object} appConfig 应用配置 */ async function startHttpProxyServer (appConfig, isProxyStarted) { - if (isProxyStarted && appConfig.httpProxyEnable) { + if (isProxyStarted) { const host = appConfig.shareOverLan ? '0.0.0.0' : '127.0.0.1' try { await ensureHostPortValid(host, appConfig.httpProxyPort) @@ -87,7 +87,7 @@ appConfig$.subscribe(data => { startHttpProxyServer(appConfig, isProxyStarted) } else { // 数据变更 - if (['shareOverLan', 'httpProxyEnable', 'httpProxyPort'].some(key => changed.indexOf(key) > -1) || isProxyStarted !== isOldProxyStarted) { + if (['shareOverLan', 'httpProxyPort'].some(key => changed.indexOf(key) > -1) || isProxyStarted !== isOldProxyStarted) { stopHttpProxyServer().then(() => { startHttpProxyServer(appConfig, isProxyStarted) }) diff --git a/src/main/pac.js b/src/main/pac.js index 88b1c22..830717e 100644 --- a/src/main/pac.js +++ b/src/main/pac.js @@ -16,10 +16,8 @@ const $t = i18n.default let pacServer httpShutdown.extend() - -/** - * 下载pac文件 - */ +const replacePac = (str) => str.replace(/__PROXY__/g, + `SOCKS5 127.0.0.1:${currentConfig.localPort}; SOCKS 127.0.0.1:${currentConfig.localPort}; PROXY 127.0.0.1:${currentConfig.localPort}; PROXY 127.0.0.1:${currentConfig.httpProxyPort}; DIRECT`) export async function downloadPac (force = false) { await bootstrapPromise const pacExisted = await pathExists(pacRawPath) @@ -33,13 +31,12 @@ export async function downloadPac (force = false) { // always gen pac from raw pac = (await readFile(pacRawPath)).toString() } - pac = pac.replace(/__PROXY__/g, - `SOCKS5 127.0.0.1:${currentConfig.localPort}; SOCKS 127.0.0.1:${currentConfig.localPort}; PROXY 127.0.0.1:${currentConfig.localPort}; ${currentConfig.httpProxyEnable ? 'PROXY 127.0.0.1:' + currentConfig.httpProxyPort + ';' : ''} DIRECT`) + pac = replacePac(pac) await writeFile(pacPath, pac) } async function updatePacProxy () { let content = (await readFile(pacRawPath)).toString() - content = content.replace(/__PROXY__/g, `SOCKS5 127.0.0.1:${currentConfig.localPort}; SOCKS 127.0.0.1:${currentConfig.localPort}; PROXY 127.0.0.1:${currentConfig.localPort}; ${currentConfig.httpProxyEnable ? 'PROXY 127.0.0.1:' + currentConfig.httpProxyPort + ';' : ''} DIRECT`) + content = replacePac(content) await writeFile(pacPath, content) } let ensurePacPromise = null @@ -125,7 +122,7 @@ appConfig$.subscribe(data => { serverPac(appConfig, isProxyStarted) }) } - if (['localPort', 'httpProxyEnable', 'httpProxyPort'].some(key => changed.indexOf(key) > -1)) { + if (['localPort', 'httpProxyPort'].some(key => changed.indexOf(key) > -1)) { console.log('Ported UPdated') updatePacProxy() } diff --git a/src/main/proxy.js b/src/main/proxy.js index 8730d63..11172f3 100644 --- a/src/main/proxy.js +++ b/src/main/proxy.js @@ -65,14 +65,18 @@ export async function setProxyToNone (force = true) { /** * 设置代理为全局 */ -export async function setProxyToGlobal (host, port) { +async function setProxyToGlobal (host, port) { let command if (isWin && await pathExists(winToolPath)) { command = `${winToolPath} global ${host}:${port} "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;"` } else if (isMac && await pathExists(macToolPath) && !await isOldMacVersion) { command = `"${macToolPath}" -m global -p ${port}` } else if (isLinux && await isGsettingsAvaliable) { - command = `gsettings set org.gnome.system.proxy mode 'manual' && gsettings set org.gnome.system.proxy.socks host '${host}' && gsettings set org.gnome.system.proxy.socks port ${port}` + if (currentConfig.preferHTTPGlobal === 1) { + command = `gsettings set org.gnome.system.proxy mode 'manual' && gsettings set org.gnome.system.proxy.http host '${host}' && gsettings set org.gnome.system.proxy.http port ${port} && gsettings set org.gnome.system.proxy.https host '${host}' && gsettings set org.gnome.system.proxy.https port ${port}` + } else { + command = `gsettings set org.gnome.system.proxy mode 'manual' && gsettings set org.gnome.system.proxy.socks host '${host}' && gsettings set org.gnome.system.proxy.socks port ${port}` + } } await runCommand(command) } @@ -80,7 +84,7 @@ export async function setProxyToGlobal (host, port) { /** * 设置代理为PAC代理 */ -export async function setProxyToPac (pacUrl) { +async function setProxyToPac (pacUrl) { let command if (isWin && await pathExists(winToolPath)) { command = `${winToolPath} pac ${pacUrl}` @@ -98,7 +102,7 @@ async function setProxyByMode (mode) { } else if (mode === 1) { await setProxyToPac(`http://127.0.0.1:${currentConfig.pacPort}/proxy.pac`) } else if (mode === 2) { - await setProxyToGlobal('127.0.0.1', currentConfig.preferHTTPGlobal ? currentConfig.httpProxyPort : currentConfig.localPort) + await setProxyToGlobal('127.0.0.1', (isWin || currentConfig.preferHTTPGlobal) ? currentConfig.httpProxyPort : currentConfig.localPort) } } @@ -116,9 +120,6 @@ export function startProxy (mode) { if (mode === undefined) { mode = currentConfig.sysProxyMode } - if (isWin && mode === 2 && currentConfig.preferHTTPGlobal === 1 && !currentConfig.httpProxyEnable) { - updateAppConfig({ httpProxyEnable: true }) - } setProxyByMode(mode) } diff --git a/src/main/tray.js b/src/main/tray.js index 1503774..e8c25a8 100644 --- a/src/main/tray.js +++ b/src/main/tray.js @@ -1,5 +1,5 @@ -import { Menu, nativeImage, Tray, dialog } from 'electron' -import { appConfig$, updateAppConfig } from './data' +import { Menu, nativeImage, Tray } from 'electron' +import { appConfig$ } from './data' import * as handler from './tray-handler' import { groupConfigs } from '@/shared/utils' import { isMac, isWin, isOldMacVersion } from '../shared/env' @@ -121,20 +121,6 @@ export function changeProxy (e, mode, appConfig) { if (mode === appConfig.sysProxyMode) { e.checked = true } else { - if (isWin && mode === 2 && appConfig.preferHTTPGlobal === -1) { - dialog.showMessageBox({ - title: $t('MENU_SUB_GLOBAL_PROXY'), - message: $t('NOTO_WIND_GLOBAL_PROXY'), - buttons: ['Yes', 'No'], - type: 'info' - }).then((response) => { - if (response.response === 0) { - updateAppConfig({ preferHTTPGlobal: 1 }) - } else { - updateAppConfig({ preferHTTPGlobal: 0 }) - } - }) - } handler.toggleProxy(mode) } } diff --git a/src/renderer/views/Setup.vue b/src/renderer/views/Setup.vue index 4391e5c..4866cfc 100644 --- a/src/renderer/views/Setup.vue +++ b/src/renderer/views/Setup.vue @@ -36,12 +36,12 @@ import { ipcRenderer } from 'electron' import { join } from 'path' import { mapState, mapMutations } from 'vuex' import { openDialog } from '../ipc' -import { isSSRPathAvaliable } from '../../shared/utils' +import { isSSRPathAvaliable } from '@/shared/utils' import { STORE_KEY_AUTO_DOWNLOAD } from '../constants' import { EVENT_SSR_DOWNLOAD_RENDERER, EVENT_SSR_DOWNLOAD_MAIN -} from '../../shared/events' +} from '@/shared/events' import Dot from '../components/Dot' export default { diff --git a/src/renderer/views/option/Common.vue b/src/renderer/views/option/Common.vue index 601faa5..038d668 100644 --- a/src/renderer/views/option/Common.vue +++ b/src/renderer/views/option/Common.vue @@ -20,9 +20,6 @@ - - -
@@ -50,7 +47,7 @@ /> - +
@@ -67,6 +64,7 @@ import { mapActions } from 'vuex' import { isSSRPathAvaliable, debounce } from '@/shared/utils' import { openDialog } from '@/renderer/ipc' import i18n from '@/renderer/i18n' +import { isWin } from '@/shared/env' export default { data () { const appConfig = this.$store.state.appConfig @@ -78,10 +76,9 @@ export default { shareOverLan: appConfig.shareOverLan, localPort: appConfig.localPort, pacPort: appConfig.pacPort, - httpProxyEnable: appConfig.httpProxyEnable, httpProxyPort: appConfig.httpProxyPort, lang: appConfig.lang, - preferHTTPGlobal: appConfig.preferHTTPGlobal === 1 + preferHTTPGlobal: isWin ? true : appConfig.preferHTTPGlobal === 1 }, rules: { ssrPath: [ @@ -98,7 +95,8 @@ export default { } } ] - } + }, + diabledPreferSwitch: isWin } }, watch: { diff --git a/src/shared/config.js b/src/shared/config.js index 40856f9..b216693 100644 --- a/src/shared/config.js +++ b/src/shared/config.js @@ -1,4 +1,4 @@ -import { isLinux } from './env' +import { isLinux, isWin } from './env' const defaultConfig = { // 配置集合 @@ -22,8 +22,6 @@ const defaultConfig = { sysProxyMode: 0, // 订阅列表 serverSubscribes: [], - // 是否开启http proxy - httpProxyEnable: false, // 全局快捷键 globalShortcuts: { toggleWindow: { @@ -44,7 +42,7 @@ const defaultConfig = { lang: 'en-US', isMacToolInstalled: false, noMacToolInstall: false, - preferHTTPGlobal: -1 + preferHTTPGlobal: isWin ? 1 : -1 } export default defaultConfig diff --git a/src/shared/env.js b/src/shared/env.js index 07b1ae6..0857244 100644 --- a/src/shared/env.js +++ b/src/shared/env.js @@ -1,22 +1,28 @@ import os from 'os' import { exec } from 'child_process' - +import util from 'util' export const platform = os.platform() export const isWin = platform === 'win32' export const isMac = platform === 'darwin' export const isLinux = platform === 'linux' - +export let pythonName = 'python' +const execAsync = util.promisify(exec) // python 是否已安装 -export let isPythonInstalled = new Promise((resolve) => { - exec(`python -c "print('hello')"`, (err, stdout) => { - if (err) { +export let isPythonInstalled = new Promise(async (resolve) => { + try { + let result = await execAsync(`python -c "print('hello')"`) + resolve(/^hello$/.test(result.stdout.toString().trim())) + } catch (error) { + try { + let result = await execAsync(`python3 -c "print('hello')"`) + pythonName = 'python3' + resolve(/^hello$/.test(result.stdout.toString().trim())) + } catch (error) { resolve(false) - } else { - resolve(/^hello$/.test(stdout.toString().trim())) } - }) + } }) // mac版本号