-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
355 lines (290 loc) · 10.4 KB
/
app.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
if (process.argv.length < 3) {
console.log(
'Usage: \n' +
'node websocket-relay.js <secret> [<stream-port> <websocket-port>]'
);
//process.exit();
}
var STREAM_SECRET = process.argv[2] || "DEFAULT",
STREAM_PORT = process.argv[3] || 8081,
WEBSOCKET_PORT = process.argv[4] || 8082,
RECORD_STREAM = false;
// Start Interface WebServer
const express = require('express');
var SocketIOFileUpload = require('socketio-file-upload');
var app = express().use(express.static(__dirname + '/')).use(SocketIOFileUpload.router);
const http = require('http');
const server = http.Server(app);
const socket = require('socket.io')(server);
const WebSocket = require('ws');
const spawnSync = require('child_process').spawnSync;
const spawn = require('child_process').spawn;
const fs = require('fs');
const PORT = 80;
server.listen(PORT, function(){
console.log(`Listening on http://localhost:${PORT}`);
});
// ------------------ Video Start ------------------ //
// Websocket Server
var socketServer = new WebSocket.Server({port: WEBSOCKET_PORT, perMessageDeflate: false});
socketServer.connectionCount = 0;
socketServer.on('connection', function(socket, upgradeReq) {
socketServer.connectionCount++;
console.log(
'New WebSocket Connection: ',
(upgradeReq || socket.upgradeReq).socket.remoteAddress,
(upgradeReq || socket.upgradeReq).headers['user-agent'],
'('+socketServer.connectionCount+' total)'
);
socket.on('close', function(code, message){
socketServer.connectionCount--;
console.log(
'Disconnected WebSocket ('+socketServer.connectionCount+' total)'
);
});
});
socketServer.broadcast = function(data) {
socketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
// HTTP Server to accept incomming MPEG-TS Stream from ffmpeg
var streamServer = http.createServer( function(request, response) {
var params = request.url.substr(1).split('/');
if (params[0] !== STREAM_SECRET) {
console.log(
'Failed Stream Connection: '+ request.socket.remoteAddress + ':' +
request.socket.remotePort + ' - wrong secret.'
);
response.end();
}
response.connection.setTimeout(0);
console.log(
'Stream Connected: ' +
request.socket.remoteAddress + ':' +
request.socket.remotePort
);
request.on('data', function(data){
socketServer.broadcast(data);
if (request.socket.recording) {
request.socket.recording.write(data);
}
});
request.on('end',function(){
console.log('close');
if (request.socket.recording) {
request.socket.recording.close();
}
});
// Record the stream to a local file?
if (RECORD_STREAM) {
var path = 'recordings/' + Date.now() + '.ts';
request.socket.recording = fs.createWriteStream(path);
}
}).listen(STREAM_PORT);
console.log('Listening for incomming MPEG-TS Stream on http://127.0.0.1:'+STREAM_PORT+'/<secret>');
console.log('Awaiting WebSocket connections on ws://127.0.0.1:'+WEBSOCKET_PORT+'/');
// ------------------ Video End ------------------ //
function resetStream(input) {
// Terminate Existing process here first
var ffmpeg = spawn('ffmpeg', ["-f","v4l2",
"-framerate","30",
"-video_size","1920x1080",
"-i", input,
"-f","mpegts",
"-codec:v","mpeg1video",
"-s","1920x1080",
"-b:v","3000k",
// "qscale:v","20",
"-bf","0",
"http://localhost:8081/DEFAULT"]);
ffmpeg.stdout.on('data', function(chunk){
var textChunk = chunk.toString('utf8');
console.log(textChunk);
});
ffmpeg.stderr.on('data', function(chunk){
var textChunk = chunk.toString('utf8');
console.log(textChunk);
});
};
// ------------------ Upload Start ------------------ //
socket.on("connection", function(socket){
// Make an instance of SocketIOFileUpload and listen on this socket:
var uploader = new SocketIOFileUpload();
uploader.dir = "uploads";
uploader.listen(socket);
// Do something when a file is saved:
uploader.on("saved", function(event){
console.log(event.file);
});
// Error handler:
uploader.on("error", function(event){
console.log("Error from uploader", event);
});
});
// ------------------ Upload End ------------------ //
// ------------------ HID Start ------------------ //
var contents = fs.readFileSync(__dirname + "/configuration/gpioConfig.json");
var Relays = JSON.parse(contents).Relay;
var Switchs = JSON.parse(contents).Switch;
var Hubs = JSON.parse(contents).Hub;
var Lircs = JSON.parse(contents).LIRC;
console.log("Relay Pins:", Relays);
console.log("Switch Pins:", Switchs);
console.log("Hub:", Hubs);
console.log("LIRC Pins:", Lircs);
const Gpio = require('onoff').Gpio; // Include onoff to interact with the GPIO
let RELAYS = [];
for (let i = 0; i < Relays.length; i++) {
const relay = new Gpio(Relays[i], 'out');
RELAYS.push({"gpio": Relays[i], "object": relay});
};
// TODO: Throw error if peripherals not detected
var mouse = '/dev/hidg0';
var keyboard = '/dev/hidg1';
function writeReport(device, data) {
fs.writeFile(device, data, (err) => {
if (err) console.log(err);
});
}
var fileTracker = {};
// Make browser connection
socket.on('connection', function(client) {
console.log("Client communication established");
// Receive keyboard data from browser and log in node console
client.on('keyboardChannel', function(data){
console.log(data);
writeReport(keyboard, Buffer.from(data));
});
// Receive mouse data from browser and log in node console
client.on('mouseChannel', function(data){
console.log(data);
writeReport(mouse, Buffer.from(data));
});
client.on('fileChannel', function(data){
console.log(data);
udcPath = '/sys/kernel/config/usb_gadget/kvm-gadget/UDC';
// UDC not recognized by the filesystem as a file -> must use echo (try removing configs also)
disconnect = spawnSync('bash', [__dirname+"/configuration/disconnectUDC.sh"]);
console.log(disconnect);
console.log('UDC Halted');
let confirmWrite = fs.readFileSync(udcPath, 'utf-8');
// console.log(confirmWrite);
// Attach file to libcomposite
if (data.Command === "Attach") {
fs.unlinkSync('/sys/kernel/config/usb_gadget/kvm-gadget/configs/c.1/mass_storage.usb');
numAttachedFiles = Object.keys(fileTracker).length;
lunNum = 'lun.'+numAttachedFiles;
fileTracker[lunNum] = {File: data.File,
CDRom: data.CDRom,
Removable: data.Removable,
ReadOnly: data.ReadOnly,
FUA: data.FUA};
lunPath = '/sys/kernel/config/usb_gadget/kvm-gadget/functions/mass_storage.usb/'+lunNum;
if (!fs.existsSync(lunPath)) {
fs.mkdirSync(lunPath);
}
if (numAttachedFiles > 8) {
socket.emit('fileChannel', "Greater than 8 files attached");
} else {
try {
fs.writeFileSync(lunPath+'/file', __dirname+'/uploads/'+data.File);
fs.writeFileSync(lunPath+'/cdrom', data.CDRom);
fs.writeFileSync(lunPath+'/removable', data.Removable);
//fs.writeFileSync(lunPath+'/ro', data.ReadOnly); // Find out why read-only doesn't work
fs.writeFileSync(lunPath+'/nofua', data.FUA);
console.log('File Attached');
console.log(fileTracker);
socket.emit('fileChannel', fileTracker);
} catch (err) {console.log(err)}
}
}
// Detach file from libcomposite
if (data.Command === "Detach") {
// Will need revision
var key = Object.keys(fileTracker).find(key => fileTracker[key] === data.File);
delete fileTracker[key];
file = '/sys/kernel/config/usb_gadget/kvm-gadget/functions/mass_storage.usb/'+key+'/file';
try {
fs.writeFileSync(file, "");
fs.writeFileSync(lunPath+'/cdrom', "");
fs.writeFileSync(lunPath+'/removable', "");
//fs.writeFileSync(lunPath+'/ro', ""); // Find out why read-only doesn't work
fs.writeFileSync(lunPath+'/nofua', "");
console.log('File Detached');
socket.emit('fileChannel', fileTracker);
} catch (err) {console.log(err)}
}
// Reconnect UDC
if (!fs.existsSync('/sys/kernel/config/usb_gadget/kvm-gadget/configs/c.1/mass_storage.usb')) {
fs.symlinkSync('/sys/kernel/config/usb_gadget/kvm-gadget/functions/mass_storage.usb', '/sys/kernel/config/usb_gadget/kvm-gadget/configs/c.1/mass_storage.usb');
}
let dirContents = fs.readdirSync('/sys/class/udc')
try {
fs.writeFileSync(udcPath, dirContents[0]);
console.log('UDC Reconnected');
} catch (err) {console.log(err)};
});
// Receive stream reset instructions from browser and reset the stream
client.on('streamChannel', function(data){
console.log("Request for stream reset received");
resetStream(data);
});
// Receive source refresh instructions from browser and repopulate menu
client.on('sourceChannel', function(data){
if (data === "RefreshVideo") {
const glob = require('glob');
glob("/dev/video*", function(err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
// Do whatever you want to do with the file
console.log(file);
socket.emit('sourceChannel', file);
});
});
};
});
// Receive request for process PID
client.on('debugChannel', function(data){
console.log("Request for PID received");
socket.emit('debugChannel', process.pid);
});
// Receive request for mac address population
const detect = require('local-devices');
client.on('networkChannel', function(data) {
console.log("Request for MACs received");
detect().then(devices => {
socket.emit('networkChannel', devices);
});
});
// Receive wake on LAN request
client.on('powerChannel', function(data){
if (data.Method === "WOL") {
console.log("Requesting WOL for " + data.MAC);
var macAddress = data.MAC;
var etherwake = spawnSync('etherwake', ['-b', macAddress]);
} else {
console.log("Resetting GPIO Connection " + data.Pin);
for (let j = 0; j < RELAYS.length; j++) {
console.log(j);
console.log(RELAYS[j]);
if (Number(data.Pin) === RELAYS[j]['gpio']) {
if (RELAYS[j]['object'].readSync() === 0) { // Check the pin state, if the state is 0 (or off)
RELAYS[j]['object'].writeSync(1); // Set pin state to 1 (turn LED on)
} else {
RELAYS[j]['object'].writeSync(0); // Set pin state to 0 (turn LED off)
}
};
}
};
});
});
// ------------------ HID End ------------------ //
const openURL = require('opn');
// opens the url in the default browser
console.log("Opening Server URL");
openURL('http://127.0.0.1:3000');