Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//go:build !confonly
// +build !confonly
 
package router
 
import (
    "context"
    "encoding/json"
 
    "github.com/golang/protobuf/jsonpb"
 
    "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"
)
 
type Rule struct {
    Tag       string
    Balancer  *Balancer
    Condition Condition
}
 
func (r *Rule) GetTag() (string, error) {
    if r.Balancer != nil {
        return r.Balancer.PickOutbound()
    }
    return r.Tag, nil
}
 
// Apply checks rule matching of current routing context.
func (r *Rule) Apply(ctx routing.Context) bool {
    return r.Condition.Apply(ctx)
}
 
func (rr *RoutingRule) BuildCondition() (Condition, error) {
    conds := NewConditionChan()
 
    if len(rr.Domain) > 0 {
        cond, err := NewDomainMatcher(rr.DomainMatcher, rr.Domain)
        if err != nil {
            return nil, newError("failed to build domain condition").Base(err)
        }
        conds.Add(cond)
    }
 
    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)
    }
 
    if len(rr.UserEmail) > 0 {
        conds.Add(NewUserMatcher(rr.UserEmail))
    }
 
    if len(rr.InboundTag) > 0 {
        conds.Add(NewInboundTagMatcher(rr.InboundTag))
    }
 
    if rr.PortList != nil {
        conds.Add(NewPortMatcher(rr.PortList, false))
    } else if rr.PortRange != nil {
        conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
    }
 
    if rr.SourcePortList != nil {
        conds.Add(NewPortMatcher(rr.SourcePortList, true))
    }
 
    if len(rr.Networks) > 0 {
        conds.Add(NewNetworkMatcher(rr.Networks))
    } else if rr.NetworkList != nil {
        conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
    }
 
    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 {
        cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.Cidr}}, false)
        if err != nil {
            return nil, err
        }
        conds.Add(cond)
    }
 
    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 {
        cond, err := NewMultiGeoIPMatcher([]*routercommon.GeoIP{{Cidr: rr.SourceCidr}}, true)
        if err != nil {
            return nil, err
        }
        conds.Add(cond)
    }
 
    if len(rr.Protocol) > 0 {
        conds.Add(NewProtocolMatcher(rr.Protocol))
    }
 
    if len(rr.Attributes) > 0 {
        cond, err := NewAttributeMatcher(rr.Attributes)
        if err != nil {
            return nil, err
        }
        conds.Add(cond)
    }
 
    if conds.Len() == 0 {
        return nil, newError("this rule has no effective fields").AtWarning()
    }
 
    return conds, nil
}
 
// Build builds the balancing rule
func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
    switch br.Strategy {
    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()
        }
        return &Balancer{
            selectors: br.OutboundSelector,
            strategy:  &LeastPingStrategy{config: s},
            ohm:       ohm,
        }, nil
    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
    case "random":
        fallthrough
    case "":
        return &Balancer{
            selectors: br.OutboundSelector,
            ohm:       ohm, fallbackTag: br.FallbackTag,
            strategy: &RandomStrategy{},
        }, nil
    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"`
    }
 
    var stub BalancingRuleStub
    if err := json.Unmarshal(bytes, &stub); err != nil {
        return err
    }
    if stub.Strategy == "" {
        stub.Strategy = "random"
    }
    settingsPack, err := v5cfg.LoadHeterogeneousConfigFromRawJSON(context.TODO(), "balancer", stub.Strategy, stub.StrategySettings)
    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
}