config.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package policy
  2. import (
  3. "time"
  4. "github.com/xtls/xray-core/features/policy"
  5. )
  6. // Duration converts Second to time.Duration.
  7. func (s *Second) Duration() time.Duration {
  8. if s == nil {
  9. return 0
  10. }
  11. return time.Second * time.Duration(s.Value)
  12. }
  13. func defaultPolicy() *Policy {
  14. p := policy.SessionDefault()
  15. return &Policy{
  16. Timeout: &Policy_Timeout{
  17. Handshake: &Second{Value: uint32(p.Timeouts.Handshake / time.Second)},
  18. ConnectionIdle: &Second{Value: uint32(p.Timeouts.ConnectionIdle / time.Second)},
  19. UplinkOnly: &Second{Value: uint32(p.Timeouts.UplinkOnly / time.Second)},
  20. DownlinkOnly: &Second{Value: uint32(p.Timeouts.DownlinkOnly / time.Second)},
  21. },
  22. Buffer: &Policy_Buffer{
  23. Connection: p.Buffer.PerConnection,
  24. },
  25. }
  26. }
  27. func (p *Policy_Timeout) overrideWith(another *Policy_Timeout) {
  28. if another.Handshake != nil {
  29. p.Handshake = &Second{Value: another.Handshake.Value}
  30. }
  31. if another.ConnectionIdle != nil {
  32. p.ConnectionIdle = &Second{Value: another.ConnectionIdle.Value}
  33. }
  34. if another.UplinkOnly != nil {
  35. p.UplinkOnly = &Second{Value: another.UplinkOnly.Value}
  36. }
  37. if another.DownlinkOnly != nil {
  38. p.DownlinkOnly = &Second{Value: another.DownlinkOnly.Value}
  39. }
  40. }
  41. func (p *Policy) overrideWith(another *Policy) {
  42. if another.Timeout != nil {
  43. p.Timeout.overrideWith(another.Timeout)
  44. }
  45. if another.Stats != nil && p.Stats == nil {
  46. p.Stats = &Policy_Stats{}
  47. p.Stats = another.Stats
  48. }
  49. if another.Buffer != nil {
  50. p.Buffer = &Policy_Buffer{
  51. Connection: another.Buffer.Connection,
  52. }
  53. }
  54. }
  55. // ToCorePolicy converts this Policy to policy.Session.
  56. func (p *Policy) ToCorePolicy() policy.Session {
  57. cp := policy.SessionDefault()
  58. if p.Timeout != nil {
  59. cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
  60. cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
  61. cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
  62. cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
  63. }
  64. if p.Stats != nil {
  65. cp.Stats.UserUplink = p.Stats.UserUplink
  66. cp.Stats.UserDownlink = p.Stats.UserDownlink
  67. cp.Stats.UserOnline = p.Stats.UserOnline
  68. }
  69. if p.Buffer != nil {
  70. cp.Buffer.PerConnection = p.Buffer.Connection
  71. }
  72. return cp
  73. }
  74. // ToCorePolicy converts this SystemPolicy to policy.System.
  75. func (p *SystemPolicy) ToCorePolicy() policy.System {
  76. return policy.System{
  77. Stats: policy.SystemStats{
  78. InboundUplink: p.Stats.InboundUplink,
  79. InboundDownlink: p.Stats.InboundDownlink,
  80. OutboundUplink: p.Stats.OutboundUplink,
  81. OutboundDownlink: p.Stats.OutboundDownlink,
  82. },
  83. }
  84. }