Commit 8182824ccd
Unsigned
Layout: unified · split
internal/episode_url.go +102 −69
| @@ -18,22 +18,30 @@ import ( | ||
| 18 | 18 | "strconv" |
| 19 | 19 | "strings" |
| 20 | 20 | "time" |
| 21 | "unicode" | |
| 22 | 21 | ) |
| 23 | 22 | |
| 23 | type allanimeSource struct { | |
| 24 | SourceUrl string `json:"sourceUrl"` | |
| 25 | SourceName string `json:"sourceName"` | |
| 26 | Priority float64 `json:"priority"` | |
| 27 | } | |
| 28 | ||
| 24 | 29 | type allanimeResponse struct { |
| 25 | 30 | Data struct { |
| 26 | 31 | M string `json:"_m"` |
| 27 | 32 | Tobeparsed string `json:"tobeparsed"` |
| 28 | 33 | Episode struct { |
| 29 | SourceUrls []struct { | |
| 30 | SourceUrl string `json:"sourceUrl"` | |
| 31 | SourceName string `json:"sourceName"` | |
| 32 | } `json:"sourceUrls"` | |
| 34 | SourceUrls []allanimeSource `json:"sourceUrls"` | |
| 33 | 35 | } `json:"episode"` |
| 34 | 36 | } `json:"data"` |
| 35 | 37 | } |
| 36 | 38 | |
| 39 | type allanimeTobeparsedPayload struct { | |
| 40 | Episode struct { | |
| 41 | SourceUrls []allanimeSource `json:"sourceUrls"` | |
| 42 | } `json:"episode"` | |
| 43 | } | |
| 44 | ||
| 37 | 45 | type result struct { |
| 38 | 46 | index int |
| 39 | 47 | links []string |
| @@ -46,27 +54,24 @@ type filemoonResponse struct { | ||
| 46 | 54 | KeyParts []string `json:"key_parts"` |
| 47 | 55 | } |
| 48 | 56 | |
| 49 | func decodeTobeparsed(blob string) string { | |
| 57 | func decodeTobeparsed(blob string) ([]allanimeSource, error) { | |
| 50 | 58 | key := []byte("Xot36i3lK3:v1") |
| 51 | 59 | hash := sha256.Sum256(key) |
| 52 | 60 | |
| 53 | 61 | data, err := base64.StdEncoding.DecodeString(blob) |
| 54 | 62 | if err != nil { |
| 55 | Log(fmt.Sprint("Error decoding base64:", err)) | |
| 56 | return "" | |
| 63 | return nil, fmt.Errorf("error decoding base64: %w", err) | |
| 57 | 64 | } |
| 58 | 65 | |
| 59 | 66 | if len(data) < 29 { |
| 60 | Log("Data too short to contain tobeparsed payload") | |
| 61 | return "" | |
| 67 | return nil, fmt.Errorf("data too short to contain tobeparsed payload") | |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | // The payload format is: 1-byte header, 12-byte IV, ciphertext, 16-byte trailer. |
| 65 | 71 | iv := data[1:13] |
| 66 | 72 | ctLen := len(data) - 13 - 16 |
| 67 | 73 | if ctLen <= 0 { |
| 68 | Log("Ciphertext length is invalid in tobeparsed payload") | |
| 69 | return "" | |
| 74 | return nil, fmt.Errorf("ciphertext length is invalid in tobeparsed payload") | |
| 70 | 75 | } |
| 71 | 76 | ct := data[13 : 13+ctLen] |
| 72 | 77 | |
| @@ -76,32 +81,23 @@ func decodeTobeparsed(blob string) string { | ||
| 76 | 81 | |
| 77 | 82 | block, err := aes.NewCipher(hash[:]) |
| 78 | 83 | if err != nil { |
| 79 | Log(fmt.Sprint("Error creating cipher:", err)) | |
| 80 | return "" | |
| 84 | return nil, fmt.Errorf("error creating cipher: %w", err) | |
| 81 | 85 | } |
| 82 | 86 | |
| 83 | 87 | stream := cipher.NewCTR(block, ctrIV) |
| 84 | 88 | plain := make([]byte, len(ct)) |
| 85 | 89 | stream.XORKeyStream(plain, ct) |
| 86 | 90 | |
| 87 | result := string(plain) | |
| 88 | result = strings.ReplaceAll(result, "{", "\n") | |
| 89 | result = strings.ReplaceAll(result, "}", "\n") | |
| 90 | ||
| 91 | re := regexp.MustCompile(`"sourceUrl":"--([^"]+)".*"sourceName":"([^"]+)"`) | |
| 92 | matches := re.FindAllStringSubmatch(result, -1) | |
| 91 | var payload allanimeTobeparsedPayload | |
| 92 | if err := json.Unmarshal(plain, &payload); err != nil { | |
| 93 | return nil, fmt.Errorf("failed to parse decrypted tobeparsed payload: %w", err) | |
| 94 | } | |
| 93 | 95 | |
| 94 | var sb strings.Builder | |
| 95 | for _, match := range matches { | |
| 96 | if len(match) == 3 { | |
| 97 | sb.WriteString(match[2]) | |
| 98 | sb.WriteString(" :") | |
| 99 | sb.WriteString(match[1]) | |
| 100 | sb.WriteString("\n") | |
| 101 | } | |
| 96 | if len(payload.Episode.SourceUrls) == 0 { | |
| 97 | return nil, fmt.Errorf("no source URLs found in decrypted tobeparsed payload") | |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | return sb.String() | |
| 100 | return payload.Episode.SourceUrls, nil | |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | 103 | func decodeProviderID(encoded string) string { |
| @@ -147,6 +143,43 @@ func decodeProviderID(encoded string) string { | ||
| 147 | 143 | return result |
| 148 | 144 | } |
| 149 | 145 | |
| 146 | func isDirectPlayableAllanimeSource(source allanimeSource) bool { | |
| 147 | sourceURL := strings.TrimSpace(source.SourceUrl) | |
| 148 | if sourceURL == "" { | |
| 149 | return false | |
| 150 | } | |
| 151 | ||
| 152 | if strings.EqualFold(source.SourceName, "Yt-mp4") || strings.EqualFold(source.SourceName, "S-mp4") { | |
| 153 | return true | |
| 154 | } | |
| 155 | ||
| 156 | lowerURL := strings.ToLower(sourceURL) | |
| 157 | if strings.Contains(lowerURL, "tools.fast4speed.rsvp/") { | |
| 158 | return true | |
| 159 | } | |
| 160 | ||
| 161 | parsedURL, err := url.Parse(sourceURL) | |
| 162 | if err != nil { | |
| 163 | return false | |
| 164 | } | |
| 165 | ||
| 166 | path := strings.ToLower(parsedURL.Path) | |
| 167 | return strings.HasSuffix(path, ".mp4") || strings.HasSuffix(path, ".m3u8") | |
| 168 | } | |
| 169 | ||
| 170 | func sortAllanimeSourcesByPriority(sourceUrls []allanimeSource) []allanimeSource { | |
| 171 | if len(sourceUrls) == 0 { | |
| 172 | return nil | |
| 173 | } | |
| 174 | ||
| 175 | sorted := append([]allanimeSource(nil), sourceUrls...) | |
| 176 | sort.SliceStable(sorted, func(i, j int) bool { | |
| 177 | return sorted[i].Priority > sorted[j].Priority | |
| 178 | }) | |
| 179 | ||
| 180 | return sorted | |
| 181 | } | |
| 182 | ||
| 150 | 183 | func extractLinks(provider_id string) map[string]interface{} { |
| 151 | 184 | provider_id = normalizeAllanimeProviderPath(provider_id) |
| 152 | 185 | |
| @@ -543,66 +576,66 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | ||
| 543 | 576 | } |
| 544 | 577 | |
| 545 | 578 | if response.Data.Tobeparsed != "" { |
| 546 | Log("Found tobeparsed field, using decoded response") | |
| 547 | decoded := decodeTobeparsed(response.Data.Tobeparsed) | |
| 548 | lines := strings.Split(strings.TrimSpace(decoded), "\n") | |
| 549 | var parsedURLs []struct { | |
| 550 | SourceName string | |
| 551 | SourceUrl string | |
| 552 | } | |
| 553 | for _, line := range lines { | |
| 554 | if parts := strings.Split(line, " :"); len(parts) == 2 { | |
| 555 | parsedURLs = append(parsedURLs, struct { | |
| 556 | SourceName string | |
| 557 | SourceUrl string | |
| 558 | }{SourceName: parts[0], SourceUrl: "--" + parts[1]}) | |
| 559 | } | |
| 560 | } | |
| 561 | ||
| 562 | validURLs := make([]string, 0) | |
| 563 | for _, url := range parsedURLs { | |
| 564 | validURLs = append(validURLs, url.SourceUrl) | |
| 565 | } | |
| 566 | ||
| 567 | if len(validURLs) == 0 { | |
| 568 | return nil, fmt.Errorf("no valid source URLs found in decoded tobeparsed") | |
| 579 | Log("Found tobeparsed field, decoding source URLs") | |
| 580 | decodedSources, err := decodeTobeparsed(response.Data.Tobeparsed) | |
| 581 | if err != nil { | |
| 582 | return nil, err | |
| 569 | 583 | } |
| 570 | 584 | |
| 571 | return getLinksFromURLs(validURLs) | |
| 585 | Log(fmt.Sprintf("Decoded %d Allanime source URLs from tobeparsed payload", len(decodedSources))) | |
| 586 | return getLinksFromSourceUrls(decodedSources) | |
| 572 | 587 | } |
| 573 | 588 | |
| 574 | 589 | return getLinksFromSourceUrls(response.Data.Episode.SourceUrls) |
| 575 | 590 | } |
| 576 | 591 | |
| 577 | func getLinksFromSourceUrls(sourceUrls []struct { | |
| 578 | SourceUrl string `json:"sourceUrl"` | |
| 579 | SourceName string `json:"sourceName"` | |
| 580 | }) ([]string, error) { | |
| 592 | func getLinksFromSourceUrls(sourceUrls []allanimeSource) ([]string, error) { | |
| 593 | sourceUrls = sortAllanimeSourcesByPriority(sourceUrls) | |
| 594 | ||
| 595 | directURLs := make([]string, 0) | |
| 581 | 596 | validURLs := make([]string, 0) |
| 582 | highestPriority := -1 | |
| 597 | highestPriority := -1.0 | |
| 583 | 598 | var highestPriorityURL string |
| 584 | 599 | |
| 585 | 600 | for _, url := range sourceUrls { |
| 586 | if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) { | |
| 587 | decodedURL := decodeProviderID(url.SourceUrl[2:]) | |
| 588 | if strings.Contains(decodedURL, LinkPriorities[0]) { | |
| 589 | priority := int(url.SourceUrl[2] - '0') | |
| 590 | if priority > highestPriority { | |
| 591 | highestPriority = priority | |
| 592 | highestPriorityURL = url.SourceUrl | |
| 593 | } | |
| 594 | } else { | |
| 595 | validURLs = append(validURLs, url.SourceUrl) | |
| 601 | sourceURL := strings.TrimSpace(url.SourceUrl) | |
| 602 | if sourceURL == "" { | |
| 603 | continue | |
| 604 | } | |
| 605 | ||
| 606 | if isDirectPlayableAllanimeSource(url) { | |
| 607 | directURLs = append(directURLs, sourceURL) | |
| 608 | continue | |
| 609 | } | |
| 610 | ||
| 611 | if !strings.HasPrefix(sourceURL, "--") || len(sourceURL) <= 2 { | |
| 612 | Log(fmt.Sprintf("Skipping unsupported Allanime source %q (%s)", url.SourceName, sourceURL)) | |
| 613 | continue | |
| 614 | } | |
| 615 | ||
| 616 | decodedURL := decodeProviderID(sourceURL[2:]) | |
| 617 | if strings.Contains(decodedURL, LinkPriorities[0]) { | |
| 618 | priority := url.Priority | |
| 619 | if priority > highestPriority { | |
| 620 | highestPriority = priority | |
| 621 | highestPriorityURL = sourceURL | |
| 596 | 622 | } |
| 623 | } else { | |
| 624 | validURLs = append(validURLs, sourceURL) | |
| 597 | 625 | } |
| 598 | 626 | } |
| 599 | 627 | |
| 628 | if len(directURLs) > 0 { | |
| 629 | Log(fmt.Sprintf("Using %d direct Allanime source URL(s)", len(directURLs))) | |
| 630 | return directURLs, nil | |
| 631 | } | |
| 632 | ||
| 600 | 633 | if highestPriorityURL != "" { |
| 601 | 634 | validURLs = []string{highestPriorityURL} |
| 602 | 635 | } |
| 603 | 636 | |
| 604 | 637 | if len(validURLs) == 0 { |
| 605 | return nil, fmt.Errorf("no valid source URLs found in response") | |
| 638 | return nil, fmt.Errorf("no supported source URLs found in response") | |
| 606 | 639 | } |
| 607 | 640 | |
| 608 | 641 | return getLinksFromURLs(validURLs) |
internal/episode_url_test.go added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | package internal | |
| 2 | ||
| 3 | import "testing" | |
| 4 | ||
| 5 | func TestGetLinksFromSourceUrlsPrefersDirectPlayableSources(t *testing.T) { | |
| 6 | t.Parallel() | |
| 7 | ||
| 8 | sourceUrls := []allanimeSource{ | |
| 9 | { | |
| 10 | SourceUrl: "--175948514e4c4f57", | |
| 11 | SourceName: "Luf-Mp4", | |
| 12 | Priority: 7.5, | |
| 13 | }, | |
| 14 | { | |
| 15 | SourceUrl: "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test", | |
| 16 | SourceName: "Yt-mp4", | |
| 17 | Priority: 7.9, | |
| 18 | }, | |
| 19 | { | |
| 20 | SourceUrl: "https://mp4upload.com/embed-example.html", | |
| 21 | SourceName: "Mp4", | |
| 22 | Priority: 4, | |
| 23 | }, | |
| 24 | } | |
| 25 | ||
| 26 | links, err := getLinksFromSourceUrls(sourceUrls) | |
| 27 | if err != nil { | |
| 28 | t.Fatalf("expected direct Allanime sources to be returned without error, got %v", err) | |
| 29 | } | |
| 30 | ||
| 31 | if len(links) != 1 { | |
| 32 | t.Fatalf("expected one direct playable source, got %d: %#v", len(links), links) | |
| 33 | } | |
| 34 | ||
| 35 | if got, want := links[0], "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test"; got != want { | |
| 36 | t.Fatalf("getLinksFromSourceUrls() returned %q, want %q", got, want) | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | func TestSortAllanimeSourcesByPriorityOrdersDescending(t *testing.T) { | |
| 41 | t.Parallel() | |
| 42 | ||
| 43 | sourceUrls := []allanimeSource{ | |
| 44 | {SourceUrl: "https://example.com/low.mp4", Priority: 1}, | |
| 45 | {SourceUrl: "https://example.com/high.mp4", Priority: 9}, | |
| 46 | {SourceUrl: "https://example.com/mid.mp4", Priority: 5}, | |
| 47 | } | |
| 48 | ||
| 49 | sorted := sortAllanimeSourcesByPriority(sourceUrls) | |
| 50 | if len(sorted) != 3 { | |
| 51 | t.Fatalf("expected 3 sorted sources, got %d", len(sorted)) | |
| 52 | } | |
| 53 | ||
| 54 | if sorted[0].SourceUrl != "https://example.com/high.mp4" { | |
| 55 | t.Fatalf("expected highest priority source first, got %#v", sorted) | |
| 56 | } | |
| 57 | if sorted[1].SourceUrl != "https://example.com/mid.mp4" { | |
| 58 | t.Fatalf("expected middle priority source second, got %#v", sorted) | |
| 59 | } | |
| 60 | if sorted[2].SourceUrl != "https://example.com/low.mp4" { | |
| 61 | t.Fatalf("expected lowest priority source last, got %#v", sorted) | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | func TestIsDirectPlayableAllanimeSource(t *testing.T) { | |
| 66 | t.Parallel() | |
| 67 | ||
| 68 | cases := []struct { | |
| 69 | name string | |
| 70 | source allanimeSource | |
| 71 | want bool | |
| 72 | }{ | |
| 73 | { | |
| 74 | name: "fast4speed player url", | |
| 75 | source: allanimeSource{ | |
| 76 | SourceName: "Yt-mp4", | |
| 77 | SourceUrl: "https://tools.fast4speed.rsvp/media9/videos/example/sub/1?Authorization=test", | |
| 78 | }, | |
| 79 | want: true, | |
| 80 | }, | |
| 81 | { | |
| 82 | name: "sharepoint mp4 source", | |
| 83 | source: allanimeSource{ | |
| 84 | SourceName: "S-mp4", | |
| 85 | SourceUrl: "https://tenant.sharepoint.com/video.mp4", | |
| 86 | }, | |
| 87 | want: true, | |
| 88 | }, | |
| 89 | { | |
| 90 | name: "mp4upload embed url", | |
| 91 | source: allanimeSource{ | |
| 92 | SourceName: "Mp4", | |
| 93 | SourceUrl: "https://mp4upload.com/embed-example.html", | |
| 94 | }, | |
| 95 | want: false, | |
| 96 | }, | |
| 97 | { | |
| 98 | name: "filemoon embed url", | |
| 99 | source: allanimeSource{ | |
| 100 | SourceName: "Fm-Hls", | |
| 101 | SourceUrl: "https://bysekoze.com/e/example", | |
| 102 | }, | |
| 103 | want: false, | |
| 104 | }, | |
| 105 | } | |
| 106 | ||
| 107 | for _, tc := range cases { | |
| 108 | tc := tc | |
| 109 | t.Run(tc.name, func(t *testing.T) { | |
| 110 | t.Parallel() | |
| 111 | ||
| 112 | if got := isDirectPlayableAllanimeSource(tc.source); got != tc.want { | |
| 113 | t.Fatalf("isDirectPlayableAllanimeSource(%+v) = %v, want %v", tc.source, got, tc.want) | |
| 114 | } | |
| 115 | }) | |
| 116 | } | |
| 117 | } | |