github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/v4/services.go (about)

     1  package v4
     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  	"google.golang.org/protobuf/types/known/anypb"
    10  
    11  	"github.com/v2fly/v2ray-core/v5/common/serial"
    12  )
    13  
    14  func (c *Config) BuildServices(service map[string]*json.RawMessage) ([]*anypb.Any, error) {
    15  	var ret []*anypb.Any
    16  	for k, v := range service {
    17  		message, err := desc.LoadMessageDescriptor(k)
    18  		if err != nil || message == nil {
    19  			return nil, newError("Cannot find service", k, "").Base(err)
    20  		}
    21  
    22  		serviceConfig := dynamic.NewMessage(message)
    23  
    24  		if err := serviceConfig.UnmarshalJSONPB(&jsonpb.Unmarshaler{AllowUnknownFields: false}, *v); err != nil {
    25  			return nil, newError("Cannot interpret service configure file", k, "").Base(err)
    26  		}
    27  
    28  		ret = append(ret, serial.ToTypedMessage(serviceConfig))
    29  	}
    30  	return ret, nil
    31  }