ql-agent 1.3.6 → 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 -171
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,171 +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
- const normalizedBase = path.resolve(baseDir);
54
- if (!full.startsWith(normalizedBase)) {
55
- throw new Error('Chemin invalide');
56
- }
57
- return full;
58
- }
59
-
60
- function buildTree(dir) {
61
- const stats = fs.statSync(dir);
62
- const name = path.basename(dir);
63
-
64
- if (stats.isFile()) {
65
- return {
66
- type: 'file',
67
- name,
68
- path: dir,
69
- size: stats.size
70
- };
71
- }
72
-
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 });
148
- }
149
- }
150
-
151
- async function poll() {
152
- if (!agentId) return;
153
- try {
154
- const res = await axios.post(`${relay}/fetch-commands`, { id: agentId }, {
155
- headers: { "x-relay-token": token }
156
- });
157
-
158
- for (const cmd of res.data) {
159
- execCommand(cmd);
160
- }
161
- } catch {}
162
- }
163
-
164
- register();
165
-
166
- setInterval(register, 93000);
167
- setInterval(heartbeat, 10000);
168
- setInterval(poll, 5000);
169
- }
170
-
171
- 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.6",
3
+ "version": "1.3.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {