Commit 8182824ccd
Unsigned
Layout: unified · split
internal/episode_url.go +102 −69
| @@ -18,22 +18,30 @@ import ( | |||
| 18 | "strconv" | 18 | "strconv" |
| 19 | "strings" | 19 | "strings" |
| 20 | "time" | 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 | type allanimeResponse struct { | 29 | type allanimeResponse struct { |
| 25 | Data struct { | 30 | Data struct { |
| 26 | M string `json:"_m"` | 31 | M string `json:"_m"` |
| 27 | Tobeparsed string `json:"tobeparsed"` | 32 | Tobeparsed string `json:"tobeparsed"` |
| 28 | Episode struct { | 33 | Episode struct { |
| 29 | SourceUrls []struct { | 34 | SourceUrls []allanimeSource `json:"sourceUrls"` |
| 30 | SourceUrl string `json:"sourceUrl"` | ||
| 31 | SourceName string `json:"sourceName"` | ||
| 32 | } `json:"sourceUrls"` | ||
| 33 | } `json:"episode"` | 35 | } `json:"episode"` |
| 34 | } `json:"data"` | 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 | type result struct { | 45 | type result struct { |
| 38 | index int | 46 | index int |
| 39 | links []string | 47 | links []string |
| @@ -46,27 +54,24 @@ type filemoonResponse struct { | |||
| 46 | KeyParts []string `json:"key_parts"` | 54 | KeyParts []string `json:"key_parts"` |
| 47 | } | 55 | } |
| 48 | 56 | ||
| 49 | func decodeTobeparsed(blob string) string { | 57 | func decodeTobeparsed(blob string) ([]allanimeSource, error) { |
| 50 | key := []byte("Xot36i3lK3:v1") | 58 | key := []byte("Xot36i3lK3:v1") |
| 51 | hash := sha256.Sum256(key) | 59 | hash := sha256.Sum256(key) |
| 52 | 60 | ||
| 53 | data, err := base64.StdEncoding.DecodeString(blob) | 61 | data, err := base64.StdEncoding.DecodeString(blob) |
| 54 | if err != nil { | 62 | if err != nil { |
| 55 | Log(fmt.Sprint("Error decoding base64:", err)) | 63 | return nil, fmt.Errorf("error decoding base64: %w", err) |
| 56 | return "" | ||
| 57 | } | 64 | } |
| 58 | 65 | ||
| 59 | if len(data) < 29 { | 66 | if len(data) < 29 { |
| 60 | Log("Data too short to contain tobeparsed payload") | 67 | return nil, fmt.Errorf("data too short to contain tobeparsed payload") |
| 61 | return "" | ||
| 62 | } | 68 | } |
| 63 | 69 | ||
| 64 | // The payload format is: 1-byte header, 12-byte IV, ciphertext, 16-byte trailer. | 70 | // The payload format is: 1-byte header, 12-byte IV, ciphertext, 16-byte trailer. |
| 65 | iv := data[1:13] | 71 | iv := data[1:13] |
| 66 | ctLen := len(data) - 13 - 16 | 72 | ctLen := len(data) - 13 - 16 |
| 67 | if ctLen <= 0 { | 73 | if ctLen <= 0 { |
| 68 | Log("Ciphertext length is invalid in tobeparsed payload") | 74 | return nil, fmt.Errorf("ciphertext length is invalid in tobeparsed payload") |
| 69 | return "" | ||
| 70 | } | 75 | } |
| 71 | ct := data[13 : 13+ctLen] | 76 | ct := data[13 : 13+ctLen] |
| 72 | 77 | ||
| @@ -76,32 +81,23 @@ func decodeTobeparsed(blob string) string { | |||
| 76 | 81 | ||
| 77 | block, err := aes.NewCipher(hash[:]) | 82 | block, err := aes.NewCipher(hash[:]) |
| 78 | if err != nil { | 83 | if err != nil { |
| 79 | Log(fmt.Sprint("Error creating cipher:", err)) | 84 | return nil, fmt.Errorf("error creating cipher: %w", err) |
| 80 | return "" | ||
| 81 | } | 85 | } |
| 82 | 86 | ||
| 83 | stream := cipher.NewCTR(block, ctrIV) | 87 | stream := cipher.NewCTR(block, ctrIV) |
| 84 | plain := make([]byte, len(ct)) | 88 | plain := make([]byte, len(ct)) |
| 85 | stream.XORKeyStream(plain, ct) | 89 | stream.XORKeyStream(plain, ct) |
| 86 | 90 | ||
| 87 | result := string(plain) | 91 | var payload allanimeTobeparsedPayload |
| 88 | result = strings.ReplaceAll(result, "{", "\n") | 92 | if err := json.Unmarshal(plain, &payload); err != nil { |
| 89 | result = strings.ReplaceAll(result, "}", "\n") | 93 | return nil, fmt.Errorf("failed to parse decrypted tobeparsed payload: %w", err) |
| 90 | 94 | } | |
| 91 | re := regexp.MustCompile(`"sourceUrl":"--([^"]+)".*"sourceName":"([^"]+)"`) | ||
| 92 | matches := re.FindAllStringSubmatch(result, -1) | ||
| 93 | 95 | ||
| 94 | var sb strings.Builder | 96 | if len(payload.Episode.SourceUrls) == 0 { |
| 95 | for _, match := range matches { | 97 | return nil, fmt.Errorf("no source URLs found in decrypted tobeparsed payload") |
| 96 | if len(match) == 3 { | ||
| 97 | sb.WriteString(match[2]) | ||
| 98 | sb.WriteString(" :") | ||
| 99 | sb.WriteString(match[1]) | ||
| 100 | sb.WriteString("\n") | ||
| 101 | } | ||
| 102 | } | 98 | } |
| 103 | 99 | ||
| 104 | return sb.String() | 100 | return payload.Episode.SourceUrls, nil |
| 105 | } | 101 | } |
| 106 | 102 | ||
| 107 | func decodeProviderID(encoded string) string { | 103 | func decodeProviderID(encoded string) string { |
| @@ -147,6 +143,43 @@ func decodeProviderID(encoded string) string { | |||
| 147 | return result | 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 | func extractLinks(provider_id string) map[string]interface{} { | 183 | func extractLinks(provider_id string) map[string]interface{} { |
| 151 | provider_id = normalizeAllanimeProviderPath(provider_id) | 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 | if response.Data.Tobeparsed != "" { | 578 | if response.Data.Tobeparsed != "" { |
| 546 | Log("Found tobeparsed field, using decoded response") | 579 | Log("Found tobeparsed field, decoding source URLs") |
| 547 | decoded := decodeTobeparsed(response.Data.Tobeparsed) | 580 | decodedSources, err := decodeTobeparsed(response.Data.Tobeparsed) |
| 548 | lines := strings.Split(strings.TrimSpace(decoded), "\n") | 581 | if err != nil { |
| 549 | var parsedURLs []struct { | 582 | return nil, err |
| 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") | ||
| 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 | return getLinksFromSourceUrls(response.Data.Episode.SourceUrls) | 589 | return getLinksFromSourceUrls(response.Data.Episode.SourceUrls) |
| 575 | } | 590 | } |
| 576 | 591 | ||
| 577 | func getLinksFromSourceUrls(sourceUrls []struct { | 592 | func getLinksFromSourceUrls(sourceUrls []allanimeSource) ([]string, error) { |
| 578 | SourceUrl string `json:"sourceUrl"` | 593 | sourceUrls = sortAllanimeSourcesByPriority(sourceUrls) |
| 579 | SourceName string `json:"sourceName"` | 594 | |
| 580 | }) ([]string, error) { | 595 | directURLs := make([]string, 0) |
| 581 | validURLs := make([]string, 0) | 596 | validURLs := make([]string, 0) |
| 582 | highestPriority := -1 | 597 | highestPriority := -1.0 |
| 583 | var highestPriorityURL string | 598 | var highestPriorityURL string |
| 584 | 599 | ||
| 585 | for _, url := range sourceUrls { | 600 | for _, url := range sourceUrls { |
| 586 | if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) { | 601 | sourceURL := strings.TrimSpace(url.SourceUrl) |
| 587 | decodedURL := decodeProviderID(url.SourceUrl[2:]) | 602 | if sourceURL == "" { |
| 588 | if strings.Contains(decodedURL, LinkPriorities[0]) { | 603 | continue |
| 589 | priority := int(url.SourceUrl[2] - '0') | 604 | } |
| 590 | if priority > highestPriority { | 605 | |
| 591 | highestPriority = priority | 606 | if isDirectPlayableAllanimeSource(url) { |
| 592 | highestPriorityURL = url.SourceUrl | 607 | directURLs = append(directURLs, sourceURL) |
| 593 | } | 608 | continue |
| 594 | } else { | 609 | } |
| 595 | validURLs = append(validURLs, url.SourceUrl) | 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 | if highestPriorityURL != "" { | 633 | if highestPriorityURL != "" { |
| 601 | validURLs = []string{highestPriorityURL} | 634 | validURLs = []string{highestPriorityURL} |
| 602 | } | 635 | } |
| 603 | 636 | ||
| 604 | if len(validURLs) == 0 { | 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 | return getLinksFromURLs(validURLs) | 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 | } | ||