From e07f1794b09495337ffdf6fee799c1b4e28fe8e5 Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Mon, 18 Nov 2019 02:32:00 +0800 Subject: [PATCH] feat: support mux and tcpFastOpen --- README.md | 2 +- gui/src/App.vue | 2 +- gui/src/components/ModalSetting.vue | 86 ++++++- gui/src/components/input/Input.spec.js | 166 +++++++++++++ gui/src/components/input/Input.vue | 221 ++++++++++++++++++ .../input/__snapshots__/Input.spec.js.snap | 9 + gui/src/components/input/index.js | 13 ++ service/controller/setting.go | 6 +- service/main.go | 2 +- service/model/v2rayTmpl/v2rayTmpl.go | 20 +- service/persistence/configure/const.go | 5 + service/persistence/configure/setting.go | 6 + service/persistence/logs/logs.go | 2 + service/service/GFWList.go | 1 - service/service/setting.go | 3 +- 15 files changed, 530 insertions(+), 14 deletions(-) create mode 100644 gui/src/components/input/Input.spec.js create mode 100644 gui/src/components/input/Input.vue create mode 100644 gui/src/components/input/__snapshots__/Input.spec.js.snap create mode 100644 gui/src/components/input/index.js diff --git a/README.md b/README.md index 249875cc..037830b8 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ V2RayA是V2Ray的一个Web客户端。 - [x] 二维码、地址分享 - [x] 自定义PAC模式(GFWList、大陆白名单、自定义路由规则) - [x] 服务端启动自动检查PAC、订阅更新 +- [x] 多路复用、TCPFastOpen支持 待开发: - [ ] 手动添加/修改节点 - [ ] 测试节点HTTP时延 -- [ ] 多路复用、TCPFastOpen支持 - [ ] VPN模式全局代理支持 - [ ] QUIC协议支持 - [ ] 登陆与安全 diff --git a/gui/src/App.vue b/gui/src/App.vue index d8a90781..a4e1df2a 100644 --- a/gui/src/App.vue +++ b/gui/src/App.vue @@ -222,7 +222,7 @@ export default { diff --git a/gui/src/components/input/Input.spec.js b/gui/src/components/input/Input.spec.js new file mode 100644 index 00000000..6e7992b6 --- /dev/null +++ b/gui/src/components/input/Input.spec.js @@ -0,0 +1,166 @@ +import { shallowMount, mount } from '@vue/test-utils' +import BInput from '@components/input/Input' +import BIcon from '@components/icon/Icon' + +describe('BInput', () => { + it('render correctly', () => { + const wrapper = shallowMount(BInput) + + expect(wrapper.html()).toMatchSnapshot() + }) + + it('is vue instance', () => { + const wrapper = shallowMount(BInput) + + expect(wrapper.name()).toBe('BInput') + expect(wrapper.isVueInstance()).toBeTruthy() + }) + + it('renders input element by default', () => { + const wrapper = shallowMount(BInput) + + expect(wrapper.contains('input')).toBeTruthy() + expect(wrapper.classes()).toContain('control') + }) + + it('render textarea element when type is textarea', () => { + const wrapper = shallowMount(BInput, { + propsData: { type: 'textarea' } + }) + const target = wrapper.find('textarea') + + expect(target.exists()).toBeTruthy() + expect(target.classes()).toContain('textarea') + }) + + it('displays the icon when the icon property is true', () => { + const wrapper = shallowMount(BInput, { + propsData: { icon: 'magnify' } + }) + const target = wrapper.find(BIcon) + + expect(target.exists()).toBeTruthy() + }) + + it('display counter when the maxlength property is passed', () => { + const wrapper = shallowMount(BInput, { + propsData: { value: 'foo', maxlength: 100 } + }) + const counter = wrapper.find('small.counter') + + expect(counter.exists()).toBeTruthy() + expect(counter.text()).toBe('3 / 100') + }) + + it('no display counter when hasCounter property set for false', () => { + const wrapper = shallowMount(BInput, { + propsData: { maxlength: 100 } + }) + expect(wrapper.find('small.counter').exists()).toBeTruthy() + + wrapper.setProps({ hasCounter: false }) + expect(wrapper.find('small.counter').exists()).toBeFalsy() + }) + + it('render field password when the type property is password', () => { + const wrapper = shallowMount(BInput, { + propsData: { + type: 'password', + passwordReveal: true + } + }) + + const target = wrapper.find('input') + expect(target.exists()).toBeTruthy() + expect(target.attributes().type).toBe('password') + }) + + it('toggles the visibility of the password to true when the togglePasswordVisibility method is called', (done) => { + const wrapper = mount(BInput, { + propsData: { + value: 'foo', + type: 'password', + passwordReveal: true + } + }) + + wrapper.setProps({ value: 'bar' }) + + expect(wrapper.find('input').exists()).toBeTruthy() + expect(wrapper.find('input').attributes().type).toBe('password') + + const visibilityIcon = wrapper.find('.icon.is-clickable') + expect(visibilityIcon.exists()).toBeTruthy() + visibilityIcon.trigger('click') + wrapper.setProps({ passwordReveal: false }) + expect(wrapper.find('input').attributes().type).toBe('text') + + wrapper.vm.$nextTick(done) + }) + + it('render the placeholder and readonly attribute when passed', () => { + const wrapper = shallowMount(BInput, { + attrs: { placeholder: 'Awesome!', readonly: true } + }) + const target = wrapper.find('input') + + expect(target.element.getAttribute('placeholder')).toBe('Awesome!') + expect(target.element.getAttribute('readonly')).toBe('readonly') + }) + + it('expands input when expanded property is passed', () => { + const wrapper = shallowMount(BInput, { + propsData: { expanded: true } + }) + + expect(wrapper.classes()).toContain('is-expanded') + }) + + it('display loading icon when loading property passed', () => { + const wrapper = shallowMount(BInput, { + propsData: { loading: true, icon: 'magnify' } + }) + + expect(wrapper.classes()).toContain('is-loading') + }) + + it('keep its value on blur', async () => { + const wrapper = mount(BInput, { + propsData: { + value: 'foo' + }, + methods: { + checkHtml5Validity: () => true + } + }) + + const input = wrapper.find('input') + + input.element.value = 'bar' + input.trigger('input') + input.trigger('blur') + + expect(input.element.value).toBe('bar') + }) + + it('change status icon when statusType updated', () => { + const parent = { + data: () => ({ + newType: 'is-success', + _isField: true + }), + components: {BInput}, + template: `` + } + const wrapper = mount(parent) + + const input = wrapper.find(BInput) + expect(input.vm.statusTypeIcon).toBe('check') + wrapper.setData({ newType: 'is-danger' }) + expect(input.vm.statusTypeIcon).toBe('alert-circle') + wrapper.setData({ newType: 'is-info' }) + expect(input.vm.statusTypeIcon).toBe('information') + wrapper.setData({ newType: 'is-warning' }) + expect(input.vm.statusTypeIcon).toBe('alert') + }) +}) diff --git a/gui/src/components/input/Input.vue b/gui/src/components/input/Input.vue new file mode 100644 index 00000000..91736735 --- /dev/null +++ b/gui/src/components/input/Input.vue @@ -0,0 +1,221 @@ + + + diff --git a/gui/src/components/input/__snapshots__/Input.spec.js.snap b/gui/src/components/input/__snapshots__/Input.spec.js.snap new file mode 100644 index 00000000..e27c645c --- /dev/null +++ b/gui/src/components/input/__snapshots__/Input.spec.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`BInput render correctly 1`] = ` +
+ + + +
+`; diff --git a/gui/src/components/input/index.js b/gui/src/components/input/index.js new file mode 100644 index 00000000..7460807b --- /dev/null +++ b/gui/src/components/input/index.js @@ -0,0 +1,13 @@ +import Input from "./Input"; + +import { use, registerComponent } from "buefy/src/utils/plugins"; + +const Plugin = { + install(Vue) { + registerComponent(Vue, Input); + } +}; + +use(Plugin); + +export default Plugin; diff --git a/service/controller/setting.go b/service/controller/setting.go index 9eb8a185..8e535ce2 100644 --- a/service/controller/setting.go +++ b/service/controller/setting.go @@ -31,7 +31,11 @@ func PutSetting(ctx *gin.Context) { var data configure.Setting err := ctx.ShouldBindJSON(&data) if err != nil { - tools.ResponseError(ctx, errors.New("参数有误")) + tools.ResponseError(ctx, errors.New("参数有误"+err.Error())) + return + } + if data.MuxOn == configure.Yes && (data.Mux < 1 || data.Mux > 1024) { + tools.ResponseError(ctx, errors.New("多路复用最大并发连接数必须介于1到1024之间")) return } err = service.UpdateSetting(&data) diff --git a/service/main.go b/service/main.go index 114ba6e1..3366526a 100644 --- a/service/main.go +++ b/service/main.go @@ -59,7 +59,7 @@ func hello() { } } func checkUpdate() { - setting := configure.GetSetting() + setting := service.GetSetting() if setting.GFWListAutoUpdateMode == configure.AutoUpdate { go func() { update, tRemote, err := service.IsUpdate() diff --git a/service/model/v2rayTmpl/v2rayTmpl.go b/service/model/v2rayTmpl/v2rayTmpl.go index 1c8263ea..72c89be7 100644 --- a/service/model/v2rayTmpl/v2rayTmpl.go +++ b/service/model/v2rayTmpl/v2rayTmpl.go @@ -103,6 +103,12 @@ type StreamSettings struct { KcpSettings *KcpSettings `json:"kcpSettings"` WsSettings *WsSettings `json:"wsSettings"` HTTPSettings *HttpSettings `json:"httpSettings"` + Sockopt *Sockopt `json:"sockopt"` +} +type Sockopt struct { + Mark *int `json:"mark"` + TCPFastOpen *bool `json:"tcpFastOpen"` + Tproxy *string `json:"tproxy"` } type Mux struct { Enabled bool `json:"enabled"` @@ -190,6 +196,7 @@ func (t *Template) FillWithVmessInfo(v vmessInfo.VmessInfo) error { t.Outbounds[0].Protocol = v.Protocol port, _ := strconv.Atoi(v.Port) aid, _ := strconv.Atoi(v.Aid) + setting := configure.GetSetting() switch strings.ToLower(v.Protocol) { case "vmess": t.Outbounds[0].Settings.Vnext = []Vnext{ @@ -230,7 +237,17 @@ func (t *Template) FillWithVmessInfo(v vmessInfo.VmessInfo) error { t.Outbounds[0].StreamSettings.Security = "tls" t.Outbounds[0].StreamSettings.TLSSettings = &tmplJson.TLSSettings } - t.Outbounds[0].Mux = &tmplJson.Mux + //是否在设置里开启了TCPFastOpen + if setting.TcpFastOpen != configure.Default { + t.Outbounds[0].StreamSettings.Sockopt = new(Sockopt) + tmp := setting.TcpFastOpen == configure.Yes + t.Outbounds[0].StreamSettings.Sockopt.TCPFastOpen = &tmp + } + //是否在设置了里开启了mux + t.Outbounds[0].Mux = &Mux{ + Enabled: setting.MuxOn == configure.Yes, + Concurrency: setting.Mux, + } case "shadowsocks": t.Outbounds[0].Settings.Servers = []Server{ { @@ -245,7 +262,6 @@ func (t *Template) FillWithVmessInfo(v vmessInfo.VmessInfo) error { return errors.New("不支持的协议: " + v.Protocol) } //根据设置修改路由部分json配置 - setting := configure.GetSetting() switch setting.PacMode { case configure.WhitelistMode: t.Routing.Rules = append(t.Routing.Rules, tmplJson.Whitelist...) diff --git a/service/persistence/configure/const.go b/service/persistence/configure/const.go index c9aa4039..ef6e5363 100644 --- a/service/persistence/configure/const.go +++ b/service/persistence/configure/const.go @@ -8,9 +8,14 @@ type ( PacMatchType string RoutingDefaultProxyMode string TouchType string + DefaultYesNo string ) const ( + Default = DefaultYesNo("default") + Yes = DefaultYesNo("yes") + No = DefaultYesNo("no") + NotAutoUpdate = AutoUpdateMode("none") AutoUpdate = AutoUpdateMode("auto_update") diff --git a/service/persistence/configure/setting.go b/service/persistence/configure/setting.go index 1ee30502..c82a1bb1 100644 --- a/service/persistence/configure/setting.go +++ b/service/persistence/configure/setting.go @@ -11,6 +11,9 @@ type Setting struct { GFWListAutoUpdateMode AutoUpdateMode `json:"pacAutoUpdateMode"` CustomAutoUpdateMode AutoUpdateMode `json:"pacAutoUpdateMode"` SubscriptionAutoUpdateMode AutoUpdateMode `json:"subscriptionAutoUpdateMode"` + TcpFastOpen DefaultYesNo `json:"tcpFastOpen"` + MuxOn DefaultYesNo `json:"muxOn"` + Mux int `json:"mux"` } func NewSetting() (setting *Setting) { @@ -25,6 +28,9 @@ func NewSetting() (setting *Setting) { GFWListAutoUpdateMode: NotAutoUpdate, CustomAutoUpdateMode: NotAutoUpdate, SubscriptionAutoUpdateMode: NotAutoUpdate, + TcpFastOpen: Default, + MuxOn: No, + Mux: 8, } } diff --git a/service/persistence/logs/logs.go b/service/persistence/logs/logs.go index 721932d1..5a5d1479 100644 --- a/service/persistence/logs/logs.go +++ b/service/persistence/logs/logs.go @@ -2,6 +2,7 @@ package logs import ( "V2RayA/persistence" + "log" "time" ) @@ -11,4 +12,5 @@ func format(s string) string { func Print(s string) { persistence.DoAndSave("rpush", "V2RayA.log", format(s)) + log.Println(s) } diff --git a/service/service/GFWList.go b/service/service/GFWList.go index ab1d344c..912b2bb5 100644 --- a/service/service/GFWList.go +++ b/service/service/GFWList.go @@ -31,7 +31,6 @@ func GetRemoteGFWListUpdateTime(c *http.Client) (t time.Time, err error) { log.Println(doc.Html()) return time.Time{}, errors.New("获取最新GFWList版本失败") } - log.Println("timeraw", timeRaw) return time.Parse(time.RFC3339, timeRaw) } func IsUpdate() (update bool, remoteTime time.Time, err error) { diff --git a/service/service/setting.go b/service/service/setting.go index 9477b7b1..7fc8ae48 100644 --- a/service/service/setting.go +++ b/service/service/setting.go @@ -10,6 +10,7 @@ func GetSetting() *configure.Setting { s := configure.GetSetting() if s == nil { s = configure.NewSetting() + _ = configure.SetSetting(s) } return s } @@ -30,7 +31,7 @@ func UpdateSetting(setting *configure.Setting) (err error) { if err != nil { return } - //如果当前有连接,则重写配置并重启连接,使得对PAC的修改立即生效 + //如果当前有连接,则重写配置并重启连接,使得对PAC模式、TCPFastOpen等配置的修改立即生效 if cs := configure.GetConnectedServer(); cs != nil { tsr, _ := cs.LocateServer() err = v2ray.UpdateV2RayConfigAndRestart(&tsr.VmessInfo)