code wiki / _hdl_build / nx_clean_serve_daemon.nx
nx_clean_serve_daemon.nx source
↩ module page · 301 lines · 26319 B
1// nx_clean_serve_daemon.nx -- the LIVE, testable face of the ethical clean-serve engine. The operator (or the
2// search engine) points it at ANY site; it fetches the page over our own sovereign TLS, runs the nx_clean_serve
3// policy (neutralize every attack vector, KEEP ads as inert static creatives, deliver the content + native
4// HTML5 media), and serves the safe clean view with a SAFETY RECEIPT banner. This is the win-win-win, live:
5// USER safe (no active content) · SITE monetizes (safe static ads survive) · SEARCH neutral (served, not deranked).
6// Self-contained (its own file -- no collision with the concurrently-edited nx_archive_daemon). Bind 0.0.0.0 so
7// WSL2/edge can reach it. Run from nxc2 root (certdata is relative). Endpoints:
8// / home form (enter a URL)
9// /clean?url= fetch + clean-serve + safe view (receipt banner + native <video> + cleaned body)
10// /receipt?url= the machine-readable safety receipt (application/json) -- for API testing
11// license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_url_safety_lib.nx" // the ONE SSRF url ruler (us_url_safe / us_reason)
14import "nx_http_server.nx"
15import "nx_x509_trust_store.nx"
16import "nx_trust_store_load_from_certdata.nx"
17import "nx_https_fetch_follow.nx"
18import "nx_media_signal.nx" // xt_find (via imports)
19import "nx_web_filter.nx" // wf_new/wf_seed (the block/allow data table)
20import "nx_clean_serve.nx" // cs_clean_page / cs_rc_new / cs_receipt_json / CR_* / cs_slen / cs_url_delim etc.
21import "nx_kvs.nx" // kvs_extract -- de-obfuscate KVS/kt_player video_url (the huge tube-site family) into the clean view
22const CSD_MAGIC_33554432: i64 = 33554432
23const CSD_MAGIC_8388608: i64 = 8388608
24const CSD_MAGIC_1048576: i64 = 1048576
25const CSD_MAGIC_4096: i64 = 4096
26const CSD_MAGIC_1024: i64 = 1024
27const CSD_MAGIC_16384: i64 = 16384
28const CSD_MAGIC_8192: i64 = 8192
29const CSD_MAGIC_8320: i64 = 8320
30const CSD_MAGIC_65536: i64 = 65536
31const CSD_MAGIC_8102: i64 = 8102
32const CSD_MAGIC_4194304: i64 = 4194304
33const CSD_LEN_CELL: i64 = 16 // one i64 length cell (plus slack) for sys_read_file, and the empty skip-table sentinel
34const CSD_TOKEN_CAP: i64 = 128 // the page identity token buffer (a slug or an id; longer segments are not ids)
35
36func csd_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 }
37func csd_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 }
38func csd_datoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while (s[i]&0xff)>=48 { if (s[i]&0xff)>57 { i=i } else { v=v*10+((s[i]&0xff)-48); i=i+1 } if (s[i]&0xff)<48 { i=i } } return v }
39func csd_hexval(c: i64) -> i64 { if c>=48 { if c<=57 { return c-48 } } if c>=97 { if c<=102 { return c-87 } } if c>=65 { if c<=70 { return c-55 } } return 0 }
40func csd_dstarts(p: *u8, plen: i64, pre: *u8) -> i64 { var i: i64=0; while pre[i]!=(0 as u8) { if i>=plen { return 0 } if (p[i]&0xff)!=(pre[i]&0xff) { return 0 } i=i+1 } return 1 }
41func csd_esc(dst: *u8, off: i64, s: *u8, sl: i64) -> i64 {
42 var i: i64=0; while i<sl { let c: i64=s[i]&0xff
43 if c==60 { off=csd_put(dst,off,"<" as *u8) } else { if c==62 { off=csd_put(dst,off,">" as *u8) } else { if c==38 { off=csd_put(dst,off,"&" as *u8) } else { if c==34 { off=csd_put(dst,off,""" as *u8) } else { dst[off]=c as u8; off=off+1 } } } } i=i+1 }
44 return off
45}
46// parse ?url= (boundary-aware) + percent-decode
47func csd_query_url(q: *u8, qlen: i64, out: *u8, cap: i64) -> i64 {
48 var start: i64 = 0-1; var si: i64=0
49 while si<qlen { var isp: i64=0; if si==0 { isp=1 } else { let pc: i64=q[si-1]&0xff; if pc==38 { isp=1 } if pc==63 { isp=1 } }
50 if isp==1 { if (si+4)<=qlen { if (q[si]&0xff)==117 { if (q[si+1]&0xff)==114 { if (q[si+2]&0xff)==108 { if (q[si+3]&0xff)==61 { start=si; si=qlen } } } } } } si=si+1 }
51 if start<0 { out[0]=0 as u8; return 0 }
52 var i: i64=start+4; var o: i64=0; var go: i64=1
53 while go==1 { if i>=qlen { go=0 } else { let c: i64=q[i]&0xff
54 if c==38 { go=0 } else { if c==37 { if (i+2)<qlen { let hi: i64=csd_hexval(q[i+1]&0xff); let lo: i64=csd_hexval(q[i+2]&0xff); if o<(cap-1) { out[o]=(hi*16+lo) as u8; o=o+1 } i=i+3 } else { i=i+1 } } else { if c==43 { if o<(cap-1){out[o]=32 as u8;o=o+1} i=i+1 } else { if o<(cap-1){out[o]=c as u8;o=o+1} i=i+1 } } } } }
55 out[o]=0 as u8; return o
56}
57// body offset of a fetch result. DELEGATES to the ONE entity-body ruler in nx_clean_serve (cs_entity_body_off): the
58// fetcher already returns the entity body, so the unconditional blank-CRLF-line scan that used to live here was a SECOND
59// header strip that ate the player block of every CRLF-formatted page (camwhores.tv, measured 2026-09-02 -- see the ruler).
60func csd_body_off(buf: *u8, n: i64) -> i64 { return cs_entity_body_off(buf, n) }
61func csd_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 }
62func send_html(cfd: i64, body: *u8, blen: i64) -> i64 {
63 let resp: *u8 = sys_mmap(blen + 512)
64 var o: i64 = csd_put(resp, 0, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: " as *u8)
65 o = csd_putn(resp, o, blen); o = csd_put(resp, o, "\r\nX-Nishi-Clean-Serve: safe\r\nConnection: close\r\n\r\n" as *u8)
66 var i: i64=0; while i<blen { resp[o]=body[i]; o=o+1; i=i+1 }
67 csd_write_all(cfd, resp, o); sys_close(cfd); return 0
68}
69func send_json(cfd: i64, body: *u8, blen: i64) -> i64 {
70 let resp: *u8 = sys_mmap(blen + 512)
71 var o: i64 = csd_put(resp, 0, "HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\nAccess-Control-Allow-Origin: *\r\nContent-Length: " as *u8)
72 o = csd_putn(resp, o, blen); o = csd_put(resp, o, "\r\nConnection: close\r\n\r\n" as *u8)
73 var i: i64=0; while i<blen { resp[o]=body[i]; o=o+1; i=i+1 }
74 csd_write_all(cfd, resp, o); sys_close(cfd); return 0
75}
76func csd_head(page: *u8, o: i64) -> i64 {
77 return csd_put(page, o, "<!doctype html><meta charset=utf-8><meta name=referrer content=no-referrer><title>Nishi Clean-Serve</title><style>body{font-family:system-ui,sans-serif;max-width:1000px;margin:0 auto;padding:1rem;background:#0f1115;color:#e8e8ea;line-height:1.55}h1{margin:.2rem 0}.sub{color:#9aa;font-size:.92rem}.card{background:#171a21;border:1px solid #2a2f3a;border-radius:10px;padding:1rem 1.2rem;margin:1rem 0}input[type=text]{background:#0c0e12;color:#eee;border:1px solid #333;border-radius:6px;padding:.55rem;width:64%}button{background:#2b6cff;color:#fff;border:0;border-radius:6px;padding:.55rem 1rem;cursor:pointer}a{color:#7ab7ff}code{color:#bcd;word-break:break-all;font-size:.85rem}video{width:100%;max-height:70vh;background:#000;border-radius:8px}img{max-width:100%;height:auto}.shield{background:#12331d;border:1px solid #1f6b39;color:#c7f0d4;border-radius:8px;padding:.55rem .8rem;margin-bottom:1rem;font-size:.9rem}.shield b{color:#7ef0a6}.win{display:flex;gap:.6rem;flex-wrap:wrap;margin:.4rem 0}.win span{background:#0c0e12;border:1px solid #2a2f3a;border-radius:6px;padding:.2rem .5rem;font-size:.8rem;color:#bcd}</style>" as *u8)
78}
79// the safety-receipt banner (the win-win-win, made visible)
80func csd_banner(page: *u8, o: i64, rc: *i64) -> i64 {
81 o = csd_put(page, o, "<div class=shield><b>🛡 Nishi clean-serve — you are protected.</b> Neutralized " as *u8)
82 o = csd_putn(page, o, rc[CR_SCRIPTS]); o = csd_put(page, o, " scripts, " as *u8)
83 o = csd_putn(page, o, rc[CR_TRACK]); o = csd_put(page, o, " trackers, " as *u8)
84 o = csd_putn(page, o, rc[CR_MINER]); o = csd_put(page, o, " miners, " as *u8)
85 o = csd_putn(page, o, rc[CR_POPUP]); o = csd_put(page, o, " popunders, " as *u8)
86 o = csd_putn(page, o, rc[CR_REDIRECTS]); o = csd_put(page, o, " redirect vectors, " as *u8)
87 o = csd_putn(page, o, rc[CR_IFRAMES]); o = csd_put(page, o, " hostile frames, " as *u8)
88 o = csd_putn(page, o, rc[CR_ADVIDEO]); o = csd_put(page, o, " ad videos · preserved " as *u8)
89 o = csd_putn(page, o, rc[CR_ADS_KEPT]); o = csd_put(page, o, " safe ad(s) so the site still earns · content delivered natively.</div>" as *u8)
90 return o
91}
92func csd_home(page: *u8) -> i64 {
93 var o: i64 = csd_head(page, 0)
94 o = csd_put(page, o, "<h1>🧭 Nishi Clean-Serve</h1><div class=sub>Neutral, safe delivery for the open web. Enter any site — we fetch it over our own sovereign TLS, strip every attack (scripts, trackers, miners, popunders, redirects, hostile frames), keep the site's safe static ads so it still earns, and play the media in native HTML5. A win for you, the site, and a neutral search engine.</div>" as *u8)
95 o = csd_put(page, o, "<div class=win><span>🛡 user: no scripts, no attacks</span><span>💰 site: safe ads survive</span><span>⚖ search: served, not deranked</span></div>" as *u8)
96 o = csd_put(page, o, "<div class=card><form action=/clean><input type=text name=url placeholder=\"https://example.com/article\" autofocus> <button>Clean-serve it</button></form><div class=sub style=margin-top:.5rem>Or get the raw safety receipt: <code>/receipt?url=<url></code></div></div>" as *u8)
97 o = csd_put(page, o, "<div class=card><h2 style=margin-top:0>Clean player</h2><div class=sub>A legal test stream, served bare (the video, nothing around it):</div><video controls preload=metadata src=\"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4\"></video></div>" as *u8)
98 return o
99}
100// --- SSRF GUARD: a public fetch proxy MUST refuse private/loopback/internal targets (or it is itself an attack
101// vector -- the opposite of this tool's purpose). Conservative allow-list of schemes + block-list of host ranges. ---
102func csd_lc2(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
103func csd_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 csd_lc2(s[i]&0xff)!=csd_lc2(p[i]&0xff) { return 0 } i=i+1 } return 1 }
104// MIGRATED 2026-09-01 to the ONE SSRF ruler. This body used to be a private copy of the rules, byte-for-byte
105// identical to ncv_url_safe in nx_cleanview.nx -- two copies of a SECURITY decision that agreed only because
106// one was pasted from the other, so the next edit to either would silently stop reaching the other surface.
107// The rules now live in nx_url_safety_lib (us_reason / us_url_safe), which additionally NAMES which rule
108// refused, and its gate carries the positive controls this estate learned to demand after four SSRF
109// deny-tests once passed against a guard that was refusing everything.
110// Signature and return contract unchanged (1 = safe, 0 = refuse), so every call site is untouched.
111func csd_url_safe(url: *u8, ulen: i64) -> i64 {
112 return us_url_safe(url, ulen)
113}
114// does a URL look like a directly-playable stream? (lightweight, self-contained -- avoids a heavy extractor dep)
115func csd_media_ext(url: *u8, ul: i64) -> i64 { return cs_media_ext(url, ul) }
116// scan the delivered HTML for the first native stream URL so "the site just works" in a bare HTML5 <video>
117// CS9: csd_media_rank -- the declared-metadata-first, skip-table-ranked media resolver (the watch symbol for rung CS9).
118// Order: schema.org / OpenGraph declaration (must itself be media) -> first literal stream URL that is not a preview,
119// thumbnail, ad or preroll clip (cs_media_skip, table from knowledge/cleanserve_media_skip.conf). 0 when nothing survives,
120// which is HONEST: a wrong player (hornyleak.tv served a related-video preview clip, 2026-09-02) is worse than none.
121func csd_media_rank(html: *u8, hn: i64, out: *u8, cap: i64, skip: *u8, url: *u8, ulen: i64) -> i64 {
122 var n: i64 = cs_find_declared(html, hn, out, cap)
123 if n > 0 { if csd_media_ext(out, n) == 1 { if cs_media_skip(out, n, skip) == 0 { return n } } }
124 // CS9c: the page's own id token first (a KVS id or a VK-style id reappears in the stream URL, a related grid never carries it)
125 let tok: *u8 = sys_mmap(CSD_TOKEN_CAP)
126 let tl: i64 = cs_page_token(url, ulen, tok, CSD_TOKEN_CAP)
127 if tl > 0 { n = csd_find_media(html, hn, out, cap, skip, tok, 0); if n > 0 { return n } }
128 let none: *u8 = sys_mmap(CSD_LEN_CELL); none[0] = 0 as u8
129 let distinct: i64 = csd_find_media(html, hn, out, cap, skip, none, 1)
130 if distinct >= CS_LISTING_MIN { out[0]=0 as u8; return 0 } // a grid of related streams: guessing one is garbage, say nothing
131 return csd_find_media(html, hn, out, cap, skip, none, 0)
132}
133// csd_find_media: literal stream candidates in document order, skip-table filtered. want (may be empty) restricts hits to
134// candidates carrying the page token; count_only=1 returns the number of distinct surviving candidates (capped at
135// CS_LISTING_MIN) instead of a URL, so the ranker can tell a listing grid from a page with one stream.
136func csd_find_media(html: *u8, hn: i64, out: *u8, cap: i64, skip: *u8, want: *u8, count_only: i64) -> i64 {
137 var found: i64 = 0
138 var i: i64 = 0
139 while i < hn {
140 var hit: i64 = 0
141 if csd_dstarts(((html as i64)+i) as *u8, hn-i, "https://" as *u8) == 1 { hit=1 }
142 if hit==0 { if csd_dstarts(((html as i64)+i) as *u8, hn-i, "http://" as *u8) == 1 { hit=1 } }
143 if hit==1 {
144 var k: i64=0; var j: i64=i; var go: i64=1
145 while go==1 { if j>=hn { go=0 } else { let c: i64=html[j]&0xff; if cs_url_delim(c)==1 { go=0 } else { if k<(cap-1) { out[k]=c as u8; k=k+1 } j=j+1 } } }
146 out[k]=0 as u8
147 if csd_media_ext(out, k) == 1 { if cs_media_skip(out, k, skip) == 0 {
148 var ok: i64 = 1
149 if want[0] != (0 as u8) { if cs_has_ci(out, k, want) == 0 { ok = 0 } }
150 if ok == 1 { if count_only == 0 { return k } found = found + 1; if found >= CS_LISTING_MIN { return found } }
151 } }
152 i=j
153 } else { i=i+1 }
154 }
155 out[0]=0 as u8; return found
156}
157// fetch + clean-serve one URL into a full HTML page; returns page length
158func csd_clean(cfd: i64, t: *i64, store: *TrustStore, url: *u8, ulen: i64, skip: *u8) -> i64 {
159 let page: *u8 = sys_mmap(CSD_MAGIC_33554432) // 32 MiB assembled page
160 var o: i64 = csd_head(page, 0)
161 // CS9: a URL that is itself media is played bare. Fetching a 57 MB mp4 as a page filled the fetch buffer and reported
162 // Fetch failed (cdn.semyana.website, measured 2026-09-02); there is nothing to neutralize in a media file.
163 if csd_media_ext(url, ulen) == 1 { if csd_url_safe(url, ulen) == 1 {
164 o = csd_put(page, o, "<div class=shield><b>🛡 Nishi clean-serve</b> · direct media URL · served bare in a native player, no page fetched, nothing to neutralize.</div><video controls autoplay playsinline webkit-playsinline src=\"" as *u8)
165 o = csd_esc(page, o, url, ulen); o = csd_put(page, o, "\"></video><div class=sub>Direct media: <code>" as *u8); o = csd_esc(page, o, url, ulen); o = csd_put(page, o, "</code></div>" as *u8)
166 page[o]=0 as u8; send_html(cfd, page, o); return o
167 } }
168 if csd_url_safe(url, ulen) == 0 {
169 o = csd_put(page, o, "<div class=shield style=\"background:#331212;border-color:#6b1f1f;color:#f0c7c7\"><b>Blocked by SSRF guard</b> — clean-serve only fetches public http(s) URLs; private / loopback / internal targets are refused. <a href=/>← back</a></div>" as *u8)
170 send_html(cfd, page, o); return o
171 }
172 if (store as i64) <= 0 {
173 o = csd_put(page, o, "<div class=shield style=\"background:#331212;border-color:#6b1f1f;color:#f0c7c7\"><b>Trust store unavailable</b> — certdata not found on the host; clean-serve cannot fetch over TLS yet. <a href=/>← back</a></div>" as *u8)
174 send_html(cfd, page, o); return o
175 }
176 let cap: i64 = CSD_MAGIC_8388608
177 let raw: *u8 = sys_mmap(cap)
178 let status: *i64 = sys_mmap(8) as *i64
179 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
180 if hn <= 0 {
181 o = csd_put(page, o, "<div class=card><b>Fetch failed</b> (status " as *u8); o = csd_putn(page, o, status[0]); o = csd_put(page, o, ") — the site may block non-browser clients or be a hard host (Cloudflare JS/TLS challenge). <a href=/>← try another</a></div>" as *u8)
182 send_html(cfd, page, o); return o
183 }
184 let boff: i64 = csd_body_off(raw, hn)
185 let body: *u8 = ((raw as i64)+boff) as *u8
186 let blen: i64 = hn - boff
187 // run the ethical clean-serve policy (scan attacks + transform)
188 let rc: *i64 = cs_rc_new()
189 let cleaned: *u8 = sys_mmap(blen + CSD_MAGIC_1048576)
190 let clen: i64 = cs_clean_page(body, blen, t, cleaned, blen + CSD_MAGIC_1048576, rc)
191 // find a native stream so "the site just works" in a bare HTML5 player
192 let media: *u8 = sys_mmap(CSD_MAGIC_4096)
193 var mlen: i64 = kvs_extract(body, blen, media, CSD_MAGIC_4096) // KVS/kt_player de-obfuscation (tube-site family) first
194 if mlen > 0 { if cs_media_skip(media, mlen, skip) == 1 { mlen = 0 } } // never a preview/ad clip, even from a KVS field
195 if mlen == 0 { mlen = csd_media_rank(body, blen, media, CSD_MAGIC_4096, skip, url, ulen) } // CS9: declared metadata, page-token match, then ranked literal streams
196 // assemble: receipt banner + (native player) + source line + cleaned body
197 o = csd_banner(page, o, rc)
198 if mlen > 0 { o = csd_put(page, o, "<video controls autoplay playsinline webkit-playsinline src=\"" as *u8); var mi: i64=0; while mi<mlen { page[o]=media[mi]; o=o+1; mi=mi+1 } o = csd_put(page, o, "\"></video>" as *u8) }
199 o = csd_put(page, o, "<div class=sub>Clean view of <code>" as *u8); o = csd_esc(page, o, url, ulen); o = csd_put(page, o, "</code> · <a href=/receipt?url=" as *u8)
200 var ui: i64=0; while ui<ulen { let c: i64=url[ui]&0xff; if c==38 { o=csd_put(page,o,"%26" as *u8) } else { page[o]=c as u8; o=o+1 } ui=ui+1 }
201 o = csd_put(page, o, ">safety receipt (JSON)</a></div><hr style=border-color:#2a2f3a>" as *u8)
202 // append the cleaned body (bounded to the page buffer; reserve tail room for the gate-surrogate style)
203 var room: i64 = CSD_MAGIC_33554432 - o - 900
204 var bl: i64 = clen; if bl > room { bl = room }
205 var ci: i64=0; while ci<bl { page[o]=cleaned[ci]; o=o+1; ci=ci+1 }
206 // gate-surrogate: neutralize client-side age/consent OVERLAYS (they cover content already present in the HTML;
207 // our zero-JS view means the site's own JS never hides them, so we do). Injected AFTER the body so it wins
208 // source-order ties; high-specificity selectors + scroll-restore. NSFW is a legal adult choice, not an attack.
209 o = csd_put(page, o, "<style>html body [class*=\"age-gate\" i],html body [id*=\"age-gate\" i],html body [class*=\"agegate\" i],html body [id*=\"agegate\" i],html body [class*=\"age_gate\" i],html body [id*=\"entrance_terms\" i],html body [class*=\"entrance_terms\" i],html body [class*=\"age-verif\" i],html body [class*=\"ageverif\" i],html body [class*=\"disclaimer-modal\" i]{display:none}html,body{overflow:auto;height:auto;position:static}</style>" as *u8)
210 page[o]=0 as u8
211 send_html(cfd, page, o); return o
212}
213func csd_receipt(cfd: i64, t: *i64, store: *TrustStore, url: *u8, ulen: i64) -> i64 {
214 if csd_url_safe(url, ulen) == 0 { send_json(cfd, "{\"safe\":false,\"error\":\"blocked-ssrf-guard\"}" as *u8, 43); return 0 }
215 if (store as i64) <= 0 { send_json(cfd, "{\"safe\":false,\"error\":\"trust-store-unavailable\"}" as *u8, 48); return 0 }
216 let cap: i64 = CSD_MAGIC_8388608
217 let raw: *u8 = sys_mmap(cap); let status: *i64 = sys_mmap(8) as *i64
218 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status)
219 let out: *u8 = sys_mmap(CSD_MAGIC_1024)
220 if hn <= 0 { var o: i64 = csd_put(out, 0, "{\"safe\":false,\"error\":\"fetch-failed\",\"status\":" as *u8); o = csd_putn(out, o, status[0]); o = csd_put(out, o, "}" as *u8); send_json(cfd, out, o); return o }
221 let boff: i64 = csd_body_off(raw, hn); let body: *u8 = ((raw as i64)+boff) as *u8; let blen: i64 = hn - boff
222 let rc: *i64 = cs_rc_new()
223 let sink: *u8 = sys_mmap(blen + CSD_MAGIC_1048576)
224 cs_clean_page(body, blen, t, sink, blen + CSD_MAGIC_1048576, rc)
225 let o: i64 = cs_receipt_json(rc, out, CSD_MAGIC_1024)
226 send_json(cfd, out, o); return o
227}
228// Per-request watchdog (seconds): the forked request-child arms sys_alarm; a page that takes longer than this is
229// killed by SIGALRM so a pathologically-slow page can never hang the child, leak its buffers, or pile up procs.
230// Generous -- a healthy fetch+clean is well under a second; only a degenerate page reaches it.
231const CSD_REQ_TIMEOUT_SECS: i64 = 25
232// normalize a user-typed URL: accept a bare host ("chaturbate.com", "www.site.tv/x") and default it to https://,
233// so the operator does not have to paste the full long-form URL. http(s):// URLs pass through unchanged.
234func csd_norm_url(inp: *u8, inlen: i64, out: *u8, cap: i64) -> i64 {
235 if csd_pfx_ci(inp, inlen, "http://" as *u8)==1 { var a: i64=0; while a<inlen { if a<(cap-1) { out[a]=inp[a] } a=a+1 } out[inlen]=0 as u8; return inlen }
236 if csd_pfx_ci(inp, inlen, "https://" as *u8)==1 { var a: i64=0; while a<inlen { if a<(cap-1) { out[a]=inp[a] } a=a+1 } out[inlen]=0 as u8; return inlen }
237 var o: i64 = csd_put(out, 0, "https://" as *u8)
238 var i: i64=0; while i<inlen { if o<(cap-1) { out[o]=inp[i]; o=o+1 } i=i+1 }
239 out[o]=0 as u8; return o
240}
241// handle ONE accepted connection: parse the request + dispatch. Run in a forked child so a slow upstream fetch
242// (anti-bot Chrome-JA3 handshakes) cannot block the accept loop, the / health endpoint, or other users.
243// the media skip table (knowledge/cleanserve_media_skip.conf, one substring per line) read ONCE at startup; an absent
244// or empty file yields an empty buffer and cs_media_skip then applies its bootstrap rows (announced on stdout).
245func csd_skip_load() -> *u8 {
246 let ln: *i64 = sys_mmap(CSD_LEN_CELL) as *i64
247 let b: *u8 = sys_read_file("knowledge/cleanserve_media_skip.conf" as *u8, ln)
248 if (b as i64) != 0 { if ln[0] > 0 { let out: *u8 = sys_mmap(ln[0] + 1); var i: i64 = 0; while i < ln[0] { out[i] = b[i]; i = i + 1 } out[ln[0]] = 0 as u8; sys_write(1, "media-skip: conf loaded\n" as *u8, 24); return out } }
249 let e: *u8 = sys_mmap(CSD_LEN_CELL); e[0] = 0 as u8; sys_write(1, "media-skip: bootstrap rows\n" as *u8, 27); return e
250}
251func csd_handle_conn(cfd: i64, t: *i64, store: *TrustStore, skip: *u8) -> i64 {
252 let req: *u8 = sys_mmap(CSD_MAGIC_16384)
253 let om: *i64=sys_mmap(8) as *i64; let opo: *i64=sys_mmap(8) as *i64; let opl: *i64=sys_mmap(8) as *i64; let ocl: *i64=sys_mmap(8) as *i64; let obo: *i64=sys_mmap(8) as *i64; let orn: *i64=sys_mmap(8) as *i64
254 let rc: i64 = nx_http_server_read_request(cfd, req, CSD_MAGIC_16384, om, opo, opl, ocl, obo, orn)
255 if rc == NXS_OK {
256 let path: *u8 = ((req as i64)+opo[0]) as *u8
257 let plen: i64 = opl[0]
258 if csd_dstarts(path, plen, "/clean" as *u8) == 1 {
259 let ub: *u8 = sys_mmap(CSD_MAGIC_8192); let un: i64 = csd_query_url(path, plen, ub, CSD_MAGIC_8192)
260 if un > 0 { let nb: *u8 = sys_mmap(CSD_MAGIC_8320); let nn: i64 = csd_norm_url(ub, un, nb, CSD_MAGIC_8320); csd_clean(cfd, t, store, nb, nn, skip) } else { let p: *u8 = sys_mmap(CSD_MAGIC_65536); let n: i64 = csd_home(p); send_html(cfd, p, n) }
261 } else {
262 if csd_dstarts(path, plen, "/receipt" as *u8) == 1 {
263 let ub: *u8 = sys_mmap(CSD_MAGIC_8192); let un: i64 = csd_query_url(path, plen, ub, CSD_MAGIC_8192)
264 if un > 0 { let nb: *u8 = sys_mmap(CSD_MAGIC_8320); let nn: i64 = csd_norm_url(ub, un, nb, CSD_MAGIC_8320); csd_receipt(cfd, t, store, nb, nn) } else { send_json(cfd, "{\"error\":\"missing url\"}" as *u8, 22) }
265 } else {
266 let p: *u8 = sys_mmap(CSD_MAGIC_65536); let n: i64 = csd_home(p); send_html(cfd, p, n)
267 }
268 }
269 }
270 return 0
271}
272func main(argc: i64, argv: *i64) -> i64 {
273 var port: i64 = CSD_MAGIC_8102
274 if argc >= 2 { port = csd_datoi(argv[1] as *u8) }
275 var r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, CSD_MAGIC_4194304)
276 if r <= 0 { r = nx_trust_store_load_from_certdata("/volume1/homes/elderwesto/nishihost/data/mozilla_certdata.txt" as *u8, 512, CSD_MAGIC_4194304) }
277 let store: *TrustStore = r as *TrustStore // may be <=0 if certdata absent -> /clean self-reports; the server STILL binds (stays healthy + diagnosable instead of crash-looping)
278 let t: *i64 = wf_new(); wf_seed(t)
279 let skip: *u8 = csd_skip_load()
280 let addr: *u8 = sys_mmap(16); nx_http_server_addr_any(addr, port)
281 let vb: *i64 = sys_mmap(8) as *i64
282 var lfd: i64 = nx_http_server_listen(addr, 64, vb)
283 var ltries: i64 = 0
284 while lfd < 0 { if ltries >= 10 { sys_write(2, "CLEANSERVE listen failed\n" as *u8, 25); sys_exit(1); return 1 } sys_sleep_ms(1000); lfd = nx_http_server_listen(addr, 64, vb); ltries = ltries + 1 }
285 let banner: *u8 = sys_mmap(96); var bo: i64 = csd_put(banner, 0, "nx_clean_serve_daemon: http://localhost:" as *u8); bo = csd_putn(banner, bo, port); bo = csd_put(banner, bo, "/\n" as *u8); sys_write(1, banner, bo)
286 var running: i64 = 1
287 while running == 1 {
288 let cfd: i64 = nx_http_server_accept_one(lfd, vb)
289 if cfd >= 0 {
290 // FORK-PER-REQUEST: a child handles this connection (incl. any slow anti-bot fetch) then exits, so the
291 // accept loop + the / health endpoint stay responsive and one slow/wedged fetch can't stall the daemon.
292 let pid: i64 = sys_fork()
293 if pid == 0 { sys_alarm(CSD_REQ_TIMEOUT_SECS); csd_handle_conn(cfd, t, store, skip); sys_close(cfd); sys_exit(0) }
294 if pid < 0 { csd_handle_conn(cfd, t, store, skip) } // fork unavailable -> synchronous fallback (no drop)
295 sys_close(cfd)
296 var reaped: i64 = 1
297 while reaped > 0 { reaped = sys_wait4(0-1, 0 as *i64, 1) } // WNOHANG: reap finished children
298 }
299 }
300 return 0
301}