1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00
v2fly/proxy/vmess/account.go

45 lines
998 B
Go
Raw Normal View History

2016-09-17 22:41:21 +00:00
package vmess
import (
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
)
2016-10-12 16:43:55 +00:00
type InternalAccount struct {
2016-09-17 22:41:21 +00:00
ID *protocol.ID
AlterIDs []*protocol.ID
2016-12-07 20:43:41 +00:00
Security protocol.Security
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *InternalAccount) AnyValidID() *protocol.ID {
if len(v.AlterIDs) == 0 {
return v.ID
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
return v.AlterIDs[dice.Roll(len(v.AlterIDs))]
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *InternalAccount) Equals(account protocol.Account) bool {
2016-10-12 16:43:55 +00:00
vmessAccount, ok := account.(*InternalAccount)
2016-09-17 22:41:21 +00:00
if !ok {
return false
}
// TODO: handle AlterIds difference
2016-11-27 20:39:09 +00:00
return v.ID.Equals(vmessAccount.ID)
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(v.Id)
2016-09-17 22:41:21 +00:00
if err != nil {
log.Error("VMess: Failed to parse ID: ", err)
return nil, err
}
protoId := protocol.NewID(id)
2016-10-12 16:43:55 +00:00
return &InternalAccount{
2016-09-17 22:41:21 +00:00
ID: protoId,
2016-11-27 20:39:09 +00:00
AlterIDs: protocol.NewAlterIDs(protoId, uint16(v.AlterId)),
2016-12-07 20:43:41 +00:00
Security: protocol.Security(v.Security),
2016-09-17 22:41:21 +00:00
}, nil
}