diff --git a/proxy/vmess/command/accounts.go b/proxy/vmess/command/accounts.go deleted file mode 100644 index 8879ddd1a..000000000 --- a/proxy/vmess/command/accounts.go +++ /dev/null @@ -1,93 +0,0 @@ -package command - -import ( - "io" - - v2net "github.com/v2ray/v2ray-core/common/net" - proto "github.com/v2ray/v2ray-core/common/protocol" - "github.com/v2ray/v2ray-core/common/serial" - "github.com/v2ray/v2ray-core/common/uuid" - "github.com/v2ray/v2ray-core/transport" -) - -func init() { - RegisterResponseCommand(1, func() Command { return new(SwitchAccount) }) -} - -// Structure -// 1 byte: host len N -// N bytes: host -// 2 bytes: port -// 16 bytes: uuid -// 2 bytes: alterid -// 1 byte: level -// 1 bytes: time -type SwitchAccount struct { - Host v2net.Address - Port v2net.Port - ID *uuid.UUID - AlterIds serial.Uint16Literal - Level proto.UserLevel - ValidMin byte -} - -func (this *SwitchAccount) Marshal(writer io.Writer) { - hostStr := "" - if this.Host != nil { - hostStr = this.Host.String() - } - writer.Write([]byte{byte(len(hostStr))}) - - if len(hostStr) > 0 { - writer.Write([]byte(hostStr)) - } - - writer.Write(this.Port.Bytes()) - - idBytes := this.ID.Bytes() - writer.Write(idBytes) - - writer.Write(this.AlterIds.Bytes()) - writer.Write([]byte{byte(this.Level)}) - - writer.Write([]byte{this.ValidMin}) -} - -func (this *SwitchAccount) Unmarshal(data []byte) error { - if len(data) == 0 { - return transport.ErrorCorruptedPacket - } - lenHost := int(data[0]) - if len(data) < lenHost+1 { - return transport.ErrorCorruptedPacket - } - if lenHost > 0 { - this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost])) - } - portStart := 1 + lenHost - if len(data) < portStart+2 { - return transport.ErrorCorruptedPacket - } - this.Port = v2net.PortFromBytes(data[portStart : portStart+2]) - idStart := portStart + 2 - if len(data) < idStart+16 { - return transport.ErrorCorruptedPacket - } - this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16]) - alterIdStart := idStart + 16 - if len(data) < alterIdStart+2 { - return transport.ErrorCorruptedPacket - } - this.AlterIds = serial.BytesLiteral(data[alterIdStart : alterIdStart+2]).Uint16() - levelStart := alterIdStart + 2 - if len(data) < levelStart+1 { - return transport.ErrorCorruptedPacket - } - this.Level = proto.UserLevel(data[levelStart]) - timeStart := levelStart + 1 - if len(data) < timeStart { - return transport.ErrorCorruptedPacket - } - this.ValidMin = data[timeStart] - return nil -} diff --git a/proxy/vmess/command/accounts_test.go b/proxy/vmess/command/accounts_test.go deleted file mode 100644 index 91049bbfb..000000000 --- a/proxy/vmess/command/accounts_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package command_test - -import ( - "bytes" - "testing" - - netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" - "github.com/v2ray/v2ray-core/common/uuid" - . "github.com/v2ray/v2ray-core/proxy/vmess/command" - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestSwitchAccount(t *testing.T) { - v2testing.Current(t) - - sa := &SwitchAccount{ - Port: 1234, - ID: uuid.New(), - AlterIds: 1024, - Level: 128, - ValidMin: 16, - } - - cmd, err := CreateResponseCommand(1) - assert.Error(err).IsNil() - - buffer := bytes.NewBuffer(make([]byte, 0, 1024)) - sa.Marshal(buffer) - - cmd.Unmarshal(buffer.Bytes()) - sa2, ok := cmd.(*SwitchAccount) - assert.Bool(ok).IsTrue() - assert.Pointer(sa.Host).IsNil() - assert.Pointer(sa2.Host).IsNil() - netassert.Port(sa.Port).Equals(sa2.Port) - assert.String(sa.ID).Equals(sa2.ID.String()) - assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) - assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) - assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) -} diff --git a/proxy/vmess/command/command.go b/proxy/vmess/command/command.go deleted file mode 100644 index d3fd37662..000000000 --- a/proxy/vmess/command/command.go +++ /dev/null @@ -1,17 +0,0 @@ -package command - -import ( - "errors" - "io" -) - -var ( - ErrorNoSuchCommand = errors.New("No such command.") -) - -type Command interface { - Marshal(io.Writer) - Unmarshal([]byte) error -} - -type CommandCreator func() Command diff --git a/proxy/vmess/command/dns.go b/proxy/vmess/command/dns.go deleted file mode 100644 index 754015419..000000000 --- a/proxy/vmess/command/dns.go +++ /dev/null @@ -1,65 +0,0 @@ -package command - -import ( - "errors" - "io" - - v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/transport" -) - -func init() { - RegisterResponseCommand(2, func() Command { return new(CacheDns) }) -} - -const ( - typeIPv4 byte = 1 - typeIPv6 byte = 2 -) - -var ( - ErrDomainAddress = errors.New("Unexpected domain address") -) - -// Size: 1 byte type + 4 or 16 byte IP addr -type CacheDns struct { - Address v2net.Address -} - -func (this *CacheDns) Marshal(writer io.Writer) { - if this.Address.IsIPv4() { - writer.Write([]byte{typeIPv4}) - writer.Write(this.Address.IP()) - } - - if this.Address.IsIPv6() { - writer.Write([]byte{typeIPv6}) - writer.Write(this.Address.IP()) - } -} - -func (this *CacheDns) Unmarshal(data []byte) error { - if len(data) == 0 { - return transport.ErrorCorruptedPacket - } - typeIP := data[0] - data = data[1:] - - if typeIP == typeIPv4 { - if len(data) < 4 { - return transport.ErrorCorruptedPacket - } - this.Address = v2net.IPAddress(data[0:4]) - return nil - } - - if typeIP == typeIPv6 { - if len(data) < 16 { - return transport.ErrorCorruptedPacket - } - this.Address = v2net.IPAddress(data[0:16]) - return nil - } - - return transport.ErrorCorruptedPacket -} diff --git a/proxy/vmess/command/dns_test.go b/proxy/vmess/command/dns_test.go deleted file mode 100644 index beb897840..000000000 --- a/proxy/vmess/command/dns_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package command_test - -import ( - "testing" - - "github.com/v2ray/v2ray-core/common/alloc" - v2net "github.com/v2ray/v2ray-core/common/net" - netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" - . "github.com/v2ray/v2ray-core/proxy/vmess/command" - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestCacheDnsIPv4(t *testing.T) { - v2testing.Current(t) - - cd := &CacheDns{ - Address: v2net.IPAddress([]byte{1, 2, 3, 4}), - } - - buffer := alloc.NewBuffer().Clear() - defer buffer.Release() - - cd.Marshal(buffer) - - cd2 := &CacheDns{} - err := cd2.Unmarshal(buffer.Value) - assert.Error(err).IsNil() - netassert.Address(cd.Address).Equals(cd2.Address) -} diff --git a/proxy/vmess/command/response.go b/proxy/vmess/command/response.go deleted file mode 100644 index 03d8a5094..000000000 --- a/proxy/vmess/command/response.go +++ /dev/null @@ -1,18 +0,0 @@ -package command - -var ( - cmdCache = make(map[byte]CommandCreator) -) - -func RegisterResponseCommand(id byte, cmdFactory CommandCreator) error { - cmdCache[id] = cmdFactory - return nil -} - -func CreateResponseCommand(id byte) (Command, error) { - creator, found := cmdCache[id] - if !found { - return nil, ErrorNoSuchCommand - } - return creator(), nil -} diff --git a/proxy/vmess/command/testing/fuzzing/accounts_fuzz.go b/proxy/vmess/command/testing/fuzzing/accounts_fuzz.go deleted file mode 100644 index 325d2501e..000000000 --- a/proxy/vmess/command/testing/fuzzing/accounts_fuzz.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build gofuzz - -package fuzzing - -import ( - . "github.com/v2ray/v2ray-core/proxy/vmess/command" -) - -func Fuzz(data []byte) int { - cmd := new(SwitchAccount) - if err := cmd.Unmarshal(data); err != nil { - return 0 - } - return 1 -} diff --git a/proxy/vmess/command/testing/fuzzing/data/empty_host.corpus b/proxy/vmess/command/testing/fuzzing/data/empty_host.corpus deleted file mode 100644 index 340adecd2..000000000 Binary files a/proxy/vmess/command/testing/fuzzing/data/empty_host.corpus and /dev/null differ