Commit 3efccce6ec
Unregistered key
Layout: unified · split
internal/providers/anipub/streams.go +8
| @@ -3,6 +3,7 @@ package anipub | |||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | 5 | ||
| 6 | "github.com/wraient/curd/internal/curdhost" | ||
| 6 | "github.com/wraient/curd/internal/providers" | 7 | "github.com/wraient/curd/internal/providers" |
| 7 | "github.com/wraient/curd/internal/providers/substyle" | 8 | "github.com/wraient/curd/internal/providers/substyle" |
| 8 | ) | 9 | ) |
| @@ -37,6 +38,13 @@ func getEpisodeStreamsForMode(showID string, config providers.PlaybackConfig, ep | |||
| 37 | return nil, nil, err | 38 | return nil, nil, err |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 41 | // Reject ad-injected decoy playlists served by the megaplay CDN so the | ||
| 42 | // caller can fall back to another provider instead of opening an idle mpv. | ||
| 43 | if err := validateResolvedStream(streamURL); err != nil { | ||
| 44 | curdhost.Log(fmt.Sprintf("anipub stream %q rejected: %v", streamURL, err)) | ||
| 45 | return nil, nil, err | ||
| 46 | } | ||
| 47 | |||
| 40 | hints := map[string]providers.StreamPlaybackHint{ | 48 | hints := map[string]providers.StreamPlaybackHint{ |
| 41 | streamURL: { | 49 | streamURL: { |
| 42 | Referrer: megaplayBaseURL + "/", | 50 | Referrer: megaplayBaseURL + "/", |
internal/providers/anipub/validate.go added +270
| @@ -0,0 +1,270 @@ | |||
| 1 | package anipub | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "io" | ||
| 6 | "net/http" | ||
| 7 | "net/url" | ||
| 8 | "strconv" | ||
| 9 | "strings" | ||
| 10 | |||
| 11 | "github.com/wraient/curd/internal/curdhost" | ||
| 12 | ) | ||
| 13 | |||
| 14 | // decoySegmentMarkers identify ad/decoy segments that the megap.kotocdn.site | ||
| 15 | // CDN injects into resolved HLS playlists. These segments are 1x1 PNGs (or | ||
| 16 | // 302 redirects to them) served from ByteDance ad infrastructure; mpv can | ||
| 17 | // never decode them, so playback never starts and the player sits on its idle | ||
| 18 | // "Drop files or URLs to play here." screen. | ||
| 19 | var decoySegmentMarkers = []string{ | ||
| 20 | "ibyteimg.com", | ||
| 21 | "byteimg.com", | ||
| 22 | "ad-site-i18n", | ||
| 23 | } | ||
| 24 | |||
| 25 | // maxPlaylistBytes bounds how much of a manifest we download during validation. | ||
| 26 | const maxPlaylistBytes = 2 << 20 | ||
| 27 | |||
| 28 | // isMegaplayCDNHost reports whether the stream is hosted by the megaplay CDN, | ||
| 29 | // which is the only CDN this validation is scoped to. | ||
| 30 | func isMegaplayCDNHost(host string) bool { | ||
| 31 | host = strings.ToLower(strings.TrimSpace(host)) | ||
| 32 | return host == "kotocdn.site" || host == "megap.kotocdn.site" || strings.HasSuffix(host, ".kotocdn.site") | ||
| 33 | } | ||
| 34 | |||
| 35 | // validateResolvedStream inspects a resolved anipub stream URL before it is | ||
| 36 | // handed to the media player. When the megaplay CDN is serving an ad-injected | ||
| 37 | // decoy playlist (or a fully decoy one), an error is returned so the caller | ||
| 38 | // can fall back to another provider instead of opening an idle mpv window. | ||
| 39 | func validateResolvedStream(rawURL string) error { | ||
| 40 | streamURL, err := url.Parse(strings.TrimSpace(rawURL)) | ||
| 41 | if err != nil || streamURL.Scheme == "" || streamURL.Host == "" { | ||
| 42 | return fmt.Errorf("invalid anipub stream url %q", rawURL) | ||
| 43 | } | ||
| 44 | if !isMegaplayCDNHost(streamURL.Host) { | ||
| 45 | return nil | ||
| 46 | } | ||
| 47 | |||
| 48 | v := &hlsStreamValidator{ | ||
| 49 | client: curdhost.HTTPClient(), | ||
| 50 | referrer: megaplayBaseURL + "/", | ||
| 51 | } | ||
| 52 | |||
| 53 | masterBody, err := v.fetch(streamURL.String()) | ||
| 54 | if err != nil { | ||
| 55 | return fmt.Errorf("anipub stream manifest fetch failed: %w", err) | ||
| 56 | } | ||
| 57 | if !strings.Contains(masterBody, "#EXTM3U") { | ||
| 58 | return fmt.Errorf("anipub stream manifest %q is not an HLS playlist", rawURL) | ||
| 59 | } | ||
| 60 | |||
| 61 | mediaURL, err := selectMediaPlaylistURL(streamURL, masterBody) | ||
| 62 | if err != nil { | ||
| 63 | return err | ||
| 64 | } | ||
| 65 | mediaBody, err := v.fetch(mediaURL) | ||
| 66 | if err != nil { | ||
| 67 | return fmt.Errorf("anipub stream media playlist fetch failed: %w", err) | ||
| 68 | } | ||
| 69 | |||
| 70 | segments := parsePlaylistSegments(mediaBody) | ||
| 71 | if len(segments) == 0 { | ||
| 72 | return fmt.Errorf("anipub stream %q has no media segments", rawURL) | ||
| 73 | } | ||
| 74 | |||
| 75 | decoyCount := 0 | ||
| 76 | for _, segment := range segments { | ||
| 77 | if isDecoySegmentURI(segment) { | ||
| 78 | decoyCount++ | ||
| 79 | } | ||
| 80 | } | ||
| 81 | if decoyCount == len(segments) { | ||
| 82 | return fmt.Errorf("anipub stream %q is an ad-injected decoy: all %d segments are ad/decoy content", rawURL, len(segments)) | ||
| 83 | } | ||
| 84 | if decoyRatio := float64(decoyCount) / float64(len(segments)); decoyRatio >= 0.5 { | ||
| 85 | return fmt.Errorf("anipub stream %q is an ad-injected decoy: %d/%d segments are ad/decoy content", rawURL, decoyCount, len(segments)) | ||
| 86 | } | ||
| 87 | |||
| 88 | // The first non-decoy segment may still redirect to ad/decoy content, so | ||
| 89 | // probe its magic bytes before trusting the playlist. | ||
| 90 | for _, segment := range segments { | ||
| 91 | if isDecoySegmentURI(segment) { | ||
| 92 | continue | ||
| 93 | } | ||
| 94 | if data := v.fetchRange(segment, 0, 15); looksLikeDecoySegment(data) { | ||
| 95 | return fmt.Errorf("anipub stream %q first media segment is not video content", rawURL) | ||
| 96 | } | ||
| 97 | break | ||
| 98 | } | ||
| 99 | |||
| 100 | return nil | ||
| 101 | } | ||
| 102 | |||
| 103 | // hlsStreamValidator fetches manifests and probe bytes for a stream. | ||
| 104 | type hlsStreamValidator struct { | ||
| 105 | client *http.Client | ||
| 106 | referrer string | ||
| 107 | } | ||
| 108 | |||
| 109 | func (v *hlsStreamValidator) fetch(rawURL string) (string, error) { | ||
| 110 | if v.client == nil { | ||
| 111 | return "", fmt.Errorf("http client not configured") | ||
| 112 | } | ||
| 113 | req, err := http.NewRequest(http.MethodGet, rawURL, nil) | ||
| 114 | if err != nil { | ||
| 115 | return "", err | ||
| 116 | } | ||
| 117 | req.Header.Set("User-Agent", userAgent) | ||
| 118 | req.Header.Set("Referer", v.referrer) | ||
| 119 | req.Header.Set("Accept", "application/vnd.apple.mpegurl, application/x-mpegURL, */*") | ||
| 120 | |||
| 121 | resp, err := v.client.Do(req) | ||
| 122 | if err != nil { | ||
| 123 | return "", err | ||
| 124 | } | ||
| 125 | defer resp.Body.Close() | ||
| 126 | |||
| 127 | if !curdhost.HTTPStatusOK(resp.StatusCode) { | ||
| 128 | body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) | ||
| 129 | return "", curdhost.HTTPStatusError("megaplay hls manifest", resp.StatusCode, body) | ||
| 130 | } | ||
| 131 | body, err := io.ReadAll(io.LimitReader(resp.Body, maxPlaylistBytes)) | ||
| 132 | if err != nil { | ||
| 133 | return "", err | ||
| 134 | } | ||
| 135 | return string(body), nil | ||
| 136 | } | ||
| 137 | |||
| 138 | // fetchRange requests the first few bytes of a segment so its magic bytes can | ||
| 139 | // be inspected. Errors are swallowed: an unavailable probe must not reject a | ||
| 140 | // stream that mpv could still play. | ||
| 141 | func (v *hlsStreamValidator) fetchRange(rawURL string, start, end int) []byte { | ||
| 142 | if v.client == nil { | ||
| 143 | return nil | ||
| 144 | } | ||
| 145 | req, err := http.NewRequest(http.MethodGet, rawURL, nil) | ||
| 146 | if err != nil { | ||
| 147 | return nil | ||
| 148 | } | ||
| 149 | req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end)) | ||
| 150 | req.Header.Set("User-Agent", userAgent) | ||
| 151 | req.Header.Set("Referer", v.referrer) | ||
| 152 | |||
| 153 | resp, err := v.client.Do(req) | ||
| 154 | if err != nil { | ||
| 155 | return nil | ||
| 156 | } | ||
| 157 | defer resp.Body.Close() | ||
| 158 | if !curdhost.HTTPStatusOK(resp.StatusCode) { | ||
| 159 | return nil | ||
| 160 | } | ||
| 161 | |||
| 162 | buf := make([]byte, end-start+1) | ||
| 163 | n, _ := io.ReadFull(resp.Body, buf) | ||
| 164 | return buf[:n] | ||
| 165 | } | ||
| 166 | |||
| 167 | // selectMediaPlaylistURL picks the highest-bandwidth variant from a master | ||
| 168 | // playlist, or returns the master URL itself when it is a media playlist. | ||
| 169 | func selectMediaPlaylistURL(masterURL *url.URL, body string) (string, error) { | ||
| 170 | if !strings.Contains(body, "#EXT-X-STREAM-INF") { | ||
| 171 | return masterURL.String(), nil | ||
| 172 | } | ||
| 173 | |||
| 174 | bestBandwidth := -1 | ||
| 175 | bestURI := "" | ||
| 176 | lines := strings.Split(body, "\n") | ||
| 177 | for i := 0; i < len(lines); i++ { | ||
| 178 | line := strings.TrimSpace(lines[i]) | ||
| 179 | if !strings.HasPrefix(line, "#EXT-X-STREAM-INF") { | ||
| 180 | continue | ||
| 181 | } | ||
| 182 | bandwidth := parseBandwidth(line) | ||
| 183 | if bandwidth <= bestBandwidth { | ||
| 184 | continue | ||
| 185 | } | ||
| 186 | for j := i + 1; j < len(lines); j++ { | ||
| 187 | next := strings.TrimSpace(lines[j]) | ||
| 188 | if next == "" || strings.HasPrefix(next, "#") { | ||
| 189 | continue | ||
| 190 | } | ||
| 191 | bestBandwidth = bandwidth | ||
| 192 | bestURI = next | ||
| 193 | break | ||
| 194 | } | ||
| 195 | } | ||
| 196 | if bestURI == "" { | ||
| 197 | return "", fmt.Errorf("no variant playlist found in master playlist") | ||
| 198 | } | ||
| 199 | ref, err := url.Parse(bestURI) | ||
| 200 | if err != nil { | ||
| 201 | return "", fmt.Errorf("invalid variant playlist uri %q: %w", bestURI, err) | ||
| 202 | } | ||
| 203 | resolved := masterURL.ResolveReference(ref) | ||
| 204 | return resolved.String(), nil | ||
| 205 | } | ||
| 206 | |||
| 207 | func parseBandwidth(infLine string) int { | ||
| 208 | upper := strings.ToUpper(infLine) | ||
| 209 | idx := strings.Index(upper, "BANDWIDTH=") | ||
| 210 | if idx < 0 { | ||
| 211 | return 0 | ||
| 212 | } | ||
| 213 | rest := upper[idx+len("BANDWIDTH="):] | ||
| 214 | if comma := strings.IndexByte(rest, ','); comma >= 0 { | ||
| 215 | rest = rest[:comma] | ||
| 216 | } | ||
| 217 | value, err := strconv.Atoi(strings.TrimSpace(rest)) | ||
| 218 | if err != nil { | ||
| 219 | return 0 | ||
| 220 | } | ||
| 221 | return value | ||
| 222 | } | ||
| 223 | |||
| 224 | // parsePlaylistSegments extracts media segment URIs from a playlist body. | ||
| 225 | func parsePlaylistSegments(body string) []string { | ||
| 226 | var segments []string | ||
| 227 | for _, raw := range strings.Split(body, "\n") { | ||
| 228 | line := strings.TrimSpace(raw) | ||
| 229 | if line == "" || strings.HasPrefix(line, "#") { | ||
| 230 | continue | ||
| 231 | } | ||
| 232 | segments = append(segments, line) | ||
| 233 | } | ||
| 234 | return segments | ||
| 235 | } | ||
| 236 | |||
| 237 | func isDecoySegmentURI(raw string) bool { | ||
| 238 | lower := strings.ToLower(raw) | ||
| 239 | for _, marker := range decoySegmentMarkers { | ||
| 240 | if strings.Contains(lower, marker) { | ||
| 241 | return true | ||
| 242 | } | ||
| 243 | } | ||
| 244 | return false | ||
| 245 | } | ||
| 246 | |||
| 247 | // looksLikeDecoySegment reports whether probe bytes look like an image, an | ||
| 248 | // HTML error page, or other non-video content instead of an HLS media segment. | ||
| 249 | func looksLikeDecoySegment(data []byte) bool { | ||
| 250 | if len(data) < 4 { | ||
| 251 | return false | ||
| 252 | } | ||
| 253 | if data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G' { | ||
| 254 | return true | ||
| 255 | } | ||
| 256 | if data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF { | ||
| 257 | return true | ||
| 258 | } | ||
| 259 | if string(data[:4]) == "GIF8" { | ||
| 260 | return true | ||
| 261 | } | ||
| 262 | if len(data) >= 12 && string(data[:4]) == "RIFF" && string(data[8:12]) == "WEBP" { | ||
| 263 | return true | ||
| 264 | } | ||
| 265 | lower := strings.ToLower(string(data)) | ||
| 266 | if strings.HasPrefix(lower, "<") || strings.Contains(lower, "<html") || strings.Contains(lower, "<!doctype") { | ||
| 267 | return true | ||
| 268 | } | ||
| 269 | return false | ||
| 270 | } | ||
internal/providers/anipub/validate_test.go added +242
| @@ -0,0 +1,242 @@ | |||
| 1 | package anipub | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net/http" | ||
| 5 | "net/url" | ||
| 6 | "strings" | ||
| 7 | "testing" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func TestSelectMediaPlaylistURL(t *testing.T) { | ||
| 11 | master := func() *url.URL { u, _ := url.Parse("https://megap.kotocdn.site/abc/master.m3u8"); return u } | ||
| 12 | |||
| 13 | t.Run("media playlist with no variants returns master", func(t *testing.T) { | ||
| 14 | got, err := selectMediaPlaylistURL(master(), "#EXTM3U\n#EXTINF:5,\nhttps://seg/1.ts\n") | ||
| 15 | if err != nil { | ||
| 16 | t.Fatalf("unexpected err: %v", err) | ||
| 17 | } | ||
| 18 | if got != "https://megap.kotocdn.site/abc/master.m3u8" { | ||
| 19 | t.Fatalf("unexpected %q", got) | ||
| 20 | } | ||
| 21 | }) | ||
| 22 | |||
| 23 | t.Run("picks highest bandwidth relative variant", func(t *testing.T) { | ||
| 24 | body := "#EXTM3U\n" + | ||
| 25 | "#EXT-X-STREAM-INF:BANDWIDTH=500000\nlow.m3u8\n" + | ||
| 26 | "#EXT-X-STREAM-INF:BANDWIDTH=2000000\nhigh.m3u8\n" | ||
| 27 | got, err := selectMediaPlaylistURL(master(), body) | ||
| 28 | if err != nil { | ||
| 29 | t.Fatalf("unexpected err: %v", err) | ||
| 30 | } | ||
| 31 | if got != "https://megap.kotocdn.site/abc/high.m3u8" { | ||
| 32 | t.Fatalf("unexpected %q", got) | ||
| 33 | } | ||
| 34 | }) | ||
| 35 | |||
| 36 | t.Run("picks highest bandwidth absolute variant", func(t *testing.T) { | ||
| 37 | body := "#EXTM3U\n" + | ||
| 38 | "#EXT-X-STREAM-INF:BANDWIDTH=2000000\nhttps://cdn.example/high.m3u8\n" + | ||
| 39 | "#EXT-X-STREAM-INF:BANDWIDTH=500000\nlow.m3u8\n" | ||
| 40 | got, err := selectMediaPlaylistURL(master(), body) | ||
| 41 | if err != nil { | ||
| 42 | t.Fatalf("unexpected err: %v", err) | ||
| 43 | } | ||
| 44 | if got != "https://cdn.example/high.m3u8" { | ||
| 45 | t.Fatalf("unexpected %q", got) | ||
| 46 | } | ||
| 47 | }) | ||
| 48 | |||
| 49 | t.Run("no variant found errors", func(t *testing.T) { | ||
| 50 | body := "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=500000\n" | ||
| 51 | if _, err := selectMediaPlaylistURL(master(), body); err == nil { | ||
| 52 | t.Fatal("expected error") | ||
| 53 | } | ||
| 54 | }) | ||
| 55 | } | ||
| 56 | |||
| 57 | func TestParsePlaylistSegmentsAndDecoyURIs(t *testing.T) { | ||
| 58 | body := "#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/seg/1.ts\n#EXTINF:5,\n" + | ||
| 59 | "https://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/decoy\n" | ||
| 60 | segments := parsePlaylistSegments(body) | ||
| 61 | if len(segments) != 2 { | ||
| 62 | t.Fatalf("expected 2 segments, got %d", len(segments)) | ||
| 63 | } | ||
| 64 | if isDecoySegmentURI(segments[0]) { | ||
| 65 | t.Fatalf("kotocdn segment misclassified as decoy") | ||
| 66 | } | ||
| 67 | if !isDecoySegmentURI(segments[1]) { | ||
| 68 | t.Fatalf("ibyteimg segment not classified as decoy") | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | func TestLooksLikeDecoySegment(t *testing.T) { | ||
| 73 | cases := []struct { | ||
| 74 | name string | ||
| 75 | data []byte | ||
| 76 | want bool | ||
| 77 | }{ | ||
| 78 | {"png", []byte{0x89, 'P', 'N', 'G', 0x0D, 0x0A}, true}, | ||
| 79 | {"jpeg", []byte{0xFF, 0xD8, 0xFF, 0xE0}, true}, | ||
| 80 | {"gif", []byte("GIF89a..."), true}, | ||
| 81 | {"webp", []byte("RIFFxxxxWEBP"), true}, | ||
| 82 | {"html", []byte("<html><body>error</body></html>"), true}, | ||
| 83 | {"doctype", []byte("<!DOCTYPE html>"), true}, | ||
| 84 | {"mpeg ts", []byte{0x47, 0x47, 0x00, 0x1B}, false}, | ||
| 85 | {"fmp4 ftyp", []byte{0x00, 0x00, 0x00, 0x18, 'f', 't', 'y', 'p'}, false}, | ||
| 86 | {"short bytes", []byte{0x47, 0x47}, false}, | ||
| 87 | } | ||
| 88 | for _, tc := range cases { | ||
| 89 | t.Run(tc.name, func(t *testing.T) { | ||
| 90 | if got := looksLikeDecoySegment(tc.data); got != tc.want { | ||
| 91 | t.Fatalf("looksLikeDecoySegment(%v) = %v, want %v", tc.data, got, tc.want) | ||
| 92 | } | ||
| 93 | }) | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | func TestValidateResolvedStream(t *testing.T) { | ||
| 98 | const masterURL = "https://megap.kotocdn.site/abc/master.m3u8" | ||
| 99 | |||
| 100 | newClient := func(handler func(*http.Request) *http.Response) *http.Client { | ||
| 101 | return &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| 102 | if resp := handler(req); resp != nil { | ||
| 103 | resp.Request = req | ||
| 104 | return resp, nil | ||
| 105 | } | ||
| 106 | return textResponse(req, http.StatusNotFound, ""), nil | ||
| 107 | })} | ||
| 108 | } | ||
| 109 | |||
| 110 | text := func(body string) string { return body } | ||
| 111 | |||
| 112 | t.Run("non kotocdn host is skipped", func(t *testing.T) { | ||
| 113 | client := newClient(func(req *http.Request) *http.Response { | ||
| 114 | t.Fatalf("no requests expected, got %s", req.URL) | ||
| 115 | return nil | ||
| 116 | }) | ||
| 117 | withAnipubTestClient(t, client) | ||
| 118 | if err := validateResolvedStream("https://cdn.example/master.m3u8"); err != nil { | ||
| 119 | t.Fatalf("expected no error, got %v", err) | ||
| 120 | } | ||
| 121 | }) | ||
| 122 | |||
| 123 | t.Run("clean stream passes", func(t *testing.T) { | ||
| 124 | client := newClient(func(req *http.Request) *http.Response { | ||
| 125 | switch { | ||
| 126 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 127 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 128 | case strings.HasSuffix(req.URL.Path, "/index.m3u8"): | ||
| 129 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/2.ts\n")) | ||
| 130 | default: | ||
| 131 | return textResponse(req, http.StatusOK, string([]byte{0x47, 0x47, 0x00, 0x1B})) | ||
| 132 | } | ||
| 133 | }) | ||
| 134 | withAnipubTestClient(t, client) | ||
| 135 | if err := validateResolvedStream(masterURL); err != nil { | ||
| 136 | t.Fatalf("expected clean stream to pass, got %v", err) | ||
| 137 | } | ||
| 138 | }) | ||
| 139 | |||
| 140 | t.Run("fully decoy stream is rejected", func(t *testing.T) { | ||
| 141 | client := newClient(func(req *http.Request) *http.Response { | ||
| 142 | switch { | ||
| 143 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 144 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 145 | default: | ||
| 146 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/x\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/y\n")) | ||
| 147 | } | ||
| 148 | }) | ||
| 149 | withAnipubTestClient(t, client) | ||
| 150 | if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "decoy") { | ||
| 151 | t.Fatalf("expected decoy error, got %v", err) | ||
| 152 | } | ||
| 153 | }) | ||
| 154 | |||
| 155 | t.Run("majority decoy stream is rejected", func(t *testing.T) { | ||
| 156 | client := newClient(func(req *http.Request) *http.Response { | ||
| 157 | switch { | ||
| 158 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 159 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 160 | default: | ||
| 161 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/a\n#EXTINF:5,\nhttps://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/b\n")) | ||
| 162 | } | ||
| 163 | }) | ||
| 164 | withAnipubTestClient(t, client) | ||
| 165 | if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "decoy") { | ||
| 166 | t.Fatalf("expected decoy error, got %v", err) | ||
| 167 | } | ||
| 168 | }) | ||
| 169 | |||
| 170 | t.Run("real segment redirecting to decoy is rejected", func(t *testing.T) { | ||
| 171 | client := newClient(func(req *http.Request) *http.Response { | ||
| 172 | switch { | ||
| 173 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 174 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 175 | case strings.HasSuffix(req.URL.Path, "/index.m3u8"): | ||
| 176 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n")) | ||
| 177 | case strings.Contains(req.URL.Host, "ibyteimg.com"): | ||
| 178 | return textResponse(req, http.StatusOK, string([]byte{0x89, 'P', 'N', 'G', 0x0D, 0x0A})) | ||
| 179 | default: | ||
| 180 | resp := textResponse(req, http.StatusFound, "") | ||
| 181 | resp.Header.Set("Location", "https://p16-ad-sg.ibyteimg.com/obj/ad-site-i18n/decoy") | ||
| 182 | return resp | ||
| 183 | } | ||
| 184 | }) | ||
| 185 | withAnipubTestClient(t, client) | ||
| 186 | if err := validateResolvedStream(masterURL); err == nil || !strings.Contains(err.Error(), "not video content") { | ||
| 187 | t.Fatalf("expected probe rejection, got %v", err) | ||
| 188 | } | ||
| 189 | }) | ||
| 190 | |||
| 191 | t.Run("segment probe failure is tolerated", func(t *testing.T) { | ||
| 192 | client := newClient(func(req *http.Request) *http.Response { | ||
| 193 | switch { | ||
| 194 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 195 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 196 | case strings.HasSuffix(req.URL.Path, "/index.m3u8"): | ||
| 197 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXTINF:5,\nhttps://megap.kotocdn.site/abc/seg/1.ts\n")) | ||
| 198 | default: | ||
| 199 | return textResponse(req, http.StatusInternalServerError, "") | ||
| 200 | } | ||
| 201 | }) | ||
| 202 | withAnipubTestClient(t, client) | ||
| 203 | if err := validateResolvedStream(masterURL); err != nil { | ||
| 204 | t.Fatalf("expected probe failure to be tolerated, got %v", err) | ||
| 205 | } | ||
| 206 | }) | ||
| 207 | |||
| 208 | t.Run("non 200 manifest is rejected", func(t *testing.T) { | ||
| 209 | client := newClient(func(req *http.Request) *http.Response { | ||
| 210 | return textResponse(req, http.StatusForbidden, "") | ||
| 211 | }) | ||
| 212 | withAnipubTestClient(t, client) | ||
| 213 | if err := validateResolvedStream(masterURL); err == nil { | ||
| 214 | t.Fatal("expected manifest error") | ||
| 215 | } | ||
| 216 | }) | ||
| 217 | |||
| 218 | t.Run("non HLS manifest is rejected", func(t *testing.T) { | ||
| 219 | client := newClient(func(req *http.Request) *http.Response { | ||
| 220 | return textResponse(req, http.StatusOK, text("<html>challenge</html>")) | ||
| 221 | }) | ||
| 222 | withAnipubTestClient(t, client) | ||
| 223 | if err := validateResolvedStream(masterURL); err == nil { | ||
| 224 | t.Fatal("expected non-hls error") | ||
| 225 | } | ||
| 226 | }) | ||
| 227 | |||
| 228 | t.Run("empty media playlist is rejected", func(t *testing.T) { | ||
| 229 | client := newClient(func(req *http.Request) *http.Response { | ||
| 230 | switch { | ||
| 231 | case strings.HasSuffix(req.URL.Path, "/master.m3u8"): | ||
| 232 | return textResponse(req, http.StatusOK, text("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000\nindex.m3u8\n")) | ||
| 233 | default: | ||
| 234 | return textResponse(req, http.StatusOK, text("#EXTM3U\n")) | ||
| 235 | } | ||
| 236 | }) | ||
| 237 | withAnipubTestClient(t, client) | ||
| 238 | if err := validateResolvedStream(masterURL); err == nil { | ||
| 239 | t.Fatal("expected empty playlist error") | ||
| 240 | } | ||
| 241 | }) | ||
| 242 | } | ||