ql-agent 1.3.4 → 1.3.6

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 +124 -41
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,63 +8,146 @@ 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
- };
11
+ async function sendResult(result) {
12
+ if (!agentId) return;
18
13
 
19
- const res = await axios.post(`${relay}/register`, { payload }, {
20
- headers: { "x-relay-token": token }
21
- });
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
+ });
22
34
 
23
- agentId = res.data.id;
35
+ agentId = res.data.id;
24
36
 
25
- } catch (err) {
26
- // retry dans 10s si échec
27
- console.log("register échoué, "+err)
28
- setTimeout(register, 20000);
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
- const full = path.resolve(baseDir, p);
45
- if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
52
+ const full = path.resolve(baseDir, p || '.');
53
+ const normalizedBase = path.resolve(baseDir);
54
+ if (!full.startsWith(normalizedBase)) {
55
+ throw new Error('Chemin invalide');
56
+ }
46
57
  return full;
47
58
  }
48
59
 
49
- // Exécution des commandes reçues
50
- function execCommand(cmd) {
51
- if (cmd.type === "read") {
52
- return fs.readFileSync(safePath(cmd.path), "utf-8");
53
- }
60
+ function buildTree(dir) {
61
+ const stats = fs.statSync(dir);
62
+ const name = path.basename(dir);
54
63
 
55
- if (cmd.type === "write") {
56
- fs.writeFileSync(safePath(cmd.path), cmd.content);
57
- return "ok";
64
+ if (stats.isFile()) {
65
+ return {
66
+ type: 'file',
67
+ name,
68
+ path: dir,
69
+ size: stats.size
70
+ };
58
71
  }
59
72
 
60
- if (cmd.type === "delete") {
61
- const f = safePath(cmd.path);
62
- if (fs.existsSync(f)) fs.unlinkSync(f);
63
- return "ok";
73
+ const children = fs.readdirSync(dir).map((entry) => {
74
+ return buildTree(path.join(dir, entry));
75
+ });
76
+
77
+ return {
78
+ type: 'directory',
79
+ name,
80
+ path: dir,
81
+ children
82
+ };
83
+ }
84
+
85
+ function execCommand(cmd) {
86
+ try {
87
+ if (cmd.type === 'read') {
88
+ const data = fs.readFileSync(safePath(cmd.path), 'utf-8');
89
+ sendResult({ type: 'read', path: cmd.path, data });
90
+ }
91
+
92
+ if (cmd.type === 'write') {
93
+ const target = safePath(cmd.path);
94
+ fs.mkdirSync(path.dirname(target), { recursive: true });
95
+ fs.writeFileSync(target, cmd.content);
96
+ sendResult({ type: 'write', path: cmd.path, status: 'ok' });
97
+ }
98
+
99
+ if (cmd.type === 'delete') {
100
+ const f = safePath(cmd.path);
101
+ if (fs.existsSync(f)) {
102
+ const stats = fs.statSync(f);
103
+ if (stats.isDirectory()) {
104
+ fs.rmSync(f, { recursive: true, force: true });
105
+ } else {
106
+ fs.unlinkSync(f);
107
+ }
108
+ }
109
+ sendResult({ type: 'delete', path: cmd.path, status: 'ok' });
110
+ }
111
+
112
+ if (cmd.type === 'list') {
113
+ const root = safePath(cmd.path || '.');
114
+ const tree = buildTree(root);
115
+ sendResult({ type: 'list', path: cmd.path || '.', tree });
116
+ }
117
+
118
+ if (cmd.type === 'mkdir') {
119
+ const dir = safePath(cmd.path);
120
+ fs.mkdirSync(dir, { recursive: true });
121
+ sendResult({ type: 'mkdir', path: cmd.path, status: 'ok' });
122
+ }
123
+
124
+ if (cmd.type === 'rename') {
125
+ const oldPath = safePath(cmd.path);
126
+ const newPath = safePath(cmd.newPath);
127
+ fs.mkdirSync(path.dirname(newPath), { recursive: true });
128
+ fs.renameSync(oldPath, newPath);
129
+ sendResult({
130
+ type: 'rename',
131
+ oldPath: cmd.path,
132
+ newPath: cmd.newPath,
133
+ status: 'ok'
134
+ });
135
+ }
136
+
137
+ if (cmd.type === 'touch') {
138
+ const file = safePath(cmd.path);
139
+ fs.mkdirSync(path.dirname(file), { recursive: true });
140
+ if (!fs.existsSync(file)) {
141
+ fs.writeFileSync(file, '');
142
+ }
143
+ sendResult({ type: 'touch', path: cmd.path, status: 'ok' });
144
+ }
145
+
146
+ } catch (err) {
147
+ sendResult({ error: err.message, path: cmd.path });
64
148
  }
65
149
  }
66
150
 
67
- // Récupération et exécution des commandes depuis le serveur
68
151
  async function poll() {
69
152
  if (!agentId) return;
70
153
  try {
@@ -73,16 +156,16 @@ async function register() {
73
156
  });
74
157
 
75
158
  for (const cmd of res.data) {
76
- try { execCommand(cmd); } catch {}
159
+ execCommand(cmd);
77
160
  }
78
- } catch (err) {}
161
+ } catch {}
79
162
  }
80
- register()
81
- // Intervalles
82
- setInterval(register, 90000)
163
+
164
+ register();
165
+
166
+ setInterval(register, 93000);
83
167
  setInterval(heartbeat, 10000);
84
168
  setInterval(poll, 5000);
85
169
  }
86
170
 
87
- // Export CommonJS
88
- module.exports = { initAgent };
171
+ 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.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {