policy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package core
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/platform"
  8. )
  9. // TimeoutPolicy contains limits for connection timeout.
  10. type TimeoutPolicy struct {
  11. // Timeout for handshake phase in a connection.
  12. Handshake time.Duration
  13. // Timeout for connection being idle, i.e., there is no egress or ingress traffic in this connection.
  14. ConnectionIdle time.Duration
  15. // Timeout for an uplink only connection, i.e., the downlink of the connection has been closed.
  16. UplinkOnly time.Duration
  17. // Timeout for an downlink only connection, i.e., the uplink of the connection has been closed.
  18. DownlinkOnly time.Duration
  19. }
  20. // StatsPolicy contains settings for stats counters.
  21. type StatsPolicy struct {
  22. // Whether or not to enable stat counter for user uplink traffic.
  23. UserUplink bool
  24. // Whether or not to enable stat counter for user downlink traffic.
  25. UserDownlink bool
  26. }
  27. // BufferPolicy contains settings for internal buffer.
  28. type BufferPolicy struct {
  29. // Size of buffer per connection, in bytes. -1 for unlimited buffer.
  30. PerConnection int32
  31. }
  32. // SystemStatsPolicy contains stat policy settings on system level.
  33. type SystemStatsPolicy struct {
  34. // Whether or not to enable stat counter for uplink traffic in inbound handlers.
  35. InboundUplink bool
  36. // Whether or not to enable stat counter for downlink traffic in inbound handlers.
  37. InboundDownlink bool
  38. }
  39. // SystemPolicy contains policy settings at system level.
  40. type SystemPolicy struct {
  41. Stats SystemStatsPolicy
  42. Buffer BufferPolicy
  43. }
  44. // Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  45. type Policy struct {
  46. Timeouts TimeoutPolicy // Timeout settings
  47. Stats StatsPolicy
  48. Buffer BufferPolicy
  49. }
  50. // PolicyManager is a feature that provides Policy for the given user by its id or level.
  51. type PolicyManager interface {
  52. Feature
  53. // ForLevel returns the Policy for the given user level.
  54. ForLevel(level uint32) Policy
  55. // ForSystem returns the Policy for V2Ray system.
  56. ForSystem() SystemPolicy
  57. }
  58. var defaultBufferSize int32 = 2 * 1024 * 1024
  59. func init() {
  60. const key = "v2ray.ray.buffer.size"
  61. size := platform.EnvFlag{
  62. Name: key,
  63. AltName: platform.NormalizeEnvName(key),
  64. }.GetValueAsInt(10)
  65. if size == 0 {
  66. defaultBufferSize = -1
  67. } else {
  68. defaultBufferSize = int32(size) * 1024 * 1024
  69. }
  70. }
  71. func defaultBufferPolicy() BufferPolicy {
  72. return BufferPolicy{
  73. PerConnection: defaultBufferSize,
  74. }
  75. }
  76. // DefaultPolicy returns the Policy when user is not specified.
  77. func DefaultPolicy() Policy {
  78. return Policy{
  79. Timeouts: TimeoutPolicy{
  80. Handshake: time.Second * 4,
  81. ConnectionIdle: time.Second * 300,
  82. UplinkOnly: time.Second * 2,
  83. DownlinkOnly: time.Second * 5,
  84. },
  85. Stats: StatsPolicy{
  86. UserUplink: false,
  87. UserDownlink: false,
  88. },
  89. Buffer: defaultBufferPolicy(),
  90. }
  91. }
  92. type policyKey int
  93. const (
  94. bufferPolicyKey policyKey = 0
  95. )
  96. func ContextWithBufferPolicy(ctx context.Context, p BufferPolicy) context.Context {
  97. return context.WithValue(ctx, bufferPolicyKey, p)
  98. }
  99. func BufferPolicyFromContext(ctx context.Context) BufferPolicy {
  100. pPolicy := ctx.Value(bufferPolicyKey)
  101. if pPolicy == nil {
  102. return defaultBufferPolicy()
  103. }
  104. return pPolicy.(BufferPolicy)
  105. }
  106. type syncPolicyManager struct {
  107. sync.RWMutex
  108. PolicyManager
  109. }
  110. func (m *syncPolicyManager) ForLevel(level uint32) Policy {
  111. m.RLock()
  112. defer m.RUnlock()
  113. if m.PolicyManager == nil {
  114. p := DefaultPolicy()
  115. if level == 1 {
  116. p.Timeouts.ConnectionIdle = time.Second * 600
  117. }
  118. return p
  119. }
  120. return m.PolicyManager.ForLevel(level)
  121. }
  122. func (m *syncPolicyManager) ForSystem() SystemPolicy {
  123. m.RLock()
  124. defer m.RUnlock()
  125. if m.PolicyManager == nil {
  126. return SystemPolicy{}
  127. }
  128. return m.PolicyManager.ForSystem()
  129. }
  130. func (m *syncPolicyManager) Start() error {
  131. m.RLock()
  132. defer m.RUnlock()
  133. if m.PolicyManager == nil {
  134. return nil
  135. }
  136. return m.PolicyManager.Start()
  137. }
  138. func (m *syncPolicyManager) Close() error {
  139. m.RLock()
  140. defer m.RUnlock()
  141. return common.Close(m.PolicyManager)
  142. }
  143. func (m *syncPolicyManager) Set(manager PolicyManager) {
  144. if manager == nil {
  145. return
  146. }
  147. m.Lock()
  148. defer m.Unlock()
  149. common.Close(m.PolicyManager) // nolint: errcheck
  150. m.PolicyManager = manager
  151. }