config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package policy
  2. import (
  3. "time"
  4. "github.com/xtls/xray-core/v1/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. }
  68. if p.Buffer != nil {
  69. cp.Buffer.PerConnection = p.Buffer.Connection
  70. }
  71. return cp
  72. }
  73. // ToCorePolicy converts this SystemPolicy to policy.System.
  74. func (p *SystemPolicy) ToCorePolicy() policy.System {
  75. return policy.System{
  76. Stats: policy.SystemStats{
  77. InboundUplink: p.Stats.InboundUplink,
  78. InboundDownlink: p.Stats.InboundDownlink,
  79. OutboundUplink: p.Stats.OutboundUplink,
  80. OutboundDownlink: p.Stats.OutboundDownlink,
  81. },
  82. }
  83. }