policy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package policy
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "v2ray.com/core/common/platform"
  7. "v2ray.com/core/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. }
  39. // System contains policy settings at system level.
  40. type System struct {
  41. Stats SystemStats
  42. Buffer Buffer
  43. }
  44. // Session 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 Session struct {
  46. Timeouts Timeout // Timeout settings
  47. Stats Stats
  48. Buffer Buffer
  49. }
  50. // Manager is a feature that provides Policy for the given user by its id or level.
  51. type Manager interface {
  52. features.Feature
  53. // ForLevel returns the Session policy for the given user level.
  54. ForLevel(level uint32) Session
  55. // ForSystem returns the System policy for V2Ray system.
  56. ForSystem() System
  57. }
  58. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  59. func ManagerType() interface{} {
  60. return (*Manager)(nil)
  61. }
  62. var defaultBufferSize int32
  63. func init() {
  64. const key = "v2ray.ray.buffer.size"
  65. const defaultValue = -17
  66. size := platform.EnvFlag{
  67. Name: key,
  68. AltName: platform.NormalizeEnvName(key),
  69. }.GetValueAsInt(defaultValue)
  70. switch size {
  71. case 0:
  72. defaultBufferSize = -1 // For pipe to use unlimited size
  73. case defaultValue: // Env flag not defined. Use default values per CPU-arch.
  74. switch runtime.GOARCH {
  75. case "arm", "mips", "mipsle":
  76. defaultBufferSize = 0
  77. case "arm64", "mips64", "mips64le":
  78. defaultBufferSize = 4 * 1024 // 4k cache for low-end devices
  79. default:
  80. defaultBufferSize = 512 * 1024
  81. }
  82. default:
  83. defaultBufferSize = int32(size) * 1024 * 1024
  84. }
  85. }
  86. func defaultBufferPolicy() Buffer {
  87. return Buffer{
  88. PerConnection: defaultBufferSize,
  89. }
  90. }
  91. // SessionDefault returns the Policy when user is not specified.
  92. func SessionDefault() Session {
  93. return Session{
  94. Timeouts: Timeout{
  95. Handshake: time.Second * 4,
  96. ConnectionIdle: time.Second * 300,
  97. UplinkOnly: time.Second * 1,
  98. DownlinkOnly: time.Second * 1,
  99. },
  100. Stats: Stats{
  101. UserUplink: false,
  102. UserDownlink: false,
  103. },
  104. Buffer: defaultBufferPolicy(),
  105. }
  106. }
  107. type policyKey int32
  108. const (
  109. bufferPolicyKey policyKey = 0
  110. )
  111. func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
  112. return context.WithValue(ctx, bufferPolicyKey, p)
  113. }
  114. func BufferPolicyFromContext(ctx context.Context) Buffer {
  115. pPolicy := ctx.Value(bufferPolicyKey)
  116. if pPolicy == nil {
  117. return defaultBufferPolicy()
  118. }
  119. return pPolicy.(Buffer)
  120. }