func TestReceiverUser(t *testing.T) { assert := assert.On(t) id := protocol.NewID(uuid.New()) alters := protocol.NewAlterIDs(id, 100) account := &protocol.VMessAccount{ ID: id, AlterIDs: alters, } user := protocol.NewUser(account, protocol.UserLevel(0), "") rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := protocol.NewID(uuid.New()) alters2 := protocol.NewAlterIDs(id2, 100) account2 := &protocol.VMessAccount{ ID: id2, AlterIDs: alters2, } user2 := protocol.NewUser(account2, protocol.UserLevel(0), "") assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { Users []*protocol.User `json:"clients"` Features *FeaturesConfig `json:"features"` Defaults *DefaultConfig `json:"default"` DetourConfig *DetourConfig `json:"detour"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return errors.New("VMessIn: Failed to parse config: " + err.Error()) } this.AllowedUsers = jsonConfig.Users this.Features = jsonConfig.Features // Backward compatibility this.Defaults = jsonConfig.Defaults if this.Defaults == nil { this.Defaults = &DefaultConfig{ Level: protocol.UserLevel(0), AlterIDs: 32, } } this.DetourConfig = jsonConfig.DetourConfig // Backward compatibility if this.Features != nil && this.DetourConfig == nil { this.DetourConfig = this.Features.Detour } return nil }
func TestReceiverUser(t *testing.T) { v2testing.Current(t) id := proto.NewID(uuid.New()) user := proto.NewUser(id, proto.UserLevel(0), 100, "") rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := proto.NewID(uuid.New()) user2 := proto.NewUser(id2, proto.UserLevel(0), 100, "") assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { Cipher serial.StringLiteral `json:"method"` Password serial.StringLiteral `json:"password"` UDP bool `json:"udp"` Level byte `json:"level"` Email string `json:"email"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return err } this.UDP = jsonConfig.UDP jsonConfig.Cipher = jsonConfig.Cipher.ToLower() switch jsonConfig.Cipher.String() { case "aes-256-cfb": this.Cipher = &AesCfb{ KeyBytes: 32, } case "aes-128-cfb": this.Cipher = &AesCfb{ KeyBytes: 16, } case "chacha20": this.Cipher = &ChaCha20{ IVBytes: 8, } case "chacha20-ietf": this.Cipher = &ChaCha20{ IVBytes: 12, } default: log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher) return internal.ErrorBadConfiguration } if len(jsonConfig.Password) == 0 { log.Error("Shadowsocks: Password is not specified.") return internal.ErrorBadConfiguration } this.Key = PasswordToCipherKey(jsonConfig.Password.String(), this.Cipher.KeySize()) this.Level = protocol.UserLevel(jsonConfig.Level) this.Email = jsonConfig.Email return nil }
func (this *DefaultConfig) UnmarshalJSON(data []byte) error { type JsonDefaultConfig struct { AlterIDs uint16 `json:"alterId"` Level byte `json:"level"` } jsonConfig := new(JsonDefaultConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return errors.New("VMessIn: Failed to parse default config: " + err.Error()) } this.AlterIDs = jsonConfig.AlterIDs if this.AlterIDs == 0 { this.AlterIDs = 32 } this.Level = protocol.UserLevel(jsonConfig.Level) return nil }
func (this *DefaultConfig) UnmarshalJSON(data []byte) error { type JsonDefaultConfig struct { AlterIDs uint16 `json:"alterId"` Level byte `json:"level"` } jsonConfig := new(JsonDefaultConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return err } this.AlterIDs = jsonConfig.AlterIDs if this.AlterIDs == 0 { this.AlterIDs = 32 } this.Level = proto.UserLevel(jsonConfig.Level) return nil }
func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { Users []*proto.User `json:"clients"` Features *FeaturesConfig `json:"features"` Defaults *DefaultConfig `json:"default"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return err } this.AllowedUsers = jsonConfig.Users this.Features = jsonConfig.Features this.Defaults = jsonConfig.Defaults if this.Defaults == nil { this.Defaults = &DefaultConfig{ Level: proto.UserLevel(0), AlterIDs: 32, } } return nil }
func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { Users []json.RawMessage `json:"clients"` Features *FeaturesConfig `json:"features"` Defaults *DefaultConfig `json:"default"` DetourConfig *DetourConfig `json:"detour"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { return errors.New("VMessIn: Failed to parse config: " + err.Error()) } this.Features = jsonConfig.Features // Backward compatibility this.Defaults = jsonConfig.Defaults if this.Defaults == nil { this.Defaults = &DefaultConfig{ Level: protocol.UserLevel(0), AlterIDs: 32, } } this.DetourConfig = jsonConfig.DetourConfig // Backward compatibility if this.Features != nil && this.DetourConfig == nil { this.DetourConfig = this.Features.Detour } this.AllowedUsers = make([]*protocol.User, len(jsonConfig.Users)) for idx, rawData := range jsonConfig.Users { user := new(protocol.User) if err := json.Unmarshal(rawData, user); err != nil { return errors.New("VMess|Inbound: Invalid user: "******"VMess|Inbound: Invalid user: " + err.Error()) } user.Account = account this.AllowedUsers[idx] = user } return nil }
func (this *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) { cmd := new(protocol.CommandSwitchAccount) if len(data) == 0 { return nil, transport.ErrCorruptedPacket } lenHost := int(data[0]) if len(data) < lenHost+1 { return nil, transport.ErrCorruptedPacket } if lenHost > 0 { cmd.Host = v2net.ParseAddress(string(data[1 : 1+lenHost])) } portStart := 1 + lenHost if len(data) < portStart+2 { return nil, transport.ErrCorruptedPacket } cmd.Port = v2net.PortFromBytes(data[portStart : portStart+2]) idStart := portStart + 2 if len(data) < idStart+16 { return nil, transport.ErrCorruptedPacket } cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16]) alterIdStart := idStart + 16 if len(data) < alterIdStart+2 { return nil, transport.ErrCorruptedPacket } cmd.AlterIds = serial.BytesToUint16(data[alterIdStart : alterIdStart+2]) levelStart := alterIdStart + 2 if len(data) < levelStart+1 { return nil, transport.ErrCorruptedPacket } cmd.Level = protocol.UserLevel(data[levelStart]) timeStart := levelStart + 1 if len(data) < timeStart { return nil, transport.ErrCorruptedPacket } cmd.ValidMin = data[timeStart] return cmd, nil }
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 }