github.com/imannamdari/v2ray-core/v5@v5.0.5/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/imannamdari/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  }