code wiki / _hdl_build / nx_media_state_gate.nx

nx_media_state_gate.nx source

↩ module page · 67 lines · 4275 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_media_state_gate.nx -- proves embedded-state stream extraction: pull the JSON blob from window.__NUXT__ / 4// ytInitialPlayerResponse / __INITIAL_STATE__ and find the stream URLs inside it -- no bundle execution. 5// Covers JSON-escaped `\/`, nested braces inside strings, and the no-marker case. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_media_state.nx" 8import "nx_gate_verdict.nx" 9 10func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 11" as *u8); return ok } 12func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 13 14func run_ms(html: *u8, marker: *u8, want_url: *u8) -> i64 { 15 let blob: *u8 = sys_mmap(262144) 16 let bl: i64 = ms_extract_state(html, gsl(html), marker, blob, 262144) 17 if bl == 0 { gw(" (no state blob)\n" as *u8); return 0 } 18 let streams: *u8 = sys_mmap(8192) 19 let n: i64 = ms_find_streams(blob, bl, streams, 8192) 20 if n == 0 { gw(" (0 streams in blob=[" as *u8); gw(blob); gw("])\n" as *u8); return 0 } 21 if ms_contains(streams, gsl(streams), want_url, gsl(want_url)) == 1 { return 1 } 22 gw(" (want missing; streams=[" as *u8); gw(streams); gw("])\n" as *u8); return 0 23} 24 25func main() -> i64 { 26 gw("media-state SOVEREIGN gate (window.__NUXT__ / ytInitialPlayerResponse -> stream URLs, no bundle exec)\n" as *u8) 27 var pass: i64 = 0 28 var ttl: i64 = 0 29 30 // MS1: Nuxt initial state (hanime shape) -- streams array with an m3u8. 31 ttl=ttl+1; pass=pass+grow("MS1 __NUXT__ -> master.m3u8\x00" as *u8, 32 run_ms("<html><body><script>window.__NUXT__={\"video\":{\"title\":\"Test\",\"streams\":[{\"quality\":\"1080p\",\"url\":\"https://cdn.example.com/v1/master.m3u8\"}]}}</script></body></html>" as *u8, 33 "__NUXT__\x00" as *u8, "https://cdn.example.com/v1/master.m3u8\x00" as *u8)) 34 // MS2: YouTube-shape player response -> direct mp4. 35 ttl=ttl+1; pass=pass+grow("MS2 ytInitialPlayerResponse -> .mp4\x00" as *u8, 36 run_ms("<script>var ytInitialPlayerResponse = {\"streamingData\":{\"formats\":[{\"itag\":22,\"url\":\"https://rr1.googlevideo.com/v.mp4\"}]}};</script>" as *u8, 37 "ytInitialPlayerResponse\x00" as *u8, "https://rr1.googlevideo.com/v.mp4\x00" as *u8)) 38 // MS3: JSON-escaped `\/` in the URL -> unescaped. 39 ttl=ttl+1; pass=pass+grow("MS3 escaped \\/ -> https://cdn/x.mp4\x00" as *u8, 40 run_ms("<script>window.__NUXT__={\"url\":\"https:\\/\\/cdn\\/x.mp4\"}</script>" as *u8, 41 "__NUXT__\x00" as *u8, "https://cdn/x.mp4\x00" as *u8)) 42 // MS4: braces INSIDE a JSON string must not break the balanced scan. 43 ttl=ttl+1; pass=pass+grow("MS4 nested {braces} in a string -> still extracts y.m3u8\x00" as *u8, 44 run_ms("<script>window.__NUXT__={\"title\":\"a{b}c[d]\",\"url\":\"https://cdn/y.m3u8\"}</script>" as *u8, 45 "__NUXT__\x00" as *u8, "https://cdn/y.m3u8\x00" as *u8)) 46 // MS5: __INITIAL_STATE__ (Vue/Vuex shape). 47 ttl=ttl+1; pass=pass+grow("MS5 __INITIAL_STATE__ -> z.m3u8\x00" as *u8, 48 run_ms("<script>window.__INITIAL_STATE__={\"media\":{\"src\":\"https://cdn/z.m3u8\"}}</script>" as *u8, 49 "__INITIAL_STATE__\x00" as *u8, "https://cdn/z.m3u8\x00" as *u8)) 50 // MS6: no such marker -> 0 (no false extraction). 51 ttl=ttl+1 52 let nb: *u8 = sys_mmap(4096) 53 let no: *u8 = "<html><body>no state here, just text</body></html>" as *u8 54 let ne: i64 = ms_extract_state(no, gsl(no), "__NUXT__\x00" as *u8, nb, 4096) 55 pass=pass+grow("MS6 no marker -> 0\x00" as *u8, (ne == 0) as i64) 56 57 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 58 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 59 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 60 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 61 let ctr__dry: *i64 = gv_ctr() 62 ctr__dry[0] = pass 63 ctr__dry[1] = ttl 64 let rc__dry: i64 = gv_verdict("MEDIA-STATE-GATE" as *u8, ctr__dry, "embedded-state extraction: __NUXT__/player-response -> stream URLs)" as *u8) 65 sys_exit(rc__dry) 66 return rc__dry 67}