code wiki / _hdl_build / nx_clean_serve_daemon.nx

nx_clean_serve_daemon.nx source

↩ module page · 272 lines · 22654 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_http_server.nx" 14import "nx_x509_trust_store.nx" 15import "nx_trust_store_load_from_certdata.nx" 16import "nx_https_fetch_follow.nx" 17import "nx_media_signal.nx" // xt_find (via imports) 18import "nx_web_filter.nx" // wf_new/wf_seed (the block/allow data table) 19import "nx_clean_serve.nx" // cs_clean_page / cs_rc_new / cs_receipt_json / CR_* / cs_slen / cs_url_delim etc. 20import "nx_kvs.nx" // kvs_extract -- de-obfuscate KVS/kt_player video_url (the huge tube-site family) into the clean view 21const CSD_MAGIC_33554432: i64 = 33554432 22const CSD_MAGIC_8388608: i64 = 8388608 23const CSD_MAGIC_1048576: i64 = 1048576 24const CSD_MAGIC_4096: i64 = 4096 25const CSD_MAGIC_1024: i64 = 1024 26const CSD_MAGIC_16384: i64 = 16384 27const CSD_MAGIC_8192: i64 = 8192 28const CSD_MAGIC_8320: i64 = 8320 29const CSD_MAGIC_65536: i64 = 65536 30const CSD_MAGIC_8102: i64 = 8102 31const CSD_MAGIC_4194304: i64 = 4194304 32 33func 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 } 34func 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 } 35func 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 } 36func 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 } 37func 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 } 38func csd_esc(dst: *u8, off: i64, s: *u8, sl: i64) -> i64 { 39 var i: i64=0; while i<sl { let c: i64=s[i]&0xff 40 if c==60 { off=csd_put(dst,off,"&lt;" as *u8) } else { if c==62 { off=csd_put(dst,off,"&gt;" as *u8) } else { if c==38 { off=csd_put(dst,off,"&amp;" as *u8) } else { if c==34 { off=csd_put(dst,off,"&quot;" as *u8) } else { dst[off]=c as u8; off=off+1 } } } } i=i+1 } 41 return off 42} 43// parse ?url= (boundary-aware) + percent-decode 44func csd_query_url(q: *u8, qlen: i64, out: *u8, cap: i64) -> i64 { 45 var start: i64 = 0-1; var si: i64=0 46 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 } } 47 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 } 48 if start<0 { out[0]=0 as u8; return 0 } 49 var i: i64=start+4; var o: i64=0; var go: i64=1 50 while go==1 { if i>=qlen { go=0 } else { let c: i64=q[i]&0xff 51 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 } } } } } 52 out[o]=0 as u8; return o 53} 54// find the response body offset (past the header block); 0 if no header block seen 55func csd_body_off(buf: *u8, n: i64) -> i64 { 56 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 } 57 return 0 58} 59func 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 } 60func send_html(cfd: i64, body: *u8, blen: i64) -> i64 { 61 let resp: *u8 = sys_mmap(blen + 512) 62 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) 63 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) 64 var i: i64=0; while i<blen { resp[o]=body[i]; o=o+1; i=i+1 } 65 csd_write_all(cfd, resp, o); sys_close(cfd); return 0 66} 67func send_json(cfd: i64, body: *u8, blen: i64) -> i64 { 68 let resp: *u8 = sys_mmap(blen + 512) 69 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) 70 o = csd_putn(resp, o, blen); o = csd_put(resp, o, "\r\nConnection: close\r\n\r\n" as *u8) 71 var i: i64=0; while i<blen { resp[o]=body[i]; o=o+1; i=i+1 } 72 csd_write_all(cfd, resp, o); sys_close(cfd); return 0 73} 74func csd_head(page: *u8, o: i64) -> i64 { 75 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) 76} 77// the safety-receipt banner (the win-win-win, made visible) 78func csd_banner(page: *u8, o: i64, rc: *i64) -> i64 { 79 o = csd_put(page, o, "<div class=shield><b>&#128737; Nishi clean-serve &mdash; you are protected.</b> Neutralized " as *u8) 80 o = csd_putn(page, o, rc[CR_SCRIPTS]); o = csd_put(page, o, " scripts, " as *u8) 81 o = csd_putn(page, o, rc[CR_TRACK]); o = csd_put(page, o, " trackers, " as *u8) 82 o = csd_putn(page, o, rc[CR_MINER]); o = csd_put(page, o, " miners, " as *u8) 83 o = csd_putn(page, o, rc[CR_POPUP]); o = csd_put(page, o, " popunders, " as *u8) 84 o = csd_putn(page, o, rc[CR_REDIRECTS]); o = csd_put(page, o, " redirect vectors, " as *u8) 85 o = csd_putn(page, o, rc[CR_IFRAMES]); o = csd_put(page, o, " hostile frames &middot; preserved " as *u8) 86 o = csd_putn(page, o, rc[CR_ADS_KEPT]); o = csd_put(page, o, " safe ad(s) so the site still earns &middot; content delivered natively.</div>" as *u8) 87 return o 88} 89func csd_home(page: *u8) -> i64 { 90 var o: i64 = csd_head(page, 0) 91 o = csd_put(page, o, "<h1>&#129517; Nishi Clean-Serve</h1><div class=sub>Neutral, safe delivery for the open web. Enter any site &mdash; we fetch it over our own sovereign TLS, strip every attack (scripts, trackers, miners, popunders, redirects, hostile frames), keep the site&#39;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) 92 o = csd_put(page, o, "<div class=win><span>&#128737; user: no scripts, no attacks</span><span>&#128176; site: safe ads survive</span><span>&#9878; search: served, not deranked</span></div>" as *u8) 93 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=&lt;url&gt;</code></div></div>" as *u8) 94 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) 95 return o 96} 97// --- SSRF GUARD: a public fetch proxy MUST refuse private/loopback/internal targets (or it is itself an attack 98// vector -- the opposite of this tool's purpose). Conservative allow-list of schemes + block-list of host ranges. --- 99func csd_lc2(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c } 100func 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 } 101func csd_url_safe(url: *u8, ulen: i64) -> i64 { 102 var sl: i64 = 0 103 if csd_pfx_ci(url, ulen, "https://" as *u8)==1 { sl=8 } else { if csd_pfx_ci(url, ulen, "http://" as *u8)==1 { sl=7 } else { return 0 } } 104 let host: *u8 = sys_mmap(512); var h: i64=0; var i: i64=sl 105 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 } } } } } 106 host[h]=0 as u8 107 if h==0 { return 0 } 108 if csd_pfx_ci(host, h, "localhost" as *u8)==1 { return 0 } 109 if csd_pfx_ci(host, h, "127." as *u8)==1 { return 0 } 110 if csd_pfx_ci(host, h, "10." as *u8)==1 { return 0 } 111 if csd_pfx_ci(host, h, "192.168." as *u8)==1 { return 0 } 112 if csd_pfx_ci(host, h, "169.254." as *u8)==1 { return 0 } 113 if csd_pfx_ci(host, h, "172." as *u8)==1 { return 0 } // conservative: blocks all 172.x (incl the 16-31 private block) 114 if csd_pfx_ci(host, h, "0." as *u8)==1 { return 0 } 115 if csd_pfx_ci(host, h, "[" as *u8)==1 { return 0 } // any IPv6 literal -> block (loopback/ULA/link-local) 116 if h>6 { if csd_pfx_ci(((host as i64)+h-6) as *u8, 6, ".local" as *u8)==1 { return 0 } } 117 if h>9 { if csd_pfx_ci(((host as i64)+h-9) as *u8, 9, ".internal" as *u8)==1 { return 0 } } 118 if h>4 { if csd_pfx_ci(((host as i64)+h-4) as *u8, 4, ".lan" as *u8)==1 { return 0 } } 119 return 1 120} 121// does a URL look like a directly-playable stream? (lightweight, self-contained -- avoids a heavy extractor dep) 122func csd_media_ext(url: *u8, ul: i64) -> i64 { 123 if xt_find(url, ul, ".m3u8" as *u8, 5, 0) >= 0 { return 1 } 124 if xt_find(url, ul, ".mp4" as *u8, 4, 0) >= 0 { return 1 } 125 if xt_find(url, ul, ".webm" as *u8, 5, 0) >= 0 { return 1 } 126 return 0 127} 128// scan the delivered HTML for the first native stream URL so "the site just works" in a bare HTML5 <video> 129func csd_find_media(html: *u8, hn: i64, out: *u8, cap: i64) -> i64 { 130 var i: i64 = 0 131 while i < hn { 132 var hit: i64 = 0 133 if csd_dstarts(((html as i64)+i) as *u8, hn-i, "https://" as *u8) == 1 { hit=1 } 134 if hit==0 { if csd_dstarts(((html as i64)+i) as *u8, hn-i, "http://" as *u8) == 1 { hit=1 } } 135 if hit==1 { 136 var k: i64=0; var j: i64=i; var go: i64=1 137 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 } } } 138 out[k]=0 as u8 139 if csd_media_ext(out, k) == 1 { return k } 140 i=j 141 } else { i=i+1 } 142 } 143 out[0]=0 as u8; return 0 144} 145// fetch + clean-serve one URL into a full HTML page; returns page length 146func csd_clean(cfd: i64, t: *i64, store: *TrustStore, url: *u8, ulen: i64) -> i64 { 147 let page: *u8 = sys_mmap(CSD_MAGIC_33554432) // 32 MiB assembled page 148 var o: i64 = csd_head(page, 0) 149 if csd_url_safe(url, ulen) == 0 { 150 o = csd_put(page, o, "<div class=shield style=\"background:#331212;border-color:#6b1f1f;color:#f0c7c7\"><b>Blocked by SSRF guard</b> &mdash; clean-serve only fetches public http(s) URLs; private / loopback / internal targets are refused. <a href=/>&larr; back</a></div>" as *u8) 151 send_html(cfd, page, o); return o 152 } 153 if (store as i64) <= 0 { 154 o = csd_put(page, o, "<div class=shield style=\"background:#331212;border-color:#6b1f1f;color:#f0c7c7\"><b>Trust store unavailable</b> &mdash; certdata not found on the host; clean-serve cannot fetch over TLS yet. <a href=/>&larr; back</a></div>" as *u8) 155 send_html(cfd, page, o); return o 156 } 157 let cap: i64 = CSD_MAGIC_8388608 158 let raw: *u8 = sys_mmap(cap) 159 let status: *i64 = sys_mmap(8) as *i64 160 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status) 161 if hn <= 0 { 162 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, ") &mdash; the site may block non-browser clients or be a hard host (Cloudflare JS/TLS challenge). <a href=/>&larr; try another</a></div>" as *u8) 163 send_html(cfd, page, o); return o 164 } 165 let boff: i64 = csd_body_off(raw, hn) 166 let body: *u8 = ((raw as i64)+boff) as *u8 167 let blen: i64 = hn - boff 168 // run the ethical clean-serve policy (scan attacks + transform) 169 let rc: *i64 = cs_rc_new() 170 let cleaned: *u8 = sys_mmap(blen + CSD_MAGIC_1048576) 171 let clen: i64 = cs_clean_page(body, blen, t, cleaned, blen + CSD_MAGIC_1048576, rc) 172 // find a native stream so "the site just works" in a bare HTML5 player 173 let media: *u8 = sys_mmap(CSD_MAGIC_4096) 174 var mlen: i64 = kvs_extract(body, blen, media, CSD_MAGIC_4096) // KVS/kt_player de-obfuscation (tube-site family) first 175 if mlen == 0 { mlen = csd_find_media(body, blen, media, CSD_MAGIC_4096) } // fallback: a direct stream URL already in the HTML 176 // assemble: receipt banner + (native player) + source line + cleaned body 177 o = csd_banner(page, o, rc) 178 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) } 179 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> &middot; <a href=/receipt?url=" as *u8) 180 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 } 181 o = csd_put(page, o, ">safety receipt (JSON)</a></div><hr style=border-color:#2a2f3a>" as *u8) 182 // append the cleaned body (bounded to the page buffer; reserve tail room for the gate-surrogate style) 183 var room: i64 = CSD_MAGIC_33554432 - o - 900 184 var bl: i64 = clen; if bl > room { bl = room } 185 var ci: i64=0; while ci<bl { page[o]=cleaned[ci]; o=o+1; ci=ci+1 } 186 // gate-surrogate: neutralize client-side age/consent OVERLAYS (they cover content already present in the HTML; 187 // our zero-JS view means the site's own JS never hides them, so we do). Injected AFTER the body so it wins 188 // source-order ties; high-specificity selectors + scroll-restore. NSFW is a legal adult choice, not an attack. 189 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) 190 page[o]=0 as u8 191 send_html(cfd, page, o); return o 192} 193func csd_receipt(cfd: i64, t: *i64, store: *TrustStore, url: *u8, ulen: i64) -> i64 { 194 if csd_url_safe(url, ulen) == 0 { send_json(cfd, "{\"safe\":false,\"error\":\"blocked-ssrf-guard\"}" as *u8, 43); return 0 } 195 if (store as i64) <= 0 { send_json(cfd, "{\"safe\":false,\"error\":\"trust-store-unavailable\"}" as *u8, 48); return 0 } 196 let cap: i64 = CSD_MAGIC_8388608 197 let raw: *u8 = sys_mmap(cap); let status: *i64 = sys_mmap(8) as *i64 198 let hn: i64 = nx_https_fetch_follow_best(url, store, raw, cap, 6, status) 199 let out: *u8 = sys_mmap(CSD_MAGIC_1024) 200 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 } 201 let boff: i64 = csd_body_off(raw, hn); let body: *u8 = ((raw as i64)+boff) as *u8; let blen: i64 = hn - boff 202 let rc: *i64 = cs_rc_new() 203 let sink: *u8 = sys_mmap(blen + CSD_MAGIC_1048576) 204 cs_clean_page(body, blen, t, sink, blen + CSD_MAGIC_1048576, rc) 205 let o: i64 = cs_receipt_json(rc, out, CSD_MAGIC_1024) 206 send_json(cfd, out, o); return o 207} 208// Per-request watchdog (seconds): the forked request-child arms sys_alarm; a page that takes longer than this is 209// killed by SIGALRM so a pathologically-slow page can never hang the child, leak its buffers, or pile up procs. 210// Generous -- a healthy fetch+clean is well under a second; only a degenerate page reaches it. 211const CSD_REQ_TIMEOUT_SECS: i64 = 25 212// normalize a user-typed URL: accept a bare host ("chaturbate.com", "www.site.tv/x") and default it to https://, 213// so the operator does not have to paste the full long-form URL. http(s):// URLs pass through unchanged. 214func csd_norm_url(inp: *u8, inlen: i64, out: *u8, cap: i64) -> i64 { 215 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 } 216 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 } 217 var o: i64 = csd_put(out, 0, "https://" as *u8) 218 var i: i64=0; while i<inlen { if o<(cap-1) { out[o]=inp[i]; o=o+1 } i=i+1 } 219 out[o]=0 as u8; return o 220} 221// handle ONE accepted connection: parse the request + dispatch. Run in a forked child so a slow upstream fetch 222// (anti-bot Chrome-JA3 handshakes) cannot block the accept loop, the / health endpoint, or other users. 223func csd_handle_conn(cfd: i64, t: *i64, store: *TrustStore) -> i64 { 224 let req: *u8 = sys_mmap(CSD_MAGIC_16384) 225 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 226 let rc: i64 = nx_http_server_read_request(cfd, req, CSD_MAGIC_16384, om, opo, opl, ocl, obo, orn) 227 if rc == NXS_OK { 228 let path: *u8 = ((req as i64)+opo[0]) as *u8 229 let plen: i64 = opl[0] 230 if csd_dstarts(path, plen, "/clean" as *u8) == 1 { 231 let ub: *u8 = sys_mmap(CSD_MAGIC_8192); let un: i64 = csd_query_url(path, plen, ub, CSD_MAGIC_8192) 232 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) } else { let p: *u8 = sys_mmap(CSD_MAGIC_65536); let n: i64 = csd_home(p); send_html(cfd, p, n) } 233 } else { 234 if csd_dstarts(path, plen, "/receipt" as *u8) == 1 { 235 let ub: *u8 = sys_mmap(CSD_MAGIC_8192); let un: i64 = csd_query_url(path, plen, ub, CSD_MAGIC_8192) 236 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) } 237 } else { 238 let p: *u8 = sys_mmap(CSD_MAGIC_65536); let n: i64 = csd_home(p); send_html(cfd, p, n) 239 } 240 } 241 } 242 return 0 243} 244func main(argc: i64, argv: *i64) -> i64 { 245 var port: i64 = CSD_MAGIC_8102 246 if argc >= 2 { port = csd_datoi(argv[1] as *u8) } 247 var r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, CSD_MAGIC_4194304) 248 if r <= 0 { r = nx_trust_store_load_from_certdata("/volume1/homes/elderwesto/nishihost/data/mozilla_certdata.txt" as *u8, 512, CSD_MAGIC_4194304) } 249 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) 250 let t: *i64 = wf_new(); wf_seed(t) 251 let addr: *u8 = sys_mmap(16); nx_http_server_addr_any(addr, port) 252 let vb: *i64 = sys_mmap(8) as *i64 253 var lfd: i64 = nx_http_server_listen(addr, 64, vb) 254 var ltries: i64 = 0 255 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 } 256 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) 257 var running: i64 = 1 258 while running == 1 { 259 let cfd: i64 = nx_http_server_accept_one(lfd, vb) 260 if cfd >= 0 { 261 // FORK-PER-REQUEST: a child handles this connection (incl. any slow anti-bot fetch) then exits, so the 262 // accept loop + the / health endpoint stay responsive and one slow/wedged fetch can't stall the daemon. 263 let pid: i64 = sys_fork() 264 if pid == 0 { sys_alarm(CSD_REQ_TIMEOUT_SECS); csd_handle_conn(cfd, t, store); sys_close(cfd); sys_exit(0) } 265 if pid < 0 { csd_handle_conn(cfd, t, store) } // fork unavailable -> synchronous fallback (no drop) 266 sys_close(cfd) 267 var reaped: i64 = 1 268 while reaped > 0 { reaped = sys_wait4(0-1, 0 as *i64, 1) } // WNOHANG: reap finished children 269 } 270 } 271 return 0 272}