2019-02-10 13:04:11 -05:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
2021-05-03 22:15:11 -04:00
|
|
|
"context"
|
2019-02-10 13:04:11 -05:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-04 09:52:35 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
2021-05-04 06:28:34 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
2021-05-03 22:15:11 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
|
|
|
|
rule2 "github.com/v2fly/v2ray-core/v4/infra/conf/rule"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type RouterRulesConfig struct {
|
|
|
|
RuleList []json.RawMessage `json:"rules"`
|
|
|
|
DomainStrategy string `json:"domainStrategy"`
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:08:20 -04:00
|
|
|
// StrategyConfig represents a strategy config
|
|
|
|
type StrategyConfig struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Settings *json.RawMessage `json:"settings"`
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
type BalancingRule struct {
|
2021-05-03 22:15:11 -04:00
|
|
|
Tag string `json:"tag"`
|
|
|
|
Selectors cfgcommon.StringList `json:"selector"`
|
|
|
|
Strategy StrategyConfig `json:"strategy"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BalancingRule) Build() (*router.BalancingRule, error) {
|
2019-06-14 09:47:28 -04:00
|
|
|
if r.Tag == "" {
|
2019-02-10 13:04:11 -05:00
|
|
|
return nil, newError("empty balancer tag")
|
|
|
|
}
|
|
|
|
if len(r.Selectors) == 0 {
|
|
|
|
return nil, newError("empty selector list")
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:08:20 -04:00
|
|
|
var strategy string
|
|
|
|
switch strings.ToLower(r.Strategy.Type) {
|
|
|
|
case strategyRandom, "":
|
|
|
|
strategy = strategyRandom
|
|
|
|
case strategyLeastPing:
|
|
|
|
strategy = "leastPing"
|
|
|
|
default:
|
|
|
|
return nil, newError("unknown balancing strategy: " + r.Strategy.Type)
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
return &router.BalancingRule{
|
|
|
|
Tag: r.Tag,
|
|
|
|
OutboundSelector: []string(r.Selectors),
|
2021-04-08 16:08:20 -04:00
|
|
|
Strategy: strategy,
|
2019-02-10 13:04:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouterConfig struct {
|
|
|
|
Settings *RouterRulesConfig `json:"settings"` // Deprecated
|
|
|
|
RuleList []json.RawMessage `json:"rules"`
|
|
|
|
DomainStrategy *string `json:"domainStrategy"`
|
|
|
|
Balancers []*BalancingRule `json:"balancers"`
|
2021-03-05 13:37:32 -05:00
|
|
|
|
|
|
|
DomainMatcher string `json:"domainMatcher"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
|
|
|
|
ds := ""
|
|
|
|
if c.DomainStrategy != nil {
|
|
|
|
ds = *c.DomainStrategy
|
|
|
|
} else if c.Settings != nil {
|
|
|
|
ds = c.Settings.DomainStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(ds) {
|
2020-12-17 19:36:34 -05:00
|
|
|
case "alwaysip", "always_ip", "always-ip":
|
2019-02-10 13:04:11 -05:00
|
|
|
return router.Config_UseIp
|
2020-12-17 19:36:34 -05:00
|
|
|
case "ipifnonmatch", "ip_if_non_match", "ip-if-non-match":
|
2019-02-10 13:04:11 -05:00
|
|
|
return router.Config_IpIfNonMatch
|
2020-12-17 19:36:34 -05:00
|
|
|
case "ipondemand", "ip_on_demand", "ip-on-demand":
|
2019-02-10 13:04:11 -05:00
|
|
|
return router.Config_IpOnDemand
|
|
|
|
default:
|
|
|
|
return router.Config_AsIs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RouterConfig) Build() (*router.Config, error) {
|
|
|
|
config := new(router.Config)
|
|
|
|
config.DomainStrategy = c.getDomainStrategy()
|
|
|
|
|
2021-05-03 22:15:11 -04:00
|
|
|
cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background())
|
|
|
|
|
2021-05-04 06:28:34 -04:00
|
|
|
geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
|
|
|
|
return "standard"
|
|
|
|
})
|
2021-05-03 22:15:11 -04:00
|
|
|
|
2021-05-04 06:28:34 -04:00
|
|
|
if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
|
2021-05-03 22:15:11 -04:00
|
|
|
cfgcommon.SetGeoDataLoader(cfgctx, loader)
|
|
|
|
} else {
|
|
|
|
return nil, newError("unable to create geo data loader ").Base(err)
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
var rawRuleList []json.RawMessage
|
|
|
|
if c != nil {
|
|
|
|
rawRuleList = c.RuleList
|
|
|
|
if c.Settings != nil {
|
|
|
|
c.RuleList = append(c.RuleList, c.Settings.RuleList...)
|
|
|
|
rawRuleList = c.RuleList
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
for _, rawRule := range rawRuleList {
|
2021-05-03 22:15:11 -04:00
|
|
|
rule, err := rule2.ParseRule(cfgctx, rawRule)
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-05 13:37:32 -05:00
|
|
|
|
|
|
|
if rule.DomainMatcher == "" {
|
|
|
|
rule.DomainMatcher = c.DomainMatcher
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
config.Rule = append(config.Rule, rule)
|
|
|
|
}
|
|
|
|
for _, rawBalancer := range c.Balancers {
|
|
|
|
balancer, err := rawBalancer.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.BalancingRule = append(config.BalancingRule, balancer)
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|