config.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 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 = new(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 core.Policy.
  56. func (p *Policy) ToCorePolicy() core.Policy {
  57. cp := core.DefaultPolicy()
  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 core.SystemPolicy.
  74. func (p *SystemPolicy) ToCorePolicy() core.SystemPolicy {
  75. return core.SystemPolicy{
  76. Stats: core.SystemStatsPolicy{
  77. InboundUplink: p.Stats.InboundUplink,
  78. InboundDownlink: p.Stats.InboundDownlink,
  79. },
  80. }
  81. }