v2ray-core/common/protocol/server_spec.go

117 lines
2.0 KiB
Go
Raw Normal View History

2016-07-24 21:22:46 +00:00
package protocol
import (
"sync"
"time"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
2016-07-24 21:22:46 +00:00
)
2016-07-25 14:48:09 +00:00
type ValidationStrategy interface {
IsValid() bool
Invalidate()
}
type AlwaysValidStrategy struct{}
func AlwaysValid() ValidationStrategy {
return AlwaysValidStrategy{}
}
2016-11-27 20:39:09 +00:00
func (v AlwaysValidStrategy) IsValid() bool {
2016-07-25 14:48:09 +00:00
return true
}
2016-11-27 20:39:09 +00:00
func (v AlwaysValidStrategy) Invalidate() {}
2016-07-25 14:48:09 +00:00
type TimeoutValidStrategy struct {
until time.Time
}
func BeforeTime(t time.Time) ValidationStrategy {
2016-07-25 19:31:16 +00:00
return &TimeoutValidStrategy{
2016-07-25 14:48:09 +00:00
until: t,
}
}
2016-11-27 20:39:09 +00:00
func (v *TimeoutValidStrategy) IsValid() bool {
return v.until.After(time.Now())
2016-07-25 14:48:09 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *TimeoutValidStrategy) Invalidate() {
v.until = time.Time{}
2016-07-25 14:48:09 +00:00
}
2016-07-24 21:22:46 +00:00
type ServerSpec struct {
sync.RWMutex
2016-11-02 15:33:04 +00:00
dest v2net.Destination
users []*User
valid ValidationStrategy
2016-07-24 21:22:46 +00:00
}
2016-11-02 15:33:04 +00:00
func NewServerSpec(dest v2net.Destination, valid ValidationStrategy, users ...*User) *ServerSpec {
2016-07-24 21:22:46 +00:00
return &ServerSpec{
2016-11-02 15:33:04 +00:00
dest: dest,
users: users,
valid: valid,
2016-07-24 21:22:46 +00:00
}
}
2016-11-02 15:33:04 +00:00
func NewServerSpecFromPB(spec ServerEndpoint) *ServerSpec {
2016-09-17 22:41:21 +00:00
dest := v2net.TCPDestination(spec.Address.AsAddress(), v2net.Port(spec.Port))
2016-11-02 15:33:04 +00:00
return NewServerSpec(dest, AlwaysValid(), spec.User...)
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) Destination() v2net.Destination {
return v.dest
2016-07-25 14:48:09 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) HasUser(user *User) bool {
v.RLock()
defer v.RUnlock()
2016-07-24 21:22:46 +00:00
2016-10-16 12:22:21 +00:00
accountA, err := user.GetTypedAccount()
2016-09-17 22:41:21 +00:00
if err != nil {
return false
}
2016-11-27 20:39:09 +00:00
for _, u := range v.users {
2016-10-16 12:22:21 +00:00
accountB, err := u.GetTypedAccount()
2016-09-17 22:41:21 +00:00
if err == nil && accountA.Equals(accountB) {
2016-07-24 21:22:46 +00:00
return true
}
}
return false
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) AddUser(user *User) {
if v.HasUser(user) {
2016-07-24 21:22:46 +00:00
return
}
2016-11-27 20:39:09 +00:00
v.Lock()
defer v.Unlock()
2016-07-24 21:22:46 +00:00
2016-11-27 20:39:09 +00:00
v.users = append(v.users, user)
2016-07-24 21:22:46 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) PickUser() *User {
userCount := len(v.users)
2017-01-08 00:06:35 +00:00
switch userCount {
case 0:
return nil
case 1:
return v.users[0]
default:
return v.users[dice.Roll(userCount)]
}
2016-07-24 21:22:46 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) IsValid() bool {
return v.valid.IsValid()
2016-07-24 21:22:46 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSpec) Invalidate() {
v.valid.Invalidate()
2016-07-24 21:22:46 +00:00
}