parser.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package base64urlline
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/base64"
  6. "io"
  7. "github.com/v2fly/v2ray-core/v5/app/subscription/containers"
  8. "github.com/v2fly/v2ray-core/v5/common"
  9. )
  10. func newBase64URLLineParser() containers.SubscriptionContainerDocumentParser {
  11. return &parser{}
  12. }
  13. type parser struct{}
  14. func (p parser) ParseSubscriptionContainerDocument(rawConfig []byte) (*containers.Container, error) {
  15. result := &containers.Container{}
  16. result.Kind = "Base64URLLine"
  17. result.Metadata = make(map[string]string)
  18. bodyDecoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(rawConfig))
  19. decoded, err := io.ReadAll(bodyDecoder)
  20. if err != nil {
  21. return nil, newError("failed to decode base64url body base64").Base(err)
  22. }
  23. scanner := bufio.NewScanner(bytes.NewReader(decoded))
  24. const maxCapacity int = 1024 * 256
  25. buf := make([]byte, maxCapacity)
  26. scanner.Buffer(buf, maxCapacity)
  27. for scanner.Scan() {
  28. result.ServerSpecs = append(result.ServerSpecs, containers.UnparsedServerConf{
  29. KindHint: "URL",
  30. Content: scanner.Bytes(),
  31. })
  32. }
  33. return result, nil
  34. }
  35. func init() {
  36. common.Must(containers.RegisterParser("Base64URLLine", newBase64URLLineParser()))
  37. }