policy.go 4.1 KB

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