code wiki / _hdl_build / nx_cleanview.nx
nx_cleanview.nx source
↩ module page · 169 lines · 11644 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_x509_trust_store.nx"
15import "nx_trust_store_load_from_certdata.nx"
16import "nx_https_fetch_follow.nx"
17import "nx_web_filter.nx" // wf_new / wf_seed (the block/allow data table)
18import "nx_clean_serve.nx" // cs_rc_new / cs_clean_page / cs_receipt_json / CR_*
19import "nx_video_sniff.nx" // vs_sniff_page_bundled (GUARDED) / vs_result_json / vs_extract_script_srcs / VSK_*
20import "nx_video_sniff_fetch.nx" // nx_hls_resolve_uri (relative-URL resolver) for external-bundle _best-fetch
21const NCV_MAGIC_8388608: i64 = 8388608
22const NCV_MAGIC_8192: i64 = 8192
23const NCV_MAGIC_8191: i64 = 8191
24const NCV_MAGIC_2560: i64 = 2560
25const NCV_MAGIC_65536: i64 = 65536
26const NCV_MAGIC_4194304: i64 = 4194304
27const NCV_MAGIC_1048576: i64 = 1048576
28const NCV_MAGIC_4000: i64 = 4000
29
30// -- minimal local helpers (mirror the daemon's csd_* / the core's cs_* idioms; kept local = self-contained) --
31func 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 }
32func 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 }
33func ncv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
34func 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 }
35func ncv_lc2(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
36func 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 }
37func 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 }
38// find the response body offset (past the header block); 0 if none
39func ncv_body_off(buf: *u8, n: i64) -> i64 {
40 var i: i64=0; while i+3 < n { if (buf[i]&0xff)==13 { if (buf[i+1]&0xff)==10 { if (buf[i+2]&0xff)==13 { if (buf[i+3]&0xff)==10 { return i+4 } } } } i=i+1 }
41 return 0
42}
43// SSRF guard: a public fetch proxy MUST refuse private/loopback/internal (mirrors csd_url_safe exactly)
44func ncv_url_safe(url: *u8, ulen: i64) -> i64 {
45 var sl: i64 = 0
46 if ncv_pfx_ci(url, ulen, "https://" as *u8)==1 { sl=8 } else { if ncv_pfx_ci(url, ulen, "http://" as *u8)==1 { sl=7 } else { return 0 } }
47 let host: *u8 = sys_mmap(512); var h: i64=0; var i: i64=sl
48 while i<ulen { let c: i64=url[i]&0xff; if c==47 { i=ulen } else { if c==58 { i=ulen } else { if c==63 { i=ulen } else { if c==64 { h=0; i=i+1 } else { if h<511 { host[h]=c as u8; h=h+1 } i=i+1 } } } } }
49 host[h]=0 as u8
50 if h==0 { return 0 }
51 if ncv_pfx_ci(host, h, "localhost" as *u8)==1 { return 0 }
52 if ncv_pfx_ci(host, h, "127." as *u8)==1 { return 0 }
53 if ncv_pfx_ci(host, h, "10." as *u8)==1 { return 0 }
54 if ncv_pfx_ci(host, h, "192.168." as *u8)==1 { return 0 }
55 if ncv_pfx_ci(host, h, "169.254." as *u8)==1 { return 0 }
56 if ncv_pfx_ci(host, h, "172." as *u8)==1 { return 0 }
57 if ncv_pfx_ci(host, h, "0." as *u8)==1 { return 0 }
58 if ncv_pfx_ci(host, h, "[" as *u8)==1 { return 0 }
59 if h>6 { if ncv_pfx_ci(((host as i64)+h-6) as *u8, 6, ".local" as *u8)==1 { return 0 } }
60 if h>9 { if ncv_pfx_ci(((host as i64)+h-9) as *u8, 9, ".internal" as *u8)==1 { return 0 } }
61 if h>4 { if ncv_pfx_ci(((host as i64)+h-4) as *u8, 4, ".lan" as *u8)==1 { return 0 } }
62 return 1
63}
64// F108d: guarded JS-exec media resolve on the _best-fetched HTML + external <script src> bundles (all fetched
65// over clean-serve's strong _best path -- the design proven this session; solves the standalone sniff's weak fetch).
66const NCV_SNIFF_CPU_SECS: i64 = 5
67const NCV_SRCCAP: i64 = 65536 // buffer for the extracted <script src> list
68const NCV_EXTCAP: i64 = 2097152 // concatenated external-bundle JS (2 MiB)
69const NCV_BUNCAP: i64 = 1048576 // one bundle response (1 MiB)
70const NCV_MAXSRC: i64 = 8 // fetch at most 8 external bundles (bounded; declared)
71func ncv_resolve(out: *u8, url: *u8, ulen: i64, store: *TrustStore) -> i64 {
72 let cap: i64 = NCV_MAGIC_8388608
73 let raw: *u8 = sys_mmap(cap)
74 let status: *i64 = sys_mmap(8) as *i64
75 // fetch over the SAME _best (Chrome-JA3) path clean-serve uses -- solves the weaker-fetch failure of the standalone sniff
76 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
77 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 }
78 let boff: i64 = ncv_body_off(raw, hn)
79 let body: *u8 = ((raw as i64)+boff) as *u8
80 let blen: i64 = hn - boff
81 // EXTERNAL-BUNDLE ENRICHMENT: extract the page's <script src>, fetch each over _best, concat into extjs, so a
82 // framework player living in an external bundle can be run by the guarded sniff (not just inline scripts).
83 let srcs: *u8 = sys_mmap(NCV_SRCCAP)
84 let nsrc: i64 = vs_extract_script_srcs(body, blen, srcs, NCV_SRCCAP)
85 let extjs: *u8 = sys_mmap(NCV_EXTCAP)
86 let bundle: *u8 = sys_mmap(NCV_BUNCAP)
87 let absurl: *u8 = sys_mmap(NCV_MAGIC_8192)
88 let line: *u8 = sys_mmap(NCV_MAGIC_8192)
89 let bstatus: *i64 = sys_mmap(8) as *i64
90 var extlen: i64 = 0
91 var fetched: i64 = 0
92 var li: i64 = 0
93 var walking: i64 = 1
94 while walking == 1 {
95 if fetched >= NCV_MAXSRC { walking = 0 } else {
96 if (srcs[li] & 0xff) == 0 { walking = 0 } else {
97 var q: i64 = 0
98 var inl: i64 = 1
99 while inl == 1 {
100 let c: i64 = srcs[li] & 0xff
101 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 } }
102 }
103 line[q] = 0 as u8
104 if q > 0 {
105 let rn: i64 = nx_hls_resolve_uri(url, ulen, line, q, absurl, NCV_MAGIC_8192)
106 if rn > 0 {
107 absurl[rn] = 0 as u8
108 let bn: i64 = nx_https_fetch_follow_best(absurl, store, bundle, NCV_BUNCAP, 6, bstatus)
109 if bn > 0 {
110 let bbo: i64 = ncv_body_off(bundle, bn) // skip the bundle's HTTP headers -> JS body only
111 var bp: i64 = bbo
112 while bp < bn { if extlen < (NCV_EXTCAP - 3) { extjs[extlen] = bundle[bp]; extlen = extlen + 1 } bp = bp + 1 }
113 if extlen < (NCV_EXTCAP - 3) { extjs[extlen] = 59 as u8; extlen = extlen + 1; extjs[extlen] = 10 as u8; extlen = extlen + 1 } // ';\n' separator
114 fetched = fetched + 1
115 }
116 }
117 }
118 }
119 }
120 }
121 extjs[extlen] = 0 as u8
122 // GUARDED JS-exec (fork + CPU/mem cap, no network in the child) on the page HTML + the _best-fetched external bundles.
123 let plan: *u8 = sys_mmap(NCV_MAGIC_2560)
124 let kind: i64 = vs_sniff_page_bundled(body, blen, extjs, extlen, plan, NCV_MAGIC_2560, NCV_SNIFF_CPU_SECS)
125 let jb: *u8 = sys_mmap(NCV_MAGIC_2560)
126 vs_result_json(kind, plan, jb, NCV_MAGIC_2560)
127 var o: i64 = ncv_put(out, 0, "{\"url\":\"" as *u8); o = ncv_put(out, o, url)
128 o = ncv_put(out, o, "\",\"js_media_resolve\":" as *u8); o = ncv_put(out, o, jb)
129 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)
130 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)
131 o = ncv_put(out, o, "\"}\n" as *u8)
132 ncv_write_all(1, out, o)
133 return 0
134}
135func main(argc: i64, argv: *i64) -> i64 {
136 let out: *u8 = sys_mmap(NCV_MAGIC_65536)
137 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 }
138 var mode_resolve: i64 = 0
139 var url: *u8 = argv[1] as *u8
140 if ncv_streq(argv[1] as *u8, "resolve" as *u8) == 1 {
141 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 }
142 mode_resolve = 1
143 url = argv[2] as *u8
144 }
145 let ulen: i64 = ncv_slen(url)
146 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 }
147 var r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, NCV_MAGIC_4194304)
148 if r <= 0 { r = nx_trust_store_load_from_certdata("/volume1/homes/elderwesto/nishihost/data/mozilla_certdata.txt" as *u8, 512, NCV_MAGIC_4194304) }
149 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 }
150 let store: *TrustStore = r as *TrustStore
151 if mode_resolve == 1 { ncv_resolve(out, url, ulen, store); return 0 }
152 // -- receipt mode (UNCHANGED: byte-identical behavior for the live MCP registration) --
153 let t: *i64 = wf_new(); wf_seed(t)
154 let cap: i64 = NCV_MAGIC_8388608
155 let raw: *u8 = sys_mmap(cap)
156 let status: *i64 = sys_mmap(8) as *i64
157 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
158 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 }
159 let boff: i64 = ncv_body_off(raw, hn)
160 let body: *u8 = ((raw as i64)+boff) as *u8
161 let blen: i64 = hn - boff
162 let rc: *i64 = cs_rc_new()
163 let sink: *u8 = sys_mmap(blen + NCV_MAGIC_1048576)
164 cs_clean_page(body, blen, t, sink, blen + NCV_MAGIC_1048576, rc)
165 let o: i64 = cs_receipt_json(rc, out, NCV_MAGIC_4000)
166 out[o] = 10 as u8
167 ncv_write_all(1, out, o+1)
168 return 0
169}