config.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package policy
  2. import (
  3. "time"
  4. "v2ray.com/core"
  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 := core.DefaultPolicy()
  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. Enabled: p.Buffer.Enabled,
  24. Size: p.Buffer.Size,
  25. },
  26. }
  27. }
  28. func (p *Policy_Timeout) overrideWith(another *Policy_Timeout) {
  29. if another.Handshake != nil {
  30. p.Handshake = &Second{Value: another.Handshake.Value}
  31. }
  32. if another.ConnectionIdle != nil {
  33. p.ConnectionIdle = &Second{Value: another.ConnectionIdle.Value}
  34. }
  35. if another.UplinkOnly != nil {
  36. p.UplinkOnly = &Second{Value: another.UplinkOnly.Value}
  37. }
  38. if another.DownlinkOnly != nil {
  39. p.DownlinkOnly = &Second{Value: another.DownlinkOnly.Value}
  40. }
  41. }
  42. func (p *Policy) overrideWith(another *Policy) {
  43. if another.Timeout != nil {
  44. p.Timeout.overrideWith(another.Timeout)
  45. }
  46. if another.Stats != nil && p.Stats == nil {
  47. p.Stats = new(Policy_Stats)
  48. *p.Stats = *another.Stats
  49. }
  50. }
  51. // ToCorePolicy converts this Policy to core.Policy.
  52. func (p *Policy) ToCorePolicy() core.Policy {
  53. cp := core.DefaultPolicy()
  54. if p.Timeout != nil {
  55. cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
  56. cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
  57. cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
  58. cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
  59. }
  60. if p.Stats != nil {
  61. cp.Stats.UserUplink = p.Stats.UserUplink
  62. cp.Stats.UserDownlink = p.Stats.UserDownlink
  63. }
  64. if p.Buffer != nil {
  65. cp.Buffer.Enabled = p.Buffer.Enabled
  66. cp.Buffer.Size = p.Buffer.Size
  67. }
  68. return cp
  69. }
  70. // ToCorePolicy converts this SystemPolicy to core.SystemPolicy.
  71. func (p *SystemPolicy) ToCorePolicy() core.SystemPolicy {
  72. return core.SystemPolicy{
  73. Stats: core.SystemStatsPolicy{
  74. InboundUplink: p.Stats.InboundUplink,
  75. InboundDownlink: p.Stats.InboundDownlink,
  76. },
  77. }
  78. }