292 lines
10 KiB
JavaScript
292 lines
10 KiB
JavaScript
// http://127.0.0.1:9001
|
|
// http://localhost:9001
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const url = require('url');
|
|
var httpServer = require('http');
|
|
|
|
const ioServer = require('socket.io');
|
|
const RTCMultiConnectionServer = require('rtcmulticonnection-server');
|
|
|
|
var PORT = 9001;
|
|
var isUseHTTPs = false;
|
|
|
|
const jsonPath = {
|
|
config: 'config.json',
|
|
logs: 'logs.json'
|
|
};
|
|
|
|
const BASH_COLORS_HELPER = RTCMultiConnectionServer.BASH_COLORS_HELPER;
|
|
const getValuesFromConfigJson = RTCMultiConnectionServer.getValuesFromConfigJson;
|
|
const getBashParameters = RTCMultiConnectionServer.getBashParameters;
|
|
const resolveURL = RTCMultiConnectionServer.resolveURL;
|
|
|
|
var config = getValuesFromConfigJson(jsonPath);
|
|
config = getBashParameters(config, BASH_COLORS_HELPER);
|
|
|
|
// if user didn't modifed "PORT" object
|
|
// then read value from "config.json"
|
|
if(PORT === 9001) {
|
|
PORT = config.port;
|
|
}
|
|
if(isUseHTTPs === false) {
|
|
isUseHTTPs = config.isUseHTTPs;
|
|
}
|
|
|
|
function serverHandler(request, response) {
|
|
// to make sure we always get valid info from json file
|
|
// even if external codes are overriding it
|
|
config = getValuesFromConfigJson(jsonPath);
|
|
config = getBashParameters(config, BASH_COLORS_HELPER);
|
|
|
|
// HTTP_GET handling code goes below
|
|
try {
|
|
var uri, filename;
|
|
|
|
try {
|
|
if (!config.dirPath || !config.dirPath.length) {
|
|
config.dirPath = null;
|
|
}
|
|
|
|
uri = url.parse(request.url).pathname;
|
|
filename = path.join(config.dirPath ? resolveURL(config.dirPath) : process.cwd(), uri);
|
|
} catch (e) {
|
|
pushLogs(config, 'url.parse', e);
|
|
}
|
|
|
|
filename = (filename || '').toString();
|
|
|
|
if (request.method !== 'GET' || uri.indexOf('..') !== -1) {
|
|
try {
|
|
response.writeHead(401, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('401 Unauthorized: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
} catch (e) {
|
|
pushLogs(config, '!GET or ..', e);
|
|
}
|
|
}
|
|
|
|
if(filename.indexOf(resolveURL('/admin/')) !== -1 && config.enableAdmin !== true) {
|
|
try {
|
|
response.writeHead(401, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('401 Unauthorized: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
} catch (e) {
|
|
pushLogs(config, '!GET or ..', e);
|
|
}
|
|
return;
|
|
}
|
|
|
|
var matched = false;
|
|
['/include/videocall/demos/', '/include/videocall/dev/', '/dist/', '/include/videocall/', '/include/videocall/demos/node_modules/canvas-designer/', '/admin/'].forEach(function(item) {
|
|
if (filename.indexOf(resolveURL(item)) !== -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('/include/videocall/demos/node_modules/')) !== -1 && filename.indexOf(resolveURL(item)) !== -1) {
|
|
matched = true;
|
|
}
|
|
});
|
|
|
|
if (filename.search(/.js|.json/g) !== -1 && !matched) {
|
|
try {
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
} catch (e) {
|
|
pushLogs(config, '404 Not Found', e);
|
|
}
|
|
}
|
|
|
|
['Video-Broadcasting', 'Screen-Sharing', 'Switch-Cameras'].forEach(function(fname) {
|
|
try {
|
|
if (filename.indexOf(fname + '.html') !== -1) {
|
|
filename = filename.replace(fname + '.html', fname.toLowerCase() + '.html');
|
|
}
|
|
} catch (e) {
|
|
pushLogs(config, 'forEach', e);
|
|
}
|
|
});
|
|
|
|
var stats;
|
|
|
|
try {
|
|
stats = fs.lstatSync(filename);
|
|
|
|
if (filename.search(/include/videocall/demos/g) === -1 && filename.search(/admin/g) === -1 && stats.isDirectory() && config.homePage === '/include/videocall/demos/index.html') {
|
|
if (response.redirect) {
|
|
response.redirect('/include/videocall/demos/');
|
|
} else {
|
|
response.writeHead(301, {
|
|
'Location': '/include/videocall/demos/'
|
|
});
|
|
}
|
|
response.end();
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (fs.statSync(filename).isDirectory()) {
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/html'
|
|
});
|
|
|
|
if (filename.indexOf(resolveURL('/include/videocall/demos/MultiRTC/')) !== -1) {
|
|
filename = filename.replace(resolveURL('/include/videocall/demos/MultiRTC/'), '');
|
|
filename += resolveURL('/include/videocall/demos/MultiRTC/index.html');
|
|
} else if (filename.indexOf(resolveURL('/admin/')) !== -1) {
|
|
filename = filename.replace(resolveURL('/admin/'), '');
|
|
filename += resolveURL('/admin/index.html');
|
|
} else if (filename.indexOf(resolveURL('/include/videocall/demos/dashboard/')) !== -1) {
|
|
filename = filename.replace(resolveURL('/include/videocall/demos/dashboard/'), '');
|
|
filename += resolveURL('/include/videocall/demos/dashboard/index.html');
|
|
} else if (filename.indexOf(resolveURL('/include/videocall/demos/video-conference/')) !== -1) {
|
|
filename = filename.replace(resolveURL('/include/videocall/demos/video-conference/'), '');
|
|
filename += resolveURL('/include/videocall/demos/video-conference/index.html');
|
|
} else if (filename.indexOf(resolveURL('/demos')) !== -1) {
|
|
filename = filename.replace(resolveURL('/include/videocall/demos/'), '');
|
|
filename = filename.replace(resolveURL('/demos'), '');
|
|
filename += resolveURL('/include/videocall/demos/index.html');
|
|
} else {
|
|
filename += resolveURL(config.homePage);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
pushLogs(config, 'statSync.isDirectory', e);
|
|
}
|
|
|
|
var contentType = 'text/plain';
|
|
if (filename.toLowerCase().indexOf('.html') !== -1) {
|
|
contentType = 'text/html';
|
|
}
|
|
if (filename.toLowerCase().indexOf('.css') !== -1) {
|
|
contentType = 'text/css';
|
|
}
|
|
if (filename.toLowerCase().indexOf('.png') !== -1) {
|
|
contentType = 'image/png';
|
|
}
|
|
|
|
fs.readFile(filename, 'binary', function(err, file) {
|
|
if (err) {
|
|
response.writeHead(500, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
|
|
response.end();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
file = file.replace('connection.socketURL = \'/\';', 'connection.socketURL = \'' + config.socketURL + '\';');
|
|
} catch (e) {}
|
|
|
|
response.writeHead(200, {
|
|
'Content-Type': contentType
|
|
});
|
|
response.write(file, 'binary');
|
|
response.end();
|
|
});
|
|
} catch (e) {
|
|
pushLogs(config, 'Unexpected', e);
|
|
|
|
response.writeHead(404, {
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
response.write('404 Not Found: Unexpected error.\n' + e.message + '\n\n' + e.stack);
|
|
response.end();
|
|
}
|
|
}
|
|
|
|
var httpApp;
|
|
|
|
if (isUseHTTPs) {
|
|
httpServer = require('https');
|
|
|
|
// See how to use a valid certificate:
|
|
// https://github.com/muaz-khan/WebRTC-Experiment/issues/62
|
|
var options = {
|
|
key: null,
|
|
cert: null,
|
|
ca: null
|
|
};
|
|
|
|
var pfx = false;
|
|
|
|
if (!fs.existsSync(config.sslKey)) {
|
|
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslKey:\t ' + config.sslKey + ' does not exist.');
|
|
} else {
|
|
pfx = config.sslKey.indexOf('.pfx') !== -1;
|
|
options.key = fs.readFileSync(config.sslKey);
|
|
}
|
|
|
|
if (!fs.existsSync(config.sslCert)) {
|
|
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslCert:\t ' + config.sslCert + ' does not exist.');
|
|
} else {
|
|
options.cert = fs.readFileSync(config.sslCert);
|
|
}
|
|
|
|
if (config.sslCabundle) {
|
|
if (!fs.existsSync(config.sslCabundle)) {
|
|
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslCabundle:\t ' + config.sslCabundle + ' does not exist.');
|
|
}
|
|
|
|
options.ca = fs.readFileSync(config.sslCabundle);
|
|
}
|
|
|
|
if (pfx === true) {
|
|
options = {
|
|
pfx: sslKey
|
|
};
|
|
}
|
|
|
|
httpApp = httpServer.createServer(options, serverHandler);
|
|
} else {
|
|
httpApp = httpServer.createServer(serverHandler);
|
|
}
|
|
|
|
RTCMultiConnectionServer.beforeHttpListen(httpApp, config);
|
|
httpApp = httpApp.listen(process.env.PORT || PORT, process.env.IP || "0.0.0.0", function() {
|
|
RTCMultiConnectionServer.afterHttpListen(httpApp, config);
|
|
});
|
|
|
|
// --------------------------
|
|
// socket.io codes goes below
|
|
|
|
ioServer(httpApp).on('connection', function(socket) {
|
|
RTCMultiConnectionServer.addSocket(socket, config);
|
|
|
|
// ----------------------
|
|
// below code is optional
|
|
|
|
const params = socket.handshake.query;
|
|
|
|
if (!params.socketCustomEvent) {
|
|
params.socketCustomEvent = 'custom-message';
|
|
}
|
|
|
|
socket.on(params.socketCustomEvent, function(message) {
|
|
socket.broadcast.emit(params.socketCustomEvent, message);
|
|
});
|
|
});
|