pinokiod 3.8.149 → 3.8.151

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/kernel/caddy2.js DELETED
@@ -1,666 +0,0 @@
1
- const axios = require('axios')
2
- const path = require('path')
3
- const Util = require('./util')
4
- class Caddy {
5
- constructor(kernel) {
6
- this.kernel = kernel
7
- }
8
- peers_home (payload) {
9
- let peers = this.kernel.peer.get()
10
- for(let ip of peers) {
11
- let account = this.kernel.peer.account(ip)
12
- let namespaced_url = `pinokio.${account}.localhost`
13
- payload.apps.http.servers.main.routes.push({
14
- "match": [{ "host": [namespaced_url] }],
15
- "handle": [
16
- {
17
- "handler": "reverse_proxy",
18
- "upstreams": [{ "dial": `${ip}:42000` }]
19
- },
20
- {
21
- "handler": "headers",
22
- "response": {
23
- "set": {
24
- "Access-Control-Allow-Origin": ["*"],
25
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
26
- "Access-Control-Allow-Headers": ["*"],
27
- "Vary": ["Origin"]
28
- }
29
- }
30
- }
31
- ]
32
- })
33
- }
34
- return payload
35
- }
36
- home () {
37
- let internal_ip = "127.0.0.1:42000"
38
- let namespaced_url = "pinokio.localhost"
39
-
40
- let current_lan_ip = this.kernel.peer.current_ip
41
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
42
-
43
- //if (!this._mapping[internal_ip]) {
44
- // this._mapping[internal_ip] = new Set()
45
- //}
46
- //this._mapping[internal_ip].add(namespaced_url)
47
- if (!this._mapping[current_lan_ip]) {
48
- this._mapping[current_lan_ip] = {}
49
- }
50
- if (!this._mapping[current_lan_ip][internal_ip]) {
51
- this._mapping[current_lan_ip][internal_ip] = new Set()
52
- }
53
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
54
-
55
- return {
56
- "apps": {
57
- "http": {
58
- "servers": {
59
- "main": {
60
- "listen": [":443"],
61
- "routes": [
62
- {
63
- "match": [{ "method": ["OPTIONS"] }],
64
- "handle": [
65
- {
66
- "handler": "headers",
67
- "response": {
68
- "set": {
69
- "Access-Control-Allow-Origin": ["*"],
70
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
71
- "Access-Control-Allow-Headers": ["*"],
72
- "Vary": ["Origin"],
73
- "X-Forwarded-Proto": ["https"],
74
- "X-Forwarded-Host": ["{http.request.host}"]
75
- }
76
- }
77
- },
78
- {
79
- "handler": "static_response",
80
- "status_code": 204
81
- }
82
- ]
83
- },
84
- {
85
- "match": [{ "host": [namespaced_url] }],
86
- "handle": [
87
- {
88
- "handler": "reverse_proxy",
89
- "upstreams": [{ "dial": internal_ip }]
90
- },
91
- {
92
- "handler": "headers",
93
- "response": {
94
- "set": {
95
- "Access-Control-Allow-Origin": ["*"],
96
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
97
- "Access-Control-Allow-Headers": ["*"],
98
- "Vary": ["Origin"]
99
- }
100
- }
101
- }
102
- ]
103
- },
104
- ]
105
- }
106
- }
107
- }
108
- }
109
- }
110
- }
111
- async peers_locals(payload) {
112
- let peers = this.kernel.peer.get()
113
- for(let ip of peers) {
114
- let account = this.kernel.peer.account(ip)
115
- let local = this.kernel.peer.local(ip)
116
- // construct Caddy update payload
117
- console.log({ local })
118
- for(let script_path in local) {
119
- let local_variables = local[script_path]
120
- console.log({ local_variables })
121
- for(let key in local_variables) {
122
- let val = local_variables[key]
123
- if (val.startsWith("http")) {
124
-
125
- // val :- https?:(some url)
126
-
127
-
128
- let internal_ip = val.replace(/https?:\/\//, '')
129
-
130
- let api_path = path.resolve(this.kernel.peer.homedir(ip), "api")
131
- let rel_path = path.relative(api_path, script_path)
132
- let name = rel_path.split(path.sep)[0]
133
- console.log({ api_path, rel_path, name, internal_ip })
134
-
135
- let chunks = internal_ip.split(":")
136
- if (chunks.length === 2) {
137
- // 1. if the URL is a localhost url with port
138
- let INTERNAL_HOST = chunks[0]
139
- let INTERNAL_PORT = chunks[1]
140
-
141
- this.ip_mapping[INTERNAL_PORT] = ip
142
-
143
-
144
- // replace 127,0.0.1 or localhost with the 192.168 address
145
- //val = val.replace(/localhost|127\.0\.0\.1/g, ip)
146
-
147
-
148
- // create a proxy proxy
149
- // - get next available port and expose
150
- payload.apps.http.servers[ip + ":" + INTERNAL_PORT] = {
151
- "listen": ["0.0.0.0:PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT],
152
- "routes": [{
153
- "handle": [
154
- {
155
- "handler": "reverse_proxy",
156
- "upstreams": [{ "dial": internal_ip }]
157
- },
158
- {
159
- "handler": "headers",
160
- "response": {
161
- "set": {
162
- "Access-Control-Allow-Origin": ["*"],
163
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
164
- "Access-Control-Allow-Headers": ["*"],
165
- "Vary": ["Origin"]
166
- }
167
- }
168
- }
169
- ]
170
- }]
171
- }
172
-
173
- let current_lan_ip = ip
174
- let current_lan_username = this.kernel.peer.account(ip)
175
-
176
- // proxy proxy => proxy
177
- if (key === "url") {
178
- let namespaced_url = `${name}.${account}.localhost`.toLowerCase()
179
- console.log("Adding namespaced_url 1", { name, account, namespaced_url })
180
- //if (!this._mapping[internal_ip]) {
181
- // this._mapping[internal_ip] = new Set()
182
- //}
183
- //this._mapping[internal_ip].add(namespaced_url)
184
-
185
- if (!this._mapping[current_lan_ip]) {
186
- this._mapping[current_lan_ip] = {}
187
- }
188
- if (!this._mapping[current_lan_ip][internal_ip]) {
189
- this._mapping[current_lan_ip][internal_ip] = new Set()
190
- }
191
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
192
-
193
- payload.apps.http.servers.main.routes.push({
194
- "match": [{
195
- "host": [namespaced_url]
196
- }],
197
- "handle": [
198
- {
199
- "handler": "reverse_proxy",
200
- "upstreams": [{ "dial": ip + ":PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT }]
201
- },
202
- {
203
- "handler": "headers",
204
- "response": {
205
- "set": {
206
- "Access-Control-Allow-Origin": ["*"],
207
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
208
- "Access-Control-Allow-Headers": ["*"],
209
- "Vary": ["Origin"]
210
- }
211
- }
212
- }
213
- ]
214
- })
215
- }
216
- let namespaced_url = `${key}.${name}.${account}.localhost`.toLowerCase()
217
- console.log("Adding namespaced_url 2", { name, account, namespaced_url })
218
- //if (!this._mapping[internal_ip]) {
219
- // this._mapping[internal_ip] = new Set()
220
- //}
221
- //this._mapping[internal_ip].add(namespaced_url)
222
-
223
- if (!this._mapping[current_lan_ip]) {
224
- this._mapping[current_lan_ip] = {}
225
- }
226
- if (!this._mapping[current_lan_ip][internal_ip]) {
227
- this._mapping[current_lan_ip][internal_ip] = new Set()
228
- }
229
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
230
-
231
- payload.apps.http.servers.main.routes.push({
232
- "match": [{
233
- "host": [namespaced_url],
234
- }],
235
- "handle": [
236
- {
237
- "handler": "reverse_proxy",
238
- "upstreams": [{ "dial": ip + ":PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT }]
239
- },
240
- {
241
- "handler": "headers",
242
- "response": {
243
- "set": {
244
- "Access-Control-Allow-Origin": ["*"],
245
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
246
- "Access-Control-Allow-Headers": ["*"],
247
- "Vary": ["Origin"]
248
- }
249
- }
250
- }
251
- ]
252
- })
253
- } else {
254
-
255
- // 2. if otherwise (anything, like https://google.com)
256
- // DO NOT create a proxy
257
- }
258
-
259
- }
260
- }
261
- }
262
- }
263
- return payload
264
- }
265
- locals (payload) {
266
- // construct Caddy update payload
267
- let local = this.kernel.memory.local;
268
- console.log({ local })
269
- for(let script_path in local) {
270
- let local_variables = local[script_path]
271
- console.log({ local_variables })
272
- for(let key in local_variables) {
273
- let val = local_variables[key]
274
- if (val.startsWith("http")) {
275
- val = val.replace(/https?:\/\//, '')
276
- let internal_ip = val
277
- let api_path = this.kernel.path("api")
278
- let rel_path = path.relative(api_path, script_path)
279
- let name = rel_path.split(path.sep)[0]
280
- console.log({ api_path, rel_path, name, val })
281
- let chunks = internal_ip.split(":")
282
- if (chunks.length === 2) {
283
- if (key === "url") {
284
- //if (!this._mapping[val]) {
285
- // this._mapping[val] = new Set()
286
- //}
287
- //this._mapping[val].add(namespaced_url)
288
-
289
-
290
-
291
- let namespaced_url = `${name}.localhost`.toLowerCase()
292
- console.log("Adding namespaced_url 3", { name, namespaced_url })
293
- let current_lan_ip = this.kernel.peer.current_ip
294
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
295
- if (!this._mapping[current_lan_ip]) {
296
- this._mapping[current_lan_ip] = {}
297
- }
298
- if (!this._mapping[current_lan_ip][internal_ip]) {
299
- this._mapping[current_lan_ip][internal_ip] = new Set()
300
- }
301
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
302
-
303
- payload.apps.http.servers.main.routes.push({
304
- "match": [{
305
- "host": [namespaced_url]
306
- }],
307
- "handle": [
308
- {
309
- "handler": "reverse_proxy",
310
- "upstreams": [{ "dial": val }]
311
- },
312
- {
313
- "handler": "headers",
314
- "response": {
315
- "set": {
316
- "Access-Control-Allow-Origin": ["*"],
317
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
318
- "Access-Control-Allow-Headers": ["*"],
319
- "Vary": ["Origin"]
320
- }
321
- }
322
- }
323
- ]
324
- })
325
- }
326
- let namespaced_url = `${key}.${name}.localhost`.toLowerCase()
327
- console.log("Adding namespaced_url 4", { key, name, namespaced_url })
328
-
329
- let current_lan_ip = this.kernel.peer.current_ip
330
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
331
- if (!this._mapping[current_lan_ip]) {
332
- this._mapping[current_lan_ip] = {}
333
- }
334
- if (!this._mapping[current_lan_ip][internal_ip]) {
335
- this._mapping[current_lan_ip][internal_ip] = new Set()
336
- }
337
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
338
-
339
- payload.apps.http.servers.main.routes.push({
340
- "match": [{
341
- "host": [namespaced_url],
342
- }],
343
- "handle": [
344
- {
345
- "handler": "reverse_proxy",
346
- "upstreams": [{ "dial": val }]
347
- },
348
- {
349
- "handler": "headers",
350
- "response": {
351
- "set": {
352
- "Access-Control-Allow-Origin": ["*"],
353
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
354
- "Access-Control-Allow-Headers": ["*"],
355
- "Vary": ["Origin"]
356
- }
357
- }
358
- }
359
- ]
360
- })
361
- } else {
362
- // 2. if otherwise (anything, like https://google.com)
363
- // do NOT create a proxy
364
- }
365
- }
366
- }
367
- }
368
- return payload
369
- }
370
- async peers_procs (payload) {
371
- console.log("####### procs", payload)
372
- let list = this.kernel.peer.get()
373
- console.log(">> peers list", list)
374
- for(let ip of list) {
375
- let account = this.kernel.peer.account(ip)
376
- console.log({ account })
377
-
378
- // if ip === current_lan_ip
379
- // => trust the info on itself only
380
- // => only use the current info, ignore the rest
381
- // ip ip !== current_lan_ip => just pick one
382
- // => trust the info coming from ip only
383
- // => only use the info for that node
384
-
385
-
386
- // caddy_mapping := { IP => HTTPS_DOMAIN }
387
-
388
- // get the mapping for the current node
389
- // for current node, only trust the info from the current node
390
- // for other nodes, just use the first one to avoid registering duplicates
391
-
392
- let https_domains_mapping = this.mapping(ip)[ip]
393
- console.log({ https_domains_mapping, ip })
394
- for(let internal_ip in https_domains_mapping) {
395
-
396
- // create a proxy proxy
397
- // 1. create an exposed port listening on 0.0.0.0
398
- // 2. the exposed port points to the internal ip
399
- let chunks = internal_ip.split(":")
400
- let INTERNAL_HOST = chunks[0]
401
- let INTERNAL_PORT = chunks[1]
402
-
403
- this.ip_mapping[INTERNAL_PORT] = ip
404
-
405
- payload.apps.http.servers[ip + ":" + INTERNAL_PORT] = {
406
- "listen": ["0.0.0.0:PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT],
407
- "routes": [{
408
- "handle": [
409
- {
410
- "handler": "reverse_proxy",
411
- "upstreams": [{ "dial": internal_ip }]
412
- },
413
- {
414
- "handler": "headers",
415
- "response": {
416
- "set": {
417
- "Access-Control-Allow-Origin": ["*"],
418
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
419
- "Access-Control-Allow-Headers": ["*"],
420
- "Vary": ["Origin"]
421
- }
422
- }
423
- }
424
- ]
425
- }]
426
- }
427
-
428
-
429
- //let https_domains = caddy_mapping[internal_ip]
430
- let https_domains = https_domains_mapping[internal_ip]
431
- console.log(">>>>>>>> https_domains", https_domains)
432
-
433
- // 1. if https_domains is made up of one item <port>.localhost,
434
- // add <port>.<account>.localhost
435
- // 2. if https_domains is made up of two items <port>.localhost and <port>.<account>.localhost, don't do anything
436
- for(let https_domain of https_domains) {
437
- // insert the $account value right before the last part (1234.localhost => 1234.x.localhost)
438
- let chunks = https_domain.split(".")
439
- let namespaced_url
440
-
441
- if (https_domain.endsWith(`${account}.localhost`)) {
442
- // already accountified
443
- namespaced_url = https_domain
444
- } else {
445
- chunks.splice(chunks.length - 1, 0, account)
446
- namespaced_url = chunks.join(".")
447
- }
448
-
449
- // if (chunks.length === 3) {
450
- // namespaced_url = https_domain
451
- // } else if (chunks.length === 2) {
452
- // chunks.splice(chunks.length - 1, 0, account)
453
- // namespaced_url = chunks.join(".")
454
- // }
455
- console.log("##x", { chunks, namespaced_url, https_domain })
456
-
457
-
458
- //if (!this._mapping[internal_ip]) {
459
- // this._mapping[internal_ip] = new Set()
460
- //}
461
- //console.log("this.mapping", this._mapping)
462
- //console.log("internal_ip", internal_ip)
463
- //console.log("result", this._mapping[internal_ip])
464
- //this._mapping[internal_ip].add(namespaced_url)
465
- //console.log({ namespaced_url, https_domain, internal_ip })
466
-
467
-
468
- let current_lan_ip = ip
469
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
470
- if (!this._mapping[current_lan_ip]) {
471
- this._mapping[current_lan_ip] = {}
472
- }
473
- if (!this._mapping[current_lan_ip][internal_ip]) {
474
- this._mapping[current_lan_ip][internal_ip] = new Set()
475
- }
476
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
477
- console.log("Adding", current_lan_ip, internal_ip, namespaced_url)
478
-
479
-
480
- // 1. point proxy URL to the 127.0.0.1:EXPOSED_PORT
481
- payload.apps.http.servers.main.routes.push({
482
- "match": [{
483
- "host": [namespaced_url],
484
- }],
485
- "handle": [
486
- {
487
- "handler": "reverse_proxy",
488
- "upstreams": [{ "dial": ip + ":PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT }]
489
- },
490
- {
491
- "handler": "headers",
492
- "response": {
493
- "set": {
494
- "Access-Control-Allow-Origin": ["*"],
495
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
496
- "Access-Control-Allow-Headers": ["*"],
497
- "Vary": ["Origin"]
498
- }
499
- }
500
- }
501
- ]
502
- })
503
- }
504
- }
505
- }
506
- return payload
507
- }
508
- async procs (payload) {
509
- console.log("####### procs", payload)
510
- let list = this.kernel.processes.list
511
- if (!list) {
512
- list = await this.kernel.processes.refresh()
513
- }
514
- console.log(">>>>>>>>>> LIST", list)
515
- for(let item of list) {
516
- let namespaced_url = `${item.port}.localhost`
517
- //if (!this._mapping[item.ip]) {
518
- // this._mapping[item.ip] = new Set()
519
- //}
520
- //this._mapping[item.ip].add(namespaced_url)
521
-
522
- let internal_ip = item.ip
523
- let current_lan_ip = this.kernel.peer.current_ip
524
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
525
- if (!this._mapping[current_lan_ip]) {
526
- this._mapping[current_lan_ip] = {}
527
- }
528
- if (!this._mapping[current_lan_ip][internal_ip]) {
529
- this._mapping[current_lan_ip][internal_ip] = new Set()
530
- }
531
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
532
-
533
- payload.apps.http.servers.main.routes.push({
534
- "match": [{
535
- "host": [namespaced_url],
536
- }],
537
- "handle": [
538
- {
539
- "handler": "reverse_proxy",
540
- "upstreams": [{ "dial": item.ip }]
541
- },
542
- {
543
- "handler": "headers",
544
- "response": {
545
- "set": {
546
- "Access-Control-Allow-Origin": ["*"],
547
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
548
- "Access-Control-Allow-Headers": ["*"],
549
- "Vary": ["Origin"]
550
- }
551
- }
552
- }
553
- ]
554
- })
555
- }
556
- return payload
557
- }
558
- peers(payload) {
559
- let peers = this.kernel.peer.get()
560
- for(let peer of peers) {
561
- let account = peer.username
562
- }
563
- }
564
- async update() {
565
- let payload = await this.payload()
566
- console.log("Make request to caddy", JSON.stringify(payload, null, 2))
567
- let response = await axios.post('http://127.0.0.1:2019/load', payload, {
568
- headers: { 'Content-Type': 'application/json' }
569
- }).catch((e) => {
570
- console.log("EEEEEEEEEEEEE", e)
571
- })
572
- console.log("Caddy Response", { response })
573
- }
574
- async payload () {
575
- this._mapping = {}
576
- this.ip_mapping = {}
577
- this.port_mapping = {} // 127.0.0.1 => 192.168,..
578
- this.local_network_mapping = {}
579
-
580
- // localhost home
581
- let payload = this.home()
582
- // peers home
583
- let peers = this.kernel.peer.get()
584
- payload = this.peers_home(payload)
585
-
586
- // localhost local variables
587
- payload = this.locals(payload)
588
- // peers local variables
589
- payload = await this.peers_locals(payload)
590
-
591
- payload = await this.procs(payload)
592
- payload = await this.peers_procs(payload)
593
-
594
-
595
- // replace :PORT_PLACEHOLDER with available ports
596
-
597
- const jsonString = JSON.stringify(payload)
598
- // find all PORT_PLACEHOLDER patterns
599
- const matches = jsonString.match(/:PORT_PLACEHOLDER_[0-9.:]+/g)
600
- console.log("matches", matches)
601
-
602
- // iterate through them and see if the port already exists
603
- let count = 0
604
- if (matches) {
605
- for(let match of matches) {
606
- const port_match = match.match(/[0-9.]+:([0-9]+)/);
607
- let port = port_match ? port_match[1] : null
608
- if (port && !this.port_mapping[port]) {
609
- count++
610
- }
611
- }
612
- }
613
-
614
-
615
- if (count > 0) {
616
- let ports = await this.kernel.ports(count)
617
- console.log("PORT_CACHE", this.port_cache)
618
- let response = Util.fill_object(payload, /PORT_PLACEHOLDER_.+/, ports, this.port_cache)
619
- payload = response.result
620
- let replaced_map = response.replaced_map
621
- for(let key in replaced_map) {
622
- // local_share_ip :- 192.168....
623
- let local_share_key = key.replace("PORT_PLACEHOLDER_", "")
624
- let chunks = local_share_key.split(":")
625
- let local_share_ip = chunks[0]
626
- let original_port = chunks[1]
627
- let local_share_port = replaced_map[key]
628
- if (!this.local_network_mapping[local_share_ip]) {
629
- this.local_network_mapping[local_share_ip] = {}
630
- }
631
- this.local_network_mapping[local_share_ip][original_port] = local_share_port
632
-
633
-
634
- //let original_port = key.replace("PORT_PLACEHOLDER_", "")
635
- this.port_mapping[original_port] = replaced_map[key]
636
- }
637
- this.port_cache = replaced_map
638
- }
639
-
640
-
641
-
642
-
643
-
644
- return payload
645
- }
646
- mapping (ip) {
647
- let current_lan_ip = this.kernel.peer.current_ip
648
- if (!ip || current_lan_ip === ip) {
649
- let mapping = {}
650
- for(let lan_ip in this._mapping) {
651
- let internal_maps = this._mapping[lan_ip]
652
- if (!mapping[lan_ip]) {
653
- mapping[lan_ip] = {}
654
- }
655
- for(let url in internal_maps) {
656
- mapping[lan_ip][url] = Array.from(internal_maps[url])
657
- }
658
- }
659
- return mapping
660
- } else {
661
- return this.kernel.peer.caddy(ip)
662
- }
663
- }
664
-
665
- }
666
- module.exports = Caddy