code wiki / _hdl_build / nx_video_sniff_page_gate.nx
nx_video_sniff_page_gate.nx source
↩ module page · 70 lines · 4363 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"
11import "nx_gate_verdict.nx"
12
13func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
14" as *u8); return ok }
15func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
16func plan_has(planbuf: *u8, want: *u8) -> i64 { return mjx_contains(planbuf, gsl(planbuf), want, gsl(want)) }
17
18func run_p(html: *u8, want_kind: i64, want_sub: *u8) -> i64 {
19 let planbuf: *u8 = sys_mmap(2560)
20 let k: i64 = vs_sniff_page(html, gsl(html), planbuf, 2560, 2)
21 if k != want_kind { gw(" (kind=" as *u8); gn(k); gw(" plan=[" as *u8); gw(planbuf); gw("])\n" as *u8); return 0 }
22 if plan_has(planbuf, want_sub) == 0 { gw(" (plan=[" as *u8); gw(planbuf); gw("])\n" as *u8); return 0 }
23 return 1
24}
25
26func main() -> i64 {
27 gw("video-sniff PAGE gate (HTML -> extract inline scripts -> guarded sniff -> download plan)\n" as *u8)
28 var pass: i64 = 0
29 var ttl: i64 = 0
30
31 // P1: inline script builds the HLS master at runtime.
32 ttl=ttl+1; pass=pass+grow("P1 inline script -> route=hls\x00" as *u8,
33 run_p("<html><body><script>function m(id){return 'https://cdn/'+id+'/master.m3u8';} fetch(m('P1'));</script></body></html>" as *u8,
34 VSK_HLS, "https://cdn/P1/master.m3u8\x00" as *u8))
35 // P2: inline direct .mp4.
36 ttl=ttl+1; pass=pass+grow("P2 inline direct .mp4 -> route=direct\x00" as *u8,
37 run_p("<script>fetch('https://cdn/'+'clip'+'.mp4');</script>" as *u8,
38 VSK_DIRECT, "https://cdn/clip.mp4\x00" as *u8))
39 // P3: CROSS-SCRIPT share -- config var in script #1, stream URL built in script #2 (concatenation works).
40 ttl=ttl+1; pass=pass+grow("P3 two scripts share a var -> route=hls\x00" as *u8,
41 run_p("<script>var vid='V3';</script><p>hi</p><script>fetch('https://cdn/'+vid+'/master.m3u8');</script>" as *u8,
42 VSK_HLS, "https://cdn/V3/master.m3u8\x00" as *u8))
43 // P4: external <script src> (empty inline body, skipped) + an inline script with the URL.
44 ttl=ttl+1; pass=pass+grow("P4 external src skipped, inline extracted -> route=hls\x00" as *u8,
45 run_p("<script src='https://cdn/bundle.js'></script><script>fetch('https://cdn/live/master.m3u8');</script>" as *u8,
46 VSK_HLS, "https://cdn/live/master.m3u8\x00" as *u8))
47 // P5: HOSTILE inline script -> guard-killed (bounded, no hang).
48 ttl=ttl+1; pass=pass+grow("P5 hostile inline while(true) -> guard-killed\x00" as *u8,
49 run_p("<script>var x=0;while(true){x=x+1;}</script>" as *u8,
50 VSK_KILLED, "guard-killed\x00" as *u8))
51 // P6: case-insensitive tag <SCRIPT>.
52 ttl=ttl+1; pass=pass+grow("P6 <SCRIPT> uppercase tag -> route=hls\x00" as *u8,
53 run_p("<SCRIPT>fetch('https://cdn/UP/master.m3u8');</SCRIPT>" as *u8,
54 VSK_HLS, "https://cdn/UP/master.m3u8\x00" as *u8))
55 // P7: no scripts at all -> route=none (transparent, quiet).
56 ttl=ttl+1; pass=pass+grow("P7 no scripts -> route=none\x00" as *u8,
57 run_p("<html><body><p>no scripts here</p></body></html>" as *u8,
58 VSK_NONE, "ROUTE=none\x00" as *u8))
59
60 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8)
61 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
62 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
63 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
64 let ctr__dry: *i64 = gv_ctr()
65 ctr__dry[0] = pass
66 ctr__dry[1] = ttl
67 let rc__dry: i64 = gv_verdict("VIDEO-SNIFF-PAGE-GATE" as *u8, ctr__dry, "page HTML -> extract inline scripts -> guarded sniff -> ranked download route)" as *u8)
68 sys_exit(rc__dry)
69 return rc__dry
70}