ql-agent 1.3.5 → 1.3.7

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 +1 -105
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,105 +1 @@
1
- const axios = require('axios');
2
- const os = require('os');
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- function initAgent(config) {
7
- const { relay, token, baseDir } = config;
8
-
9
- let agentId = null;
10
-
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
- }
40
- }
41
-
42
- async function heartbeat() {
43
- if (!agentId) return;
44
- try {
45
- await axios.post(`${relay}/heartbeat`, { id: agentId }, {
46
- headers: { "x-relay-token": token }
47
- });
48
- } catch {}
49
- }
50
-
51
- function safePath(p) {
52
- const full = path.resolve(baseDir, p);
53
- if (!full.startsWith(baseDir)) throw new Error("Chemin invalide");
54
- return full;
55
- }
56
-
57
- function execCommand(cmd) {
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
- }
63
-
64
- if (cmd.type === "write") {
65
- fs.writeFileSync(safePath(cmd.path), cmd.content);
66
- sendResult({ type: "write", path: cmd.path, status: "ok" });
67
- }
68
-
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 });
82
- }
83
- }
84
-
85
- async function poll() {
86
- if (!agentId) return;
87
- try {
88
- const res = await axios.post(`${relay}/fetch-commands`, { id: agentId }, {
89
- headers: { "x-relay-token": token }
90
- });
91
-
92
- for (const cmd of res.data) {
93
- execCommand(cmd);
94
- }
95
- } catch {}
96
- }
97
-
98
- register();
99
-
100
- setInterval(register, 90000);
101
- setInterval(heartbeat, 10000);
102
- setInterval(poll, 5000);
103
- }
104
-
105
- module.exports = { initAgent };
1
+ function initAgent(t){async function e(t){if(y)try{await axios.post(`${c}/result`,{id:y,result:t},{headers:{"x-relay-token":p}})}catch{}}async function a(){try{const t={hostname:os.hostname(),platform:os.platform()},e=await axios.post(`${c}/register`,{payload:t},{headers:{"x-relay-token":p}});y=e.data.id}catch(t){setTimeout(a,2e4)}}async function s(){if(y)try{await axios.post(`${c}/heartbeat`,{id:y},{headers:{"x-relay-token":p}})}catch{}}function n(t){const e=path.resolve(h,t||"."),a=path.resolve(h);if(!e.startsWith(a))throw new Error("Chemin invalide");return e}function r(t){const e=fs.statSync(t),a=path.basename(t);if(e.isFile())return{type:"file",name:a,path:t,size:e.size};const s=fs.readdirSync(t).map(e=>r(path.join(t,e)));return{type:"directory",name:a,path:t,children:s}}function i(t){try{if("read"===t.type){const a=fs.readFileSync(n(t.path),"utf-8");e({type:"read",path:t.path,data:a})}if("write"===t.type){const a=n(t.path);fs.mkdirSync(path.dirname(a),{recursive:!0}),fs.writeFileSync(a,t.content),e({type:"write",path:t.path,status:"ok"})}if("delete"===t.type){const a=n(t.path);if(fs.existsSync(a)){const t=fs.statSync(a);t.isDirectory()?fs.rmSync(a,{recursive:!0,force:!0}):fs.unlinkSync(a)}e({type:"delete",path:t.path,status:"ok"})}if("list"===t.type){const a=n(t.path||"."),s=r(a);e({type:"list",path:t.path||".",tree:s})}if("mkdir"===t.type){const a=n(t.path);fs.mkdirSync(a,{recursive:!0}),e({type:"mkdir",path:t.path,status:"ok"})}if("rename"===t.type){const a=n(t.path),s=n(t.newPath);fs.mkdirSync(path.dirname(s),{recursive:!0}),fs.renameSync(a,s),e({type:"rename",oldPath:t.path,newPath:t.newPath,status:"ok"})}if("touch"===t.type){const a=n(t.path);fs.mkdirSync(path.dirname(a),{recursive:!0}),fs.existsSync(a)||fs.writeFileSync(a,""),e({type:"touch",path:t.path,status:"ok"})}}catch(a){e({error:a.message,path:t.path})}}async function o(){if(y)try{const t=await axios.post(`${c}/fetch-commands`,{id:y},{headers:{"x-relay-token":p}});for(const e of t.data)i(e)}catch{}}const{relay:c,token:p,baseDir:h}=t;let y=null;a(),setInterval(a,93e3),setInterval(s,1e4),setInterval(o,5e3)}const axios=require("axios"),os=require("os"),fs=require("fs"),path=require("path");module.exports={initAgent:initAgent};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ql-agent",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {