-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
88 lines (75 loc) · 2.7 KB
/
server.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
var express = require('express'); // call express
var app = express(); // define our app using express
var fs = require('fs');
var model = require('./model.js');
var path = require('path');
var mime = require('mime');
var port = process.env.PORT || 8080; // set our port
var router = express.Router(); // get an instance of the express Router
// router.get('/', function(req, res) {
// res.send("test");
// });
function parseParameters(req, callback) {
options = {}
if (req.query.author != undefined) {
options['author'] = req.query.author;
}
if (req.query.platform != undefined) {
options['platform'] = req.query.platform;
}
if (req.query.port != undefined) {
options['port'] = req.query.port;
}
if (req.query.description != undefined) {
options['description'] = req.query.description;
}
if (req.query.type != undefined) {
options['type'] = req.query.type;
}
if (req.query.before != undefined) {
options['before'] = req.query.before;
}
if (req.query.after != undefined) {
options['after'] = req.query.after;
}
callback(options);
}
// retrieves all exploit
router.get('/exploits', function (req, res) {
res.setHeader('Content-Type', 'application/json');
parseParameters(req, function (options) {
model.getExploits(undefined, options, function (results){
res.status((results.length > 0) ? 200 : 404).end(JSON.stringify(results));
});
});
});
// retrieves a specific exploit
router.get('/exploits/:id', function (req, res) {
res.setHeader('Content-Type', 'application/json');
model.getExploits(req.params.id, undefined, function (results){
res.status((results.length > 0) ? 200 : 404).end(JSON.stringify(results));
});
});
// retrieves the attachment of an exploit
router.get('/exploits/:id/attachment', function (req, res) {
model.getExploits(req.params.id, undefined, function (results){
if (results[0] && results[0]['file']) {
var file = __dirname + '/exploitdb/' + results[0]['file'];
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
} else {
res.status(404).end();
}
});
});
// manages 404
router.get('*', function(req, res){
res.redirect('/exploits');
});
app.use('/', router);
app.listen(port);
console.log("Open browser at: http://127.0.0.1:" + port);