-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
148 lines (119 loc) · 3.24 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
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
'use strict';
// DECLARE APP MODULES AND DEPENDENCIES
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const pg = require('pg').native;
const nmap = require('libnmap');
const os = require('os');
const db = require('./models/');
// SYNC DATABASE ({force: true} to drop tables)
db.sequelize.sync({force: false});
// INSTANTIATE EXPRESS APP
const app = express();
// APP VARIABLES
const PORT = process.env.PORT || 3000;
// SET VIEW ENGINE
app.set('view engine', 'jade');
// SERVE CLIENT-SIDE STATIC CONTENT
app.use(express.static('public'));
// EXPRESS MIDDLEWARE
app.use(bodyParser.json());
// ROUTES
app.get('/', (req, res) => {
res.status(200).send('index.html');
});
// DISCOVER NETWORK NEIGHBORS
app.get('/scan/discover', (req, res) => {
nmap.discover( (err, report) => {
if (err) throw new Error(err);
let hostIPs = [];
for ( const ipRange in report) {
if (report[ipRange].hasOwnProperty('host')) {
report[ipRange].host.forEach( (host) => {
hostIPs.push(host.address[0].item.addr);
});
};
}
res.status(200).send(hostIPs);
});
});
// GET: HOST SCAN
app.get('/scan/host/:hostToScan', (req, res) => {
const opts = {
range: [ req.params.hostToScan ]
};
nmap.scan(opts, function(err, report) {
if (err) throw new Error(err);
// DEBUGGING - REMOVE FOR PRODUCTION
// fs.writeFile('./db/hostScanRes.txt', JSON.stringify(report));
res.status(200).send(report);
});
});
// POST: SAVE SCAN TO DB
app.post('/scan/save', (req, res) => {
const scanData = (req.body);
// create date/time to name scan
const scanName = new Date();
// save scan to scan table
db.scan.create({
name: scanName
}).then( (savedScan) => {
const savedScanId = savedScan.dataValues.id;
// save hosts to host table, attach scanid
for (const host in scanData) {
db.host.create({
scanId: savedScanId,
ipAddress: host,
scanStatus: scanData[host].scanStatus
}).then( (savedHost) => {
const savedHostId = savedHost.dataValues.id;
// save ports to port table, attach scanid
scanData[host].ports.forEach( (port) => {
db.openPort.create({
portNumber: port.portid,
protocol: port.protocol,
service: port.serviceName,
hostId: savedHostId
}).then( (savedPort) => {
});
});
});
}
res.status(200).send(`Saved scan ${savedScanId}`);
});
});
// GET SCANS FROM DATABASE
app.get('/scans/load', (req, res) => {
db.scan.findAll({
include: [
{
model: db.host,
scanId: db.Sequelize.col('scan.id'),
include: [
{
model: db.openPort,
hostId: db.Sequelize.col('host.id')
}
]
},
]
})
.then( (scans) => {
res.status(200).send(scans);
});
});
// Delete scan from DB
app.delete('/scan/delete', (req, res) => {
const scanToDelete = req.body.scanNameToDelete;
db.scan.destroy({
where: {name: scanToDelete}
})
.then( (deletedScan) => {
res.status(200).send(`DELETED ${deletedScan}`);
});
});
// START SERVER LISTENING
app.listen(PORT, () => {
console.log(`Server listening on PORT: ${PORT}`);
});