This repository was archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (94 loc) · 3.24 KB
/
Copy pathserver.js
File metadata and controls
108 lines (94 loc) · 3.24 KB
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
const express = require('express');
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
// const debug = require('debug')('shodan:server');
const config = require('config');
const bunyan = require('bunyan');
const knex = require('knex')(config.db);
require('./modules/knex-timings')(knex, false);
const showLogsByErrorId = require('./modules/showLogsByErrorId');
const {showTopErrors} = require('./modules/showTopErrors');
const showHanged = require('./modules/showHanged');
const showSpeed = require('./modules/showSpeed');
const showConditions = require('./modules/showConditions');
const updateMessageComment = require('./modules/updateMessageComment');
const {getIndexes} = require('./modules/getIndexes');
const log = bunyan.createLogger({name: 'shodan:server'});
if (config.ui.auth && config.ui.auth.enabled) {
// eslint-disable-next-line global-require
require('./modules/auth.js')(app, io, knex);
}
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/dist/index.html`);
});
app.use(express.static('dist'));
const cache = {
indexes: null,
};
setInterval(() => {
log.info(`Current users connected: ${Object.keys(io.sockets.connected).length}`);
}, 5000);
function sendClientConfig(socket, indexes) {
const eventData = {
name: 'updateConfig',
data: {
config:
{
indexes,
updater: {
indexes: config.updater.kibana.indexes,
kibana:
{
url: config.updater.kibana.url,
},
},
ui: {
display: config.ui.display,
},
},
},
};
socket.emit('event', eventData);
}
io.on('connection', async (socket) => {
log.info(`A user connected (${socket.request.user && socket.request.user.displayName || 'unknown'})`);
if (!cache.indexes) {
try {
cache.indexes = await getIndexes();
} catch (err) {
log.error('Failed to get indexes data', err);
}
}
sendClientConfig(socket, cache.indexes);
socket.on('event', (event) => {
log.info(`event ${event.name} fired: ${JSON.stringify(event)}`);
if (!event.data.index) {
log.info(`No index for request ${JSON.stringify(event.data)}, setting to default`);
event.data.index = 'twapi-avia-*';
}
if (config.ui.auth && config.ui.auth.enabled && (!socket.request.user || !socket.request.user.logged_in)) {
log.warn('user not authorized!');
socket.emit('event', {name: 'updateTopErrors', data: [], fetchErrors: ['Not authorized']});
return;
}
if (event.name === 'showTopErrors') {
showTopErrors(knex, socket, event);
} else if (event.name === 'updateMessageComment') {
updateMessageComment(knex, socket, event);
} else if (event.name === 'showHanged') {
showHanged(knex, socket, event);
} else if (event.name === 'showSpeed') {
showSpeed(knex, socket, event);
} else if (event.name === 'showConditions') {
showConditions(knex, socket, event);
} else if (event.name === 'showLogsByErrorId') {
showLogsByErrorId(knex, socket, event);
} else {
log.error(`dunno event name ${event.name}`);
}
});
});
http.listen(config.ui.port, () => {
log.info(`listening on *:${config.ui.port}`);
});