code wiki / _hdl_build / nx_video_native_client_gate.nx
nx_video_native_client_gate.nx source
↩ module page · 92 lines · 6015 B
1// nx_video_native_client_gate.nx -- MOBILE-FIRST, ZERO-JS proof (task #22). A pure-NishiLang client with
2// NO browser, NO JavaScript, and NO wasm is a FULL room participant on the LIVE edge: native peer A packs a
3// REAL room chat message with the SAME core the browser runs (vc_chat_pack -> vc_wire_pack, kind 0x43),
4// sends it over sovereign TLS-1.3 + RFC-6455 WSS to nishifamily.com/signal/<room>, and native peer B
5// RECEIVES it via the relay's content-blind broadcast and parses it back with the SAME core (vc_wire_parse
6// -> vc_chat_parse), asserting kind 0x43 and byte-exact text. GREEN = the mobile-native core works end to
7// end on the real relay. This is the concrete answer to "why JS": for anything that isn't a stock browser,
8// we use NONE -- the identical NishiLang core runs native (and compiles to ARM for a phone the same way).
9// usage: nx_video_native_client_gate (room fixed "nxnative")
10// expect_exit: 0 license_tier: ORIGINAL
11import "nx_ws_client_session.nx" // wscs_connect / wscs_send / wscs_ws_recv (+ transitive TLS/WS/syscalls/trust)
12import "nx_video_client_wasm.nx" // the SAME browser core, native: vc_chat_pack / vc_wire_pack / vc_wire_parse / vc_chat_parse
13
14func nw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func nn(v: i64) -> i64 {
16 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
17 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}
18 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
19func fwln(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
20
21func main() -> i64 {
22 nw("=== nx_video_native_client_gate: pure-nx (no browser/JS/wasm) full room participant on the LIVE relay ===\n" as *u8)
23 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 512, 4194304)
24 if r <= 0 { nw("trust store load failed\n" as *u8); return 1 }
25 let store: *TrustStore = r as *TrustStore
26 let url: *u8 = "https://nishifamily.com/signal/nxnative\x00" as *u8
27 nw(" connecting native peer A + native peer B to /signal/nxnative (TLS-1.3 + WSS, sovereign) ...\n" as *u8)
28 let sa: *i64 = sys_mmap(16) as *i64
29 let fa: *i64 = sys_mmap(16) as *i64
30 let sb: *i64 = sys_mmap(16) as *i64
31 let fb: *i64 = sys_mmap(16) as *i64
32 if wscs_connect(store, url, 0xC0, sa, fa) != 1 { nw("NATIVE PEER-A connect/upgrade FAILED\n" as *u8); return 1 }
33 nw(" native peer A: WebSocket 101 OK\n" as *u8)
34 if wscs_connect(store, url, 0x30, sb, fb) != 1 { nw("NATIVE PEER-B connect/upgrade FAILED\n" as *u8); return 1 }
35 nw(" native peer B: WebSocket 101 OK (both in the room, ZERO browser)\n" as *u8)
36 let sessA: *Tls13ClientSession = sa[0] as *Tls13ClientSession
37 let sessB: *Tls13ClientSession = sb[0] as *Tls13ClientSession
38
39 // ---- A builds a REAL room chat frame with the SAME core the browser uses ----
40 let name: *u8 = "nishi-native" as *u8
41 let nlen: i64 = 12
42 let text: *u8 = "HELLO FROM A NATIVE NISHI CLIENT -- NO BROWSER, NO JS, NO WASM" as *u8
43 var tlen: i64 = 0
44 while text[tlen]!=(0 as u8) { tlen = tlen + 1 }
45 let chatbuf: *u8 = sys_mmap(1024)
46 let clen: i64 = vc_chat_pack(chatbuf, name, nlen, text, tlen)
47 if clen < 0 { nw("vc_chat_pack failed\n" as *u8); return 1 }
48 let id8: *u8 = sys_mmap(16)
49 var z: i64 = 0
50 while z < 8 { id8[z] = (65 + z) as u8; z = z + 1 }
51 let wirebuf: *u8 = sys_mmap(2048)
52 let wlen: i64 = vc_wire_pack(wirebuf, 0x43, id8, 1, chatbuf, clen)
53 let frame: *u8 = sys_mmap(4096)
54 let mask: i64 = 0x5A6B7C8D
55 let hn: i64 = ws_build_header(frame, 4096, 1, WS_OP_BINARY, 1, mask, wlen)
56 var c: i64 = 0
57 while c < wlen { frame[hn + c] = wirebuf[c]; c = c + 1 }
58 ws_apply_mask(frame, hn, wlen, mask)
59 if wscs_send(sessA, fa[0], frame, hn + wlen) != 0 { nw("A send FAILED\n" as *u8); return 1 }
60 nw(" native peer A packed vc_wire_pack(kind=0x43, vc_chat_pack(name,text)) and sent it (wire=" as *u8); nn(wlen); nw("B)\n" as *u8)
61
62 // ---- B receives the frame, parses with the SAME core, verifies the text round-trips ----
63 let got: *u8 = sys_mmap(4096)
64 let glen: i64 = wscs_ws_recv(sessB, fb[0], got, 4096)
65 sys_close(fa[0]); sys_close(fb[0])
66 if glen <= 0 { nw("B received nothing -> RED\n" as *u8); return 1 }
67 let winfo: *i64 = sys_mmap(64) as *i64
68 if vc_wire_parse(got, glen, winfo) != 0 { nw("vc_wire_parse failed -> RED\n" as *u8); return 1 }
69 let kind: i64 = winfo[0]
70 let payoff: i64 = winfo[2]
71 let paylen: i64 = winfo[3]
72 nw(" native peer B received " as *u8); nn(glen); nw("B; vc_wire_parse kind=" as *u8); nn(kind); nw(" (want 67=0x43) paylen=" as *u8); nn(paylen); nw("\n" as *u8)
73 if kind != 0x43 { nw("wrong kind -> RED\n" as *u8); return 1 }
74 let chatpay: *u8 = ((got as i64) + payoff) as *u8
75 let cinfo: *i64 = sys_mmap(64) as *i64
76 if vc_chat_parse(chatpay, paylen, cinfo) != 0 { nw("vc_chat_parse failed -> RED\n" as *u8); return 1 }
77 let toff: i64 = cinfo[2]
78 let tl: i64 = cinfo[3]
79 var okv: i64 = 1
80 if tl != tlen { okv = 0 }
81 if okv == 1 { var i: i64 = 0; while i < tl { if chatpay[toff + i] != text[i] { okv = 0; i = tl } else { i = i + 1 } } }
82 nw(" decoded chat text: '" as *u8); if tl > 0 { sys_write(1, ((chatpay as i64) + toff) as *u8, tl) } nw("'\n" as *u8)
83
84 let lf: i64 = sys_openat_append("knowledge/status/video_native.log" as *u8, 420)
85 if lf >= 0 {
86 if okv == 1 { fwln(lf, "VIDEONATIVE a_upgrade=1 b_upgrade=1 kind=0x43 text_roundtrip=GREEN\n" as *u8) } else { fwln(lf, "VIDEONATIVE verdict=RED\n" as *u8) }
87 sys_close(lf)
88 }
89 if okv == 1 { nw("NATIVE-CLIENT verdict=GREEN -- a pure-NishiLang client (no browser/JS/wasm) is a FULL room participant on the LIVE relay\n" as *u8); return 0 }
90 nw("NATIVE-CLIENT verdict=RED -- chat text did not round-trip\n" as *u8)
91 return 1
92}