pinokiod 3.8.147 → 3.8.149
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/bin/caddy.js +6 -5
- package/kernel/bin/conda.js +5 -1
- package/kernel/bin/index.js +244 -66
- package/kernel/bin/setup.js +130 -0
- package/kernel/environment.js +36 -1
- package/kernel/index.js +23 -13
- package/kernel/loader.js +6 -4
- package/kernel/peer.js +5 -3
- package/kernel/procs.js +2 -1
- package/kernel/prototype.js +12 -9
- package/kernel/router/common.js +50 -41
- package/kernel/router/connector.js +43 -33
- package/kernel/router/index.js +118 -88
- package/kernel/router/localhost_home_router.js +1 -0
- package/kernel/router/localhost_port_router.js +0 -1
- package/kernel/router/localhost_variable_router.js +0 -1
- package/kernel/router/peer_home_router.js +0 -1
- package/kernel/router/peer_peer_router.js +0 -2
- package/kernel/router/peer_port_router.js +0 -5
- package/kernel/router/peer_variable_router.js +0 -4
- package/kernel/util.js +2 -2
- package/package.json +1 -1
- package/server/index.js +240 -375
- package/server/public/fscreator.js +1 -1
- package/server/views/network.ejs +49 -22
- package/server/views/prototypes.ejs +1 -1
- package/server/views/setup.ejs +254 -0
- package/server/views/setup_home.ejs +181 -0
package/kernel/loader.js
CHANGED
|
@@ -40,30 +40,32 @@ class Loader {
|
|
|
40
40
|
resolved = null
|
|
41
41
|
}
|
|
42
42
|
} catch (e) {
|
|
43
|
-
console.log(`[load error] ${_path}`)
|
|
44
43
|
resolved = null
|
|
45
44
|
}
|
|
46
45
|
}
|
|
46
|
+
if (!resolved) {
|
|
47
|
+
console.log(`[did not load] ${_path}`)
|
|
48
|
+
}
|
|
47
49
|
return { resolved, extension, dirname }
|
|
48
50
|
}
|
|
49
51
|
async requireJSON(filepath) {
|
|
50
52
|
let config
|
|
51
53
|
try { config = require(filepath) } catch (e) {
|
|
52
|
-
console.log(`[load
|
|
54
|
+
// console.log(`[did not load] ${filepath}`)
|
|
53
55
|
}
|
|
54
56
|
return config
|
|
55
57
|
}
|
|
56
58
|
async requireJS(filepath) {
|
|
57
59
|
let config
|
|
58
60
|
try { config = require(filepath) } catch (e) {
|
|
59
|
-
console.log(`[load
|
|
61
|
+
// console.log(`[did not load] ${filepath}`)
|
|
60
62
|
}
|
|
61
63
|
try {
|
|
62
64
|
// if the required module is a class, return the instantiated object
|
|
63
65
|
return new config();
|
|
64
66
|
} catch (e) {
|
|
65
67
|
// otherwise return normally
|
|
66
|
-
console.log(`[load
|
|
68
|
+
// console.log(`[did not load] ${filepath}`)
|
|
67
69
|
return config
|
|
68
70
|
}
|
|
69
71
|
}
|
package/kernel/peer.js
CHANGED
|
@@ -86,7 +86,7 @@ class PeerDiscovery {
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
async _broadcast(host) {
|
|
89
|
-
console.log("peer _broadcast", host)
|
|
89
|
+
// console.log("peer _broadcast", host)
|
|
90
90
|
try {
|
|
91
91
|
let res = await axios.post(`http://${host}:${this.default_port}/pinokio/peer/refresh`, {
|
|
92
92
|
timeout: 2000
|
|
@@ -108,6 +108,7 @@ class PeerDiscovery {
|
|
|
108
108
|
// refresh peer info
|
|
109
109
|
async refresh(peers) {
|
|
110
110
|
if (this.active) {
|
|
111
|
+
this.refreshing = true
|
|
111
112
|
let refresh_peers
|
|
112
113
|
if (peers) {
|
|
113
114
|
refresh_peers = peers
|
|
@@ -118,7 +119,7 @@ class PeerDiscovery {
|
|
|
118
119
|
let peer_info = await Promise.all(refresh_peers.map((host) => {
|
|
119
120
|
return this._refresh(host)
|
|
120
121
|
}))
|
|
121
|
-
console.log({ peer_info })
|
|
122
|
+
// console.log({ peer_info })
|
|
122
123
|
for(let i=0; i<peer_info.length; i++) {
|
|
123
124
|
let peer = peer_info[i]
|
|
124
125
|
if (peer) {
|
|
@@ -128,7 +129,8 @@ class PeerDiscovery {
|
|
|
128
129
|
}
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
|
-
console.log("REFRESH", this.info)
|
|
132
|
+
// console.log("REFRESH", this.info)
|
|
133
|
+
this.refreshing = false
|
|
132
134
|
return this.info
|
|
133
135
|
}
|
|
134
136
|
}
|
package/kernel/procs.js
CHANGED
|
@@ -98,6 +98,7 @@ class Procs {
|
|
|
98
98
|
const fullname = pidToName[pid] || 'Unknown';
|
|
99
99
|
const name = fullname.split(path.sep).pop()
|
|
100
100
|
if (["caddy", "caddy.exe"].includes(name)) {
|
|
101
|
+
this.caddy_pid = pid
|
|
101
102
|
return null
|
|
102
103
|
} else {
|
|
103
104
|
map["" + pid] = fullname
|
|
@@ -107,7 +108,7 @@ class Procs {
|
|
|
107
108
|
// list = list.filter((item) => {
|
|
108
109
|
// return !["caddy", "caddy.exe"].includes(item.name.toLowerCase())
|
|
109
110
|
// })
|
|
110
|
-
console.log("#LIST", { list, size: list.length })
|
|
111
|
+
// console.log("#LIST", { list, size: list.length })
|
|
111
112
|
resolve(list)
|
|
112
113
|
});
|
|
113
114
|
});
|
package/kernel/prototype.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const path = require('path')
|
|
3
|
+
const { glob, sync, hasMagic } = require('glob-gitignore')
|
|
3
4
|
class Proto {
|
|
4
5
|
constructor(kernel) {
|
|
5
6
|
this.kernel = kernel
|
|
@@ -10,24 +11,26 @@ class Proto {
|
|
|
10
11
|
let exists = await this.kernel.exists("prototype")
|
|
11
12
|
if (!exists) {
|
|
12
13
|
console.log("prototype doesn't exist. cloning...")
|
|
14
|
+
await fs.promises.mkdir(this.kernel.path("prototype"), { recursive: true }).catch((e) => { })
|
|
13
15
|
await this.kernel.exec({
|
|
14
|
-
message: "git clone https://github.com/peanutcocktail/prototype",
|
|
15
|
-
path: this.kernel.
|
|
16
|
+
message: "git clone https://github.com/peanutcocktail/prototype system",
|
|
17
|
+
path: this.kernel.path("prototype")
|
|
16
18
|
}, (e) => {
|
|
17
19
|
process.stdout.write(e.raw)
|
|
18
20
|
})
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
let cwd = path.resolve(this.kernel.homedir, "prototype")
|
|
25
|
+
let prototype_paths = (await glob('**/pinokio.js', { cwd }))
|
|
21
26
|
let prototype_dir = path.resolve(this.kernel.homedir, "prototype")
|
|
22
|
-
let prototypes = await fs.promises.readdir(prototype_dir, { withFileTypes: true })
|
|
23
|
-
prototypes = prototypes.filter(d => d.isDirectory()).map(d => d.name);
|
|
24
|
-
console.log({ prototypes })
|
|
25
27
|
this.items = []
|
|
26
28
|
this.kv = {}
|
|
27
|
-
for(let
|
|
28
|
-
let
|
|
29
|
+
for(let prototype_path of prototype_paths) {
|
|
30
|
+
let proto = path.dirname(prototype_path)
|
|
31
|
+
let pinokiojs = path.resolve(prototype_dir, prototype_path)
|
|
29
32
|
let config = await this.kernel.require(pinokiojs)
|
|
30
|
-
if (config && config.
|
|
33
|
+
if (config && config.create && typeof config.create === "function") {
|
|
31
34
|
if (config.icon) {
|
|
32
35
|
config.icon = "/prototype/" + proto + "/" + config.icon
|
|
33
36
|
} else {
|
package/kernel/router/common.js
CHANGED
|
@@ -1,50 +1,59 @@
|
|
|
1
|
-
|
|
1
|
+
const Processor = require('./processor')
|
|
2
|
+
class Common extends Processor {
|
|
2
3
|
constructor (router) {
|
|
4
|
+
super()
|
|
3
5
|
this.router = router
|
|
4
6
|
}
|
|
5
7
|
handle({ match, dial, host }) {
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"Origin": ["http://127.0.0.1:11434"],
|
|
16
|
-
"X-Forwarded-Proto": ["https"],
|
|
17
|
-
"X-Forwarded-Host": ["{http.request.host}"]
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
"response": {
|
|
21
|
-
"set": {
|
|
22
|
-
"Access-Control-Allow-Origin": ["*"],
|
|
23
|
-
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
24
|
-
"Access-Control-Allow-Headers": ["*"],
|
|
25
|
-
"Vary": ["Origin"]
|
|
26
|
-
}
|
|
27
|
-
}
|
|
8
|
+
let handler = [{
|
|
9
|
+
"handler": "reverse_proxy",
|
|
10
|
+
"upstreams": [{ "dial": dial }],
|
|
11
|
+
"headers": {
|
|
12
|
+
"request": {
|
|
13
|
+
"set": {
|
|
14
|
+
// "Origin": ["http://127.0.0.1:11434"],
|
|
15
|
+
"X-Forwarded-Proto": ["https"],
|
|
16
|
+
"X-Forwarded-Host": ["{http.request.host}"]
|
|
28
17
|
}
|
|
29
18
|
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
19
|
+
"response": {
|
|
20
|
+
"set": {
|
|
21
|
+
"Access-Control-Allow-Origin": ["*"],
|
|
22
|
+
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
23
|
+
"Access-Control-Allow-Headers": ["*"],
|
|
24
|
+
"Vary": ["Origin"]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
//{
|
|
30
|
+
// "handler": "headers",
|
|
31
|
+
// "request": {
|
|
32
|
+
// "set": {
|
|
33
|
+
// "X-Forwarded-Proto": ["https"],
|
|
34
|
+
// "X-Forwarded-Host": ["{http.request.host}"]
|
|
35
|
+
// }
|
|
36
|
+
// },
|
|
37
|
+
// "response": {
|
|
38
|
+
// "set": {
|
|
39
|
+
// "Access-Control-Allow-Origin": ["*"],
|
|
40
|
+
// "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
41
|
+
// "Access-Control-Allow-Headers": ["*"],
|
|
42
|
+
// "Vary": ["Origin"]
|
|
43
|
+
// }
|
|
44
|
+
// }
|
|
45
|
+
//}
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
// if the dial port has been overridden by router.custom_routers, use that instead
|
|
49
|
+
let parsed_dial = this.parse_ip(dial)
|
|
50
|
+
let override_handler = this.router.custom_routers[String(parsed_dial.port)]
|
|
51
|
+
if (override_handler) {
|
|
52
|
+
handler = override_handler
|
|
53
|
+
}
|
|
54
|
+
this.router.config.apps.http.servers.main.routes.push({
|
|
55
|
+
"match": [{ "host": [match] }],
|
|
56
|
+
"handle": handler
|
|
48
57
|
})
|
|
49
58
|
this.router.add({ host, dial, match })
|
|
50
59
|
}
|
|
@@ -1,41 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
const Processor = require('./processor')
|
|
2
|
+
class Connector extends Processor {
|
|
2
3
|
constructor(router) {
|
|
4
|
+
super()
|
|
3
5
|
this.router = router
|
|
4
6
|
}
|
|
5
7
|
handle ({ match, connector, dial, }) {
|
|
6
|
-
console.log("connector.handle", { match, connector, dial })
|
|
8
|
+
// console.log("connector.handle", { match, connector, dial })
|
|
7
9
|
let connector_name = connector.host + ":" + connector.port
|
|
8
10
|
let listener = "0.0.0.0:PORT_PLACEHOLDER_" + connector_name
|
|
9
11
|
let listener_pointer = connector.host + ":PORT_PLACEHOLDER_" + connector_name
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
},
|
|
12
|
+
let port_placeholder = "PORT_PLACEHOLDER_" + connector_name
|
|
13
|
+
// console.log("CONNECTING", { listener, dial, listener_pointer, match })
|
|
14
|
+
|
|
15
|
+
let handler = [{
|
|
16
|
+
"handler": "reverse_proxy",
|
|
17
|
+
"upstreams": [{ "dial": dial }],
|
|
18
|
+
"headers": {
|
|
19
|
+
"request": {
|
|
20
|
+
"set": {
|
|
21
|
+
// "Origin": ["http://127.0.0.1:11434"],
|
|
22
|
+
"X-Forwarded-Proto": ["{http.request.header.X-Forwarded-Proto}"],
|
|
23
|
+
//"X-Forwarded-Proto": ["{http.request.scheme}"],
|
|
24
|
+
//"X-Forwarded-Proto": ["https"],
|
|
25
|
+
//"X-Forwarded-Host": ["{http.request.host}"]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"response": {
|
|
29
|
+
"set": {
|
|
30
|
+
"Access-Control-Allow-Origin": ["*"],
|
|
31
|
+
"Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
|
|
32
|
+
"Access-Control-Allow-Headers": ["*"],
|
|
33
|
+
"Vary": ["Origin"]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
39
37
|
// {
|
|
40
38
|
// "handler": "headers",
|
|
41
39
|
// "request": {
|
|
@@ -53,7 +51,18 @@ class Connector {
|
|
|
53
51
|
// }
|
|
54
52
|
// }
|
|
55
53
|
// }
|
|
56
|
-
|
|
54
|
+
}]
|
|
55
|
+
// if the dial port has been overridden by router.custom_routers, use that instead
|
|
56
|
+
let parsed_dial = this.parse_ip(dial)
|
|
57
|
+
let override_handler = this.router.custom_routers[String(parsed_dial.port)]
|
|
58
|
+
if (override_handler) {
|
|
59
|
+
handler = override_handler
|
|
60
|
+
}
|
|
61
|
+
if(!this.router.config.apps.http.servers[connector_name]) {
|
|
62
|
+
this.router.config.apps.http.servers[connector_name] = {
|
|
63
|
+
"listen": [listener],
|
|
64
|
+
"routes": [{
|
|
65
|
+
"handle": handler
|
|
57
66
|
}]
|
|
58
67
|
}
|
|
59
68
|
}
|
|
@@ -103,8 +112,9 @@ class Connector {
|
|
|
103
112
|
// }
|
|
104
113
|
]
|
|
105
114
|
})
|
|
106
|
-
console.log("Router.add", { host: connector.host, dial: listener_pointer, match })
|
|
107
115
|
this.router.add({ host: connector.host, dial: listener_pointer, match })
|
|
116
|
+
// this.router._tree[connector_name] = dial
|
|
117
|
+
// this.router._tree[connector_name] = dial
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
120
|
module.exports = Connector
|
package/kernel/router/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const axios = require('axios')
|
|
2
2
|
const path = require('path')
|
|
3
|
+
const { glob, sync, hasMagic } = require('glob-gitignore')
|
|
3
4
|
const Util = require('../util')
|
|
4
5
|
const LocalhostHomeRouter = require('./localhost_home_router')
|
|
5
6
|
const LocalhostVariableRouter = require('./localhost_variable_router')
|
|
@@ -28,6 +29,47 @@ class Router {
|
|
|
28
29
|
this.mapping = {}
|
|
29
30
|
this.port_mapping = {} // 127.0.0.1 => 192.168,..
|
|
30
31
|
this.local_network_mapping = {}
|
|
32
|
+
this.custom_routers = {}
|
|
33
|
+
}
|
|
34
|
+
async init() {
|
|
35
|
+
console.log("router.init")
|
|
36
|
+
// if ~/pinokio/network doesn't exist, clone
|
|
37
|
+
let exists = await this.kernel.exists("network/system")
|
|
38
|
+
if (!exists) {
|
|
39
|
+
console.log("network doesn't exist. cloning...")
|
|
40
|
+
await fs.promises.mkdir(this.kernel.path("network"), { recursive: true }).catch((e) => { })
|
|
41
|
+
await this.kernel.exec({
|
|
42
|
+
message: "git clone https://github.com/peanutcocktail/network system",
|
|
43
|
+
path: this.kernel.path("network")
|
|
44
|
+
}, (e) => {
|
|
45
|
+
process.stdout.write(e.raw)
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
let cwd = path.resolve(this.kernel.homedir, "network")
|
|
51
|
+
let router_paths = (await glob('**/*.json', { cwd }))
|
|
52
|
+
let router_dir = path.resolve(this.kernel.homedir, "network")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
// create a custom_map that maps port to custom router declarations
|
|
56
|
+
/*
|
|
57
|
+
custom_routers = {
|
|
58
|
+
PORT1: <caddy handler>,
|
|
59
|
+
PORT2: <caddy handler>,
|
|
60
|
+
...
|
|
61
|
+
}
|
|
62
|
+
*/
|
|
63
|
+
this.custom_routers = {}
|
|
64
|
+
console.log({ router_paths })
|
|
65
|
+
for(let router_path of router_paths) {
|
|
66
|
+
let router_abs_path = this.kernel.path("network", router_path)
|
|
67
|
+
let config = await this.kernel.require(router_abs_path)
|
|
68
|
+
console.log({ config })
|
|
69
|
+
for(let port in config) {
|
|
70
|
+
this.custom_routers[port] = config[port]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
31
73
|
}
|
|
32
74
|
async add({ host, match, dial }) {
|
|
33
75
|
if (!this._mapping[host]) {
|
|
@@ -50,7 +92,6 @@ class Router {
|
|
|
50
92
|
}
|
|
51
93
|
}
|
|
52
94
|
|
|
53
|
-
console.log("BEFORE REPLACING", mapping)
|
|
54
95
|
|
|
55
96
|
let host_mapping = mapping[this.kernel.peer.host]
|
|
56
97
|
if (host_mapping) {
|
|
@@ -60,7 +101,7 @@ class Router {
|
|
|
60
101
|
let transformed = key.replace(":" + cache_key, '')
|
|
61
102
|
let port = this.port_cache[cache_key]
|
|
62
103
|
host_mapping[transformed + ":" + port] = host_mapping[key]
|
|
63
|
-
console.log("replacing", transformed + ":" + port, "with", host_mapping[key])
|
|
104
|
+
// console.log("replacing", transformed + ":" + port, "with", host_mapping[key])
|
|
64
105
|
delete host_mapping[key]
|
|
65
106
|
}
|
|
66
107
|
}
|
|
@@ -75,9 +116,10 @@ class Router {
|
|
|
75
116
|
// let m = JSON.parse(json)
|
|
76
117
|
//
|
|
77
118
|
// mapping[this.kernel.peer.host] = m
|
|
78
|
-
console.log("AFTER REPLACING", mapping)
|
|
119
|
+
// console.log("AFTER REPLACING", mapping)
|
|
79
120
|
}
|
|
80
121
|
|
|
122
|
+
|
|
81
123
|
return mapping
|
|
82
124
|
|
|
83
125
|
// let json = JSON.stringify(mapping)
|
|
@@ -105,17 +147,17 @@ class Router {
|
|
|
105
147
|
*/
|
|
106
148
|
published() {
|
|
107
149
|
let pub = {}
|
|
108
|
-
console.log("published info", this.info)
|
|
150
|
+
// console.log("published info", this.info)
|
|
109
151
|
if (this.info) {
|
|
110
152
|
let routes = this.info[this.kernel.peer.host]
|
|
111
153
|
for(let dial in routes) {
|
|
112
154
|
let matches = routes[dial]
|
|
113
155
|
// current host => the single source of truth
|
|
114
|
-
if (dial.startsWith(this.kernel.peer.host)) {
|
|
156
|
+
// if (dial.startsWith(this.kernel.peer.host)) {
|
|
115
157
|
pub[dial] = matches
|
|
116
|
-
}
|
|
158
|
+
// }
|
|
117
159
|
}
|
|
118
|
-
console.log({ pub })
|
|
160
|
+
// console.log({ pub })
|
|
119
161
|
}
|
|
120
162
|
return pub
|
|
121
163
|
}
|
|
@@ -143,14 +185,10 @@ class Router {
|
|
|
143
185
|
}
|
|
144
186
|
*/
|
|
145
187
|
|
|
146
|
-
|
|
147
|
-
console.log("^^^^^^^^", { count , matches })
|
|
148
|
-
|
|
149
188
|
let new_config
|
|
150
189
|
|
|
151
190
|
if (count > 0) {
|
|
152
191
|
let ports = await this.kernel.ports(count)
|
|
153
|
-
console.log("Filling with port cache", JSON.stringify({ port_cache: this.port_cache, config: this.config }, null, 2))
|
|
154
192
|
let response = Util.fill_object(this.config, /PORT_PLACEHOLDER_.+/, ports, this.port_cache)
|
|
155
193
|
this.config = response.result
|
|
156
194
|
let replaced_map = response.replaced_map
|
|
@@ -170,7 +208,7 @@ class Router {
|
|
|
170
208
|
|
|
171
209
|
//let original_port = key.replace("PORT_PLACEHOLDER_", "")
|
|
172
210
|
this.port_mapping[original_port] = replaced_map[key]
|
|
173
|
-
console.log("set port_mapping", { original_port, key, replaced_map })
|
|
211
|
+
// console.log("set port_mapping", { original_port, key, replaced_map })
|
|
174
212
|
}
|
|
175
213
|
this.port_cache = replaced_map
|
|
176
214
|
}
|
|
@@ -181,122 +219,114 @@ class Router {
|
|
|
181
219
|
// set local config
|
|
182
220
|
async local() {
|
|
183
221
|
this._mapping = {}
|
|
184
|
-
|
|
222
|
+
// this._tree = {}
|
|
185
223
|
this.localhost_home_router.handle()
|
|
186
|
-
console.log("after 1", this._mapping)
|
|
187
224
|
this.localhost_variable_router.handle(this.kernel.memory.local)
|
|
188
|
-
console.log("after 2", this._mapping)
|
|
189
225
|
for(let proc of this.kernel.processes.info) {
|
|
190
226
|
this.localhost_port_router.handle(proc)
|
|
191
227
|
}
|
|
192
|
-
console.log("after 3", this._mapping)
|
|
193
228
|
//this.mapping = this._mapping
|
|
194
229
|
|
|
195
230
|
|
|
196
231
|
for(let host in this.kernel.peer.info) {
|
|
197
232
|
let peer = this.kernel.peer.info[host]
|
|
198
233
|
if (peer.host === this.kernel.peer.host) {
|
|
199
|
-
console.log("peer.host === kernel.peer.host", peer.host)
|
|
200
234
|
this.peer_home_router.handle(peer)
|
|
201
|
-
console.log("after 4", this._mapping)
|
|
202
235
|
this.peer_variable_router.handle(peer)
|
|
203
|
-
console.log("after 5", this._mapping)
|
|
204
236
|
this.peer_port_router.handle(peer)
|
|
205
|
-
console.log("after 6", this._mapping)
|
|
206
237
|
}
|
|
207
238
|
}
|
|
208
|
-
console.log("after 7", this._mapping)
|
|
209
239
|
await this.fill()
|
|
210
|
-
console.log("after 8", this._mapping)
|
|
211
240
|
this.mapping = this._mapping
|
|
241
|
+
// this.tree = this._tree
|
|
212
242
|
}
|
|
213
243
|
|
|
214
244
|
// set remote config
|
|
215
245
|
async remote() {
|
|
216
|
-
console.log("Before remote", this.kernel.peer.info)
|
|
217
246
|
for(let host in this.kernel.peer.info) {
|
|
218
247
|
let peer = this.kernel.peer.info[host]
|
|
219
248
|
if (peer.host !== this.kernel.peer.host) {
|
|
220
249
|
this.peer_peer_router.handle(peer)
|
|
221
250
|
}
|
|
222
251
|
}
|
|
223
|
-
console.log("After remote", this.kernel.peer.info)
|
|
224
252
|
//await this.fill()
|
|
225
253
|
this.mapping = this._mapping
|
|
254
|
+
// this.tree = this._tree
|
|
226
255
|
this.info = this._info()
|
|
256
|
+
// console.log("TREE", JSON.stringify(this.tree, null, 2))
|
|
227
257
|
}
|
|
228
258
|
|
|
229
259
|
// update caddy config
|
|
230
260
|
async update() {
|
|
231
|
-
// console.log("this.config", JSON.stringify(this.config))
|
|
232
|
-
// console.log("this.old_config", JSON.stringify(this.old_config))
|
|
233
261
|
if (JSON.stringify(this.config) === JSON.stringify(this.old_config)) {
|
|
234
|
-
console.log("config hasn't updated")
|
|
262
|
+
// console.log("config hasn't updated")
|
|
235
263
|
} else {
|
|
236
|
-
console.log("config has updated")
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
264
|
+
console.log("caddy config has updated", this.config)
|
|
265
|
+
try {
|
|
266
|
+
let response = await axios.post('http://127.0.0.1:2019/load', this.config, {
|
|
267
|
+
headers: { 'Content-Type': 'application/json' }
|
|
268
|
+
})
|
|
269
|
+
console.log("Caddy Response", { response })
|
|
270
|
+
this.old_config = this.config
|
|
271
|
+
} catch (e) {
|
|
240
272
|
console.log("EEEEEEEEEEEEE", e.message)
|
|
241
|
-
})
|
|
242
|
-
// console.log("Caddy Response", { response })
|
|
243
|
-
this.old_config = this.config
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async refresh() {
|
|
248
|
-
// console.log("router.refresh")
|
|
249
|
-
// console.time("router.refresh")
|
|
250
|
-
// console.log("> procs", this.kernel.processes.info)
|
|
251
|
-
// console.log("> peers", this.kernel.peer.info)
|
|
252
|
-
this._mapping = {}
|
|
253
|
-
// this.port_mapping = {} // 127.0.0.1 => 192.168,..
|
|
254
|
-
// this.local_network_mapping = {}
|
|
255
|
-
|
|
256
|
-
let old_config = this.config
|
|
257
|
-
|
|
258
|
-
// 1. Localhost handling
|
|
259
|
-
this.localhost_home_router.handle()
|
|
260
|
-
this.localhost_variable_router.handle(this.kernel.memory.local)
|
|
261
|
-
for(let proc of this.kernel.processes.info) {
|
|
262
|
-
this.localhost_port_router.handle(proc)
|
|
263
|
-
}
|
|
264
|
-
this.mapping = this._mapping
|
|
265
|
-
|
|
266
|
-
// 2. Peers handling
|
|
267
|
-
for(let host in this.kernel.peer.info) {
|
|
268
|
-
let peer = this.kernel.peer.info[host]
|
|
269
|
-
if (peer.host === this.kernel.peer.host) {
|
|
270
|
-
this.peer_home_router.handle(peer)
|
|
271
|
-
this.peer_variable_router.handle(peer)
|
|
272
|
-
this.peer_port_router.handle(peer)
|
|
273
|
-
} else {
|
|
274
|
-
this.peer_peer_router.handle(peer)
|
|
275
273
|
}
|
|
276
274
|
}
|
|
277
|
-
// replace :PORT_PLACEHOLDER with available ports
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
await this.fill()
|
|
281
|
-
this.mapping = this._mapping
|
|
282
|
-
console.timeEnd("router.refresh")
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
// compare old config with new config
|
|
286
|
-
// if it hasn't changed, don't do anything
|
|
287
|
-
// if it has changed, refresh
|
|
288
|
-
|
|
289
|
-
if (JSON.stringify(this.config) === JSON.stringify(old_config)) {
|
|
290
|
-
console.log("config hasn't updated")
|
|
291
|
-
} else {
|
|
292
|
-
console.log("config has updated")
|
|
293
|
-
let response = await axios.post('http://127.0.0.1:2019/load', this.config, {
|
|
294
|
-
headers: { 'Content-Type': 'application/json' }
|
|
295
|
-
}).catch((e) => {
|
|
296
|
-
console.log("EEEEEEEEEEEEE", e.message)
|
|
297
|
-
})
|
|
298
|
-
console.log("Caddy Response", { response })
|
|
299
|
-
}
|
|
300
275
|
}
|
|
276
|
+
|
|
277
|
+
// async refresh() {
|
|
278
|
+
//// console.log("router.refresh")
|
|
279
|
+
//// console.time("router.refresh")
|
|
280
|
+
//// console.log("> procs", this.kernel.processes.info)
|
|
281
|
+
//// console.log("> peers", this.kernel.peer.info)
|
|
282
|
+
// this._mapping = {}
|
|
283
|
+
//// this.port_mapping = {} // 127.0.0.1 => 192.168,..
|
|
284
|
+
//// this.local_network_mapping = {}
|
|
285
|
+
//
|
|
286
|
+
// let old_config = this.config
|
|
287
|
+
//
|
|
288
|
+
// // 1. Localhost handling
|
|
289
|
+
// this.localhost_home_router.handle()
|
|
290
|
+
// this.localhost_variable_router.handle(this.kernel.memory.local)
|
|
291
|
+
// for(let proc of this.kernel.processes.info) {
|
|
292
|
+
// this.localhost_port_router.handle(proc)
|
|
293
|
+
// }
|
|
294
|
+
// this.mapping = this._mapping
|
|
295
|
+
//
|
|
296
|
+
// // 2. Peers handling
|
|
297
|
+
// for(let host in this.kernel.peer.info) {
|
|
298
|
+
// let peer = this.kernel.peer.info[host]
|
|
299
|
+
// if (peer.host === this.kernel.peer.host) {
|
|
300
|
+
// this.peer_home_router.handle(peer)
|
|
301
|
+
// this.peer_variable_router.handle(peer)
|
|
302
|
+
// this.peer_port_router.handle(peer)
|
|
303
|
+
// } else {
|
|
304
|
+
// this.peer_peer_router.handle(peer)
|
|
305
|
+
// }
|
|
306
|
+
// }
|
|
307
|
+
// // replace :PORT_PLACEHOLDER with available ports
|
|
308
|
+
//
|
|
309
|
+
//
|
|
310
|
+
// await this.fill()
|
|
311
|
+
// this.mapping = this._mapping
|
|
312
|
+
// console.timeEnd("router.refresh")
|
|
313
|
+
//
|
|
314
|
+
//
|
|
315
|
+
// // compare old config with new config
|
|
316
|
+
// // if it hasn't changed, don't do anything
|
|
317
|
+
// // if it has changed, refresh
|
|
318
|
+
//
|
|
319
|
+
// if (JSON.stringify(this.config) === JSON.stringify(old_config)) {
|
|
320
|
+
// console.log("config hasn't updated")
|
|
321
|
+
// } else {
|
|
322
|
+
// console.log("config has updated")
|
|
323
|
+
// let response = await axios.post('http://127.0.0.1:2019/load', this.config, {
|
|
324
|
+
// headers: { 'Content-Type': 'application/json' }
|
|
325
|
+
// }).catch((e) => {
|
|
326
|
+
// console.log("EEEEEEEEEEEEE", e.message)
|
|
327
|
+
// })
|
|
328
|
+
// console.log("Caddy Response", { response })
|
|
329
|
+
// }
|
|
330
|
+
// }
|
|
301
331
|
}
|
|
302
332
|
module.exports = Router
|
|
@@ -3,6 +3,7 @@ class LocalhostHomeRouter {
|
|
|
3
3
|
this.router = router
|
|
4
4
|
}
|
|
5
5
|
handle () {
|
|
6
|
+
this.router.add({ host: this.router.kernel.peer.host, dial: this.router.default_host + ":" + this.router.default_port, match: this.router.default_match })
|
|
6
7
|
this.router.config = {
|
|
7
8
|
"apps": {
|
|
8
9
|
"http": {
|