From e52b2e629085269b96b1185c72ba055d991e93f7 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 12 Jul 2020 12:33:57 +0200 Subject: websockets: Parametrise port/host parameters --- websockets/package.json | 2 +- websockets/server.js | 95 ----------------------------------------- websockets/server_proxy.js | 103 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 96 deletions(-) delete mode 100755 websockets/server.js create mode 100755 websockets/server_proxy.js (limited to 'websockets') diff --git a/websockets/package.json b/websockets/package.json index a92d034..7d6d802 100644 --- a/websockets/package.json +++ b/websockets/package.json @@ -2,7 +2,7 @@ "name": "tomsg-webserver", "version": "0.1.0", "description": "tomsg websocket-to-tcp converter", - "main": "server.js", + "main": "server_proxy.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/websockets/server.js b/websockets/server.js deleted file mode 100755 index 763b343..0000000 --- a/websockets/server.js +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env node -const fs=require("fs"); -const net=require("net"); -const https=require("https"); -const WebSocket=require("uWebSockets.js"); - -const PORT=29546; - -const upstream={ - host: "localhost", - port: 29536 -}; - - -let httpsConfig=null; -if(process.argv.length==4){ - console.log("Reading keys for https"); - httpsConfig={ - key_file_name: process.argv[2], - cert_file_name: process.argv[3], - }; -} else if(process.argv.length==2){ - console.log("WARNING: Running without SSL!"); -} else { - console.error("Usage: ./server.js # proxy without SSL"); - console.error(" ./server.js # proxy with SSL"); - process.exit(1); -} - -if(process.getuid()==0){ - console.log(`Old uid: ${process.getuid()}, old gid: ${process.getgid()}; setting to nobody...`); - process.setgid("nobody"); - process.setuid("nobody"); - console.log(`New uid: ${process.getuid()}, new gid: ${process.getgid()}`); -} - - -let wsServer; -if(httpsConfig){ - wsServer=WebSocket.SSLApp(httpsConfig); -} else { - wsServer=WebSocket.App(); -} - -wsServer=wsServer.ws("/*",{ - open: sock=>{ - const stateobj={netconn: null, buffer: [], sock_closed: false}; - sock["tomsgdata"]=stateobj; - let linebuf=""; - - stateobj.netconn=net.connect(upstream.port,upstream.host,()=>{ - for(const item of stateobj.buffer){ - stateobj.netconn.write(item); - stateobj.netconn.write("\n"); - } - stateobj.buffer=[]; - }); - stateobj.netconn.on("close",()=>{ - if(!stateobj.sock_closed)sock.close(); - }); - stateobj.netconn.on("data",(data)=>{ - linebuf+=data; - let idx; - while((idx=linebuf.indexOf("\n"))!=-1){ - sock.send(linebuf.slice(0,idx)); - linebuf=linebuf.slice(idx+1); - } - }); - stateobj.netconn.on("error",()=>{ - if(!stateobj.sock_closed)sock.close(); - }); - }, - message: (sock,data,isBinary)=>{ - const stateobj=sock["tomsgdata"]; - data=new Uint8Array(data); - if(stateobj.netconn.connecting){ - stateobj.buffer.push(data); - } else { - stateobj.netconn.write(data); - stateobj.netconn.write("\n"); - } - }, - close: sock=>{ - const stateobj=sock["tomsgdata"]; - stateobj.sock_closed=true; - try {stateobj.netconn.end();} - catch (e) {} - } -}); - -wsServer=wsServer.listen(PORT,listenSocket=>{ - if(listenSocket){ - console.log(`Websocket server${httpsConfig?" (SSL)":""} bound on port ${PORT}`); - } -}); diff --git a/websockets/server_proxy.js b/websockets/server_proxy.js new file mode 100755 index 0000000..5f4ea17 --- /dev/null +++ b/websockets/server_proxy.js @@ -0,0 +1,103 @@ +#!/usr/bin/env node +const fs=require("fs"); +const net=require("net"); +const https=require("https"); +const WebSocket=require("uWebSockets.js"); + +let listenport=null; + +let upstream=null; + +function parseUpstream(arg){ + const [host, port]=arg.split(":"); + return {host, port}; +} + + +let httpsConfig=null; +if(process.argv.length==6){ + upstream=parseUpstream(process.argv[2]); + listenport=+process.argv[3]; + console.log("Reading keys for https"); + httpsConfig={ + key_file_name: process.argv[4], + cert_file_name: process.argv[5], + }; +} else if(process.argv.length==4){ + upstream=parseUpstream(process.argv[2]); + listenport=+process.argv[3]; + console.log("WARNING: Running without SSL!"); +} else { + console.error("Usage: ./server.js # proxy without SSL"); + console.error(" ./server.js # proxy with SSL"); + console.error("'' indicates the plain-tcp tomsg server to proxy to."); + console.error("'' indicates the port to listen for websocket connections on."); + process.exit(1); +} + +if(process.getuid()==0){ + console.log(`Old uid: ${process.getuid()}, old gid: ${process.getgid()}; setting to nobody...`); + process.setgid("nobody"); + process.setuid("nobody"); + console.log(`New uid: ${process.getuid()}, new gid: ${process.getgid()}`); +} + + +let wsServer; +if(httpsConfig){ + wsServer=WebSocket.SSLApp(httpsConfig); +} else { + wsServer=WebSocket.App(); +} + +wsServer=wsServer.ws("/*",{ + open: sock=>{ + const stateobj={netconn: null, buffer: [], sock_closed: false}; + sock["tomsgdata"]=stateobj; + let linebuf=""; + + stateobj.netconn=net.connect(upstream.port,upstream.host,()=>{ + for(const item of stateobj.buffer){ + stateobj.netconn.write(item); + stateobj.netconn.write("\n"); + } + stateobj.buffer=[]; + }); + stateobj.netconn.on("close",()=>{ + if(!stateobj.sock_closed)sock.close(); + }); + stateobj.netconn.on("data",(data)=>{ + linebuf+=data; + let idx; + while((idx=linebuf.indexOf("\n"))!=-1){ + sock.send(linebuf.slice(0,idx)); + linebuf=linebuf.slice(idx+1); + } + }); + stateobj.netconn.on("error",()=>{ + if(!stateobj.sock_closed)sock.close(); + }); + }, + message: (sock,data,isBinary)=>{ + const stateobj=sock["tomsgdata"]; + data=new Uint8Array(data); + if(stateobj.netconn.connecting){ + stateobj.buffer.push(data); + } else { + stateobj.netconn.write(data); + stateobj.netconn.write("\n"); + } + }, + close: sock=>{ + const stateobj=sock["tomsgdata"]; + stateobj.sock_closed=true; + try {stateobj.netconn.end();} + catch (e) {} + } +}); + +wsServer=wsServer.listen(listenport,listenSocket=>{ + if(listenSocket){ + console.log(`Websocket server${httpsConfig?" (SSL)":""} bound on port ${listenport}`); + } +}); -- cgit v1.2.3-70-g09d2