common.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package v5cfg
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/golang/protobuf/jsonpb"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/v2fly/v2ray-core/v4/common/registry"
  8. "github.com/v2fly/v2ray-core/v4/common/serial"
  9. "strings"
  10. )
  11. func loadHeterogeneousConfigFromRawJson(interfaceType, name string, rawJson json.RawMessage) (proto.Message, error) {
  12. var implementationFullName string
  13. if strings.HasPrefix(name, "#") {
  14. // skip resolution for full name
  15. implementationFullName = name
  16. } else {
  17. registryResult, err := registry.FindImplementationByAlias(interfaceType, name)
  18. if err != nil {
  19. return nil, newError("unable to find implementation").Base(err)
  20. }
  21. implementationFullName = registryResult
  22. }
  23. implementationConfigInstance, err := serial.GetInstance(implementationFullName)
  24. if err != nil {
  25. return nil, newError("unable to create implementation config instance").Base(err)
  26. }
  27. unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: false}
  28. err = unmarshaler.Unmarshal(bytes.NewReader([]byte(rawJson)), implementationConfigInstance.(proto.Message))
  29. if err != nil {
  30. return nil, newError("unable to parse json content").Base(err)
  31. }
  32. return implementationConfigInstance.(proto.Message), nil
  33. }