v2ray-core/proxy/vmess/account.go

45 lines
1013 B
Go
Raw Normal View History

2016-09-18 00:41:21 +02:00
package vmess
import (
2017-02-01 21:35:40 +01:00
"v2ray.com/core/app/log"
2017-02-03 00:14:43 +01:00
"v2ray.com/core/common/dice"
2016-09-18 00:41:21 +02:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
)
2016-10-12 18:43:55 +02:00
type InternalAccount struct {
2016-09-18 00:41:21 +02:00
ID *protocol.ID
AlterIDs []*protocol.ID
2016-12-07 21:43:41 +01:00
Security protocol.Security
2016-09-18 00:41:21 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *InternalAccount) AnyValidID() *protocol.ID {
if len(v.AlterIDs) == 0 {
return v.ID
2016-09-18 00:41:21 +02:00
}
2016-11-27 21:39:09 +01:00
return v.AlterIDs[dice.Roll(len(v.AlterIDs))]
2016-09-18 00:41:21 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *InternalAccount) Equals(account protocol.Account) bool {
2016-10-12 18:43:55 +02:00
vmessAccount, ok := account.(*InternalAccount)
2016-09-18 00:41:21 +02:00
if !ok {
return false
}
// TODO: handle AlterIds difference
2016-11-27 21:39:09 +01:00
return v.ID.Equals(vmessAccount.ID)
2016-09-18 00:41:21 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(v.Id)
2016-09-18 00:41:21 +02:00
if err != nil {
2017-04-09 01:43:25 +02:00
log.Trace(newError("failed to parse ID").Base(err).AtError())
2016-09-18 00:41:21 +02:00
return nil, err
}
2016-12-27 21:34:14 +01:00
protoID := protocol.NewID(id)
2016-10-12 18:43:55 +02:00
return &InternalAccount{
2016-12-27 21:34:14 +01:00
ID: protoID,
AlterIDs: protocol.NewAlterIDs(protoID, uint16(v.AlterId)),
2016-12-11 14:58:53 +01:00
Security: v.SecuritySettings.AsSecurity(),
2016-09-18 00:41:21 +02:00
}, nil
}