-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.js
73 lines (55 loc) · 1.99 KB
/
main.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
'use strict';
const g_constants = require('./constants');
const https = require('https');
const util = require('util');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs')
const log_file = fs.openSync(__dirname + '/debug.log', 'w');
const log_stdout = process.stdout;
console.log = function(d, userID) {
if (!g_constants.DEBUG_LOG)
return;
fs.writeSync(log_file, (new Date()).toUTCString()+" "+util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
if (userID)
require("./utils").log_user(userID, d);
};
const app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.static('./static_pages'));
var httpsServer = https.createServer(g_constants.SSL_options, app);
var httpsListener = httpsServer.listen(g_constants.share.my_port, () => {
console.log("Proxy listening on port "+g_constants.share.my_port);
});
var lastSocketKey = 0;
var socketMap = {https: {}};
httpsListener.on('connection', socket => {
socket.setTimeout(1000 * 60 * 10); // 10 min
/* generate a new, unique socket-key */
const socketKey = ++lastSocketKey;
/* add socket when it is connected */
socketMap.https[socketKey] = socket;
socket.on('close', function() {
/* remove socket when it is closed */
//g_constants.ReleaseAddress(socketMap.https[socketKey].remoteAddress);
delete socketMap.https[socketKey];
});
if (!g_constants.IsAllowedAddress(socket.remoteAddress))
socket.end();
});
process.on('uncaughtException', err => {
console.error(err.stack);
console.log("Node NOT Exiting...");
require("fs").writeFileSync(__dirname + '/debug'+Date.now()+'.log', err.stack);
process.exit(0);
});
app.use((err, req, res, next) => {
res.send(500, 'Something broke!');
});
require("./database").Init(() => {
require('./reqHandler.js').handle(app);
});