config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package router
  2. import (
  3. "context"
  4. "net"
  5. v2net "v2ray.com/core/common/net"
  6. )
  7. type Rule struct {
  8. Tag string
  9. Condition Condition
  10. }
  11. func (r *Rule) Apply(ctx context.Context) bool {
  12. return r.Condition.Apply(ctx)
  13. }
  14. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  15. conds := NewConditionChan()
  16. if len(rr.Domain) > 0 {
  17. anyCond := NewAnyCondition()
  18. for _, domain := range rr.Domain {
  19. switch domain.Type {
  20. case Domain_Plain:
  21. anyCond.Add(NewPlainDomainMatcher(domain.Value))
  22. case Domain_Regex:
  23. matcher, err := NewRegexpDomainMatcher(domain.Value)
  24. if err != nil {
  25. return nil, err
  26. }
  27. anyCond.Add(matcher)
  28. case Domain_Domain:
  29. anyCond.Add(NewSubDomainMatcher(domain.Value))
  30. default:
  31. panic("Unknown domain type.")
  32. }
  33. }
  34. conds.Add(anyCond)
  35. }
  36. if len(rr.Cidr) > 0 {
  37. ipv4Net := v2net.NewIPNet()
  38. ipv6Cond := NewAnyCondition()
  39. hasIpv6 := false
  40. for _, ip := range rr.Cidr {
  41. switch len(ip.Ip) {
  42. case net.IPv4len:
  43. ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
  44. case net.IPv6len:
  45. hasIpv6 = true
  46. matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, false)
  47. if err != nil {
  48. return nil, err
  49. }
  50. ipv6Cond.Add(matcher)
  51. default:
  52. return nil, newError("invalid IP length").AtError()
  53. }
  54. }
  55. if !ipv4Net.IsEmpty() && hasIpv6 {
  56. cond := NewAnyCondition()
  57. cond.Add(NewIPv4Matcher(ipv4Net, false))
  58. cond.Add(ipv6Cond)
  59. conds.Add(cond)
  60. } else if !ipv4Net.IsEmpty() {
  61. conds.Add(NewIPv4Matcher(ipv4Net, false))
  62. } else if hasIpv6 {
  63. conds.Add(ipv6Cond)
  64. }
  65. }
  66. if rr.PortRange != nil {
  67. conds.Add(NewPortMatcher(*rr.PortRange))
  68. }
  69. if rr.NetworkList != nil {
  70. conds.Add(NewNetworkMatcher(rr.NetworkList))
  71. }
  72. if len(rr.SourceCidr) > 0 {
  73. ipv4Net := v2net.NewIPNet()
  74. ipv6Cond := NewAnyCondition()
  75. hasIpv6 := false
  76. for _, ip := range rr.SourceCidr {
  77. switch len(ip.Ip) {
  78. case net.IPv4len:
  79. ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
  80. case net.IPv6len:
  81. hasIpv6 = true
  82. matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, true)
  83. if err != nil {
  84. return nil, err
  85. }
  86. ipv6Cond.Add(matcher)
  87. default:
  88. return nil, newError("invalid IP length").AtError()
  89. }
  90. }
  91. if !ipv4Net.IsEmpty() && hasIpv6 {
  92. cond := NewAnyCondition()
  93. cond.Add(NewIPv4Matcher(ipv4Net, true))
  94. cond.Add(ipv6Cond)
  95. conds.Add(cond)
  96. } else if !ipv4Net.IsEmpty() {
  97. conds.Add(NewIPv4Matcher(ipv4Net, true))
  98. } else if hasIpv6 {
  99. conds.Add(ipv6Cond)
  100. }
  101. }
  102. if len(rr.UserEmail) > 0 {
  103. conds.Add(NewUserMatcher(rr.UserEmail))
  104. }
  105. if len(rr.InboundTag) > 0 {
  106. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  107. }
  108. if conds.Len() == 0 {
  109. return nil, newError("this rule has no effective fields").AtError()
  110. }
  111. return conds, nil
  112. }