dataurl.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dataurlsingle
  2. import (
  3. "bytes"
  4. "strings"
  5. "github.com/vincent-petithory/dataurl"
  6. "github.com/v2fly/v2ray-core/v5/app/subscription/containers"
  7. "github.com/v2fly/v2ray-core/v5/common"
  8. )
  9. //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
  10. func newSingularDataURLParser() containers.SubscriptionContainerDocumentParser {
  11. return &parser{}
  12. }
  13. type parser struct{}
  14. func (p parser) ParseSubscriptionContainerDocument(rawConfig []byte) (*containers.Container, error) {
  15. dataURL, err := dataurl.Decode(bytes.NewReader(rawConfig))
  16. if err != nil {
  17. return nil, newError("unable to decode dataURL").Base(err)
  18. }
  19. if dataURL.MediaType.Type != "application" {
  20. return nil, newError("unsupported media type: ", dataURL.MediaType.Type)
  21. }
  22. if !strings.HasPrefix(dataURL.MediaType.Subtype, "vnd.v2ray.subscription-singular") {
  23. return nil, newError("unsupported media subtype: ", dataURL.MediaType.Subtype)
  24. }
  25. result := &containers.Container{}
  26. result.Kind = "DataURLSingle"
  27. result.Metadata = make(map[string]string)
  28. result.ServerSpecs = append(result.ServerSpecs, containers.UnparsedServerConf{
  29. KindHint: "",
  30. Content: dataURL.Data,
  31. })
  32. return result, nil
  33. }
  34. func init() {
  35. common.Must(containers.RegisterParser("DataURLSingle", newSingularDataURLParser()))
  36. }