router.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package router
  2. //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
  3. import (
  4. "context"
  5. "encoding/json"
  6. "strings"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/v2fly/v2ray-core/v4/app/router"
  9. "github.com/v2fly/v2ray-core/v4/common/platform"
  10. "github.com/v2fly/v2ray-core/v4/common/serial"
  11. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
  12. "github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
  13. rule2 "github.com/v2fly/v2ray-core/v4/infra/conf/rule"
  14. )
  15. type RouterRulesConfig struct {
  16. RuleList []json.RawMessage `json:"rules"`
  17. DomainStrategy string `json:"domainStrategy"`
  18. }
  19. // StrategyConfig represents a strategy config
  20. type StrategyConfig struct {
  21. Type string `json:"type"`
  22. Settings *json.RawMessage `json:"settings"`
  23. }
  24. type BalancingRule struct {
  25. Tag string `json:"tag"`
  26. Selectors cfgcommon.StringList `json:"selector"`
  27. Strategy StrategyConfig `json:"strategy"`
  28. FallbackTag string `json:"fallbackTag"`
  29. }
  30. // Build builds the balancing rule
  31. func (r *BalancingRule) Build() (*router.BalancingRule, error) {
  32. if r.Tag == "" {
  33. return nil, newError("empty balancer tag")
  34. }
  35. if len(r.Selectors) == 0 {
  36. return nil, newError("empty selector list")
  37. }
  38. var strategy string
  39. switch strings.ToLower(r.Strategy.Type) {
  40. case strategyRandom, "":
  41. r.Strategy.Type = strategyRandom
  42. strategy = strategyRandom
  43. case strategyLeastLoad:
  44. strategy = strategyLeastLoad
  45. case strategyLeastPing:
  46. strategy = "leastping"
  47. default:
  48. return nil, newError("unknown balancing strategy: " + r.Strategy.Type)
  49. }
  50. settings := []byte("{}")
  51. if r.Strategy.Settings != nil {
  52. settings = ([]byte)(*r.Strategy.Settings)
  53. }
  54. rawConfig, err := strategyConfigLoader.LoadWithID(settings, r.Strategy.Type)
  55. if err != nil {
  56. return nil, newError("failed to parse to strategy config.").Base(err)
  57. }
  58. var ts proto.Message
  59. if builder, ok := rawConfig.(cfgcommon.Buildable); ok {
  60. ts, err = builder.Build()
  61. if err != nil {
  62. return nil, err
  63. }
  64. }
  65. return &router.BalancingRule{
  66. Strategy: strategy,
  67. StrategySettings: serial.ToTypedMessage(ts),
  68. FallbackTag: r.FallbackTag,
  69. OutboundSelector: r.Selectors,
  70. Tag: r.Tag,
  71. }, nil
  72. }
  73. type RouterConfig struct {
  74. Settings *RouterRulesConfig `json:"settings"` // Deprecated
  75. RuleList []json.RawMessage `json:"rules"`
  76. DomainStrategy *string `json:"domainStrategy"`
  77. Balancers []*BalancingRule `json:"balancers"`
  78. DomainMatcher string `json:"domainMatcher"`
  79. }
  80. func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
  81. ds := ""
  82. if c.DomainStrategy != nil {
  83. ds = *c.DomainStrategy
  84. } else if c.Settings != nil {
  85. ds = c.Settings.DomainStrategy
  86. }
  87. switch strings.ToLower(ds) {
  88. case "alwaysip", "always_ip", "always-ip":
  89. return router.Config_UseIp
  90. case "ipifnonmatch", "ip_if_non_match", "ip-if-non-match":
  91. return router.Config_IpIfNonMatch
  92. case "ipondemand", "ip_on_demand", "ip-on-demand":
  93. return router.Config_IpOnDemand
  94. default:
  95. return router.Config_AsIs
  96. }
  97. }
  98. func (c *RouterConfig) Build() (*router.Config, error) {
  99. config := new(router.Config)
  100. config.DomainStrategy = c.getDomainStrategy()
  101. cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background())
  102. geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
  103. return "standard"
  104. })
  105. if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
  106. cfgcommon.SetGeoDataLoader(cfgctx, loader)
  107. } else {
  108. return nil, newError("unable to create geo data loader ").Base(err)
  109. }
  110. var rawRuleList []json.RawMessage
  111. if c != nil {
  112. rawRuleList = c.RuleList
  113. if c.Settings != nil {
  114. c.RuleList = append(c.RuleList, c.Settings.RuleList...)
  115. rawRuleList = c.RuleList
  116. }
  117. }
  118. for _, rawRule := range rawRuleList {
  119. rule, err := rule2.ParseRule(cfgctx, rawRule)
  120. if err != nil {
  121. return nil, err
  122. }
  123. if rule.DomainMatcher == "" {
  124. rule.DomainMatcher = c.DomainMatcher
  125. }
  126. config.Rule = append(config.Rule, rule)
  127. }
  128. for _, rawBalancer := range c.Balancers {
  129. balancer, err := rawBalancer.Build()
  130. if err != nil {
  131. return nil, err
  132. }
  133. config.BalancingRule = append(config.BalancingRule, balancer)
  134. }
  135. return config, nil
  136. }