1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/common/protocol/server_spec.go

123 lines
2.1 KiB
Go
Raw Normal View History

2016-07-24 17:22:46 -04:00
package protocol
import (
"sync"
"time"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/dice"
"github.com/v2fly/v2ray-core/v4/common/net"
2016-07-24 17:22:46 -04:00
)
2016-07-25 10:48:09 -04:00
type ValidationStrategy interface {
IsValid() bool
Invalidate()
}
2017-04-25 17:41:07 -04:00
type alwaysValidStrategy struct{}
2016-07-25 10:48:09 -04:00
func AlwaysValid() ValidationStrategy {
2017-04-25 17:41:07 -04:00
return alwaysValidStrategy{}
2016-07-25 10:48:09 -04:00
}
2017-04-25 17:41:07 -04:00
func (alwaysValidStrategy) IsValid() bool {
2016-07-25 10:48:09 -04:00
return true
}
2017-04-25 17:41:07 -04:00
func (alwaysValidStrategy) Invalidate() {}
2016-07-25 10:48:09 -04:00
2017-04-25 17:41:07 -04:00
type timeoutValidStrategy struct {
2016-07-25 10:48:09 -04:00
until time.Time
}
func BeforeTime(t time.Time) ValidationStrategy {
2017-04-25 17:41:07 -04:00
return &timeoutValidStrategy{
2016-07-25 10:48:09 -04:00
until: t,
}
}
2017-04-25 17:42:51 -04:00
func (s *timeoutValidStrategy) IsValid() bool {
return s.until.After(time.Now())
2016-07-25 10:48:09 -04:00
}
2017-04-25 17:42:51 -04:00
func (s *timeoutValidStrategy) Invalidate() {
s.until = time.Time{}
2016-07-25 10:48:09 -04:00
}
2016-07-24 17:22:46 -04:00
type ServerSpec struct {
sync.RWMutex
2017-01-13 18:27:45 -05:00
dest net.Destination
2018-08-26 18:11:32 -04:00
users []*MemoryUser
2016-11-02 11:33:04 -04:00
valid ValidationStrategy
2016-07-24 17:22:46 -04:00
}
2018-08-26 18:11:32 -04:00
func NewServerSpec(dest net.Destination, valid ValidationStrategy, users ...*MemoryUser) *ServerSpec {
2016-07-24 17:22:46 -04:00
return &ServerSpec{
2016-11-02 11:33:04 -04:00
dest: dest,
users: users,
valid: valid,
2016-07-24 17:22:46 -04:00
}
}
2020-08-26 07:35:33 -04:00
func NewServerSpecFromPB(spec *ServerEndpoint) (*ServerSpec, error) {
2017-01-13 18:27:45 -05:00
dest := net.TCPDestination(spec.Address.AsAddress(), net.Port(spec.Port))
2018-08-26 18:11:32 -04:00
mUsers := make([]*MemoryUser, len(spec.User))
for idx, u := range spec.User {
mUser, err := u.ToMemoryUser()
if err != nil {
return nil, err
}
mUsers[idx] = mUser
}
return NewServerSpec(dest, AlwaysValid(), mUsers...), nil
2016-09-17 18:41:21 -04:00
}
2017-04-25 17:42:51 -04:00
func (s *ServerSpec) Destination() net.Destination {
return s.dest
2016-07-25 10:48:09 -04:00
}
2018-08-26 18:11:32 -04:00
func (s *ServerSpec) HasUser(user *MemoryUser) bool {
2017-04-25 17:42:51 -04:00
s.RLock()
defer s.RUnlock()
2016-07-24 17:22:46 -04:00
2017-04-25 17:42:51 -04:00
for _, u := range s.users {
2018-08-26 18:11:32 -04:00
if u.Account.Equals(user.Account) {
2016-07-24 17:22:46 -04:00
return true
}
}
return false
}
2018-08-26 18:11:32 -04:00
func (s *ServerSpec) AddUser(user *MemoryUser) {
2017-04-25 17:42:51 -04:00
if s.HasUser(user) {
2016-07-24 17:22:46 -04:00
return
}
2017-04-25 17:42:51 -04:00
s.Lock()
defer s.Unlock()
2016-07-24 17:22:46 -04:00
2017-04-25 17:42:51 -04:00
s.users = append(s.users, user)
2016-07-24 17:22:46 -04:00
}
2018-08-26 18:11:32 -04:00
func (s *ServerSpec) PickUser() *MemoryUser {
2017-04-25 17:42:51 -04:00
s.RLock()
defer s.RUnlock()
userCount := len(s.users)
2017-01-07 19:06:35 -05:00
switch userCount {
case 0:
return nil
case 1:
2017-04-25 17:42:51 -04:00
return s.users[0]
2017-01-07 19:06:35 -05:00
default:
2017-04-25 17:42:51 -04:00
return s.users[dice.Roll(userCount)]
2017-01-07 19:06:35 -05:00
}
2016-07-24 17:22:46 -04:00
}
2018-03-23 04:18:57 -04:00
func (s *ServerSpec) IsValid() bool {
return s.valid.IsValid()
2016-07-24 17:22:46 -04:00
}
2018-03-23 04:18:57 -04:00
func (s *ServerSpec) Invalidate() {
s.valid.Invalidate()
2016-07-24 17:22:46 -04:00
}