This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
talks-ctrl.js
151 lines (127 loc) · 3.69 KB
/
talks-ctrl.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
var Sequelize = require('sequelize');
var inSubnet = require('insubnet');
var rangeCheck = require('range_check');
var { createLogger, format, transports } = require('winston');
var { combine, timestamp, label, printf } = format;
var meetingPassword = require('./meeting-password.json')
var commandLineArgs = process.argv.slice(2);
var myFormat = printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`;
})
var logger = createLogger({
level: 'info',
transports: [
new transports.Console(),
new transports.File({ filename: './public/log/talks.log' })
],
format: combine(
timestamp(),
myFormat
)
});
module.exports.getTalks = (req, res) => {
let sequelize = connectToServer();
let talksModel = sequelize.import(__dirname + "/talks-model.js");
loadTalks(talksModel, res);
};
module.exports.getVisibleTalks = (req, res) => {
let sequelize = connectToServer();
let talksModel = sequelize.import(__dirname + "/talks-model.js");
loadVisibleTalks(talksModel, res);
}
module.exports.createTalk = (req, res) => {
if(!allowed(req)) {
res.sendStatus(500);
return;
}
let sequelize = connectToServer();
let talksModel = sequelize.import(__dirname + "/talks-model.js");
// write the new talk to the server
return talksModel.sync().then(() => {
const { name, type, desc } = req.body;
// create an instance of the model and save to the db
talksModel.create({name, type, desc}, {
fields: ['id', 'name', 'type', 'desc']
}).then(data => {
logger.log({
level: 'info',
message: `[CREATE] ${name} created a ${type} with the description: ${desc}`
});
// reload the talks
loadTalks(talksModel, res);
});
});
};
module.exports.updateTalk = (req, res) => {
if(!allowed(req)) {
res.sendStatus(500);
return;
}
let sequelize = connectToServer();
let talksModel = sequelize.import(__dirname + "/talks-model.js");
return talksModel.sync().then(() => {
const { hiddenStatus, talkId } = req.body;
return talksModel.update(
{ hidden: hiddenStatus },
{ where: { id: talkId }}
);
}).then(() => {
return talksModel.findAll({
attributes: ['id', 'name', 'type', 'desc', 'hidden'],
where: { id: req.body.talkId },
limit: 1
}).then(data => {
const talk = data[0].dataValues;
logger.log({
level: 'info',
message: `[UPDATE] The ${talk.type} "${talk.desc}" by ${talk.name} was ${talk.hidden ? 'hidden': 'unhidden'}`
});
loadTalks(talksModel, res);
})
});
};
// Send a SELECT query to the database and return the response as JSON
function loadTalks(model, res) {
return model.findAll({
attributes: ['id', 'name', 'type', 'desc', 'hidden']
}).then(result => {
return res.json(result);
});
}
function loadVisibleTalks(model, res) {
return model.findAll({
attributes: ['id', 'name', 'type', 'desc', 'hidden'],
where: {hidden: false}
}).then(result => {
return res.json(result);
});
}
function connectToServer() {
return new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: './talks.db'
});
}
function allowedIP(ip) {
// Overrides while in development mode
if (commandLineArgs.includes('-d') || commandLineArgs.includes('--dev')) {
return true;
}
let inv4 = rangeCheck.inRange(rangeCheck.displayIP(ip), ['128.153.0.0/16']);
let inv6 = rangeCheck.inRange(rangeCheck.displayIP(ip), "2605:6480::/32");
logger.log({
level: 'info',
message: `[VALIDATE-IP] Validating IP ${ip} : IPv4:${inv4} IPv6:${inv6}`
});
return inv4 || inv6;
}
function validPassword(req) {
if (!meetingPassword || !meetingPassword.password) {
return false;
}
return req.body.password == meetingPassword.password;
}
function allowed(req) {
let ip = req.headers["x-forwarded-for"];
return allowedIP(ip) || validPassword(req);
}