Skip to content

Commit

Permalink
Added "enableAdmin=false". Which allows to enable/disable /admin/ page
Browse files Browse the repository at this point in the history
"enableLogs" is disabled by default.
"autoRebootServerOnFailure" is disabled by default.
getJsonFile now handles error if JSON file is missing.
"dirPath" is set to EMPTY string to force current-directory as default
option. Behind the scene "process.cwd()" is used for empty "dirPath".
  • Loading branch information
muaz-khan committed Nov 4, 2018
1 parent bbdc3bf commit a1a1932
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "rtcmulticonnection-server",
"description": "RTCMultiConnection socket.io server",
"version": "1.2.3",
"version": "1.2.4",
"authors": [
{
"name": "Muaz Khan",
Expand Down
7 changes: 4 additions & 3 deletions config.json
@@ -1,16 +1,17 @@
{
"socketURL": "/",
"dirPath": "./",
"dirPath": "",
"homePage": "/demos/index.html",
"socketMessageEvent": "RTCMultiConnection-Message",
"socketCustomEvent": "RTCMultiConnection-Custom-Message",
"port": "9001",
"enableLogs": "true",
"autoRebootServerOnFailure": "true",
"enableLogs": "false",
"autoRebootServerOnFailure": "false",
"isUseHTTPs": "false",
"sslKey": "./fake-keys/privatekey.pem",
"sslCert": "./fake-keys/certificate.pem",
"sslCabundle": "",
"enableAdmin": "false",
"adminUserName": "username",
"adminPassword": "password"
}
12 changes: 8 additions & 4 deletions node_scripts/Signaling-Server.js
Expand Up @@ -81,13 +81,15 @@ module.exports = exports = function(root, app, socketCallback) {
}

function sendToAdmin(all) {
return; // disabled admin
if(app.config.enableAdmin === false) {
return;
}

try {
if (adminSocket) {
var users = [];
// temporarily disabled
false && Object.keys(listOfUsers).forEach(function(userid) {
app.config.enableAdmin === true && Object.keys(listOfUsers).forEach(function(userid) {
try {
var item = listOfUsers[userid];
if (!item) return; // maybe user just left?
Expand Down Expand Up @@ -128,8 +130,10 @@ module.exports = exports = function(root, app, socketCallback) {
}

function handleAdminSocket(socket, params) {
socket.disconnect(); //disabled admin
return;
if(app.config.enableAdmin === false) {
socket.disconnect(); //disabled admin
return;
}

if (!app.request || !app.isAdminAuthorized || !app.config || !app.isAdminAuthorized(app.request, app.config)) {
var adminAuthorization = require('basic-auth');
Expand Down
15 changes: 12 additions & 3 deletions node_scripts/get-bash-parameters.js
Expand Up @@ -24,9 +24,9 @@ module.exports = exports = function(config, BASH_COLORS_HELPER) {
}
}

// node server.js --autoRebootServerOnFailure=false
if (val.indexOf('--autoRebootServerOnFailure=false') === 0) {
config.autoRebootServerOnFailure = false;
// node server.js --autoRebootServerOnFailure=true
if (val.indexOf('--autoRebootServerOnFailure=true') === 0) {
config.autoRebootServerOnFailure = true;
}

// node server.js --port=9002
Expand Down Expand Up @@ -56,6 +56,11 @@ module.exports = exports = function(config, BASH_COLORS_HELPER) {
}
}

// node server.js --enableAdmin=true
if (val.indexOf('--enableAdmin=true') === 0) {
config.enableAdmin = true;
}

// node server.js --adminUserName=username
if (val.indexOf('--adminUserName') === 0) {
var inner = val.split('--adminUserName=')[1];
Expand Down Expand Up @@ -157,6 +162,8 @@ module.exports = exports = function(config, BASH_COLORS_HELPER) {
console.log('\tDirectory path that is used for HTML/CSS/JS content delivery.');
console.log(BASH_COLORS_HELPER.getYellowFG(), '--homePage=/demos/Video-Conferencing.html');
console.log('\tOpen a specific demo instead of loading list of demos.');
console.log(BASH_COLORS_HELPER.getYellowFG(), '--enableAdmin=true');
console.log('\tEnable /admin/ page.');
console.log(BASH_COLORS_HELPER.getYellowFG(), '--adminUserName=username');
console.log('\t/admin/ page\'s username.');
console.log(BASH_COLORS_HELPER.getYellowFG(), '--adminPassword=password');
Expand All @@ -166,4 +173,6 @@ module.exports = exports = function(config, BASH_COLORS_HELPER) {
process.exit(1);
}
});

return config;
};
9 changes: 7 additions & 2 deletions node_scripts/get-values-from-config-json.js
Expand Up @@ -14,12 +14,13 @@ function getValues(root) {
socketMessageEvent: 'RTCMultiConnection-Message',
socketCustomEvent: 'RTCMultiConnection-Custom-Message',
port: process.env.PORT || 9001,
enableLogs: true,
autoRebootServerOnFailure: null,
enableLogs: false,
autoRebootServerOnFailure: false,
isUseHTTPs: null,
sslKey: null,
sslCert: null,
sslCabundle: null,
enableAdmin: false,
adminUserName: null,
adminPassword: null
};
Expand Down Expand Up @@ -88,6 +89,10 @@ function getValues(root) {
result.socketCustomEvent = (config.socketCustomEvent || '').toString();
}

if ((config.enableAdmin || '').toString() === 'true') {
result.enableAdmin = true;
}

if ((config.adminUserName || '').toString().length) {
result.adminUserName = (config.adminUserName || '').toString();
}
Expand Down
13 changes: 11 additions & 2 deletions node_scripts/getJsonFile.js
@@ -1,7 +1,16 @@
function getJsonFile(path) {
var fs = require('fs');
var json = fs.readFileSync(path);
return JSON.parse(json);
var output = {};
try {
var json = fs.readFileSync(path);
output = JSON.parse(json);
}
catch(e) {
output = {};

// console.log(e.message, e.stack);
}
return output;
}

module.exports = exports = getJsonFile;
37 changes: 29 additions & 8 deletions node_scripts/index.js
Expand Up @@ -13,9 +13,13 @@ module.exports = exports = function(root) {

var resolveURL = require('./resolveURL.js');
var BASH_COLORS_HELPER = require('./BASH_COLORS_HELPER.js');
var config = require('./get-values-from-config-json.js')(root);

require('./get-bash-parameters.js')(config, BASH_COLORS_HELPER);
var getValuesFromConfigJson = require('./get-values-from-config-json.js');
var config = getValuesFromConfigJson(root);

var getBashParameters = require('./get-bash-parameters.js');

config = getBashParameters(config, BASH_COLORS_HELPER);
root.enableLogs = config.enableLogs; // used by "pushLogs"

var isAdminAuthorized = require('./verify-admin.js');
Expand All @@ -27,6 +31,11 @@ module.exports = exports = function(root) {

function serverHandler(request, response) {
try {
// to make sure we always get valid info from json file
// even if nested codes are overriding it
config = getValuesFromConfigJson(root);
config = getBashParameters(config, BASH_COLORS_HELPER);

var uri, filename;

try {
Expand All @@ -41,6 +50,8 @@ module.exports = exports = function(root) {
pushLogs(root, 'url.parse', e);
}

filename = (filename || '').toString();

if (request.method !== 'GET' || uri.indexOf('..') !== -1) {
try {
response.writeHead(401, {
Expand All @@ -56,20 +67,24 @@ module.exports = exports = function(root) {
}

var matched = false;
filename && ['/demos/', '/dev/', '/dist/', '/socket.io/', /*'/admin/',*/ '/node_modules/canvas-designer/'].forEach(function(item) {
['/demos/', '/dev/', '/dist/', '/socket.io/', '/node_modules/canvas-designer/'].forEach(function(item) {
if (filename.indexOf(resolveURL(item)) !== -1) {
matched = true;
}
});

if(config.enableAdmin === true && filename.indexOf(resolveURL('/admin/')) !== -1) {
matched = true;
}

// files from node_modules
['RecordRTC.js', 'FileBufferReader.js', 'getStats.js', 'getScreenId.js', 'adapter.js', 'MultiStreamsMixer.js'].forEach(function(item) {
if (filename.indexOf(resolveURL('/node_modules/')) !== -1 && filename.indexOf(resolveURL(item)) !== -1) {
matched = true;
}
});

if(false && filename.indexOf(resolveURL('/logs.json')) !== -1) {
if(config.enableAdmin === true && filename.indexOf(resolveURL('/logs.json')) !== -1) {
filename = path.join(config.dirPath ? resolveURL(config.dirPath) : process.cwd(), '/logs.json');

try {
Expand All @@ -89,7 +104,7 @@ module.exports = exports = function(root) {
}

// handle /admin/ page
if (false && filename && filename.indexOf(resolveURL('/admin/')) !== -1) {
if (config.enableAdmin === true && filename.indexOf(resolveURL('/admin/')) !== -1) {
if (!isAdminAuthorized(request, config)) {
try {
var adminAuthorization = require('basic-auth');
Expand Down Expand Up @@ -142,7 +157,7 @@ module.exports = exports = function(root) {
return;
}

if (filename && filename.search(/.js|.json/g) !== -1 && !matched) {
if (filename.search(/.js|.json/g) !== -1 && !matched) {
try {
response.writeHead(404, {
'Content-Type': 'text/plain'
Expand All @@ -158,7 +173,7 @@ module.exports = exports = function(root) {

['Video-Broadcasting', 'Screen-Sharing', 'Switch-Cameras'].forEach(function(fname) {
try {
if (filename && filename.indexOf(fname + '.html') !== -1) {
if (filename.indexOf(fname + '.html') !== -1) {
filename = filename.replace(fname + '.html', fname.toLowerCase() + '.html');
}
}
Expand All @@ -172,7 +187,7 @@ module.exports = exports = function(root) {
try {
stats = fs.lstatSync(filename);

if (filename && filename.search(/demos/g) === -1 && stats.isDirectory() && config.homePage === '/demos/index.html') {
if (filename.search(/demos/g) === -1 && stats.isDirectory() && config.homePage === '/demos/index.html') {
if (response.redirect) {
response.redirect('/demos/');
} else {
Expand Down Expand Up @@ -429,6 +444,12 @@ module.exports = exports = function(root) {
console.log(BASH_COLORS_HELPER.getRedBG(), 'Please run on HTTPs to make sure audio,video and screen demos can work on Google Chrome as well.');
}

if(config.enableAdmin === true) {
console.log('Admin page is enabled and running on: ' + domainURL + 'admin/');
console.log('\tAdmin page username: ' + config.adminUserName);
console.log('\tAdmin page password: ' + config.adminPassword);
}

console.log('For more help: ', BASH_COLORS_HELPER.getYellowFG('node server.js --help'));
console.log('\n');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "rtcmulticonnection-server",
"preferGlobal": false,
"version": "1.2.3",
"version": "1.2.4",
"author": {
"name": "Muaz Khan",
"email": "muazkh@gmail.com",
Expand Down

0 comments on commit a1a1932

Please sign in to comment.