config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //go:build !confonly
  2. // +build !confonly
  3. package router
  4. import (
  5. "github.com/v2fly/v2ray-core/v4/app/router/routercommon"
  6. "github.com/v2fly/v2ray-core/v4/common/net"
  7. "github.com/v2fly/v2ray-core/v4/features/outbound"
  8. "github.com/v2fly/v2ray-core/v4/features/routing"
  9. )
  10. type Rule struct {
  11. Tag string
  12. Balancer *Balancer
  13. Condition Condition
  14. }
  15. func (r *Rule) GetTag() (string, error) {
  16. if r.Balancer != nil {
  17. return r.Balancer.PickOutbound()
  18. }
  19. return r.Tag, nil
  20. }
  21. // Apply checks rule matching of current routing context.
  22. func (r *Rule) Apply(ctx routing.Context) bool {
  23. return r.Condition.Apply(ctx)
  24. }
  25. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  26. conds := NewConditionChan()
  27. if len(rr.Domain) > 0 {
  28. switch rr.DomainMatcher {
  29. case "mph", "hybrid":
  30. matcher, err := NewMphMatcherGroup(rr.Domain)
  31. if err != nil {
  32. return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
  33. }
  34. newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
  35. conds.Add(matcher)
  36. case "linear":
  37. fallthrough
  38. default:
  39. matcher, err := NewDomainMatcher(rr.Domain)
  40. if err != nil {
  41. return nil, newError("failed to build domain condition").Base(err)
  42. }
  43. conds.Add(matcher)
  44. }
  45. }
  46. if len(rr.UserEmail) > 0 {
  47. conds.Add(NewUserMatcher(rr.UserEmail))
  48. }
  49. if len(rr.InboundTag) > 0 {
  50. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  51. }
  52. if rr.PortList != nil {
  53. conds.Add(NewPortMatcher(rr.PortList, false))
  54. } else if rr.PortRange != nil {
  55. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
  56. }
  57. if rr.SourcePortList != nil {
  58. conds.Add(NewPortMatcher(rr.SourcePortList, true))
  59. }
  60. if len(rr.Networks) > 0 {
  61. conds.Add(NewNetworkMatcher(rr.Networks))
  62. } else if rr.NetworkList != nil {
  63. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  64. }
  65. if len(rr.Geoip) > 0 {
  66. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  67. if err != nil {
  68. return nil, err
  69. }
  70. conds.Add(cond)
  71. } else if len(rr.Cidr) > 0 {
  72. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
  73. if err != nil {
  74. return nil, err
  75. }
  76. conds.Add(cond)
  77. }
  78. if len(rr.SourceGeoip) > 0 {
  79. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  80. if err != nil {
  81. return nil, err
  82. }
  83. conds.Add(cond)
  84. } else if len(rr.SourceCidr) > 0 {
  85. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
  86. if err != nil {
  87. return nil, err
  88. }
  89. conds.Add(cond)
  90. }
  91. if len(rr.Protocol) > 0 {
  92. conds.Add(NewProtocolMatcher(rr.Protocol))
  93. }
  94. if len(rr.Attributes) > 0 {
  95. cond, err := NewAttributeMatcher(rr.Attributes)
  96. if err != nil {
  97. return nil, err
  98. }
  99. conds.Add(cond)
  100. }
  101. if conds.Len() == 0 {
  102. return nil, newError("this rule has no effective fields").AtWarning()
  103. }
  104. return conds, nil
  105. }
  106. func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
  107. switch br.Strategy {
  108. case "leastPing":
  109. return &Balancer{
  110. selectors: br.OutboundSelector,
  111. strategy: &LeastPingStrategy{},
  112. ohm: ohm,
  113. }, nil
  114. case "random":
  115. fallthrough
  116. default:
  117. return &Balancer{
  118. selectors: br.OutboundSelector,
  119. strategy: &RandomStrategy{},
  120. ohm: ohm,
  121. }, nil
  122. }
  123. }