Commit f6b128db23
Unsigned
Layout: unified · split
internal/episode_url.go +105 −11
| @@ -2,6 +2,11 @@ package internal | ||
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "bytes" |
| 5 | "crypto/aes" | |
| 6 | "crypto/cipher" | |
| 7 | "crypto/sha256" | |
| 8 | "encoding/base64" | |
| 9 | "encoding/binary" | |
| 5 | 10 | "encoding/json" |
| 6 | 11 | "fmt" |
| 7 | 12 | "io" |
| @@ -14,9 +19,12 @@ import ( | ||
| 14 | 19 | |
| 15 | 20 | type allanimeResponse struct { |
| 16 | 21 | Data struct { |
| 17 | Episode struct { | |
| 22 | M string `json:"_m"` | |
| 23 | Tobeparsed string `json:"tobeparsed"` | |
| 24 | Episode struct { | |
| 18 | 25 | SourceUrls []struct { |
| 19 | SourceUrl string `json:"sourceUrl"` | |
| 26 | SourceUrl string `json:"sourceUrl"` | |
| 27 | SourceName string `json:"sourceName"` | |
| 20 | 28 | } `json:"sourceUrls"` |
| 21 | 29 | } `json:"episode"` |
| 22 | 30 | } `json:"data"` |
| @@ -28,6 +36,58 @@ type result struct { | ||
| 28 | 36 | err error |
| 29 | 37 | } |
| 30 | 38 | |
| 39 | func 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 | ||
| 31 | 91 | func decodeProviderID(encoded string) string { |
| 32 | 92 | // Split the string into pairs of characters (.. equivalent of 'sed s/../&\n/g') |
| 33 | 93 | re := regexp.MustCompile("..") |
| @@ -204,7 +264,7 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | ||
| 204 | 264 | |
| 205 | 265 | body, err := io.ReadAll(resp.Body) |
| 206 | 266 | 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) | |
| 208 | 268 | } |
| 209 | 269 | |
| 210 | 270 | var response allanimeResponse |
| @@ -214,12 +274,47 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | ||
| 214 | 274 | return nil, err |
| 215 | 275 | } |
| 216 | 276 | |
| 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 | ||
| 309 | func getLinksFromSourceUrls(sourceUrls []struct { | |
| 310 | SourceUrl string `json:"sourceUrl"` | |
| 311 | SourceName string `json:"sourceName"` | |
| 312 | }) ([]string, error) { | |
| 218 | 313 | validURLs := make([]string, 0) |
| 219 | 314 | highestPriority := -1 |
| 220 | 315 | var highestPriorityURL string |
| 221 | 316 | |
| 222 | for _, url := range response.Data.Episode.SourceUrls { | |
| 317 | for _, url := range sourceUrls { | |
| 223 | 318 | if len(url.SourceUrl) > 2 && unicode.IsDigit(rune(url.SourceUrl[2])) { |
| 224 | 319 | decodedURL := decodeProviderID(url.SourceUrl[2:]) |
| 225 | 320 | if strings.Contains(decodedURL, LinkPriorities[0]) { |
| @@ -234,7 +329,6 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | ||
| 234 | 329 | } |
| 235 | 330 | } |
| 236 | 331 | |
| 237 | // If we found a highest priority URL, use only that | |
| 238 | 332 | if highestPriorityURL != "" { |
| 239 | 333 | validURLs = []string{highestPriorityURL} |
| 240 | 334 | } |
| @@ -243,22 +337,22 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | ||
| 243 | 337 | return nil, fmt.Errorf("no valid source URLs found in response") |
| 244 | 338 | } |
| 245 | 339 | |
| 246 | // Create channels for results and a slice to store ordered results | |
| 340 | return getLinksFromURLs(validURLs) | |
| 341 | } | |
| 342 | ||
| 343 | func getLinksFromURLs(validURLs []string) ([]string, error) { | |
| 247 | 344 | results := make(chan result, len(validURLs)) |
| 248 | 345 | orderedResults := make([][]string, len(validURLs)) |
| 249 | 346 | |
| 250 | // Add a channel for high priority links | |
| 251 | 347 | highPriorityLink := make(chan []string, 1) |
| 252 | 348 | |
| 253 | // Create rate limiter | |
| 254 | 349 | rateLimiter := time.NewTicker(50 * time.Millisecond) |
| 255 | 350 | defer rateLimiter.Stop() |
| 256 | 351 | |
| 257 | // Launch goroutines | |
| 258 | 352 | remainingURLs := len(validURLs) |
| 259 | 353 | for i, sourceUrl := range validURLs { |
| 260 | 354 | go func(idx int, url string) { |
| 261 | <-rateLimiter.C // Rate limit the requests | |
| 355 | <-rateLimiter.C | |
| 262 | 356 | |
| 263 | 357 | decodedProviderID := decodeProviderID(url[2:]) |
| 264 | 358 | Log(fmt.Sprintf("Processing URL %d/%d with provider ID: %s", idx+1, len(validURLs), decodedProviderID)) |