Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.cert
*.key
.node_modules
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vdo.ninja"]
path = vdo.ninja
url = https://github.com/steveseguin/vdo.ninja.git
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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://<your ip/domain>: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('<path/to/your>.key'),
cert: readFileSync('<path/to/your>.crt')
};
```

5 changes: 5 additions & 0 deletions create-localnetwork-cert.sh
Original file line number Diff line number Diff line change
@@ -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")
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"start": "node server"
},
"dependencies": {
"polka": "0.5.x",
"sirv": "2.0.x",
"ws": "8.4.x"
}
}
85 changes: 45 additions & 40 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://<your-ip>:${HTTPS_PORT}`);
console.log(`Update vdo.ninja/index.html 'session.wss=wss://<your-ip>:${HTTPS_PORT}' & 'session.customWSS = true'`)
})


1 change: 1 addition & 0 deletions vdo.ninja
Submodule vdo.ninja added at 1325da