2018-09-29 01:39:35 +00:00
|
|
|
var fs = require('fs');
|
2018-09-28 05:37:08 +00:00
|
|
|
var http = require('http');
|
2018-09-28 16:13:17 +00:00
|
|
|
var https = require('https');
|
2018-09-29 01:57:26 +00:00
|
|
|
var express = require('express');
|
|
|
|
var app = express()
|
|
|
|
module.exports = function(s,config,lang,io){
|
2018-11-06 04:27:19 +00:00
|
|
|
//get page URL
|
|
|
|
if(!config.baseURL){
|
|
|
|
config.baseURL = ""
|
|
|
|
}else if(config.baseURL !== ''){
|
|
|
|
config.baseURL = s.checkCorrectPathEnding(config.baseURL)
|
|
|
|
}
|
|
|
|
//Render Configurations - Web Paths
|
|
|
|
if(config.webPaths === undefined){config.webPaths={}}
|
|
|
|
//main access URI
|
|
|
|
if(config.webPaths.home === undefined){config.webPaths.home='/'}
|
|
|
|
//Super User URI
|
|
|
|
if(config.webPaths.super === undefined){config.webPaths.super='/super'}
|
|
|
|
//Admin URI
|
|
|
|
if(config.webPaths.admin === undefined){config.webPaths.admin='/admin'}
|
|
|
|
//Libraries URI
|
|
|
|
if(config.webPaths.libs === undefined){config.webPaths.libs='/libs'}
|
|
|
|
//API Prefix
|
|
|
|
if(config.webPaths.apiPrefix === undefined){config.webPaths.apiPrefix = s.checkCorrectPathEnding(config.webPaths.home)}else{config.webPaths.apiPrefix = s.checkCorrectPathEnding(config.webPaths.apiPrefix)}
|
|
|
|
//Admin API Prefix
|
2018-11-06 16:25:00 +00:00
|
|
|
if(config.webPaths.adminApiPrefix === undefined){config.webPaths.adminApiPrefix=s.checkCorrectPathEnding(config.webPaths.admin)}else{config.webPaths.adminApiPrefix = s.checkCorrectPathEnding(config.webPaths.adminApiPrefix)}
|
2018-11-06 04:27:19 +00:00
|
|
|
//Super API Prefix
|
2018-11-06 16:25:00 +00:00
|
|
|
if(config.webPaths.superApiPrefix === undefined){config.webPaths.superApiPrefix=s.checkCorrectPathEnding(config.webPaths.super)}else{config.webPaths.superApiPrefix = s.checkCorrectPathEnding(config.webPaths.superApiPrefix)}
|
2018-11-06 04:27:19 +00:00
|
|
|
//Render Configurations - Page Render Paths
|
|
|
|
if(config.renderPaths === undefined){config.renderPaths={}}
|
|
|
|
//login page
|
|
|
|
if(config.renderPaths.index === undefined){config.renderPaths.index='pages/index'}
|
|
|
|
//dashboard page
|
|
|
|
if(config.renderPaths.home === undefined){config.renderPaths.home='pages/home'}
|
|
|
|
//sub-account administration page
|
|
|
|
if(config.renderPaths.admin === undefined){config.renderPaths.admin='pages/admin'}
|
|
|
|
//superuser page
|
|
|
|
if(config.renderPaths.super === undefined){config.renderPaths.super='pages/super'}
|
|
|
|
//2-Factor Auth page
|
|
|
|
if(config.renderPaths.factorAuth === undefined){config.renderPaths.factorAuth='pages/factor'}
|
|
|
|
//Streamer v1 (Dashcam Prototype) page
|
|
|
|
if(config.renderPaths.streamer === undefined){config.renderPaths.streamer='pages/streamer'}
|
|
|
|
//Streamer v2 (Dashcam) page
|
|
|
|
if(config.renderPaths.dashcam === undefined){config.renderPaths.dashcam='pages/dashcam'}
|
|
|
|
//embeddable widget page
|
|
|
|
if(config.renderPaths.embed === undefined){config.renderPaths.embed='pages/embed'}
|
2019-04-03 03:47:03 +00:00
|
|
|
//timelapse page (not modal)
|
|
|
|
if(config.renderPaths.timelapse === undefined){config.renderPaths.timelapse='pages/timelapse'}
|
2018-11-06 04:27:19 +00:00
|
|
|
//mjpeg full screen page
|
|
|
|
if(config.renderPaths.mjpeg === undefined){config.renderPaths.mjpeg='pages/mjpeg'}
|
|
|
|
//gridstack only page
|
|
|
|
if(config.renderPaths.grid === undefined){config.renderPaths.grid='pages/grid'}
|
|
|
|
//slick.js (cycle) page
|
|
|
|
if(config.renderPaths.cycle === undefined){config.renderPaths.cycle='pages/cycle'}
|
2019-02-25 02:47:39 +00:00
|
|
|
// Use uws/cws
|
|
|
|
if(config.useUWebsocketJs === undefined){config.useUWebsocketJs=true}
|
2018-09-28 05:37:08 +00:00
|
|
|
//SSL options
|
2019-04-03 05:42:48 +00:00
|
|
|
var wellKnownDirectory = s.mainDirectory + '/web/.well-known'
|
|
|
|
if(fs.existsSync(wellKnownDirectory))app.use('/.well-known',express.static(wellKnownDirectory))
|
2020-09-22 06:49:18 +00:00
|
|
|
config.sslEnabled = false
|
2018-09-28 05:37:08 +00:00
|
|
|
if(config.ssl&&config.ssl.key&&config.ssl.cert){
|
2020-09-22 06:49:18 +00:00
|
|
|
config.sslEnabled = true
|
2018-09-28 05:37:08 +00:00
|
|
|
config.ssl.key=fs.readFileSync(s.checkRelativePath(config.ssl.key),'utf8')
|
|
|
|
config.ssl.cert=fs.readFileSync(s.checkRelativePath(config.ssl.cert),'utf8')
|
|
|
|
if(config.ssl.port === undefined){
|
|
|
|
config.ssl.port=443
|
|
|
|
}
|
|
|
|
if(config.ssl.bindip === undefined){
|
|
|
|
config.ssl.bindip=config.bindip
|
|
|
|
}
|
|
|
|
if(config.ssl.ca&&config.ssl.ca instanceof Array){
|
|
|
|
config.ssl.ca.forEach(function(v,n){
|
|
|
|
config.ssl.ca[n]=fs.readFileSync(s.checkRelativePath(v),'utf8')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var serverHTTPS = https.createServer(config.ssl,app);
|
|
|
|
serverHTTPS.listen(config.ssl.port,config.bindip,function(){
|
2018-09-29 23:03:55 +00:00
|
|
|
console.log('SSL '+lang.Shinobi+' : SSL Web Server Listening on '+config.ssl.port);
|
2018-09-28 05:37:08 +00:00
|
|
|
});
|
2018-11-06 04:27:19 +00:00
|
|
|
if(config.webPaths.home !== '/'){
|
|
|
|
io.attach(serverHTTPS,{
|
|
|
|
path:'/socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
}
|
|
|
|
io.attach(serverHTTPS,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.home)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
io.attach(serverHTTPS,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.admin)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
io.attach(serverHTTPS,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.super)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
2020-07-15 06:38:13 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if(!req.secure) {
|
2020-07-15 16:34:43 +00:00
|
|
|
return res.redirect(['https://', req.hostname,":",config.ssl.port, req.url].join(''));
|
2020-07-15 06:38:13 +00:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
2018-09-28 05:37:08 +00:00
|
|
|
}
|
|
|
|
//start HTTP
|
2018-11-06 04:27:19 +00:00
|
|
|
var server = http.createServer(app);
|
2018-09-28 05:37:08 +00:00
|
|
|
server.listen(config.port,config.bindip,function(){
|
2018-09-29 23:03:55 +00:00
|
|
|
console.log(lang.Shinobi+' : Web Server Listening on '+config.port);
|
2018-09-28 05:37:08 +00:00
|
|
|
});
|
2018-11-06 04:27:19 +00:00
|
|
|
if(config.webPaths.home !== '/'){
|
|
|
|
io.attach(server,{
|
|
|
|
path:'/socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
}
|
|
|
|
io.attach(server,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.home)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
io.attach(server,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.admin)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
|
|
|
io.attach(server,{
|
|
|
|
path:s.checkCorrectPathEnding(config.webPaths.super)+'socket.io',
|
|
|
|
transports: ['websocket']
|
|
|
|
})
|
2019-02-25 02:47:39 +00:00
|
|
|
if(config.useUWebsocketJs === true){
|
|
|
|
io.engine.ws = new (require('cws').Server)({
|
|
|
|
noServer: true,
|
|
|
|
perMessageDeflate: false
|
|
|
|
})
|
|
|
|
}
|
2018-09-29 01:57:26 +00:00
|
|
|
return app
|
2018-09-28 05:37:08 +00:00
|
|
|
}
|