policy.go 4.0 KB

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