policy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. type SystemStatsPolicy struct {
  26. // Whether or not to enable stat counter for uplink traffic in inbound handlers.
  27. InboundUplink bool
  28. // Whether or not to enable stat counter for downlink traffic in inbound handlers.
  29. InboundDownlink bool
  30. }
  31. type SystemPolicy struct {
  32. Stats SystemStatsPolicy
  33. }
  34. // Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  35. type Policy struct {
  36. Timeouts TimeoutPolicy // Timeout settings
  37. Stats StatsPolicy
  38. }
  39. // PolicyManager is a feature that provides Policy for the given user by its id or level.
  40. type PolicyManager interface {
  41. Feature
  42. // ForLevel returns the Policy for the given user level.
  43. ForLevel(level uint32) Policy
  44. // ForSystem returns the Policy for V2Ray system.
  45. ForSystem() SystemPolicy
  46. }
  47. // DefaultPolicy returns the Policy when user is not specified.
  48. func DefaultPolicy() Policy {
  49. return Policy{
  50. Timeouts: TimeoutPolicy{
  51. Handshake: time.Second * 4,
  52. ConnectionIdle: time.Second * 300,
  53. UplinkOnly: time.Second * 2,
  54. DownlinkOnly: time.Second * 5,
  55. },
  56. Stats: StatsPolicy{
  57. UserUplink: false,
  58. UserDownlink: false,
  59. },
  60. }
  61. }
  62. type syncPolicyManager struct {
  63. sync.RWMutex
  64. PolicyManager
  65. }
  66. func (m *syncPolicyManager) ForLevel(level uint32) Policy {
  67. m.RLock()
  68. defer m.RUnlock()
  69. if m.PolicyManager == nil {
  70. p := DefaultPolicy()
  71. if level == 1 {
  72. p.Timeouts.ConnectionIdle = time.Second * 600
  73. }
  74. return p
  75. }
  76. return m.PolicyManager.ForLevel(level)
  77. }
  78. func (m *syncPolicyManager) Start() error {
  79. m.RLock()
  80. defer m.RUnlock()
  81. if m.PolicyManager == nil {
  82. return nil
  83. }
  84. return m.PolicyManager.Start()
  85. }
  86. func (m *syncPolicyManager) Close() error {
  87. m.RLock()
  88. defer m.RUnlock()
  89. return common.Close(m.PolicyManager)
  90. }
  91. func (m *syncPolicyManager) Set(manager PolicyManager) {
  92. if manager == nil {
  93. return
  94. }
  95. m.Lock()
  96. defer m.Unlock()
  97. common.Close(m.PolicyManager)
  98. m.PolicyManager = manager
  99. }