ql-agent 1.3.3 → 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 +59 -69
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,98 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// const path = require('path');
|
|
6
|
-
|
|
7
|
-
// const { PrismaClient } = require('@prisma/client');
|
|
8
|
-
|
|
9
|
-
// const prisma = new PrismaClient({
|
|
10
|
-
// log: process.env.NODE_ENV === 'development'
|
|
11
|
-
// ? ['query', 'error', 'warn']
|
|
12
|
-
// : ['error'],
|
|
13
|
-
// transactionOptions: {
|
|
14
|
-
// maxWait: 10000, // temps max d'attente pour demarrer la transaction
|
|
15
|
-
// timeout: 30000, // duree max de la transaction (BD distante)
|
|
16
|
-
// },
|
|
17
|
-
// });
|
|
18
|
-
|
|
19
|
-
// // 🔎 Test de connexion au démarrage
|
|
20
|
-
// async function connectDatabase() {
|
|
21
|
-
// try {
|
|
22
|
-
// await prisma.$connect();
|
|
23
|
-
// console.log("✅ Connexion à la base de données établie avec succès");
|
|
24
|
-
// } catch (error) {
|
|
25
|
-
// console.error("❌ Impossible de se connecter à la base de données");
|
|
26
|
-
// console.error(error.message);
|
|
27
|
-
// process.exit(1); // Arrête le serveur si la DB est inaccessible
|
|
28
|
-
// }
|
|
29
|
-
// }
|
|
30
|
-
|
|
31
|
-
// connectDatabase();
|
|
32
|
-
|
|
33
|
-
// module.exports = prisma;
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
34
5
|
|
|
35
6
|
function initAgent(config) {
|
|
36
7
|
const { relay, token, baseDir } = config;
|
|
37
8
|
|
|
38
9
|
let agentId = null;
|
|
39
10
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
}
|
|
47
23
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
+
});
|
|
51
34
|
|
|
52
|
-
|
|
35
|
+
agentId = res.data.id;
|
|
53
36
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
37
|
+
} catch (err) {
|
|
38
|
+
setTimeout(register, 20000);
|
|
39
|
+
}
|
|
57
40
|
}
|
|
58
|
-
}
|
|
59
41
|
|
|
60
|
-
// Envoi du heartbeat
|
|
61
42
|
async function heartbeat() {
|
|
62
43
|
if (!agentId) return;
|
|
63
44
|
try {
|
|
64
45
|
await axios.post(`${relay}/heartbeat`, { id: agentId }, {
|
|
65
46
|
headers: { "x-relay-token": token }
|
|
66
47
|
});
|
|
67
|
-
} catch
|
|
48
|
+
} catch {}
|
|
68
49
|
}
|
|
69
50
|
|
|
70
|
-
// Vérification des chemins pour éviter les sorties de baseDir
|
|
71
51
|
function safePath(p) {
|
|
72
52
|
const full = path.resolve(baseDir, p);
|
|
73
53
|
if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
|
|
74
54
|
return full;
|
|
75
55
|
}
|
|
76
56
|
|
|
77
|
-
// Exécution des commandes reçues
|
|
78
57
|
function execCommand(cmd) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
}
|
|
82
63
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
64
|
+
if (cmd.type === "write") {
|
|
65
|
+
fs.writeFileSync(safePath(cmd.path), cmd.content);
|
|
66
|
+
sendResult({ type: "write", path: cmd.path, status: "ok" });
|
|
67
|
+
}
|
|
87
68
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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 });
|
|
92
82
|
}
|
|
93
83
|
}
|
|
94
84
|
|
|
95
|
-
// Récupération et exécution des commandes depuis le serveur
|
|
96
85
|
async function poll() {
|
|
97
86
|
if (!agentId) return;
|
|
98
87
|
try {
|
|
@@ -101,15 +90,16 @@ async function register() {
|
|
|
101
90
|
});
|
|
102
91
|
|
|
103
92
|
for (const cmd of res.data) {
|
|
104
|
-
|
|
93
|
+
execCommand(cmd);
|
|
105
94
|
}
|
|
106
|
-
} catch
|
|
95
|
+
} catch {}
|
|
107
96
|
}
|
|
108
|
-
|
|
109
|
-
|
|
97
|
+
|
|
98
|
+
register();
|
|
99
|
+
|
|
100
|
+
setInterval(register, 90000);
|
|
110
101
|
setInterval(heartbeat, 10000);
|
|
111
102
|
setInterval(poll, 5000);
|
|
112
103
|
}
|
|
113
104
|
|
|
114
|
-
// Export CommonJS
|
|
115
105
|
module.exports = { initAgent };
|