v2fly/infra/conf/v4/services.go

32 lines
876 B
Go
Raw Normal View History

package v4
2021-03-06 17:14:01 +00:00
import (
"encoding/json"
2021-06-19 13:36:54 +00:00
"google.golang.org/protobuf/types/known/anypb"
2021-04-08 19:56:04 +00:00
2021-03-06 17:14:01 +00:00
"github.com/golang/protobuf/jsonpb"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
2021-04-08 19:56:04 +00:00
2021-03-06 17:14:01 +00:00
"github.com/v2fly/v2ray-core/v4/common/serial"
)
2021-06-19 13:36:54 +00:00
func (c *Config) BuildServices(service map[string]*json.RawMessage) ([]*anypb.Any, error) {
var ret []*anypb.Any
2021-03-06 17:14:01 +00:00
for k, v := range service {
message, err := desc.LoadMessageDescriptor(k)
2021-03-06 17:48:39 +00:00
if err != nil || message == nil {
2021-03-06 17:14:01 +00:00
return nil, newError("Cannot find service", k, "").Base(err)
}
serviceConfig := dynamic.NewMessage(message)
2021-03-06 17:48:39 +00:00
if err := serviceConfig.UnmarshalJSONPB(&jsonpb.Unmarshaler{AllowUnknownFields: false}, *v); err != nil {
2021-03-06 17:14:01 +00:00
return nil, newError("Cannot interpret service configure file", k, "").Base(err)
}
ret = append(ret, serial.ToTypedMessage(serviceConfig))
}
return ret, nil
}