policy.go 2.8 KB

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