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.
Files changed (2) hide show
  1. package/index.js +58 -41
  2. 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
- // Enregistrement de l'agent auprès du serveur relay
12
- async function register() {
13
- try {
14
- const payload = {
15
- hostname: os.hostname(),
16
- platform: os.platform()
17
- };
18
-
19
- const res = await axios.post(`${relay}/register`, { payload }, {
20
- headers: { "x-relay-token": token }
21
- });
22
-
23
- agentId = res.data.id;
24
-
25
- } catch (err) {
26
- // retry dans 10s si échec
27
- console.log("register échoué, "+err)
28
- setTimeout(register, 20000);
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 (err) {}
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
- if (cmd.type === "read") {
52
- return fs.readFileSync(safePath(cmd.path), "utf-8");
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
- if (cmd.type === "write") {
56
- fs.writeFileSync(safePath(cmd.path), cmd.content);
57
- return "ok";
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
- if (cmd.type === "delete") {
61
- const f = safePath(cmd.path);
62
- if (fs.existsSync(f)) fs.unlinkSync(f);
63
- return "ok";
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
- try { execCommand(cmd); } catch {}
93
+ execCommand(cmd);
77
94
  }
78
- } catch (err) {}
95
+ } catch {}
79
96
  }
80
- register()
81
- // Intervalles
82
- setInterval(register, 90000)
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ql-agent",
3
- "version": "1.3.4",
3
+ "version": "1.3.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {