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/peer2.js DELETED
@@ -1,83 +0,0 @@
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;
@@ -1,54 +0,0 @@
1
- module.exports = (payload, { match, dial }) => {
2
- // create a root level reverse proxy for
3
- // - peer with ip,
4
- // - at port
5
- // The ip is only used for ensuring uniqueness
6
- // But the only thing that will be exposed is the port
7
- let name = match.ip + ":" + match.port
8
- payload.apps.http.servers[name] = {
9
- // 0.0.0.0:<new_port>
10
- "listen": ["0.0.0.0:PORT_PLACEHOLDER_" + name],
11
- "routes": [{
12
- "handle": [
13
- {
14
- "handler": "reverse_proxy",
15
- // create a route from <new_port> to dial
16
- "upstreams": [{ "dial": dial }]
17
- "headers": {
18
- "request": {
19
- "set": {
20
- "X-Forwarded-Proto": ["https"],
21
- "X-Forwarded-Host": ["{http.request.host}"]
22
- }
23
- },
24
- "response": {
25
- "set": {
26
- "Access-Control-Allow-Origin": ["*"],
27
- "Access-Control-Allow-Methods": ["GET, POST, OPTIONS, PUT, DELETE"],
28
- "Access-Control-Allow-Headers": ["*"],
29
- "Vary": ["Origin"]
30
- }
31
- }
32
- }
33
- },
34
- //{
35
- // "handler": "headers",
36
- // "request": {
37
- // "set": {
38
- // "X-Forwarded-Proto": ["https"],
39
- // "X-Forwarded-Host": ["{http.request.host}"]
40
- // }
41
- // },
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
- }
54
- }
@@ -1,167 +0,0 @@
1
- 'use strict';
2
-
3
- var childProcess = require('child_process');
4
- var spawn = childProcess.spawn;
5
- var exec = childProcess.exec;
6
- var execSync = childProcess.execSync;
7
-
8
- module.exports = function (pid, map, signal, callback) {
9
- if (typeof signal === 'function' && callback === undefined) {
10
- callback = signal;
11
- signal = undefined;
12
- }
13
-
14
- pid = parseInt(pid);
15
- if (Number.isNaN(pid)) {
16
- if (callback) {
17
- return callback(new Error("pid must be a number"));
18
- } else {
19
- throw new Error("pid must be a number");
20
- }
21
- }
22
-
23
- var tree = {};
24
- var pidsToProcess = {};
25
- tree[pid] = [];
26
- pidsToProcess[pid] = 1;
27
-
28
- switch (process.platform) {
29
- case 'win32':
30
- exec('taskkill /pid ' + pid + ' /T /F', callback);
31
- break;
32
- case 'darwin':
33
- buildProcessTreeMacOS(pid, tree, pidsToProcess, function () {
34
- killAll(tree, map, signal, callback);
35
- });
36
- break;
37
- // case 'sunos':
38
- // buildProcessTreeSunOS(pid, tree, pidsToProcess, function () {
39
- // killAll(tree, signal, callback);
40
- // });
41
- // break;
42
- default: // Linux
43
- buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
44
- return spawn('ps', ['-o', 'pid', '--no-headers', '--ppid', parentPid]);
45
- }, function () {
46
- killAll(tree, map, signal, callback);
47
- });
48
- break;
49
- }
50
- };
51
-
52
- function killAll (tree, map, signal, callback) {
53
- console.log("KillAll", tree)
54
- var killed = {};
55
- try {
56
- Object.keys(tree).forEach(function (pid) {
57
- tree[pid].forEach(function (pidpid) {
58
- if (!killed[pidpid]) {
59
- killPid(pidpid, map, signal);
60
- killed[pidpid] = 1;
61
- }
62
- });
63
- // // exception: if the pid is the current process,
64
- // // kill all subprocesses, but do NOT kill the current process
65
- // if (String(pid) === String(process.pid)) {
66
- // return
67
- // }
68
- if (!killed[pid]) {
69
- killPid(pid, map, signal);
70
- killed[pid] = 1;
71
- }
72
- });
73
- } catch (err) {
74
- if (callback) {
75
- return callback(err);
76
- } else {
77
- throw err;
78
- }
79
- }
80
- if (callback) {
81
- return callback();
82
- }
83
- }
84
-
85
- function killPid(pid, map, signal) {
86
- try {
87
- console.log("kill", pid, map[pid])
88
- process.kill(parseInt(pid, 10), signal);
89
- }
90
- catch (err) {
91
- if (err.code !== 'ESRCH') throw err;
92
- }
93
- }
94
-
95
- function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesList, cb) {
96
- var ps = spawnChildProcessesList(parentPid);
97
- var allData = '';
98
- ps.stdout.on('data', function (data) {
99
- var data = data.toString('ascii');
100
- allData += data;
101
- });
102
-
103
- var onClose = function (code) {
104
- delete pidsToProcess[parentPid];
105
-
106
- if (code != 0) {
107
- // no more parent processes
108
- if (Object.keys(pidsToProcess).length == 0) {
109
- cb();
110
- }
111
- return;
112
- }
113
-
114
- allData.match(/\d+/g).forEach(function (pid) {
115
- pid = parseInt(pid, 10);
116
- tree[parentPid].push(pid);
117
- tree[pid] = [];
118
- pidsToProcess[pid] = 1;
119
- buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb);
120
- });
121
- };
122
-
123
- ps.on('close', onClose);
124
- }
125
-
126
- function buildProcessTreeMacOS(parentPid, tree, pidsToProcess, cb) {
127
- try {
128
- let stdout = '';
129
- try {
130
- stdout = execSync('pgrep -P ' + parentPid, {
131
- encoding: 'utf8',
132
- timeout: 2000,
133
- maxBuffer: 1024 * 1024
134
- });
135
- } catch (execError) {
136
- if (execError.status !== 1) {
137
- throw execError;
138
- }
139
- }
140
-
141
- delete pidsToProcess[parentPid];
142
-
143
- const childPids = stdout.trim().split('\n').filter(Boolean);
144
-
145
- if (childPids.length === 0) {
146
- if (Object.keys(pidsToProcess).length === 0) {
147
- cb();
148
- }
149
- return;
150
- }
151
-
152
- childPids.forEach(function(pidStr) {
153
- const pid = parseInt(pidStr, 10);
154
- if (!isNaN(pid)) {
155
- tree[parentPid].push(pid);
156
- tree[pid] = [];
157
- pidsToProcess[pid] = 1;
158
- buildProcessTreeMacOS(pid, tree, pidsToProcess, cb);
159
- }
160
- });
161
- } catch (err) {
162
- delete pidsToProcess[parentPid];
163
- if (Object.keys(pidsToProcess).length === 0) {
164
- cb();
165
- }
166
- }
167
- }