Commit f6b128db23

f6b128db2378c4d9056a4c9c66f86b8d325cb5a1

parent: 3f97038598

Unsigned

Wraient <wraient.911@gmail.com> · 2026-04-16 13:32 UTC

fix: All anime decryption logic

Layout: unified · split

internal/episode_url.go +105 −11
@@ -2,6 +2,11 @@ package internal
22
33import (
44 "bytes"
5 "crypto/aes"
6 "crypto/cipher"
7 "crypto/sha256"
8 "encoding/base64"
9 "encoding/binary"
510 "encoding/json"
611 "fmt"
712 "io"
@@ -14,9 +19,12 @@ import (
1419
1520type allanimeResponse struct {
1621 Data struct {
17 Episode struct {
22 M string `json:"_m"`
23 Tobeparsed string `json:"tobeparsed"`
24 Episode struct {
1825 SourceUrls []struct {
19 SourceUrl string `json:"sourceUrl"`
26 SourceUrl string `json:"sourceUrl"`
27 SourceName string `json:"sourceName"`
2028 } `json:"sourceUrls"`
2129 } `json:"episode"`
2230 } `json:"data"`
@@ -28,6 +36,58 @@ type result struct {
2836 err error
2937}
3038
39func decodeTobeparsed(blob string) string {
40 key := []byte("SimtVuagFbGR2K7P")
41 hash := sha256.Sum256(key)
42
43 data, err := base64.StdEncoding.DecodeString(blob)
44 if err != nil {
45 Log(fmt.Sprint("Error decoding base64:", err))
46 return ""
47 }
48
49 if len(data) < 28 {
50 Log(fmt.Sprint("Data too short to contain IV and ciphertext"))
51 return ""
52 }
53
54 iv := data[:12]
55 ct := data[12:]
56
57 ctrIV := make([]byte, 16)
58 copy(ctrIV, iv)
59 binary.BigEndian.PutUint32(ctrIV[12:], uint32(2))
60
61 block, err := aes.NewCipher(hash[:])
62 if err != nil {
63 Log(fmt.Sprint("Error creating cipher:", err))
64 return ""
65 }
66
67 stream := cipher.NewCTR(block, ctrIV)
68 plain := make([]byte, len(ct))
69 stream.XORKeyStream(plain, ct)
70
71 result := string(plain)
72 result = strings.ReplaceAll(result, "{", "\n")
73 result = strings.ReplaceAll(result, "}", "\n")
74
75 re := regexp.MustCompile(`"sourceUrl":"--([^"]+)".*"sourceName":"([^"]+)"`)
76 matches := re.FindAllStringSubmatch(result, -1)
77
78 var sb strings.Builder
79 for _, match := range matches {
80 if len(match) == 3 {
81 sb.WriteString(match[2])
82 sb.WriteString(" :")
83 sb.WriteString(match[1])
84 sb.WriteString("\n")
85 }
86 }
87
88 return sb.String()
89}
90
3191func decodeProviderID(encoded string) string {
3292 // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g')
3393 re := regexp.MustCompile("..")
@@ -204,7 +264,7 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
204264
205265 body, err := io.ReadAll(resp.Body)
206266 if err != nil {
207 return nil, fmt.Errorf("failed to read response: %w", err)
267 return nil, fmt.Errorf("failed to read response body: %w", err)
208268 }
209269
210270 var response allanimeResponse
@@ -214,12 +274,47 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
214274 return nil, err
215275 }
216276
217 // Pre-count valid URLs and create slice to preserve order
277 if response.Data.Tobeparsed != "" {
278 Log("Found tobeparsed field, using decoded response")
279 decoded := decodeTobeparsed(response.Data.Tobeparsed)
280 lines := strings.Split(strings.TrimSpace(decoded), "\n")
281 var parsedURLs []struct {
282 SourceName string
283 SourceUrl string
284 }
285 for _, line := range lines {
286 if parts := strings.Split(line, " :"); len(parts) == 2 {
287 parsedURLs = append(parsedURLs, struct {
288 SourceName string
289 SourceUrl string
290 }{SourceName: parts[0], SourceUrl: "--" + parts[1]})
291 }
292 }
293
294 validURLs := make([]string, 0)
295 for _, url := range parsedURLs {
296 validURLs = append(validURLs, url.SourceUrl)
297 }
298
299 if len(validURLs) == 0 {
300 return nil, fmt.Errorf("no valid source URLs found in decoded tobeparsed")
301 }
302
303 return getLinksFromURLs(validURLs)
304 }
305
306 return getLinksFromSourceUrls(response.Data.Episode.SourceUrls)
307}
308
309func getLinksFromSourceUrls(sourceUrls []struct {
310 SourceUrl string `json:"sourceUrl"`
311 SourceName string `json:"sourceName"`
312}) ([]string, error) {
218313 validURLs := make([]string, 0)
219314 highestPriority := -1
220315 var highestPriorityURL string
221316
222 for _, url := range response.Data.Episode.SourceUrls {
317 for _, url := range sourceUrls {
223318 if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) {
224319 decodedURL := decodeProviderID(url.SourceUrl[2:])
225320 if strings.Contains(decodedURL, LinkPriorities[0]) {
@@ -234,7 +329,6 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
234329 }
235330 }
236331
237 // If we found a highest priority URL, use only that
238332 if highestPriorityURL != "" {
239333 validURLs = []string{highestPriorityURL}
240334 }
@@ -243,22 +337,22 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
243337 return nil, fmt.Errorf("no valid source URLs found in response")
244338 }
245339
246 // Create channels for results and a slice to store ordered results
340 return getLinksFromURLs(validURLs)
341}
342
343func getLinksFromURLs(validURLs []string) ([]string, error) {
247344 results := make(chan result, len(validURLs))
248345 orderedResults := make([][]string, len(validURLs))
249346
250 // Add a channel for high priority links
251347 highPriorityLink := make(chan []string, 1)
252348
253 // Create rate limiter
254349 rateLimiter := time.NewTicker(50 * time.Millisecond)
255350 defer rateLimiter.Stop()
256351
257 // Launch goroutines
258352 remainingURLs := len(validURLs)
259353 for i, sourceUrl := range validURLs {
260354 go func(idx int, url string) {
261 <-rateLimiter.C // Rate limit the requests
355 <-rateLimiter.C
262356
263357 decodedProviderID := decodeProviderID(url[2:])
264358 Log(fmt.Sprintf("Processing URL %d/%d with provider ID: %s", idx+1, len(validURLs), decodedProviderID))