2016-09-17 18:41:21 -04: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 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 {
|
|
|
|
log.Error("VMess: Failed to parse ID: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
protoId := protocol.NewID(id)
|
2016-10-12 12:43:55 -04:00
|
|
|
return &InternalAccount{
|
2016-09-17 18:41:21 -04:00
|
|
|
ID: protoId,
|
2016-11-27 15:39:09 -05:00
|
|
|
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
|
|
|
|
}
|