forked from DockerSecurityPlayground/DSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
270 lines (231 loc) · 9.01 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const path = require('path');
const multipart = require('connect-multiparty');
const app = express();
const http = require('http');
const fs = require('fs');
const labels = require('./app/handlers/labels');
const snippets = require('./app/handlers/snippets')
const labs = require('./app/handlers/labs');
const configHandler = require('./app/handlers/config');
const networkHandler = require('./app/handlers/network');
const repoHandler = require('./app/handlers/repos');
const dockerImages = require('./app/handlers/docker-images');
const serviceHandler = require('./app/handlers/services')
const dockerfilesHandler = require('./app/handlers/dockerfiles')
// const installationHandler = require('./app/handlers/installation.js');
const treeRoutes = require('./app/handlers/tree_routes.js');
const Checker = require('./app/util/AppChecker.js');
const multer = require('multer');
const webSocketHandler = require('./app/util/ws_handler.js');
const localConfig = require('./config/local.config.json');
const healthChecker = require('./app/util/HealthLabState.js');
const errorHandler = require('express-error-handler');
const port = +process.env.PORT || 8080;
const host = process.env.HOSTNAME || "127.0.0.1";
// var upload = multer({ dest: "uploads/" });
const storage = multer.memoryStorage();
const upload = multer({ storage });
const server = http.createServer(app);
const AppUtils = require('./app/util/AppUtils.js');
const dockerSocket = require('./app/util/docker_socket');
// const webshellConfigFile =
const log = AppUtils.getLogger();
// Initialize the checker
Checker.init((err) => {
if (err) {
console.error(err.message);
throw err;
}
});
// If is installed start the healtChecker
Checker.isInstalled((isInstalled) => {
if (!localConfig.config.test && isInstalled) {
healthChecker.run((err) => {
if (err) {
log.error('Health checker error!');
log.error(err);
}
});
}
});
app.use(multipart({
uploadDir: localConfig.tmpDir
}));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Parse application/x-www-form-urlencoded & JSON
// app.use(bodyParser.urlencoded({ extended: false }));
// app.use(bodyParser.json());
// Extend the dimension of body
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use('/webshell', express.static(path.join(__dirname, 'public', 'webshell')));
// app.use((err, req, res) => {
// res.status(500);
// res.end(`${err}\n`);
// });
// IT's TOO MUCH INFO FOR OUR APP , ANYWAY FOR FURTHER DEVELOPMENT UNCOMMENT
// app.use(log.requestLogger());
// Installation
// app.post('/dsp_v1/installation/', installationHandler.installation);
app.all('/', (req, res, next) => {
// console.log("SONO QUI");
Checker.isInstalled((installed) => {
if (installed) {
next();
} else if (!res.headersSent) {
res.redirect('/installation.html');
}
});
});
app.use(express.static(`${__dirname}/public`));
app.use((req, res, next) => {
Checker.isInstalled((installed) => {
if (installed) {
next();
} else {
if (!res.headersSent) {
res.redirect('/installation.html');
}
}
});
});
// Api images
app.get('/dsp_v1/docker_images/', networkHandler.getListImages);
app.get('/dsp_v1/dsp_images', dockerImages.getImagesAllRepos);
app.get('/dsp_v1/dsp_images/:reponame', dockerImages.getImagesRepo);
app.get('/dsp_v1/dsp_images/:reponame/:labname', dockerImages.getImagesLab);
app.post('/dsp_v1/dockerfiles/:dockerfile', dockerfilesHandler.createDockerFile);
app.put('/dsp_v1/dockerfiles/:dockerfile', dockerfilesHandler.editDockerFile);
app.get('/dsp_v1/dockerfiles', dockerfilesHandler.getDockerFiles);
app.get('/dsp_v1/dockerfiles/:dockerfile', dockerfilesHandler.getDockerFile);
app.delete('/dsp_v1/dockerfiles/:dockerfile', dockerfilesHandler.deleteDockerFile);
//Api snippets
app.get('/dsp_v1/snippets/', snippets.allSnippets);
//Api Hack Tools
app.get('/dsp_v1/hack_tools/', serviceHandler.getListHackTools);
app.delete('/dsp_v1/hack_tools/:name', serviceHandler.deleteHackTool);
// Api labels
app.get('/dsp_v1/labels/:repo', labels.allLabels);
app.get('/dsp_v1/labels/:repo/:nameLab', labels.labelsOfLab);
// new label in a repository
app.post('/dsp_v1/labels/:repo', labels.addLabel);
// Change label
app.put('/dsp_v1/labels/:repo', labels.updateLabel);
app.delete('/dsp_v1/labels/:repo/:labelname', labels.deleteLabel);
// get all the repositories in main dir
app.get('/dsp_v1/all/', labs.getAll);
app.post('/dsp_v1/all/', labs.importLab);
// Api labs
app.get('/dsp_v1/labs/:repo', labs.getLabs);
app.get('/dsp_v1/labs/:repo/:labname', labs.getInformation);
app.put('/dsp_v1/labs/:labname', labs.saveInformation);
app.post('/dsp_v1/labs/:labname', (req, res) => {
if (req.query.wantToCopy) {
labs.copyLab(req, res);
}
else labs.newLab(req, res);
});
app.delete('/dsp_v1/labs/:labname', labs.deleteLab);
// Api config
app.get('/dsp_v1/config', configHandler.getConfig);
app.post('/dsp_v1/config', configHandler.updateConfig);
// Api docker network
app.get('/dsp_v1/docker_network/:namerepo/:namelab', networkHandler.get);
app.get('/dsp_v1/docker_network/:namelab', networkHandler.getUser);
app.post('/dsp_v1/docker_network/:namelab', networkHandler.save);
// API DOCKER MANAGMENT
app.get('/dsp_v1/services', serviceHandler.getServices);
app.post('/dsp_v1/services/:nameservice', serviceHandler.runService);
app.put('/dsp_v1/services/start/:nameservice', serviceHandler.startService);
app.get('/dsp_v1/services/browser', serviceHandler.isBrowserRun);
app.delete('/dsp_v1/services/browser', serviceHandler.stopBrowser);
app.get('/dsp_v1/services/httpd', serviceHandler.isHttpdRun);
app.delete('/dsp_v1/services/httpd', serviceHandler.stopHttpd);
app.get('/dsp_v1/services/kali', serviceHandler.isKaliRun);
app.delete('/dsp_v1/services/kali', serviceHandler.stopKali);
app.get('/dsp_v1/services/wireshark', serviceHandler.isWiresharkRun);
app.delete('/dsp_v1/services/wireshark', serviceHandler.stopWireshark);
app.get('/dsp_v1/services/tcpdump', serviceHandler.isTcpdumpRun);
app.delete('/dsp_v1/services/tcpdump', serviceHandler.stopTcpdump);
app.put('/dsp_v1/services/stop/:nameservice', serviceHandler.stopService);
app.post('/dsp_v1/services/defaultnetwork/:nameservice', serviceHandler.setAsDefault);
app.delete('/dsp_v1/services/:nameservice', serviceHandler.removeService);
// API GIT REPOS
app.get('/dsp_v1/git-repos', repoHandler.get);
app.delete('/dsp_v1/git-repos/:name', repoHandler.remove);
app.get('/dsp_v1/networkservices/:namerepo/:namelab', serviceHandler.getNetworkList);
app.post('/dsp_v1/networkservices', serviceHandler.attachNetwork);
app.delete('/dsp_v1/networkservices', serviceHandler.detachNetwork);
// Open docker shell
app.post('/dsp_v1/dockershell', networkHandler.dockershell)
// Copy from container to download dir
app.post('/dsp_v1/dockercopy', networkHandler.dockercopy)
app.post('/dsp_v1/dockerupload', networkHandler.dockerupload)
// Check if a dir exists
app.post('/dsp_v1/dir_exists', networkHandler.dirExists);
// Manage repositories
// Only for avatar
app.post('/upload', upload.single('avatar'), (req, res) => {
if (req.file && req.file.buffer) {
const buffer = req.file.buffer;
const wstream = fs.createWriteStream('./public/assets/img/avatar.jpeg');
wstream.write(buffer);
wstream.end();
}
res.redirect('/');
});
// File manager chooser
// Home chooser
app.get('/api/tree/', treeRoutes.treeSearch);
// Lab chooser
app.get('/api/tree/repo', treeRoutes.projectTreeSearch);
app.get('/api/resource', treeRoutes.resourceSearch);
app.post('/api/tree/repo', treeRoutes.uploadFile);
app.delete('/api/tree/repo/', treeRoutes.deleteFile);
//
app.use('/', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`);
});
app.use(log.errorLogger());
// Log the error
app.use((err, req, res, next) => {
log.error(err);
next(err);
});
// Respond to errors and conditionally shut
// down the server. Pass in the server object
// so the error handler can shut it down
// gracefully:
app.use(errorHandler({ server }));
// function fourOhFour(req, res) {
// res.writeHead(404, { 'Content-Type': 'application/json' });
// res.end(`${JSON.stringify(helpers.invalid_resource())}\n`);
// }
/**
* Initialise the server and start listening when we're ready!
*/
// project_init.init( (err, results) => {
// if (err) {
// console.error("** FATAL ERROR ON STARTUP: ");
// console.error(err);
// process.exit(-1);
// }
// });
// error handler
// Initialize web socket handler
webSocketHandler.init(server);
dockerSocket.init(server);
// Set COMPOSE_INTERACTIVE_NO_CLI=1
process.env.COMPOSE_INTERACTIVE_NO_CLI = 1
server.listen(port, host, () => {
if (localConfig.config.test) { log.warn('Testing mode enabled'); }
log.info(`Server listening on ${host}:${port}`);
});