fix(model&component): fixed proxy model & refactor select component

This commit is contained in:
chs97 2018-10-25 21:56:36 +08:00
parent 77525e3f5a
commit 510908c823
5 changed files with 50 additions and 23 deletions

View File

@ -49,17 +49,15 @@ export class Select extends React.Component<SelectProps, SelectState> {
} }
componentDidMount () { componentDidMount () {
// create container element
const container = document.createElement('div')
document.body.appendChild(container)
this.$container = container
document.addEventListener('click', this.handleGlobalClick, true) document.addEventListener('click', this.handleGlobalClick, true)
this.setState({ dropdownListStyles: this.calculateAttachmentPosition() }) this.setState({ dropdownListStyles: this.calculateAttachmentPosition() })
} }
componentWillUnmount () { componentWillUnmount () {
if (this.state.hasCreateDropList) {
document.body.removeChild(this.$container) document.body.removeChild(this.$container)
}
document.removeEventListener('click', this.handleGlobalClick, true) document.removeEventListener('click', this.handleGlobalClick, true)
} }
shouldComponentUpdate (nextProps, nextState) { shouldComponentUpdate (nextProps, nextState) {
@ -71,6 +69,10 @@ export class Select extends React.Component<SelectProps, SelectState> {
handleShowDropList = () => { handleShowDropList = () => {
if (!this.state.hasCreateDropList) { if (!this.state.hasCreateDropList) {
// create container element
const container = document.createElement('div')
document.body.appendChild(container)
this.$container = container
this.setState({ this.setState({
hasCreateDropList: true hasCreateDropList: true
}) })

View File

@ -140,6 +140,10 @@ export class JsBridge {
public setPasteboard (data: string) { public setPasteboard (data: string) {
return this.callHandler('setPasteboard', data) return this.callHandler('setPasteboard', data)
} }
public writeConfigWithString (data: string) {
return this.callHandler('writeConfigWithString', data)
}
} }
export function setupJsBridge (callback) { export function setupJsBridge (callback) {

View File

@ -8,21 +8,10 @@ export enum ProxyType {
Socks5 = 'Socks5' Socks5 = 'Socks5'
} }
export interface Proxy { export type Proxy = ShadowsocksProxy | VmessProxy | Socks5Proxy
/**
* proxy name
*/
name?: string
/**
* configs of proxy server
* now support shadowsocks, v2ray and socks5
*/
config?: ShadowsocksProxy | VmessProxy | Socks5Proxy
}
export interface ShadowsocksProxy { export interface ShadowsocksProxy {
name?: string
type?: 'ss' type?: 'ss'
@ -41,6 +30,7 @@ export interface ShadowsocksProxy {
} }
export interface VmessProxy { export interface VmessProxy {
name?: string
type?: 'vmess' type?: 'vmess'
@ -59,6 +49,7 @@ export interface VmessProxy {
} }
export interface Socks5Proxy { export interface Socks5Proxy {
name?: string
type?: 'socks5' type?: 'socks5'

View File

@ -13,5 +13,6 @@ export enum RuleType {
'DOMAIN-SUFFIX' = 'DOMAIN-SUFFIX', 'DOMAIN-SUFFIX' = 'DOMAIN-SUFFIX',
'DOMAIN-KEYWORD' = 'DOMAIN-KEYWORD', 'DOMAIN-KEYWORD' = 'DOMAIN-KEYWORD',
'GEOIP' = 'GEOIP', 'GEOIP' = 'GEOIP',
'FINAL' = 'FINAL' 'FINAL' = 'FINAL',
'IP-CIDR' = 'IP-CIDR'
} }

View File

@ -4,6 +4,7 @@ import * as Models from '@models'
import { jsBridge } from '@lib/jsBridge' import { jsBridge } from '@lib/jsBridge'
import { getConfig } from '@lib/request' import { getConfig } from '@lib/request'
import { getLocalStorageItem } from '@lib/helper' import { getLocalStorageItem } from '@lib/helper'
import { Rule, RuleType } from '@models';
export class ConfigStore { export class ConfigStore {
@ -36,13 +37,17 @@ export class ConfigStore {
const proxies = config.Proxy as any[] || [] const proxies = config.Proxy as any[] || []
const proxy: Models.Proxy[] = proxies const proxy: Models.Proxy[] = proxies
.filter(p => ['vmess', 'ss', 'socks5'].includes(p.type)) .filter(p => ['vmess', 'ss', 'socks5'].includes(p.type))
.map(p => ({ name: p.name, config: p }))
const proxyGroups = config['Proxy Group'] as any[] || [] const proxyGroups = config['Proxy Group'] as any[] || []
const proxyGroup: Models.ProxyGroup[] = proxyGroups const proxyGroup: Models.ProxyGroup[] = proxyGroups
.filter(p => ['url-test', 'select', 'fallback'].includes(p.type)) .filter(p => ['url-test', 'select', 'fallback'].includes(p.type))
.map(p => ({ name: p.name, config: p })) .map(p => ({ name: p.name, config: p }))
const rules = config['Rule'] as any[] || []
const rule: Rule[] = rules.map(r => r.split(',')).filter(r => r.length !== 3).map(r => ({
type: RuleType[r[0] as string],
payload: r[1],
proxy: r[2]
}))
this.config = { this.config = {
general: { general: {
port: config.port || 0, port: config.port || 0,
@ -57,7 +62,7 @@ export class ConfigStore {
}, },
proxy, proxy,
proxyGroup, proxyGroup,
rules: config['Rule'] || [] rules: rule || []
} }
this.state = 'ok' this.state = 'ok'
}) })
@ -79,5 +84,29 @@ export class ConfigStore {
} }
} }
} }
@action
async updateConfig () {
const { general, proxy, proxyGroup, rules } = this.config
const externalController = `${general.externalControllerAddr}:${general.externalControllerPort}`
const proxyGroups = proxyGroup.map(p => ({
name: p.name,
...p.config
}))
const config = {
'external-controller': externalController,
port: general.port,
'socks-port': general.socksPort,
'redir-port': general.redirPort,
'allow-lan': general.allowLan,
secret: general.secret,
'log-level': general.logLevel,
mode: general.mode,
Proxy: proxy,
'Proxy Group': proxyGroups,
Rule: rules
}
const data = yaml.stringify(config)
console.log(data)
// jsBridge.writeConfigWithString(data)
}
} }