policy.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. type SystemStatsPolicy struct {
  33. // Whether or not to enable stat counter for uplink traffic in inbound handlers.
  34. InboundUplink bool
  35. // Whether or not to enable stat counter for downlink traffic in inbound handlers.
  36. InboundDownlink bool
  37. }
  38. type SystemPolicy struct {
  39. Stats SystemStatsPolicy
  40. Buffer BufferPolicy
  41. }
  42. // Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  43. type Policy struct {
  44. Timeouts TimeoutPolicy // Timeout settings
  45. Stats StatsPolicy
  46. Buffer BufferPolicy
  47. }
  48. // PolicyManager is a feature that provides Policy for the given user by its id or level.
  49. type PolicyManager interface {
  50. Feature
  51. // ForLevel returns the Policy for the given user level.
  52. ForLevel(level uint32) Policy
  53. // ForSystem returns the Policy for V2Ray system.
  54. ForSystem() SystemPolicy
  55. }
  56. var defaultBufferSize int32 = 10 * 1024 * 1024
  57. func init() {
  58. const key = "v2ray.ray.buffer.size"
  59. size := platform.EnvFlag{
  60. Name: key,
  61. AltName: platform.NormalizeEnvName(key),
  62. }.GetValueAsInt(10)
  63. if size == 0 {
  64. defaultBufferSize = -1
  65. } else {
  66. defaultBufferSize = int32(size) * 1024 * 1024
  67. }
  68. }
  69. func defaultBufferPolicy() BufferPolicy {
  70. return BufferPolicy{
  71. PerConnection: defaultBufferSize,
  72. }
  73. }
  74. // DefaultPolicy returns the Policy when user is not specified.
  75. func DefaultPolicy() Policy {
  76. return Policy{
  77. Timeouts: TimeoutPolicy{
  78. Handshake: time.Second * 4,
  79. ConnectionIdle: time.Second * 300,
  80. UplinkOnly: time.Second * 2,
  81. DownlinkOnly: time.Second * 5,
  82. },
  83. Stats: StatsPolicy{
  84. UserUplink: false,
  85. UserDownlink: false,
  86. },
  87. Buffer: defaultBufferPolicy(),
  88. }
  89. }
  90. type policyKey int
  91. const (
  92. bufferPolicyKey policyKey = 0
  93. )
  94. func ContextWithBufferPolicy(ctx context.Context, p BufferPolicy) context.Context {
  95. return context.WithValue(ctx, bufferPolicyKey, p)
  96. }
  97. func BufferPolicyFromContext(ctx context.Context) BufferPolicy {
  98. pPolicy := ctx.Value(bufferPolicyKey)
  99. if pPolicy == nil {
  100. return defaultBufferPolicy()
  101. }
  102. return pPolicy.(BufferPolicy)
  103. }
  104. type syncPolicyManager struct {
  105. sync.RWMutex
  106. PolicyManager
  107. }
  108. func (m *syncPolicyManager) ForLevel(level uint32) Policy {
  109. m.RLock()
  110. defer m.RUnlock()
  111. if m.PolicyManager == nil {
  112. p := DefaultPolicy()
  113. if level == 1 {
  114. p.Timeouts.ConnectionIdle = time.Second * 600
  115. }
  116. return p
  117. }
  118. return m.PolicyManager.ForLevel(level)
  119. }
  120. func (m *syncPolicyManager) ForSystem() SystemPolicy {
  121. m.RLock()
  122. defer m.RUnlock()
  123. if m.PolicyManager == nil {
  124. return SystemPolicy{}
  125. }
  126. return m.PolicyManager.ForSystem()
  127. }
  128. func (m *syncPolicyManager) Start() error {
  129. m.RLock()
  130. defer m.RUnlock()
  131. if m.PolicyManager == nil {
  132. return nil
  133. }
  134. return m.PolicyManager.Start()
  135. }
  136. func (m *syncPolicyManager) Close() error {
  137. m.RLock()
  138. defer m.RUnlock()
  139. return common.Close(m.PolicyManager)
  140. }
  141. func (m *syncPolicyManager) Set(manager PolicyManager) {
  142. if manager == nil {
  143. return
  144. }
  145. m.Lock()
  146. defer m.Unlock()
  147. common.Close(m.PolicyManager)
  148. m.PolicyManager = manager
  149. }