Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/hive-view/blob/main/src/main.om # hive-view aien-dev/hive-view Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `hive-view / src / main.om` 408 lines ยท 7213 bytes Raw Blame History 1 note hive-view src/main.om: entry points. 2 note server serve RUNDIR PORT ADDR [ADDR] listen on each IPv4 ADDR:PORT (127.0.0.1 or the 3 note Tailscale range 100.64.0.0/10 only), fork the 4 note shared poller and one process per connection 5 note server mktoken create ~/.config/hive-view/token (mode 600, 64 hex) 6 note server poll-once RUNDIR one full poller pass, then exit (tests) 7 note Exit codes: 2 usage, 3 token missing or unsafe, 4 address refused, 5 listen failed, 8 note 6 HOME unset, 17 token already exists, 18 token could not be created or written, 9 note 19 an internal path or tmux argument buffer would overflow (util.om vwpc, vwkc). 10 global gpoll uchar 32 11 global glf ulong 4 12 global glfn ulong 1 13 text tserve "serve" 14 text tmktok "mktoken" 15 text tpoll1 "poll-once" 16 text tenvh "HOME=" 17 text tcfgd "/.config/hive-view" 18 text ttokf "/.config/hive-view/token" 19 text tstd1 "/.local/state" 20 text tstd2 "/.local/state/hive-view" 21 text tlhost "localhost:" 22 text tlspark "spark.hub.internal:" 23 text tlspk "spark:" 24 text tmsgtok "token written to ~/.config/hive-view/token (mode 600)\n" 25 text tmsgex "refused: ~/.config/hive-view/token already exists\n" 26 text tmsgcr "refused: cannot create ~/.config/hive-view/token (does ~/.config exist and is it writable?)\n" 27 fn vwhome to bool 28 eb = new uchar 600 0 29 for i ulong ulong envcnt 30 n = envread i eb 0 600 31 if gt n 5 32 if heq view part eb 0 5 tenvh 33 k = hmin sub ulong n 5 511 34 for j ulong k 35 put ghome j get eb add 5 j 36 end 37 put ghomel 0 k 38 ret gt k 0 39 end 40 end 41 end 42 ret false 43 end 44 note vwarg: argument i into ab; its length, or 0 45 fn vwarg i ulong ab edit uchar 512 to ulong 46 if ge i ulong argcnt 47 ret 0 48 end 49 n = argread i ab 0 512 50 if le n 0 51 ret 0 52 end 53 ret ulong n 54 end 55 fn vwargis ab edit uchar 512 n ulong t view uchar * to bool 56 ret heq view part ab 0 n t 57 end 58 note vwtokld: the token file is ours, mode 0600 (no group/other bits), and 64 lowercase hex 59 fn vwtokld to bool 60 vwhpath ttokf 61 fd = vwfopen 62 if lt fd 0 63 ret false 64 end 65 r = fstat int fd gstb 0 128 66 if lt r 0 67 x = fclose int fd 68 ret false 69 end 70 mode = vwst32 16 71 uid = vwst32 24 72 size = vwst64 48 73 ok = and eq and mode 63 0 eq uid ulong getuid 74 ok = and ok or eq size 64 eq size 65 75 if ok 76 r = read int fd gtok 0 64 77 ok = eq r 64 78 end 79 x = fclose int fd 80 if not ok 81 ret false 82 end 83 for i ulong 64 84 if not vwhex get gtok i 85 ret false 86 end 87 end 88 ret true 89 end 90 fn vwmktok to int 91 vwhpath tcfgd 92 r = makedir gpth 0 get gpl 0 448 93 vwhpath ttokf 94 fd = fopen gpth 0 get gpl 0 524481 384 95 if lt fd 0 96 if eq fd -17 97 x = write 2 tmsgex 0 len tmsgex 98 ret 17 99 end 100 x = write 2 tmsgcr 0 len tmsgcr 101 ret 18 102 end 103 rb = new uchar 32 0 104 hx = new uchar 65 0 105 k = random rb 0 32 0 106 if ne k 32 107 x = fclose int fd 108 ret 18 109 end 110 for i ulong 32 111 c = get rb i 112 put hx mul i 2 get thexd shr c 4 113 put hx add mul i 2 1 get thexd and c 15 114 end 115 put hx 64 10 116 w = write int fd hx 0 65 117 s = fsync int fd 118 x = fclose int fd 119 if ne w 65 120 ret 18 121 end 122 x = write 1 tmsgtok 0 len tmsgtok 123 ret 0 124 end 125 note vwip: dotted IPv4 in ab[0, n) as a host-order uint, or 0 when malformed 126 fn vwip ab edit uchar 512 n ulong to ulong 127 v = ulong 0 128 part8 = ulong 0 129 digs = ulong 0 130 dots = ulong 0 131 for i ulong n 132 c = get ab i 133 if eq c 46 134 if or eq digs 0 gt part8 255 135 ret 0 136 end 137 v = or shl v 8 part8 138 part8 = 0 139 digs = 0 140 dots = add dots 1 141 else 142 if not vwdig c 143 ret 0 144 end 145 part8 = add mul part8 10 ulong sub c 48 146 digs = add digs 1 147 if gt digs 3 148 ret 0 149 end 150 end 151 end 152 if or or ne dots 3 eq digs 0 gt part8 255 153 ret 0 154 end 155 ret or shl v 8 part8 156 end 157 note vwipok: loopback 127.0.0.1 or the Tailscale CGNAT range 100.64.0.0/10; never 0.0.0.0 158 fn vwipok ip ulong to bool 159 if eq ip `2130706433` 160 ret true 161 end 162 ret eq shr ip 22 401 163 end 164 fn vwahadd ab edit uchar 512 n ulong to void 165 k = get gahn 0 166 if ge k 8 167 ret 168 end 169 b = ulong 0 170 if gt k 0 171 b = add get gaho sub k 1 get gahl sub k 1 172 end 173 if gt add b add n 6 len gah 174 ret 175 end 176 for i ulong n 177 put gah add b i get ab i 178 end 179 put gaho k b 180 put gahl k n 181 put gahn 0 add k 1 182 end 183 fn vwahport t view uchar * to void 184 ab = new uchar 512 0 185 n = ulong 0 186 for i ulong len t 187 put ab n get t i 188 n = add n 1 189 end 190 vwahtail ab n 191 end 192 note vwahtail: append ":PORT" (unless already ends in ':') and add as an allowed Host 193 fn vwahtail ab edit uchar 512 n ulong to void 194 m = n 195 if gt m 0 196 if ne get ab sub m 1 58 197 put ab m 58 198 m = add m 1 199 end 200 end 201 d = new uchar 20 0 202 i = ulong 20 203 x = get gport 0 204 for it ulong 20 205 i = sub i 1 206 put d i uchar add rem x 10 48 207 x = div x 10 208 if eq x 0 209 break 210 end 211 end 212 for k ulong sub 20 i 213 put ab m get d add i k 214 m = add m 1 215 end 216 vwahadd ab m 217 end 218 fn vwlisten ab edit uchar 512 n ulong tail bool to int 219 ip = vwip ab n 220 if eq ip 0 221 ret 4 222 end 223 if not vwipok ip 224 ret 4 225 end 226 a = new uchar 16 0 227 r = haddr4 a uint ip get gport 0 228 lf = hlisten a 64 229 if lt lf 0 230 ret 5 231 end 232 k = get glfn 0 233 put glf k ulong lf 234 put glfn 0 add k 1 235 vwahtail ab n 236 if tail 237 vwahport tlspark 238 vwahport tlspk 239 end 240 ret 0 241 end 242 fn vwclosel to void 243 for k ulong get glfn 0 244 x = fclose int get glf k 245 end 246 end 247 fn vwspawn to long 248 pid = fork 249 if eq pid 0 250 vwclosel 251 vwpoll 252 exit 0 253 end 254 ret pid 255 end 256 fn vwpollset to ulong 257 for k ulong get glfn 0 258 b = mul k 8 259 fd = get glf k 260 put gpoll b uchar and fd 255 261 put gpoll add b 1 uchar and shr fd 8 255 262 put gpoll add b 2 0 263 put gpoll add b 3 0 264 put gpoll add b 4 1 265 put gpoll add b 5 0 266 put gpoll add b 6 0 267 put gpoll add b 7 0 268 end 269 ret mul get glfn 0 8 270 end 271 fn vwaccept1 k ulong to long 272 if eq and get gpoll add mul k 8 6 1 0 273 ret 0 274 end 275 c = accept int get glf k 526336 276 if lt c 0 277 ret 0 278 end 279 pid = fork 280 if eq pid 0 281 vwclosel 282 vwconn c 283 exit 0 284 end 285 x = fclose int c 286 if gt pid 0 287 ret 1 288 end 289 ret 0 290 end 291 fn vwserve to int 292 ab = new uchar 512 0 293 n = vwarg 2 ab 294 if or eq n 0 gt n 400 295 ret 2 296 end 297 for i ulong n 298 put grun i get ab i 299 end 300 put grunl 0 n 301 n = vwarg 3 ab 302 if not hdigits view part ab 0 n 303 ret 2 304 end 305 port = hdecv view part ab 0 n 306 if or eq port 0 gt port 65535 307 ret 2 308 end 309 put gport 0 port 310 if not vwtokld 311 ret 3 312 end 313 put gahn 0 0 314 vwahport tlhost 315 argn = ulong argcnt 316 if or lt argn 5 gt argn 6 317 ret 2 318 end 319 for i ulong sub argn 4 320 n = vwarg add i 4 ab 321 r = vwlisten ab n not eq vwip ab n `2130706433` 322 if ne r 0 323 ret r 324 end 325 end 326 vwhpath tstd1 327 r = makedir gpth 0 get gpl 0 448 328 vwhpath tstd2 329 r = makedir gpth 0 get gpl 0 448 330 r = makedir grun 0 get grunl 0 448 331 ret vwloop 332 end 333 fn vwloop to int 334 poller = vwspawn 335 backoff = long 0 336 active = ulong 0 337 np = vwpollset 338 for it ulong `9223372036854775807` 339 for k ulong 128 340 w = waitpid -1 gwst 0 4 1 341 if le w 0 342 break 343 end 344 if eq w poller 345 poller = -1 346 backoff = add long monons `2000000000` 347 else 348 if gt active 0 349 active = sub active 1 350 end 351 end 352 end 353 if and lt poller 0 gt long monons backoff 354 poller = vwspawn 355 end 356 if ge active 48 357 x = sleepns `50000000` 358 continue 359 end 360 for k ulong 4 361 put gpoll add mul k 8 6 0 362 end 363 r = pollfds gpoll 0 np `500000000` 364 if le r 0 365 continue 366 end 367 for k ulong get glfn 0 368 active = add active ulong vwaccept1 k 369 end 370 end 371 ret 0 372 end 373 fn vwpollonce to int 374 ab = new uchar 512 0 375 n = vwarg 2 ab 376 if or eq n 0 gt n 400 377 ret 2 378 end 379 for i ulong n 380 put grun i get ab i 381 end 382 put grunl 0 n 383 r = makedir grun 0 get grunl 0 448 384 vwinitsec 385 put ggen 0 vwnowms 386 vwpanes 387 vwslow 388 vwkanban 389 vwindex 0 390 ret 0 391 end 392 fn main to int 393 if not vwhome 394 ret 6 395 end 396 ab = new uchar 512 0 397 n = vwarg 1 ab 398 if vwargis ab n tserve 399 ret vwserve 400 end 401 if vwargis ab n tmktok 402 ret vwmktok 403 end 404 if vwargis ab n tpoll1 405 ret vwpollonce 406 end 407 ret 2 408 end