11import axios from "axios" ;
22import CryptoJS from "crypto-js" ;
33import { v1_base_url } from "../../utils/base_v1.js" ;
4+ import { fallback_1 , fallback_2 } from "../../utils/fallback.js" ;
45
5- export async function decryptSources_v1 ( id , name , type ) {
6+ export async function decryptSources_v1 ( epID , id , name , type ) {
67 try {
78 const [ { data : sourcesData } , { data : key } ] = await Promise . all ( [
89 axios . get ( `https://${ v1_base_url } /ajax/v2/episode/sources?id=${ id } ` ) ,
910 axios . get (
10- "https://raw.githubusercontent.com/itzzzme/megacloud-keys/refs/heads/main/key.txt"
11+ "https://raw.githubusercontent.com/itzzzme/megacloud-keys/refs/heads/main/key.txt" ,
1112 ) ,
1213 ] ) ;
1314
@@ -19,21 +20,51 @@ export async function decryptSources_v1(id, name, type) {
1920 if ( ! sourceId ) throw new Error ( "Unable to extract sourceId from link" ) ;
2021
2122 const { data : rawSourceData } = await axios . get (
22- `https://megacloud.blog/embed-2/v2 /e-1/getSources?id=${ sourceId } `
23+ `https://megacloud.blog/embed-2/v3 /e-1/getSources?id=${ sourceId } ` ,
2324 ) ;
24- const encrypted = rawSourceData ?. sources ;
25- if ( ! encrypted ) throw new Error ( "Encrypted source missing in response" ) ;
26- const decrypted = CryptoJS . AES . decrypt ( encrypted , key . trim ( ) ) . toString (
27- CryptoJS . enc . Utf8
28- ) ;
29- if ( ! decrypted ) throw new Error ( "Failed to decrypt source" ) ;
3025
3126 let decryptedSources ;
27+
3228 try {
29+ const encrypted = rawSourceData ?. sources ;
30+ if ( ! encrypted ) throw new Error ( "Encrypted source missing" ) ;
31+
32+ const decrypted = CryptoJS . AES . decrypt ( encrypted , key . trim ( ) ) . toString (
33+ CryptoJS . enc . Utf8 ,
34+ ) ;
35+ if ( ! decrypted ) throw new Error ( "Failed to decrypt source" ) ;
36+
3337 decryptedSources = JSON . parse ( decrypted ) ;
3438 } catch ( e ) {
35- throw new Error ( "Decrypted data is not valid JSON" ) ;
39+ try {
40+ const fallback = name . toLowerCase ( ) === 'hd-1' ?fallback_1 :fallback_2 ;
41+ const { data : html } = await axios . get (
42+ `https://${ fallback } /stream/s-2/${ epID } /${ type } ` ,
43+ {
44+ headers : {
45+ Referer : `https://${ fallback_1 } /` ,
46+ } ,
47+ } ,
48+ ) ;
49+ const dataIdMatch = html . match ( / d a t a - i d = [ " ' ] ( \d + ) [ " ' ] / ) ;
50+ const realId = dataIdMatch ?. [ 1 ] ;
51+ if ( ! realId ) throw new Error ( "Could not extract data-id for fallback" ) ;
52+
53+ const { data : fallback_data } = await axios . get (
54+ `https://${ fallback } /stream/getSources?id=${ realId } ` ,
55+ {
56+ headers : {
57+ "X-Requested-With" : "XMLHttpRequest" ,
58+ } ,
59+ } ,
60+ ) ;
61+
62+ decryptedSources = [ { file : fallback_data . sources . file } ] ;
63+ } catch ( fallbackError ) {
64+ throw new Error ( "Fallback failed: " + fallbackError . message ) ;
65+ }
3666 }
67+
3768 return {
3869 id,
3970 type,
0 commit comments