1
+ var express = require ( 'express' ) ; // call express
2
+ var app = express ( ) ; // define our app using express
3
+ var fs = require ( 'fs' ) ;
4
+ var model = require ( './model.js' ) ;
5
+ var path = require ( 'path' ) ;
6
+ var mime = require ( 'mime' ) ;
7
+
8
+ var port = process . env . PORT || 8080 ; // set our port
9
+
10
+ var router = express . Router ( ) ; // get an instance of the express Router
11
+
12
+ // router.get('/', function(req, res) {
13
+ // res.send("test");
14
+ // });
15
+
16
+ function parseParameters ( req , callback ) {
17
+ options = { }
18
+ if ( req . query . author != undefined ) {
19
+ options [ 'author' ] = req . query . author ;
20
+ }
21
+ if ( req . query . platform != undefined ) {
22
+ options [ 'platform' ] = req . query . platform ;
23
+ }
24
+ if ( req . query . port != undefined ) {
25
+ options [ 'port' ] = req . query . port ;
26
+ }
27
+ if ( req . query . description != undefined ) {
28
+ options [ 'description' ] = req . query . description ;
29
+ }
30
+ if ( req . query . type != undefined ) {
31
+ options [ 'type' ] = req . query . type ;
32
+ }
33
+ if ( req . query . before != undefined ) {
34
+ options [ 'before' ] = req . query . before ;
35
+ }
36
+ if ( req . query . after != undefined ) {
37
+ options [ 'after' ] = req . query . after ;
38
+ }
39
+ callback ( options ) ;
40
+ }
41
+
42
+ // retrieves all exploit
43
+ router . get ( '/exploits' , function ( req , res ) {
44
+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
45
+
46
+ parseParameters ( req , function ( options ) {
47
+ model . getExploits ( undefined , options , function ( results ) {
48
+ res . status ( ( results . length > 0 ) ? 200 : 404 ) . end ( JSON . stringify ( results ) ) ;
49
+ } ) ;
50
+ } ) ;
51
+ } ) ;
52
+
53
+ // retrieves a specific exploit
54
+ router . get ( '/exploits/:id' , function ( req , res ) {
55
+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
56
+ model . getExploits ( req . params . id , undefined , function ( results ) {
57
+ res . status ( ( results . length > 0 ) ? 200 : 404 ) . end ( JSON . stringify ( results ) ) ;
58
+ } ) ;
59
+ } ) ;
60
+
61
+ // retrieves the attachment of an exploit
62
+ router . get ( '/exploits/:id/attachment' , function ( req , res ) {
63
+ model . getExploits ( req . params . id , undefined , function ( results ) {
64
+ if ( results [ 0 ] && results [ 0 ] [ 'file' ] ) {
65
+ var file = __dirname + '/exploitdb/' + results [ 0 ] [ 'file' ] ;
66
+
67
+ var filename = path . basename ( file ) ;
68
+ var mimetype = mime . lookup ( file ) ;
69
+
70
+ res . setHeader ( 'Content-disposition' , 'attachment; filename=' + filename ) ;
71
+ res . setHeader ( 'Content-type' , mimetype ) ;
72
+
73
+ var filestream = fs . createReadStream ( file ) ;
74
+ filestream . pipe ( res ) ;
75
+ } else {
76
+ res . status ( 404 ) . end ( ) ;
77
+ }
78
+ } ) ;
79
+ } ) ;
80
+
81
+ // manages 404
82
+ router . get ( '*' , function ( req , res ) {
83
+ res . redirect ( '/exploits' ) ;
84
+ } ) ;
85
+
86
+ app . use ( '/' , router ) ;
87
+ app . listen ( port ) ;
88
+ console . log ( "Open browser at: http://127.0.0.1:" + port ) ;
0 commit comments