Skip to content

Commit

Permalink
Merge pull request #92 from xVanTuring/v0.3.0
Browse files Browse the repository at this point in the history
0.3.0-alpha.6
  • Loading branch information
xVanTuring authored Jun 2, 2020
2 parents 21c6b77 + d777cab commit 7254abd
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/main/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
Expand Down
13 changes: 5 additions & 8 deletions src/main/pac.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,26 @@ 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.*;<local>"`
} 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)
}

/**
* 设置代理为PAC代理
*/
export async function setProxyToPac (pacUrl) {
async function setProxyToPac (pacUrl) {
let command
if (isWin && await pathExists(winToolPath)) {
command = `${winToolPath} pac ${pacUrl}`
Expand All @@ -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)
}
}

Expand All @@ -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)
}

Expand Down
18 changes: 2 additions & 16 deletions src/main/tray.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/views/Setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions src/renderer/views/option/Common.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
<i-form-item class="flex" :label="$t('UI_SETTING_SHARE_LAN')">
<i-checkbox v-model="form.shareOverLan" @on-change="update('shareOverLan')" />
</i-form-item>
<i-form-item class="flex" :label="$t('UI_SETTING_ENABLE_HTTP_PORT')">
<i-checkbox v-model="form.httpProxyEnable" @on-change="update('httpProxyEnable')" />
</i-form-item>
</div>
<div class="flex flex-jc-between">
<i-form-item class="flex" :label="$t('UI_SETTING_PAC_PORT')">
Expand Down Expand Up @@ -50,7 +47,7 @@
/>
</i-form-item>
<i-form-item class="flex" :label="$t('UI_SETTING_PREFER_HTTP')">
<i-checkbox v-model="form.preferHTTPGlobal" @on-change="update('preferHTTPGlobal')" />
<i-checkbox v-model="form.preferHTTPGlobal" :disabled="diabledPreferSwitch" @on-change="update('preferHTTPGlobal')" />
</i-form-item>
</div>
<i-form-item prop="lang" label="Language">
Expand All @@ -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
Expand All @@ -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: [
Expand All @@ -98,7 +95,8 @@ export default {
}
}
]
}
},
diabledPreferSwitch: isWin
}
},
watch: {
Expand Down
6 changes: 2 additions & 4 deletions src/shared/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isLinux } from './env'
import { isLinux, isWin } from './env'

const defaultConfig = {
// 配置集合
Expand All @@ -22,8 +22,6 @@ const defaultConfig = {
sysProxyMode: 0,
// 订阅列表
serverSubscribes: [],
// 是否开启http proxy
httpProxyEnable: false,
// 全局快捷键
globalShortcuts: {
toggleWindow: {
Expand All @@ -44,7 +42,7 @@ const defaultConfig = {
lang: 'en-US',
isMacToolInstalled: false,
noMacToolInstall: false,
preferHTTPGlobal: -1
preferHTTPGlobal: isWin ? 1 : -1
}

export default defaultConfig
Expand Down
22 changes: 14 additions & 8 deletions src/shared/env.js
Original file line number Diff line number Diff line change
@@ -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版本号
Expand Down

0 comments on commit 7254abd

Please sign in to comment.