func TestSwitchAccount(t *testing.T) { assert := assert.On(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } buffer := buf.New() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2)) assert.Error(err).IsNil() sa2, ok := cmd.(*protocol.CommandSwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() assert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID.String()).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
func (v *userByEmail) Get(email string) (*protocol.User, bool) { var user *protocol.User var found bool v.RLock() user, found = v.cache[email] v.RUnlock() if !found { v.Lock() user, found = v.cache[email] if !found { account := &vmess.Account{ Id: uuid.New().String(), AlterId: uint32(v.defaultAlterIDs), } user = &protocol.User{ Level: v.defaultLevel, Email: email, Account: serial.ToTypedMessage(account), } v.cache[email] = user } v.Unlock() } return user, found }
func (this *userByEmail) Get(email string) (*protocol.User, bool) { var user *protocol.User var found bool this.RLock() user, found = this.cache[email] this.RUnlock() if !found { this.Lock() user, found = this.cache[email] if !found { account := &vmess.Account{ Id: uuid.New().String(), AlterId: uint32(this.defaultAlterIDs), } user = &protocol.User{ Level: this.defaultLevel, Email: email, Account: loader.NewTypedSettings(account), } this.cache[email] = user } this.Unlock() } return user, found }
func RunV2Ray(configFile string) *exec.Cmd { binaryPath := GetTestBinaryPath() covDir := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "cov") profile := uuid.New().String() + ".out" proc := exec.Command(binaryPath, "-config", configFile, "-test.run", "TestRunMainForCoverage", "-test.coverprofile", profile, "-test.outputdir", covDir) proc.Stderr = os.Stderr proc.Stdout = os.Stdout return proc }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := &protocol.User{ Level: 0, Email: "*****@*****.**", } account := &vmess.Account{ Id: uuid.New().String(), AlterId: 0, } user.Account = serial.ToTypedMessage(account) expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOptionConnectionReuse, Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), Security: protocol.Security(protocol.SecurityType_AES128_GCM), } buffer := buf.New() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) userValidator.Add(user) server := NewServerSession(userValidator) actualRequest, err := server.DecodeRequestHeader(buffer) assert.Error(err).IsNil() assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security)) }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := protocol.NewUser( protocol.UserLevelUntrusted, "*****@*****.**") user.Account = &vmess.Account{ ID: protocol.NewID(uuid.New()), AlterIDs: nil, } expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOption(0), Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), } buffer := alloc.NewBuffer().Clear() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) userValidator.Add(user) server := NewServerSession(userValidator) actualRequest, err := server.DecodeRequestHeader(buffer) assert.Error(err).IsNil() assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) }
func (this *userByEmail) Get(email string) (*protocol.User, bool) { var user *protocol.User var found bool this.RLock() user, found = this.cache[email] this.RUnlock() if !found { this.Lock() user, found = this.cache[email] if !found { id := protocol.NewID(uuid.New()) alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs) account := &vmess.Account{ ID: id, AlterIDs: alterIDs, } user = protocol.NewUser(this.defaultLevel, email) user.Account = account this.cache[email] = user } this.Unlock() } return user, found }
func TestNoOpConnectionHeader(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() userID := protocol.NewID(uuid.New()) serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), StreamSettings: &internet.StreamConfig{ TransportSettings: []*internet.TransportConfig{ { Protocol: internet.TransportProtocol_TCP, Settings: serial.ToTypedMessage(&tcptransport.Config{ HeaderSettings: serial.ToTypedMessage(&http.Config{}), }), }, }, }, }), ProxySettings: serial.ToTypedMessage(&inbound.Config{ User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } clientPort := pickPort() clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(clientPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&outbound.Config{ Receiver: []*protocol.ServerEndpoint{ { Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(serverPort), User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }, }, }), SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ StreamSettings: &internet.StreamConfig{ TransportSettings: []*internet.TransportConfig{ { Protocol: internet.TransportProtocol_TCP, Settings: serial.ToTypedMessage(&tcptransport.Config{ HeaderSettings: serial.ToTypedMessage(&http.Config{}), }), }, }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() assert.Error(InitializeServerConfig(clientConfig)).IsNil() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) assert.Error(err).IsNil() payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := readFrom(conn, time.Second*2, len(payload)) assert.Bytes(response).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() CloseAllServers() }
func TestCmdKey(t *testing.T) { assert := assert.On(t) id := NewID(uuid.New()) assert.Bool(predicate.BytesAll(id.CmdKey(), 0)).IsFalse() }
func TestDokodemoTCP(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() userID := protocol.NewID(uuid.New()) serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&inbound.Config{ User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } clientPort := uint32(pickPort()) clientPortRange := uint32(5) clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange}, Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&outbound.Config{ Receiver: []*protocol.ServerEndpoint{ { Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(serverPort), User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() assert.Error(InitializeServerConfig(clientConfig)).IsNil() for port := clientPort; port <= clientPort+clientPortRange; port++ { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), }) assert.Error(err).IsNil() payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }