1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-18 01:46:06 -04:00
v2fly/proxy/vmess/account.go

45 lines
1013 B
Go
Raw Normal View History

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