config_cache.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package registry
  2. import (
  3. "v2ray.com/core/common/loader"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/golang/protobuf/ptypes"
  6. "github.com/golang/protobuf/ptypes/any"
  7. )
  8. var (
  9. inboundConfigCreatorCache = loader.ConfigCreatorCache{}
  10. outboundConfigCreatorCache = loader.ConfigCreatorCache{}
  11. )
  12. func RegisterInboundConfig(protocol string, creator loader.ConfigCreator) error {
  13. return inboundConfigCreatorCache.RegisterCreator(protocol, creator)
  14. }
  15. func RegisterOutboundConfig(protocol string, creator loader.ConfigCreator) error {
  16. return outboundConfigCreatorCache.RegisterCreator(protocol, creator)
  17. }
  18. func MarshalInboundConfig(protocol string, settings *any.Any) (interface{}, error) {
  19. config, err := inboundConfigCreatorCache.CreateConfig(protocol)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if settings == nil {
  24. return config, nil
  25. }
  26. if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
  27. return nil, err
  28. }
  29. return config, nil
  30. }
  31. func MarshalOutboundConfig(protocol string, settings *any.Any) (interface{}, error) {
  32. config, err := outboundConfigCreatorCache.CreateConfig(protocol)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if settings == nil {
  37. return config, nil
  38. }
  39. if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
  40. return nil, err
  41. }
  42. return config, nil
  43. }