code wiki / _hdl_build / nx_vroom_deploy.nx

nx_vroom_deploy.nx source

↩ module page · 95 lines · 6400 B

1// nx_vroom_deploy.nx -- SOVEREIGN deploy of the video daemon to the live NAS: streams 2// /tmp/nx_vroom_daemon.sov.elf over the team's own SSH (ssh_put_file, no scp/3rd party), 3// then chmod + (re)start on :8444 + verifies endpoints locally on the box. New port = 4// zero impact on nginx/8443/8282 or the working Synology services. pw from /tmp/nxpw. 5// Long command strings live as file-scope consts (keeps main small -> no empty-.s). 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_ssh_lib.nx" 9const VR_MAGIC_1048576: i64 = 1048576 10const VR_MAGIC_16384: i64 = 16384 11const VR_MAGIC_65536: i64 = 65536 12const VR_MAGIC_1024: i64 = 1024 13 14const VR_WCMD: *u8 = "cat > /volume1/homes/elderwesto/nishihost/nx_vroom_daemon.elf.new" as *u8 15// kill any running daemon FIRST (via /proc -- no pgrep on synology) so the binary file 16// isn't busy (cat > a running exe = ETXTBSY) and the new one can bind :8444. 17const VR_KILL: *u8 = "for pid in /proc/[0-9]*; do if grep -qa nx_vroom_daemon \"$pid/cmdline\" 2>/dev/null; then kill -9 \"$(basename $pid)\" 2>/dev/null; fi; done; sleep 1; echo killed-old" as *u8 18// kill old (via /proc -- no pgrep on synology), mv the freshly-uploaded .new over the 19// (now-unused) binary, then start DETACHED with setsid (plain nohup dies on channel close). 20// Uploading to .new first avoids ETXTBSY (cat > a RUNNING exe fails). 21const VR_START: *u8 = "cd /volume1/homes/elderwesto/nishihost; for pid in /proc/[0-9]*; do if grep -qa nx_vroom_daemon \"$pid/cmdline\" 2>/dev/null; then kill -9 \"$(basename $pid)\" 2>/dev/null; fi; done; sleep 1; mv -f nx_vroom_daemon.elf.new nx_vroom_daemon.elf; chmod +x nx_vroom_daemon.elf; setsid ./nx_vroom_daemon.elf >vroom.log 2>&1 </dev/null & sleep 2; echo started; tail -2 vroom.log" as *u8 22const VR_V1: *u8 = "echo ===PORTS===; (ss -tln 2>/dev/null || netstat -tln 2>/dev/null) | grep 8444; echo ===PAGE===; curl -sk --max-time 6 https://localhost:8444/video/ -H 'Host: nishifamily.com' | head -c 70; echo" as *u8 23const VR_V2: *u8 = "echo ===POST===; printf NASFRAME | curl -sk --max-time 6 -X POST --data-binary @- 'https://localhost:8444/post?room=fam&id=100' -H 'Host: nishifamily.com'; echo; echo ===FRAMES101BYTES===; curl -sk --max-time 6 'https://localhost:8444/frames?room=fam&id=101' -H 'Host: nishifamily.com' | wc -c; echo; echo ===PAGE_HAS_CLIENT===; curl -sk --max-time 6 https://localhost:8444/video/ -H 'Host: nishifamily.com' | grep -ioE 'Nishi Family Video|getUserMedia|daemon live' | head -3" as *u8 24const VR_HTML_SRC: *u8 = "web_assets/vroom_client.html" as *u8 25const VR_HTML_WCMD: *u8 = "cat > /volume1/homes/elderwesto/nishihost/vroom_client.html" as *u8 26 27func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=0 as u8 { n=n+1 } return n } 28 29// stream data[0..datalen) to the remote, run as `wcmd` (e.g. "cat > /path"), chunked 16KB. 30// Copied from nx_ssh_put.nx (it's a per-tool fn, not exported by nx_ssh_lib). 31func ssh_put_file(st: *SshState, wcmd: *u8, wcmdlen: i64, data: *u8, datalen: i64) -> i64 { 32 let co: *u8 = sys_mmap(64); var c: i64 = 0 33 c = ssh_put_byte(co, c, 90); c = ssh_put_str(co, c, "session" as *u8, 7) 34 c = ssh_put_u32(co, c, 0); c = ssh_put_u32(co, c, VR_MAGIC_1048576); c = ssh_put_u32(co, c, VR_MAGIC_16384) 35 ssh_enc_send(st, co, c) 36 let rep: *u8 = sys_mmap(VR_MAGIC_65536); var rcid: i64 = 0 - 1; var guard: i64 = 0 37 while rcid < 0 { 38 if guard > 16 { return 0 - 1 } 39 let rl: i64 = ssh_enc_recv(st, rep); if rl < 0 { return 0 - 1 } 40 if rep[0] == 91 as u8 { rcid = ssh_u32be(rep, 5) } 41 if rep[0] == 92 as u8 { return 0 - 2 } 42 guard = guard + 1 43 } 44 let cr: *u8 = sys_mmap(VR_MAGIC_1024); var q: i64 = 0 45 q = ssh_put_byte(cr, q, 98); q = ssh_put_u32(cr, q, rcid); q = ssh_put_str(cr, q, "exec" as *u8, 4); q = ssh_put_byte(cr, q, 1); q = ssh_put_str(cr, q, wcmd, wcmdlen) 46 ssh_enc_send(st, cr, q) 47 var off: i64 = 0 48 while off < datalen { 49 var nn: i64 = datalen - off; if nn > VR_MAGIC_16384 { nn = VR_MAGIC_16384 } 50 let dp: *u8 = sys_mmap(nn + 64); var p: i64 = 0 51 p = ssh_put_byte(dp, p, 94); p = ssh_put_u32(dp, p, rcid); p = ssh_put_u32(dp, p, nn) 52 var i: i64 = 0; while i < nn { dp[p + i] = data[off + i]; i = i + 1 } p = p + nn 53 ssh_enc_send(st, dp, p) 54 off = off + nn 55 } 56 let eo: *u8 = sys_mmap(16); var e: i64 = 0; e = ssh_put_byte(eo, e, 96); e = ssh_put_u32(eo, e, rcid); ssh_enc_send(st, eo, e) 57 var done: i64 = 0; var loops: i64 = 0 58 while done == 0 { 59 if loops > VR_MAGIC_16384 { done = 1 } 60 let rl: i64 = ssh_enc_recv(st, rep); if rl < 0 { done = 1 } else { if rep[0] == 97 as u8 { done = 1 } } 61 loops = loops + 1 62 } 63 let clo: *u8 = sys_mmap(16); var k: i64 = 0; k = ssh_put_byte(clo, k, 97); k = ssh_put_u32(clo, k, rcid); ssh_enc_send(st, clo, k) 64 return 0 65} 66 67func main() -> i64 { 68 let pwbox: *i64 = sys_mmap(16) as *i64 69 let pw: *u8 = sys_read_file("/tmp/nxpw" as *u8, pwbox) 70 if (pw as i64) == 0 { ssh_puts("no /tmp/nxpw\n" as *u8); return 1 } 71 var pwlen: i64 = pwbox[0] 72 while pwlen > 0 { if pw[pwlen-1] == 10 as u8 { pwlen = pwlen - 1 } else { if pw[pwlen-1] == 13 as u8 { pwlen = pwlen - 1 } else { break } } } 73 74 let dbox: *i64 = sys_mmap(16) as *i64 75 let payload: *u8 = sys_read_file("/tmp/nx_vroom_daemon.sov.elf" as *u8, dbox) 76 if (payload as i64) == 0 { ssh_puts("no daemon elf in /tmp\n" as *u8); return 4 } 77 let plen: i64 = dbox[0] 78 79 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState 80 if ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 227) != 0 { ssh_puts("session fail\n" as *u8); return 2 } 81 if ssh_userauth_password(st, "elderwesto" as *u8, 10, pw, pwlen) != 1 { ssh_puts("auth fail\n" as *u8); sys_close(st.fd); return 3 } 82 ssh_puts("[auth ok] deploying sovereign video daemon...\n" as *u8) 83 84 ssh_put_file(st, VR_WCMD, slen(VR_WCMD), payload, plen) 85 86 // also upload the browser client HTML (daemon reads it at startup) 87 let hbox: *i64 = (sys_mmap(16)) as *i64 88 hbox[0] = 0 89 let html: *u8 = sys_read_file(VR_HTML_SRC, hbox) 90 if (html as i64) != 0 { if hbox[0] > 0 { ssh_put_file(st, VR_HTML_WCMD, slen(VR_HTML_WCMD), html, hbox[0]); ssh_puts("[client html uploaded]\n" as *u8) } } 91 92 ssh_puts("[uploaded: nx_vroom_daemon.elf.new + vroom_client.html -- start manually]\n" as *u8) 93 sys_close(st.fd) 94 return 0 95}