policy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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", "arm64", "mips", "mipsle", "mips64", "mips64le":
  76. defaultBufferSize = 16 * 1024 // 16k cache for low-end devices
  77. default:
  78. defaultBufferSize = 2 * 1024 * 1024
  79. }
  80. default:
  81. defaultBufferSize = int32(size) * 1024 * 1024
  82. }
  83. }
  84. func defaultBufferPolicy() Buffer {
  85. return Buffer{
  86. PerConnection: defaultBufferSize,
  87. }
  88. }
  89. // SessionDefault returns the Policy when user is not specified.
  90. func SessionDefault() Session {
  91. return Session{
  92. Timeouts: Timeout{
  93. Handshake: time.Second * 4,
  94. ConnectionIdle: time.Second * 300,
  95. UplinkOnly: time.Second * 2,
  96. DownlinkOnly: time.Second * 5,
  97. },
  98. Stats: Stats{
  99. UserUplink: false,
  100. UserDownlink: false,
  101. },
  102. Buffer: defaultBufferPolicy(),
  103. }
  104. }
  105. type policyKey int32
  106. const (
  107. bufferPolicyKey policyKey = 0
  108. )
  109. func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
  110. return context.WithValue(ctx, bufferPolicyKey, p)
  111. }
  112. func BufferPolicyFromContext(ctx context.Context) Buffer {
  113. pPolicy := ctx.Value(bufferPolicyKey)
  114. if pPolicy == nil {
  115. return defaultBufferPolicy()
  116. }
  117. return pPolicy.(Buffer)
  118. }