github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/subscription/containers/dataurlsingle/dataurl.go (about)

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