pinokiod 3.8.38 → 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
@@ -369,113 +369,122 @@ class Caddy {
369
369
  let account = this.kernel.peer.account(ip)
370
370
  console.log({ account })
371
371
 
372
- // caddy_mapping := { IP => HTTPS_DOMAIN }
373
- let caddy_mapping = this.kernel.peer.caddy(ip)
374
- console.log({ caddy_mapping })
375
- for(let lan_ip in caddy_mapping) {
376
- let https_domains_mapping = caddy_mapping[lan_ip]
377
- for(let internal_ip in https_domains_mapping) {
378
-
379
- // create a proxy proxy
380
- // 1. create an exposed port listening on 0.0.0.0
381
- // 2. the exposed port points to the internal ip
382
- let chunks = internal_ip.split(":")
383
- let INTERNAL_HOST = chunks[0]
384
- let INTERNAL_PORT = chunks[1]
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
385
378
 
386
- this.ip_mapping[INTERNAL_PORT] = ip
387
379
 
388
- payload.apps.http.servers[":" + INTERNAL_PORT] = {
389
- "listen": ["0.0.0.0:PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT],
390
- "routes": [{
391
- "handle": [
392
- {
393
- "handler": "reverse_proxy",
394
- "upstreams": [{ "dial": internal_ip }]
395
- },
396
- {
397
- "handler": "headers",
398
- "response": {
399
- "set": {
400
- "Access-Control-Allow-Origin": ["*"],
401
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
402
- "Access-Control-Allow-Headers": ["*"],
403
- "Vary": ["Origin"]
404
- }
380
+ // caddy_mapping := { IP => HTTPS_DOMAIN }
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
+
390
+ // create a proxy proxy
391
+ // 1. create an exposed port listening on 0.0.0.0
392
+ // 2. the exposed port points to the internal ip
393
+ let chunks = internal_ip.split(":")
394
+ let INTERNAL_HOST = chunks[0]
395
+ let INTERNAL_PORT = chunks[1]
396
+
397
+ this.ip_mapping[INTERNAL_PORT] = ip
398
+
399
+ payload.apps.http.servers[":" + INTERNAL_PORT] = {
400
+ "listen": ["0.0.0.0:PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT],
401
+ "routes": [{
402
+ "handle": [
403
+ {
404
+ "handler": "reverse_proxy",
405
+ "upstreams": [{ "dial": internal_ip }]
406
+ },
407
+ {
408
+ "handler": "headers",
409
+ "response": {
410
+ "set": {
411
+ "Access-Control-Allow-Origin": ["*"],
412
+ "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
413
+ "Access-Control-Allow-Headers": ["*"],
414
+ "Vary": ["Origin"]
405
415
  }
406
416
  }
407
- ]
408
- }]
409
- }
417
+ }
418
+ ]
419
+ }]
420
+ }
410
421
 
411
422
 
412
- //let https_domains = caddy_mapping[internal_ip]
413
- let https_domains = https_domains_mapping[internal_ip]
414
- console.log(">>>>>>>> https_domains", https_domains)
415
-
416
- // 1. if https_domains is made up of one item <port>.localhost,
417
- // add <port>.<account>.localhost
418
- // 2. if https_domains is made up of two items <port>.localhost and <port>.<account>.localhost, don't do anything
419
- for(let https_domain of https_domains) {
420
- // insert the $account value right before the last part (1234.localhost => 1234.x.localhost)
421
- let chunks = https_domain.split(".")
422
- let namespaced_url
423
- if (chunks.length === 3) {
424
- namespaced_url = https_domain
425
- } else if (chunks.length === 2) {
426
- chunks.splice(chunks.length - 1, 0, account)
427
- namespaced_url = chunks.join(".")
428
- }
429
- console.log("##x", { chunks, namespaced_url, https_domain })
423
+ //let https_domains = caddy_mapping[internal_ip]
424
+ let https_domains = https_domains_mapping[internal_ip]
425
+ console.log(">>>>>>>> https_domains", https_domains)
426
+
427
+ // 1. if https_domains is made up of one item <port>.localhost,
428
+ // add <port>.<account>.localhost
429
+ // 2. if https_domains is made up of two items <port>.localhost and <port>.<account>.localhost, don't do anything
430
+ for(let https_domain of https_domains) {
431
+ // insert the $account value right before the last part (1234.localhost => 1234.x.localhost)
432
+ let chunks = https_domain.split(".")
433
+ let namespaced_url
434
+ if (chunks.length === 3) {
435
+ namespaced_url = https_domain
436
+ } else if (chunks.length === 2) {
437
+ chunks.splice(chunks.length - 1, 0, account)
438
+ namespaced_url = chunks.join(".")
439
+ }
440
+ console.log("##x", { chunks, namespaced_url, https_domain })
430
441
 
431
442
 
432
- //if (!this._mapping[internal_ip]) {
433
- // this._mapping[internal_ip] = new Set()
434
- //}
435
- //console.log("this.mapping", this._mapping)
436
- //console.log("internal_ip", internal_ip)
437
- //console.log("result", this._mapping[internal_ip])
438
- //this._mapping[internal_ip].add(namespaced_url)
439
- //console.log({ namespaced_url, https_domain, internal_ip })
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 })
440
451
 
441
452
 
442
- let current_lan_ip = ip
443
- let current_lan_username = this.kernel.peer.account(current_lan_ip)
444
- if (!this._mapping[current_lan_ip]) {
445
- this._mapping[current_lan_ip] = {}
446
- }
447
- if (!this._mapping[current_lan_ip][internal_ip]) {
448
- this._mapping[current_lan_ip][internal_ip] = new Set()
449
- }
450
- this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
451
- console.log({ internal_ip, current_lan_ip })
452
-
453
-
454
- // 1. point proxy URL to the 127.0.0.1:EXPOSED_PORT
455
- payload.apps.http.servers.main.routes.push({
456
- "match": [{
457
- "host": [namespaced_url],
458
- }],
459
- "handle": [
460
- {
461
- "handler": "reverse_proxy",
462
- "upstreams": [{ "dial": ip + ":PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT }]
463
- },
464
- {
465
- "handler": "headers",
466
- "response": {
467
- "set": {
468
- "Access-Control-Allow-Origin": ["*"],
469
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
470
- "Access-Control-Allow-Headers": ["*"],
471
- "Vary": ["Origin"]
472
- }
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()
460
+ }
461
+ this._mapping[current_lan_ip][internal_ip].add(namespaced_url)
462
+ console.log("Adding", current_lan_ip, internal_ip, namespaced_url)
463
+
464
+
465
+ // 1. point proxy URL to the 127.0.0.1:EXPOSED_PORT
466
+ payload.apps.http.servers.main.routes.push({
467
+ "match": [{
468
+ "host": [namespaced_url],
469
+ }],
470
+ "handle": [
471
+ {
472
+ "handler": "reverse_proxy",
473
+ "upstreams": [{ "dial": ip + ":PORT_PLACEHOLDER_" + ip + ":" + INTERNAL_PORT }]
474
+ },
475
+ {
476
+ "handler": "headers",
477
+ "response": {
478
+ "set": {
479
+ "Access-Control-Allow-Origin": ["*"],
480
+ "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
481
+ "Access-Control-Allow-Headers": ["*"],
482
+ "Vary": ["Origin"]
473
483
  }
474
484
  }
475
- ]
476
- })
477
- }
478
-
485
+ }
486
+ ]
487
+ })
479
488
  }
480
489
  }
481
490
  }
@@ -619,19 +628,30 @@ class Caddy {
619
628
  this.port_cache = replaced_map
620
629
  }
621
630
 
622
- this.mapping = {}
623
- for(let lan_ip in this._mapping) {
624
- let internal_maps = this._mapping[lan_ip]
625
- if (!this.mapping[lan_ip]) {
626
- this.mapping[lan_ip] = {}
627
- }
628
- for(let url in internal_maps) {
629
- this.mapping[lan_ip][url] = Array.from(internal_maps[url])
630
- }
631
- }
631
+
632
+
632
633
 
633
634
 
634
635
  return payload
635
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
+
636
656
  }
637
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "3.8.38",
3
+ "version": "3.8.39",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/server/index.js CHANGED
@@ -2662,10 +2662,11 @@ class Server {
2662
2662
  await this.kernel.caddy.update()
2663
2663
  let list = await this.kernel.processes.refresh()
2664
2664
  let current_lan_ip = this.kernel.peer.current_ip
2665
+ console.log("this.kernel.caddy.mapping", this.kernel.caddy.mapping())
2665
2666
  list = list.map((item) => {
2666
2667
  return {
2667
2668
  //proxy: this.kernel.caddy.mapping[item.ip] || [],
2668
- proxy: this.kernel.caddy.mapping[current_lan_ip][item.ip] || [],
2669
+ proxy: this.kernel.caddy.mapping()[current_lan_ip][item.ip] || [],
2669
2670
  ...item
2670
2671
  }
2671
2672
  })
@@ -3293,7 +3294,7 @@ class Server {
3293
3294
  res.json({
3294
3295
  homedir: this.kernel.homedir,
3295
3296
  account: this.kernel.peer.current_account,
3296
- proxy: this.kernel.caddy.mapping, // mapping: { IP => https_domain }
3297
+ proxy: this.kernel.caddy.mapping(), // mapping: { IP => https_domain }
3297
3298
  ports: this.kernel.caddy.port_mapping,
3298
3299
  lan: this.kernel.caddy.local_network_mapping,
3299
3300
  ips: this.kernel.caddy.ip_mapping,