pinokiod 3.8.44 → 3.8.46
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/api/index.js +2 -2
- package/kernel/bin/caddy.js +1 -1
- package/kernel/bin/index.js +0 -18
- package/kernel/{caddy.js → caddy2.js} +0 -6
- package/kernel/index.js +15 -29
- package/kernel/loader.js +1 -1
- package/kernel/peer.js +21 -31
- package/kernel/peer2.js +83 -0
- package/kernel/prototype.js +0 -1
- package/kernel/router/common.js +29 -0
- package/kernel/router/connector.js +56 -0
- package/kernel/router/index.js +137 -0
- package/kernel/router/localhost_home_router.js +61 -0
- package/kernel/router/localhost_port_router.js +15 -0
- package/kernel/router/localhost_variable_router.js +39 -0
- package/kernel/router/peer_home_router.js +15 -0
- package/kernel/router/peer_port_router.js +43 -0
- package/kernel/router/peer_variable_router.js +48 -0
- package/kernel/router/processor.js +18 -0
- package/kernel/router/root.js +32 -0
- package/kernel/sysinfo.js +0 -1
- package/kernel/util.js +1 -3
- package/package.json +1 -1
- package/pipe/index.js +0 -2
- package/server/index.js +34 -70
- package/server/views/install.ejs +0 -1
- package/server/views/proxy.ejs +4 -4
package/kernel/api/index.js
CHANGED
|
@@ -186,7 +186,7 @@ class Api {
|
|
|
186
186
|
type: "disconnect"
|
|
187
187
|
})
|
|
188
188
|
console.timeEnd("stop 4")
|
|
189
|
-
await this.kernel.
|
|
189
|
+
await this.kernel.router.refresh()
|
|
190
190
|
return true
|
|
191
191
|
}
|
|
192
192
|
async startScripts() {
|
|
@@ -1074,7 +1074,7 @@ class Api {
|
|
|
1074
1074
|
})
|
|
1075
1075
|
}
|
|
1076
1076
|
}
|
|
1077
|
-
await this.kernel.
|
|
1077
|
+
await this.kernel.router.refresh()
|
|
1078
1078
|
} catch (e) {
|
|
1079
1079
|
ondata({ raw: e.toString() })
|
|
1080
1080
|
}
|
package/kernel/bin/caddy.js
CHANGED
package/kernel/bin/index.js
CHANGED
|
@@ -38,17 +38,14 @@ class Bin {
|
|
|
38
38
|
this.platform = os.platform()
|
|
39
39
|
}
|
|
40
40
|
async shell_kill(params, ondata) {
|
|
41
|
-
console.log("shell_kill", params)
|
|
42
41
|
this.kernel.shell.kill(params.params)
|
|
43
42
|
}
|
|
44
43
|
async shell_start(params, ondata) {
|
|
45
44
|
params.path = params.path || this.path()
|
|
46
|
-
console.log("this.client?", this.client)
|
|
47
45
|
if (this.client) {
|
|
48
46
|
params.cols = this.client.cols
|
|
49
47
|
params.rows = this.client.rows
|
|
50
48
|
}
|
|
51
|
-
console.log({ params })
|
|
52
49
|
let response = await this.kernel.shell.start(params, null, ondata)
|
|
53
50
|
return response
|
|
54
51
|
}
|
|
@@ -58,7 +55,6 @@ class Bin {
|
|
|
58
55
|
params.cols = this.client.cols
|
|
59
56
|
params.rows = this.client.rows
|
|
60
57
|
}
|
|
61
|
-
console.log({ params })
|
|
62
58
|
let response = await this.kernel.shell.run(params, null, ondata)
|
|
63
59
|
return response
|
|
64
60
|
}
|
|
@@ -71,10 +67,8 @@ class Bin {
|
|
|
71
67
|
// })
|
|
72
68
|
// }
|
|
73
69
|
async download(url, dest, ondata) {
|
|
74
|
-
console.log("DOWNLOAD process.env", process.env)
|
|
75
70
|
const agent = new ProxyAgent();
|
|
76
71
|
const userAgent = fakeUa()
|
|
77
|
-
console.log("download userAgent", userAgent)
|
|
78
72
|
const opts = {
|
|
79
73
|
fileName: dest,
|
|
80
74
|
override: true,
|
|
@@ -90,7 +84,6 @@ class Bin {
|
|
|
90
84
|
opts.httpRequestOptions = { agent }
|
|
91
85
|
opts.httpsRequestOptions = { agent }
|
|
92
86
|
}
|
|
93
|
-
console.log("opts", opts)
|
|
94
87
|
const dl = new DownloaderHelper(url, this.path(), opts)
|
|
95
88
|
ondata({ raw: `\r\nDownloading ${url} to ${this.path()}...\r\n` })
|
|
96
89
|
let res = await new Promise((resolve, reject) => {
|
|
@@ -203,7 +196,6 @@ class Bin {
|
|
|
203
196
|
return e
|
|
204
197
|
}
|
|
205
198
|
async init() {
|
|
206
|
-
console.log("kernel.bin.init", this.kernel.homedir)
|
|
207
199
|
if (this.kernel.homedir) {
|
|
208
200
|
const bin_folder = this.path()
|
|
209
201
|
await fs.promises.mkdir(bin_folder, { recursive: true }).catch((e) => { })
|
|
@@ -213,7 +205,6 @@ class Bin {
|
|
|
213
205
|
}
|
|
214
206
|
// await fs.promises.mkdir(playwright_folder, { recursive: true }).catch((e) => { })
|
|
215
207
|
let system_env = await Environment.get(this.kernel.homedir)
|
|
216
|
-
console.log("***********", { system_env })
|
|
217
208
|
|
|
218
209
|
if (system_env.HTTP_PROXY) {
|
|
219
210
|
process.env.HTTP_PROXY = system_env.HTTP_PROXY
|
|
@@ -339,13 +330,11 @@ class Bin {
|
|
|
339
330
|
let to_reset_exists = false
|
|
340
331
|
for(let module_path of module_paths) {
|
|
341
332
|
let e = await this.kernel.exists(module_path)
|
|
342
|
-
console.log({ e, module_path })
|
|
343
333
|
if (e) {
|
|
344
334
|
to_reset_exists = true
|
|
345
335
|
break;
|
|
346
336
|
}
|
|
347
337
|
}
|
|
348
|
-
console.log({ to_reset_exists })
|
|
349
338
|
if (to_reset_exists) {
|
|
350
339
|
this.correct_conda = false
|
|
351
340
|
} else {
|
|
@@ -406,7 +395,6 @@ class Bin {
|
|
|
406
395
|
}
|
|
407
396
|
}
|
|
408
397
|
|
|
409
|
-
console.log({ conda_check })
|
|
410
398
|
if (conda_check.conda && conda_check.mamba && conda_check.sqlite) {
|
|
411
399
|
//if (conda_check.conda && conda_check.mamba) {
|
|
412
400
|
this.correct_conda = true
|
|
@@ -417,23 +405,17 @@ class Bin {
|
|
|
417
405
|
}
|
|
418
406
|
async refreshInstalled() {
|
|
419
407
|
|
|
420
|
-
console.log("refreshInstalled start")
|
|
421
|
-
|
|
422
|
-
|
|
423
408
|
/// A. installed packages detection
|
|
424
409
|
|
|
425
410
|
this.installed_initialized = false
|
|
426
411
|
|
|
427
412
|
//this.installed = {}
|
|
428
413
|
|
|
429
|
-
console.log("## check conda")
|
|
430
|
-
|
|
431
414
|
// 1. conda
|
|
432
415
|
|
|
433
416
|
// check conda location and see if it exists. only run if it exists
|
|
434
417
|
let conda_path = path.resolve(this.kernel.homedir, "bin", "miniconda")
|
|
435
418
|
let conda_exists = await this.exists(conda_path)
|
|
436
|
-
console.log({ conda_path, conda_exists })
|
|
437
419
|
|
|
438
420
|
let start
|
|
439
421
|
let res
|
|
@@ -601,22 +601,17 @@ class Caddy {
|
|
|
601
601
|
let count = 0
|
|
602
602
|
if (matches) {
|
|
603
603
|
for(let match of matches) {
|
|
604
|
-
console.log("testing", match)
|
|
605
604
|
const port_match = match.match(/[0-9.]+:([0-9]+)/);
|
|
606
605
|
let port = port_match ? port_match[1] : null
|
|
607
|
-
console.log({ port, port_match })
|
|
608
606
|
if (port && !this.port_mapping[port]) {
|
|
609
607
|
count++
|
|
610
608
|
}
|
|
611
609
|
}
|
|
612
610
|
}
|
|
613
611
|
|
|
614
|
-
console.log("count", count)
|
|
615
|
-
console.log("&&", JSON.stringify(payload, null, 2))
|
|
616
612
|
|
|
617
613
|
if (count > 0) {
|
|
618
614
|
let ports = await this.kernel.ports(count)
|
|
619
|
-
console.log({ count, ports })
|
|
620
615
|
console.log("PORT_CACHE", this.port_cache)
|
|
621
616
|
let response = Util.fill_object(payload, /PORT_PLACEHOLDER_.+/, ports, this.port_cache)
|
|
622
617
|
payload = response.result
|
|
@@ -628,7 +623,6 @@ class Caddy {
|
|
|
628
623
|
let local_share_ip = chunks[0]
|
|
629
624
|
let original_port = chunks[1]
|
|
630
625
|
let local_share_port = replaced_map[key]
|
|
631
|
-
console.log({ local_share_ip, local_share_port })
|
|
632
626
|
if (!this.local_network_mapping[local_share_ip]) {
|
|
633
627
|
this.local_network_mapping[local_share_ip] = {}
|
|
634
628
|
}
|
package/kernel/index.js
CHANGED
|
@@ -30,7 +30,7 @@ const Pipe = require("../pipe")
|
|
|
30
30
|
const Cloudflare = require('./api/cloudflare')
|
|
31
31
|
const Store = require('./store')
|
|
32
32
|
const Proto = require('./prototype')
|
|
33
|
-
const
|
|
33
|
+
const Router = require("./router")
|
|
34
34
|
const Procs = require('./procs')
|
|
35
35
|
const Peer = require('./peer')
|
|
36
36
|
const VARS = {
|
|
@@ -71,7 +71,6 @@ class Kernel {
|
|
|
71
71
|
this.store.clone(store.store)
|
|
72
72
|
// load from the store (this will be the last time this is used, since the next time it loads, it will load from the new store)
|
|
73
73
|
}
|
|
74
|
-
console.log("this.store", this.store.store())
|
|
75
74
|
|
|
76
75
|
this.arch = os.arch()
|
|
77
76
|
this.os = os
|
|
@@ -88,6 +87,7 @@ class Kernel {
|
|
|
88
87
|
this.cloudflare = new Cloudflare()
|
|
89
88
|
this.shellpath = shellPath.sync()
|
|
90
89
|
|
|
90
|
+
|
|
91
91
|
this.peer = new Peer()
|
|
92
92
|
}
|
|
93
93
|
/*
|
|
@@ -198,6 +198,16 @@ class Kernel {
|
|
|
198
198
|
let id = this.api.filePath(uri, cwd)
|
|
199
199
|
return this.api.running[id]
|
|
200
200
|
}
|
|
201
|
+
start_port () {
|
|
202
|
+
if (this.router.port_mapping && this.router.port_mapping.length > 0) {
|
|
203
|
+
let max_caddy_port = Math.max(Object.values(this.router.port_mapping))
|
|
204
|
+
let start_port = Math.max(42003, max_caddy_port + 1)
|
|
205
|
+
console.log({ start_port })
|
|
206
|
+
return start_port
|
|
207
|
+
} else {
|
|
208
|
+
return 42003
|
|
209
|
+
}
|
|
210
|
+
}
|
|
201
211
|
port(port) {
|
|
202
212
|
// 1. if port is passed, check port
|
|
203
213
|
// 2. if no port is passed, get the next available port
|
|
@@ -209,13 +219,13 @@ class Kernel {
|
|
|
209
219
|
if (port) {
|
|
210
220
|
return portfinder.isAvailablePromise({ host: "0.0.0.0", port })
|
|
211
221
|
} else {
|
|
212
|
-
return portfinder.getPortPromise({ port:
|
|
222
|
+
return portfinder.getPortPromise({ port: this.start_port() })
|
|
213
223
|
}
|
|
214
224
|
}
|
|
215
225
|
ports(count) {
|
|
216
226
|
// get "count" number of available ports
|
|
217
227
|
return new Promise((resolve, reject) => {
|
|
218
|
-
portfinder.getPorts(count, { port:
|
|
228
|
+
portfinder.getPorts(count, { port: this.start_port() }, (err, ports) => {
|
|
219
229
|
if (err) {
|
|
220
230
|
reject(err)
|
|
221
231
|
} else {
|
|
@@ -299,12 +309,9 @@ class Kernel {
|
|
|
299
309
|
}
|
|
300
310
|
async getInfo(refresh){
|
|
301
311
|
let info = this.sysinfo
|
|
302
|
-
console.time(">>>>> updateSysinfo")
|
|
303
|
-
console.log("refresh", refresh)
|
|
304
312
|
if (refresh) {
|
|
305
313
|
await this.update_sysinfo()
|
|
306
314
|
}
|
|
307
|
-
console.timeEnd(">>>>> updateSysinfo")
|
|
308
315
|
let i = {
|
|
309
316
|
version: this.version,
|
|
310
317
|
platform: this.platform,
|
|
@@ -506,7 +513,7 @@ class Kernel {
|
|
|
506
513
|
this.api = new Api(this)
|
|
507
514
|
this.python = new Python(this)
|
|
508
515
|
this.shell = new Shells(this)
|
|
509
|
-
this.
|
|
516
|
+
this.router = new Router(this)
|
|
510
517
|
this.system = system
|
|
511
518
|
this.keys = {}
|
|
512
519
|
this.memory = {
|
|
@@ -526,7 +533,6 @@ class Kernel {
|
|
|
526
533
|
|
|
527
534
|
// 0. create homedir
|
|
528
535
|
let home_exists = await this.exists(this.homedir)
|
|
529
|
-
console.log({ home_exists, homedir: this.homedir })
|
|
530
536
|
if (!home_exists) {
|
|
531
537
|
// home didn't exist.
|
|
532
538
|
// 1. mkdir
|
|
@@ -551,7 +557,6 @@ class Kernel {
|
|
|
551
557
|
NO_PROXY: (this.store.get("NO_PROXY") || "")
|
|
552
558
|
}
|
|
553
559
|
let fullpath = path.resolve(this.homedir, "ENVIRONMENT")
|
|
554
|
-
console.log("> update env", { fullpath, updated })
|
|
555
560
|
await Util.update_env(fullpath, updated)
|
|
556
561
|
|
|
557
562
|
// 2. mkdir all the folders if not already created
|
|
@@ -574,10 +579,8 @@ class Kernel {
|
|
|
574
579
|
//await this.bin.init()
|
|
575
580
|
let ts = Date.now()
|
|
576
581
|
this.bin.init().then(() => {
|
|
577
|
-
console.log("bin init finished")
|
|
578
582
|
if (this.homedir) {
|
|
579
583
|
this.shell.init().then(async () => {
|
|
580
|
-
console.log("shell init finished")
|
|
581
584
|
this.bin.check_bin()
|
|
582
585
|
if (this.envs) {
|
|
583
586
|
this.template.update({
|
|
@@ -602,24 +605,18 @@ class Kernel {
|
|
|
602
605
|
}
|
|
603
606
|
}
|
|
604
607
|
})
|
|
605
|
-
console.log("*******************")
|
|
606
608
|
|
|
607
609
|
// get env
|
|
608
610
|
if (!this.launch_complete) {
|
|
609
611
|
let interval = setInterval(async () => {
|
|
610
|
-
console.log("this.i", this.i)
|
|
611
612
|
if (this.i) {
|
|
612
613
|
for(let api of this.i.api) {
|
|
613
|
-
console.log({ api })
|
|
614
614
|
let env_path = path.resolve(this.api.userdir, api.path)
|
|
615
615
|
let e = await Environment.get(env_path)
|
|
616
|
-
console.log(e)
|
|
617
616
|
if (e.PINOKIO_SCRIPT_AUTOLAUNCH && e.PINOKIO_SCRIPT_AUTOLAUNCH.trim().length > 0) {
|
|
618
617
|
let autolaunch_path = path.resolve(env_path, e.PINOKIO_SCRIPT_AUTOLAUNCH)
|
|
619
618
|
let exists = await this.exists(autolaunch_path)
|
|
620
|
-
console.log("ATTEMPTING AUTOLAUNCH", autolaunch_path)
|
|
621
619
|
if (exists) {
|
|
622
|
-
console.log("SCRIPT EXISTS. Starting...", autolaunch_path)
|
|
623
620
|
this.api.process({
|
|
624
621
|
uri: autolaunch_path,
|
|
625
622
|
input: {}
|
|
@@ -634,7 +631,6 @@ class Kernel {
|
|
|
634
631
|
}
|
|
635
632
|
}
|
|
636
633
|
}
|
|
637
|
-
console.log("clear Interval")
|
|
638
634
|
clearInterval(interval)
|
|
639
635
|
setTimeout(() => {
|
|
640
636
|
this.launch_complete = true
|
|
@@ -648,14 +644,11 @@ class Kernel {
|
|
|
648
644
|
}
|
|
649
645
|
})
|
|
650
646
|
let ts2 = Date.now()
|
|
651
|
-
console.time(">>>> 6 api.init")
|
|
652
647
|
await this.api.init()
|
|
653
|
-
console.timeEnd(">>>> 6 api.init")
|
|
654
648
|
|
|
655
649
|
//await this.shell.init()
|
|
656
650
|
|
|
657
651
|
if (this.homedir) {
|
|
658
|
-
console.time(">>>> 7 template.init")
|
|
659
652
|
await this.template.init({
|
|
660
653
|
kernel: this,
|
|
661
654
|
system,
|
|
@@ -665,20 +658,13 @@ class Kernel {
|
|
|
665
658
|
return this.api.get_proxy_url("/proxy", port)
|
|
666
659
|
},
|
|
667
660
|
})
|
|
668
|
-
console.timeEnd(">>>> 7 template.init")
|
|
669
661
|
this.sys = new Sysinfo()
|
|
670
|
-
console.time("sys init")
|
|
671
|
-
console.time(">>>> 8 sys.init")
|
|
672
662
|
await this.sys.init(this)
|
|
673
|
-
console.timeEnd(">>>> 8 sys.init")
|
|
674
|
-
console.timeEnd("sys init")
|
|
675
663
|
let info = this.sys.info
|
|
676
664
|
|
|
677
665
|
this.sysinfo = info
|
|
678
666
|
|
|
679
|
-
console.time(">>>> 9 getInfo")
|
|
680
667
|
await this.getInfo()
|
|
681
|
-
console.timeEnd(">>>> 9 getInfo")
|
|
682
668
|
|
|
683
669
|
await fs.promises.mkdir(this.path("logs"), { recursive: true }).catch((e) => { })
|
|
684
670
|
await fs.promises.writeFile(this.path("logs/system.json"), JSON.stringify(this.i, null, 2))
|
package/kernel/loader.js
CHANGED
|
@@ -48,7 +48,7 @@ class Loader {
|
|
|
48
48
|
async requireJS(filepath) {
|
|
49
49
|
let config
|
|
50
50
|
try { config = require(filepath) } catch (e) {
|
|
51
|
-
console.log("load error", filepath, e)
|
|
51
|
+
console.log("load error", filepath, e.message)
|
|
52
52
|
}
|
|
53
53
|
try {
|
|
54
54
|
// if the required module is a class, return the instantiated object
|
package/kernel/peer.js
CHANGED
|
@@ -8,43 +8,19 @@ class PeerDiscovery {
|
|
|
8
8
|
this.interval = interval;
|
|
9
9
|
this.socket = dgram.createSocket('udp4');
|
|
10
10
|
this.peers = new Set();
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
11
|
+
this.name = os.userInfo().username
|
|
12
|
+
this.host = this._getLocalIPAddress()
|
|
13
|
+
this.default_port = 42000
|
|
13
14
|
// this.start();
|
|
14
15
|
}
|
|
15
|
-
account(ip) {
|
|
16
|
-
return this.data[ip].account.username
|
|
17
|
-
}
|
|
18
|
-
homedir(ip) {
|
|
19
|
-
return this.data[ip].homedir
|
|
20
|
-
}
|
|
21
|
-
caddy(ip) {
|
|
22
|
-
return this.data[ip].proxy
|
|
23
|
-
}
|
|
24
|
-
local(ip) {
|
|
25
|
-
return this.data[ip].memory.local
|
|
26
|
-
}
|
|
27
|
-
global(ip) {
|
|
28
|
-
return this.data[ip].memory.global
|
|
29
|
-
}
|
|
30
16
|
start() {
|
|
31
17
|
// Listen for incoming pings
|
|
32
|
-
this.data = {}
|
|
33
18
|
this.socket.on('message', (msg, rinfo) => {
|
|
34
19
|
const ip = rinfo.address;
|
|
35
20
|
if (msg.toString() === this.message.toString() && this._isLocalLAN(ip)) {
|
|
36
21
|
if (!this.peers.has(ip)) {
|
|
37
22
|
console.log(`Discovered peer: ${ip}`);
|
|
38
23
|
this.peers.add(ip);
|
|
39
|
-
let interval = setInterval(() => {
|
|
40
|
-
axios.get(`http://${ip}:42000/pinokio/mem`).then((res) => {
|
|
41
|
-
this.data[ip] = res.data
|
|
42
|
-
console.log("this.data", { ip, data: this.data[ip] })
|
|
43
|
-
if (res.data.proxy) {
|
|
44
|
-
// clearInterval(interval)
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
}, 1000)
|
|
48
24
|
}
|
|
49
25
|
}
|
|
50
26
|
});
|
|
@@ -59,11 +35,25 @@ class PeerDiscovery {
|
|
|
59
35
|
}, this.interval);
|
|
60
36
|
});
|
|
61
37
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
38
|
+
_refresh(host) {
|
|
39
|
+
return axios.get(`http://${host}:${this.default_port}/pinokio/peer`).then((res) => {
|
|
40
|
+
return res.data
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
async refresh() {
|
|
44
|
+
this.info = {}
|
|
45
|
+
let peer_info = await Promise.all(Array.from(this.peers).map((host) => {
|
|
46
|
+
return this._refresh(host)
|
|
47
|
+
}))
|
|
48
|
+
for(let i=0; i<peer_info.length; i++) {
|
|
49
|
+
let host = this.peers[i]
|
|
50
|
+
this.info[host] = {
|
|
51
|
+
host,
|
|
52
|
+
...peer_info[i],
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return this.info
|
|
65
56
|
}
|
|
66
|
-
|
|
67
57
|
_isLocalLAN(ip) {
|
|
68
58
|
return ip.startsWith('192.168.');
|
|
69
59
|
}
|
package/kernel/peer2.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const dgram = require('dgram');
|
|
2
|
+
const axios = require('axios');
|
|
3
|
+
const os = require('os')
|
|
4
|
+
class PeerDiscovery {
|
|
5
|
+
constructor(port = 41234, message = 'ping', interval = 1000) {
|
|
6
|
+
this.port = port;
|
|
7
|
+
this.message = Buffer.from(message);
|
|
8
|
+
this.interval = interval;
|
|
9
|
+
this.socket = dgram.createSocket('udp4');
|
|
10
|
+
this.peers = new Set();
|
|
11
|
+
this.current_account = os.userInfo()
|
|
12
|
+
this.current_ip = this._getLocalIPAddress()
|
|
13
|
+
// this.start();
|
|
14
|
+
}
|
|
15
|
+
account(ip) {
|
|
16
|
+
return this.data[ip].account.username
|
|
17
|
+
}
|
|
18
|
+
homedir(ip) {
|
|
19
|
+
return this.data[ip].homedir
|
|
20
|
+
}
|
|
21
|
+
caddy(ip) {
|
|
22
|
+
return this.data[ip].proxy
|
|
23
|
+
}
|
|
24
|
+
local(ip) {
|
|
25
|
+
return this.data[ip].memory.local
|
|
26
|
+
}
|
|
27
|
+
global(ip) {
|
|
28
|
+
return this.data[ip].memory.global
|
|
29
|
+
}
|
|
30
|
+
start() {
|
|
31
|
+
// Listen for incoming pings
|
|
32
|
+
this.data = {}
|
|
33
|
+
this.socket.on('message', (msg, rinfo) => {
|
|
34
|
+
const ip = rinfo.address;
|
|
35
|
+
if (msg.toString() === this.message.toString() && this._isLocalLAN(ip)) {
|
|
36
|
+
if (!this.peers.has(ip)) {
|
|
37
|
+
console.log(`Discovered peer: ${ip}`);
|
|
38
|
+
this.peers.add(ip);
|
|
39
|
+
let interval = setInterval(() => {
|
|
40
|
+
axios.get(`http://${ip}:42000/pinokio/mem`).then((res) => {
|
|
41
|
+
this.data[ip] = res.data
|
|
42
|
+
console.log("this.data", { ip, data: this.data[ip] })
|
|
43
|
+
if (res.data.proxy) {
|
|
44
|
+
// clearInterval(interval)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
}, 1000)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Enable broadcast
|
|
53
|
+
this.socket.bind(this.port, () => {
|
|
54
|
+
this.socket.setBroadcast(true);
|
|
55
|
+
|
|
56
|
+
// Send broadcast pings every interval
|
|
57
|
+
setInterval(() => {
|
|
58
|
+
this.socket.send(this.message, 0, this.message.length, this.port, '255.255.255.255');
|
|
59
|
+
}, this.interval);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get() {
|
|
64
|
+
return Array.from(this.peers);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_isLocalLAN(ip) {
|
|
68
|
+
return ip.startsWith('192.168.');
|
|
69
|
+
}
|
|
70
|
+
_getLocalIPAddress() {
|
|
71
|
+
const interfaces = os.networkInterfaces();
|
|
72
|
+
for (const ifaceList of Object.values(interfaces)) {
|
|
73
|
+
for (const iface of ifaceList) {
|
|
74
|
+
if (iface.family === 'IPv4' && !iface.internal && iface.address.startsWith('192.168.')) {
|
|
75
|
+
return iface.address;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = PeerDiscovery;
|
package/kernel/prototype.js
CHANGED
|
@@ -27,7 +27,6 @@ class Proto {
|
|
|
27
27
|
for(let proto of prototypes) {
|
|
28
28
|
let pinokiojs = path.resolve(prototype_dir, proto, "pinokio.js")
|
|
29
29
|
let config = await this.kernel.require(pinokiojs)
|
|
30
|
-
console.log({ config })
|
|
31
30
|
if (config && config.type === "prototype") {
|
|
32
31
|
if (config.icon) {
|
|
33
32
|
config.icon = "/prototype/" + proto + "/" + config.icon
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class Common {
|
|
2
|
+
constructor (router) {
|
|
3
|
+
this.router = router
|
|
4
|
+
}
|
|
5
|
+
handle({ match, dial, host }) {
|
|
6
|
+
this.router.config.apps.http.servers.main.routes.push({
|
|
7
|
+
"match": [{ "host": [match] }],
|
|
8
|
+
"handle": [
|
|
9
|
+
{
|
|
10
|
+
"handler": "reverse_proxy",
|
|
11
|
+
"upstreams": [{ "dial": dial }]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"handler": "headers",
|
|
15
|
+
"response": {
|
|
16
|
+
"set": {
|
|
17
|
+
"Access-Control-Allow-Origin": ["*"],
|
|
18
|
+
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
19
|
+
"Access-Control-Allow-Headers": ["*"],
|
|
20
|
+
"Vary": ["Origin"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
})
|
|
26
|
+
this.router.add({ host, dial, match })
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
module.exports = Common
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
class Connector {
|
|
2
|
+
constructor(router) {
|
|
3
|
+
this.router = router
|
|
4
|
+
}
|
|
5
|
+
handle ({ match, connector, dial, }) {
|
|
6
|
+
let connector_name = connector.host + ":" + connector.port
|
|
7
|
+
if(!this.router.config.apps.http.servers[connector_name]) {
|
|
8
|
+
this.router.config.apps.http.servers[connector_name] = {
|
|
9
|
+
"listen": ["0.0.0.0:PORT_PLACEHOLDER_" + connector_name],
|
|
10
|
+
"routes": [{
|
|
11
|
+
"handle": [
|
|
12
|
+
{
|
|
13
|
+
"handler": "reverse_proxy",
|
|
14
|
+
"upstreams": [{ "dial": dial }]
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"handler": "headers",
|
|
18
|
+
"response": {
|
|
19
|
+
"set": {
|
|
20
|
+
"Access-Control-Allow-Origin": ["*"],
|
|
21
|
+
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
22
|
+
"Access-Control-Allow-Headers": ["*"],
|
|
23
|
+
"Vary": ["Origin"]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
this.router.config.apps.http.servers.main.routes.push({
|
|
32
|
+
"match": [{
|
|
33
|
+
"host": [match],
|
|
34
|
+
}],
|
|
35
|
+
"handle": [
|
|
36
|
+
{
|
|
37
|
+
"handler": "reverse_proxy",
|
|
38
|
+
"upstreams": [{ "dial": connector.host + ":PORT_PLACEHOLDER_" + connector_name }]
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"handler": "headers",
|
|
42
|
+
"response": {
|
|
43
|
+
"set": {
|
|
44
|
+
"Access-Control-Allow-Origin": ["*"],
|
|
45
|
+
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
46
|
+
"Access-Control-Allow-Headers": ["*"],
|
|
47
|
+
"Vary": ["Origin"]
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
this.router.add({ host: connector.host, dial, match })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
module.exports = Connector
|