Browse Source

added custom balancer settings processing logic

Shelikhoo 4 years ago
parent
commit
30648c3e07
2 changed files with 39 additions and 0 deletions
  1. 34 0
      app/router/config.go
  2. 5 0
      infra/conf/v5cfg/common.go

+ 34 - 0
app/router/config.go

@@ -1,11 +1,15 @@
 package router
 package router
 
 
 import (
 import (
+	"context"
+	"encoding/json"
+	"github.com/golang/protobuf/jsonpb"
 	"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
 	"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
 	"github.com/v2fly/v2ray-core/v4/common/net"
 	"github.com/v2fly/v2ray-core/v4/common/net"
 	"github.com/v2fly/v2ray-core/v4/common/serial"
 	"github.com/v2fly/v2ray-core/v4/common/serial"
 	"github.com/v2fly/v2ray-core/v4/features/outbound"
 	"github.com/v2fly/v2ray-core/v4/features/outbound"
 	"github.com/v2fly/v2ray-core/v4/features/routing"
 	"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.
 // 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")
 		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
+}

+ 5 - 0
infra/conf/v5cfg/common.go

@@ -14,3 +14,8 @@ func loadHeterogeneousConfigFromRawJson(interfaceType, name string, rawJson json
 	ctx := envctx.ContextWithEnvironment(context.TODO(), fsdef)
 	ctx := envctx.ContextWithEnvironment(context.TODO(), fsdef)
 	return registry.LoadImplementationByAlias(ctx, interfaceType, name, []byte(rawJson))
 	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)
+}