config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }
  23. }
  24. func (p *Policy_Timeout) overrideWith(another *Policy_Timeout) {
  25. if another.Handshake != nil {
  26. p.Handshake = &Second{Value: another.Handshake.Value}
  27. }
  28. if another.ConnectionIdle != nil {
  29. p.ConnectionIdle = &Second{Value: another.ConnectionIdle.Value}
  30. }
  31. if another.UplinkOnly != nil {
  32. p.UplinkOnly = &Second{Value: another.UplinkOnly.Value}
  33. }
  34. if another.DownlinkOnly != nil {
  35. p.DownlinkOnly = &Second{Value: another.DownlinkOnly.Value}
  36. }
  37. }
  38. func (p *Policy) overrideWith(another *Policy) {
  39. if another.Timeout != nil {
  40. p.Timeout.overrideWith(another.Timeout)
  41. }
  42. if another.Stats != nil && p.Stats == nil {
  43. p.Stats = new(Policy_Stats)
  44. *p.Stats = *another.Stats
  45. }
  46. }
  47. // ToCorePolicy converts this Policy to core.Policy.
  48. func (p *Policy) ToCorePolicy() core.Policy {
  49. var cp core.Policy
  50. if p.Timeout != nil {
  51. cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
  52. cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
  53. cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
  54. cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
  55. }
  56. if p.Stats != nil {
  57. cp.Stats.UserUplink = p.Stats.UserUplink
  58. cp.Stats.UserDownlink = p.Stats.UserDownlink
  59. }
  60. return cp
  61. }