config.go 6.0 KB

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