policy.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. func ManagerType() interface{} {
  59. return (*Manager)(nil)
  60. }
  61. var defaultBufferSize int32
  62. func init() {
  63. const key = "v2ray.ray.buffer.size"
  64. const defaultValue = -17
  65. size := platform.EnvFlag{
  66. Name: key,
  67. AltName: platform.NormalizeEnvName(key),
  68. }.GetValueAsInt(defaultValue)
  69. switch size {
  70. case 0:
  71. defaultBufferSize = -1 // For pipe to use unlimited size
  72. case defaultValue: // Env flag not defined. Use default values per CPU-arch.
  73. switch runtime.GOARCH {
  74. case "arm", "arm64", "mips", "mipsle", "mips64", "mips64le":
  75. defaultBufferSize = 16 * 1024 // 16k cache for low-end devices
  76. default:
  77. defaultBufferSize = 2 * 1024 * 1024
  78. }
  79. default:
  80. defaultBufferSize = int32(size) * 1024 * 1024
  81. }
  82. }
  83. func defaultBufferPolicy() Buffer {
  84. return Buffer{
  85. PerConnection: defaultBufferSize,
  86. }
  87. }
  88. // SessionDefault returns the Policy when user is not specified.
  89. func SessionDefault() Session {
  90. return Session{
  91. Timeouts: Timeout{
  92. Handshake: time.Second * 4,
  93. ConnectionIdle: time.Second * 300,
  94. UplinkOnly: time.Second * 2,
  95. DownlinkOnly: time.Second * 5,
  96. },
  97. Stats: Stats{
  98. UserUplink: false,
  99. UserDownlink: false,
  100. },
  101. Buffer: defaultBufferPolicy(),
  102. }
  103. }
  104. type policyKey int32
  105. const (
  106. bufferPolicyKey policyKey = 0
  107. )
  108. func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
  109. return context.WithValue(ctx, bufferPolicyKey, p)
  110. }
  111. func BufferPolicyFromContext(ctx context.Context) Buffer {
  112. pPolicy := ctx.Value(bufferPolicyKey)
  113. if pPolicy == nil {
  114. return defaultBufferPolicy()
  115. }
  116. return pPolicy.(Buffer)
  117. }