pinokiod 3.8.37 → 3.8.39

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/caddy.js CHANGED
@@ -37,10 +37,20 @@ class Caddy {
37
37
  let internal_ip = "127.0.0.1:42000"
38
38
  let namespaced_url = "pinokio.localhost"
39
39
 
40
- if (!this._mapping[internal_ip]) {
41
- this._mapping[internal_ip] = new Set()
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()
42
52
  }
43
- this._mapping[internal_ip].add(namespaced_url)
53
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
44
54
 
45
55
  return {
46
56
  "apps": {
@@ -158,13 +168,25 @@ class Caddy {
158
168
  }]
159
169
  }
160
170
 
171
+ let current_lan_ip = ip
172
+ let current_lan_username = this.kernel.peer.account(ip)
173
+
161
174
  // proxy proxy => proxy
162
175
  if (key === "url") {
163
176
  let namespaced_url = `${name}.${account}.localhost`.toLowerCase()
164
- if (!this._mapping[internal_ip]) {
165
- this._mapping[internal_ip] = new Set()
177
+ //if (!this._mapping[internal_ip]) {
178
+ // this._mapping[internal_ip] = new Set()
179
+ //}
180
+ //this._mapping[internal_ip].add(namespaced_url)
181
+
182
+ if (!this._mapping[current_lan_ip]) {
183
+ this._mapping[current_lan_ip] = {}
166
184
  }
167
- this._mapping[internal_ip].add(namespaced_url)
185
+ if (!this._mapping[current_lan_ip][internal_ip]) {
186
+ this._mapping[current_lan_ip][internal_ip] = new Set()
187
+ }
188
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
189
+
168
190
  payload.apps.http.servers.main.routes.push({
169
191
  "match": [{
170
192
  "host": [namespaced_url]
@@ -189,10 +211,19 @@ class Caddy {
189
211
  })
190
212
  }
191
213
  let namespaced_url = `${name}.${account}.localhost`.toLowerCase()
192
- if (!this._mapping[internal_ip]) {
193
- this._mapping[internal_ip] = new Set()
214
+ //if (!this._mapping[internal_ip]) {
215
+ // this._mapping[internal_ip] = new Set()
216
+ //}
217
+ //this._mapping[internal_ip].add(namespaced_url)
218
+
219
+ if (!this._mapping[current_lan_ip]) {
220
+ this._mapping[current_lan_ip] = {}
194
221
  }
195
- this._mapping[internal_ip].add(namespaced_url)
222
+ if (!this._mapping[current_lan_ip][internal_ip]) {
223
+ this._mapping[current_lan_ip][internal_ip] = new Set()
224
+ }
225
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
226
+
196
227
  payload.apps.http.servers.main.routes.push({
197
228
  "match": [{
198
229
  "host": [namespaced_url],
@@ -246,14 +277,27 @@ class Caddy {
246
277
  let chunks = internal_ip.split(":")
247
278
  if (chunks.length === 2) {
248
279
  if (key === "url") {
249
- let url = `${name}.localhost`.toLowerCase()
250
- if (!this._mapping[val]) {
251
- this._mapping[val] = new Set()
280
+ //if (!this._mapping[val]) {
281
+ // this._mapping[val] = new Set()
282
+ //}
283
+ //this._mapping[val].add(namespaced_url)
284
+
285
+
286
+
287
+ let namespaced_url = `${name}.localhost`.toLowerCase()
288
+ let current_lan_ip = this.kernel.peer.current_ip
289
+ let current_lan_username = this.kernel.peer.account(current_lan_ip)
290
+ if (!this._mapping[current_lan_ip]) {
291
+ this._mapping[current_lan_ip] = {}
292
+ }
293
+ if (!this._mapping[current_lan_ip][internal_ip]) {
294
+ this._mapping[current_lan_ip][internal_ip] = new Set()
252
295
  }
253
- this._mapping[val].add(url)
296
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
297
+
254
298
  payload.apps.http.servers.main.routes.push({
255
299
  "match": [{
256
- "host": [url]
300
+ "host": [namespaced_url]
257
301
  }],
258
302
  "handle": [
259
303
  {
@@ -274,9 +318,21 @@ class Caddy {
274
318
  ]
275
319
  })
276
320
  }
321
+ let namespaced_url = `${key}.${name}.localhost`.toLowerCase()
322
+
323
+ let current_lan_ip = this.kernel.peer.current_ip
324
+ let current_lan_username = this.kernel.peer.account(current_lan_ip)
325
+ if (!this._mapping[current_lan_ip]) {
326
+ this._mapping[current_lan_ip] = {}
327
+ }
328
+ if (!this._mapping[current_lan_ip][internal_ip]) {
329
+ this._mapping[current_lan_ip][internal_ip] = new Set()
330
+ }
331
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
332
+
277
333
  payload.apps.http.servers.main.routes.push({
278
334
  "match": [{
279
- "host": [`${key}.${name}.localhost`.toLowerCase()],
335
+ "host": [namespaced_url],
280
336
  }],
281
337
  "handle": [
282
338
  {
@@ -313,10 +369,24 @@ class Caddy {
313
369
  let account = this.kernel.peer.account(ip)
314
370
  console.log({ account })
315
371
 
372
+ // if ip === current_lan_ip
373
+ // => trust the info on itself only
374
+ // => only use the current info, ignore the rest
375
+ // ip ip !== current_lan_ip => just pick one
376
+ // => trust the info coming from ip only
377
+ // => only use the info for that node
378
+
379
+
316
380
  // caddy_mapping := { IP => HTTPS_DOMAIN }
317
- let caddy_mapping = this.kernel.peer.caddy(ip)
318
- console.log({ caddy_mapping })
319
- for(let internal_ip in caddy_mapping) {
381
+
382
+ // get the mapping for the current node
383
+ // for current node, only trust the info from the current node
384
+ // for other nodes, just use the first one to avoid registering duplicates
385
+
386
+ let https_domains_mapping = this.mapping(ip)[ip]
387
+ console.log({ https_domains_mapping, ip })
388
+ for(let internal_ip in https_domains_mapping) {
389
+
320
390
  // create a proxy proxy
321
391
  // 1. create an exposed port listening on 0.0.0.0
322
392
  // 2. the exposed port points to the internal ip
@@ -348,7 +418,10 @@ class Caddy {
348
418
  ]
349
419
  }]
350
420
  }
351
- let https_domains = caddy_mapping[internal_ip]
421
+
422
+
423
+ //let https_domains = caddy_mapping[internal_ip]
424
+ let https_domains = https_domains_mapping[internal_ip]
352
425
  console.log(">>>>>>>> https_domains", https_domains)
353
426
 
354
427
  // 1. if https_domains is made up of one item <port>.localhost,
@@ -365,14 +438,30 @@ class Caddy {
365
438
  namespaced_url = chunks.join(".")
366
439
  }
367
440
  console.log("##x", { chunks, namespaced_url, https_domain })
368
- if (!this._mapping[internal_ip]) {
369
- this._mapping[internal_ip] = new Set()
441
+
442
+
443
+ //if (!this._mapping[internal_ip]) {
444
+ // this._mapping[internal_ip] = new Set()
445
+ //}
446
+ //console.log("this.mapping", this._mapping)
447
+ //console.log("internal_ip", internal_ip)
448
+ //console.log("result", this._mapping[internal_ip])
449
+ //this._mapping[internal_ip].add(namespaced_url)
450
+ //console.log({ namespaced_url, https_domain, internal_ip })
451
+
452
+
453
+ let current_lan_ip = ip
454
+ let current_lan_username = this.kernel.peer.account(current_lan_ip)
455
+ if (!this._mapping[current_lan_ip]) {
456
+ this._mapping[current_lan_ip] = {}
457
+ }
458
+ if (!this._mapping[current_lan_ip][internal_ip]) {
459
+ this._mapping[current_lan_ip][internal_ip] = new Set()
370
460
  }
371
- console.log("this.mapping", this._mapping)
372
- console.log("internal_ip", internal_ip)
373
- console.log("result", this._mapping[internal_ip])
374
- this._mapping[internal_ip].add(namespaced_url)
375
- console.log({ namespaced_url, https_domain, internal_ip })
461
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
462
+ console.log("Adding", current_lan_ip, internal_ip, namespaced_url)
463
+
464
+
376
465
  // 1. point proxy URL to the 127.0.0.1:EXPOSED_PORT
377
466
  payload.apps.http.servers.main.routes.push({
378
467
  "match": [{
@@ -407,15 +496,28 @@ class Caddy {
407
496
  if (!list) {
408
497
  list = await this.kernel.processes.refresh()
409
498
  }
499
+ console.log(">>>>>>>>>> LIST", list)
410
500
  for(let item of list) {
411
- let url = `${item.port}.localhost`
412
- if (!this._mapping[item.ip]) {
413
- this._mapping[item.ip] = new Set()
501
+ let namespaced_url = `${item.port}.localhost`
502
+ //if (!this._mapping[item.ip]) {
503
+ // this._mapping[item.ip] = new Set()
504
+ //}
505
+ //this._mapping[item.ip].add(namespaced_url)
506
+
507
+ let internal_ip = item.ip
508
+ let current_lan_ip = this.kernel.peer.current_ip
509
+ let current_lan_username = this.kernel.peer.account(current_lan_ip)
510
+ if (!this._mapping[current_lan_ip]) {
511
+ this._mapping[current_lan_ip] = {}
512
+ }
513
+ if (!this._mapping[current_lan_ip][internal_ip]) {
514
+ this._mapping[current_lan_ip][internal_ip] = new Set()
414
515
  }
415
- this._mapping[item.ip].add(url)
516
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
517
+
416
518
  payload.apps.http.servers.main.routes.push({
417
519
  "match": [{
418
- "host": [url],
520
+ "host": [namespaced_url],
419
521
  }],
420
522
  "handle": [
421
523
  {
@@ -478,13 +580,32 @@ class Caddy {
478
580
  // replace :PORT_PLACEHOLDER with available ports
479
581
 
480
582
  const jsonString = JSON.stringify(payload)
481
- const matches = jsonString.match(/:PORT_PLACEHOLDER/g);
482
- const count = matches ? matches.length : 0;
583
+ // find all PORT_PLACEHOLDER patterns
584
+ const matches = jsonString.match(/:PORT_PLACEHOLDER_[0-9.:]+/g)
585
+ console.log("matches", matches)
586
+
587
+ // iterate through them and see if the port already exists
588
+ let count = 0
589
+ if (matches) {
590
+ for(let match of matches) {
591
+ console.log("testing", match)
592
+ const port_match = match.match(/[0-9.]+:([0-9]+)/);
593
+ let port = port_match ? port_match[1] : null
594
+ console.log({ port, port_match })
595
+ if (port && !this.port_mapping[port]) {
596
+ count++
597
+ }
598
+ }
599
+ }
600
+
601
+ console.log("count", count)
602
+ console.log("&&", JSON.stringify(payload, null, 2))
483
603
 
484
604
  if (count > 0) {
485
605
  let ports = await this.kernel.ports(count)
486
606
  console.log({ count, ports })
487
- let response = Util.fill_object(payload, /PORT_PLACEHOLDER_.+/, ports)
607
+ console.log("PORT_CACHE", this.port_cache)
608
+ let response = Util.fill_object(payload, /PORT_PLACEHOLDER_.+/, ports, this.port_cache)
488
609
  payload = response.result
489
610
  let replaced_map = response.replaced_map
490
611
  for(let key in replaced_map) {
@@ -504,15 +625,33 @@ class Caddy {
504
625
  //let original_port = key.replace("PORT_PLACEHOLDER_", "")
505
626
  this.port_mapping[original_port] = replaced_map[key]
506
627
  }
628
+ this.port_cache = replaced_map
507
629
  }
508
630
 
509
- this.mapping = {}
510
- for(let key in this._mapping) {
511
- this.mapping[key] = Array.from(this._mapping[key])
512
- }
631
+
632
+
513
633
 
514
634
 
515
635
  return payload
516
636
  }
637
+ mapping (ip) {
638
+ let current_lan_ip = this.kernel.peer.current_ip
639
+ if (!ip || current_lan_ip === ip) {
640
+ let mapping = {}
641
+ for(let lan_ip in this._mapping) {
642
+ let internal_maps = this._mapping[lan_ip]
643
+ if (!mapping[lan_ip]) {
644
+ mapping[lan_ip] = {}
645
+ }
646
+ for(let url in internal_maps) {
647
+ mapping[lan_ip][url] = Array.from(internal_maps[url])
648
+ }
649
+ }
650
+ return mapping
651
+ } else {
652
+ return this.kernel.peer.caddy(ip)
653
+ }
654
+ }
655
+
517
656
  }
518
657
  module.exports = Caddy
package/kernel/peer.js CHANGED
@@ -39,6 +39,7 @@ class PeerDiscovery {
39
39
  let interval = setInterval(() => {
40
40
  axios.get(`http://${ip}:42000/pinokio/mem`).then((res) => {
41
41
  this.data[ip] = res.data
42
+ console.log("this.data", { ip, data: this.data[ip] })
42
43
  if (res.data.proxy) {
43
44
  // clearInterval(interval)
44
45
  }
package/kernel/util.js CHANGED
@@ -307,19 +307,24 @@ const update_env = async (filepath, changes) => {
307
307
  }, normalizeEOL(env));
308
308
  await fs.promises.writeFile(filepath, newval)
309
309
  };
310
- function fill_object(obj, pattern, list) {
310
+ function fill_object(obj, pattern, list, cache) {
311
311
  console.log("FILL_OBJECT")
312
312
  const map = {};
313
- const replaced_map = {}
313
+ const replaced_map = cache || {}
314
314
  let index = 0;
315
315
 
316
316
  function recurse(value) {
317
317
  if (typeof value === 'string') {
318
318
  return value.replace(pattern, (match) => {
319
319
  if (!(match in map)) {
320
- if (index >= list.length) throw new Error("Not enough items provided");
321
- map[match] = list[index++];
322
- replaced_map[match] = map[match]
320
+ console.log( "fill_object", { replaced_map, match })
321
+ if (replaced_map[match]) {
322
+ map[match] = replaced_map[match]
323
+ } else {
324
+ if (index >= list.length) throw new Error("Not enough items provided");
325
+ map[match] = list[index++];
326
+ replaced_map[match] = map[match]
327
+ }
323
328
  }
324
329
  return map[match];
325
330
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "3.8.37",
3
+ "version": "3.8.39",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/server/index.js CHANGED
@@ -2661,9 +2661,12 @@ class Server {
2661
2661
  this.app.get("/proxy", ex(async (req, res) => {
2662
2662
  await this.kernel.caddy.update()
2663
2663
  let list = await this.kernel.processes.refresh()
2664
+ let current_lan_ip = this.kernel.peer.current_ip
2665
+ console.log("this.kernel.caddy.mapping", this.kernel.caddy.mapping())
2664
2666
  list = list.map((item) => {
2665
2667
  return {
2666
- proxy: this.kernel.caddy.mapping[item.ip] || [],
2668
+ //proxy: this.kernel.caddy.mapping[item.ip] || [],
2669
+ proxy: this.kernel.caddy.mapping()[current_lan_ip][item.ip] || [],
2667
2670
  ...item
2668
2671
  }
2669
2672
  })
@@ -2751,7 +2754,7 @@ class Server {
2751
2754
  }
2752
2755
  }
2753
2756
 
2754
- console.log({ port_mapping: this.kernel.caddy.port_mapping, ip_mapping: this.kernel.caddy.ip_mapping })
2757
+ console.log({ port_mapping: this.kernel.caddy.port_mapping, ip_mapping: this.kernel.caddy.ip_mapping, lan: this.kernel.caddy.local_network_mapping, ip: this.kernel.peer.current_ip })
2755
2758
  res.render("proxy", {
2756
2759
  ip: this.kernel.peer.current_ip,
2757
2760
  peers,
@@ -3291,7 +3294,7 @@ class Server {
3291
3294
  res.json({
3292
3295
  homedir: this.kernel.homedir,
3293
3296
  account: this.kernel.peer.current_account,
3294
- proxy: this.kernel.caddy.mapping, // mapping: { IP => https_domain }
3297
+ proxy: this.kernel.caddy.mapping(), // mapping: { IP => https_domain }
3295
3298
  ports: this.kernel.caddy.port_mapping,
3296
3299
  lan: this.kernel.caddy.local_network_mapping,
3297
3300
  ips: this.kernel.caddy.ip_mapping,
@@ -274,9 +274,11 @@ td:first-child, th:first-child {
274
274
  <div>
275
275
  <a target="_blank" href="http://localhost:<%=item.port%>">http://localhost:<%=item.port%></a>
276
276
  </div>
277
- <div>
278
- <a target="_blank" href="http://<%=ip%>:<%=lan[ip][item.port]%>">http://<%=ip%>:<%=lan[ip][item.port]%></a>
279
- </div>
277
+ <% if (lan[ip]) { %>
278
+ <div>
279
+ <a target="_blank" href="http://<%=ip%>:<%=lan[ip][item.port]%>">http://<%=ip%>:<%=lan[ip][item.port]%></a>
280
+ </div>
281
+ <% } %>
280
282
  </td>
281
283
  </tr>
282
284
  <% }) %>