76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
const fs = require('fs');
|
|
const https = require('https');
|
|
const url = require('url');
|
|
const mime = require('mime-types');
|
|
|
|
const WebSocket = require('ws');
|
|
const driverWS = require("./programs/driver"); // Import the WebSocket forwarding module
|
|
|
|
const httpsOptions = {
|
|
"enable": true,
|
|
'key': fs.readFileSync('https/localhost.key'),
|
|
'cert': fs.readFileSync('https/localhost.pem'),
|
|
"passphrase": "abcd"
|
|
}
|
|
|
|
server = https.createServer(httpsOptions, (req, res) => {
|
|
console.log("Request");
|
|
let parsedURL = url.parse(req.url, true);
|
|
let path = parsedURL.path.replace(/^\/+|\/+$/g,"");
|
|
if(path == ""){ path = "index.html"; }
|
|
|
|
|
|
let file = __dirname + "/public/" + path;
|
|
if(path.indexOf("node_modules/")!= -1){
|
|
console.warn("Pfad muss auf Module umgehängt werden: " + path);
|
|
file = __dirname + "/" + path;
|
|
}
|
|
|
|
|
|
console.log("file : " + file);
|
|
fs.readFile(file, function(err, content){
|
|
if(err){
|
|
console.log("File Not found "+ file);
|
|
res.writeHead(404);
|
|
res.end();
|
|
} else {
|
|
console.log("test");
|
|
res.setHeader('X-Content-Type-Option', 'nosniff');
|
|
let mimeT = mime.lookup(path);
|
|
res.writeHead(200,{'Content-type': mimeT});
|
|
res.end(content);
|
|
}
|
|
})
|
|
});
|
|
|
|
|
|
|
|
// vvvvvvvvvvvvvvvvvvvvvvvvvvv Weiterleitung WebSocket vvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
|
|
const wssInput = new WebSocket.Server({ noServer: true });
|
|
|
|
|
|
const targetServer = process.env.TARGET_SERVER || 'wss://appRobot_Driver:2095';
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
|
driverWS.setupCommandForwarding(wssInput, targetServer);
|
|
|
|
server.on('upgrade', (request, socket, head) => {
|
|
const pathname = url.parse(request.url).pathname;
|
|
|
|
if (pathname === '/ws' || pathname === '/echo') {
|
|
console.log('WS Upgrade auf', pathname);
|
|
|
|
wssInput.handleUpgrade(request, socket, head, (ws) => {
|
|
wssInput.emit('connection', ws, request);
|
|
});
|
|
|
|
} else {
|
|
socket.destroy();
|
|
}
|
|
});
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Weiterleitung WebSocket ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
server.listen(1003);
|
|
console.log("WebServer auf https://localhost:1003")
|