github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/services.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/jsonpb"
     7  	"github.com/jhump/protoreflect/desc"
     8  	"github.com/jhump/protoreflect/dynamic"
     9  
    10  	"github.com/v2fly/v2ray-core/v4/common/serial"
    11  )
    12  
    13  func (c *Config) BuildServices(service map[string]*json.RawMessage) ([]*serial.TypedMessage, error) {
    14  	var ret []*serial.TypedMessage
    15  	for k, v := range service {
    16  		message, err := desc.LoadMessageDescriptor(k)
    17  		if err != nil || message == nil {
    18  			return nil, newError("Cannot find service", k, "").Base(err)
    19  		}
    20  
    21  		serviceConfig := dynamic.NewMessage(message)
    22  
    23  		if err := serviceConfig.UnmarshalJSONPB(&jsonpb.Unmarshaler{AllowUnknownFields: false}, *v); err != nil {
    24  			return nil, newError("Cannot interpret service configure file", k, "").Base(err)
    25  		}
    26  
    27  		ret = append(ret, serial.ToTypedMessage(serviceConfig))
    28  	}
    29  	return ret, nil
    30  }