policy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package core
  2. import (
  3. "sync"
  4. "time"
  5. "v2ray.com/core/common"
  6. )
  7. // TimeoutPolicy contains limits for connection timeout.
  8. type TimeoutPolicy struct {
  9. // Timeout for handshake phase in a connection.
  10. Handshake time.Duration
  11. // Timeout for connection being idle, i.e., there is no egress or ingress traffic in this connection.
  12. ConnectionIdle time.Duration
  13. // Timeout for an uplink only connection, i.e., the downlink of the connection has ben closed.
  14. UplinkOnly time.Duration
  15. // Timeout for an downlink only connection, i.e., the uplink of the connection has ben closed.
  16. DownlinkOnly time.Duration
  17. }
  18. // OverrideWith overrides the current TimeoutPolicy with another one. All timeouts with zero value will be overridden with the new value.
  19. func (p TimeoutPolicy) OverrideWith(another TimeoutPolicy) TimeoutPolicy {
  20. if p.Handshake == 0 {
  21. p.Handshake = another.Handshake
  22. }
  23. if p.ConnectionIdle == 0 {
  24. p.ConnectionIdle = another.ConnectionIdle
  25. }
  26. if p.UplinkOnly == 0 {
  27. p.UplinkOnly = another.UplinkOnly
  28. }
  29. if p.DownlinkOnly == 0 {
  30. p.DownlinkOnly = another.DownlinkOnly
  31. }
  32. return p
  33. }
  34. // Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
  35. type Policy struct {
  36. Timeouts TimeoutPolicy // Timeout settings
  37. }
  38. // OverrideWith overrides the current Policy with another one. All values with default value will be overridden.
  39. func (p Policy) OverrideWith(another Policy) Policy {
  40. p.Timeouts.OverrideWith(another.Timeouts)
  41. return p
  42. }
  43. // PolicyManager is a feature that provides Policy for the given user by its id or level.
  44. type PolicyManager interface {
  45. Feature
  46. // ForLevel returns the Policy for the given user level.
  47. ForLevel(level uint32) Policy
  48. }
  49. // DefaultPolicy returns the Policy when user is not specified.
  50. func DefaultPolicy() Policy {
  51. return Policy{
  52. Timeouts: TimeoutPolicy{
  53. Handshake: time.Second * 4,
  54. ConnectionIdle: time.Second * 300,
  55. UplinkOnly: time.Second * 5,
  56. DownlinkOnly: time.Second * 30,
  57. },
  58. }
  59. }
  60. type syncPolicyManager struct {
  61. sync.RWMutex
  62. PolicyManager
  63. }
  64. func (m *syncPolicyManager) ForLevel(level uint32) Policy {
  65. m.RLock()
  66. defer m.RUnlock()
  67. if m.PolicyManager == nil {
  68. p := DefaultPolicy()
  69. if level == 1 {
  70. p.Timeouts.ConnectionIdle = time.Second * 600
  71. }
  72. return p
  73. }
  74. return m.PolicyManager.ForLevel(level)
  75. }
  76. func (m *syncPolicyManager) Start() error {
  77. m.RLock()
  78. defer m.RUnlock()
  79. if m.PolicyManager == nil {
  80. return nil
  81. }
  82. return m.PolicyManager.Start()
  83. }
  84. func (m *syncPolicyManager) Close() error {
  85. m.RLock()
  86. defer m.RUnlock()
  87. return common.Close(m.PolicyManager)
  88. }
  89. func (m *syncPolicyManager) Set(manager PolicyManager) {
  90. if manager == nil {
  91. return
  92. }
  93. m.Lock()
  94. defer m.Unlock()
  95. m.PolicyManager = manager
  96. }