ql-agent 1.0.0 → 1.2.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.
Files changed (2) hide show
  1. package/index.js +66 -19
  2. package/package.json +5 -3
package/index.js CHANGED
@@ -1,39 +1,80 @@
1
- import axios from "axios";
2
- import os from "os";
3
- import fs from "fs";
4
- import path from "path";
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
- export function initAgent(config) {
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;
34
+
35
+ function initAgent(config) {
7
36
  const { relay, token, baseDir } = config;
8
37
 
9
38
  let agentId = null;
10
39
 
11
- async function register() {
40
+ // Enregistrement de l'agent auprès du serveur relay
41
+ async function register() {
42
+ try {
12
43
  const payload = {
13
44
  hostname: os.hostname(),
14
45
  platform: os.platform()
15
46
  };
16
47
 
17
- const res = await axios.post(relay + "/register", { payload }, {
48
+ const res = await axios.post(`${relay}/register`, { payload }, {
18
49
  headers: { "x-relay-token": token }
19
50
  });
20
51
 
21
52
  agentId = res.data.id;
53
+
54
+ } catch (err) {
55
+ // retry dans 10s si échec
56
+ setTimeout(register, 10000);
22
57
  }
58
+ }
23
59
 
60
+ // Envoi du heartbeat
24
61
  async function heartbeat() {
25
62
  if (!agentId) return;
26
- await axios.post(relay + "/heartbeat", { id: agentId }, {
27
- headers: { "x-relay-token": token }
28
- });
63
+ try {
64
+ await axios.post(`${relay}/heartbeat`, { id: agentId }, {
65
+ headers: { "x-relay-token": token }
66
+ });
67
+ } catch (err) {}
29
68
  }
30
69
 
70
+ // Vérification des chemins pour éviter les sorties de baseDir
31
71
  function safePath(p) {
32
72
  const full = path.resolve(baseDir, p);
33
- if (!full.startsWith(baseDir)) throw new Error();
73
+ if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
34
74
  return full;
35
75
  }
36
76
 
77
+ // Exécution des commandes reçues
37
78
  function execCommand(cmd) {
38
79
  if (cmd.type === "read") {
39
80
  return fs.readFileSync(safePath(cmd.path), "utf-8");
@@ -51,19 +92,25 @@ export function initAgent(config) {
51
92
  }
52
93
  }
53
94
 
95
+ // Récupération et exécution des commandes depuis le serveur
54
96
  async function poll() {
55
97
  if (!agentId) return;
98
+ try {
99
+ const res = await axios.post(`${relay}/fetch-commands`, { id: agentId }, {
100
+ headers: { "x-relay-token": token }
101
+ });
56
102
 
57
- const res = await axios.post(relay + "/fetch-commands", { id: agentId }, {
58
- headers: { "x-relay-token": token }
59
- });
60
-
61
- for (const cmd of res.data) {
62
- try { execCommand(cmd); } catch {}
63
- }
103
+ for (const cmd of res.data) {
104
+ try { execCommand(cmd); } catch {}
105
+ }
106
+ } catch (err) {}
64
107
  }
65
108
 
66
- setInterval(register, 15000);
109
+ // Intervalles
110
+ register();
67
111
  setInterval(heartbeat, 10000);
68
112
  setInterval(poll, 5000);
69
113
  }
114
+
115
+ // Export CommonJS
116
+ module.exports = { initAgent };
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "ql-agent",
3
- "version": "1.0.0",
3
+ "version": "1.2.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": "nodemon index.js"
8
9
  },
9
10
  "keywords": [],
10
11
  "author": "",
11
12
  "license": "ISC",
12
13
  "dependencies": {
13
- "axios": "^1.14.0"
14
+ "axios": "^1.14.0",
15
+ "nodemon": "^3.1.14"
14
16
  }
15
17
  }