forked from d0nd3r3k/PiR.tv
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
executable file
·313 lines (271 loc) · 8.67 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
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, fs = require('fs')
, path = require('path')
, spawn = require('child_process').spawn
, omx = require('omxcontrol')
, misc = require('misc')
, pandora = require('pandora');
// Read private data
var passwords = require('./private')
// MAGIC HAPPENS HERE!
var opts
if (passwords.use_ssl == true) {
opts = {
// Specify the key file for the server
key: fs.readFileSync('/home/pi/Authentication/server.key'),
// Specify the certificate file
cert: fs.readFileSync('/home/pi/Authentication/server.crt'),
// Specify the Certificate Authority certificate
ca: fs.readFileSync('/home/pi/Authentication/ca.crt'),
// This is where the magic happens in Node. All previous
// steps simply setup SSL (except the CA). By requesting
// the client provide a certificate, we are essentially
// authenticating the user.
requestCert: true,
// If specified as "true", no unauthenticated traffic
// will make it to the route specified.
//rejectUnauthorized: false,
// Passord
passphrase: passwords.ssl_password
};
} else {
console.log('Not authenticating')
opts = {
// Specify the key file for the server
key: fs.readFileSync('/home/pi/Authentication/server.key'),
// Specify the certificate file
cert: fs.readFileSync('/home/pi/Authentication/server.crt'),
// Specify the Certificate Authority certificate
ca: fs.readFileSync('/home/pi/Authentication/ca.crt'),
// This is where the magic happens in Node. All previous
// steps simply setup SSL (except the CA). By requesting
// the client provide a certificate, we are essentially
// authenticating the user.
requestCert: false,
// If specified as "true", no unauthenticated traffic
// will make it to the route specified.
rejectUnauthorized: false,
// Passord
passphrase: passwords.ssl_password
};
}
var server = require('https').createServer(opts, app)
, io = require('socket.io').listen(server)
// all environments
app.set('port', process.env.TEST_PORT || 9000);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
//app.use(express.static(path.join(__dirname, 'public')));
app.use(omx());
app.use(misc());
app.use(pandora());
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//Attempt to allow access control origin
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
//app.get('/public/*', function(req, res, next) {
// if (req.client.authorized) {
// next(); // call the next handler, which in this case is express.static
// } else {
// return res.status(403).send({
// success: false,
// message: 'Access Unauthorized.'
// });
// }
//});
//Routes
app.get('/', function (req, res) {
if (req.client.authorized || (passwords.use_ssl == false)) {
res.sendfile(__dirname + '/public/index.html');
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
console.log('Passwords false')
}
});
app.get('/remote', function (req, res) {
if (req.client.authorized || passwords.use_ssl == false) {
res.sendfile(__dirname + '/public/main.html');
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
}
});
app.get('/main', function (req, res) {
if (req.client.authorized || passwords.use_ssl == false) {
res.sendfile(__dirname + '/public/main.html');
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
}
});
app.get('/play/:video_id', function (req, res) {
});
//Catch alls here
app.get('/:dir/:dir2/:file', function(req, res) {
//console.log('req ', req);
if (req.client.authorized || passwords.use_ssl == false) {
res.sendfile(__dirname + '/public/' + req.params.dir + '/' + req.params.dir2 + '/' + req.params.file);
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
}
});
//Catch alls here
app.get('/:dir/:file', function(req, res) {
//console.log('req ', req);
if (req.client.authorized || passwords.use_ssl == false) {
res.sendfile(__dirname + '/public/' + req.params.dir + '/' + req.params.file);
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
}
});
app.get('/:file', function(req, res) {
//console.log('req ', req);
if (req.client.authorized || passwords.use_ssl == false) {
res.sendfile(__dirname + '/public/' + req.params.file);
} else {
return res.status(403).send({
success: false,
message: 'Access Unauthorized.'
});
}
});
//Socket.io Config
io.set('log level', 1);
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var ss;
var message;
var query;
//Run and pipe shell script output
function run_shell(cmd, args, cb, end) {
var spawn = require('child_process').spawn,
child = spawn(cmd, args),
me = this;
child.stdout.on('data', function (buffer) { cb(me, buffer); });
child.stdout.on('end', end);
}
//Socket.io Server
io.sockets.on('connection', function (socket) {
socket.on("screen", function(data){
socket.type = "screen";
ss = socket;
console.log("Screen ready...");
});
socket.on("remote", function(data){
socket.type = "remote";
console.log("Remote ready...");
});
socket.on("controll", function(data){
console.log(data);
if(socket.type === "remote"){
if(data.action === "tap"){
if(ss != undefined){
ss.emit("controlling", {action:"enter"});
}
}
else if(data.action === "swipeLeft"){
if(ss != undefined){
ss.emit("controlling", {action:"goLeft"});
}
}
else if(data.action === "swipeRight"){
if(ss != undefined){
ss.emit("controlling", {action:"goRight"});
}
}
}
});
socket.on("video", function(data){
if( data.action === "play"){
var id = data.video_id;
var url = "https://www.youtube.com/watch?v="+id;
console.log("Command is youtube " + url);
var runShell = new run_shell('youtube',[url],
function (me, buffer) {
me.stdout += buffer.toString();
console.log(me.stdout);
console.log(me.stderr);
socket.emit("loading",{output: me.stdout});
},
function () {
//child = spawn('omxplayer',[id+'.mp4']);
//omx.start(id+'.mp4');
});
} else if ( data.action == "local"){
var msg = unescape(data.video_id);
if(msg == "Random") {
console.log("command is playvideo -r -m " + query);
var queryArray = query.split(' ');
var runShell = new run_shell('playvideo',["-r","-m"].concat(queryArray),
function (me, buffer) {
me.stdout += buffer.toString();
socket.emit("loading",{output: me.stdout});
console.log(me.stdout);
},
function () {
});
} else {
console.log("command is playdirect " + msg);
var runShell = new run_shell('playdirect',[msg],
function (me, buffer) {
me.stdout += buffer.toString();
socket.emit("loading",{output: me.stdout});
console.log(me.stdout);
},
function () {
});
}
} else if ( data.action == "getvideos"){
query = data.query;
console.log("Command is listvideo " + query);
var runShell = new run_shell('listvideo',[query],
function (me, buffer) {
//me.stdout += buffer.toString();
//console.log(me.stdout);
message += buffer.toString();
},
function () {
//child = spawn('omxplayer',[id+'.mp4']);
//omx.start(id+'.mp4');
console.log(message);
socket.emit("loading",{output: message});
message = "";
});
}
});
});