-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
279 lines (262 loc) · 9.8 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const express = require("express");
const { exec } = require("child_process");
const path = require("path");
const sudoPrompt = require("sudo-prompt");
var zip = require("express-zip");
bodyParser = require("body-parser");
let labsconfig = require("./config/labspath.json");
const app = express();
app.use(bodyParser.json());
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "assets")));
const port = 3000;
let RunningVmData = [];
// test route
app.get("/", (req, res) => {
exec("whoami", (error, stdout, stderr) => {
if (error) {
console.log(stdout);
console.error("-------------------------------------------");
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
res.status(200).send("VM created successfully");
}
});
});
// create vm route
app.get("/create-vm/:lab_name", (req, res) => {
//const labname = req.query.labname || req.body.labname || "";
let labname = req.params.lab_name || "";
if (labname === "") {
res.status(400).send("Lab name is required");
return;
}
// search in labsconfig
const labpath = labsconfig.labs[labname].lab_path;
if (!labpath) {
res.status(404).send("Lab not found");
return;
}
// make new directory in running directory from labpath with date name
let vmname = "vm" + Date.now();
let runningDir = "running" + "/" + vmname;
let copyCommand =
"mkdir -p " + runningDir + " && cp -r " + labpath + "/* " + runningDir;
//console.log(copyCommand);
// copy lab files to running directory
exec(copyCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error copying lab files");
} else {
console.log(stdout);
// change name "@@VM_NAME@@" vagrant file with vmname
// change boxname @@VM_BOX@@" vagrant file with boxname
const vagrantfilePath = `./${runningDir}`;
const ReplacevVmCommand = `sed -i 's/@@VM_NAME@@/${vmname}/g' ${vagrantfilePath}/Vagrantfile ; sed -i 's/@@VM_BOX@@/${labsconfig.labs[labname].lab_box}/g' ${vagrantfilePath}/Vagrantfile`;
//console.log(ReplacevVmCommand);
exec(ReplacevVmCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// this test command
// ---------------------------------------------
let data1 = {
vmname: vmname,
labname: labname,
labpath: labpath,
vagrantfilePath: vagrantfilePath,
ip: "192.168.11.165",
hostname: "test",
hosts: "test",
resolv: "test",
};
res.render("runlab", { lab: data1 });
return;
// ---------------------------------------------
const command = `VAGRANT_CWD=${vagrantfilePath} vagrant up`;
console.log(command);
// run vm in RunningVmData array
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// run provision script
const provisionCommand = `VAGRANT_CWD=${vagrantfilePath} vagrant provision`;
exec(provisionCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// get ip of vm
const getIpCommand = `VAGRANT_CWD=${vagrantfilePath} vagrant ssh -c "hostname -I | awk '{print $2}'"`;
exec(getIpCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// get hostname of vm
const ip = stdout.split("\n")[1];
const provisionCommand = `VAGRANT_CWD=${vagrantfilePath} vagrant ssh -c "cat /etc/hostname"`;
exec(provisionCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// get hostes of vm
const hostname = stdout.split("\n")[1];
const provisionCommand = `VAGRANT_CWD=${vagrantfilePath} vagrant ssh -c "cat /etc/hosts"`;
exec(provisionCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
// get resolv of vm
const hosts = stdout.split("\n");
const provisionCommand = `VAGRANT_CWD=${vagrantfilePath} vagrant ssh -c "cat /etc/resolv.conf"`;
exec(
provisionCommand,
(error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VM");
} else {
console.log(stdout);
const resolv = stdout.split("\n");
let hostsData = {
vmname: vmname,
labname: labname,
labpath: labpath,
vagrantfilePath: vagrantfilePath,
ip: ip,
hostname: hostname,
hosts: hosts,
resolv: resolv,
};
RunningVmData.push(hostsData);
res.status(200).send(hostsData);
}
}
);
}
});
}
});
}
});
}
});
}
});
}
});
}
});
});
app.get("/delete-vm/:id", (req, res) => {
const vmname = req.params.id || "";
if (vmname === "") {
res.status(400).send("VM name is required");
return;
}
// search in runningVmData
let vm = RunningVmData.find((vm) => vm.vmname === vmname);
if (!vm) {
res.status(404).send("VM not found");
return;
}
// delete vm from RunningVmData array
RunningVmData = RunningVmData.filter((vm) => vm.vmname !== vmname);
// stop running vm
const vagrantfilePath = vm.vagrantfilePath;
const command = `VAGRANT_CWD=${vagrantfilePath} vagrant halt`;
console.log(command);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error deleting VM");
} else {
// destory this vm
const command = `VAGRANT_CWD=${vagrantfilePath} vagrant destroy`;
console.log(command);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error deleting VM");
} else {
console.log(stdout);
// delete vm from running directory
const deleteCommand = `rm -rf running/${vmname}`;
exec(deleteCommand, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error deleting VM");
} else {
console.log(stdout);
res.status(200).send("VM deleted successfully");
}
});
}
});
}
});
});
app.get("/get-vm/:id", (req, res) => {
const vmname = req.params.id || "";
if (vmname === "") {
res.status(400).send("VM name is required");
return;
}
// search in runningVmData
let vm = RunningVmData.find((vm) => vm.vmname === vmname);
if (!vm) {
res.status(404).send("VM not found");
return;
}
res.status(200).send(vm);
});
app.get("/get-all-running-vm", (req, res) => {
res.status(200).send(RunningVmData);
});
app.get("/get-all-labs", (req, res) => {
res.status(200).send(labsconfig.labs);
});
app.get("/labs", (req, res) => {
let labs = Object.values(labsconfig.labs);
res.render("labs", { view: "labs", labs: labs });
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
app.get("/download/vpnfile", (req, res) => {
let rootpassword = "149";
let client = "client" + Date.now();
// let command = `echo -e "${rootpassword}\n1\n${client}\n1\n" | sudo -S ./scripts/openvpn-install.sh`;
let command = `./scripts/createuser.sh ${client}`;
let filepath = `/home/shebl/${client}.ovpn`;
console.log(filepath);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(error);
res.status(500).send("Error creating VPN");
} else {
// console.log(stdout);
// download vpn file
res.download(filepath);
// res.zip([
// { path: '/path/to/file1.name', name: '/path/in/zip/file1.name' },
// { path: '/path/to/file2.name', name: 'file2.name' }
// ]);
}
});
});