-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
95 lines (73 loc) · 2.66 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
const { NodeMediaServer } = require('node-media-server');
const express = require('express');
const app = express();
const path = require('path');
const request = require('request-promise');
const config = require('./config.json');
const moment = require('moment');
// Generate a random API key if non-given
if(!config.apiAuth.apiKey){
config.apiAuth.apiKey = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
console.log('[IMPORTANT] No API Key given, randomly assigned key (temporary): '+config.apiAuth.apiKey);
} else {
console.log('Using provided API Key for access!')
}
// Start media server
var nms = new NodeMediaServer(config);
nms.run();
// Add static assets and engine for routing/templates
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
// Grab the stream data (users, apps, etc)
async function getApi(){
return request('http://127.0.0.1:' + config.http.port + '/api/streams?apiKey='+config.apiAuth.secret);
}
// Check if the stream key matches from the request
async function checkStreamKey(name, reqkey){
if(reqkey == null){ reqkey = ''; }
let result = await getApi();
let api = await JSON.parse(result);
let keys = await Object.keys(api[name]);
let key = keys[0];
if(reqkey == key){ return true; } else {
return false;
}
}
// Is the stream online?
async function isOnline(name){
let api = await getApi();
let json = JSON.parse(api);
if(json[name]){
return true;
} else {
return false;
}
}
/* Routes */
app.get('/', async (req, res) => {
let host = req.get('host');
let api = await getApi();
let json = await JSON.parse(api);
res.render('home', { api: json, host, moment, port: config.rtmp.port, title: config.isp.title, motto:config.isp.motto });
});
app.get('/:id', async (req, res) => {
let host = req.get('host');
let name = req.params.id;
if(await isOnline(name)){
let check = await checkStreamKey(name, req.query.key);
// Ensure the user has the stream key
if(check){
// Check if the user wants mobile/WSS
if(req.query.method == 'wss'){
res.render('wss', { name, port: config.rtmp.port, host, domain: host.split(':')[0] });
} else {
res.render('rtmp', { name, auth: req.query.key, port: config.rtmp.port, domain: host.split(':')[0]});
}
} else {
res.render('auth', { name, query: req.query.key })
}
} else {
res.render('offline', { name })
}
});
app.listen(config.isp.webport, () => console.log('Express listening on port '+config.isp.webport));