This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·103 lines (83 loc) · 3 KB
/
app.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
module.exports = function() {
var express = require('express'),
fs = require('fs'),
addNotify = require("./njs/addNotify"),
removeNotify = require('./njs/removeNotify'),
setops = require('setops'),
logIt = require('./njs/logIt'),
scheduler = require('node-schedule'),
exec = require('child_process').exec,
logme = require('logme'),
config = require('./config.json');
app = express();
STATIC_DIR = __dirname+'/public';
var options = {
key: fs.readFileSync(config.privateKey),
cert: fs.readFileSync(config.certificate)
};
var userList = [];
app.configure(function(){
app.use(express.static(STATIC_DIR));
app.use(express.bodyParser());
});
app.post('/addNotify', function(req, res){
newRequest('[addNotify]', req);
addNotify([req.body.email, req.body.txt], function(err) {
if(err) return res.send("failure");
var iconValue = 0;
var user = [req.body.username];
if (user) {
userList = setops(user).union(userList);
console.log(new Date()+" [sid:"+sid+"] [addNotify] [userList] current list: " + userList);
}
res.send("success");
});
});
app.post('/removeNotify', function(req, res){
newRequest('[removeNotify]', req);
removeNotify([req.body.email, req.body.txt], function(err) {
if(err) return res.send("failure");
userList = userList.filter(function(val) { return val != req.body.username; });
logIt("[removeNotify] [userList] current list: " + userList);
res.send("success");
});
});
app.post('/getUsers', function(req, res){
res.send(userList);
});
function newRequest(type, req) {
sid = Math.random().toString(36).substring(7);
console.log(new Date()+" [sid:"+sid+"]", type, "New request from", req.ip+":", req.body.username, "-", req.body.email+",", req.body.txt);
};
process.on( 'SIGINT', function() {
console.log(new Date()+" Express server shutting down.");
process.exit()
});
// Listen on http:80
if (config.listenHttp) {
http = express();
http.listen(config.httpPort, function() {
logme.info('qNotify is listening via http on ' + this.address().address + ':' + this.address().port + ' and redirecting to port ' + config.httpsPort);
});
// Redirect from http:80 --> https:443
http.get('*',function(req,res){
res.redirect('https://' + req.headers.host + req.url)
});
}
// Listen on https:443
serverhttps = require('https').createServer(options, app);
serverhttps.listen(config.httpsPort, function() {
logme.info('qNotify is listening securely on ' + this.address().address + ':' + this.address().port);
});
scheduler.scheduleJob({hour: 21, minute: 0}, function(){
exec('qmon -S EmailAddress=null', function(err, stdout, stderr) {
if (err) {
console.log(new Date()+' [scheduler] [reset] exec error: ' + err);
}
else {
userList = [];
console.log(new Date()+' [scheduler] [reset] Cleared userList.');
}
});
});
};