diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4e451c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.cert +*.key +.node_modules \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..90203a5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vdo.ninja"] + path = vdo.ninja + url = https://github.com/steveseguin/vdo.ninja.git diff --git a/README.md b/README.md index 9ee69ef..afc7ad5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -This is a basic web-socket server, similar to that provided by piesocket.com. It accepts messages and broadcasts them to everyone else connected. +This is a basic web-socket & static-file server. Websocket messages broadcasts them to everyone else connected. ## Purpose This can be used with a number of apps provided by Steve Seguin, including caption.ninja, vdo.ninja, chat.overlay.ninja, and more. @@ -11,23 +11,47 @@ sudo apt-get update sudo apt-get upgrade sudo apt-get install nodejs -y sudo apt-get install npm -y -sudo npm install express -sudo npm install ws -sudo npm install fs -sudo add-apt-repository ppa:certbot/certbot -sudo apt-get install certbot -y -sudo certbot certonly // register your domain +npm install +``` + +Fetch vdo.ninja static files +``` +git submodule update --init +``` + +Create self-signed cert for local network +``` +./create-localnetwork-cert.sh ``` ## Run ``` -sudo nodejs server.js // port 443 needs to be open. THIS STARTS THE SERVER +npm start ``` +visit to https://localhost:3000 ## If using with VDO.Ninja -If using this with a ninja deploy, you'll also need to deploy ninja v17.3 or newer, and then update the index.html of the ninja installation with the connection details. You'll need to enable the `customWSS` mode and set the wss server address to whatever you setup, such as with: +Update `vdo.ninja/index.html` to match your setup: ``` -session.wss = "wss://wss.contribute.cam:443"; +session.wss = "wss://:3000"; session.customWSS = true; ``` note: The most update to date directions on how to configure VDO.Ninja will likely be found in its index.html file; this repository's instructions may be out of date in comparison. + +## Advanced +### Certbot certificate setup +Using certbot https://certbot.eff.org/ +``` +sudo add-apt-repository ppa:certbot/certbot +sudo apt-get install certbot -y +sudo certbot certonly // register your domain +``` + +configure server.js +``` +const options = { + key: readFileSync('.key'), + cert: readFileSync('.crt') + }; +``` + diff --git a/create-localnetwork-cert.sh b/create-localnetwork-cert.sh new file mode 100755 index 0000000..b941377 --- /dev/null +++ b/create-localnetwork-cert.sh @@ -0,0 +1,5 @@ +openssl req -x509 -out localhost.crt -keyout localhost.key \ + -newkey rsa:2048 -nodes -sha256 \ + -addext basicConstraints=CA:TRUE,pathlen:0 \ + -subj '/CN=vdo.local' -extensions EXT -config <( \ + printf "[dn]\nCN=vdo.local\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:vdo.local\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth") \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..224b18f --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "scripts": { + "start": "node server" + }, + "dependencies": { + "polka": "0.5.x", + "sirv": "2.0.x", + "ws": "8.4.x" + } + } \ No newline at end of file diff --git a/server.js b/server.js index 364f6a5..3b9130a 100644 --- a/server.js +++ b/server.js @@ -2,46 +2,51 @@ // Copyright (c) 2021 Steve Seguin. All Rights Reserved. // Use of this source code is governed by the APGLv3 open-source // -///// INSTALLATION -// sudo apt-get update -// sudo apt-get upgrade -// sudo apt-get install nodejs -y -// sudo apt-get install npm -y -// sudo npm install express -// sudo npm install ws -// sudo npm install fs -// sudo add-apt-repository ppa:certbot/certbot -// sudo apt-get install certbot -y -// sudo certbot certonly // register your domain -// sudo nodejs server.js // port 443 needs to be open. THIS STARTS THE SERVER -// -//// Finally, if using this with a ninja deploy, update index.html of the ninja installation as needed, such as with: -// session.wss = "wss://wss.contribute.cam:443"; -// session.customWSS = true; # Please refer to the vdo.ninja instructions for exact details on settings; this is just a demo. -///////////////////////// - -"use strict"; -var fs = require("fs"); -var https = require("https"); -var express = require("express"); -var app = express(); -var WebSocket = require("ws"); - -const key = fs.readFileSync("/etc/letsencrypt/live/wss.contribute.cam/privkey.pem"); /// UPDATE THIS PATH -const cert = fs.readFileSync("/etc/letsencrypt/live/wss.contribute.cam/fullchain.pem"); /// UPDATE THIS PATH - -var server = https.createServer({key,cert}, app); -var websocketServer = new WebSocket.Server({ server }); - -websocketServer.on('connection', (webSocketClient) => { - webSocketClient.on('message', (message) => { - websocketServer.clients.forEach( client => { - if (webSocketClient!=client){ - client.send(message.toString()); - } - }); + +const { createServer } = require('https'); +const { readFileSync } = require('fs'); +const polka = require('polka'); +const { HTTPS_PORT=3000 } = process.env; +const serve = require('sirv')('vdo.ninja'); +const ws = require("ws"); + +// Run create-localnetwork-cert.sh to generate self-signed certs +const options = { + key: readFileSync('localhost.key'), + cert: readFileSync('localhost.crt') + }; + +const { handler } = polka() + .use(serve) + .get('/health', (req, res) => { + res.end('OK'); + }); + +const { handler2 } = polka() + .use(serve) + .get('/health', (req, res) => { + res.end('OK'); }); -}); -server.listen(443, () => {console.log(`Server started on port 443`) }); + +const httpsServer = createServer(options, handler); + +function wssSetup(wss) { + wss.on('connection', (ws) => { + ws.on('message', (message) => { + wss.clients.forEach( client => { + if (ws!=client){ + client.send(message.toString()); + } + }); + }); + }); +} + +wssSetup(new ws.WebSocketServer({ server: httpsServer })); + +httpsServer.listen(HTTPS_PORT, _ => { + console.log(`> Running on https://:${HTTPS_PORT}`); + console.log(`Update vdo.ninja/index.html 'session.wss=wss://:${HTTPS_PORT}' & 'session.customWSS = true'`) +}) diff --git a/vdo.ninja b/vdo.ninja new file mode 160000 index 0000000..1325da9 --- /dev/null +++ b/vdo.ninja @@ -0,0 +1 @@ +Subproject commit 1325da964a355aab53a137f2e0d6c2333b23a8e0