From 9a8488074ea36561e72af010d85a704796723e24 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 18 Jul 2018 10:40:28 +0200 Subject: [PATCH] env flag controlled global padding --- common/platform/platform.go | 13 ++++++++++--- proxy/vmess/encoding/auth.go | 2 +- proxy/vmess/inbound/inbound.go | 5 ++--- proxy/vmess/outbound/outbound.go | 20 +++++++++++++++++--- testing/scenarios/vmess_test.go | 9 +++++++-- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/common/platform/platform.go b/common/platform/platform.go index 3b86ec9dc..4fa253d9c 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -12,6 +12,13 @@ type EnvFlag struct { AltName string } +func NewEnvFlag(name string) EnvFlag { + return EnvFlag{ + Name: name, + AltName: NormalizeEnvName(name), + } +} + func (f EnvFlag) GetValue(defaultValue func() string) string { if v, found := os.LookupEnv(f.Name); found { return v @@ -61,18 +68,18 @@ func getExecutableSubDir(dir string) func() string { func GetAssetLocation(file string) string { const name = "v2ray.location.asset" - assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) + assetPath := NewEnvFlag(name).GetValue(getExecutableDir) return filepath.Join(assetPath, file) } func GetPluginDirectory() string { const name = "v2ray.location.plugin" - pluginDir := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableSubDir("plugins")) + pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins")) return pluginDir } func GetConfigurationPath() string { const name = "v2ray.location.config" - configPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) + configPath := NewEnvFlag(name).GetValue(getExecutableDir) return filepath.Join(configPath, "config.json") } diff --git a/proxy/vmess/encoding/auth.go b/proxy/vmess/encoding/auth.go index 9f0128960..dc668d879 100644 --- a/proxy/vmess/encoding/auth.go +++ b/proxy/vmess/encoding/auth.go @@ -112,6 +112,6 @@ func (s *ShakeSizeParser) NextPaddingLen() uint16 { return s.next() % 64 } -func (s *ShakeSizeParser) MaxPaddingLne() uint16 { +func (s *ShakeSizeParser) MaxPaddingLen() uint16 { return 64 } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 73be09876..b63c80b83 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -9,9 +9,6 @@ import ( "sync" "time" - "v2ray.com/core/common/session" - "v2ray.com/core/common/task" - "v2ray.com/core" "v2ray.com/core/common" "v2ray.com/core/common/buf" @@ -20,7 +17,9 @@ import ( "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/common/serial" + "v2ray.com/core/common/session" "v2ray.com/core/common/signal" + "v2ray.com/core/common/task" "v2ray.com/core/common/uuid" "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/encoding" diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 1f58befcf..65de77fad 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -6,16 +6,16 @@ import ( "context" "time" - "v2ray.com/core/common/session" - "v2ray.com/core/common/task" - "v2ray.com/core" "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/net" + "v2ray.com/core/common/platform" "v2ray.com/core/common/protocol" "v2ray.com/core/common/retry" + "v2ray.com/core/common/session" "v2ray.com/core/common/signal" + "v2ray.com/core/common/task" "v2ray.com/core/proxy" "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/encoding" @@ -87,6 +87,10 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia Option: protocol.RequestOptionChunkStream, } + if enablePadding { + request.Option.Set(protocol.RequestOptionGlobalPadding) + } + rawAccount, err := request.User.GetTypedAccount() if err != nil { return newError("failed to get user account").Base(err).AtWarning() @@ -161,8 +165,18 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia return nil } +var ( + enablePadding = false +) + func init() { common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { return New(ctx, config.(*Config)) })) + + const defaultFlagValue = "NOT_DEFINED_AT_ALL" + paddingValue := platform.NewEnvFlag("v2ray.vmess.padding").GetValue(func() string { return defaultFlagValue }) + if paddingValue != defaultFlagValue { + enablePadding = true + } } diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index e70a2b675..8a73cec84 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -253,8 +253,15 @@ func TestVMessGCM(t *testing.T) { }, } + /* + const envName = "V2RAY_VMESS_PADDING" + common.Must(os.Setenv(envName, "1")) + defer os.Unsetenv(envName) + */ + servers, err := InitializeServerConfigs(serverConfig, clientConfig) assert(err, IsNil) + defer CloseAllServers(servers) var wg sync.WaitGroup wg.Add(10) @@ -280,8 +287,6 @@ func TestVMessGCM(t *testing.T) { }() } wg.Wait() - - CloseAllServers(servers) } func TestVMessGCMUDP(t *testing.T) {