1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00

added custom balancer settings processing logic

This commit is contained in:
Shelikhoo 2021-09-07 11:42:45 +01:00
parent 5182727054
commit 30648c3e07
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 39 additions and 0 deletions

View File

@ -1,11 +1,15 @@
package router
import (
"context"
"encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/features/outbound"
"github.com/v2fly/v2ray-core/v4/features/routing"
"github.com/v2fly/v2ray-core/v4/infra/conf/v5cfg"
)
// CIDRList is an alias of []*CIDR to provide sort.Interface.
@ -203,3 +207,33 @@ func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatch
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
}

View File

@ -14,3 +14,8 @@ func loadHeterogeneousConfigFromRawJson(interfaceType, name string, rawJson json
ctx := envctx.ContextWithEnvironment(context.TODO(), fsdef)
return registry.LoadImplementationByAlias(ctx, interfaceType, name, []byte(rawJson))
}
// LoadHeterogeneousConfigFromRawJson private API
func LoadHeterogeneousConfigFromRawJson(ctx context.Context, interfaceType, name string, rawJson json.RawMessage) (proto.Message, error) {
return loadHeterogeneousConfigFromRawJson(interfaceType, name, rawJson)
}