From d8bc41e65338afeaaae51523c28f427a23f4982c Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Mon, 8 Feb 2021 18:08:01 +0800 Subject: [PATCH] support enforcing VMessAEAD via environment variable (#596) * support enforcing VMessAEAD via env var to prevent downgrade attack * Add comments to make Codacy Production happy --- proxy/vmess/encoding/server.go | 5 +++++ proxy/vmess/inbound/inbound.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index c33dff982..264dcf12b 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -118,6 +118,11 @@ func NewServerSession(validator *vmess.TimedUserValidator, sessionHistory *Sessi } } +// SetAEADForced sets isAEADForced for a ServerSession. +func (s *ServerSession) SetAEADForced(isAEADForced bool) { + s.isAEADForced = isAEADForced +} + func parseSecurityType(b byte) protocol.SecurityType { if _, f := protocol.SecurityType_name[int32(b)]; f { st := protocol.SecurityType(b) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 12386b5fc..c5b32784c 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -17,6 +17,7 @@ import ( "v2ray.com/core/common/errors" "v2ray.com/core/common/log" "v2ray.com/core/common/net" + "v2ray.com/core/common/platform" "v2ray.com/core/common/protocol" "v2ray.com/core/common/session" "v2ray.com/core/common/signal" @@ -224,6 +225,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i reader := &buf.BufferedReader{Reader: buf.NewReader(connection)} svrSession := encoding.NewServerSession(h.clients, h.sessionHistory) + svrSession.SetAEADForced(aeadForced) request, err := svrSession.DecodeRequestHeader(reader) if err != nil { if errors.Cause(err) != io.EOF { @@ -350,8 +352,17 @@ func (h *Handler) generateCommand(ctx context.Context, request *protocol.Request return nil } +var aeadForced = 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" + + isAeadForced := platform.NewEnvFlag("v2ray.vmess.aead.forced").GetValue(func() string { return defaultFlagValue }) + if isAeadForced == "true" { + aeadForced = true + } }