Skip to content

Commit

Permalink
Added TestsEnabled Settings to enable VMessAEAD test
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed Jun 8, 2020
1 parent 00103d1 commit d06a4d1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
8 changes: 5 additions & 3 deletions infra/conf/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

type VMessAccount struct {
ID string `json:"id"`
AlterIds uint16 `json:"alterId"`
Security string `json:"security"`
ID string `json:"id"`
AlterIds uint16 `json:"alterId"`
Security string `json:"security"`
TestsEnabled string `json:"testsEnabled"`
}

// Build implements Buildable
Expand All @@ -40,6 +41,7 @@ func (a *VMessAccount) Build() *vmess.Account {
SecuritySettings: &protocol.SecurityConfig{
Type: st,
},
TestsEnabled: a.TestsEnabled,
}
}

Expand Down
9 changes: 6 additions & 3 deletions proxy/vmess/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type MemoryAccount struct {
AlterIDs []*protocol.ID
// Security type of the account. Used for client connections.
Security protocol.SecurityType

TestsEnabled string
}

// AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
Expand Down Expand Up @@ -44,8 +46,9 @@ func (a *Account) AsAccount() (protocol.Account, error) {
}
protoID := protocol.NewID(id)
return &MemoryAccount{
ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
Security: a.SecuritySettings.GetSecurityType(),
ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
Security: a.SecuritySettings.GetSecurityType(),
TestsEnabled: a.TestsEnabled,
}, nil
}
13 changes: 11 additions & 2 deletions proxy/vmess/encoding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package encoding

import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
Expand All @@ -13,6 +14,7 @@ import (
"hash/fnv"
"io"
"os"
"strings"
vmessaead "v2ray.com/core/proxy/vmess/aead"

"golang.org/x/crypto/chacha20poly1305"
Expand Down Expand Up @@ -49,13 +51,20 @@ type ClientSession struct {
}

// NewClientSession creates a new ClientSession.
func NewClientSession(idHash protocol.IDHash) *ClientSession {
func NewClientSession(idHash protocol.IDHash, ctx context.Context) *ClientSession {
randomBytes := make([]byte, 33) // 16 + 16 + 1
common.Must2(rand.Read(randomBytes))

session := &ClientSession{}

session.isAEADRequest = true
session.isAEADRequest = false

if ctxValueTestsEnabled := ctx.Value(vmess.TestsEnabled); ctxValueTestsEnabled != nil {
testsEnabled := ctxValueTestsEnabled.(string)
if strings.Contains(testsEnabled, "VMessAEAD") {
session.isAEADRequest = true
}
}

if vmessexp, vmessexp_found := os.LookupEnv("VMESSAEADEXPERIMENT"); vmessexp_found {
if vmessexp == "y" {
Expand Down
7 changes: 4 additions & 3 deletions proxy/vmess/encoding/encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package encoding_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestRequestSerialization(t *testing.T) {
}

buffer := buf.New()
client := NewClientSession(protocol.DefaultIDHash)
client := NewClientSession(protocol.DefaultIDHash, context.TODO())
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))

buffer2 := buf.New()
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestInvalidRequest(t *testing.T) {
}

buffer := buf.New()
client := NewClientSession(protocol.DefaultIDHash)
client := NewClientSession(protocol.DefaultIDHash, context.TODO())
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))

buffer2 := buf.New()
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestMuxRequest(t *testing.T) {
}

buffer := buf.New()
client := NewClientSession(protocol.DefaultIDHash)
client := NewClientSession(protocol.DefaultIDHash, context.TODO())
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))

buffer2 := buf.New()
Expand Down
7 changes: 5 additions & 2 deletions proxy/vmess/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func (v *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
command = protocol.RequestCommandMux
}

user := rec.PickUser()
request := &protocol.RequestHeader{
Version: encoding.Version,
User: rec.PickUser(),
User: user,
Command: command,
Address: target.Address,
Port: target.Port,
Expand All @@ -112,7 +113,9 @@ func (v *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
input := link.Reader
output := link.Writer

session := encoding.NewClientSession(protocol.DefaultIDHash)
ctx = context.WithValue(ctx, vmess.TestsEnabled, user.Account.(*vmess.MemoryAccount).TestsEnabled)

session := encoding.NewClientSession(protocol.DefaultIDHash, ctx)
sessionPolicy := v.policyManager.ForLevel(request.User.Level)

ctx, cancel := context.WithCancel(ctx)
Expand Down
3 changes: 3 additions & 0 deletions proxy/vmess/vmessCtxInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package vmess

const TestsEnabled = "VMessCtxInterface_TestsEnabled"

0 comments on commit d06a4d1

Please sign in to comment.