config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // OverrideWith overrides current Policy with another one.
  14. func (p *Policy) OverrideWith(another *Policy) {
  15. if another.Timeout != nil {
  16. if another.Timeout.Handshake != nil {
  17. p.Timeout.Handshake = another.Timeout.Handshake
  18. }
  19. if another.Timeout.ConnectionIdle != nil {
  20. p.Timeout.ConnectionIdle = another.Timeout.ConnectionIdle
  21. }
  22. if another.Timeout.UplinkOnly != nil {
  23. p.Timeout.UplinkOnly = another.Timeout.UplinkOnly
  24. }
  25. if another.Timeout.DownlinkOnly != nil {
  26. p.Timeout.DownlinkOnly = another.Timeout.DownlinkOnly
  27. }
  28. }
  29. }
  30. func (p *Policy) ToCorePolicy() core.Policy {
  31. var cp core.Policy
  32. if p.Timeout != nil {
  33. cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
  34. cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
  35. cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
  36. cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
  37. }
  38. return cp
  39. }