code wiki / _hdl_build / nx_adnet.nx
nx_adnet.nx source
↩ module page · 64 lines · 4827 B
1// nx_adnet.nx -- LIB: the SOVEREIGN 4chan-style BANNER AD SERVER core (operator: "our ad network ... like how 4chan
2// has their banners"). First-party, same-origin, PRIVACY-RESPECTING: contextual by SECTION only -- NO cookies, NO
3// cross-site behavioral tracking, NO 3rd-party. Offsets hosting cost; site-level banners = safe-harbor-compatible
4// (platform ads, not per-content monetization). Inventory row: id<TAB>advertiser<TAB>img<TAB>clickurl<TAB>section<TAB>weight.
5// ad_pick(section, rot) -> rotate 1 of N matching the section ad_render_banner -> first-party <a><img> (click via our tracker)
6// ad_impression/ad_click -> append-only per-AD counters (not per-user) ad_click_target -> the redirect url
7// Self-contained (own parsing). No TLS. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9const K_MAGIC_1024: i64 = 1024
10
11func ad_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12func ad_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o }
13func ad_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 }
14func ad_eol(b: *u8, from: i64, blen: i64) -> i64 { var e: i64=from; while e<blen { if b[e]==10 as u8 { return e } e=e+1 } return blen }
15func ad_tab(b: *u8, from: i64, limit: i64) -> i64 { var e: i64=from; while e<limit { if b[e]==9 as u8 { return e } e=e+1 } return limit }
16func ad_field(row: *u8, rlen: i64, idx: i64, out: *u8) -> i64 { var fs: i64=0; var f: i64=0; while f<idx { let t: i64=ad_tab(row,fs,rlen); fs=t+1; f=f+1 } let fe: i64=ad_tab(row,fs,rlen); var o: i64=0; var k: i64=fs; while k<fe { out[o]=row[k]; o=o+1; k=k+1 } out[o]=0 as u8; return o }
17
18// count inventory rows whose SECTION field (idx 4) == section.
19func ad_section_count(inv: *u8, ilen: i64, section: *u8) -> i64 {
20 var cnt: i64=0; var ls: i64=0; let tmp: *u8=sys_mmap(256)
21 while ls<ilen { let le: i64=ad_eol(inv,ls,ilen); if le>ls { ad_field((inv as i64+ls) as *u8, le-ls, 4, tmp); if ad_streq(tmp, section)==1 { cnt=cnt+1 } } ls=le+1 }
22 return cnt
23}
24// ROTATE: copy the (rot mod matches)-th ad matching `section` into out (0-terminated row). Returns 1 if found.
25func ad_pick(inv: *u8, ilen: i64, section: *u8, rot: i64, out: *u8) -> i64 {
26 let matches: i64=ad_section_count(inv, ilen, section)
27 if matches==0 { out[0]=0 as u8; return 0 }
28 let target: i64=rot % matches
29 var seen: i64=0; var ls: i64=0; let tmp: *u8=sys_mmap(256)
30 while ls<ilen {
31 let le: i64=ad_eol(inv,ls,ilen)
32 if le>ls { ad_field((inv as i64+ls) as *u8, le-ls, 4, tmp)
33 if ad_streq(tmp, section)==1 { if seen==target { var o: i64=0; var k: i64=ls; while k<le { out[o]=inv[k]; o=o+1; k=k+1 } out[o]=0 as u8; return 1 } seen=seen+1 } }
34 ls=le+1
35 }
36 return 0
37}
38// RENDER a first-party banner: click goes through OUR /ad/click/<id> tracker (then redirects). No cookies, no 3rd-party,
39// no tracking pixel -> privacy-respecting + ad-block-resilient (same-origin). alt = advertiser (contextual only).
40func ad_render_banner(ad_row: *u8, rlen: i64, out: *u8) -> i64 {
41 let id: *u8=sys_mmap(128); ad_field(ad_row, rlen, 0, id)
42 let adv: *u8=sys_mmap(256); ad_field(ad_row, rlen, 1, adv)
43 let img: *u8=sys_mmap(K_MAGIC_1024); ad_field(ad_row, rlen, 2, img)
44 var o: i64=ad_cat(out, 0, "<a class=\"nx-ad\" rel=\"noopener nofollow\" href=\"/ad/click/" as *u8)
45 o=ad_cat(out, o, id); o=ad_cat(out, o, "\"><img src=\"" as *u8); o=ad_cat(out, o, img)
46 o=ad_cat(out, o, "\" alt=\"" as *u8); o=ad_cat(out, o, adv); o=ad_cat(out, o, "\" width=\"728\" height=\"90\" loading=\"lazy\"></a>" as *u8)
47 out[o]=0 as u8; return o
48}
49// APPEND-ONLY per-AD event counters (impressions / clicks) -- keyed by ad-id, NOT by user (no behavioral tracking).
50func ad_event(logbuf: *u8, llen: i64, ad_id: *u8) -> i64 { var o: i64=llen; o=ad_cat(logbuf,o,ad_id); logbuf[o]=10 as u8; o=o+1; return o }
51func ad_event_count(logbuf: *u8, llen: i64, ad_id: *u8) -> i64 {
52 let cl: i64=ad_slen(ad_id); var cnt: i64=0; var ls: i64=0
53 while ls<llen { let le: i64=ad_eol(logbuf,ls,llen); if le-ls==cl { var i: i64=0; var eq: i64=1; while i<cl { if logbuf[ls+i]!=ad_id[i] { eq=0 } i=i+1 } if eq==1 { cnt=cnt+1 } } ls=le+1 }
54 return cnt
55}
56// resolve a click: the redirect target (clickurl, field 3) for ad_id. Returns 1 if found.
57func ad_click_target(inv: *u8, ilen: i64, ad_id: *u8, out: *u8) -> i64 {
58 var ls: i64=0; let tmp: *u8=sys_mmap(256)
59 while ls<ilen { let le: i64=ad_eol(inv,ls,ilen)
60 if le>ls { ad_field((inv as i64+ls) as *u8, le-ls, 0, tmp)
61 if ad_streq(tmp, ad_id)==1 { ad_field((inv as i64+ls) as *u8, le-ls, 3, out); return 1 } }
62 ls=le+1 }
63 out[0]=0 as u8; return 0
64}