outbound_parser.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package specs
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/v2fly/v2ray-core/v5/common/registry"
  8. "github.com/v2fly/v2ray-core/v5/common/serial"
  9. )
  10. func NewOutboundParser() *OutboundParser {
  11. return &OutboundParser{}
  12. }
  13. type OutboundParser struct{}
  14. func (p *OutboundParser) ParseOutboundConfig(rawConfig []byte) (*OutboundConfig, error) {
  15. skeleton := &OutboundConfig{}
  16. decoder := json.NewDecoder(bytes.NewReader(rawConfig))
  17. decoder.DisallowUnknownFields()
  18. err := decoder.Decode(skeleton)
  19. if err != nil {
  20. return nil, newError("failed to parse outbound config skeleton").Base(err)
  21. }
  22. return skeleton, nil
  23. }
  24. func (p *OutboundParser) toAbstractServerSpec(config *OutboundConfig) (*ServerConfiguration, error) {
  25. serverConfig := &ServerConfiguration{}
  26. serverConfig.Protocol = config.Protocol
  27. {
  28. protocolSettings, err := loadHeterogeneousConfigFromRawJSONRestricted("outbound", config.Protocol, config.Settings)
  29. if err != nil {
  30. return nil, newError("failed to parse protocol settings").Base(err)
  31. }
  32. serverConfig.ProtocolSettings = serial.ToTypedMessage(protocolSettings)
  33. }
  34. if config.StreamSetting != nil {
  35. if config.StreamSetting.Transport == "" {
  36. config.StreamSetting.Transport = "tcp"
  37. }
  38. if config.StreamSetting.Security == "" {
  39. config.StreamSetting.Security = "none"
  40. }
  41. {
  42. serverConfig.Transport = config.StreamSetting.Transport
  43. transportSettings, err := loadHeterogeneousConfigFromRawJSONRestricted(
  44. "transport", config.StreamSetting.Transport, config.StreamSetting.TransportSettings)
  45. if err != nil {
  46. return nil, newError("failed to parse transport settings").Base(err)
  47. }
  48. serverConfig.TransportSettings = serial.ToTypedMessage(transportSettings)
  49. }
  50. {
  51. securitySettings, err := loadHeterogeneousConfigFromRawJSONRestricted(
  52. "security", config.StreamSetting.Security, config.StreamSetting.SecuritySettings)
  53. if err != nil {
  54. return nil, newError("failed to parse security settings").Base(err)
  55. }
  56. serverConfig.SecuritySettings = serial.ToTypedMessage(securitySettings)
  57. serverConfig.Security = serial.V2Type(serverConfig.SecuritySettings)
  58. }
  59. }
  60. return serverConfig, nil
  61. }
  62. func (p *OutboundParser) ToSubscriptionServerConfig(config *OutboundConfig) (*SubscriptionServerConfig, error) {
  63. serverSpec, err := p.toAbstractServerSpec(config)
  64. if err != nil {
  65. return nil, newError("unable to parse server specification")
  66. }
  67. return &SubscriptionServerConfig{
  68. Configuration: serverSpec,
  69. Metadata: config.Metadata,
  70. }, nil
  71. }
  72. func loadHeterogeneousConfigFromRawJSONRestricted(interfaceType, name string, rawJSON json.RawMessage) (proto.Message, error) {
  73. ctx := context.TODO()
  74. ctx = registry.CreateRestrictedModeContext(ctx)
  75. if len(rawJSON) == 0 {
  76. rawJSON = []byte("{}")
  77. }
  78. return registry.LoadImplementationByAlias(ctx, interfaceType, name, []byte(rawJSON))
  79. }