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"
|
2017-01-13 23:27:45 +00:00
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:41:07 +00:00
|
|
|
type alwaysValidStrategy struct{}
|
2016-07-25 14:48:09 +00:00
|
|
|
|
|
|
|
func AlwaysValid() ValidationStrategy {
|
2017-04-25 21:41:07 +00:00
|
|
|
return alwaysValidStrategy{}
|
2016-07-25 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:41:07 +00:00
|
|
|
func (alwaysValidStrategy) IsValid() bool {
|
2016-07-25 14:48:09 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:41:07 +00:00
|
|
|
func (alwaysValidStrategy) Invalidate() {}
|
2016-07-25 14:48:09 +00:00
|
|
|
|
2017-04-25 21:41:07 +00:00
|
|
|
type timeoutValidStrategy struct {
|
2016-07-25 14:48:09 +00:00
|
|
|
until time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func BeforeTime(t time.Time) ValidationStrategy {
|
2017-04-25 21:41:07 +00:00
|
|
|
return &timeoutValidStrategy{
|
2016-07-25 14:48:09 +00:00
|
|
|
until: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *timeoutValidStrategy) IsValid() bool {
|
|
|
|
return s.until.After(time.Now())
|
2016-07-25 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *timeoutValidStrategy) Invalidate() {
|
|
|
|
s.until = time.Time{}
|
2016-07-25 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 21:22:46 +00:00
|
|
|
type ServerSpec struct {
|
|
|
|
sync.RWMutex
|
2017-01-13 23:27:45 +00:00
|
|
|
dest net.Destination
|
2016-11-02 15:33:04 +00:00
|
|
|
users []*User
|
|
|
|
valid ValidationStrategy
|
2016-07-24 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:27:45 +00:00
|
|
|
func NewServerSpec(dest net.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 {
|
2017-01-13 23:27:45 +00:00
|
|
|
dest := net.TCPDestination(spec.Address.AsAddress(), net.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
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *ServerSpec) Destination() net.Destination {
|
|
|
|
return s.dest
|
2016-07-25 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *ServerSpec) HasUser(user *User) bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.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
|
|
|
|
}
|
2017-04-25 21:42:51 +00:00
|
|
|
for _, u := range s.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
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *ServerSpec) AddUser(user *User) {
|
|
|
|
if s.HasUser(user) {
|
2016-07-24 21:22:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2016-07-24 21:22:46 +00:00
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
s.users = append(s.users, user)
|
2016-07-24 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:42:51 +00:00
|
|
|
func (s *ServerSpec) PickUser() *User {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
userCount := len(s.users)
|
2017-01-08 00:06:35 +00:00
|
|
|
switch userCount {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
2017-04-25 21:42:51 +00:00
|
|
|
return s.users[0]
|
2017-01-08 00:06:35 +00:00
|
|
|
default:
|
2017-04-25 21:42:51 +00:00
|
|
|
return s.users[dice.Roll(userCount)]
|
2017-01-08 00:06:35 +00:00
|
|
|
}
|
2016-07-24 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 08:18:57 +00:00
|
|
|
func (s *ServerSpec) IsValid() bool {
|
|
|
|
return s.valid.IsValid()
|
2016-07-24 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 08:18:57 +00:00
|
|
|
func (s *ServerSpec) Invalidate() {
|
|
|
|
s.valid.Invalidate()
|
2016-07-24 21:22:46 +00:00
|
|
|
}
|