server_spec.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package protocol
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/v2ray/v2ray-core/common/dice"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. )
  8. type ServerSpec struct {
  9. sync.RWMutex
  10. Destination v2net.Destination
  11. users []*User
  12. }
  13. func NewServerSpec(dest v2net.Destination, users ...*User) *ServerSpec {
  14. return &ServerSpec{
  15. Destination: dest,
  16. users: users,
  17. }
  18. }
  19. func (this *ServerSpec) HasUser(user *User) bool {
  20. this.RLock()
  21. defer this.RUnlock()
  22. account := user.Account
  23. for _, u := range this.users {
  24. if u.Account.Equals(account) {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. func (this *ServerSpec) AddUser(user *User) {
  31. if this.HasUser(user) {
  32. return
  33. }
  34. this.Lock()
  35. defer this.Unlock()
  36. this.users = append(this.users, user)
  37. }
  38. func (this *ServerSpec) PickUser() *User {
  39. userCount := len(this.users)
  40. return this.users[dice.Roll(userCount)]
  41. }
  42. func (this *ServerSpec) IsValid() bool {
  43. return true
  44. }
  45. func (this *ServerSpec) SetValid(b bool) {
  46. }
  47. type TimeoutServerSpec struct {
  48. *ServerSpec
  49. until time.Time
  50. }
  51. func NewTimeoutServerSpec(spec *ServerSpec, until time.Time) *TimeoutServerSpec {
  52. return &TimeoutServerSpec{
  53. ServerSpec: spec,
  54. until: until,
  55. }
  56. }
  57. func (this *TimeoutServerSpec) IsValid() bool {
  58. return this.until.Before(time.Now())
  59. }
  60. func (this *TimeoutServerSpec) SetValid(b bool) {
  61. if !b {
  62. this.until = time.Time{}
  63. }
  64. }