-
Notifications
You must be signed in to change notification settings - Fork 23
/
serve.js
330 lines (298 loc) · 9.18 KB
/
serve.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
319
320
321
322
323
324
325
326
327
328
329
330
const express = require('express');
const { program } = require('commander');
const bodyParser = require('body-parser');
const session = require('express-session');
const MemoryStore = require('memorystore')(session);
const crypto = require('crypto');
const fs = require('fs');
const https = require('https');
const compression = require('compression');
const child_process = require('child_process');
const { marked } = require('marked');
const TOML = require('@iarna/toml');
const scraper = require('./js/scrape');
const dep_mappings = require('./frontend-dependencies');
// -------------------------------------------
// Get Aspine version number without leading 'v'
const version = child_process
.execSync('git describe')
.toString()
.trim()
.match(/^v?(.*)/)[1];
const changelog = marked(
fs.readFileSync(__dirname + '/CHANGELOG.md').toString()
);
const schedule = TOML.parse(
fs.readFileSync(__dirname + '/public/schedule.toml')
);
program
.version(version)
.option('-p, --port <number>', 'port to listen on', 8080)
.option('-P, --port-https <number>', 'port to listen on for HTTPS', 4430);
// Secure by default on production, insecure by default for development
if (process.env.NODE_ENV === 'production') {
program.option(
'-i, --no-secure, --insecure',
'do not secure connections with TLS (HTTPS)'
);
program.option(
'-s, --secure',
'secure connections with TLS (HTTPS) [default for production]'
);
} else {
program.option('-s, --secure', 'secure connections with TLS (HTTPS)');
program.option(
'-d, --dev',
'HTTPS designed to work with the gen-key script to make self signed ' +
'certs'
);
program.option(
'-i, --no-secure, --insecure',
'do not secure connections with TLS (HTTPS) [default for development]'
);
}
program.parse();
const options = program.opts();
// ------------ Web Server -------------------
const app = express();
app.use(compression());
app.listen(options.port, () =>
console.log(`Aspine listening on port ${options.port}!`)
);
if (options.secure || options.dev) {
app.all('*', (req, res, next) => {
if (req.secure) {
return next();
}
// handle port numbers if you need non defaults
res.redirect('https://' + req.hostname + req.url);
}); // at top of routing calls
const credentials = options.dev
? {
key: fs.readFileSync('local.key', 'utf8'),
cert: fs.readFileSync('local.crt', 'utf8'),
ca: fs.readFileSync('local.csr', 'utf8')
}
: {
key: fs.readFileSync('/etc/ssl/certs/private-key.pem', 'utf8'),
cert: fs.readFileSync('/etc/ssl/certs/public-key.pem', 'utf8'),
ca: fs.readFileSync('/etc/ssl/certs/CA-key.pem', 'utf8')
};
https
.createServer(credentials, app)
.listen(options.portHttps, () =>
console.log(`HTTPS Server running on port ${options.portHttps}`)
);
}
// Expose frontend dependencies from node-modules
// https://stackoverflow.com/a/27464258
for (const [endpoint, path] of dep_mappings.files) {
app.get(endpoint, (req, res) => {
res.sendFile(__dirname + path);
});
}
for (const [endpoint, path] of dep_mappings.directories) {
app.use(endpoint, express.static(__dirname + path));
}
// Endpoint to expose version number to client
app.get('/version', (req, res) => res.send(version));
// Endpoint to expose rendered changelog to client
app.get('/updates', (req, res) => res.send(changelog));
// Endpoint to expose specification of schedules to client
app.get('/schedule.json', (req, res) => res.send(schedule));
app.use(function (req, res, next) {
// enable cors
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.get('/home.html', (req, res) => res.redirect('/'));
app.get('/login.html', (req, res) => res.redirect('/login'));
app.use(express.static(__dirname + '/public'));
// Serve any files in public directory
app.use(bodyParser.urlencoded({ extended: true })); // Allows form submission
app.use(bodyParser.json()); // json parser
app.use(
session({
// Sessions expire every 8 hours
cookie: { maxAge: 8 * 60 * 60 * 1000 },
// Check for expired sessions once per hour
store: new MemoryStore({ checkPeriod: 60 * 60 * 1000 }),
resave: false,
saveUninitialized: false,
secret: crypto.randomBytes(64).toString('hex')
// Sessions are destroyed on restarting the server anyway (because we use
// MemoryStore), so the secret can be random
})
);
app.post('/stats', async (req, res) => {
console.log(
`\n\nNEW STATS REQUEST: ${req.body.assignment_id}, ${req.body.class_id}, ${req.body.quarter_id}, ${req.body.year} \n------------------`
);
try {
res.send(
await scraper.get_stats(
req.session.username,
req.session.password,
req.body.assignment_id,
req.body.class_id,
req.body.quarter_id,
req.body.year
)
);
} catch (e) {
console.error(e);
res.send({});
}
});
app.post('/data', async (req, res) => {
// console.log(`\n\nNEW LOGIN: ${req.session.username}\n------------------`);
// fs.appendFile('usage_log.txt', `\n\nNEW LOGIN: ${req.session.username}\n------------------`, function (err) {
// if (err) throw err;
// });
let response;
// Get data from scraper:
if (req.session.nologin) {
res.send({ nologin: true });
} else {
// TODO add nologin field for consistency
try {
res.send(
await scraper.get_student(
req.session.username,
req.session.password,
parseInt(req.body.quarter),
req.body.year
)
);
} catch (e) {
console.error(e);
res.send({ error: e.message });
}
}
});
app.post('/schedule', async (req, res) => {
try {
res.send(
await scraper.get_schedule(
req.session.username,
req.session.password
)
);
} catch (e) {
console.error(e);
res.send({ login_fail: true });
}
});
app.post('/pdf', async (req, res) => {
try {
res.send(
await scraper.get_pdf_files(
req.session.username,
req.session.password
)
);
} catch (e) {
console.error(e);
res.send([]);
}
});
app.get('/', async (req, res) => {
if (req.session.username || req.session.nologin) {
res.sendFile(__dirname + '/public/home.html');
} else {
res.redirect('/login');
}
});
app.get('/login', (req, res) => res.sendFile(__dirname + '/public/login.html'));
app.post('/login', async (req, res) => {
if (req.body.username && req.body.password) {
req.session.username = req.body.username;
req.session.password = req.body.password;
// Keep session for up to 30 days if "remember me" is enabled
if (req.body.rememberme) {
req.session.cookie.maxAge = 30 * 24 * 60 * 60 * 1000;
}
} else {
req.session.nologin = true;
}
res.redirect('/');
});
app.get('/logout', async (req, res) => {
req.session.destroy();
let err;
if ((err = req.query.error)) res.redirect(`/login?error=${err}`);
else res.redirect('/login');
});
app.use((req, res) => {
res.status(404);
res.sendFile(__dirname + '/public/404.html');
});
// app.post('/set-settings', async (req, res) => {
// // TODO: Sanitization
// let key = crypto.createHash('md5').update(req.session.username).digest('hex');
// client.set(`settings:${key}`, JSON.stringify(req.body));
// res.status(200).send("set settings");
// });
//
// app.get('/get-settings', async (req, res) => {
// if(typeof(req.session.username) == "undefined") {
// res.status(400).send("User not logged in");
// return;
// }
// let key = crypto.createHash('md5').update(req.session.username).digest('hex');
// client.get(`settings:${key}`, function (err, reply) {
// if(!reply) {
// //res.send(JSON.stringify({calendars:[]}));
// res.send({calendars:["CRLS", "holidays"]});
// } else {
// res.send(JSON.parse(reply));
// }
// });
// });
//
// app.post('/add-calendar', async (req, res) => {
// console.log(req.body.color);
// // Security: MUST SANITIZE URLS
// if(req.body.id == undefined || !validator.isEmail(req.body.id) ||
// req.body.name == undefined || !req.body.name.match(/^[\-0-9a-zA-Z.'' ]+$/g) ||
// req.body.color == undefined || !req.body.color.match(/^([\-0-9a-fA-F]){6}$/g)) {
// res.status(400).send("Malformed ID, Name, or Color");
// return;
// }
//
// // Check to see if it is public and working
// const calendar = await fetch(`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(req.body.id)}/events?key=AIzaSyDtbQCoHa4lC4kW4g4YXTyb8f5ayJct2Ao&timeMin=2019-02-23T00%3A00%3A00Z&timeMax=2019-04-08T00%3A00%3A00Z&singleEvents=true&maxResults=9999&_=1552838482460`);
// if(!calendar.ok) {
// res.status(400).send("Bad Calendar ID");
// return;
// }
//
// let cal = JSON.stringify({
// name: req.body.name,
// id: req.body.id,
// color: req.body.color
// });
// client.lrem('calendars', 0, cal);
//
// client.rpush('calendars',
// JSON.stringify({
// name: req.body.name,
// id: req.body.id,
// color: req.body.color
// }));
//
// res.status(200).send("added calendar");
// });
//
// app.get('/get-calendars', async (req, res) => {
// client.lrange(`calendars`, 0, -1, function (err, reply) {
// for(i in reply) {
// reply[i] = JSON.parse(reply[i]);
// }
// res.send(reply);
// });
// });