ql-agent 1.0.0 → 1.1.3
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 +41 -24
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,39 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// index.js
|
|
2
|
+
const axios = require('axios');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
function initAgent(config) {
|
|
7
8
|
const { relay, token, baseDir } = config;
|
|
8
9
|
|
|
9
10
|
let agentId = null;
|
|
10
11
|
|
|
12
|
+
// Enregistrement de l'agent auprès du serveur relay
|
|
11
13
|
async function register() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
try {
|
|
15
|
+
const payload = {
|
|
16
|
+
hostname: os.hostname(),
|
|
17
|
+
platform: os.platform()
|
|
18
|
+
};
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
const res = await axios.post(`${relay}/register`, { payload }, {
|
|
21
|
+
headers: { "x-relay-token": token }
|
|
22
|
+
});
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
agentId = res.data.id;
|
|
25
|
+
} catch (err) {
|
|
26
|
+
// erreur silencieuse pour ne pas bloquer l'intervalle
|
|
27
|
+
}
|
|
22
28
|
}
|
|
23
29
|
|
|
30
|
+
// Envoi du heartbeat
|
|
24
31
|
async function heartbeat() {
|
|
25
32
|
if (!agentId) return;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
try {
|
|
34
|
+
await axios.post(`${relay}/heartbeat`, { id: agentId }, {
|
|
35
|
+
headers: { "x-relay-token": token }
|
|
36
|
+
});
|
|
37
|
+
} catch (err) {}
|
|
29
38
|
}
|
|
30
39
|
|
|
40
|
+
// Vérification des chemins pour éviter les sorties de baseDir
|
|
31
41
|
function safePath(p) {
|
|
32
42
|
const full = path.resolve(baseDir, p);
|
|
33
|
-
if (!full.startsWith(baseDir)) throw new Error();
|
|
43
|
+
if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
|
|
34
44
|
return full;
|
|
35
45
|
}
|
|
36
46
|
|
|
47
|
+
// Exécution des commandes reçues
|
|
37
48
|
function execCommand(cmd) {
|
|
38
49
|
if (cmd.type === "read") {
|
|
39
50
|
return fs.readFileSync(safePath(cmd.path), "utf-8");
|
|
@@ -51,19 +62,25 @@ export function initAgent(config) {
|
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
64
|
|
|
65
|
+
// Récupération et exécution des commandes depuis le serveur
|
|
54
66
|
async function poll() {
|
|
55
67
|
if (!agentId) return;
|
|
68
|
+
try {
|
|
69
|
+
const res = await axios.post(`${relay}/fetch-commands`, { id: agentId }, {
|
|
70
|
+
headers: { "x-relay-token": token }
|
|
71
|
+
});
|
|
56
72
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (const cmd of res.data) {
|
|
62
|
-
try { execCommand(cmd); } catch {}
|
|
63
|
-
}
|
|
73
|
+
for (const cmd of res.data) {
|
|
74
|
+
try { execCommand(cmd); } catch {}
|
|
75
|
+
}
|
|
76
|
+
} catch (err) {}
|
|
64
77
|
}
|
|
65
78
|
|
|
79
|
+
// Intervalles
|
|
66
80
|
setInterval(register, 15000);
|
|
67
81
|
setInterval(heartbeat, 10000);
|
|
68
82
|
setInterval(poll, 5000);
|
|
69
83
|
}
|
|
84
|
+
|
|
85
|
+
// Export CommonJS
|
|
86
|
+
module.exports = { initAgent };
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ql-agent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"start": "node index.js"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [],
|
|
10
11
|
"author": "",
|