code wiki / _hdl_build / nx_archive_daemon.nx
nx_archive_daemon.nx source
↩ module page · 504 lines · 43955 B
1// nx_archive_daemon.nx -- the LIVE preservation service, expanded per operator: a TRUE archive (keeps the clean
2// PAGE and the MEDIA), rated so SFW lands on /media and NSFW on /gallery, with in-browser playback (Range-aware
3// so a 562MB film streams+seeks). Endpoints:
4// / home: preserve form (URL + SFW/NSFW) + links to the two libraries
5// /preserve?url=&rating= fetch as a browser (archive.org details -> resolve to the real file), STRIP tracking/
6// ad/redirect junk from HTML, archive the clean page AND the media under the rating
7// /media list SFW preserved items (play/view/download)
8// /gallery list NSFW preserved items
9// /play?url= in-browser player page
10// /archive?url= raw byte-exact serve, Range/206 aware (streams big video)
11// Durable: the WARC persists to disk + reloads at startup. Run from nxc2 root. 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_csprng.nx"
17import "nx_browser_fetch.nx"
18import "nx_web_archive.nx"
19import "nx_ia_resolve.nx"
20import "nx_media_extract.nx" // mx_extract -- find the playable stream in a page (static: raw + embedded-state)
21import "nx_video_sniff_fetch.nx" // vsf_sniff_url_pre -- JS-EXEC layer (run page JS + bundles, guarded) for heavy-JS sites
22import "nx_surrogate.nx" // sg_build_preamble -- surrogate scriptlets that satisfy anti-adblock/tracker gates
23import "nx_web_harden.nx" // wh_neutralize -- 90s-neutral: render the site+ads, defang forced-download/popunder/nav-hijack
24import "nx_kvs.nx" // kvs_extract -- de-obfuscate KVS/kt_player video_url (huge legal-download tube-site family)
25const K_MAGIC_12288: i64 = 12288
26const K_MAGIC_8192: i64 = 8192
27const K_MAGIC_1024: i64 = 1024
28const K_MAGIC_4096: i64 = 4096
29const K_MAGIC_8101: i64 = 8101
30const K_MAGIC_4194304: i64 = 4194304
31const K_MAGIC_1610612736: i64 = 1610612736
32const K_MAGIC_838860800: i64 = 838860800
33const K_MAGIC_2097152: i64 = 2097152
34const K_MAGIC_16777216: i64 = 16777216
35const K_MAGIC_16384: i64 = 16384
36const K_MAGIC_65536: i64 = 65536
37const K_MAGIC_2560: i64 = 2560
38const K_MAGIC_2048: i64 = 2048
39
40func ad_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
41func ad_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 }
42func ad_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 }
43func ad_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 }
44func ad_hex(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 }
45func ad_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 }
46func ad_find(hay: *u8, hl: i64, needle: *u8, nl: i64, from: i64) -> i64 { if nl==0 { return from } var i: i64=from; let last: i64=hl-nl; while i<=last { var j: i64=0; var m: i64=1; while j<nl { if (hay[i+j]&0xff)!=(needle[j]&0xff){m=0;j=nl} else {j=j+1} } if m==1 {return i} i=i+1 } return 0-1 }
47func ad_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { let x: i64=a[i]&0xff; let y: i64=b[i]&0xff; if x!=y { return 0 } if x==0 { return 1 } i=i+1 } return 0 }
48func ad_lc(c: i64) -> i64 { if c>=65 { if c<=90 { return c+32 } } return c }
49func ad_ci_at(src: *u8, slen: i64, pos: i64, lit: *u8, litlen: i64) -> i64 { if pos+litlen>slen { return 0 } var i: i64=0; while i<litlen { if ad_lc(src[pos+i]&0xff)!=ad_lc(lit[i]&0xff) { return 0 } i=i+1 } return 1 }
50// --- RATING OVERRIDE store: a sidecar "<rating>\t<url>\n" log so an item can be re-classified (moved between
51// Media/Gallery) WITHOUT re-downloading its bytes. Append-only, last-write-wins. Loaded at startup, appended on /rate.
52func rat_memeq(buf: *u8, off: i64, s: *u8, slen: i64) -> i64 { var i: i64=0; while i<slen { if (buf[off+i]&0xff)!=(s[i]&0xff) { return 0 } i=i+1 } return 1 }
53func rat_lookup(buf: *u8, off: i64, url: *u8, ulen: i64, out: *u8, cap: i64) -> i64 {
54 var i: i64=0; var found: i64=0
55 while i < off {
56 var e: i64=i; var g: i64=1; while g==1 { if e>=off { g=0 } else { if (buf[e]&0xff)==10 { g=0 } else { e=e+1 } } }
57 var t: i64=i; var g2: i64=1; while g2==1 { if t>=e { g2=0 } else { if (buf[t]&0xff)==9 { g2=0 } else { t=t+1 } } }
58 if t < e { let us: i64=t+1; let ul2: i64=e-us; if ul2==ulen { if rat_memeq(buf, us, url, ulen)==1 { let rl: i64=t-i; var k: i64=0; while k<rl { if k<(cap-1) { out[k]=buf[i+k] } k=k+1 } out[rl]=0 as u8; found=1 } } }
59 i = e + 1
60 }
61 return found
62}
63// extract named query param (percent-decoded) from a request path; boundary-aware ("?name="/"&name=")
64func ad_query_param(q: *u8, qlen: i64, name: *u8, namelen: i64, out: *u8, cap: i64) -> i64 {
65 var start: i64 = 0-1; var si: i64 = 0
66 while si < qlen {
67 var isp: i64 = 0
68 if si == 0 { isp = 1 } else { let pc: i64 = q[si-1]&0xff; if pc==38 { isp=1 } if pc==63 { isp=1 } }
69 if isp == 1 { if (si+namelen+1) <= qlen { var m: i64=1; var k: i64=0; while k<namelen { if (q[si+k]&0xff)!=(name[k]&0xff) { m=0; k=namelen } else { k=k+1 } } if m==1 { if (q[si+namelen]&0xff)==61 { start=si+namelen+1; si=qlen } } } }
70 si = si + 1
71 }
72 if start < 0 { out[0]=0 as u8; return 0 }
73 var i: i64 = start; var o: i64 = 0; var go: i64 = 1
74 while go == 1 { if i>=qlen { go=0 } else { let c: i64=q[i]&0xff
75 if c==38 { go=0 } else { if c==37 { if (i+2)<qlen { let hi: i64=ad_hex(q[i+1]&0xff); let lo: i64=ad_hex(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 } } } } }
76 out[o]=0 as u8; return o
77}
78// URL-encode for embedding in an href (so listing links round-trip)
79func ad_urlenc(s: *u8, slen: i64, out: *u8, cap: i64) -> i64 {
80 let hexd: *u8 = "0123456789ABCDEF" as *u8; var o: i64=0; var i: i64=0
81 while i<slen { let c: i64=s[i]&0xff; var safe: i64=0
82 if c>=48 { if c<=57 { safe=1 } } if c>=65 { if c<=90 { safe=1 } } if c>=97 { if c<=122 { safe=1 } }
83 if c==46 { safe=1 } if c==95 { safe=1 } if c==45 { safe=1 } if c==126 { safe=1 }
84 if safe==1 { if o<(cap-1){out[o]=c as u8;o=o+1} } else { if o<(cap-4){ out[o]=37 as u8; out[o+1]=hexd[(c>>4)&0xf]; out[o+2]=hexd[c&0xf]; o=o+3 } } i=i+1 }
85 out[o]=0 as u8; return o
86}
87// HTML-escape into dst
88func ad_esc(dst: *u8, off: i64, s: *u8, sl: i64) -> i64 {
89 var i: i64=0; while i<sl { let c: i64=s[i]&0xff
90 if c==60 { off=ad_put(dst,off,"<" as *u8) } else { if c==62 { off=ad_put(dst,off,">" as *u8) } else { if c==38 { off=ad_put(dst,off,"&" as *u8) } else { if c==34 { off=ad_put(dst,off,""" as *u8) } else { dst[off]=c as u8; off=off+1 } } } } i=i+1 }
91 return off
92}
93// loop-write all n bytes to a socket (a single sys_write won't drain a 562MB body)
94func ad_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 }
95// skip a tag block <tag...</close>: from just-after-open-tag, return index past the close tag (junk removal)
96func ad_skip_block(src: *u8, slen: i64, from: i64, close: *u8, closelen: i64) -> i64 {
97 var j: i64=from; var found: i64=0
98 while found==0 { if j>=slen { found=1 } else { if ad_ci_at(src, slen, j, close, closelen)==1 { j=j+closelen; found=1 } else { j=j+1 } } }
99 return j
100}
101func ad_putb(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[off]=src[i]; off=off+1; i=i+1 } return off }
102// STRIP the junk into `out` starting at `off`: drop <script>/<iframe>/<noscript>/<embed>/<object> blocks +
103// on*= inline handlers (tracking/ads/redirects/miners/popups) -> a clean, inert page. Returns the new offset.
104func ad_strip_junk_into(out: *u8, off: i64, cap: i64, src: *u8, slen: i64) -> i64 {
105 var o: i64=off; var i: i64=0
106 while i<slen {
107 var handled: i64=0
108 if ad_ci_at(src, slen, i, "<script" as *u8, 7)==1 { i=ad_skip_block(src, slen, i+7, "</script>" as *u8, 9); handled=1 }
109 if handled==0 { if ad_ci_at(src, slen, i, "<iframe" as *u8, 7)==1 { i=ad_skip_block(src, slen, i+7, "</iframe>" as *u8, 9); handled=1 } }
110 if handled==0 { if ad_ci_at(src, slen, i, "<noscript" as *u8, 9)==1 { i=ad_skip_block(src, slen, i+9, "</noscript>" as *u8, 11); handled=1 } }
111 if handled==0 { if ad_ci_at(src, slen, i, "<embed" as *u8, 6)==1 { i=ad_skip_block(src, slen, i+6, ">" as *u8, 1); handled=1 } }
112 if handled==0 { if ad_ci_at(src, slen, i, "<object" as *u8, 7)==1 { i=ad_skip_block(src, slen, i+7, "</object>" as *u8, 9); handled=1 } }
113 if handled==0 { if ad_ci_at(src, slen, i, " on" as *u8, 3)==1 { // strip inline event handlers " onclick=..." -> drop to next space/>
114 var j: i64=i+3; var ish: i64=0
115 while j<slen { if (src[j]&0xff)==61 { ish=1; j=slen } else { if (src[j]&0xff)==32 { j=slen } else { if (src[j]&0xff)==62 { j=slen } else { j=j+1 } } } }
116 if ish==1 { var k: i64=i+3; var q: i64=0; var g: i64=1; while g==1 { if k>=slen { g=0 } else { let c: i64=src[k]&0xff; if c==34 { if q==0 { q=1 } else { q=0; k=k+1; g=0 } k=k+1 } else { if c==39 { if q==0 { q=2 } else { if q==2 { q=0; k=k+1; g=0 } else { k=k+1 } } k=k+1 } else { if q==0 { if c==32 { g=0 } else { if c==62 { g=0 } else { k=k+1 } } } else { k=k+1 } } } } } i=k; handled=1 }
117 } }
118 if handled==0 { if o<(cap-1) { out[o]=src[i]; o=o+1 } i=i+1 }
119 }
120 return o
121}
122func ad_strip_junk(src: *u8, slen: i64, out: *u8, cap: i64) -> i64 { let o: i64 = ad_strip_junk_into(out, 0, cap, src, slen); out[o]=0 as u8; return o }
123// CLEAN VIEW: assemble a minimal, safe page = a native HTML5 <video> (if a stream was found) over the junk-
124// stripped original -- "the site just works" with no scripts/ads/trackers/popups. media_url empty = no player.
125func ad_clean_view(out: *u8, cap: i64, body: *u8, blen: i64, page_url: *u8, ulen: i64, media_url: *u8, mlen: i64, is_html: i64, ocss: *u8, ocss_len: i64) -> i64 {
126 var o: i64 = ad_put(out, 0, "<!doctype html><meta charset=utf-8><meta name=referrer content=no-referrer><title>Nishi clean view</title><style>body{background:#0f1115;color:#e8e8ea;font-family:system-ui,sans-serif;max-width:1000px;margin:0 auto;padding:1rem;line-height:1.5}video{width:100%;max-height:74vh;background:#000;border-radius:8px}img{max-width:100%;height:auto}a{color:#7ab7ff}.nbar{position:sticky;top:0;background:#171a21;border:1px solid #2a2f3a;padding:.5rem .8rem;border-radius:8px;margin-bottom:1rem;font-size:.9rem}.nbar a{color:#7ab7ff;text-decoration:none}.nsub{color:#9aa;font-size:.85rem;margin:.3rem 0 1rem}</style>" as *u8)
127 // overlay-neutralizer CSS LAST (so !important wins over the page's own <style>): restore scroll + hide walls
128 if ocss_len > 0 { o = ad_put(out, o, "<style>" as *u8); o = ad_putb(out, o, ocss, ocss_len); o = ad_put(out, o, "</style>" as *u8) }
129 o = ad_put(out, o, "<div class=nbar>🧭 Nishi clean view — scripts / ads / trackers / popups / miners stripped · walls neutralized · <a href=/>home</a></div>" as *u8)
130 if mlen > 0 {
131 let venc: *u8 = sys_mmap(K_MAGIC_12288); ad_urlenc(media_url, mlen, venc, K_MAGIC_12288)
132 o = ad_put(out, o, "<video controls autoplay playsinline webkit-playsinline src=\"/stream?url=" as *u8); o = ad_put(out, o, venc); o = ad_put(out, o, "\"></video>" as *u8)
133 o = ad_put(out, o, "<div class=nsub>Native HTML5 stream (proxied via localhost for cross-origin playback).<br>found media: <code>" as *u8); o = ad_esc(out, o, media_url, mlen); o = ad_put(out, o, "</code></div>" as *u8)
134 }
135 if mlen == 0 { if is_html == 1 { o = ad_put(out, o, "<div class=nsub>⚠ No playable media URL found on this page. It likely builds the video via MSE / blob URLs or a protected player that our static + JS-exec extraction can't yet surface — that's the next extraction rung, not a playback issue.</div>" as *u8) } }
136 if is_html == 1 {
137 var room: i64 = cap - o - 16
138 var bl: i64 = blen; if bl > room { bl = room }
139 o = ad_strip_junk_into(out, o, cap, body, bl)
140 }
141 out[o]=0 as u8
142 return o
143}
144// origin Content-Type from a fetched response header block (for the WARC record)
145func ad_origin_ctype(resp: *u8, boff: i64, out: *u8, cap: i64) -> i64 {
146 var p: i64 = ad_find(resp, boff, "\r\nContent-Type: " as *u8, 16, 0)
147 if p < 0 { p = ad_find(resp, boff, "\r\ncontent-type: " as *u8, 16, 0) }
148 if p < 0 { ad_put(out, 0, "application/octet-stream" as *u8); out[24]=0 as u8; return 24 }
149 var i: i64 = p + 16; var o: i64 = 0; var go: i64 = 1
150 while go == 1 { if i>=boff { go=0 } else { let c: i64=resp[i]&0xff; if c==13 { go=0 } else { if c==59 { go=0 } else { if o<(cap-1) { out[o]=c as u8; o=o+1 } i=i+1 } } } }
151 out[o]=0 as u8; return o
152}
153// pull the URL= value out of a sniffer plan ("ROUTE=<r> URL=<u>"); returns len, 0 if none/"-"/not-a-url
154func ad_plan_url(plan: *u8, out: *u8, cap: i64) -> i64 {
155 let p: i64 = ad_find(plan, ad_slen(plan), "URL=" as *u8, 4, 0)
156 if p < 0 { out[0]=0 as u8; return 0 }
157 var i: i64 = p + 4; var o: i64 = 0; var go: i64 = 1
158 while go == 1 { let c: i64 = plan[i]&0xff; if c==0 { go=0 } else { if c==32 { go=0 } else { if o<(cap-1) { out[o]=c as u8; o=o+1 } i=i+1 } } }
159 out[o]=0 as u8
160 if o == 0 { return 0 }
161 if ad_find(out, o, "http" as *u8, 4, 0) < 0 { return 0 } // "-" / guard-killed -> no url
162 return o
163}
164func ad_is_html(ct: *u8) -> i64 { if ad_find(ct, ad_slen(ct), "text/html" as *u8, 9, 0) >= 0 { return 1 } return 0 }
165func ad_is_video(ct: *u8) -> i64 { if ad_find(ct, ad_slen(ct), "video/" as *u8, 6, 0) >= 0 { return 1 } return 0 }
166func ad_is_image(ct: *u8) -> i64 { if ad_find(ct, ad_slen(ct), "image/" as *u8, 6, 0) >= 0 { return 1 } return 0 }
167// parse a Range: bytes=START-END header from the raw request; 1 if present (sets start; end=-1 if open)
168func ad_parse_range(req: *u8, reqlen: i64, out_start: *i64, out_end: *i64) -> i64 {
169 var p: i64 = ad_find(req, reqlen, "Range: bytes=" as *u8, 13, 0)
170 if p < 0 { p = ad_find(req, reqlen, "range: bytes=" as *u8, 13, 0) }
171 if p < 0 { return 0 }
172 var i: i64 = p + 13; var s: i64 = 0; var seen: i64 = 0
173 var go: i64 = 1
174 while go == 1 { let c: i64=req[i]&0xff; if c>=48 { if c<=57 { s=s*10+(c-48); seen=1; i=i+1 } else { go=0 } } if c<48 { go=0 } if c>57 { go=0 } }
175 var e: i64 = 0-1
176 if (req[i]&0xff)==45 { i=i+1; var e2: i64=0; var eseen: i64=0; var g2: i64=1
177 while g2==1 { let c: i64=req[i]&0xff; if c>=48 { if c<=57 { e2=e2*10+(c-48); eseen=1; i=i+1 } else { g2=0 } } if c<48 { g2=0 } if c>57 { g2=0 } }
178 if eseen==1 { e=e2 } }
179 if seen==0 { return 0 }
180 out_start[0]=s; out_end[0]=e
181 return 1
182}
183func send_html(cfd: i64, body: *u8, blen: i64) -> i64 {
184 let resp: *u8 = sys_mmap(blen + 512); var o: i64 = ad_put(resp, 0, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: " as *u8)
185 o = ad_putn(resp, o, blen); o = ad_put(resp, o, "\r\nConnection: close\r\n\r\n" as *u8)
186 var i: i64=0; while i<blen { resp[o]=body[i]; o=o+1; i=i+1 }
187 return nx_http_server_send_response(cfd, resp, o)
188}
189
190// shared page CSS/header
191func ad_head(page: *u8, o: i64, title: *u8) -> i64 {
192 o = ad_put(page, o, "<!doctype html><meta charset=utf-8><title>" as *u8); o = ad_put(page, o, title)
193 o = ad_put(page, o, "</title><style>body{font-family:system-ui,sans-serif;max-width:900px;margin:2rem auto;padding:0 1rem;background:#0f1115;color:#e8e8ea;line-height:1.5}h1{margin: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:.5rem;width:60%}button{background:#2b6cff;color:#fff;border:0;border-radius:6px;padding:.55rem 1rem;cursor:pointer}a{color:#7ab7ff;text-decoration:none}code{color:#bcd;word-break:break-all;font-size:.85rem}.row{padding:.5rem 0;border-bottom:1px solid #21252e}.tag{font-size:.75rem;padding:.1rem .4rem;border-radius:4px;background:#2a2f3a;color:#bcd;margin-right:.4rem}.play{color:#51cf66}.nav a{margin-right:1rem}video{width:100%;border-radius:8px;background:#000}</style>" as *u8)
194 o = ad_put(page, o, "<div class=nav><a href=/>🏠 Home</a><a href=/media>📺 Media (SFW)</a><a href=/gallery>🔒 Gallery (NSFW)</a></div>" as *u8)
195 return o
196}
197func ad_home(page: *u8, warc: *u8, warc_off: i64) -> i64 {
198 var o: i64 = ad_head(page, 0, "Nishi Archive" as *u8)
199 o = ad_put(page, o, "<h1>Nishi Archive</h1><div class=sub>Preserve the page AND the media, clean of trackers/ads/redirects, so it survives the origin vanishing. Link-rot prevention for the search engine.</div>" as *u8)
200 o = ad_put(page, o, "<div class=card><b>" as *u8); o = ad_putn(page, o, wa_count(warc, warc_off)); o = ad_put(page, o, "</b> records preserved (" as *u8); o = ad_putn(page, o, warc_off); o = ad_put(page, o, " bytes on disk)</div>" as *u8)
201 o = ad_put(page, o, "<div class=card><h2>Visit clean</h2><form action=/visit><input type=text name=url placeholder=\"https://...\" autofocus><button>Visit</button></form><div class=sub>Reader mode: strips junk/ads/trackers/popups/miners down to the content + media playing in native HTML5. Nothing nasty loads.</div></div>" as *u8)
202 o = ad_put(page, o, "<div class=card><h2>Neutral browse <span class=tag>90s-neutral</span></h2><form action=/browse><input type=text name=url placeholder=\"https://...\"><button>Browse</button></form><div class=sub>Loads the FULL site and its ads (the operator still gets paid) but defangs every attack vector — forced downloads, popunders, redirect-to-scam, navigate-to-binary, meta-refresh payloads. For hostile sites you still want to see, like the 90s web.</div></div>" as *u8)
203 o = ad_put(page, o, "<div class=card><h2>Preserve a URL</h2><form action=/preserve><input type=text name=url placeholder=\"https://...\"><div style=margin:.6rem_0><label><input type=radio name=rating value=sfw checked> SFW → Media</label> <label><input type=radio name=rating value=nsfw> NSFW → Gallery</label></div><button>Preserve</button></form><div class=sub>Fetches it as a browser (archive.org detail pages resolve to the real file), strips the junk, archives the clean page + the media.</div></div>" as *u8)
204 return o
205}
206
207// render a library listing for the wanted rating (dedup: newest capture of each URL; rating-override applied so
208// re-tagged items move here). `other`/`otherlabel` = the one-click move link to the opposite library.
209func ad_listing(page: *u8, warc: *u8, warc_off: i64, want: *u8, title: *u8, other: *u8, otherlabel: *u8, rat_buf: *u8, rat_off: i64) -> i64 {
210 var o: i64 = ad_head(page, 0, title)
211 o = ad_put(page, o, "<h1>" as *u8); o = ad_put(page, o, title); o = ad_put(page, o, "</h1>" as *u8)
212 o = ad_put(page, o, "<div class=card>" as *u8)
213 let cnt: i64 = wa_count(warc, warc_off)
214 let uri: *u8 = sys_mmap(K_MAGIC_8192); let ct: *u8 = sys_mmap(256); let rat: *u8 = sys_mmap(64); let lenb: *i64 = sys_mmap(8) as *i64
215 let uri2: *u8 = sys_mmap(K_MAGIC_8192); let ct2: *u8 = sys_mmap(256); let rat2: *u8 = sys_mmap(64); let len2: *i64 = sys_mmap(8) as *i64
216 let enc: *u8 = sys_mmap(K_MAGIC_12288); let eff: *u8 = sys_mmap(64)
217 var shown: i64 = 0
218 var idx: i64 = 0
219 while idx < cnt {
220 if wa_record_at(warc, warc_off, idx, uri, K_MAGIC_8192, ct, 256, rat, 64, lenb) == 1 {
221 // effective rating = override if present, else the record's stored rating
222 var use_rat: *u8 = rat
223 if rat_lookup(rat_buf, rat_off, uri, ad_slen(uri), eff, 64) == 1 { use_rat = eff }
224 if ad_streq(use_rat, want) == 1 {
225 var last: i64 = 1
226 var j: i64 = idx + 1
227 while j < cnt { if wa_record_at(warc, warc_off, j, uri2, K_MAGIC_8192, ct2, 256, rat2, 64, len2) == 1 { if ad_streq(uri, uri2) == 1 { last = 0; j = cnt } } j = j + 1 }
228 if last == 1 {
229 shown = shown + 1
230 ad_urlenc(uri, ad_slen(uri), enc, K_MAGIC_12288)
231 o = ad_put(page, o, "<div class=row>" as *u8)
232 o = ad_put(page, o, "<span class=tag>" as *u8); o = ad_put(page, o, ct); o = ad_put(page, o, "</span>" as *u8)
233 o = ad_put(page, o, "<span class=tag>" as *u8); o = ad_putn(page, o, lenb[0]/K_MAGIC_1024); o = ad_put(page, o, " KB</span> " as *u8)
234 if ad_is_video(ct) == 1 { o = ad_put(page, o, "<a class=play href=/play?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, ">▶ Play</a> · " as *u8) }
235 if ad_is_image(ct) == 1 { o = ad_put(page, o, "<a class=play href=/play?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, ">🖼 View</a> · " as *u8) }
236 o = ad_put(page, o, "<a href=/archive?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, ">Open</a> · " as *u8)
237 o = ad_put(page, o, "<a href=/rate?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, "&rating=" as *u8); o = ad_put(page, o, other); o = ad_put(page, o, ">" as *u8); o = ad_put(page, o, otherlabel); o = ad_put(page, o, "</a><br>" as *u8)
238 o = ad_put(page, o, "<code>" as *u8); o = ad_esc(page, o, uri, ad_slen(uri)); o = ad_put(page, o, "</code></div>" as *u8)
239 }
240 }
241 }
242 idx = idx + 1
243 }
244 if shown == 0 { o = ad_put(page, o, "<div class=sub>Nothing here yet. Preserve a URL and mark it " as *u8); o = ad_put(page, o, want); o = ad_put(page, o, ", or move an item here from the other library.</div>" as *u8) }
245 o = ad_put(page, o, "</div>" as *u8)
246 return o
247}
248
249func ad_play(page: *u8, url: *u8, ulen: i64) -> i64 {
250 var o: i64 = ad_head(page, 0, "Play" as *u8)
251 let enc: *u8 = sys_mmap(K_MAGIC_12288); ad_urlenc(url, ulen, enc, K_MAGIC_12288)
252 o = ad_put(page, o, "<div class=card><video controls autoplay playsinline webkit-playsinline src=/archive?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, "></video>" as *u8)
253 o = ad_put(page, o, "<div class=sub style=margin-top:.6rem>Served byte-exact from the sovereign archive. <a href=/archive?url=" as *u8); o = ad_put(page, o, enc); o = ad_put(page, o, ">Download</a><br><code>" as *u8); o = ad_esc(page, o, url, ulen); o = ad_put(page, o, "</code></div></div>" as *u8)
254 return o
255}
256
257// Range-aware raw serve straight from the WARC buffer (streams big video, no giant copy)
258func ad_serve_archive(cfd: i64, warc: *u8, warc_off: i64, url: *u8, ulen: i64, req: *u8, reqlen: i64) -> i64 {
259 let op: *i64 = sys_mmap(8) as *i64; let ol: *i64 = sys_mmap(8) as *i64; let ct: *u8 = sys_mmap(256)
260 if wa_resolve(warc, warc_off, url, ulen, op, ol, ct, 256) == 0 {
261 let body: *u8 = "Not preserved.\n" as *u8
262 let h: *u8 = sys_mmap(256); var ho: i64 = ad_put(h, 0, "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 15\r\nConnection: close\r\n\r\nNot preserved.\n" as *u8)
263 ad_write_all(cfd, h, ho); sys_close(cfd); return 0
264 }
265 let total: i64 = ol[0]
266 let rs: *i64 = sys_mmap(8) as *i64; let re: *i64 = sys_mmap(8) as *i64
267 var start: i64 = 0; var end: i64 = total - 1; var code: i64 = 200
268 if ad_parse_range(req, reqlen, rs, re) == 1 {
269 start = rs[0]; if re[0] >= 0 { end = re[0] }
270 if end >= total { end = total - 1 }
271 if start < 0 { start = 0 }
272 if start > end { start = 0; end = total - 1 }
273 code = 206
274 }
275 let clen: i64 = end - start + 1
276 let h: *u8 = sys_mmap(K_MAGIC_1024); var ho: i64 = 0
277 if code == 206 { ho = ad_put(h, 0, "HTTP/1.1 206 Partial Content\r\n" as *u8) } else { ho = ad_put(h, 0, "HTTP/1.1 200 OK\r\n" as *u8) }
278 ho = ad_put(h, ho, "Content-Type: " as *u8); ho = ad_put(h, ho, ct); ho = ad_put(h, ho, "\r\nAccept-Ranges: bytes\r\n" as *u8)
279 if code == 206 { ho = ad_put(h, ho, "Content-Range: bytes " as *u8); ho = ad_putn(h, ho, start); ho = ad_put(h, ho, "-" as *u8); ho = ad_putn(h, ho, end); ho = ad_put(h, ho, "/" as *u8); ho = ad_putn(h, ho, total); ho = ad_put(h, ho, "\r\n" as *u8) }
280 ho = ad_put(h, ho, "Content-Length: " as *u8); ho = ad_putn(h, ho, clen); ho = ad_put(h, ho, "\r\nX-Nishi-Archive: preserved\r\nConnection: close\r\n\r\n" as *u8)
281 ad_write_all(cfd, h, ho)
282 ad_write_all(cfd, ((warc as i64) + op[0] + start) as *u8, clen)
283 sys_close(cfd)
284 return 0
285}
286
287// STREAM PROXY: fetch a media URL from its origin AS A BROWSER (headers + redirects) and relay it to the client
288// with Range/206 support. This makes a cross-origin/referrer/session-touchy stream PLAY from localhost: the
289// browser's <video> hits same-origin /stream (no CORS), and we fetch origin server-side. Re-fetches per Range
290// request (fine for a test; a fetch cache is the follow-up for big files).
291func ad_serve_stream(cfd: i64, store: *TrustStore, cr: *u8, pk: *u8, url: *u8, ulen: i64, req: *u8, reqlen: i64, resp: *u8, cap: i64) -> i64 {
292 let n: i64 = bf_fetch_meta(url, store, cr, pk, sys_now_realtime_sec(), resp, cap, 2)
293 let st: i64 = bf_status(resp, n)
294 if st != 200 {
295 let h: *u8 = sys_mmap(256); var ho: i64 = ad_put(h, 0, "HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nContent-Length: 13\r\nConnection: close\r\n\r\norigin failed" as *u8)
296 ad_write_all(cfd, h, ho); sys_close(cfd); return 0
297 }
298 let boff: i64 = bf_body_off(resp, n)
299 let blen: i64 = n - boff
300 let ct: *u8 = sys_mmap(256); ad_origin_ctype(resp, boff, ct, 256)
301 let rs: *i64 = sys_mmap(8) as *i64; let re: *i64 = sys_mmap(8) as *i64
302 var start: i64 = 0; var end: i64 = blen - 1; var code: i64 = 200
303 if ad_parse_range(req, reqlen, rs, re) == 1 {
304 start = rs[0]; if re[0] >= 0 { end = re[0] }
305 if end >= blen { end = blen - 1 }
306 if start < 0 { start = 0 }
307 if start > end { start = 0; end = blen - 1 }
308 code = 206
309 }
310 let clen: i64 = end - start + 1
311 let h: *u8 = sys_mmap(K_MAGIC_1024); var ho: i64 = 0
312 if code == 206 { ho = ad_put(h, 0, "HTTP/1.1 206 Partial Content\r\n" as *u8) } else { ho = ad_put(h, 0, "HTTP/1.1 200 OK\r\n" as *u8) }
313 ho = ad_put(h, ho, "Content-Type: " as *u8); ho = ad_put(h, ho, ct); ho = ad_put(h, ho, "\r\nAccept-Ranges: bytes\r\nAccess-Control-Allow-Origin: *\r\n" as *u8)
314 if code == 206 { ho = ad_put(h, ho, "Content-Range: bytes " as *u8); ho = ad_putn(h, ho, start); ho = ad_put(h, ho, "-" as *u8); ho = ad_putn(h, ho, end); ho = ad_put(h, ho, "/" as *u8); ho = ad_putn(h, ho, blen); ho = ad_put(h, ho, "\r\n" as *u8) }
315 ho = ad_put(h, ho, "Content-Length: " as *u8); ho = ad_putn(h, ho, clen); ho = ad_put(h, ho, "\r\nConnection: close\r\n\r\n" as *u8)
316 ad_write_all(cfd, h, ho)
317 ad_write_all(cfd, ((resp as i64) + boff + start) as *u8, clen)
318 sys_close(cfd)
319 return 0
320}
321
322// preserve one resource (fetch as browser, strip if HTML, archive rated). Returns new warc_off; sets flags/bytes.
323func ad_preserve_one(warc: *u8, warc_off: i64, fetch_url: *u8, store_url: *u8, rating: *u8, store: *TrustStore, cr: *u8, pk: *u8, resp: *u8, cap: i64, out_bytes: *i64, out_ct: *u8) -> i64 {
324 let n: i64 = bf_fetch_meta(fetch_url, store, cr, pk, sys_now_realtime_sec(), resp, cap, 3) // follow 3xx + meta-refresh to real content
325 let st: i64 = bf_status(resp, n); let boff: i64 = bf_body_off(resp, n); let blen: i64 = n - boff
326 out_bytes[0] = 0
327 if st != 200 { return warc_off }
328 if blen <= 0 { return warc_off }
329 let ct: *u8 = sys_mmap(256); ad_origin_ctype(resp, boff, ct, 256)
330 var i: i64 = 0; while (ct[i]&0xff)!=0 { out_ct[i]=ct[i]; i=i+1 } out_ct[i]=0 as u8
331 let body: *u8 = ((resp as i64)+boff) as *u8
332 if ad_is_html(ct) == 1 {
333 let cleaned: *u8 = sys_mmap(blen + K_MAGIC_4096)
334 let clen: i64 = ad_strip_junk(body, blen, cleaned, blen + K_MAGIC_4096)
335 out_bytes[0] = clen
336 return wa_write_resource_rated(warc, warc_off, store_url, "2026-07-11T00:00:00Z" as *u8, "text/html" as *u8, rating, cleaned, clen)
337 }
338 out_bytes[0] = blen
339 return wa_write_resource_rated(warc, warc_off, store_url, "2026-07-11T00:00:00Z" as *u8, ct, rating, body, blen)
340}
341
342func main(argc: i64, argv: *i64) -> i64 {
343 var port: i64 = K_MAGIC_8101
344 if argc >= 2 { port = ad_datoi(argv[1] as *u8) }
345 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
346 if r <= 0 { sys_write(2, "ARCHIVED: certdata load failed (run from nxc2 root)\n" as *u8, 51); sys_exit(1); return 1 }
347 let store: *TrustStore = r as *TrustStore
348 let cr: *u8 = sys_mmap(32); let pk: *u8 = sys_mmap(32); nx_csprng_fill(cr, 32); nx_csprng_fill(pk, 32)
349 let wpath: *u8 = "nx_archive_store.warc" as *u8
350 let WCAP: i64 = K_MAGIC_1610612736
351 let warc: *u8 = sys_mmap(WCAP)
352 var warc_off: i64 = wa_load(wpath, warc, WCAP)
353 let rpath: *u8 = "nx_archive_ratings.txt" as *u8 // rating-override sidecar (re-tag without re-download)
354 let rat_buf: *u8 = sys_mmap(K_MAGIC_4194304)
355 var rat_off: i64 = wa_load(rpath, rat_buf, K_MAGIC_4194304)
356
357 let addr: *u8 = sys_mmap(16); nx_http_server_addr_any(addr, port)
358 let vb: *i64 = sys_mmap(8) as *i64
359 let lfd: i64 = nx_http_server_listen(addr, 64, vb)
360 if lfd < 0 { sys_write(2, "ARCHIVED: listen failed\n" as *u8, 24); sys_exit(1); return 1 }
361 let banner: *u8 = sys_mmap(96); var bo: i64 = ad_put(banner, 0, "nx_archive_daemon: http://localhost:" as *u8); bo = ad_putn(banner, bo, port); bo = ad_put(banner, bo, "/\n" as *u8); sys_write(1, banner, bo)
362
363 let cap: i64 = K_MAGIC_838860800
364 let resp: *u8 = sys_mmap(cap)
365 let page: *u8 = sys_mmap(K_MAGIC_2097152)
366 let visit: *u8 = sys_mmap(K_MAGIC_16777216) // 16 MiB clean-view assembly buffer (reused)
367 let kb: *i64 = sys_mmap(8) as *i64
368 let sgt: *i64 = sg_new(); sg_seed(sgt) // surrogate table + preamble (satisfy anti-adblock gates)
369 let preamble: *u8 = sys_mmap(K_MAGIC_16384)
370 let pre_len: i64 = sg_build_preamble(sgt, preamble, K_MAGIC_16384)
371 let wh_pre: *u8 = sys_mmap(K_MAGIC_16384) // 90s-neutral attack-vector neutralizer preamble (built once)
372 let wh_pre_len: i64 = wh_build_preamble(wh_pre, K_MAGIC_16384)
373 let overlay_css: *u8 = sys_mmap(K_MAGIC_8192) // overlay-wall neutralizer CSS for the rendered clean-view
374 let ocss_len: i64 = sg_build_overlay_css(sgt, overlay_css, K_MAGIC_8192)
375 var running: i64 = 1
376 while running == 1 {
377 let cfd: i64 = nx_http_server_accept_one(lfd, vb)
378 if cfd >= 0 {
379 let req: *u8 = sys_mmap(K_MAGIC_16384)
380 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
381 let rc: i64 = nx_http_server_read_request(cfd, req, K_MAGIC_16384, om, opo, opl, ocl, obo, orn)
382 if rc == NXS_OK {
383 let path: *u8 = ((req as i64)+opo[0]) as *u8
384 let plen: i64 = opl[0]
385 if ad_dstarts(path, plen, "/preserve" as *u8) == 1 {
386 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
387 let rating: *u8 = sys_mmap(64); let rn: i64 = ad_query_param(path, plen, "rating" as *u8, 6, rating, 64)
388 if rn == 0 { rating[0]=115 as u8; rating[1]=102 as u8; rating[2]=119 as u8; rating[3]=0 as u8 }
389 if un > 0 {
390 let pbytes: *i64 = sys_mmap(8) as *i64; let pct: *u8 = sys_mmap(256)
391 // 1. archive the PAGE at the pasted URL (clean)
392 warc_off = ad_preserve_one(warc, warc_off, ub, ub, rating, store, cr, pk, resp, cap, pbytes, pct)
393 let page_bytes: i64 = pbytes[0]
394 // 2. archive.org detail page -> ALSO resolve + archive the real MEDIA under its own URL
395 var media_bytes: i64 = 0; let murl: *u8 = sys_mmap(K_MAGIC_4096); murl[0]=0 as u8
396 if ad_find(ub, un, "archive.org/details/" as *u8, 20, 0) >= 0 {
397 let res: *u8 = sys_mmap(K_MAGIC_4096); let rl: i64 = ia_resolve(ub, un, store, cr, pk, sys_now_realtime_sec(), res, K_MAGIC_4096)
398 if rl > 0 { let mb: *i64 = sys_mmap(8) as *i64; let mct: *u8 = sys_mmap(256)
399 warc_off = ad_preserve_one(warc, warc_off, res, res, rating, store, cr, pk, resp, cap, mb, mct)
400 media_bytes = mb[0]; var mi: i64=0; while (res[mi]&0xff)!=0 { murl[mi]=res[mi]; mi=mi+1 } murl[mi]=0 as u8 }
401 }
402 wa_save(wpath, warc, warc_off)
403 // result page
404 let out: *u8 = sys_mmap(K_MAGIC_65536); var oo: i64 = ad_head(out, 0, "Preserved" as *u8)
405 oo = ad_put(out, oo, "<div class=card><h2>Preserved (" as *u8); oo = ad_put(out, oo, rating); oo = ad_put(out, oo, ")</h2>" as *u8)
406 if page_bytes > 0 { oo = ad_put(out, oo, "<p>Clean page: " as *u8); oo = ad_putn(out, oo, page_bytes); oo = ad_put(out, oo, " bytes archived.</p>" as *u8) }
407 else { oo = ad_put(out, oo, "<p>Page: not captured (fetch failed / hard host).</p>" as *u8) }
408 if media_bytes > 0 { oo = ad_put(out, oo, "<p>Media: " as *u8); oo = ad_putn(out, oo, media_bytes); oo = ad_put(out, oo, " bytes archived. " as *u8)
409 let enc: *u8 = sys_mmap(K_MAGIC_12288); ad_urlenc(murl, ad_slen(murl), enc, K_MAGIC_12288)
410 oo = ad_put(out, oo, "<a class=play href=/play?url=" as *u8); oo = ad_put(out, oo, enc); oo = ad_put(out, oo, ">▶ Play now</a></p>" as *u8) }
411 var glink: *u8 = "/media" as *u8
412 if ad_streq(rating, "nsfw" as *u8) == 1 { glink = "/gallery" as *u8 }
413 oo = ad_put(out, oo, "<p><a href=" as *u8); oo = ad_put(out, oo, glink); oo = ad_put(out, oo, ">→ see it in the library</a></p></div>" as *u8)
414 send_html(cfd, out, oo)
415 } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
416 } else {
417 if ad_dstarts(path, plen, "/visit" as *u8) == 1 {
418 // LIVE CLEAN VIEW: fetch as a browser, strip the junk, play the media native HTML5
419 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
420 if un > 0 {
421 let n: i64 = bf_fetch_meta(ub, store, cr, pk, sys_now_realtime_sec(), resp, cap, 3) // 3xx + meta-refresh chain -> real content
422 let st: i64 = bf_status(resp, n); let boff: i64 = bf_body_off(resp, n); let blen: i64 = n - boff
423 if st == 200 { if blen > 0 {
424 let ct: *u8 = sys_mmap(256); ad_origin_ctype(resp, boff, ct, 256)
425 let body: *u8 = ((resp as i64)+boff) as *u8
426 let media: *u8 = sys_mmap(K_MAGIC_4096); var mlen: i64 = 0
427 if ad_find(ub, un, "archive.org/details/" as *u8, 20, 0) >= 0 { let rl: i64 = ia_resolve(ub, un, store, cr, pk, sys_now_realtime_sec(), media, K_MAGIC_4096); if rl > 0 { mlen = rl } }
428 // KVS/kt_player: de-obfuscate flashvars video_url (get_file redirects to the CDN mp4). Most-specific first.
429 if mlen == 0 { if ad_is_html(ct) == 1 { let kv: i64 = kvs_extract(body, blen, media, K_MAGIC_4096); if kv > 0 { mlen = kv } } }
430 if mlen == 0 { if ad_is_html(ct) == 1 { let conf: i64 = mx_extract(body, blen, media, K_MAGIC_4096, kb); if conf > 0 { mlen = ad_slen(media) } } }
431 // JS-EXEC fallback: static extraction found nothing -> run the page's JS (+ external
432 // bundles) in the GUARDED fork, WITH the surrogate preamble first so anti-adblock /
433 // tracker gates are satisfied and the gated media fetch fires, then capture it.
434 if mlen == 0 { if ad_is_html(ct) == 1 { let plan: *u8 = sys_mmap(K_MAGIC_2560); vsf_sniff_url_pre(ub, un, store, preamble, pre_len, plan, K_MAGIC_2560, 5); let jm: i64 = ad_plan_url(plan, media, K_MAGIC_4096); if jm > 0 { mlen = jm } } }
435 if ad_is_video(ct) == 1 { var mc: i64=0; while mc<un { media[mc]=ub[mc]; mc=mc+1 } media[un]=0 as u8; mlen = un }
436 let vlen: i64 = ad_clean_view(visit, K_MAGIC_16777216, body, blen, ub, un, media, mlen, ad_is_html(ct), overlay_css, ocss_len)
437 send_html(cfd, visit, vlen)
438 } else { send_html(cfd, "<a href=/>empty</a>" as *u8, 19) } }
439 else { let e: *u8 = sys_mmap(512); var eo: i64 = ad_head(e, 0, "Fetch failed" as *u8); eo = ad_put(e, eo, "<div class=card>Fetch failed (status " as *u8); eo = ad_putn(e, eo, st); eo = ad_put(e, eo, ") — hard host or access-control. <a href=/>back</a></div>" as *u8); send_html(cfd, e, eo) }
440 } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
441 } else {
442 if ad_dstarts(path, plen, "/rate" as *u8) == 1 {
443 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
444 let rating: *u8 = sys_mmap(64); let rn: i64 = ad_query_param(path, plen, "rating" as *u8, 6, rating, 64)
445 if un > 0 { if rn > 0 {
446 // append "<rating>\t<url>\n" to the override log (last-write-wins) + persist
447 var lo: i64 = ad_put(rat_buf, rat_off, rating); rat_buf[lo]=9 as u8; lo=lo+1
448 var k: i64=0; while k<un { rat_buf[lo]=ub[k]; lo=lo+1; k=k+1 } rat_buf[lo]=10 as u8; lo=lo+1
449 rat_off = lo; wa_save(rpath, rat_buf, rat_off)
450 } }
451 var dest: *u8 = "/media" as *u8
452 if ad_streq(rating, "nsfw" as *u8) == 1 { dest = "/gallery" as *u8 }
453 let out: *u8 = sys_mmap(K_MAGIC_2048); var oo: i64 = ad_head(out, 0, "Moved" as *u8)
454 oo = ad_put(out, oo, "<div class=card><h2>Moved to " as *u8); oo = ad_put(out, oo, rating); oo = ad_put(out, oo, ".</h2><p><a href=" as *u8); oo = ad_put(out, oo, dest); oo = ad_put(out, oo, ">→ view it there</a></p></div>" as *u8)
455 send_html(cfd, out, oo)
456 } else {
457 if ad_dstarts(path, plen, "/media" as *u8) == 1 {
458 let n: i64 = ad_listing(page, warc, warc_off, "sfw" as *u8, "Media (SFW)" as *u8, "nsfw" as *u8, "→ Move to Gallery" as *u8, rat_buf, rat_off); send_html(cfd, page, n)
459 } else {
460 if ad_dstarts(path, plen, "/gallery" as *u8) == 1 {
461 let n: i64 = ad_listing(page, warc, warc_off, "nsfw" as *u8, "Gallery (NSFW)" as *u8, "sfw" as *u8, "→ Move to Media" as *u8, rat_buf, rat_off); send_html(cfd, page, n)
462 } else {
463 if ad_dstarts(path, plen, "/play" as *u8) == 1 {
464 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
465 if un > 0 { let n: i64 = ad_play(page, ub, un); send_html(cfd, page, n) } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
466 } else {
467 if ad_dstarts(path, plen, "/stream" as *u8) == 1 {
468 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
469 if un > 0 { ad_serve_stream(cfd, store, cr, pk, ub, un, req, orn[0], resp, cap) } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
470 } else {
471 if ad_dstarts(path, plen, "/archive" as *u8) == 1 {
472 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
473 if un > 0 { ad_serve_archive(cfd, warc, warc_off, ub, un, req, orn[0]) } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
474 } else {
475 if ad_dstarts(path, plen, "/browse" as *u8) == 1 {
476 // NEUTRAL BROWSE (90s-neutral): render the FULL site + its ads, but defang every
477 // attack vector -- forced downloads, popunders, navigate-to-binary, beforeunload
478 // hijack, meta-refresh-to-payload. The operator still gets ad impressions.
479 let ub: *u8 = sys_mmap(K_MAGIC_8192); let un: i64 = ad_query_param(path, plen, "url" as *u8, 3, ub, K_MAGIC_8192)
480 if un > 0 {
481 let bn: i64 = bf_fetch_meta(ub, store, cr, pk, sys_now_realtime_sec(), resp, cap, 3)
482 let bst: i64 = bf_status(resp, bn); let bboff: i64 = bf_body_off(resp, bn); let bblen: i64 = bn - bboff
483 if bst == 200 { if bblen > 0 {
484 let bbody: *u8 = ((resp as i64)+bboff) as *u8
485 let bvlen: i64 = wh_neutralize(visit, K_MAGIC_16777216, bbody, bblen, wh_pre, wh_pre_len)
486 send_html(cfd, visit, bvlen)
487 } else { send_html(cfd, "<a href=/>empty</a>" as *u8, 19) } }
488 else { let e: *u8 = sys_mmap(512); var eo: i64 = ad_head(e, 0, "Fetch failed" as *u8); eo = ad_put(e, eo, "<div class=card>Fetch failed (status " as *u8); eo = ad_putn(e, eo, bst); eo = ad_put(e, eo, ") — hard host or access-control. <a href=/>back</a></div>" as *u8); send_html(cfd, e, eo) }
489 } else { send_html(cfd, "<a href=/>missing url</a>" as *u8, 24) }
490 } else {
491 let n: i64 = ad_home(page, warc, warc_off); send_html(cfd, page, n)
492 }
493 }
494 }
495 }
496 }
497 }
498 }
499 }
500 } else { sys_close(cfd) }
501 }
502 }
503 return 0
504}