code wiki / _hdl_build / nx_cleanview.nx
nx_cleanview.nx source
↩ module page · 155 lines · 10925 B
1// nx_cleanview.nx -- one-shot CLI face of the ethical clean-serve engine, exposed as an MCP tool (nx_cleanview).
2// TWO modes:
3// nx_cleanview <url> -> the machine-readable SAFETY RECEIPT JSON (attacks neutralized + safe ads kept).
4// nx_cleanview resolve <url> -> F108d: sovereign GUARDED JS-EXEC media resolution. Fetches the page over the
5// SAME _best (Chrome-JA3) path clean-serve uses, then runs the page's inline JS
6// through the guarded sniff (fork + CPU/mem cap, NO network in the child) to
7// surface the runtime-constructed stream URL (VSK_HLS/DIRECT). This is the
8// _best-HTML-passthrough integration design (do NOT re-fetch with a weaker path).
9// External-<script src> _best-fetch enrichment = the next rung.
10// Shares the daemon's exact clean core (cs_clean_page / cs_receipt_json) so the receipt can NEVER drift from /receipt.
11// SSRF-guarded by construction (public http(s) only). Read-only, no persistent state, safe to fork-exec per call.
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_url_safety_lib.nx" // the ONE SSRF url ruler (us_url_safe / us_reason)
15import "nx_x509_trust_store.nx"
16import "nx_trust_store_load_from_certdata.nx"
17import "nx_https_fetch_follow.nx"
18import "nx_web_filter.nx" // wf_new / wf_seed (the block/allow data table)
19import "nx_clean_serve.nx" // cs_rc_new / cs_clean_page / cs_receipt_json / CR_*
20import "nx_video_sniff.nx" // vs_sniff_page_bundled (GUARDED) / vs_result_json / vs_extract_script_srcs / VSK_*
21import "nx_video_sniff_fetch.nx" // nx_hls_resolve_uri (relative-URL resolver) for external-bundle _best-fetch
22const NCV_MAGIC_8388608: i64 = 8388608
23const NCV_MAGIC_8192: i64 = 8192
24const NCV_MAGIC_8191: i64 = 8191
25const NCV_MAGIC_2560: i64 = 2560
26const NCV_MAGIC_65536: i64 = 65536
27const NCV_MAGIC_4194304: i64 = 4194304
28const NCV_MAGIC_1048576: i64 = 1048576
29const NCV_MAGIC_4000: i64 = 4000
30
31// -- minimal local helpers (mirror the daemon's csd_* / the core's cs_* idioms; kept local = self-contained) --
32func ncv_put(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off]=s[i];off=off+1;i=i+1} return off }
33func ncv_putn(dst: *u8, off: i64, v: i64) -> i64 { var m: i64=v; if m==0 { dst[off]=48 as u8; return off+1 } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var j: i64=0; while j<k { dst[off]=t[k-1-j]; off=off+1; j=j+1 } return off }
34func ncv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
35func ncv_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i] { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 }
36func ncv_lc2(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
37func ncv_pfx_ci(s: *u8, sl: i64, p: *u8) -> i64 { var i: i64=0; while p[i]!=(0 as u8) { if i>=sl { return 0 } if ncv_lc2(s[i]&0xff)!=ncv_lc2(p[i]&0xff) { return 0 } i=i+1 } return 1 }
38func ncv_write_all(fd: i64, buf: *u8, n: i64) -> i64 { var off: i64=0; while off<n { let w: i64=sys_write(fd, ((buf as i64)+off) as *u8, n-off); if w<=0 { return off } off=off+w } return off }
39// body offset of a fetch result. DELEGATES to the ONE entity-body ruler in nx_clean_serve (cs_entity_body_off) -- the
40// same second-header-strip defect the daemon carried (a CRLF page lost everything before its first blank line, measured
41// 2026-09-02 on camwhores.tv); fixed in one place for both surfaces.
42func ncv_body_off(buf: *u8, n: i64) -> i64 { return cs_entity_body_off(buf, n) }
43// SSRF guard: a public fetch proxy MUST refuse private/loopback/internal (mirrors csd_url_safe exactly)
44// MIGRATED 2026-09-01 to the ONE SSRF ruler (see the same note in nx_clean_serve_daemon.nx). This body was a
45// byte-for-byte twin of csd_url_safe; the rules now live once, in nx_url_safety_lib, which also names WHICH
46// rule refused. Signature and return contract unchanged (1 = safe, 0 = refuse).
47func ncv_url_safe(url: *u8, ulen: i64) -> i64 {
48 return us_url_safe(url, ulen)
49}
50// F108d: guarded JS-exec media resolve on the _best-fetched HTML + external <script src> bundles (all fetched
51// over clean-serve's strong _best path -- the design proven this session; solves the standalone sniff's weak fetch).
52const NCV_SNIFF_CPU_SECS: i64 = 5
53const NCV_SRCCAP: i64 = 65536 // buffer for the extracted <script src> list
54const NCV_EXTCAP: i64 = 2097152 // concatenated external-bundle JS (2 MiB)
55const NCV_BUNCAP: i64 = 1048576 // one bundle response (1 MiB)
56const NCV_MAXSRC: i64 = 8 // fetch at most 8 external bundles (bounded; declared)
57func ncv_resolve(out: *u8, url: *u8, ulen: i64, store: *TrustStore) -> i64 {
58 let cap: i64 = NCV_MAGIC_8388608
59 let raw: *u8 = sys_mmap(cap)
60 let status: *i64 = sys_mmap(8) as *i64
61 // fetch over the SAME _best (Chrome-JA3) path clean-serve uses -- solves the weaker-fetch failure of the standalone sniff
62 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
63 if hn <= 0 { var o: i64 = ncv_put(out, 0, "{\"url\":\"" as *u8); o = ncv_put(out, o, url); o = ncv_put(out, o, "\",\"route\":\"fetch-failed\",\"status\":" as *u8); o = ncv_putn(out, o, status[0]); o = ncv_put(out, o, "}\n" as *u8); ncv_write_all(1, out, o); return 0 }
64 let boff: i64 = ncv_body_off(raw, hn)
65 let body: *u8 = ((raw as i64)+boff) as *u8
66 let blen: i64 = hn - boff
67 // EXTERNAL-BUNDLE ENRICHMENT: extract the page's <script src>, fetch each over _best, concat into extjs, so a
68 // framework player living in an external bundle can be run by the guarded sniff (not just inline scripts).
69 let srcs: *u8 = sys_mmap(NCV_SRCCAP)
70 let nsrc: i64 = vs_extract_script_srcs(body, blen, srcs, NCV_SRCCAP)
71 let extjs: *u8 = sys_mmap(NCV_EXTCAP)
72 let bundle: *u8 = sys_mmap(NCV_BUNCAP)
73 let absurl: *u8 = sys_mmap(NCV_MAGIC_8192)
74 let line: *u8 = sys_mmap(NCV_MAGIC_8192)
75 let bstatus: *i64 = sys_mmap(8) as *i64
76 var extlen: i64 = 0
77 var fetched: i64 = 0
78 var li: i64 = 0
79 var walking: i64 = 1
80 while walking == 1 {
81 if fetched >= NCV_MAXSRC { walking = 0 } else {
82 if (srcs[li] & 0xff) == 0 { walking = 0 } else {
83 var q: i64 = 0
84 var inl: i64 = 1
85 while inl == 1 {
86 let c: i64 = srcs[li] & 0xff
87 if c == 0 { inl = 0 } else { if c == 10 { inl = 0; li = li + 1 } else { if q < NCV_MAGIC_8191 { line[q] = c as u8; q = q + 1 } li = li + 1 } }
88 }
89 line[q] = 0 as u8
90 if q > 0 {
91 let rn: i64 = nx_hls_resolve_uri(url, ulen, line, q, absurl, NCV_MAGIC_8192)
92 if rn > 0 {
93 absurl[rn] = 0 as u8
94 let bn: i64 = nx_https_fetch_follow_best(absurl, store, bundle, NCV_BUNCAP, 6, bstatus)
95 if bn > 0 {
96 let bbo: i64 = ncv_body_off(bundle, bn) // skip the bundle's HTTP headers -> JS body only
97 var bp: i64 = bbo
98 while bp < bn { if extlen < (NCV_EXTCAP - 3) { extjs[extlen] = bundle[bp]; extlen = extlen + 1 } bp = bp + 1 }
99 if extlen < (NCV_EXTCAP - 3) { extjs[extlen] = 59 as u8; extlen = extlen + 1; extjs[extlen] = 10 as u8; extlen = extlen + 1 } // ';\n' separator
100 fetched = fetched + 1
101 }
102 }
103 }
104 }
105 }
106 }
107 extjs[extlen] = 0 as u8
108 // GUARDED JS-exec (fork + CPU/mem cap, no network in the child) on the page HTML + the _best-fetched external bundles.
109 let plan: *u8 = sys_mmap(NCV_MAGIC_2560)
110 let kind: i64 = vs_sniff_page_bundled(body, blen, extjs, extlen, plan, NCV_MAGIC_2560, NCV_SNIFF_CPU_SECS)
111 let jb: *u8 = sys_mmap(NCV_MAGIC_2560)
112 vs_result_json(kind, plan, jb, NCV_MAGIC_2560)
113 var o: i64 = ncv_put(out, 0, "{\"url\":\"" as *u8); o = ncv_put(out, o, url)
114 o = ncv_put(out, o, "\",\"js_media_resolve\":" as *u8); o = ncv_put(out, o, jb)
115 o = ncv_put(out, o, ",\"engine\":\"sovereign guarded JS-VM (fork+CPU/mem-cap, no-network child)\",\"scope\":\"page + up to " as *u8); o = ncv_putn(out, o, NCV_MAXSRC)
116 o = ncv_put(out, o, " external <script src> bundles, all fetched over _best (Chrome-JA3); bundles_fetched=" as *u8); o = ncv_putn(out, o, fetched)
117 o = ncv_put(out, o, "\"}\n" as *u8)
118 ncv_write_all(1, out, o)
119 return 0
120}
121func main(argc: i64, argv: *i64) -> i64 {
122 let out: *u8 = sys_mmap(NCV_MAGIC_65536)
123 if argc < 2 { let o: i64 = ncv_put(out, 0, "{\"error\":\"usage: nx_cleanview <url> | nx_cleanview resolve <url>\"}\n" as *u8); ncv_write_all(1, out, o); return 2 }
124 var mode_resolve: i64 = 0
125 var url: *u8 = argv[1] as *u8
126 if ncv_streq(argv[1] as *u8, "resolve" as *u8) == 1 {
127 if argc < 3 { let o: i64 = ncv_put(out, 0, "{\"error\":\"usage: nx_cleanview resolve <url>\"}\n" as *u8); ncv_write_all(1, out, o); return 2 }
128 mode_resolve = 1
129 url = argv[2] as *u8
130 }
131 let ulen: i64 = ncv_slen(url)
132 if ncv_url_safe(url, ulen) == 0 { let o: i64 = ncv_put(out, 0, "{\"safe\":false,\"error\":\"blocked-ssrf-guard\"}\n" as *u8); ncv_write_all(1, out, o); return 0 }
133 var r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, NCV_MAGIC_4194304)
134 if r <= 0 { r = nx_trust_store_load_from_certdata("/volume1/homes/elderwesto/nishihost/data/mozilla_certdata.txt" as *u8, 512, NCV_MAGIC_4194304) }
135 if r <= 0 { let o: i64 = ncv_put(out, 0, "{\"safe\":false,\"error\":\"trust-store-unavailable\"}\n" as *u8); ncv_write_all(1, out, o); return 0 }
136 let store: *TrustStore = r as *TrustStore
137 if mode_resolve == 1 { ncv_resolve(out, url, ulen, store); return 0 }
138 // -- receipt mode (UNCHANGED: byte-identical behavior for the live MCP registration) --
139 let t: *i64 = wf_new(); wf_seed(t)
140 let cap: i64 = NCV_MAGIC_8388608
141 let raw: *u8 = sys_mmap(cap)
142 let status: *i64 = sys_mmap(8) as *i64
143 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
144 if hn <= 0 { var o: i64 = ncv_put(out, 0, "{\"safe\":false,\"error\":\"fetch-failed\",\"status\":" as *u8); o = ncv_putn(out, o, status[0]); o = ncv_put(out, o, "}\n" as *u8); ncv_write_all(1, out, o); return 0 }
145 let boff: i64 = ncv_body_off(raw, hn)
146 let body: *u8 = ((raw as i64)+boff) as *u8
147 let blen: i64 = hn - boff
148 let rc: *i64 = cs_rc_new()
149 let sink: *u8 = sys_mmap(blen + NCV_MAGIC_1048576)
150 cs_clean_page(body, blen, t, sink, blen + NCV_MAGIC_1048576, rc)
151 let o: i64 = cs_receipt_json(rc, out, NCV_MAGIC_4000)
152 out[o] = 10 as u8
153 ncv_write_all(1, out, o+1)
154 return 0
155}