config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //go:build !confonly
  2. // +build !confonly
  3. package router
  4. import (
  5. "context"
  6. "encoding/json"
  7. "github.com/golang/protobuf/jsonpb"
  8. "github.com/v2fly/v2ray-core/v4/app/router/routercommon"
  9. "github.com/v2fly/v2ray-core/v4/common/net"
  10. "github.com/v2fly/v2ray-core/v4/common/serial"
  11. "github.com/v2fly/v2ray-core/v4/features/outbound"
  12. "github.com/v2fly/v2ray-core/v4/features/routing"
  13. "github.com/v2fly/v2ray-core/v4/infra/conf/v5cfg"
  14. )
  15. type Rule struct {
  16. Tag string
  17. Balancer *Balancer
  18. Condition Condition
  19. }
  20. func (r *Rule) GetTag() (string, error) {
  21. if r.Balancer != nil {
  22. return r.Balancer.PickOutbound()
  23. }
  24. return r.Tag, nil
  25. }
  26. // Apply checks rule matching of current routing context.
  27. func (r *Rule) Apply(ctx routing.Context) bool {
  28. return r.Condition.Apply(ctx)
  29. }
  30. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  31. conds := NewConditionChan()
  32. if len(rr.Domain) > 0 {
  33. switch rr.DomainMatcher {
  34. case "mph", "hybrid":
  35. matcher, err := NewMphMatcherGroup(rr.Domain)
  36. if err != nil {
  37. return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
  38. }
  39. newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
  40. conds.Add(matcher)
  41. case "linear":
  42. fallthrough
  43. default:
  44. matcher, err := NewDomainMatcher(rr.Domain)
  45. if err != nil {
  46. return nil, newError("failed to build domain condition").Base(err)
  47. }
  48. conds.Add(matcher)
  49. }
  50. }
  51. if len(rr.UserEmail) > 0 {
  52. conds.Add(NewUserMatcher(rr.UserEmail))
  53. }
  54. if len(rr.InboundTag) > 0 {
  55. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  56. }
  57. if rr.PortList != nil {
  58. conds.Add(NewPortMatcher(rr.PortList, false))
  59. } else if rr.PortRange != nil {
  60. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
  61. }
  62. if rr.SourcePortList != nil {
  63. conds.Add(NewPortMatcher(rr.SourcePortList, true))
  64. }
  65. if len(rr.Networks) > 0 {
  66. conds.Add(NewNetworkMatcher(rr.Networks))
  67. } else if rr.NetworkList != nil {
  68. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  69. }
  70. if len(rr.Geoip) > 0 {
  71. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  72. if err != nil {
  73. return nil, err
  74. }
  75. conds.Add(cond)
  76. } else if len(rr.Cidr) > 0 {
  77. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
  78. if err != nil {
  79. return nil, err
  80. }
  81. conds.Add(cond)
  82. }
  83. if len(rr.SourceGeoip) > 0 {
  84. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  85. if err != nil {
  86. return nil, err
  87. }
  88. conds.Add(cond)
  89. } else if len(rr.SourceCidr) > 0 {
  90. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
  91. if err != nil {
  92. return nil, err
  93. }
  94. conds.Add(cond)
  95. }
  96. if len(rr.Protocol) > 0 {
  97. conds.Add(NewProtocolMatcher(rr.Protocol))
  98. }
  99. if len(rr.Attributes) > 0 {
  100. cond, err := NewAttributeMatcher(rr.Attributes)
  101. if err != nil {
  102. return nil, err
  103. }
  104. conds.Add(cond)
  105. }
  106. if conds.Len() == 0 {
  107. return nil, newError("this rule has no effective fields").AtWarning()
  108. }
  109. return conds, nil
  110. }
  111. // Build builds the balancing rule
  112. func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
  113. switch br.Strategy {
  114. case "leastping":
  115. i, err := serial.GetInstanceOf(br.StrategySettings)
  116. if err != nil {
  117. return nil, err
  118. }
  119. s, ok := i.(*StrategyLeastPingConfig)
  120. if !ok {
  121. return nil, newError("not a StrategyLeastPingConfig").AtError()
  122. }
  123. return &Balancer{
  124. selectors: br.OutboundSelector,
  125. strategy: &LeastPingStrategy{config: s},
  126. ohm: ohm,
  127. }, nil
  128. case "leastload":
  129. i, err := serial.GetInstanceOf(br.StrategySettings)
  130. if err != nil {
  131. return nil, err
  132. }
  133. s, ok := i.(*StrategyLeastLoadConfig)
  134. if !ok {
  135. return nil, newError("not a StrategyLeastLoadConfig").AtError()
  136. }
  137. leastLoadStrategy := NewLeastLoadStrategy(s)
  138. return &Balancer{
  139. selectors: br.OutboundSelector,
  140. ohm: ohm, fallbackTag: br.FallbackTag,
  141. strategy: leastLoadStrategy,
  142. }, nil
  143. case "random":
  144. fallthrough
  145. case "":
  146. return &Balancer{
  147. selectors: br.OutboundSelector,
  148. ohm: ohm, fallbackTag: br.FallbackTag,
  149. strategy: &RandomStrategy{},
  150. }, nil
  151. default:
  152. return nil, newError("unrecognized balancer type")
  153. }
  154. }
  155. func (br *BalancingRule) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
  156. type BalancingRuleStub struct {
  157. Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
  158. OutboundSelector []string `protobuf:"bytes,2,rep,name=outbound_selector,json=outboundSelector,proto3" json:"outbound_selector,omitempty"`
  159. Strategy string `protobuf:"bytes,3,opt,name=strategy,proto3" json:"strategy,omitempty"`
  160. StrategySettings json.RawMessage `protobuf:"bytes,4,opt,name=strategy_settings,json=strategySettings,proto3" json:"strategy_settings,omitempty"`
  161. FallbackTag string `protobuf:"bytes,5,opt,name=fallback_tag,json=fallbackTag,proto3" json:"fallback_tag,omitempty"`
  162. }
  163. var stub BalancingRuleStub
  164. if err := json.Unmarshal(bytes, &stub); err != nil {
  165. return err
  166. }
  167. if stub.Strategy == "" {
  168. stub.Strategy = "random"
  169. }
  170. settingsPack, err := v5cfg.LoadHeterogeneousConfigFromRawJson(context.TODO(), "balancer", stub.Strategy, stub.StrategySettings)
  171. if err != nil {
  172. return err
  173. }
  174. br.StrategySettings = serial.ToTypedMessage(settingsPack)
  175. br.Tag = stub.Tag
  176. br.Strategy = stub.Strategy
  177. br.OutboundSelector = stub.OutboundSelector
  178. br.FallbackTag = stub.FallbackTag
  179. return nil
  180. }