interplane.Get Omega
Read in your language

hive-view

aien-dev/hive-view

Brought by drakestapleton

branch main Branches Tags History

hive-view / src / web.om

700 lines · 14451 bytes Raw Blame History
1note hive-view src/web.om: HTTP routes. One forked process per browser connection (keep-alive).
2note Reads need the session cookie; the only write, POST /send, also needs the form key field k,
3note a same-origin Origin, a session name present in a fresh `tmux list-sessions`, and text that
4note is valid UTF-8 without control characters. It types that text literally (send-keys -l)
5note and then Enter, or sends Enter or Escape alone, and logs one line without the text.
6global gah uchar 1024
7global gaho ulong 8
8global gahl ulong 8
9global gahn ulong 1
10global glog uchar 1024
11global glogl ulong 1
12global glfd long 1
13text tget "GET"
14text tpost "POST"
15text troot "/"
16text tpcss "/app.css"
17text tpsend "/send"
18text tphealth "/health"
19text tfcss "app.css"
20text tctype "Content-Type"
21text tthtml "text/html; charset=utf-8"
22text ttcss "text/css; charset=utf-8"
23text tttext "text/plain; charset=utf-8"
24text tcc "Cache-Control"
25text tnostore "no-store"
26text tcsp "Content-Security-Policy"
27text tcspv "default-src 'none'; style-src 'self'; img-src 'self'; frame-src 'self'; form-action 'self'; frame-ancestors 'self'; base-uri 'none'"
28text txcto "X-Content-Type-Options"
29text tnosniff "nosniff"
30text trefp "Referrer-Policy"
31text tnoref "same-origin"
32text txfo "X-Frame-Options"
33text tdeny "SAMEORIGIN"
34text tsetck "Set-Cookie"
35text tckpre "hv="
36text tckattr "; Path=/; HttpOnly; SameSite=Strict; Max-Age=31536000"
37text tloc "Location"
38text thhost "host"
39text thcook "cookie"
40text thorig "origin"
41text thttp "http://"
42text tqt "/?t="
43text tok200 "ok\n"
44text tbadlink "This link is not valid. Ask for the current link (the token is in ~/.config/hive-view/token on the Spark).\n"
45text tnoauth "Not signed in: open the one-time link once on this device.\n"
46text tbadkey "Refused: the form key is wrong. Reload the page and try again.\n"
47text tnoorig "Refused: cross-origin request.\n"
48text tbadbody "Refused: malformed request.\n"
49text tbigbody "Refused: the text is longer than 4096 bytes.\n"
50text tbadname "Refused: that is not a session name the dashboard can type into.\n"
51text tnosess "Refused: no tmux session has that name right now.\n"
52text tbadact "Refused: unknown action (allowed: line, enter, escape).\n"
53text tbadtext "Refused: the text must be one line of printable characters (no control keys).\n"
54text ttmuxbad "tmux did not accept the keys; nothing more was sent.\n"
55text tlogbad "Refused: the send log ~/.local/state/hive-view/sends.log cannot be written, so nothing was sent.\n"
56text tnotfnd "Not found.\n"
57text tmeth "Method not allowed.\n"
58text tbadhost "Refused: unknown Host.\n"
59text tsendk "send-keys"
60text tdashl "-l"
61text tdd "--"
62text tenter "Enter"
63text tescape "Escape"
64text taline "line"
65text taenter "enter"
66text taesc "escape"
67text tlogp "/.local/state/hive-view/sends.log"
68text tlsfmt1 "#{session_name}"
69text tlsessn "session="
70text tlact " action="
71text tlbytes " bytes="
72text tlsha " sha256="
73text tlres " result="
74text tlok "ok"
75text tlnosess "refused-no-session"
76text tlbadtx "refused-text"
77text tlfail "tmux-failed"
78text tlatt "attempt"
79note ---- request checks
80fn vwhostok cs edit ulong 320 io edit uchar 131072 to bool
81i = hhfind cs io thhost
82if lt i 0
83ret false
84end
85vo = hhvoff cs ulong i
86vl = hhvlen cs ulong i
87for k ulong get gahn 0
88if vwahis io vo vl k
89ret true
90end
91end
92ret false
93end
94fn vwahis io edit uchar 131072 vo ulong vl ulong k ulong to bool
95if ne vl get gahl k
96ret false
97end
98b = get gaho k
99for i ulong vl
100if ne get io add vo i get gah add b i
101ret false
102end
103end
104ret true
105end
106note vwtokeq: io[off, off+64) equals the token; the comparison time does not depend on where it differs
107fn vwtokeq io edit uchar 131072 off ulong to bool
108if gt add off 64 len io
109ret false
110end
111d = uchar 0
112for i ulong 64
113d = or d xor get io add off i get gtok i
114end
115ret eq d 0
116end
117note vwauth: any Cookie header carries hv=<token> as one of its cookies
118fn vwauth cs edit ulong 320 io edit uchar 131072 to bool
119ok = false
120for i ulong get cs HHN
121if hieqm view part io hhnoff cs i hhnlen cs i thcook
122if vwckval io hhvoff cs i hhvlen cs i
123ok = true
124end
125end
126end
127ret ok
128end
129fn vwckval io edit uchar 131072 vo ulong vl ulong to bool
130p = ulong 0
131for it ulong 4096
132for s ulong 4096
133if ge p vl
134break
135end
136if ne get io add vo p 32
137break
138end
139p = add p 1
140end
141if le add p 67 vl
142if vwckat io add vo p
143if eq add p 67 vl
144ret vwtokeq io add add vo p 3
145end
146if eq get io add add vo p 67 59
147ret vwtokeq io add add vo p 3
148end
149end
150end
151for s ulong 4096
152if ge p vl
153ret false
154end
155if eq get io add vo p 59
156break
157end
158p = add p 1
159end
160p = add p 1
161end
162ret false
163end
164fn vwckat io edit uchar 131072 o ulong to bool
165if ne get io o 104
166ret false
167end
168if ne get io add o 1 118
169ret false
170end
171ret eq get io add o 2 61
172end
173note vworigin: the Origin header is present and exactly "http://" + the (already checked) Host.
174note Browsers send it on every form POST (Referrer-Policy same-origin keeps it from being "null").
175fn vworigin cs edit ulong 320 io edit uchar 131072 to bool
176i = hhfind cs io thorig
177if lt i 0
178ret false
179end
180h = hhfind cs io thhost
181if lt h 0
182ret false
183end
184oo = hhvoff cs ulong i
185ol = hhvlen cs ulong i
186ho = hhvoff cs ulong h
187hl = hhvlen cs ulong h
188m = len thttp
189if ne ol add m hl
190ret false
191end
192if not heq view part io oo m thttp
193ret false
194end
195ret vwioeq io add oo m ho hl
196end
197fn vwioeq io edit uchar 131072 a ulong b ulong n ulong to bool
198for i ulong n
199if ne get io add a i get io add b i
200ret false
201end
202end
203ret true
204end
205note ---- responses
206fn vwsech cs edit ulong 320 io edit uchar 131072 to long
207r = hhname cs io tcc
208r = hfe r hhval cs io tnostore
209r = hfe r hhname cs io txcto
210r = hfe r hhval cs io tnosniff
211r = hfe r hhname cs io trefp
212r = hfe r hhval cs io tnoref
213r = hfe r hhname cs io txfo
214r = hfe r hhval cs io tdeny
215r = hfe r hhname cs io tcsp
216ret hfe r hhval cs io tcspv
217end
218fn vwtext cs edit ulong 320 io edit uchar 131072 code ulong t view uchar * to long
219r = hrspst cs io code
220r = hfe r vwsech cs io
221r = hfe r hhname cs io tctype
222r = hfe r hhval cs io tttext
223r = hfe r hhend cs io 0 len t
224r = hfe r hput cs io t
225ret hfe r hwfin cs io
226end
227fn vwcss cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 to long
228vwpb
229vwpt tfcss
230n = vwrdbuf buf
231if lt n 0
232ret vwtext cs io 404 tnotfnd
233end
234r = hrspst cs io 200
235r = hfe r vwsech cs io
236r = hfe r hhname cs io tctype
237r = hfe r hhval cs io ttcss
238r = hfe r hhend cs io 0 ulong n
239r = hfe r hput cs io view part buf 0 ulong n
240ret hfe r hwfin cs io
241end
242note vwrdbuf: the whole file at gpth into buf (refused when it does not fit)
243fn vwrdbuf buf edit uchar 262144 to long
244fd = fopen gpth 0 get gpl 0 524288 0
245if lt fd 0
246ret fd
247end
248n = ulong 0
249for it ulong 100000
250if ge n len buf
251x = fclose int fd
252ret -27
253end
254r = read int fd buf n sub len buf n
255if lt r 0
256x = fclose int fd
257ret r
258end
259if eq r 0
260break
261end
262n = add n ulong r
263end
264x = fclose int fd
265ret long n
266end
267note vwlogin: GET /?t=<64 hex> sets the HttpOnly SameSite=Strict cookie and redirects to /
268fn vwlogin cs edit ulong 320 io edit uchar 131072 to long
269tg = get cs HTOFF
270tl = get cs HTLEN
271ok = false
272if eq tl add len tqt 64
273if heq view part io tg len tqt tqt
274ok = vwtokeq io add tg len tqt
275end
276end
277if not ok
278ret vwtext cs io 403 tbadlink
279end
280ck = new uchar 160 0
281n = ulong 0
282for i ulong len tckpre
283put ck n get tckpre i
284n = add n 1
285end
286for i ulong 64
287put ck n get gtok i
288n = add n 1
289end
290for i ulong len tckattr
291put ck n get tckattr i
292n = add n 1
293end
294r = hrspst cs io 302
295r = hfe r vwsech cs io
296r = hfe r hhname cs io tsetck
297r = hfe r hhval cs io view part ck 0 n
298r = hfe r hhname cs io tloc
299r = hfe r hhval cs io troot
300r = hfe r hhend cs io 0 0
301ret hfe r hwfin cs io
302end
303fn vwnlat buf edit uchar 262144 p ulong n ulong to ulong
304for i ulong sub n p
305if eq get buf add p i 10
306ret add p i
307end
308end
309ret n
310end
311fn vwbeq buf edit uchar 262144 a ulong n ulong t view uchar * to bool
312if ne n len t
313ret false
314end
315for i ulong n
316if ne get buf add a i get t i
317ret false
318end
319end
320ret true
321end
322note vwsendp: body "NAME<LF>ACTION<LF>TEXT". Returns 0 sent, 1 malformed, 2 bad name,
323note 3 bad action, 4 bad text, 5 no such session, 6 tmux failed, 7 text too long, 8 send log
324note cannot be opened or the attempt line cannot be written (both before anything is typed).
325note Codes 1, 2, 3 and 7 are refused before the log is opened. The form handler (route.om vwfbuild)
326note never passes text with Enter or Escape; code 4 for that case guards direct callers only.
327fn vwsendp buf edit uchar 262144 n ulong to ulong
328e1 = vwnlat buf 0 n
329if ge e1 n
330ret 1
331end
332e2 = vwnlat buf add e1 1 n
333if ge e2 n
334ret 1
335end
336if or eq e1 0 gt e1 63
337ret 2
338end
339for i ulong e1
340if not vwnamec get buf i
341ret 2
342end
343end
344act = ulong 9
345if vwbeq buf add e1 1 sub sub e2 e1 1 taline
346act = 0
347end
348if vwbeq buf add e1 1 sub sub e2 e1 1 taenter
349act = 1
350end
351if vwbeq buf add e1 1 sub sub e2 e1 1 taesc
352act = 2
353end
354if eq act 9
355ret 3
356end
357t0 = add e2 1
358tn = sub n t0
359if gt tn 4096
360ret 7
361end
362if ne act 0
363if ne tn 0
364ret 4
365end
366end
367vwhpath tlogp
368lf = fopen gpth 0 get gpl 0 525377 384
369if lt lf 0
370ret 8
371end
372put glfd 0 lf
373if not vwutf8 view part buf t0 tn
374x = vwlogl buf e1 act t0 tn 1
375vwlcl
376ret 4
377end
378if not vwlive buf e1
379x = vwlogl buf e1 act t0 tn 2
380vwlcl
381ret 5
382end
383if not vwlogl buf e1 act t0 tn 4
384vwlcl
385ret 8
386end
387ok = vwtype buf e1 act t0 tn
388if ok
389x = vwlogl buf e1 act t0 tn 0
390vwlcl
391ret 0
392end
393x = vwlogl buf e1 act t0 tn 3
394vwlcl
395ret 6
396end
397note vwlcl: close the send log opened by vwsendp
398fn vwlcl to void
399x = fclose int get glfd 0
400end
401note vwutf8: well-formed UTF-8 with no C0 control, DEL or C1 control character
402fn vwutf8 a view uchar * to bool
403n = len a
404i = ulong 0
405for it ulong 8192
406if ge i n
407ret true
408end
409c = get a i
410if lt c 128
411if or lt c 32 eq c 127
412ret false
413end
414i = add i 1
415continue
416end
417k = vwu8len a i
418if eq k 0
419ret false
420end
421i = add i k
422end
423ret false
424end
425note vwu8len: length of the well-formed multi-byte sequence at a[i], or 0 (RFC 3629 table 3-7)
426fn vwu8len a view uchar * i ulong to ulong
427n = len a
428c = get a i
429lo = uchar 128
430hi = uchar 191
431k = ulong 0
432if and ge c 194 le c 223
433k = 2
434if eq c 194
435lo = 160
436end
437end
438if and ge c 224 le c 239
439k = 3
440if eq c 224
441lo = 160
442end
443if eq c 237
444hi = 159
445end
446end
447if and ge c 240 le c 244
448k = 4
449if eq c 240
450lo = 144
451end
452if eq c 244
453hi = 143
454end
455end
456if eq k 0
457ret 0
458end
459if gt add i k n
460ret 0
461end
462b = get a add i 1
463if or lt b lo gt b hi
464ret 0
465end
466for j ulong sub k 2
467b = get a add i add j 2
468if or lt b 128 gt b 191
469ret 0
470end
471end
472ret k
473end
474note vwlive: buf[0, e1) is exactly one line of a fresh `tmux list-sessions -F #{session_name}`
475fn vwlive buf edit uchar 262144 e1 ulong to bool
476vwkb
477vwka ttmux
478vwka tlsess
479vwka tdashf
480vwka tlsfmt1
481r = vwrun 3000000000
482if ne r 0
483ret false
484end
485a = ulong 0
486n = get gol 0
487for i ulong n
488if eq get gob i 10
489if vwobeq buf e1 a i
490ret true
491end
492a = add i 1
493end
494end
495ret false
496end
497fn vwobeq buf edit uchar 262144 e1 ulong a ulong b ulong to bool
498if ne sub b a e1
499ret false
500end
501for i ulong e1
502if ne get gob add a i get buf i
503ret false
504end
505end
506ret true
507end
508note vwtarget: the packed argument "=NAME:" (exact session match, its active pane)
509fn vwtarget buf edit uchar 262144 e1 ulong to void
510vwkopen
511vwkc 61
512for i ulong e1
513vwkc get buf i
514end
515vwkc 58
516end
517fn vwkey buf edit uchar 262144 e1 ulong key view uchar * to bool
518vwkb
519vwka ttmux
520vwka tsendk
521vwka tdasht
522vwtarget buf e1
523vwka key
524ret eq vwrun 5000000000 0
525end
526note vwtype: send-keys -l in chunks of at most 2048 bytes cut at character boundaries (a packed
527note argument list holds 4096 bytes), then Enter. tmux 3.4 would pass a split character through
528note intact, but each chunk is a separate write to the program in the pane, and a reader that
529note decodes each write alone would show a broken character. tmux reads an argument ending in ';'
530note as a command separator; such a chunk is sent as "...\;", which tmux turns back into "...;".
531fn vwtype buf edit uchar 262144 e1 ulong act ulong t0 ulong tn ulong to bool
532if eq act 1
533ret vwkey buf e1 tenter
534end
535if eq act 2
536ret vwkey buf e1 tescape
537end
538p = t0
539e = add t0 tn
540for it ulong 8
541if ge p e
542break
543end
544q = hmin e add p 2048
545for s ulong 4
546if ge q e
547break
548end
549if ne and get buf q 192 128
550break
551end
552q = sub q 1
553end
554if not vwchunk buf e1 p q
555ret false
556end
557p = q
558end
559ret vwkey buf e1 tenter
560end
561fn vwchunk buf edit uchar 262144 e1 ulong p ulong q ulong to bool
562vwkb
563vwka ttmux
564vwka tsendk
565vwka tdasht
566vwtarget buf e1
567vwka tdashl
568vwka tdd
569vwkopen
570for i ulong sub q p
571if and eq add p i sub q 1 eq get buf add p i 59
572vwkc 92
573end
574vwkc get buf add p i
575end
576ret eq vwrun 5000000000 0
577end
578note ---- the send log: one line per attempt that reached the session check (never the text)
579fn vwlc c uchar to void
580n = get glogl 0
581if lt n len glog
582put glog n c
583put glogl 0 add n 1
584end
585end
586fn vwlt t view uchar * to void
587for i ulong len t
588vwlc get t i
589end
590end
591fn vwld v ulong w ulong to void
592d = new uchar 20 0
593i = ulong 20
594x = v
595for it ulong 20
596i = sub i 1
597put d i uchar add rem x 10 48
598x = div x 10
599if and eq x 0 ge sub 20 i w
600break
601end
602end
603for k ulong sub 20 i
604vwlc get d add i k
605end
606end
607note vwltime: realns as UTC YYYY-MM-DDTHH:MM:SSZ (civil-from-days, proleptic Gregorian)
608fn vwltime to void
609s = div ulong realns 1000000000
610vwldate div s 86400
611sec = rem s 86400
612vwlc 84
613vwld div sec 3600 2
614vwlc 58
615vwld div rem sec 3600 60 2
616vwlc 58
617vwld rem sec 60 2
618vwlc 90
619end
620fn vwldate days ulong to void
621z = add days 719468
622era = div z 146097
623doe = sub z mul era 146097
624yoe = vwyoe doe
625doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
626mp = div add mul 5 doy 2 153
627m = add mp 3
628if ge mp 10
629m = sub mp 9
630end
631y = add yoe mul era 400
632if le m 2
633y = add y 1
634end
635vwld y 4
636vwlc 45
637vwld m 2
638vwlc 45
639vwld add sub doy div add mul 153 mp 2 5 1 2
640end
641fn vwyoe doe ulong to ulong
642a = sub doe div doe 1460
643b = add a div doe 36524
644ret div sub b div doe 146096 365
645end
646fn vwlres res ulong to void
647if eq res 0
648vwlt tlok
649ret
650end
651if eq res 1
652vwlt tlbadtx
653ret
654end
655if eq res 2
656vwlt tlnosess
657ret
658end
659if eq res 4
660vwlt tlatt
661ret
662end
663vwlt tlfail
664end
665note vwlogl: append one line to the open send log; true only when the whole line was written
666fn vwlogl buf edit uchar 262144 e1 ulong act ulong t0 ulong tn ulong res ulong to bool
667put glogl 0 0
668vwltime
669vwlc 32
670vwlt tlsessn
671for i ulong e1
672vwlc get buf i
673end
674vwlt tlact
675if eq act 0
676vwlt taline
677end
678if eq act 1
679vwlt taenter
680end
681if eq act 2
682vwlt taesc
683end
684vwlt tlbytes
685vwld tn 1
686vwlt tlsha
687dg = new uchar 32 0
688k = sha2_digest 256 view part buf t0 tn dg
689for i ulong 32
690c = get dg i
691vwlc get thexd shr c 4
692vwlc get thexd and c 15
693end
694vwlt tlres
695vwlres res
696vwlc 10
697fd = get glfd 0
698w = write int fd glog 0 get glogl 0
699ret eq w long get glogl 0
700end