policy.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package policy
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "github.com/v2fly/v2ray-core/v5/common/platform"
  7. "github.com/v2fly/v2ray-core/v5/features"
  8. )
  9. // Timeout contains limits for connection timeout.
  10. type Timeout 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. // Stats contains settings for stats counters.
  21. type Stats 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. // Buffer contains settings for internal buffer.
  28. type Buffer struct {
  29. // Size of buffer per connection, in bytes. -1 for unlimited buffer.
  30. PerConnection int32
  31. }
  32. // SystemStats contains stat policy settings on system level.
  33. type SystemStats 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. // Whether or not to enable stat counter for uplink traffic in outbound handlers.
  39. OutboundUplink bool
  40. // Whether or not to enable stat counter for downlink traffic in outbound handlers.
  41. OutboundDownlink bool
  42. }
  43. // System contains policy settings at system level.
  44. type System struct {
  45. Stats SystemStats
  46. OverrideAccessLogDest bool
  47. Buffer Buffer
  48. }
  49. // Session is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  50. type Session struct {
  51. Timeouts Timeout // Timeout settings
  52. Stats Stats
  53. Buffer Buffer
  54. }
  55. // Manager is a feature that provides Policy for the given user by its id or level.
  56. //
  57. // v2ray:api:stable
  58. type Manager interface {
  59. features.Feature
  60. // ForLevel returns the Session policy for the given user level.
  61. ForLevel(level uint32) Session
  62. // ForSystem returns the System policy for V2Ray system.
  63. ForSystem() System
  64. }
  65. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  66. //
  67. // v2ray:api:stable
  68. func ManagerType() interface{} {
  69. return (*Manager)(nil)
  70. }
  71. var defaultBufferSize int32
  72. func init() {
  73. const key = "v2ray.ray.buffer.size"
  74. const defaultValue = -17
  75. size := platform.EnvFlag{
  76. Name: key,
  77. AltName: platform.NormalizeEnvName(key),
  78. }.GetValueAsInt(defaultValue)
  79. switch size {
  80. case 0:
  81. defaultBufferSize = -1 // For pipe to use unlimited size
  82. case defaultValue: // Env flag not defined. Use default values per CPU-arch.
  83. switch runtime.GOARCH {
  84. case "arm", "mips", "mipsle":
  85. defaultBufferSize = 0
  86. case "arm64", "mips64", "mips64le":
  87. defaultBufferSize = 4 * 1024 // 4k cache for low-end devices
  88. default:
  89. defaultBufferSize = 512 * 1024
  90. }
  91. default:
  92. defaultBufferSize = int32(size) * 1024 * 1024
  93. }
  94. }
  95. func defaultBufferPolicy() Buffer {
  96. return Buffer{
  97. PerConnection: defaultBufferSize,
  98. }
  99. }
  100. // SessionDefault returns the Policy when user is not specified.
  101. func SessionDefault() Session {
  102. return Session{
  103. Timeouts: Timeout{
  104. // Align Handshake timeout with nginx client_header_timeout
  105. // So that this value will not indicate server identity
  106. Handshake: time.Second * 60,
  107. ConnectionIdle: time.Second * 300,
  108. UplinkOnly: time.Second * 1,
  109. DownlinkOnly: time.Second * 1,
  110. },
  111. Stats: Stats{
  112. UserUplink: false,
  113. UserDownlink: false,
  114. },
  115. Buffer: defaultBufferPolicy(),
  116. }
  117. }
  118. type policyKey int32
  119. const (
  120. bufferPolicyKey policyKey = 0
  121. )
  122. func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
  123. return context.WithValue(ctx, bufferPolicyKey, p)
  124. }
  125. func BufferPolicyFromContext(ctx context.Context) Buffer {
  126. pPolicy := ctx.Value(bufferPolicyKey)
  127. if pPolicy == nil {
  128. return defaultBufferPolicy()
  129. }
  130. return pPolicy.(Buffer)
  131. }