nx_source_health_probe.nx source
↩ module page · 199 lines · 8347 B
1// nx_source_health_probe.nx -- the BOUNDED PROBE half of /compare/mediaingest R1 (source health pre-flight).
2// The reusable decision, persistence, skip and no-reprobe logic all live in nx_source_health.nx (sh_preflight),
3// gate-proven by nx_source_health_gate 12/12 with planted numbers and no network. THIS file is the thin driver
4// that produces those numbers from a REAL remote host: it composes the shipped Chrome-hello fetch
5// (nx_https_fetch_follow_chrome -- the one that gets past anti-bot RSTs and follows redirects, giving both the
6// body and the HTTP status) and the shipped bot-wall classifier (nx_antibot abt_classify), times the round-trip,
7// and hands the outcome to sh_preflight, which classifies UP / SLOW / DEAD(null-routed) / WALLED and PERSISTS it.
8// The flagship case -- a null-routed CDN (Coomer, April 2026) -- is a connect that never completes: the fetch
9// returns no bytes and status 0, so connect_ok=0 and the verdict is DEAD, skipped with its reason, before any
10// item is queued against it.
11//
12// USAGE
13// nx_source_health_probe <url> probe the host, classify, persist, print the verdict row
14// nx_source_health_probe check <host> read the persisted verdict (the cheap row the queue reads); prints skip=0|1
15// A url begins with 'h' (http...) so it can never collide with the literal verb `check`.
16//
17// EXIT CODES (each is the verdict, so a caller can branch on it without parsing):
18// 0 UP 1 SLOW 2 DEAD 3 WALLED 4 UNKNOWN(could not load trust store / usage)
19// license_tier: ORIGINAL No hw writes (Rule 26).
20import "nx_syscalls.nx"
21import "nx_x509_trust_store.nx"
22import "nx_trust_store_load_from_certdata.nx"
23import "nx_https_fetch_follow.nx"
24import "nx_antibot.nx"
25import "nx_source_health.nx"
26
27const SHP_CERTDATA: *u8 = "data/mozilla_certdata.txt"
28const SHP_CERT_MAXCERTS: i64 = 512
29const SHP_CERT_BUFCAP: i64 = 4194304 // 4 MiB scratch for the CA bundle parse (matches nx_4chan)
30const SHP_RESP_CAP: i64 = 1048576 // 1 MiB: a health probe needs the head + a wall body, not a media payload
31const SHP_REDIRECTS: i64 = 6
32const SHP_HOST_CAP: i64 = 256
33const SHP_STATUS_SLOT: i64 = 8
34
35// extract the host authority from a URL into out (NUL-terminated); returns its length.
36// skips an optional scheme ("://"), stops at the first '/', ':' or NUL.
37func shp_host_of(url: *u8, out: *u8, cap: i64) -> i64 {
38 var slen: i64 = 0
39 while url[slen] != 0 { slen = slen + 1 }
40 // find the authority start: past "://" if present, else 0
41 var s: i64 = 0
42 var i: i64 = 0
43 var done: i64 = 0
44 while i + 2 < slen {
45 if done == 0 {
46 if url[i] == 58 { // ':'
47 if url[i+1] == 47 { if url[i+2] == 47 { s = i + 3; done = 1 } } // "://"
48 }
49 }
50 i = i + 1
51 }
52 // copy authority until '/', ':' or end -- flag-based stop, never a cursor clobber
53 var w: i64 = 0
54 var j: i64 = s
55 var stop: i64 = 0
56 while j < slen {
57 if stop == 0 {
58 let c: i64 = url[j] as i64
59 if c == 47 { stop = 1 } // '/'
60 if c == 58 { stop = 1 } // ':' (port)
61 if stop == 0 {
62 if w < cap - 1 { out[w] = url[j]; w = w + 1 }
63 }
64 }
65 j = j + 1
66 }
67 out[w] = 0 as u8
68 return w
69}
70
71func shp_streq(a: *u8, b: *u8) -> i64 {
72 var i: i64 = 0
73 while a[i] != 0 {
74 if a[i] != b[i] { return 0 }
75 i = i + 1
76 }
77 if b[i] != 0 { return 0 }
78 return 1
79}
80
81const SHP_HTTP_ERR_FLOOR: i64 = 400 // wall-classify ONLY a hard-block status; a clean or challenged 2xx is reachable = UP
82
83// return the offset of the body (just past the first "\r\n\r\n"), or 0 if no header terminator is found.
84// abt_classify scans page HTML for vendor markers; HTTP headers (server: cloudflare, cf-ray:, ...) are NOT the
85// body and must never be scanned, or every CDN-fronted host false-matches as a wall.
86func shp_body_start(buf: *u8, n: i64) -> i64 {
87 var i: i64 = 0
88 var off: i64 = 0
89 var found: i64 = 0
90 while i + 3 < n {
91 if found == 0 {
92 if buf[i] == 13 { if buf[i+1] == 10 { if buf[i+2] == 13 { if buf[i+3] == 10 { off = i + 4; found = 1 } } } }
93 }
94 i = i + 1
95 }
96 return off
97}
98
99func main(argc: i64, argv: *i64) -> i64 {
100 if argc < 2 {
101 sys_write(2, "usage: nx_source_health_probe <url> | nx_source_health_probe check <host>\n" as *u8, 72)
102 return SH_UNKNOWN
103 }
104 let a1: *u8 = argv[1] as *u8
105 let host: *u8 = sys_mmap(SHP_HOST_CAP)
106
107 // ---- check verb: read the persisted verdict (the cheap row the queue reads) ----
108 if shp_streq(a1, "check" as *u8) == 1 {
109 if argc < 3 { sys_write(2, "usage: nx_source_health_probe check <host>\n" as *u8, 42); return SH_UNKNOWN }
110 let h: *u8 = argv[2] as *u8
111 var hl: i64 = 0
112 while h[hl] != 0 { hl = hl + 1 }
113 let now_ms: i64 = sys_now_realtime_ms()
114 let v: i64 = sh_lookup(h, hl, now_ms)
115 let skip: i64 = sh_should_skip(v)
116 sys_write(1, "SOURCE-HEALTH-CHECK host=" as *u8, 25)
117 sys_write(1, h, hl)
118 sys_write(1, " verdict=" as *u8, 9)
119 let vs: *u8 = sh_verdict_str(v)
120 var vsl: i64 = 0
121 while vs[vsl] != 0 { vsl = vsl + 1 }
122 sys_write(1, vs, vsl)
123 if skip == 1 { sys_write(1, " skip=1\n" as *u8, 7) }
124 if skip == 0 { sys_write(1, " skip=0\n" as *u8, 7) }
125 return v
126 }
127
128 // ---- probe verb: a1 is the URL ----
129 let url: *u8 = a1
130 let hl: i64 = shp_host_of(url, host, SHP_HOST_CAP)
131
132 let tr: i64 = nx_trust_store_load_from_certdata(SHP_CERTDATA, SHP_CERT_MAXCERTS, SHP_CERT_BUFCAP)
133 if tr <= 0 {
134 sys_write(2, "SOURCE-HEALTH-PROBE UNKNOWN reason=trust-store-load-failed (cannot classify UP vs WALLED without it)\n" as *u8, 99)
135 return SH_UNKNOWN
136 }
137 let store: *TrustStore = tr as *TrustStore
138 let buf: *u8 = sys_mmap(SHP_RESP_CAP)
139 let stp: *i64 = sys_mmap(SHP_STATUS_SLOT) as *i64
140 stp[0] = 0
141
142 let t0: i64 = sys_now_realtime_ms()
143 let n: i64 = nx_https_fetch_follow_chrome(url, store, buf, SHP_RESP_CAP, SHP_REDIRECTS, stp)
144 let elapsed_ms: i64 = sys_now_realtime_ms() - t0
145 let status: i64 = stp[0]
146
147 var connect_ok: i64 = 0
148 if n > 0 { connect_ok = 1 }
149 if status > 0 { connect_ok = 1 }
150
151 // Reachability is the pre-flight question: a challenged or clean 2xx means the host ANSWERED (UP); a
152 // bot-shaped pre-flight getting a Cloudflare interstitial is not evidence the host is unusable -- that is
153 // R13's per-capture call on the real body. So classify a wall ONLY on a hard-block status (>=400), and
154 // scan the BODY, never the headers.
155 var wall_code: i64 = SH_ABT_CLEAN
156 if status >= SHP_HTTP_ERR_FLOOR {
157 if n > 0 {
158 let boff: i64 = shp_body_start(buf, n)
159 let bptr: *u8 = (buf as i64 + boff) as *u8
160 let blen: i64 = n - boff
161 if blen > 0 { wall_code = abt_classify(status, bptr, blen) }
162 }
163 }
164
165 let now_ms: i64 = sys_now_realtime_ms()
166 let v: i64 = sh_preflight(host, hl, connect_ok, status, elapsed_ms, wall_code, now_ms)
167
168 // machine-readable verdict row + the reason, on stdout
169 sys_write(1, "SOURCE-HEALTH-PROBE host=" as *u8, 25)
170 sys_write(1, host, hl)
171 sys_write(1, " verdict=" as *u8, 9)
172 let vs: *u8 = sh_verdict_str(v)
173 var vsl: i64 = 0
174 while vs[vsl] != 0 { vsl = vsl + 1 }
175 sys_write(1, vs, vsl)
176 sys_write(1, " status=" as *u8, 8)
177 shp_wnum(status)
178 sys_write(1, " elapsed_ms=" as *u8, 12)
179 shp_wnum(elapsed_ms)
180 sys_write(1, " slow_bar_ms=" as *u8, 13)
181 shp_wnum(sh_slow_ms())
182 sys_write(1, " connect_ok=" as *u8, 12)
183 shp_wnum(connect_ok)
184 sys_write(1, " bytes=" as *u8, 7)
185 shp_wnum(n)
186 let skip: i64 = sh_should_skip(v)
187 if skip == 1 { sys_write(1, " skip=1 persisted=1\n" as *u8, 19) }
188 if skip == 0 { sys_write(1, " skip=0 persisted=1\n" as *u8, 19) }
189 return v
190}
191
192// small unsigned/int decimal writer to fd 1 (no libc, no magic base literal beyond ASCII '0')
193func shp_wnum(x: i64) {
194 if x < 0 { sys_write(1, "-" as *u8, 1); shp_wnum(0 - x); return }
195 if x >= 10 { shp_wnum(x / 10) }
196 let d: *u8 = sys_mmap(1)
197 d[0] = (48 + (x % 10)) as u8
198 sys_write(1, d, 1)
199}