code wiki / _hdl_build / nx_video_sniff_page_gate.nx
nx_video_sniff_page_gate.nx source
↩ module page · 62 lines · 3976 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_video_sniff_page_gate.nx -- proves PAGE-level sniff: HTML -> extract inline <script> bodies -> run
4// them (guarded) -> download plan. Covers cross-script variable sharing (config in one <script>, URL in
5// another), external <script src> skip, case-insensitive tags, and a hostile inline script (guard-killed).
6// This is the "paste a video-page, get the stream URL" pipeline (minus the live TLS fetch). license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_js_eval.nx"
9import "nx_media_jsexec.nx"
10import "nx_video_sniff.nx"
11
12func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
13" as *u8); return ok }
14func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
15func plan_has(planbuf: *u8, want: *u8) -> i64 { return mjx_contains(planbuf, gsl(planbuf), want, gsl(want)) }
16
17func run_p(html: *u8, want_kind: i64, want_sub: *u8) -> i64 {
18 let planbuf: *u8 = sys_mmap(2560)
19 let k: i64 = vs_sniff_page(html, gsl(html), planbuf, 2560, 2)
20 if k != want_kind { gw(" (kind=" as *u8); gn(k); gw(" plan=[" as *u8); gw(planbuf); gw("])\n" as *u8); return 0 }
21 if plan_has(planbuf, want_sub) == 0 { gw(" (plan=[" as *u8); gw(planbuf); gw("])\n" as *u8); return 0 }
22 return 1
23}
24
25func main() -> i64 {
26 gw("video-sniff PAGE gate (HTML -> extract inline scripts -> guarded sniff -> download plan)\n" as *u8)
27 var pass: i64 = 0
28 var ttl: i64 = 0
29
30 // P1: inline script builds the HLS master at runtime.
31 ttl=ttl+1; pass=pass+grow("P1 inline script -> route=hls\x00" as *u8,
32 run_p("<html><body><script>function m(id){return 'https://cdn/'+id+'/master.m3u8';} fetch(m('P1'));</script></body></html>" as *u8,
33 VSK_HLS, "https://cdn/P1/master.m3u8\x00" as *u8))
34 // P2: inline direct .mp4.
35 ttl=ttl+1; pass=pass+grow("P2 inline direct .mp4 -> route=direct\x00" as *u8,
36 run_p("<script>fetch('https://cdn/'+'clip'+'.mp4');</script>" as *u8,
37 VSK_DIRECT, "https://cdn/clip.mp4\x00" as *u8))
38 // P3: CROSS-SCRIPT share -- config var in script #1, stream URL built in script #2 (concatenation works).
39 ttl=ttl+1; pass=pass+grow("P3 two scripts share a var -> route=hls\x00" as *u8,
40 run_p("<script>var vid='V3';</script><p>hi</p><script>fetch('https://cdn/'+vid+'/master.m3u8');</script>" as *u8,
41 VSK_HLS, "https://cdn/V3/master.m3u8\x00" as *u8))
42 // P4: external <script src> (empty inline body, skipped) + an inline script with the URL.
43 ttl=ttl+1; pass=pass+grow("P4 external src skipped, inline extracted -> route=hls\x00" as *u8,
44 run_p("<script src='https://cdn/bundle.js'></script><script>fetch('https://cdn/live/master.m3u8');</script>" as *u8,
45 VSK_HLS, "https://cdn/live/master.m3u8\x00" as *u8))
46 // P5: HOSTILE inline script -> guard-killed (bounded, no hang).
47 ttl=ttl+1; pass=pass+grow("P5 hostile inline while(true) -> guard-killed\x00" as *u8,
48 run_p("<script>var x=0;while(true){x=x+1;}</script>" as *u8,
49 VSK_KILLED, "guard-killed\x00" as *u8))
50 // P6: case-insensitive tag <SCRIPT>.
51 ttl=ttl+1; pass=pass+grow("P6 <SCRIPT> uppercase tag -> route=hls\x00" as *u8,
52 run_p("<SCRIPT>fetch('https://cdn/UP/master.m3u8');</SCRIPT>" as *u8,
53 VSK_HLS, "https://cdn/UP/master.m3u8\x00" as *u8))
54 // P7: no scripts at all -> route=none (transparent, quiet).
55 ttl=ttl+1; pass=pass+grow("P7 no scripts -> route=none\x00" as *u8,
56 run_p("<html><body><p>no scripts here</p></body></html>" as *u8,
57 VSK_NONE, "ROUTE=none\x00" as *u8))
58
59 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8)
60 if pass == ttl { gw("verdict=GREEN (page HTML -> extract inline scripts -> guarded sniff -> ranked download route)\n" as *u8); sys_exit(0); return 0 }
61 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
62}