syntex-webserver
Advanced tools
Comparing version 1.1.1-b1 to 1.1.1-b10
112
main.js
let LanguageManager = require('./language_manager'); | ||
const http = require('http'), url = require('url'), fs = require('fs'), path = require('path'), socket = require('ws'), formidable = require('formidable'); | ||
const http = require('http'), https = require('https'), net = require('net'), url = require('url'), fs = require('fs'), path = require('path'), socket = require('ws'), formidable = require('formidable'); | ||
@@ -17,2 +17,17 @@ var pages = [], sockets = [], head = '', footer = ''; | ||
var createTCPCallback = (request) => { | ||
request.once('data', (buffer) => { | ||
var port = (buffer[0] == 22) ? (options.https || options.http) : (options.http || options.https); | ||
var proxy = net.createConnection(port, () => { | ||
proxy.write(buffer); | ||
request.pipe(proxy).pipe(request); | ||
}); | ||
}); | ||
}; | ||
var createServerCallback = async (request, response) => { | ||
@@ -29,6 +44,8 @@ | ||
'.png': 'image/png', | ||
'.ico': 'image/x-icon', | ||
'.mp4' : 'video/mp4', | ||
'.mov' : 'video/quicktime', | ||
'.js': 'text/javascript', | ||
'.css': 'text/css', | ||
'.ttf': 'font/ttf', | ||
'.ico': 'image/x-icon', | ||
'.json': 'application/json' | ||
@@ -46,9 +63,11 @@ }; | ||
{ | ||
var extension = path.parse(relPath).ext.toLowerCase(); | ||
content = await this.read(relPath); | ||
if(path.parse(relPath).ext == '.html') | ||
if(extension == '.html') | ||
{ | ||
content = this.LanguageManager.parseLanguage(head + content + footer); | ||
} | ||
else if(path.parse(relPath).ext == '.js') | ||
else if(extension == '.js') | ||
{ | ||
@@ -58,3 +77,3 @@ content = this.LanguageManager.parseLanguage(content.toString()); | ||
if(mimeType[path.parse(relPath).ext] != null && (mimeType[path.parse(relPath).ext].startsWith('image') || mimeType[path.parse(relPath).ext].startsWith('font'))) | ||
if(mimeType[extension] != null && (mimeType[extension].startsWith('image') || mimeType[extension].startsWith('video') || mimeType[extension].startsWith('font'))) | ||
{ | ||
@@ -64,3 +83,3 @@ response.setHeader('Cache-Control', 'public, max-age=3600'); | ||
response.setHeader('Content-Type', mimeType[path.parse(relPath).ext] || 'text/html; charset=utf-8'); | ||
response.setHeader('Content-Type', mimeType[extension] || 'text/html; charset=utf-8'); | ||
} | ||
@@ -106,3 +125,3 @@ } | ||
{ | ||
this.logger.log('error', 'webServer', 'WebServer', '%json_parse_error%!', e); | ||
//this.logger.log('error', 'webServer', 'WebServer', '%json_parse_error%!', e); | ||
} | ||
@@ -168,38 +187,69 @@ } | ||
let WebSocket = new socket.Server({ server : http.createServer(createServerCallback).listen(platform.port, '0.0.0.0') }); | ||
var createSocketCallback = (server) => { | ||
setInterval(() => { | ||
let WebSocket = new socket.Server({ server }); | ||
WebSocket.clients.forEach((ws) => ws.ping(() => {})); | ||
setInterval(() => { | ||
}, 30000); | ||
WebSocket.clients.forEach((ws) => ws.ping(() => {})); | ||
WebSocket.on('connection', (ws, request) => { | ||
}, 30000); | ||
var urlParts = url.parse(request.url, true); | ||
var urlParams = urlParts.query; | ||
var urlPath = urlParts.pathname; | ||
WebSocket.on('connection', (ws, request) => { | ||
ws.on('message', (message) => { | ||
var urlParts = url.parse(request.url, true); | ||
var urlParams = urlParts.query; | ||
var urlPath = urlParts.pathname; | ||
try | ||
{ | ||
message = JSON.parse(message); | ||
} | ||
catch(e) | ||
{ | ||
this.logger.log('error', 'webServer', 'WebServer', 'WebSocket %json_parse_error%!', e); | ||
} | ||
ws.on('message', (message) => { | ||
for(const socket of sockets) | ||
{ | ||
if(socket.path == urlPath && socket.line == message.line) | ||
try | ||
{ | ||
socket.callback(ws, message.params, urlParams); | ||
message = JSON.parse(message); | ||
} | ||
} | ||
catch(e) | ||
{ | ||
this.logger.log('error', 'webServer', 'WebServer', 'WebSocket %json_parse_error%!', e); | ||
} | ||
for(const socket of sockets) | ||
{ | ||
if(socket.path == urlPath && socket.line == message.line) | ||
{ | ||
socket.callback(ws, message.params, urlParams); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
this.logger.log('info', 'webServer', 'WebServer', platform.pluginName + ' %port_running% [' + platform.port + ']'); | ||
if(options.http || options.https) | ||
{ | ||
var Proxy = net.createServer(createTCPCallback).listen(platform.port); | ||
Proxy.on('error', (error) => { | ||
this.logger.err(error); | ||
}); | ||
this.logger.log('info', 'webServer', 'WebServer', platform.pluginName + ' %port_running% [' + platform.port + '] ( TCP )'); | ||
} | ||
else | ||
{ | ||
options.http = platform.port; | ||
} | ||
if(options.http) | ||
{ | ||
createSocketCallback(http.createServer(createServerCallback).listen(options.http)); | ||
this.logger.log('info', 'webServer', 'WebServer', platform.pluginName + ' %port_running% [' + options.http + '] ( HTTP )'); | ||
} | ||
if(options.https) | ||
{ | ||
createSocketCallback(https.createServer(options.certificate, createServerCallback).listen(options.https)); | ||
this.logger.log('info', 'webServer', 'WebServer', platform.pluginName + ' %port_running% [' + options.https + '] ( HTTPS )'); | ||
} | ||
} | ||
@@ -206,0 +256,0 @@ |
{ | ||
"name": "syntex-webserver", | ||
"version": "1.1.1-b1", | ||
"version": "1.1.1-b10", | ||
"description": "A HTTP Based WebServer System", | ||
@@ -13,3 +13,3 @@ "main": "main.js", | ||
"formidable": "^2.0.1", | ||
"syntex-logger": "1.0.8", | ||
"syntex-logger": "1.0.9-b2", | ||
"ws": "^7.4.0" | ||
@@ -16,0 +16,0 @@ }, |
@@ -18,3 +18,3 @@ # SynTex WebServer | ||
let logger = new Logger({ pluginName : 'Demo Plugin', language : 'us', debug : true }); | ||
let logger = new Logger({ pluginName : 'Demo Plugin' }, { language : 'us', levels : { debug : false }, time : true }); | ||
@@ -21,0 +21,0 @@ logger.setLogDirectory('/var/demo_plugin/logs/'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13749
381
3
+ Addedsyntex-logger@1.0.9-b2(transitive)
- Removedsyntex-logger@1.0.8(transitive)
Updatedsyntex-logger@1.0.9-b2