policy.go 4.6 KB

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