Commit ea1da556af
Unsigned
Layout: unified · split
internal/episode_url.go +254 −21
| @@ -7,11 +7,15 @@ import ( | |||
| 7 | "crypto/sha256" | 7 | "crypto/sha256" |
| 8 | "encoding/base64" | 8 | "encoding/base64" |
| 9 | "encoding/binary" | 9 | "encoding/binary" |
| 10 | "encoding/hex" | ||
| 10 | "encoding/json" | 11 | "encoding/json" |
| 11 | "fmt" | 12 | "fmt" |
| 12 | "io" | 13 | "io" |
| 13 | "net/http" | 14 | "net/http" |
| 15 | "net/url" | ||
| 14 | "regexp" | 16 | "regexp" |
| 17 | "sort" | ||
| 18 | "strconv" | ||
| 15 | "strings" | 19 | "strings" |
| 16 | "time" | 20 | "time" |
| 17 | "unicode" | 21 | "unicode" |
| @@ -36,6 +40,12 @@ type result struct { | |||
| 36 | err error | 40 | err error |
| 37 | } | 41 | } |
| 38 | 42 | ||
| 43 | type filemoonResponse struct { | ||
| 44 | IV string `json:"iv"` | ||
| 45 | Payload string `json:"payload"` | ||
| 46 | KeyParts []string `json:"key_parts"` | ||
| 47 | } | ||
| 48 | |||
| 39 | func decodeTobeparsed(blob string) string { | 49 | func decodeTobeparsed(blob string) string { |
| 40 | key := []byte("Xot36i3lK3:v1") | 50 | key := []byte("Xot36i3lK3:v1") |
| 41 | hash := sha256.Sum256(key) | 51 | hash := sha256.Sum256(key) |
| @@ -138,6 +148,8 @@ func decodeProviderID(encoded string) string { | |||
| 138 | } | 148 | } |
| 139 | 149 | ||
| 140 | func extractLinks(provider_id string) map[string]interface{} { | 150 | func extractLinks(provider_id string) map[string]interface{} { |
| 151 | provider_id = normalizeAllanimeProviderPath(provider_id) | ||
| 152 | |||
| 141 | // Check if provider_id is already a full URL (external link) | 153 | // Check if provider_id is already a full URL (external link) |
| 142 | if strings.HasPrefix(provider_id, "http://") || strings.HasPrefix(provider_id, "https://") { | 154 | if strings.HasPrefix(provider_id, "http://") || strings.HasPrefix(provider_id, "https://") { |
| 143 | // It's an external direct video link, preserve it exactly as provided. | 155 | // It's an external direct video link, preserve it exactly as provided. |
| @@ -187,10 +199,182 @@ func extractLinks(provider_id string) map[string]interface{} { | |||
| 187 | return videoData | 199 | return videoData |
| 188 | } | 200 | } |
| 189 | 201 | ||
| 202 | // Filemoon extractor payload does not return a top-level "links" field. | ||
| 203 | if _, hasLinks := videoData["links"]; !hasLinks { | ||
| 204 | if filemoonLinks := extractFilemoonLinks(videoData); len(filemoonLinks) > 0 { | ||
| 205 | links := make([]interface{}, 0, len(filemoonLinks)) | ||
| 206 | for _, link := range filemoonLinks { | ||
| 207 | links = append(links, map[string]interface{}{"link": link}) | ||
| 208 | } | ||
| 209 | videoData["links"] = links | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 190 | // Process the data as needed | 213 | // Process the data as needed |
| 191 | return videoData | 214 | return videoData |
| 192 | } | 215 | } |
| 193 | 216 | ||
| 217 | func normalizeAllanimeProviderPath(providerID string) string { | ||
| 218 | const allanimePrefix = "/https://allanime.day" | ||
| 219 | |||
| 220 | if strings.HasPrefix(providerID, allanimePrefix) { | ||
| 221 | trimmed := strings.TrimPrefix(providerID, allanimePrefix) | ||
| 222 | if trimmed == "" { | ||
| 223 | return "/" | ||
| 224 | } | ||
| 225 | if !strings.HasPrefix(trimmed, "/") { | ||
| 226 | return "/" + trimmed | ||
| 227 | } | ||
| 228 | return trimmed | ||
| 229 | } | ||
| 230 | |||
| 231 | return providerID | ||
| 232 | } | ||
| 233 | |||
| 234 | func decodeBase64URLRaw(input string) ([]byte, error) { | ||
| 235 | if decoded, err := base64.RawURLEncoding.DecodeString(input); err == nil { | ||
| 236 | return decoded, nil | ||
| 237 | } | ||
| 238 | return base64.URLEncoding.DecodeString(input) | ||
| 239 | } | ||
| 240 | |||
| 241 | func extractFilemoonLinks(videoData map[string]interface{}) []string { | ||
| 242 | raw, err := json.Marshal(videoData) | ||
| 243 | if err != nil { | ||
| 244 | Log(fmt.Sprintf("Error marshaling filemoon payload: %v", err)) | ||
| 245 | return nil | ||
| 246 | } | ||
| 247 | |||
| 248 | var payload filemoonResponse | ||
| 249 | if err := json.Unmarshal(raw, &payload); err != nil { | ||
| 250 | Log(fmt.Sprintf("Error parsing filemoon payload: %v", err)) | ||
| 251 | return nil | ||
| 252 | } | ||
| 253 | |||
| 254 | if payload.IV == "" || payload.Payload == "" || len(payload.KeyParts) < 2 { | ||
| 255 | return nil | ||
| 256 | } | ||
| 257 | |||
| 258 | keyPart1, err := decodeBase64URLRaw(payload.KeyParts[0]) | ||
| 259 | if err != nil { | ||
| 260 | Log(fmt.Sprintf("Error decoding filemoon key part 1: %v", err)) | ||
| 261 | return nil | ||
| 262 | } | ||
| 263 | |||
| 264 | keyPart2, err := decodeBase64URLRaw(payload.KeyParts[1]) | ||
| 265 | if err != nil { | ||
| 266 | Log(fmt.Sprintf("Error decoding filemoon key part 2: %v", err)) | ||
| 267 | return nil | ||
| 268 | } | ||
| 269 | |||
| 270 | iv, err := decodeBase64URLRaw(payload.IV) | ||
| 271 | if err != nil { | ||
| 272 | Log(fmt.Sprintf("Error decoding filemoon IV: %v", err)) | ||
| 273 | return nil | ||
| 274 | } | ||
| 275 | |||
| 276 | ciphertext, err := decodeBase64URLRaw(payload.Payload) | ||
| 277 | if err != nil { | ||
| 278 | Log(fmt.Sprintf("Error decoding filemoon ciphertext: %v", err)) | ||
| 279 | return nil | ||
| 280 | } | ||
| 281 | |||
| 282 | if len(ciphertext) <= 16 { | ||
| 283 | Log("Filemoon ciphertext is too short") | ||
| 284 | return nil | ||
| 285 | } | ||
| 286 | |||
| 287 | // Match jerry.sh behavior: decrypt all bytes except the final 16-byte trailer. | ||
| 288 | ciphertext = ciphertext[:len(ciphertext)-16] | ||
| 289 | |||
| 290 | keyHex := hex.EncodeToString(append(keyPart1, keyPart2...)) | ||
| 291 | key, err := hex.DecodeString(keyHex) | ||
| 292 | if err != nil { | ||
| 293 | Log(fmt.Sprintf("Error decoding filemoon key hex: %v", err)) | ||
| 294 | return nil | ||
| 295 | } | ||
| 296 | |||
| 297 | ctrIVHex := hex.EncodeToString(iv) + "00000002" | ||
| 298 | ctrIV, err := hex.DecodeString(ctrIVHex) | ||
| 299 | if err != nil { | ||
| 300 | Log(fmt.Sprintf("Error decoding filemoon CTR IV: %v", err)) | ||
| 301 | return nil | ||
| 302 | } | ||
| 303 | |||
| 304 | if len(ctrIV) != aes.BlockSize { | ||
| 305 | Log(fmt.Sprintf("Invalid filemoon CTR IV size: %d", len(ctrIV))) | ||
| 306 | return nil | ||
| 307 | } | ||
| 308 | |||
| 309 | block, err := aes.NewCipher(key) | ||
| 310 | if err != nil { | ||
| 311 | Log(fmt.Sprintf("Error creating filemoon cipher: %v", err)) | ||
| 312 | return nil | ||
| 313 | } | ||
| 314 | |||
| 315 | plain := make([]byte, len(ciphertext)) | ||
| 316 | cipher.NewCTR(block, ctrIV).XORKeyStream(plain, ciphertext) | ||
| 317 | |||
| 318 | decoded := strings.ReplaceAll(string(plain), `\u0026`, "&") | ||
| 319 | decoded = strings.ReplaceAll(decoded, `\u003D`, "=") | ||
| 320 | |||
| 321 | type qualityLink struct { | ||
| 322 | height int | ||
| 323 | link string | ||
| 324 | } | ||
| 325 | |||
| 326 | collected := make([]qualityLink, 0) | ||
| 327 | seen := make(map[string]struct{}) | ||
| 328 | |||
| 329 | reURLFirst := regexp.MustCompile(`"url":"([^"]+)".*?"height":([0-9]+)`) | ||
| 330 | reHeightFirst := regexp.MustCompile(`"height":([0-9]+).*?"url":"([^"]+)"`) | ||
| 331 | |||
| 332 | for _, match := range reURLFirst.FindAllStringSubmatch(decoded, -1) { | ||
| 333 | if len(match) != 3 { | ||
| 334 | continue | ||
| 335 | } | ||
| 336 | height, err := strconv.Atoi(match[2]) | ||
| 337 | if err != nil { | ||
| 338 | continue | ||
| 339 | } | ||
| 340 | if _, exists := seen[match[1]]; exists { | ||
| 341 | continue | ||
| 342 | } | ||
| 343 | seen[match[1]] = struct{}{} | ||
| 344 | collected = append(collected, qualityLink{height: height, link: match[1]}) | ||
| 345 | } | ||
| 346 | |||
| 347 | for _, match := range reHeightFirst.FindAllStringSubmatch(decoded, -1) { | ||
| 348 | if len(match) != 3 { | ||
| 349 | continue | ||
| 350 | } | ||
| 351 | height, err := strconv.Atoi(match[1]) | ||
| 352 | if err != nil { | ||
| 353 | continue | ||
| 354 | } | ||
| 355 | if _, exists := seen[match[2]]; exists { | ||
| 356 | continue | ||
| 357 | } | ||
| 358 | seen[match[2]] = struct{}{} | ||
| 359 | collected = append(collected, qualityLink{height: height, link: match[2]}) | ||
| 360 | } | ||
| 361 | |||
| 362 | sort.Slice(collected, func(i, j int) bool { | ||
| 363 | return collected[i].height > collected[j].height | ||
| 364 | }) | ||
| 365 | |||
| 366 | links := make([]string, 0, len(collected)) | ||
| 367 | for _, entry := range collected { | ||
| 368 | links = append(links, entry.link) | ||
| 369 | } | ||
| 370 | |||
| 371 | if len(links) > 0 { | ||
| 372 | Log("Filemoon links fetched") | ||
| 373 | } | ||
| 374 | |||
| 375 | return links | ||
| 376 | } | ||
| 377 | |||
| 194 | // Get anime episode url respective to given config | 378 | // Get anime episode url respective to given config |
| 195 | // If the link is found, it returns a list of links. Otherwise, it returns an error. | 379 | // If the link is found, it returns a list of links. Otherwise, it returns an error. |
| 196 | // | 380 | // |
| @@ -264,7 +448,11 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) { | |||
| 264 | } | 448 | } |
| 265 | 449 | ||
| 266 | func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | 450 | func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { |
| 267 | query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` | 451 | const ( |
| 452 | episodeQueryHash = "d405d0edd690624b66baba3068e0edc3ac90f1597d898a1ec8db4e5c43c00fec" | ||
| 453 | ) | ||
| 454 | |||
| 455 | episodeEmbedGQL := `query ($showId: String!, $translationType: VaildTranslationTypeEnumType!, $episodeString: String!) { episode( showId: $showId translationType: $translationType episodeString: $episodeString ) { episodeString sourceUrls }}` | ||
| 268 | 456 | ||
| 269 | variables := map[string]interface{}{ | 457 | variables := map[string]interface{}{ |
| 270 | "showId": id, | 458 | "showId": id, |
| @@ -272,41 +460,86 @@ func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { | |||
| 272 | "episodeString": fmt.Sprintf("%d", epNo), | 460 | "episodeString": fmt.Sprintf("%d", epNo), |
| 273 | } | 461 | } |
| 274 | 462 | ||
| 275 | // Build POST request body | 463 | extensions := map[string]interface{}{ |
| 276 | requestBody, err := json.Marshal(map[string]interface{}{ | 464 | "persistedQuery": map[string]interface{}{ |
| 277 | "query": query, | 465 | "version": 1, |
| 278 | "variables": variables, | 466 | "sha256Hash": episodeQueryHash, |
| 279 | }) | 467 | }, |
| 468 | } | ||
| 469 | |||
| 470 | variablesJSON, err := json.Marshal(variables) | ||
| 280 | if err != nil { | 471 | if err != nil { |
| 281 | return nil, fmt.Errorf("failed to marshal request body: %w", err) | 472 | return nil, fmt.Errorf("failed to marshal persisted query variables: %w", err) |
| 282 | } | 473 | } |
| 283 | 474 | ||
| 284 | req, err := http.NewRequest("POST", "https://api.allanime.day/api", bytes.NewBuffer(requestBody)) | 475 | extensionsJSON, err := json.Marshal(extensions) |
| 285 | if err != nil { | 476 | if err != nil { |
| 286 | return nil, fmt.Errorf("failed to create request: %w", err) | 477 | return nil, fmt.Errorf("failed to marshal persisted query extensions: %w", err) |
| 287 | } | 478 | } |
| 288 | 479 | ||
| 289 | req.Header.Set("Content-Type", "application/json") | 480 | persistedURL := "https://api.allanime.day/api?variables=" + url.QueryEscape(string(variablesJSON)) + "&extensions=" + url.QueryEscape(string(extensionsJSON)) |
| 290 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") | ||
| 291 | req.Header.Set("Referer", "https://allanime.to") | ||
| 292 | req.Header.Set("Origin", "https://allanime.to") | ||
| 293 | 481 | ||
| 294 | resp, err := sharedHTTPClient.Do(req) | 482 | req, err := http.NewRequest("GET", persistedURL, nil) |
| 295 | if err != nil { | 483 | if err != nil { |
| 296 | return nil, fmt.Errorf("failed to send request: %w", err) | 484 | return nil, fmt.Errorf("failed to create persisted query request: %w", err) |
| 297 | } | 485 | } |
| 298 | defer resp.Body.Close() | 486 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0") |
| 487 | req.Header.Set("Referer", "https://allmanga.to") | ||
| 488 | req.Header.Set("Origin", "https://youtu-chan.com") | ||
| 299 | 489 | ||
| 490 | resp, err := sharedHTTPClient.Do(req) | ||
| 491 | if err != nil { | ||
| 492 | return nil, fmt.Errorf("failed to send persisted query request: %w", err) | ||
| 493 | } | ||
| 300 | body, err := io.ReadAll(resp.Body) | 494 | body, err := io.ReadAll(resp.Body) |
| 495 | resp.Body.Close() | ||
| 301 | if err != nil { | 496 | if err != nil { |
| 302 | return nil, fmt.Errorf("failed to read response body: %w", err) | 497 | return nil, fmt.Errorf("failed to read persisted query response: %w", err) |
| 303 | } | 498 | } |
| 304 | 499 | ||
| 305 | var response allanimeResponse | 500 | var response allanimeResponse |
| 306 | err = json.Unmarshal(body, &response) | 501 | if err := json.Unmarshal(body, &response); err != nil { |
| 307 | if err != nil { | 502 | Log(fmt.Sprint("Error parsing persisted query JSON: ", err)) |
| 308 | Log(fmt.Sprint("Error parsing JSON: ", err)) | 503 | } |
| 309 | return nil, err | 504 | |
| 505 | useFallback := response.Data.Tobeparsed == "" && len(response.Data.Episode.SourceUrls) == 0 | ||
| 506 | if useFallback { | ||
| 507 | query := episodeEmbedGQL | ||
| 508 | |||
| 509 | // Build POST request body | ||
| 510 | requestBody, err := json.Marshal(map[string]interface{}{ | ||
| 511 | "query": query, | ||
| 512 | "variables": variables, | ||
| 513 | }) | ||
| 514 | if err != nil { | ||
| 515 | return nil, fmt.Errorf("failed to marshal request body: %w", err) | ||
| 516 | } | ||
| 517 | |||
| 518 | req, err := http.NewRequest("POST", "https://api.allanime.day/api", bytes.NewBuffer(requestBody)) | ||
| 519 | if err != nil { | ||
| 520 | return nil, fmt.Errorf("failed to create request: %w", err) | ||
| 521 | } | ||
| 522 | |||
| 523 | req.Header.Set("Content-Type", "application/json") | ||
| 524 | req.Header.Set("User-Agent", "Mozilla/5.0") | ||
| 525 | req.Header.Set("Referer", "https://allmanga.to") | ||
| 526 | req.Header.Set("Origin", "https://allanime.to") | ||
| 527 | |||
| 528 | resp, err := sharedHTTPClient.Do(req) | ||
| 529 | if err != nil { | ||
| 530 | return nil, fmt.Errorf("failed to send request: %w", err) | ||
| 531 | } | ||
| 532 | defer resp.Body.Close() | ||
| 533 | |||
| 534 | body, err := io.ReadAll(resp.Body) | ||
| 535 | if err != nil { | ||
| 536 | return nil, fmt.Errorf("failed to read response body: %w", err) | ||
| 537 | } | ||
| 538 | |||
| 539 | if err := json.Unmarshal(body, &response); err != nil { | ||
| 540 | Log(fmt.Sprint("Error parsing fallback JSON: ", err)) | ||
| 541 | return nil, err | ||
| 542 | } | ||
| 310 | } | 543 | } |
| 311 | 544 | ||
| 312 | if response.Data.Tobeparsed != "" { | 545 | if response.Data.Tobeparsed != "" { |