omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / interplane-forge / visits / owner.om
| 1 | note owner.om: the owner's pages, served inside the private network only. Composed after |
| 2 | note seed/net/http.om and seed/net/notls.om. |
| 3 | note owner A B C D PORT listen on the IPv4 address A.B.C.D |
| 4 | note It starts only on a loopback address (127.x.x.x) or a tailnet address (100.64.0.0/10); any other |
| 5 | note address is refused with exit 1, so the pages cannot be put on a public interface by mistake. |
| 6 | note It serves three files from the working directory, GET only, one request per connection: |
| 7 | note / visits.html text/html |
| 8 | note /tokens.css tokens.css text/css |
| 9 | note /visits.css visits.css text/css |
| 10 | note Every other path is 404 and every other method 405. Each answer carries Cache-Control: |
| 11 | note no-store, X-Robots-Tag: noindex and a policy that allows no script. A file is read whole, at |
| 12 | note most 1,048,576 bytes. Exit 1 usage or refused address, 2 cannot listen. |
| 13 | global oarg uchar 32 |
| 14 | text otget "GET" |
| 15 | text otroot "/" |
| 16 | text ottok "/tokens.css" |
| 17 | text otcss "/visits.css" |
| 18 | text ofhtm "visits.html" |
| 19 | text oftok "tokens.css" |
| 20 | text ofcss "visits.css" |
| 21 | text octn "Content-Type" |
| 22 | text octh "text/html; charset=utf-8" |
| 23 | text octc "text/css; charset=utf-8" |
| 24 | text occn "Cache-Control" |
| 25 | text occv "no-store" |
| 26 | text orbn "X-Robots-Tag" |
| 27 | text orbv "noindex" |
| 28 | text ocsn "Content-Security-Policy" |
| 29 | text ocsv "default-src 'none'; style-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'" |
| 30 | note oanum: argument idx as a decimal number; 99999999 when it is not 1 to 5 digits |
| 31 | fn oanum idx ulong to ulong |
| 32 | n = argread idx oarg 0 31 |
| 33 | if lt n 1 |
| 34 | ret 99999999 |
| 35 | end |
| 36 | if gt n 5 |
| 37 | ret 99999999 |
| 38 | end |
| 39 | v = ulong 0 |
| 40 | i = ulong 0 |
| 41 | while lt i ulong n bound 5 |
| 42 | c = get oarg i |
| 43 | if or lt c 48 gt c 57 |
| 44 | ret 99999999 |
| 45 | end |
| 46 | v = add mul v 10 ulong sub c 48 |
| 47 | i = add i 1 |
| 48 | end |
| 49 | ret v |
| 50 | end |
| 51 | note oaddrok: loopback or tailnet only |
| 52 | fn oaddrok a ulong b ulong to bool |
| 53 | if eq a 127 |
| 54 | ret true |
| 55 | end |
| 56 | if eq a 100 |
| 57 | ret and ge b 64 le b 127 |
| 58 | end |
| 59 | ret false |
| 60 | end |
| 61 | fn ofile cs edit ulong 320 io edit uchar 131072 f view uchar * ct view uchar * to long |
| 62 | fb = new uchar 1048576 0 |
| 63 | fd = int fopen f 0 len f 0 0 |
| 64 | if lt fd 0 |
| 65 | ret hrsperr cs io 404 |
| 66 | end |
| 67 | n = ulong 0 |
| 68 | k = long 1 |
| 69 | while gt k 0 bound 1048577 |
| 70 | k = read fd fb n sub 1048576 n |
| 71 | if gt k 0 |
| 72 | n = add n ulong k |
| 73 | end |
| 74 | end |
| 75 | c = fclose fd |
| 76 | r = hrspst cs io 200 |
| 77 | r = hfe r hhname cs io octn |
| 78 | r = hfe r hhval cs io ct |
| 79 | r = hfe r hhname cs io occn |
| 80 | r = hfe r hhval cs io occv |
| 81 | r = hfe r hhname cs io orbn |
| 82 | r = hfe r hhval cs io orbv |
| 83 | r = hfe r hhname cs io ocsn |
| 84 | r = hfe r hhval cs io ocsv |
| 85 | r = hfe r hhend cs io 0 n |
| 86 | if gt n 0 |
| 87 | r = hfe r hput cs io part fb 0 n |
| 88 | end |
| 89 | ret hfe r hwfin cs io |
| 90 | end |
| 91 | fn oserve c int cs edit ulong 320 io edit uchar 131072 to void |
| 92 | r = hinit cs io c 1 |
| 93 | if lt r 0 |
| 94 | ret |
| 95 | end |
| 96 | hsettmo cs 5000000000 5000000000 |
| 97 | k = hreqrd cs io |
| 98 | if lt k 0 |
| 99 | x = hreject cs io k |
| 100 | end |
| 101 | if gt k 0 |
| 102 | if not hismeth cs io otget |
| 103 | x = hrsperr cs io 405 |
| 104 | else |
| 105 | if hispath cs io otroot |
| 106 | x = ofile cs io ofhtm octh |
| 107 | else |
| 108 | if hispath cs io ottok |
| 109 | x = ofile cs io oftok octc |
| 110 | else |
| 111 | if hispath cs io otcss |
| 112 | x = ofile cs io ofcss octc |
| 113 | else |
| 114 | x = hrsperr cs io 404 |
| 115 | end |
| 116 | end |
| 117 | end |
| 118 | end |
| 119 | end |
| 120 | y = hclose cs |
| 121 | end |
| 122 | fn main to int |
| 123 | cs = new ulong 320 0 |
| 124 | io = new uchar 131072 0 |
| 125 | ad = new uchar 16 0 |
| 126 | if ne argcnt 6 |
| 127 | ret 1 |
| 128 | end |
| 129 | a = oanum 1 |
| 130 | b = oanum 2 |
| 131 | c = oanum 3 |
| 132 | d = oanum 4 |
| 133 | p = oanum 5 |
| 134 | if or or gt a 255 gt b 255 or gt c 255 gt d 255 |
| 135 | ret 1 |
| 136 | end |
| 137 | if or lt p 1 gt p 65535 |
| 138 | ret 1 |
| 139 | end |
| 140 | if not oaddrok a b |
| 141 | ret 1 |
| 142 | end |
| 143 | ip = uint add add add shl a 24 shl b 16 shl c 8 d |
| 144 | r = haddr4 ad ip p |
| 145 | lf = hlisten ad 16 |
| 146 | if lt lf 0 |
| 147 | ret 2 |
| 148 | end |
| 149 | for it ulong 4000000000 |
| 150 | s = haccept int lf 1000000000 |
| 151 | if ge s 0 |
| 152 | oserve int s cs io |
| 153 | end |
| 154 | end |
| 155 | ret 0 |
| 156 | end |