1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00
v2fly/proxy/vmess/json/user.go

74 lines
1.5 KiB
Go
Raw Normal View History

2015-10-16 06:03:22 -04:00
package json
import (
"encoding/json"
2016-01-08 18:10:57 -05:00
"math/rand"
2015-10-16 06:03:22 -04:00
2015-12-12 15:40:16 -05:00
"github.com/v2ray/v2ray-core/common/uuid"
2015-12-07 14:32:38 -05:00
"github.com/v2ray/v2ray-core/proxy/vmess"
2015-10-16 06:03:22 -04:00
)
// ConfigUser is an user account in VMess configuration.
type ConfigUser struct {
2015-12-07 14:32:38 -05:00
Id *vmess.ID
2015-10-31 09:08:13 -04:00
Email string
2015-12-07 14:32:38 -05:00
LevelValue vmess.UserLevel
2016-01-08 18:10:57 -05:00
AlterIds []*vmess.ID
2015-10-16 06:03:22 -04:00
}
func (u *ConfigUser) UnmarshalJSON(data []byte) error {
type rawUser struct {
2016-01-08 18:10:57 -05:00
IdString string `json:"id"`
EmailString string `json:"email"`
LevelInt int `json:"level"`
AlterIdCount int `json:"alterId"`
2015-10-16 06:03:22 -04:00
}
var rawUserValue rawUser
if err := json.Unmarshal(data, &rawUserValue); err != nil {
return err
}
2015-12-12 15:40:16 -05:00
id, err := uuid.ParseString(rawUserValue.IdString)
2015-10-16 06:03:22 -04:00
if err != nil {
return err
}
2015-12-12 15:40:16 -05:00
u.Id = vmess.NewID(id)
2015-10-16 06:03:22 -04:00
u.Email = rawUserValue.EmailString
2015-12-07 14:32:38 -05:00
u.LevelValue = vmess.UserLevel(rawUserValue.LevelInt)
2016-01-08 18:10:57 -05:00
if rawUserValue.AlterIdCount > 0 {
prevId := u.Id.UUID()
// TODO: check duplicate
u.AlterIds = make([]*vmess.ID, rawUserValue.AlterIdCount)
for idx, _ := range u.AlterIds {
newid := prevId.Next()
u.AlterIds[idx] = vmess.NewID(newid)
prevId = newid
}
}
2015-10-16 06:03:22 -04:00
return nil
}
2015-12-07 14:32:38 -05:00
func (u *ConfigUser) ID() *vmess.ID {
2015-10-16 06:03:22 -04:00
return u.Id
}
2015-10-30 19:38:31 -04:00
2015-12-07 14:32:38 -05:00
func (this *ConfigUser) Level() vmess.UserLevel {
2015-10-31 09:08:13 -04:00
return this.LevelValue
}
2016-01-08 18:10:57 -05:00
func (this *ConfigUser) AlterIDs() []*vmess.ID {
return this.AlterIds
}
func (this *ConfigUser) AnyValidID() *vmess.ID {
if len(this.AlterIds) == 0 {
return this.ID()
}
if len(this.AlterIds) == 1 {
return this.AlterIds[0]
}
idIdx := rand.Intn(len(this.AlterIds))
return this.AlterIds[idIdx]
}