2021-09-10 09:31:25 +01:00
|
|
|
//go:build !confonly
|
|
|
|
// +build !confonly
|
|
|
|
|
2015-12-07 22:47:47 +01:00
|
|
|
package router
|
2015-11-14 14:24:56 +01:00
|
|
|
|
2016-10-12 16:11:13 +02:00
|
|
|
import (
|
2021-09-27 21:50:18 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-10-28 18:34:19 +08:00
|
|
|
|
2021-09-27 21:50:18 +01:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
2021-10-28 18:34:19 +08:00
|
|
|
|
2022-01-02 15:16:23 +00:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/routing"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/v5cfg"
|
2016-10-12 16:11:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Tag string
|
2018-11-07 21:08:20 +01:00
|
|
|
Balancer *Balancer
|
2016-10-12 16:11:13 +02:00
|
|
|
Condition Condition
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:08:20 +01:00
|
|
|
func (r *Rule) GetTag() (string, error) {
|
|
|
|
if r.Balancer != nil {
|
|
|
|
return r.Balancer.PickOutbound()
|
|
|
|
}
|
|
|
|
return r.Tag, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:32:19 +08:00
|
|
|
// Apply checks rule matching of current routing context.
|
|
|
|
func (r *Rule) Apply(ctx routing.Context) bool {
|
2017-05-08 11:48:41 +02:00
|
|
|
return r.Condition.Apply(ctx)
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 11:48:41 +02:00
|
|
|
func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
2016-10-12 16:11:13 +02:00
|
|
|
conds := NewConditionChan()
|
|
|
|
|
2017-05-08 11:48:41 +02:00
|
|
|
if len(rr.Domain) > 0 {
|
2021-10-31 18:01:13 +08:00
|
|
|
cond, err := NewDomainMatcher(rr.DomainMatcher, rr.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build domain condition").Base(err)
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
2021-10-31 18:01:13 +08:00
|
|
|
conds.Add(cond)
|
2022-05-07 02:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var geoDomains []*routercommon.Domain
|
|
|
|
for _, geo := range rr.GeoDomain {
|
|
|
|
geoDomains = append(geoDomains, geo.Domain...)
|
|
|
|
}
|
|
|
|
if len(geoDomains) > 0 {
|
|
|
|
cond, err := NewDomainMatcher(rr.DomainMatcher, geoDomains)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build geo domain condition").Base(err)
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:56:53 +02:00
|
|
|
if len(rr.UserEmail) > 0 {
|
|
|
|
conds.Add(NewUserMatcher(rr.UserEmail))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rr.InboundTag) > 0 {
|
|
|
|
conds.Add(NewInboundTagMatcher(rr.InboundTag))
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:43:00 +01:00
|
|
|
if rr.PortList != nil {
|
2020-08-09 16:53:45 +08:00
|
|
|
conds.Add(NewPortMatcher(rr.PortList, false))
|
2019-02-24 23:43:00 +01:00
|
|
|
} else if rr.PortRange != nil {
|
2020-08-09 16:53:45 +08:00
|
|
|
conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.SourcePortList != nil {
|
|
|
|
conds.Add(NewPortMatcher(rr.SourcePortList, true))
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:58:26 +01:00
|
|
|
if len(rr.Networks) > 0 {
|
|
|
|
conds.Add(NewNetworkMatcher(rr.Networks))
|
|
|
|
} else if rr.NetworkList != nil {
|
2018-11-20 16:12:14 +01:00
|
|
|
conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 09:10:41 +01:00
|
|
|
if len(rr.Geoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.Cidr) > 0 {
|
2021-09-07 09:13:58 +01:00
|
|
|
cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
|
2017-05-17 13:24:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-18 23:01:39 +02:00
|
|
|
}
|
2017-05-17 13:24:53 +02:00
|
|
|
conds.Add(cond)
|
2016-10-18 23:01:39 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 09:10:41 +01:00
|
|
|
if len(rr.SourceGeoip) > 0 {
|
|
|
|
cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
} else if len(rr.SourceCidr) > 0 {
|
2021-09-07 09:13:58 +01:00
|
|
|
cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
|
2018-04-06 15:56:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
2016-11-13 21:23:34 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 13:47:00 +02:00
|
|
|
if len(rr.Protocol) > 0 {
|
|
|
|
conds.Add(NewProtocolMatcher(rr.Protocol))
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:04:43 +01:00
|
|
|
if len(rr.Attributes) > 0 {
|
|
|
|
cond, err := NewAttributeMatcher(rr.Attributes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conds.Add(cond)
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:11:13 +02:00
|
|
|
if conds.Len() == 0 {
|
2017-12-27 22:25:12 +01:00
|
|
|
return nil, newError("this rule has no effective fields").AtWarning()
|
2016-10-12 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conds, nil
|
2015-11-14 14:24:56 +01:00
|
|
|
}
|
2018-11-07 21:08:20 +01:00
|
|
|
|
2021-09-27 21:50:18 +01:00
|
|
|
// Build builds the balancing rule
|
|
|
|
func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
|
2021-04-08 20:56:04 +01:00
|
|
|
switch br.Strategy {
|
2021-09-27 21:50:18 +01:00
|
|
|
case "leastping":
|
|
|
|
i, err := serial.GetInstanceOf(br.StrategySettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, ok := i.(*StrategyLeastPingConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not a StrategyLeastPingConfig").AtError()
|
|
|
|
}
|
2021-04-08 20:56:04 +01:00
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
2021-09-27 21:50:18 +01:00
|
|
|
strategy: &LeastPingStrategy{config: s},
|
2024-08-03 19:04:20 +08:00
|
|
|
ohm: ohm, fallbackTag: br.FallbackTag,
|
2021-04-08 20:56:04 +01:00
|
|
|
}, nil
|
2021-09-27 21:50:18 +01:00
|
|
|
case "leastload":
|
|
|
|
i, err := serial.GetInstanceOf(br.StrategySettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, ok := i.(*StrategyLeastLoadConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not a StrategyLeastLoadConfig").AtError()
|
|
|
|
}
|
|
|
|
leastLoadStrategy := NewLeastLoadStrategy(s)
|
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
|
|
|
ohm: ohm, fallbackTag: br.FallbackTag,
|
|
|
|
strategy: leastLoadStrategy,
|
|
|
|
}, nil
|
2021-04-08 20:56:04 +01:00
|
|
|
case "random":
|
|
|
|
fallthrough
|
2021-09-27 21:50:18 +01:00
|
|
|
case "":
|
2024-03-10 19:00:06 +08:00
|
|
|
var randomStrategy *RandomStrategy
|
|
|
|
if br.StrategySettings != nil {
|
|
|
|
i, err := serial.GetInstanceOf(br.StrategySettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, ok := i.(*StrategyRandomConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not a StrategyRandomConfig").AtError()
|
|
|
|
}
|
|
|
|
randomStrategy = NewRandomStrategy(s)
|
|
|
|
}
|
2021-04-08 20:56:04 +01:00
|
|
|
return &Balancer{
|
|
|
|
selectors: br.OutboundSelector,
|
2021-09-27 21:50:18 +01:00
|
|
|
ohm: ohm, fallbackTag: br.FallbackTag,
|
2024-03-10 19:00:06 +08:00
|
|
|
strategy: randomStrategy,
|
2021-04-08 20:56:04 +01:00
|
|
|
}, nil
|
2021-09-27 21:50:18 +01:00
|
|
|
default:
|
|
|
|
return nil, newError("unrecognized balancer type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *BalancingRule) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
|
|
|
|
type BalancingRuleStub struct {
|
|
|
|
Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
|
|
|
|
OutboundSelector []string `protobuf:"bytes,2,rep,name=outbound_selector,json=outboundSelector,proto3" json:"outbound_selector,omitempty"`
|
|
|
|
Strategy string `protobuf:"bytes,3,opt,name=strategy,proto3" json:"strategy,omitempty"`
|
|
|
|
StrategySettings json.RawMessage `protobuf:"bytes,4,opt,name=strategy_settings,json=strategySettings,proto3" json:"strategy_settings,omitempty"`
|
|
|
|
FallbackTag string `protobuf:"bytes,5,opt,name=fallback_tag,json=fallbackTag,proto3" json:"fallback_tag,omitempty"`
|
2021-09-07 11:42:45 +01:00
|
|
|
}
|
2021-09-27 21:50:18 +01:00
|
|
|
|
|
|
|
var stub BalancingRuleStub
|
|
|
|
if err := json.Unmarshal(bytes, &stub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stub.Strategy == "" {
|
|
|
|
stub.Strategy = "random"
|
|
|
|
}
|
2021-11-27 17:16:41 +08:00
|
|
|
settingsPack, err := v5cfg.LoadHeterogeneousConfigFromRawJSON(context.TODO(), "balancer", stub.Strategy, stub.StrategySettings)
|
2021-09-27 21:50:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
br.StrategySettings = serial.ToTypedMessage(settingsPack)
|
|
|
|
|
|
|
|
br.Tag = stub.Tag
|
|
|
|
br.Strategy = stub.Strategy
|
|
|
|
br.OutboundSelector = stub.OutboundSelector
|
|
|
|
br.FallbackTag = stub.FallbackTag
|
|
|
|
|
|
|
|
return nil
|
2021-09-07 11:42:45 +01:00
|
|
|
}
|