code wiki / _hdl_build / nx_media_state_gate.nx

nx_media_state_gate.nx source

↩ module page · 59 lines · 3893 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" 8 9func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 10" as *u8); return ok } 11func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 12 13func run_ms(html: *u8, marker: *u8, want_url: *u8) -> i64 { 14 let blob: *u8 = sys_mmap(262144) 15 let bl: i64 = ms_extract_state(html, gsl(html), marker, blob, 262144) 16 if bl == 0 { gw(" (no state blob)\n" as *u8); return 0 } 17 let streams: *u8 = sys_mmap(8192) 18 let n: i64 = ms_find_streams(blob, bl, streams, 8192) 19 if n == 0 { gw(" (0 streams in blob=[" as *u8); gw(blob); gw("])\n" as *u8); return 0 } 20 if ms_contains(streams, gsl(streams), want_url, gsl(want_url)) == 1 { return 1 } 21 gw(" (want missing; streams=[" as *u8); gw(streams); gw("])\n" as *u8); return 0 22} 23 24func main() -> i64 { 25 gw("media-state SOVEREIGN gate (window.__NUXT__ / ytInitialPlayerResponse -> stream URLs, no bundle exec)\n" as *u8) 26 var pass: i64 = 0 27 var ttl: i64 = 0 28 29 // MS1: Nuxt initial state (hanime shape) -- streams array with an m3u8. 30 ttl=ttl+1; pass=pass+grow("MS1 __NUXT__ -> master.m3u8\x00" as *u8, 31 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, 32 "__NUXT__\x00" as *u8, "https://cdn.example.com/v1/master.m3u8\x00" as *u8)) 33 // MS2: YouTube-shape player response -> direct mp4. 34 ttl=ttl+1; pass=pass+grow("MS2 ytInitialPlayerResponse -> .mp4\x00" as *u8, 35 run_ms("<script>var ytInitialPlayerResponse = {\"streamingData\":{\"formats\":[{\"itag\":22,\"url\":\"https://rr1.googlevideo.com/v.mp4\"}]}};</script>" as *u8, 36 "ytInitialPlayerResponse\x00" as *u8, "https://rr1.googlevideo.com/v.mp4\x00" as *u8)) 37 // MS3: JSON-escaped `\/` in the URL -> unescaped. 38 ttl=ttl+1; pass=pass+grow("MS3 escaped \\/ -> https://cdn/x.mp4\x00" as *u8, 39 run_ms("<script>window.__NUXT__={\"url\":\"https:\\/\\/cdn\\/x.mp4\"}</script>" as *u8, 40 "__NUXT__\x00" as *u8, "https://cdn/x.mp4\x00" as *u8)) 41 // MS4: braces INSIDE a JSON string must not break the balanced scan. 42 ttl=ttl+1; pass=pass+grow("MS4 nested {braces} in a string -> still extracts y.m3u8\x00" as *u8, 43 run_ms("<script>window.__NUXT__={\"title\":\"a{b}c[d]\",\"url\":\"https://cdn/y.m3u8\"}</script>" as *u8, 44 "__NUXT__\x00" as *u8, "https://cdn/y.m3u8\x00" as *u8)) 45 // MS5: __INITIAL_STATE__ (Vue/Vuex shape). 46 ttl=ttl+1; pass=pass+grow("MS5 __INITIAL_STATE__ -> z.m3u8\x00" as *u8, 47 run_ms("<script>window.__INITIAL_STATE__={\"media\":{\"src\":\"https://cdn/z.m3u8\"}}</script>" as *u8, 48 "__INITIAL_STATE__\x00" as *u8, "https://cdn/z.m3u8\x00" as *u8)) 49 // MS6: no such marker -> 0 (no false extraction). 50 ttl=ttl+1 51 let nb: *u8 = sys_mmap(4096) 52 let no: *u8 = "<html><body>no state here, just text</body></html>" as *u8 53 let ne: i64 = ms_extract_state(no, gsl(no), "__NUXT__\x00" as *u8, nb, 4096) 54 pass=pass+grow("MS6 no marker -> 0\x00" as *u8, (ne == 0) as i64) 55 56 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 57 if pass == ttl { gw("verdict=GREEN (embedded-state extraction: __NUXT__/player-response -> stream URLs)\n" as *u8); sys_exit(0); return 0 } 58 gw("verdict=RED\n" as *u8); sys_exit(1); return 1 59}