-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
318 lines (267 loc) · 10.7 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Module dependencies.
*/
const express = require('express'); // HTTP server
const compression = require('compression'); // gzip for the HTTP body
const cors = require('cors'); // CORS
const logger = require('morgan'); // HTTP request logger
const expressStatusMonitor = require('express-status-monitor'); // Monitor of the service (CPU/Mem,....)
const errorHandler = require('errorhandler');
const dotenv = require('dotenv'); // Application configuration
const path = require('path');
const chalk = require('chalk'); // To color message in console log
const passport = require('passport'); // Authentication
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const BasicStrategy = require('passport-http').BasicStrategy;
const jwt = require('jsonwebtoken'); // JWT Authentication
const bodyParser = require('body-parser');
//const crypto = require('crypto'); // To encrypt Notify keys
const MongoClient = require('mongodb').MongoClient;
const processEnv = process.env;
/**
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.config({
path: '.env'
});
const notifyQueue = require('./notifyQueue.js');
const _corsSettings = JSON.parse(processEnv.cors || '{"optionSucessStatus":200}'); // Parse CORS settings
const _baseFolder = process.env.baseFolder || "";
//
// HTTP auth
//
passport.use(new BasicStrategy({ qop: 'auth' },
function( user, pass, cb ) {
if( user !== processEnv.user || pass !== processEnv.password ) {
return cb( null, false );
}
return cb( null, user );
}
));
/**
* Create Express server.
*/
const app = express();
/**
* Connect to MongoDB.
*/
MongoClient.connect( processEnv.MONGODB_URI || '', {useUnifiedTopology: true} ).then( ( mongoInstance ) => {
module.exports.dbConn = mongoInstance.db( processEnv.MONGODB_NAME || 'subs' );
//app.emit('ready');
/**
* Controllers (route handlers).
*/
const subsController = require('./controllers/subscriptions');
const managersController = require('./controllers/managers');
const smtpController = require('./controllers/sendsmtp');
const adminController = require('./controllers/admin');
const mailingController = require('./controllers/mailing_view');
const userController = require('./controllers/user');
/**
* Express configuration.
*/
app.set('host', processEnv.Host || '0.0.0.0');
app.set('port', processEnv.Port || 8080);
app.use(expressStatusMonitor( { path: processEnv.ServerStatusPath || "/admin/sys-status" } ));
app.use(compression());
app.use(logger( processEnv.LOG_FORMAT || 'dev'));
app.use(bodyParser.json()); // for parsing application/json
app.disable('x-powered-by');
/**
* Subscriber routes.
*/
app.get('/api/v0.1/subs/postkey', subsController.getKey);
app.post('/api/v0.1/subs/email/add',
// Need to do more testing
// passport.authenticate('basic', { session: false }),
subsController.addEmail);
//app.post('/api/v0.1/subs/email/confirm', subsController.confirmEmail); // TODO: need to handle data from "post"
//app.post('/api/v0.1/subs/email/remove', subsController.removeEmail); // TODO: need to handle data from "post"
app.get('/subs/confirm/:subscode/:emlParam', subsController.confirmEmail); // Deprecated, to be removed after 60 days of it's deployment date
app.get('/subs/remove/:subscode/:emlParam', subsController.removeEmail); // Deprecated, to be removed after 60 days of it's deployment date
app.get('/subs/confirm/:subscode', subsController.confirmEmail);
app.get('/subs/remove/:subscode', subsController.removeEmail);
app.post('/subs/post',
bodyParser.urlencoded({extended:false, limit: '10kb'}),
subsController.addEmailPOST);
// app.get('/api/v0.1/subs/email/getAll', subsController.getAll); // TODO: kept for later if we create a "subscription" management page.
/**
* Manager routes.
*/
app.get('/api/v0.1/t-manager/:topicId/stats',
passport.authenticate('basic', { session: false }),
managersController.getTopicStats);
app.get('/api/v0.1/t-manager/:accessCode/home',
passport.authenticate('basic', { session: false }),
managersController.serveHome);
app.post('/api/v0.1/t-manager/:accessCode/topic',
passport.authenticate('basic', { session: false }),
bodyParser.urlencoded({extended:false, limit: '10kb'}),
managersController.createTopic);
app.get('/api/v0.1/t-manager/:accessCode/:topicId',
passport.authenticate('basic', { session: false }),
managersController.getTopic);
app.put('/api/v0.1/t-manager/:accessCode/:topicId',
passport.authenticate('basic', { session: false }),
managersController.modifyTopic);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/modSuccess',
passport.authenticate('basic', { session: false }),
managersController.showModSuccess);
app.delete('/api/v0.1/t-manager/:accessCode/:topicId/',
passport.authenticate('basic', { session: false }),
managersController.deleteTopic);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/deleteSuccess',
passport.authenticate('basic', { session: false }),
managersController.showDeleteSuccess);
//app.param('/api/v0.1/t-manager/:code/:topic', managersController.validateCodeTopic);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/list',
passport.authenticate('basic', { session: false }),
managersController.getTopicSubs);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/list-upload',
passport.authenticate('basic', { session: false }),
managersController.uploadTopicSubs);
app.post('/api/v0.1/t-manager/:accessCode/:topicId/list',
passport.authenticate('basic', { session: false }),
bodyParser.urlencoded({extended:true, limit: '250k'}),
managersController.getTopicOver50kSubs);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/bulk/form',
passport.authenticate('basic', { session: false }),
managersController.serveBulkForm);
app.post('/api/v0.1/t-manager/:accessCode/:topicId/bulk/action',
passport.authenticate('basic', { session: false }),
bodyParser.urlencoded({extended:true, limit: '50mb'}),
managersController.actionBulk);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/email/add/test',
passport.authenticate('basic', { session: false }),
subsController.testAdd);
app.get('/api/v0.1/t-manager/:topicId/:prefix/:suffix/test/addJSON',
subsController.simulateAddJSON);
app.get('/api/v0.1/t-manager/:topicId/:prefix/:suffix/test/addPost',
subsController.simulateAddPost);
app.get('/api/v0.1/t-manager/:topicId/test/sendsmtpPOST',
smtpController.testsendMailPOST);
app.post('/api/v0.1/t-manager/:topicId/confirmedSubscribers',
verifyJWT,
managersController.getAllConfSubs);
/**
* Admin routes.
*/
// app.get('/subs/remove_unconfirm/:subscode/:email', subsController.removeUnconfirmEmail);
app.get('/api/v0.1/t-manager/:accessCode/:topicId/flush-cache',
passport.authenticate('basic', { session: false }),
smtpController.flushCacheSMTP,
subsController.flushCache);
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET || "shhhut",
cookie: {
maxAge: 1209600000
}, // two weeks in milliseconds
store: new MongoStore({
url: process.env.MONGODB_URI,
autoReconnect: true,
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.get( '/api/v1/mailing/login', mailingController.v_mailingLogin );
app.get( '/api/v1/mailing/logout', userController.logout );
app.post( '/api/v1/mailing/login',
bodyParser.urlencoded({extended:true, limit: '1mb'}),
passport.authenticate('local', { successRedirect: _baseFolder + '/api/v1/mailing/manage', failureRedirect: _baseFolder + '/api/v1/mailing/login'} ) );
app.get('/api/v1/mailing/manage',
userController.isAuthenticated,
bodyParser.urlencoded({extended:true, limit: '2mb'}),
mailingController.v_mailingManage);
app.post('/api/v1/mailing/create',
userController.isAuthenticated,
bodyParser.urlencoded({extended:true, limit: '2mb'}),
mailingController.v_mailingCreate);
app.get('/api/v1/mailing/:mailingid/edit',
userController.isAuthenticated,
bodyParser.urlencoded({extended:true, limit: '5mb'}),
mailingController.v_mailingEdit);
app.post('/api/v1/mailing/:mailingid/edit',
userController.isAuthenticated,
bodyParser.urlencoded({extended:true, limit: '5mb'}),
mailingController.v_mailingSave);
app.get('/api/v1/mailing/:mailingid/history',
userController.isAuthenticated,
mailingController.v_mailingHistory);
app.get('/api/v1/mailing/:mailingid/approval', //this is deperecated URL
userController.isAuthenticated,
mailingController.v_mailingApproval);
app.get('/api/v1/mailing/:mailingid/:subscode/approval',
userController.isAuthenticated,
mailingController.v_mailingApproval);
app.get('/api/v1/mailing/:mailingid/approved',
userController.isAuthenticated,
mailingController.v_mailingApproved);
app.get('/api/v1/mailing/:mailingid/cancel',
userController.isAuthenticated,
mailingController.v_mailingCancelled);
app.get('/api/v1/mailing/:mailingid/sendToSubs',
userController.isAuthenticated,
mailingController.v_mailingSendToSub);
app.get('/api/v1/mailing/stats',
userController.isAuthenticated,
mailingController.v_mailingGetTopicStats);
/**
* SMTP Mail routes.
*/
app.get('/api/v0.1/eml/postkey', smtpController.getKey);
app.post('/eml/send',
cors(_corsSettings),
bodyParser.urlencoded({extended:false, limit: '10kb'}),
smtpController.sendMailPOST);
/**
* Bull routes
*/
app.use( "/admin/queues",
passport.authenticate( "basic", { session: false } ),
notifyQueue.UI( _baseFolder + "/admin/queues" ) );
/**
* JWT Authentication
*/
function verifyJWT(req, res, next){
var token = req.headers['authorization'].replace(/^bearer\s/i, '');
if (!token)
return res.status(403).send({ auth: false, message: 'No token provided.' });
jwt.verify(token, processEnv.jwtSecretAllSubsForTopic, function(err, decoded){
if (err){
console.log('if(err) ' + err);
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
}
next();
});
}
/**
* Error Handler.
*/
if (processEnv.NODE_ENV === 'development') {
// only use in development
app.use(errorHandler());
} else {
app.use((err, req, res, next) => {
console.log( "app.use: Server Error" );
console.error(err);
res.status(500).send('Server Error');
});
}
/**
* Start Express server.
*/
//app.on('ready', function() {
app.listen(app.get('port'), () => {
console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
console.log(' Press CTRL-C to stop\n');
});
//});
}).catch( (e) => { console.log( "%s MongoDB ERRROR: %s", chalk.red('✗'), e ) } );
process.once('SIGUSR2', function () {
console.log( "+++++++++++++++++++ restart +++++++++++");
process.kill(process.pid, 'SIGUSR2');
});
module.exports = app;