dataurl.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package dataurlfetcher
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/vincent-petithory/dataurl"
  6. "github.com/v2fly/v2ray-core/v5/app/subscription"
  7. "github.com/v2fly/v2ray-core/v5/app/subscription/documentfetcher"
  8. "github.com/v2fly/v2ray-core/v5/common"
  9. )
  10. //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
  11. func newDataURLFetcher() *dataURLFetcher {
  12. return &dataURLFetcher{}
  13. }
  14. func init() {
  15. common.Must(documentfetcher.RegisterFetcher("dataurl", newDataURLFetcher()))
  16. }
  17. type dataURLFetcher struct{}
  18. func (d *dataURLFetcher) DownloadDocument(ctx context.Context, source *subscription.ImportSource, opts ...documentfetcher.FetcherOptions) ([]byte, error) {
  19. dataURL, err := dataurl.DecodeString(source.Url)
  20. if err != nil {
  21. return nil, newError("unable to decode dataURL").Base(err)
  22. }
  23. if dataURL.MediaType.Type != "application" {
  24. return nil, newError("unsupported media type: ", dataURL.MediaType.Type)
  25. }
  26. if !strings.HasPrefix(dataURL.MediaType.Subtype, "vnd.v2ray.subscription") {
  27. return nil, newError("unsupported media subtype: ", dataURL.MediaType.Subtype)
  28. }
  29. return []byte(source.Url), nil
  30. }