-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathindex.js
132 lines (119 loc) · 3.73 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
/*
* @Author: Eduardo Policarpo
* @contact: +55 43996611437
* @Date: 2021-05-10 18:09:49
* @LastEditTime: 2021-06-07 03:18:01
*/
'use strict';
const cors = require('cors');
const express = require('express');
const app = express();
const path = require('path')
const server = require('http').Server(app);
const serveIndex = require('serve-index');
const motor = require('./engines');
const config = require('./config');
const { yo } = require('yoo-hoo');
const router = motor.engines[process.env.ENGINE].router
require('events').EventEmitter.prototype._maxListeners = 999;
const { startAllSessions } = require('./startup');
const io = require('socket.io')(server, {
cors: {
origins: ["*"],
methods: ["GET", "POST"],
transports: ['websocket', 'polling'],
credentials: true
},
allowEIO3: true
});
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.set('json spaces', 2);
app.use(express.static('public'));
express.static(path.join(__dirname, '/public'));
app.use('/files', express.static('files-received'), serveIndex('files-received', { icons: true }));
app.use((req, res, next) => {
req.io = io;
next();
});
app.use(router);
io.on('connection', sock => {
console.log(`ID: ${sock.id} socket in`)
sock.on('event', data => {
console.log(data)
});
sock.on('disconnect', () => {
console.log(`ID: ${sock.id} socket out`)
});
});
app.get('/start', function (req, res) {
res.render('index', { port: config.port, host: config.host })
});
if (config.https == 1) {
https.createServer(
{
key: fs.readFileSync(config.ssl_key_path),
cert: fs.readFileSync(config.ssl_cert_path)
},
server).listen(config.port, async (error) => {
if (error) {
console.log(error)
}
else {
console.log('\n\nWelcome to')
yo('MyZAP', {
color: 'rainbow',
spacing: 1,
});
console.log(`Http server running on ${config.host}:${config.port}\n\n`);
if (config.start_all_sessions === 'true') {
let result = await startAllSessions()
if (result != undefined) {
console.log(result)
}
}
}
});
} else {
server.listen(config.port, async (error) => {
if (error) {
console.log(error)
}
else {
console.log('\n\nWelcome to')
yo('MyZAP', {
color: 'rainbow',
spacing: 1,
});
console.log(`Http server running on ${config.host}:${config.port}\n\n`);
if (config.start_all_sessions === 'true') {
let result = await startAllSessions()
if (result != undefined) {
console.log(result)
}
}
}
});
}
process.stdin.resume();
async function exitHandler(options, exitCode) {
if (options.cleanup) {
console.log('cleanup');
await Sessions.getSessions().forEach(async session => {
await Sessions.closeSession(session);
});
}
if (exitCode || exitCode === 0) {
console.log(exitCode);
}
if (options.exit) {
process.exit();
}
}
process.on('exit', exitHandler.bind(null, { cleanup: true }));
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }));
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }));
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));