policy.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package core
  2. import (
  3. "sync"
  4. "time"
  5. "v2ray.com/core/common"
  6. )
  7. // TimeoutPolicy contains limits for connection timeout.
  8. type TimeoutPolicy struct {
  9. // Timeout for handshake phase in a connection.
  10. Handshake time.Duration
  11. // Timeout for connection being idle, i.e., there is no egress or ingress traffic in this connection.
  12. ConnectionIdle time.Duration
  13. // Timeout for an uplink only connection, i.e., the downlink of the connection has been closed.
  14. UplinkOnly time.Duration
  15. // Timeout for an downlink only connection, i.e., the uplink of the connection has been closed.
  16. DownlinkOnly time.Duration
  17. }
  18. // StatsPolicy contains settings for stats counters.
  19. type StatsPolicy struct {
  20. // Whether or not to enable stat counter for user uplink traffic.
  21. UserUplink bool
  22. // Whether or not to enable stat counter for user downlink traffic.
  23. UserDownlink bool
  24. }
  25. // Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  26. type Policy struct {
  27. Timeouts TimeoutPolicy // Timeout settings
  28. Stats StatsPolicy
  29. }
  30. // PolicyManager is a feature that provides Policy for the given user by its id or level.
  31. type PolicyManager interface {
  32. Feature
  33. // ForLevel returns the Policy for the given user level.
  34. ForLevel(level uint32) Policy
  35. }
  36. // DefaultPolicy returns the Policy when user is not specified.
  37. func DefaultPolicy() Policy {
  38. return Policy{
  39. Timeouts: TimeoutPolicy{
  40. Handshake: time.Second * 4,
  41. ConnectionIdle: time.Second * 300,
  42. UplinkOnly: time.Second * 5,
  43. DownlinkOnly: time.Second * 30,
  44. },
  45. Stats: StatsPolicy{
  46. UserUplink: false,
  47. UserDownlink: false,
  48. },
  49. }
  50. }
  51. type syncPolicyManager struct {
  52. sync.RWMutex
  53. PolicyManager
  54. }
  55. func (m *syncPolicyManager) ForLevel(level uint32) Policy {
  56. m.RLock()
  57. defer m.RUnlock()
  58. if m.PolicyManager == nil {
  59. p := DefaultPolicy()
  60. if level == 1 {
  61. p.Timeouts.ConnectionIdle = time.Second * 600
  62. }
  63. return p
  64. }
  65. return m.PolicyManager.ForLevel(level)
  66. }
  67. func (m *syncPolicyManager) Start() error {
  68. m.RLock()
  69. defer m.RUnlock()
  70. if m.PolicyManager == nil {
  71. return nil
  72. }
  73. return m.PolicyManager.Start()
  74. }
  75. func (m *syncPolicyManager) Close() error {
  76. m.RLock()
  77. defer m.RUnlock()
  78. return common.Close(m.PolicyManager)
  79. }
  80. func (m *syncPolicyManager) Set(manager PolicyManager) {
  81. if manager == nil {
  82. return
  83. }
  84. m.Lock()
  85. defer m.Unlock()
  86. common.Close(m.PolicyManager)
  87. m.PolicyManager = manager
  88. }