config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. cond, err := NewDomainMatcher(rr.DomainMatcher, rr.Domain)
  34. if err != nil {
  35. return nil, newError("failed to build domain condition").Base(err)
  36. }
  37. conds.Add(cond)
  38. }
  39. if len(rr.UserEmail) > 0 {
  40. conds.Add(NewUserMatcher(rr.UserEmail))
  41. }
  42. if len(rr.InboundTag) > 0 {
  43. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  44. }
  45. if rr.PortList != nil {
  46. conds.Add(NewPortMatcher(rr.PortList, false))
  47. } else if rr.PortRange != nil {
  48. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
  49. }
  50. if rr.SourcePortList != nil {
  51. conds.Add(NewPortMatcher(rr.SourcePortList, true))
  52. }
  53. if len(rr.Networks) > 0 {
  54. conds.Add(NewNetworkMatcher(rr.Networks))
  55. } else if rr.NetworkList != nil {
  56. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  57. }
  58. if len(rr.Geoip) > 0 {
  59. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  60. if err != nil {
  61. return nil, err
  62. }
  63. conds.Add(cond)
  64. } else if len(rr.Cidr) > 0 {
  65. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
  66. if err != nil {
  67. return nil, err
  68. }
  69. conds.Add(cond)
  70. }
  71. if len(rr.SourceGeoip) > 0 {
  72. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  73. if err != nil {
  74. return nil, err
  75. }
  76. conds.Add(cond)
  77. } else if len(rr.SourceCidr) > 0 {
  78. cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
  79. if err != nil {
  80. return nil, err
  81. }
  82. conds.Add(cond)
  83. }
  84. if len(rr.Protocol) > 0 {
  85. conds.Add(NewProtocolMatcher(rr.Protocol))
  86. }
  87. if len(rr.Attributes) > 0 {
  88. cond, err := NewAttributeMatcher(rr.Attributes)
  89. if err != nil {
  90. return nil, err
  91. }
  92. conds.Add(cond)
  93. }
  94. if conds.Len() == 0 {
  95. return nil, newError("this rule has no effective fields").AtWarning()
  96. }
  97. return conds, nil
  98. }
  99. // Build builds the balancing rule
  100. func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
  101. switch br.Strategy {
  102. case "leastping":
  103. i, err := serial.GetInstanceOf(br.StrategySettings)
  104. if err != nil {
  105. return nil, err
  106. }
  107. s, ok := i.(*StrategyLeastPingConfig)
  108. if !ok {
  109. return nil, newError("not a StrategyLeastPingConfig").AtError()
  110. }
  111. return &Balancer{
  112. selectors: br.OutboundSelector,
  113. strategy: &LeastPingStrategy{config: s},
  114. ohm: ohm,
  115. }, nil
  116. case "leastload":
  117. i, err := serial.GetInstanceOf(br.StrategySettings)
  118. if err != nil {
  119. return nil, err
  120. }
  121. s, ok := i.(*StrategyLeastLoadConfig)
  122. if !ok {
  123. return nil, newError("not a StrategyLeastLoadConfig").AtError()
  124. }
  125. leastLoadStrategy := NewLeastLoadStrategy(s)
  126. return &Balancer{
  127. selectors: br.OutboundSelector,
  128. ohm: ohm, fallbackTag: br.FallbackTag,
  129. strategy: leastLoadStrategy,
  130. }, nil
  131. case "random":
  132. fallthrough
  133. case "":
  134. return &Balancer{
  135. selectors: br.OutboundSelector,
  136. ohm: ohm, fallbackTag: br.FallbackTag,
  137. strategy: &RandomStrategy{},
  138. }, nil
  139. default:
  140. return nil, newError("unrecognized balancer type")
  141. }
  142. }
  143. func (br *BalancingRule) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
  144. type BalancingRuleStub struct {
  145. Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
  146. OutboundSelector []string `protobuf:"bytes,2,rep,name=outbound_selector,json=outboundSelector,proto3" json:"outbound_selector,omitempty"`
  147. Strategy string `protobuf:"bytes,3,opt,name=strategy,proto3" json:"strategy,omitempty"`
  148. StrategySettings json.RawMessage `protobuf:"bytes,4,opt,name=strategy_settings,json=strategySettings,proto3" json:"strategy_settings,omitempty"`
  149. FallbackTag string `protobuf:"bytes,5,opt,name=fallback_tag,json=fallbackTag,proto3" json:"fallback_tag,omitempty"`
  150. }
  151. var stub BalancingRuleStub
  152. if err := json.Unmarshal(bytes, &stub); err != nil {
  153. return err
  154. }
  155. if stub.Strategy == "" {
  156. stub.Strategy = "random"
  157. }
  158. settingsPack, err := v5cfg.LoadHeterogeneousConfigFromRawJson(context.TODO(), "balancer", stub.Strategy, stub.StrategySettings)
  159. if err != nil {
  160. return err
  161. }
  162. br.StrategySettings = serial.ToTypedMessage(settingsPack)
  163. br.Tag = stub.Tag
  164. br.Strategy = stub.Strategy
  165. br.OutboundSelector = stub.OutboundSelector
  166. br.FallbackTag = stub.FallbackTag
  167. return nil
  168. }