code wiki / _hdl_build / nx_site_probe.nx
nx_site_probe.nx source
↩ module page · 65 lines · 4006 B
1// nx_site_probe.nx -- diagnostic: GET several paths on nishifamily.com through our own TLS-1.3 + Mozilla CA and
2// DUMP status + the first bytes of each served body, so we can see what a BROWSER actually gets at /, /library,
3// /library.html, /hub/ (marker-presence checks can pass while the visible page is a placeholder / different
4// route). license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_x509_trust_store.nx"
7import "nx_trust_store_load_from_certdata.nx"
8import "nx_tls13_client_validate_certificate.nx"
9import "nx_tls13_client_session_run.nx"
10import "nx_https_url_for_fetch.nx"
11import "nx_https_url_connect.nx"
12import "nx_https_get.nx"
13import "nx_https_get_complete.nx"
14import "nx_http_response_parse.nx"
15const K_MAGIC_262144: i64 = 262144
16const K_MAGIC_4194304: i64 = 4194304
17
18func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func pn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=(48 as u8);k=1}; while m>0{t[k]=((48+(m%10)) as u8);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
20func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
21
22// GET https://nishifamily.com<path> and dump status + first `headn` body bytes (control chars -> space).
23func probe(store: *TrustStore, path: *u8, headn: i64) -> i64 {
24 let plen: i64 = slen(path)
25 let url: *u8 = sys_mmap(256); var o: i64=0
26 let pre: *u8 = "https://nishifamily.com"; var i: i64=0; while pre[i]!=(0 as u8){ url[o]=pre[i]; o=o+1; i=i+1 }
27 i=0; while path[i]!=(0 as u8){ url[o]=path[i]; o=o+1; i=i+1 } url[o]=0 as u8
28
29 let url_p: *NxUrl = nx_url_new()
30 let target: *NxHttpsTarget = sys_mmap(64) as *NxHttpsTarget
31 target.url = url_p; target.port = 0
32 pw("GET "); pw(path); pw(" -> ")
33 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { pw("url-parse-fail\n"); return 0 }
34 let fd_p: *i64 = sys_mmap(16) as *i64
35 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { pw("connect-fail\n"); return 0 }
36 let fd: i64 = fd_p[0]
37 let cr: *u8 = sys_mmap(32); var z: i64=0; while z<32 { cr[z]=(0xC0+z) as u8; z=z+1 }
38 let priv: *u8 = sys_mmap(32); z=0; while z<32 { priv[z]=(0xA0+z) as u8; z=z+1 }
39 let vctx: *TlsValidationContext = sys_mmap(64) as *TlsValidationContext
40 vctx.store = store; vctx.sni_host = ((url as i64)+target.url.host_off) as *u8; vctx.sni_host_len = target.url.host_len; vctx.now_epoch = sys_now_realtime_sec()
41 let sr: i64 = nx_tls13_client_session_run(fd, ((url as i64)+target.url.host_off) as *u8, target.url.host_len, cr, priv, vctx)
42 if sr <= 0 { pw("tls-fail\n"); return 0 }
43 let buf: *u8 = sys_mmap(K_MAGIC_262144)
44 let gc: i64 = nx_https_get_complete(sr as *Tls13ClientSession, fd, path, plen, ((url as i64)+target.url.host_off) as *u8, target.url.host_len, buf, K_MAGIC_262144)
45 sys_close(fd)
46 var status: i64=0; var body_off: i64=0
47 let pr: *i64 = sys_mmap(128) as *i64; if nx_http_response_parse(buf, gc, pr)==0 { status=pr[1]; body_off=pr[6] }
48 pw("status="); pn(status); pw(" bytes="); pn(gc); pw("\n head: ")
49 var c: i64=0; var p2: i64=body_off
50 while c < headn { if p2 >= gc { c = headn } else { let ch: i64 = buf[p2] as i64; let one: *u8=sys_mmap(2); if ch<32 { one[0]=32 as u8 } else { one[0]=buf[p2] } sys_write(1,one,1); p2=p2+1; c=c+1 } }
51 pw("\n\n")
52 return 0
53}
54
55func main() -> i64 {
56 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 300, K_MAGIC_4194304)
57 if r <= 0 { pw("no CA\n"); sys_exit(1); return 1 }
58 let store: *TrustStore = r as *TrustStore
59 pw("=== SITE PROBE: what does each path actually serve? ===\n\n")
60 probe(store, "/library" as *u8, 160)
61 probe(store, "/library/" as *u8, 160)
62 probe(store, "/hub" as *u8, 160)
63 probe(store, "/hub/" as *u8, 160)
64 sys_exit(0); return 0
65}