refs #982 Save proxy configuration in db

This commit is contained in:
AkiraFukushima 2019-10-23 00:08:56 +09:00
parent ffdc55d619
commit 671cca8878
7 changed files with 136 additions and 9 deletions

View File

@ -213,7 +213,8 @@
"socks5": "socks5",
"socks5h": "socks5h"
}
}
},
"save": "Save"
},
"language": {
"title": "Language",

View File

@ -44,6 +44,7 @@ import { StreamingError } from '~/src/errors/streamingError'
import HashtagCache from './cache/hashtag'
import AccountCache from './cache/account'
import { InsertAccountCache } from '~/src/types/insertAccountCache'
import { Proxy } from '~/src/types/proxy'
/**
* Context menu
@ -966,6 +967,22 @@ ipcMain.on('get-global-header', (event: Event) => {
})
})
// proxy
ipcMain.on('update-proxy-config', (event: Event, proxy: Proxy) => {
const preferences = new Preferences(preferencesDBPath)
preferences
.update({
proxy: proxy
})
.then(conf => {
event.sender.send('response-update-proxy-config', conf)
})
.catch(err => {
log.error(err)
})
})
// language
ipcMain.on('change-language', (event: Event, value: string) => {
const preferences = new Preferences(preferencesDBPath)
preferences

View File

@ -12,6 +12,7 @@ import { Notify } from '~/src/types/notify'
import { Appearance } from '~/src/types/appearance'
import { Language as LanguageSet } from '~/src/types/language'
import { General, State, Notification, BaseConfig, Other } from '~/src/types/preference'
import { Proxy, ProxySource } from '~/src/types/proxy'
const sound: Sound = {
fav_rb: true,
@ -64,12 +65,22 @@ const appearance: Appearance = {
tootPadding: 8
}
const proxy: Proxy = {
source: ProxySource.no,
manualProxyConfig: {
protocol: '',
host: '',
port: ''
}
}
const Base: BaseConfig = {
general: general,
state: state,
language: language,
notification: notification,
appearance: appearance
appearance: appearance,
proxy: proxy
}
export default class Preferences {

View File

@ -27,6 +27,9 @@
<el-form-item for="proxyPort" :label="$t('preferences.network.proxy.port')">
<el-input v-model="proxyPort" :disabled="!manualProxyConfiguration" placeholder="8080"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSave">{{ $t('preferences.network.save') }}</el-button>
</el-form-item>
</el-form>
</div>
</template>
@ -70,6 +73,19 @@ export default {
this.$store.dispatch('Preferences/Network/updatePort', value)
}
}
},
created() {
this.$store.dispatch('Preferences/Network/loadProxy').catch(() => {
this.$message({
message: this.$t('message.preferences_load_error'),
type: 'error'
})
})
},
methods: {
onSave() {
this.$store.dispatch('Preferences/Network/saveProxyConfig')
}
}
}
</script>

View File

@ -1,10 +1,13 @@
import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
import { RootState } from '@/store'
import { BaseConfig } from '~/src/types/preference'
import { Proxy, ProxySource, ProxyProtocol } from '~/src/types/proxy'
export type NetworkState = {
source: 'no' | 'system' | 'manual'
source: ProxySource
proxy: {
protocol: string
protocol: '' | ProxyProtocol
host: string
port: string
}
@ -12,7 +15,7 @@ export type NetworkState = {
const state = (): NetworkState => {
return {
source: 'no',
source: ProxySource.no,
proxy: {
protocol: '',
host: '',
@ -22,6 +25,7 @@ const state = (): NetworkState => {
}
export const MUTATION_TYPES = {
UPDATE_PROXY: 'updateProxy',
CHANGE_SOURCE: 'changeSource',
UPDATE_PROTOCOL: 'updateProtocol',
UPDATE_HOST: 'updateHost',
@ -29,11 +33,47 @@ export const MUTATION_TYPES = {
}
const mutations: MutationTree<NetworkState> = {
[MUTATION_TYPES.CHANGE_SOURCE]: (state, source: 'no' | 'system' | 'manual') => {
state.source = source
[MUTATION_TYPES.UPDATE_PROXY]: (state, config: Proxy) => {
state.source = config.source
state.proxy = config.manualProxyConfig
},
[MUTATION_TYPES.UPDATE_PROTOCOL]: (state, protocol: string) => {
state.proxy.protocol = protocol
[MUTATION_TYPES.CHANGE_SOURCE]: (state, source: 'no' | 'system' | 'manual') => {
switch (source) {
case 'no':
state.source = ProxySource.no
break
case 'system':
state.source = ProxySource.system
break
case 'manual':
state.source = ProxySource.manual
break
}
},
[MUTATION_TYPES.UPDATE_PROTOCOL]: (state, protocol: '' | 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h') => {
switch (protocol) {
case 'http':
state.proxy.protocol = ProxyProtocol.http
break
case 'https':
state.proxy.protocol = ProxyProtocol.https
break
case 'socks4':
state.proxy.protocol = ProxyProtocol.socks4
break
case 'socks4a':
state.proxy.protocol = ProxyProtocol.socks4a
break
case 'socks5':
state.proxy.protocol = ProxyProtocol.socks5
break
case 'socks5h':
state.proxy.protocol = ProxyProtocol.socks5h
break
default:
state.proxy.protocol = ''
break
}
},
[MUTATION_TYPES.UPDATE_HOST]: (state, host: string) => {
state.proxy.host = host
@ -44,6 +84,20 @@ const mutations: MutationTree<NetworkState> = {
}
const actions: ActionTree<NetworkState, RootState> = {
loadProxy: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: BaseConfig) => {
ipcRenderer.removeAllListeners('error-get-preferences')
commit(MUTATION_TYPES.UPDATE_PROXY, conf.proxy as Proxy)
resolve(conf)
})
})
},
changeSource: ({ commit }, source: string) => {
commit(MUTATION_TYPES.CHANGE_SOURCE, source)
},
@ -55,6 +109,9 @@ const actions: ActionTree<NetworkState, RootState> = {
},
updatePort: ({ commit }, port: string) => {
commit(MUTATION_TYPES.UPDATE_PORT, port)
},
saveProxyConfig: ({ state }) => {
ipcRenderer.send('update-proxy-config', state)
}
}

View File

@ -3,6 +3,7 @@ import { Timeline } from '~/src/types/timeline'
import { Notify } from '~/src/types/notify'
import { Appearance } from '~/src/types/appearance'
import { Language } from '~/src/types/language'
import { Proxy } from '~/src/types/proxy'
export type Other = {
launch: boolean
@ -29,4 +30,5 @@ export type BaseConfig = {
language: Language
notification: Notification
appearance: Appearance
proxy: Proxy
}

23
src/types/proxy.ts Normal file
View File

@ -0,0 +1,23 @@
export enum ProxySource {
no = 'no',
system = 'system',
manual = 'manual'
}
export enum ProxyProtocol {
http = 'http',
https = 'https',
socks4 = 'socks4',
socks4a = 'socks4a',
socks5 = 'socks5',
socks5h = 'socks5h'
}
export type Proxy = {
source: ProxySource
manualProxyConfig: {
protocol: '' | ProxyProtocol
host: string
port: string
}
}