refactor: 修复mac无法开启代理的bug

pull/67/head
xiaojunnuo 2021-03-25 23:40:54 +08:00
parent 7cf5c6119e
commit 8cd5708529
10 changed files with 9473 additions and 9204 deletions

View File

@ -198,6 +198,7 @@ module.exports = {
'gh.docmirror.top': 'usa'
},
speedTest: {
enabled: true,
hostnameList: ['github.com'],
dnsProviders: ['usa', 'quad9', 'rubyfish']
}

View File

@ -127,8 +127,8 @@ const executor = {
},
async mac (exec, params) {
// exec = _exec
let wifiAdaptor = await exec('sh -c "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep \'interface\' | cut -d \':\' -f2` -B 1 | head -n 1 | cut -d \' \' -f2"')
wifiAdaptor = wifiAdaptor.trim()
let wifiAdaptor = await exec('sh -c "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep \'interface\' | cut -d \':\' -f2` -B 1 | head -n 1 "')
wifiAdaptor = wifiAdaptor.substring(3).trim()
if (params == null) {
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`)

View File

@ -17,14 +17,14 @@ Vue.component(DsContainer)
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
})
view.initApi().then(async (api) => {
const app = new Vue({
router,
render: h => h(App)
})
view.initApi(app).then(async (api) => {
// 初始化status
await view.initPre(Vue, api)
const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')
app.$mount('#app')
view.initModules(app, router)
})

View File

@ -1,42 +1,46 @@
import lodash from 'lodash'
import { ipcRenderer, shell } from 'electron'
let inited = false
let apiObj = null
export function apiInit (app) {
const invoke = (api, args) => {
return ipcRenderer.invoke('apiInvoke', [api, args]).catch(e => {
app.$notification.error({
message: 'Api invoke error',
description: e.message
})
})
}
const send = (channel, message) => {
console.log('do send,', channel, message)
return ipcRenderer.send(channel, message)
}
const invoke = (api, args) => {
return ipcRenderer.invoke('apiInvoke', [api, args]).catch(err => {
console.error('api invoke error:', err)
})
}
const send = (channel, message) => {
console.log('do send,', channel, message)
return ipcRenderer.send(channel, message)
}
const bindApi = (api, param1) => {
lodash.set(apiObj, api, (param2) => {
return invoke(api, param2 || param1)
})
}
const apiObj = {
ipc: {
on (channel, callback) {
ipcRenderer.on(channel, callback)
},
removeAllListeners (channel) {
ipcRenderer.removeAllListeners(channel)
},
invoke,
send,
openExternal (href) {
shell.openExternal(href)
},
openPath (file) {
shell.openPath(file)
apiObj = {
ipc: {
on (channel, callback) {
ipcRenderer.on(channel, callback)
},
removeAllListeners (channel) {
ipcRenderer.removeAllListeners(channel)
},
invoke,
send,
openExternal (href) {
shell.openExternal(href)
},
openPath (file) {
shell.openPath(file)
}
}
}
}
let inited = false
export function apiInit () {
const bindApi = (api, param1) => {
lodash.set(apiObj, api, (param2) => {
return invoke(api, param2 || param1)
})
}
if (!inited) {
return invoke('getApiList').then(list => {
inited = true
@ -52,4 +56,6 @@ export function apiInit () {
resolve(apiObj)
})
}
export default apiObj
export function useApi () {
return apiObj
}

View File

@ -1,4 +1,4 @@
import api, { apiInit } from './api'
import { apiInit, useApi } from './api'
import modules from '../bridge/front'
import status from './status'
export default {
@ -10,6 +10,7 @@ export default {
await status.install(api)
},
initModules (app, router) {
const api = useApi()
modules.install(app, api, router)
}
}

View File

@ -55,10 +55,13 @@
</a-tab-pane>
<a-tab-pane tab="DNS设置" key="3">
<div>
<div>某些域名需要通过国外DNS服务器获取到IP列表中选取相对最优的IP进行访问</div>
<a-row style="margin-top:10px">
<a-col>
<a-button type="primary" icon="plus" @click="addDnsMapping()" />
<a-col span="19">
<div>这里配置哪些域名需要通过国外DNS服务器获取IP进行访问</div>
</a-col>
<a-col span="3">
<a-button style="margin-left:8px" type="primary" icon="plus" @click="addDnsMapping()" />
</a-col>
</a-row>
<a-row :gutter="10" style="margin-top: 10px" v-for="(item,index) of dnsMappings" :key = 'index'>
@ -73,8 +76,8 @@
</a-select>
</a-col>
<a-col :span="3">
<a-button v-if="item.value!==false" style="margin-left:10px" type="danger" icon="minus" @click="deleteDnsMapping(item,index)" />
<a-button v-if="item.value===false" style="margin-left:10px" type="primary" icon="checked" @click="restoreDefDnsMapping(item,index)" ></a-button>
<a-button v-if="item.value!==false" type="danger" icon="minus" @click="deleteDnsMapping(item,index)" />
<a-button v-if="item.value===false" type="primary" icon="checked" @click="restoreDefDnsMapping(item,index)" ></a-button>
</a-col>
</a-row>
@ -82,6 +85,12 @@
</a-tab-pane>
<a-tab-pane tab="DNS测速设置" key="4">
<div>
<a-alert type="info" message="对从dns获取到的ip进行测速使用速度最快的ip进行访问。对使用增强功能的域名没啥用"></a-alert>
<a-form-item label="开启dns测速" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="getSpeedTestConfig().enabled" >
启用
</a-checkbox>
</a-form-item>
<div>使用以下dns获取ip进行测速</div>
<a-row style="margin-top:10px">
<a-col span="24">

File diff suppressed because it is too large Load Diff

View File

@ -70,7 +70,7 @@ module.exports = {
map[dnsProvider] = dnsMap[dnsProvider]
}
}
speedTest.initSpeedTestPool({ hostnameList: speedTestConfig.hostnameList, dnsMap: map })
speedTest.initSpeedTest({ enabled: speedTestConfig.enabled, hostnameList: speedTestConfig.hostnameList, dnsMap: map })
}
const requestHandler = createRequestHandler(

View File

@ -1,21 +1,29 @@
const SpeedTester = require('./SpeedTester.js')
const _ = require('lodash')
const config = require('./config')
const log = require('../../utils/util.log.js')
const SpeedTestPool = {
}
function initSpeedTestPool ({ hostnameList, dnsMap }) {
config.getConfig().dnsMap = dnsMap
function initSpeedTest ({ enabled, hostnameList, dnsMap }) {
const conf = config.getConfig()
conf.enabled = enabled
if (!conf.enabled) {
return
}
conf.dnsMap = dnsMap
_.forEach(hostnameList, (hostname) => {
SpeedTestPool[hostname] = new SpeedTester({ hostname })
})
console.log('[speed] dnsMap', dnsMap)
log.info('[speed] dnsMap', dnsMap)
}
function getAllSpeedTester () {
const allSpeed = {}
if (!config.getConfig().enabled) {
return allSpeed
}
_.forEach(SpeedTestPool, (item, key) => {
allSpeed[key] = {
hostname: key,
@ -27,6 +35,9 @@ function getAllSpeedTester () {
}
function getSpeedTester (hostname) {
if (!config.getConfig().enabled) {
return
}
let instance = SpeedTestPool[hostname]
if (instance == null) {
instance = new SpeedTester({ hostname })
@ -55,7 +66,7 @@ function action (event) {
}
module.exports = {
SpeedTester,
initSpeedTestPool,
initSpeedTest,
getSpeedTester,
getAllSpeedTester,
registerNotify,

View File

@ -30,7 +30,7 @@ const dnsMap = dns.initDNS({
// }
})
SpeedTest.initSpeedTestPool({ hostnameList: {}, dnsMap })
SpeedTest.initSpeedTest({ hostnameList: {}, dnsMap })
const tester = new SpeedTester({ hostname: 'github.com' })
tester.test().then(ret => {