1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 08:16:00 -04:00
v2fly/app/subscription/containers/dataurlsingle/dataurl.go
2023-11-26 10:55:27 +00:00

45 lines
1.3 KiB
Go

package dataurlsingle
import (
"bytes"
"strings"
"github.com/vincent-petithory/dataurl"
"github.com/v2fly/v2ray-core/v5/app/subscription/containers"
"github.com/v2fly/v2ray-core/v5/common"
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
func newSingularDataURLParser() containers.SubscriptionContainerDocumentParser {
return &parser{}
}
type parser struct{}
func (p parser) ParseSubscriptionContainerDocument(rawConfig []byte) (*containers.Container, error) {
dataURL, err := dataurl.Decode(bytes.NewReader(rawConfig))
if err != nil {
return nil, newError("unable to decode dataURL").Base(err)
}
if dataURL.MediaType.Type != "application" {
return nil, newError("unsupported media type: ", dataURL.MediaType.Type)
}
if !strings.HasPrefix(dataURL.MediaType.Subtype, "vnd.v2ray.subscription-singular") {
return nil, newError("unsupported media subtype: ", dataURL.MediaType.Subtype)
}
result := &containers.Container{}
result.Kind = "DataURLSingle"
result.Metadata = make(map[string]string)
result.ServerSpecs = append(result.ServerSpecs, containers.UnparsedServerConf{
KindHint: "",
Content: dataURL.Data,
})
return result, nil
}
func init() {
common.Must(containers.RegisterParser("DataURLSingle", newSingularDataURLParser()))
}