ql-agent 1.3.5 → 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.
- package/index.js +82 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -49,32 +49,98 @@ function initAgent(config) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
function safePath(p) {
|
|
52
|
-
const full = path.resolve(baseDir, p);
|
|
53
|
-
|
|
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
|
+
}
|
|
54
57
|
return full;
|
|
55
58
|
}
|
|
56
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
|
+
|
|
57
85
|
function execCommand(cmd) {
|
|
58
86
|
try {
|
|
59
|
-
if (cmd.type ===
|
|
60
|
-
const data = fs.readFileSync(safePath(cmd.path),
|
|
61
|
-
sendResult({ type:
|
|
87
|
+
if (cmd.type === 'read') {
|
|
88
|
+
const data = fs.readFileSync(safePath(cmd.path), 'utf-8');
|
|
89
|
+
sendResult({ type: 'read', path: cmd.path, data });
|
|
62
90
|
}
|
|
63
91
|
|
|
64
|
-
if (cmd.type ===
|
|
65
|
-
|
|
66
|
-
|
|
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' });
|
|
67
97
|
}
|
|
68
98
|
|
|
69
|
-
if (cmd.type ===
|
|
99
|
+
if (cmd.type === 'delete') {
|
|
70
100
|
const f = safePath(cmd.path);
|
|
71
|
-
if (fs.existsSync(f))
|
|
72
|
-
|
|
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
|
+
});
|
|
73
135
|
}
|
|
74
136
|
|
|
75
|
-
if (cmd.type ===
|
|
76
|
-
const
|
|
77
|
-
|
|
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' });
|
|
78
144
|
}
|
|
79
145
|
|
|
80
146
|
} catch (err) {
|
|
@@ -97,9 +163,9 @@ function initAgent(config) {
|
|
|
97
163
|
|
|
98
164
|
register();
|
|
99
165
|
|
|
100
|
-
setInterval(register,
|
|
166
|
+
setInterval(register, 93000);
|
|
101
167
|
setInterval(heartbeat, 10000);
|
|
102
168
|
setInterval(poll, 5000);
|
|
103
169
|
}
|
|
104
170
|
|
|
105
|
-
module.exports = { initAgent };
|
|
171
|
+
module.exports = { initAgent };
|