interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / runner / 20net.om

384 lines · 6995 bytes Raw Blame History
1note runner/20net.om -- outbound HTTP(S) to the hub, and the ipcc1 messages (docs/PROTOCOL.md).
2note The runner only ever dials out: it opens no listening socket. https:// URLs use the om/tls
3note TLS 1.3 client with the system trust store; plain http:// is accepted only for 127.0.0.1
4note (a private test hub on the same machine).
5
6text rtget "GET"
7text rtpost "POST"
8text rthost "Host"
9text rtuan "User-Agent"
10text rtuav "interplane-runner/1"
11text rtctn "Content-Type"
12text rtctv "text/plain"
13text rthttps "https://"
14text rthttp "http://127.0.0.1:"
15text rtlo "127.0.0.1"
16text rtapi "/community/api"
17text rtsalv "/salvage/"
18text rtrecs "/record"
19text rtipcc "ipcc1 "
20text rtsig "sig"
21text rtkeyn "key"
22text rttime "time"
23
24note r_hubset: parse the hub URL u ("https://HOST" or "https://HOST:PORT", or for tests
25note "http://127.0.0.1:PORT"); sets rhub, rhost, rport, rtls
26fn r_hubset u view uchar * to bool
27if or lt len u 9 gt len u 300
28ret false
29end
30tls = true
31o = len rthttps
32ht = false
33if gt len u len rthttp
34ht = r_eq part u 0 len rthttp rthttp
35end
36if ht
37tls = false
38o = sub len rthttp 1
39r_hostset rtlo
40else
41if not r_eq part u 0 len rthttps rthttps
42ret false
43end
44end
45e = len u
46c = r_colon u o
47if tls
48if not r_hostok part u o sub c o
49ret false
50end
51r_hostset part u o sub c o
52end
53port = ulong 443
54if lt c e
55port = r_undec part u add c 1 sub sub e c 1
56if or eq port 0 gt port 65535
57ret false
58end
59else
60if not tls
61ret false
62end
63end
64put rgv rport port
65put rgv rtls 0
66if tls
67put rgv rtls 1
68end
69for i ulong e bound 512
70put rhub i get u i
71end
72put rgv rhubl e
73ret true
74end
75
76note r_colon: the index of the first ':' in u at or after o, or len u
77fn r_colon u view uchar * o ulong to ulong
78c = o
79while lt c len u bound 512
80if eq get u c 58
81ret c
82end
83c = add c 1
84end
85ret c
86end
87
88note r_hostok: 1..253 bytes of a-z 0-9 . -
89fn r_hostok s view uchar * to bool
90if or eq len s 0 gt len s 253
91ret false
92end
93for i ulong len s bound 256
94ch = get s i
95lo = and ge ch 97 le ch 122
96dg = and ge ch 48 le ch 57
97if not or or lo dg or eq ch 46 eq ch 45
98ret false
99end
100end
101ret true
102end
103
104fn r_hostset s view uchar * to void
105for i ulong len s bound 256
106put rhost i get s i
107end
108put rgv rhstl len s
109end
110
111note r_call: one request to the hub; GET when rgv rpost is 0, else POST of rmsg. The response body
112note goes to rrsp. Returns the HTTP status, or negative: -1000 connect, -2000 trust store,
113note -413 body larger than rrsp, else the http.om / TLS result.
114fn r_call pth view uchar * to long
115cs = new ulong 320 0
116io = new uchar 262144 0
117st = new uchar 262144 0
118hb = new uchar 256 0
119hl = get rgv rhstl
120for i ulong hl bound 256
121put hb i get rhost i
122end
123tls = eq get rgv rtls 1
124if tls
125e = tls_store st
126if ne e 0
127ret -2000
128end
129end
130fd = tls_dial part hb 0 hl get rgv rport
131if lt fd 0
132ret -1000
133end
134r = hinit cs io int fd 0
135hsettmo cs 30000000000 30000000000
136htlsdl cs 120000000000
137if tls
138r = hfe r htlsinit cs io part hb 0 hl
139if ge r 0
140r = htlsconn cs io st
141end
142end
143if ge r 0
144r = r_xchg cs io pth
145end
146if tls
147x = htlsend cs io
148else
149x = hclose cs
150end
151ret r
152end
153
154fn r_xchg cs edit ulong * io edit uchar * pth view uchar * to long
155r = r_xhead cs io pth
156if lt r 0
157ret r
158end
159ret r_xresp cs io
160end
161
162note r_xhead: the request line, headers and (POST) body
163fn r_xhead cs edit ulong * io edit uchar * pth view uchar * to long
164hb = new uchar 256 0
165hl = get rgv rhstl
166for i ulong hl bound 256
167put hb i get rhost i
168end
169post = eq get rgv rpost 1
170r = long 0
171if post
172r = hreqst cs io rtpost
173else
174r = hreqst cs io rtget
175end
176r = hfe r hreqtg cs io pth
177r = hfe r hhname cs io rthost
178r = hfe r hhval cs io part hb 0 hl
179r = hfe r hhname cs io rtuan
180r = hfe r hhval cs io rtuav
181if post
182r = hfe r r_xbody cs io
183else
184r = hfe r hhend cs io 0 0
185end
186ret hfe r hwfin cs io
187end
188
189fn r_xbody cs edit ulong * io edit uchar * to long
190mb = new uchar 8192 0
191n = get rgv rmsgl
192for i ulong n bound 8192
193put mb i get rmsg i
194end
195r = hhname cs io rtctn
196r = hfe r hhval cs io rtctv
197r = hfe r hhend cs io 0 n
198ret hfe r hput cs io part mb 0 n
199end
200
201note r_xresp: the status, and the body into rrsp
202fn r_xresp cs edit ulong * io edit uchar * to long
203d = new uchar 16384 0
204s = hrsprd cs io
205if lt s 0
206ret s
207end
208put rgv rrspl 0
209for it ulong 999999
210k = hbodyrd cs io d
211if lt k 0
212ret k
213end
214if eq k 0
215ret s
216end
217if not r_rspapp part d 0 ulong k
218ret -413
219end
220end
221ret -413
222end
223
224note r_rspapp: append d to rrsp; false when it would not fit
225fn r_rspapp d view uchar * to bool
226m = get rgv rrspl
227if gt add m len d len rrsp
228ret false
229end
230for j ulong len d bound 16384
231put rrsp add m j get d j
232end
233put rgv rrspl add m len d
234ret true
235end
236
237note ---- messages
238fn r_madd s view uchar * to void
239n = get rgv rmsgl
240for i ulong len s bound 8192
241put rmsg add n i get s i
242end
243put rgv rmsgl add n len s
244end
245
246fn r_mbeg k view uchar * to void
247put rgv rmsgl 0
248r_madd rtipcc
249r_madd k
250r_madd rtnl
251end
252
253fn r_mline nm view uchar * v view uchar * to void
254r_madd nm
255r_madd rtsp
256r_madd v
257r_madd rtnl
258end
259
260fn r_mnum nm view uchar * v ulong to void
261d = new uchar 20 0
262k = r_dec d 0 v
263r_mline nm part d 0 k
264end
265
266note r_mkey: "key <key id>"
267fn r_mkey to void
268k = new uchar 64 0
269for i ulong 64
270put k i get rkid i
271end
272r_mline rtkeyn k
273end
274
275note r_mjf: "nm <rjf[o, o+n)>"
276fn r_mjf nm view uchar * o ulong n ulong to void
277d = new uchar 256 0
278for i ulong n bound 256
279put d i get rjf add o i
280end
281r_mline nm part d 0 n
282end
283
284note r_mtime: "time <unix seconds>"
285fn r_mtime to void
286r_mnum rttime ulong div realns 1000000000
287end
288
289note r_msign: append "sig <r||s hex>" over every byte of rmsg so far
290fn r_msign to bool
291m = new uchar 8192 0
292sg = new uchar 128 0
293n = get rgv rmsgl
294for i ulong n bound 8192
295put m i get rmsg i
296end
297if not r_ksign part m 0 n sg
298ret false
299end
300r_mline rtsig sg
301ret true
302end
303
304note r_post: POST rmsg to the hub API; the HTTP status or a negative r_call result
305fn r_post to long
306put rgv rpost 1
307ret r_call rtapi
308end
309
310note r_rspis: the response body starts with t
311fn r_rspis t view uchar * to bool
312if lt get rgv rrspl len t
313ret false
314end
315for i ulong len t bound 4096
316if ne get rrsp i get t i
317ret false
318end
319end
320ret true
321end
322
323note r_rspfld: the value of the response line "nm <value>" into d; its length must be len d
324fn r_rspfld nm view uchar * d edit uchar * to bool
325n = get rgv rrspl
326s = ulong 0
327while lt s n bound 65536
328ok = le add add s len nm 1 n
329if ok
330for i ulong len nm bound 64
331if ne get rrsp add s i get nm i
332ok = false
333end
334end
335end
336if ok
337ok = eq get rrsp add s len nm 32
338end
339if ok
340v = add add s len nm 1
341e = v
342while lt e n bound 65536
343if eq get rrsp e 10
344break
345end
346e = add e 1
347end
348if or ge e n ne sub e v len d
349ret false
350end
351for i ulong len d bound 65536
352put d i get rrsp add v i
353end
354ret true
355end
356while lt s n bound 65536
357if eq get rrsp s 10
358break
359end
360s = add s 1
361end
362s = add s 1
363end
364ret false
365end
366
367note r_rspline: the first response line to stdout (the hub's answer word), for the log
368fn r_rspline to void
369n = get rgv rrspl
370d = new uchar 256 0
371k = ulong 0
372while lt k n bound 256
373c = get rrsp k
374if eq c 10
375break
376end
377if or lt c 32 gt c 126
378c = 63
379end
380put d k c
381k = add k 1
382end
383w = write 1 d 0 k
384end