pinokiod 3.10.7 → 3.10.9
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/procs.js +51 -36
- package/package.json +1 -1
package/kernel/procs.js
CHANGED
|
@@ -6,19 +6,25 @@ const platform = os.platform();
|
|
|
6
6
|
class Procs {
|
|
7
7
|
constructor () {
|
|
8
8
|
console.log("Initializing procs")
|
|
9
|
+
this.cache = {}
|
|
9
10
|
}
|
|
10
11
|
async isHttp(localAddress) {
|
|
12
|
+
if (this.cache.hasOwnProperty(localAddress)) {
|
|
13
|
+
return this.cache[localAddress]
|
|
14
|
+
}
|
|
11
15
|
try {
|
|
12
16
|
await axios.head(`http://${localAddress}`, { timeout: 1000 });
|
|
17
|
+
this.cache[localAddress] = true
|
|
13
18
|
return true;
|
|
14
19
|
} catch (err) {
|
|
20
|
+
this.cache[localAddress] = false
|
|
15
21
|
return false;
|
|
16
22
|
}
|
|
17
23
|
}
|
|
18
24
|
getPortPidList(callback) {
|
|
19
25
|
const isWin = platform === 'win32';
|
|
20
26
|
const cmd = isWin ? 'netstat -ano -p tcp' : 'lsof -nP -iTCP -sTCP:LISTEN';
|
|
21
|
-
exec(cmd, async (err, stdout) => {
|
|
27
|
+
exec(cmd, { maxBuffer: 10 * 1024 * 1024 }, async (err, stdout) => {
|
|
22
28
|
if (err) return callback(err);
|
|
23
29
|
|
|
24
30
|
const results = [];
|
|
@@ -29,48 +35,57 @@ class Procs {
|
|
|
29
35
|
for(let line of lines) {
|
|
30
36
|
if (isWin) {
|
|
31
37
|
// Skip headers
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
try {
|
|
39
|
+
if (!line.startsWith(' TCP')) continue;
|
|
40
|
+
const parts = line.trim().split(/\s+/);
|
|
41
|
+
const [ , localAddress, , state, pid ] = parts;
|
|
42
|
+
|
|
43
|
+
//if (state !== 'LISTENING') continue;
|
|
44
|
+
const chunks = localAddress.split(":")
|
|
45
|
+
const port = chunks.pop()
|
|
46
|
+
let ip = chunks.pop()
|
|
47
|
+
if (!ip || ip === "*") {
|
|
48
|
+
ip = "127.0.0.1:" + port
|
|
49
|
+
} else {
|
|
50
|
+
ip = localAddress
|
|
51
|
+
}
|
|
35
52
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
ip = "127.0.0.1:" + port
|
|
44
|
-
} else {
|
|
45
|
-
ip = localAddress
|
|
53
|
+
let isHttp = await this.isHttp(ip)
|
|
54
|
+
if (!isHttp) continue
|
|
55
|
+
|
|
56
|
+
if (pids.has(pid+"/"+port)) continue;
|
|
57
|
+
pids.add(pid+"/"+port)
|
|
58
|
+
results.push({ port, pid, ip });
|
|
59
|
+
} catch (e) {
|
|
46
60
|
}
|
|
47
|
-
if (pids.has(pid+"/"+port)) continue;
|
|
48
|
-
pids.add(pid+"/"+port)
|
|
49
|
-
results.push({ port, pid, ip });
|
|
50
61
|
} else {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
// if (!/LISTEN/.test(line)) continue;
|
|
63
|
+
try {
|
|
64
|
+
const parts = line.trim().split(/\s+/);
|
|
65
|
+
const pid = parts[1];
|
|
66
|
+
|
|
67
|
+
const match = line.match(/([^\s]+:\d+)\s/);
|
|
68
|
+
const localAddress = match?.[1];
|
|
54
69
|
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
const chunks = localAddress.split(":")
|
|
71
|
+
const port = chunks.pop()
|
|
72
|
+
let ip = chunks.pop()
|
|
73
|
+
if (!ip || ip === "*") {
|
|
74
|
+
ip = "127.0.0.1:" + port
|
|
75
|
+
} else {
|
|
76
|
+
ip = localAddress
|
|
77
|
+
}
|
|
57
78
|
|
|
58
|
-
|
|
59
|
-
|
|
79
|
+
let isHttp = await this.isHttp(ip)
|
|
80
|
+
if (!isHttp) continue;
|
|
60
81
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
ip = localAddress
|
|
82
|
+
//const portMatch = line.match(/:(\d+)\s/);
|
|
83
|
+
//const port = portMatch?.[1];
|
|
84
|
+
if (pids.has(pid+"/"+port)) continue;
|
|
85
|
+
pids.add(pid+"/"+port)
|
|
86
|
+
if (pid && port) results.push({ port, pid, ip });
|
|
87
|
+
} catch (e) {
|
|
68
88
|
}
|
|
69
|
-
//const portMatch = line.match(/:(\d+)\s/);
|
|
70
|
-
//const port = portMatch?.[1];
|
|
71
|
-
if (pids.has(pid+"/"+port)) continue;
|
|
72
|
-
pids.add(pid+"/"+port)
|
|
73
|
-
if (pid && port) results.push({ port, pid, ip });
|
|
74
89
|
}
|
|
75
90
|
}
|
|
76
91
|
callback(null, results);
|