-
Notifications
You must be signed in to change notification settings - Fork 2
/
stuhl.js
123 lines (100 loc) · 3.32 KB
/
stuhl.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
#!/usr/bin/env node
const
bodyParser = require('body-parser'),
express = require('express'),
fs = require('fs'),
https = require('https');
let configName = process.argv.length > 2 ? process.argv[2] : 'config.json';
if (!fs.existsSync(configName)) {
console.error('Configuration file %s not found', configName);
process.exit(1);
}
if (!configName.match(/^[./]/)) configName = './' + configName;
const
opts = require(configName),
plugins = [],
channels = {};
opts.plugins.forEach(pluginOpts => {
const pluginClass = require('./plugins/' + pluginOpts.plugin);
const plugin = new pluginClass(pluginOpts);
plugin.channels.forEach(channel => {
if (channels[channel.name]) {
console.error('Configuration error: Duplicate channel %s', channel.name);
process.exit(1);
}
channels[channel.name] = channel;
});
plugins.push(plugin);
});
Object.keys(opts.destinations).forEach(destination => {
opts.destinations[destination].forEach(destinationChannel => {
if (!channels[destinationChannel]) {
console.error('Configuration error: Destination %s references invalid channel %s',
destination, destinationChannel);
process.exit(1);
}
});
});
console.log('[sTUHL] Loaded %d destinations and %d channels',
Object.keys(opts.destinations).length, Object.keys(channels).length);
plugins.forEach(plugin => plugin.start());
const app = express();
app.use(bodyParser.json());
if (opts.frontendEnabled) {
app.use(express.static('frontend'));
app.get('/destinations', (req, res) => res.json(Object.keys(opts.destinations)));
} else {
app.get('/', (req, res) => res.send('Das ist ja sTUHL!'));
}
app.get('/stuhl', (req, res) => res.status(405).json({
success: false,
error: 'Use POST'
}));
app.post('/stuhl', (req, res) => {
const key = req.body.key;
if (key !== opts.key) {
res.status(403).json({
success: false,
error: 'Invalid key'
});
return;
}
const message = req.body.message;
if (!message) {
res.status(400).json({
success: false,
error: 'Missing message'
});
return;
}
const destination = req.body.destination;
if (!opts.destinations[destination]) {
res.status(400).json({
success: false,
error: 'Invalid destination'
});
return;
}
const ttl = req.body.ttl;
if (typeof ttl !== 'undefined' && !Number.isInteger(ttl)) {
res.status(400).json({
success: false,
error: 'Invalid TTL'
});
return;
}
opts.destinations[destination].forEach(destinationChannel =>
channels[destinationChannel].broadcast(message, req.body.title, req.body.link, req.body.level, req.body.ttl));
console.log('[sTUHL] Message broadcast to destination %s: %s', destination, message);
res.json({
success: true
});
});
const listener = opts.tls ? https.createServer({
key: fs.readFileSync(opts.tls.key),
cert: fs.readFileSync(opts.tls.cert)
}, app) : app;
const server = listener.listen(3000, '::', () => {
const address = server.address();
console.log('[sTUHL] Der sTUHL läuft! %s://%s:%s', opts.tls ? 'https' : 'http', address.address, address.port);
});