-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
193 lines (150 loc) · 4.98 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
const http = require('http');
const crypto = require('crypto');
const {spawn} = require('child_process');
const express = require('express');
const bodyParser = require('body-parser');
const socketIo = require('socket.io');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.post(
'/deployhook',
bodyParser.text({type: 'application/json'}),
(request, response) => {
console.log('Received deploy hook');
const expectedHmac = crypto
.createHmac('sha1', process.env.DEPLOY_HOOK_SECRET)
.update(request.body)
.digest('hex');
const signature = request.get('X-Hub-Signature');
const match = /^sha1=(.+)$/.exec(signature);
if (match) {
const hmac = match[1];
if (hmac !== expectedHmac) {
console.log('Unauthorized');
response.status(403).end();
} else {
if (process.env.ENVIRONMENT === 'production') {
console.log('Updating in the background');
const child = spawn('./update.sh');
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
console.log(chunk);
});
child.stderr.on('data', (chunk) => {
console.error(chunk);
});
child.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
} else {
console.log('Not doing anything');
}
response.status(204).end();
}
} else {
console.log('HMAC not found');
response.status(400).end();
}
},
);
app.post(
'/ifttthook',
bodyParser.json({type: 'application/json'}),
(request, response) => {
console.log('Received IFTTT hook');
const authorization = request.get('Authorization');
const {activateScene} = request.body;
if (authorization !== `Basic ${process.env.IFTTT_HOOK_SECRET}`) {
console.log('Unauthorized');
response.status(401).end();
return;
}
if (activateScene) {
console.log('Activating scene', activateScene);
io.emit('activate', activateScene);
}
response.status(204).end();
},
);
app.get('/as.html', (_req, res) => {
res.redirect('/obegransad.html');
});
app.use(express.static('public'));
app.use('/modules', express.static('node_modules'));
const ui = {socket: null};
let timeSyncInterval = null;
const frekvens = {
latency: 0,
syncDelta: 0,
};
const obegransad = {
latency: 0,
syncDelta: 0,
};
io.on('connection', (socket) => {
socket.on('sync', (syncInfo) => {
syncInfo.server = Date.now();
syncInfo.frekvens = frekvens;
syncInfo.obegransad = obegransad;
socket.emit('syncResponse', syncInfo);
});
socket.on('identify', (secret) => {
if (secret === process.env.FREKVENS_CLIENT_SECRET) {
console.log('FREKVENS authorized');
timeSyncInterval = setInterval(() => {
socket.emit('sync', {client: Date.now()});
}, 1000);
socket.on('syncResponse', (syncInfo) => {
const now = Date.now();
frekvens.latency = (now - syncInfo.client) / 2;
frekvens.syncDelta = syncInfo.server - now + frekvens.latency;
});
socket.on('disconnect', () => clearInterval(timeSyncInterval));
} else if (secret === process.env.OBEGRANSAD_CLIENT_SECRET) {
console.log('OBEGRÄNSAD authorized');
timeSyncInterval = setInterval(() => {
socket.emit('sync', {client: Date.now()});
}, 1000);
socket.on('syncResponse', (syncInfo) => {
console.log('OBEGRÄNSAD sync info', syncInfo);
const now = Date.now();
obegransad.latency = (now - syncInfo.client) / 2;
obegransad.syncDelta = syncInfo.server - now + obegransad.latency;
});
socket.on('disconnect', () => clearInterval(timeSyncInterval));
} else if (secret === process.env.UI_CLIENT_SECRET) {
console.log('UI authorized');
ui.socket = socket;
socket.emit('drive');
socket.on('script', (script) => {
socket.broadcast.emit('script', script);
});
socket.on('binaryBegin', (chunkCount) => {
socket.broadcast.emit('binaryBegin', chunkCount);
});
socket.on('binaryChunk', (chunk) => {
socket.broadcast.emit('binaryChunk', chunk);
});
socket.on('binaryEnd', () => {
socket.broadcast.emit('binaryEnd');
});
socket.on('yellowDown', () => socket.broadcast.emit('yellowDown'));
socket.on('yellowUp', () => socket.broadcast.emit('yellowUp'));
socket.on('redDown', () => socket.broadcast.emit('redDown'));
socket.on('redUp', () => socket.broadcast.emit('redUp'));
socket.on('midi', (message) => socket.broadcast.emit('midi', message));
socket.on('disconnect', () => {
ui.socket = null;
});
} else {
console.log('Unauthorized');
}
});
});
server.listen(process.env.PORT, () => {
console.log('Listening on', process.env.PORT);
});