converter.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package nonnative
  2. import (
  3. "io/fs"
  4. "github.com/v2fly/v2ray-core/v5/app/subscription/entries"
  5. "github.com/v2fly/v2ray-core/v5/app/subscription/entries/nonnative/nonnativeifce"
  6. "github.com/v2fly/v2ray-core/v5/app/subscription/entries/outbound"
  7. "github.com/v2fly/v2ray-core/v5/app/subscription/specs"
  8. "github.com/v2fly/v2ray-core/v5/common"
  9. )
  10. type nonNativeConverter struct {
  11. matcher *DefMatcher
  12. }
  13. func (n *nonNativeConverter) ConvertToAbstractServerConfig(rawConfig []byte, kindHint string) (*specs.SubscriptionServerConfig, error) {
  14. nonNativeLink := ExtractAllValuesFromBytes(rawConfig)
  15. nonNativeLink.Values["_kind"] = kindHint
  16. result, err := n.matcher.ExecuteAll(nonNativeLink)
  17. if err != nil {
  18. return nil, newError("failed to find working converting template").Base(err)
  19. }
  20. outboundParser := outbound.NewOutboundEntriesParser()
  21. outboundEntries, err := outboundParser.ConvertToAbstractServerConfig(result, "")
  22. if err != nil {
  23. return nil, newError("failed to parse template output as outbound entries").Base(err)
  24. }
  25. return outboundEntries, nil
  26. }
  27. func NewNonNativeConverter(fs fs.FS) (entries.Converter, error) {
  28. matcher := NewDefMatcher()
  29. if fs == nil {
  30. err := matcher.LoadEmbeddedDefinitions()
  31. if err != nil {
  32. return nil, newError("failed to load embedded definitions").Base(err)
  33. }
  34. } else {
  35. err := matcher.LoadDefinitions(fs)
  36. if err != nil {
  37. return nil, newError("failed to load provided definitions").Base(err)
  38. }
  39. }
  40. return &nonNativeConverter{matcher: matcher}, nil
  41. }
  42. func init() {
  43. common.Must(entries.RegisterConverter("nonnative", common.Must2(NewNonNativeConverter(nil)).(entries.Converter)))
  44. nonnativeifce.NewNonNativeConverterConstructor = NewNonNativeConverter
  45. }