subscription.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package control
  2. import (
  3. "github.com/v2fly/v2ray-core/v4/common"
  4. "github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
  5. "github.com/v2fly/v2ray-core/v4/common/platform/securedload"
  6. "github.com/v2fly/v2ray-core/v4/common/templates"
  7. "os"
  8. "text/template"
  9. )
  10. type SubscriptionParseCommand struct {
  11. }
  12. func (s SubscriptionParseCommand) Name() string {
  13. return "subscriptionParse"
  14. }
  15. func (s SubscriptionParseCommand) Description() Description {
  16. return Description{
  17. Short: "a tool to parse all kind of subscription into V2Ray outbound",
  18. Usage: []string{
  19. "v2ctl subscriptionParse <subscription file>",
  20. },
  21. }
  22. }
  23. func (s SubscriptionParseCommand) Execute(args []string) error {
  24. templatedef, err := securedload.GetAssetSecured("subscriptions/subscriptionsDefinition.v2flyTemplate")
  25. if err != nil {
  26. return newError("Cannot load subscription template file").Base(err)
  27. }
  28. templatedata, errtempl := template.New("").Funcs(templates.AssistFunctions).Parse(string(templatedef))
  29. if errtempl != nil {
  30. return newError("Cannot parse subscription template file").Base(errtempl)
  31. }
  32. subscription, errsubscription := filesystem.ReadFile(args[0])
  33. if errsubscription != nil {
  34. return newError("cannot read subscriptions file")
  35. }
  36. dot := templates.NewUniversalDot(subscription)
  37. if errtemp := templatedata.Execute(os.Stdout, dot); errtemp != nil {
  38. return newError("Cannot load execute template file").Base(errtemp)
  39. }
  40. return nil
  41. }
  42. func init() {
  43. common.Must(RegisterCommand(SubscriptionParseCommand{}))
  44. }