ql-agent 1.3.4 → 1.3.5
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/index.js +58 -41
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,63 +8,80 @@ function initAgent(config) {
|
|
|
8
8
|
|
|
9
9
|
let agentId = null;
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
async function sendResult(result) {
|
|
12
|
+
if (!agentId) return;
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await axios.post(`${relay}/result`, {
|
|
16
|
+
id: agentId,
|
|
17
|
+
result
|
|
18
|
+
}, {
|
|
19
|
+
headers: { "x-relay-token": token }
|
|
20
|
+
});
|
|
21
|
+
} catch {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function register() {
|
|
25
|
+
try {
|
|
26
|
+
const payload = {
|
|
27
|
+
hostname: os.hostname(),
|
|
28
|
+
platform: os.platform()
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const res = await axios.post(`${relay}/register`, { payload }, {
|
|
32
|
+
headers: { "x-relay-token": token }
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
agentId = res.data.id;
|
|
36
|
+
|
|
37
|
+
} catch (err) {
|
|
38
|
+
setTimeout(register, 20000);
|
|
39
|
+
}
|
|
29
40
|
}
|
|
30
|
-
}
|
|
31
41
|
|
|
32
|
-
// Envoi du heartbeat
|
|
33
42
|
async function heartbeat() {
|
|
34
43
|
if (!agentId) return;
|
|
35
44
|
try {
|
|
36
45
|
await axios.post(`${relay}/heartbeat`, { id: agentId }, {
|
|
37
46
|
headers: { "x-relay-token": token }
|
|
38
47
|
});
|
|
39
|
-
} catch
|
|
48
|
+
} catch {}
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
// Vérification des chemins pour éviter les sorties de baseDir
|
|
43
51
|
function safePath(p) {
|
|
44
52
|
const full = path.resolve(baseDir, p);
|
|
45
53
|
if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
|
|
46
54
|
return full;
|
|
47
55
|
}
|
|
48
56
|
|
|
49
|
-
// Exécution des commandes reçues
|
|
50
57
|
function execCommand(cmd) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
try {
|
|
59
|
+
if (cmd.type === "read") {
|
|
60
|
+
const data = fs.readFileSync(safePath(cmd.path), "utf-8");
|
|
61
|
+
sendResult({ type: "read", path: cmd.path, data });
|
|
62
|
+
}
|
|
54
63
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
if (cmd.type === "write") {
|
|
65
|
+
fs.writeFileSync(safePath(cmd.path), cmd.content);
|
|
66
|
+
sendResult({ type: "write", path: cmd.path, status: "ok" });
|
|
67
|
+
}
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
if (cmd.type === "delete") {
|
|
70
|
+
const f = safePath(cmd.path);
|
|
71
|
+
if (fs.existsSync(f)) fs.unlinkSync(f);
|
|
72
|
+
sendResult({ type: "delete", path: cmd.path, status: "ok" });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (cmd.type === "list") {
|
|
76
|
+
const files = fs.readdirSync(safePath(cmd.path || "."));
|
|
77
|
+
sendResult({ type: "list", path: cmd.path, files });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
} catch (err) {
|
|
81
|
+
sendResult({ error: err.message, path: cmd.path });
|
|
64
82
|
}
|
|
65
83
|
}
|
|
66
84
|
|
|
67
|
-
// Récupération et exécution des commandes depuis le serveur
|
|
68
85
|
async function poll() {
|
|
69
86
|
if (!agentId) return;
|
|
70
87
|
try {
|
|
@@ -73,16 +90,16 @@ async function register() {
|
|
|
73
90
|
});
|
|
74
91
|
|
|
75
92
|
for (const cmd of res.data) {
|
|
76
|
-
|
|
93
|
+
execCommand(cmd);
|
|
77
94
|
}
|
|
78
|
-
} catch
|
|
95
|
+
} catch {}
|
|
79
96
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
97
|
+
|
|
98
|
+
register();
|
|
99
|
+
|
|
100
|
+
setInterval(register, 90000);
|
|
83
101
|
setInterval(heartbeat, 10000);
|
|
84
102
|
setInterval(poll, 5000);
|
|
85
103
|
}
|
|
86
104
|
|
|
87
|
-
// Export CommonJS
|
|
88
105
|
module.exports = { initAgent };
|