Compare commits

..

9 Commits
xr ... master

  1. 43
      README.md
  2. 16
      cloudinit
  3. 29
      examples/events.html
  4. 3156
      examples/spasca-offline/engine/index.html
  5. 225
      index.js

@ -0,0 +1,43 @@
# Offtopus, offline-octopus
## Goal
Provide a way to connect local machines to work and tinker with efficiently, building prototypes with resilience in mind.
## Demo
5min recorded demo video https://video.benetou.fr/w/sHE39WSZkQgfPNDdiypfog
Note that XR is optional. Original extract from https://youtu.be/BRjohy0ruAg?t=2061 for Future of Text demo day.
## How does it work
It provides a Web server with endpoints for different functions, including listing machines joining that network, make functions available to that new network of machines, sharing files, etc. Note that the point is to build on top of it, consequently functions as endpoints here are solely examples.
## How to install
1. clone repository
1. install dependancies (no package.json yet) but mostly express missing, i.e `npm i express`
1. run e.g `node .`
1. connect to it locally first, e.g https://localhost:8082 (assumes existing SSL certificates) or https://localhost:8082/routes
Note that the cloudinit file is an example on a brand new machine.
### tested on
- Linux machines, e.g Ubuntu on desktop, RPi Zero
- Android devices using Termux, e.g Quest 1
- iOS via iSH
Most likely partially works on Windows with Linux subsystem. System commands, e.g shutdown, xrandr, etc must be adapted.
### Developing and debugging
- See the console REPL, starting with help()
- see `/routes/json` to connect to all services
## Digging deeper
- Future of Text WebXR demo with shared filesystems https://video.benetou.fr/w/h291CfZiezenY1t46cQQo5 extract from https://www.youtube.com/results?search_query=demo+prototype+session
- hour long video discussion https://video.benetou.fr/w/aR81WVHg6H3E93GPG4jYUg
- self hosting AI notes https://fabien.benetou.fr/Content/SelfHostingArtificialIntelligence
- self hosting subreddit https://old.reddit.com/r/selfhosted/
- small Web https://ar.al/2020/08/07/what-is-the-small-web/
- DWeb https://en.wikipedia.org/wiki/Decentralized_web
### Inspiration
- Designing for serendipity: supporting end-user configuration of ubiquitous computing environments, Xerox PARC 2002
- Providing an Integrated User Experience of Networked Media, Devices, and Services through End-User Composition, 2008

@ -0,0 +1,16 @@
#!/bin/bash
cd /root
curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
source nodesource_setup.sh
apt install -y nodejs
apt update && apt install -y git
git clone https://git.benetou.fr/utopiah/offline-octopus
mkdir Prototypes
mv offline-octopus/ Prototypes/
cd Prototypes/offline-octopus/
npm i express
openssl req -nodes -new -x509 -keyout naf-key.pem -out naf.pem -subj "/C=BE/ST=Brussels/L=Brussels/O=Global Security/OU=IT Department/CN=offtopus.benetou.fr"
touch /root/.ssh/id_rsa_offlineoctopus.pub
touch /root/Prototypes/offline-octopus/.keyfrommd5
HOME=/root /usr/bin/node index.js &>> log.txt

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="https://glitch.com/favicon.ico" />
<title>offtopus events</title>
</head>
<body>
<div id="content"></div>
<hr>
<div id="events"></div>
<script>
const source = new EventSource('/events');
source.addEventListener('message', message => {
console.log('Got', message);
// Display the event data in the `content` div
document.querySelector('#events').innerHTML = event.data;
})
</script>
</body>
</html>

File diff suppressed because it is too large Load Diff

@ -8,10 +8,10 @@ const express = require("express"); // could be good to replace with c
// (worked at least on RPi, Deck, Quest and x64 machines) // (worked at least on RPi, Deck, Quest and x64 machines)
// Get port or default to 8082 // Get port or default to 8082
const port = process.env.PORT || 8082; // assumes homogenity across peers const port = process.env.PORT || 8082;
const protocol = process.env.PROTOCOL || 'https' const protocol = 'https'
const subclass = process.env.SUBCLASS || '192.168.0.' // defaulting to IP used at home rather than RPi0. Could also change it there as it's not justified beside helping distinction. const subclass = '10.160.168.'
// Object.values(require("os").networkInterfaces()).flat().filter(({ family, internal }) => family === "IPv4" && !internal).map(({ address }) => address)[0].split('.').slice(0,3).join('.')+'.' //const subclass = '192.168.4.'
const publicKeyPath = path.resolve(process.env.HOME,'.ssh','id_rsa_offlineoctopus.pub') const publicKeyPath = path.resolve(process.env.HOME,'.ssh','id_rsa_offlineoctopus.pub')
const publicKey = fs.readFileSync(publicKeyPath).toString().split(' ')[1] const publicKey = fs.readFileSync(publicKeyPath).toString().split(' ')[1]
@ -28,12 +28,6 @@ const minfileSaveFullPath = path.join(__dirname,'examples', minfilename)
const sshconfigpath = path.resolve(process.env.HOME,'.ssh','config') const sshconfigpath = path.resolve(process.env.HOME,'.ssh','config')
const propath = path.resolve(process.env.HOME,'Prototypes') const propath = path.resolve(process.env.HOME,'Prototypes')
const sshfsmounts = "sshfsmounts"
let workspaces = {}
let mountPoints = []
// note that stopping this process removes the mounts
// does not apply in a P2P fashion, must rely on local configuration here config.json // does not apply in a P2P fashion, must rely on local configuration here config.json
let localServices = [ ] let localServices = [ ]
const configFilePath = path.resolve(__dirname, "config.json") const configFilePath = path.resolve(__dirname, "config.json")
@ -55,17 +49,6 @@ pmwiki http localhost 4000 /pmwiki.php
sshd see quest in ssh config, specific user and port sshd see quest in ssh config, specific user and port
*/ */
const kwinmin = `
echo "const clients = workspace.clientList();
for (var i = 0; i < clients.length; i++) {
print(clients[i].caption);
clients[i].minimized = true;
}" > /tmp/kwinscriptdemo
num=$(dbus-send --print-reply --dest=org.kde.KWin /Scripting org.kde.kwin.Scripting.loadScript string:"/tmp/kwinscriptdemo" | awk 'END {print $2}' )
dbus-send --print-reply --dest=org.kde.KWin /$num org.kde.kwin.Script.run`
const utilsCmd = { // security risk but for now not accepting user input so safer const utilsCmd = { // security risk but for now not accepting user input so safer
//'update' : { desc: 'note that will lose the state, e.g foundpeers', cmd: 'killall '+process.title+' && ' }, //'update' : { desc: 'note that will lose the state, e.g foundpeers', cmd: 'killall '+process.title+' && ' },
// should first download the new version and proceed only if new // should first download the new version and proceed only if new
@ -78,19 +61,6 @@ const utilsCmd = { // security risk but for now not accepting user input so safe
'listprototypes': { cmd: 'ls', context: {cwd: propath}, 'listprototypes': { cmd: 'ls', context: {cwd: propath},
format: res => res.toString().split('\n') format: res => res.toString().split('\n')
}, },
'mousemove' : { cmd: 'xdotool mousemove ', optionsPattern: /\d+ \d+/},
'mouseup' : { cmd: 'xdotool mouseup 1'},
'mousedown' : { cmd: 'xdotool mousedown 1'},
// per device specific (until adjustable per user)
'minimizeall' : { cmd: kwinmin}, //KWin script
//'minimizeall' : { cmd: '/home/fabien/Prototypes/kwin-scripting/launch'}, //KWin script
'highresscreen' : { cmd: 'xrandr --output DP-4 --mode 3840x2160'},
'lowresscreen' : { cmd: 'xrandr --output DP-4 --mode 1920x1080'},
// 'anyresscreen' : { cmd: 'xrandr --output DP-4 --mode '}, // would require user input which is risky e.g here 'OxO; wget rootkit; bash rootkit;'
'availableRes' : { cmd: 'xrandr -q | grep 0x | grep -v primary | cut -f 4 -d " "',
format: res => res.toString().split('\n').filter( l => l!='' )
},
//'npmfind' : { desc: 'package manager finder', cmd: 'find . -wholename "*node_modules/acorn"' }, //'npmfind' : { desc: 'package manager finder', cmd: 'find . -wholename "*node_modules/acorn"' },
// security risk if relying on user provided name, e.g replacing acorn by user input // security risk if relying on user provided name, e.g replacing acorn by user input
// example that could be generalized to other package managers e.g .deb or opkg // example that could be generalized to other package managers e.g .deb or opkg
@ -98,7 +68,6 @@ const utilsCmd = { // security risk but for now not accepting user input so safe
// could be interesting to consider also recent containers and ~/.bashrc for services // could be interesting to consider also recent containers and ~/.bashrc for services
const instructions = ` const instructions = `
<pre>
/home/deck/.ssh/ /home/deck/.ssh/
trusted context, i.e on closed WiFi and over https with bearer authorization trusted context, i.e on closed WiFi and over https with bearer authorization
/home/deck/.ssh/config /home/deck/.ssh/config
@ -120,7 +89,6 @@ ssh remarkable2 to get drawing preview
util functions util functions
modify WiFi parameters, including AP if available modify WiFi parameters, including AP if available
shutdown/reboot shutdown/reboot
</pre>
` `
const auth_instructions = `generate md5 from pub offline-octopus then provide as bearer query param` const auth_instructions = `generate md5 from pub offline-octopus then provide as bearer query param`
@ -138,19 +106,6 @@ app.use(function(req, res, next) {
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send( instructions ) res.send( instructions )
// see issue 20
})
app.get('/routes', (req, res) => {
let formattedRoutes = routes.map( r => `<li><a href='${r}'>${r}</a></li>` ).join('\n')
res.send( formattedRoutes )
})
app.get('/routes/json', (req, res) => {
// minimalist for JXR but could also be OpenAPI, cf https://git.benetou.fr/utopiah/offline-octopus/issues/16
// used in /webxr as
// fetch('/routes/json').then(r=>r.json()).then(r=>r.filter(p=>p.match(/resolution/)).map(p=>addNewNote('jxr fetch("https://192.168.0.129:8082'+p+'")')))
res.send( routes )
}) })
app.get('/authtestviaheader', (req, res) => { app.get('/authtestviaheader', (req, res) => {
@ -222,8 +177,8 @@ app.get('/exec', (req, res) => {
} }
}) })
function execConfiguredCommand(cmdName, options=''){ function execConfiguredCommand(cmdName){
let resultFromExecution = execSync(utilsCmd[cmdName].cmd+options, utilsCmd[cmdName].context) let resultFromExecution = execSync(utilsCmd[cmdName].cmd, utilsCmd[cmdName].context)
let formatter = utilsCmd[cmdName].format let formatter = utilsCmd[cmdName].format
if (formatter) resultFromExecution = formatter(resultFromExecution) if (formatter) resultFromExecution = formatter(resultFromExecution)
return resultFromExecution return resultFromExecution
@ -265,11 +220,9 @@ app.get('/recentfiles', (req, res) => {
res.json( {msg: 'not yet implemented'}) res.json( {msg: 'not yet implemented'})
}) })
let dynURL = '/' let dynURL = 'https://192.168.4.1/offline.html'
app.get('/hmdlink/set', (req, res) => { // could be a PUT instead app.get('/hmdlink/set', (req, res) => { // could be a PUT instead
// e.g http://192.168.4.3:8082/hmdlink/set?url=http://192.168.4.3:8082/114df5f8-3921-42f0-81e7-48731b563571.thumbnails/f07120ba-0ca1-429d-869f-c704a52b7aa3.png // e.g http://192.168.4.3:8082/hmdlink/set?url=http://192.168.4.3:8082/114df5f8-3921-42f0-81e7-48731b563571.thumbnails/f07120ba-0ca1-429d-869f-c704a52b7aa3.png
// should return a warning if req.query.url is undefined
if (!req.query.url) {res.json( {error: 'use /hmdlink/set?url=YOURURL'}); return}
dynURL = req.query.url dynURL = req.query.url
res.redirect( dynURL ) res.redirect( dynURL )
}) })
@ -279,8 +232,7 @@ app.get('/hmdlink', (req, res) => {
}) })
app.get('/webxr', (req, res) => { app.get('/webxr', (req, res) => {
res.redirect( '/spasca-offline/engine/index.html') res.redirect( '/local-metaverse-tooling/local-aframe-test.html' )
//res.redirect( '/local-metaverse-tooling/local-aframe-test.html' )
}) })
// user for /scan to populate foundPeers // user for /scan to populate foundPeers
@ -299,8 +251,9 @@ app.get('/scan', (req, res) => {
}) })
function scanpeers(){ function scanpeers(){
sendEventsToAll({'action':'scanning started'})
foundPeers = [] foundPeers = []
for (let i=1;i<254;i++){ // async so blasting, gives very quick result for positives for (let i=1;i<25;i++){ // async so blasting, gives very quick result for positives
let url=protocol+'://'+subclass+i+':'+port+'/available' let url=protocol+'://'+subclass+i+':'+port+'/available'
let opt={rejectUnauthorized: false} let opt={rejectUnauthorized: false}
https.get(url, opt, res => { https.get(url, opt, res => {
@ -321,57 +274,20 @@ function scanpeers(){
app.get('/sshconfig', (req, res) => { app.get('/sshconfig', (req, res) => {
res.json( getSshConfig() ) res.json( getSshConfig() )
// should filter on foundPeers to avoid offline peers
}) })
app.get('/sshconfig/live', (req, res) => { // note that stopping this process removes the mounts
res.json( getSshConfig().filter( i => foundPeers.map( e => subclass+e ).filter( x => i.connectionString.match(x) ).length ) )
})
app.get('/unifiedworkspaces', (req, res) => {
res.json( workspaces )
})
function getUnifiedworkspaces(){
mountPoints.map( mp => {
let dir = path.resolve(__dirname, sshfsmounts, mp)
workspaces[mp] = fs.readdirSync( dir )
console.log('reading', mp, workspaces.mp)
})
return workspaces
}
function mountAll(){ function mountAll(){
// should scanpeers() first getSshConfig().map( l => {
if (foundPeers.length==0) return
getSshConfig().filter( i => foundPeers.map( e => subclass+e ).filter( x => i.connectionString.match(x) ).length )
.map( l => {
let cs = 'sshfs ' + l.name + ':' let cs = 'sshfs ' + l.name + ':'
if (l.custom) if (l.custom)
cs+= l.custom cs+= l.custom
else else
cs+='/home/'+l.user cs+='/home/'+l.user
let targetPath = path.resolve(__dirname, sshfsmounts, l.name) return cs + ' ' + path.resolve(__dirname, "sshfsmounts", l.name)
if (!fs.existsSync(targetPath)){ fs.mkdirSync(targetPath, { recursive: true }); }
mountPoints.push(l.name)
return cs + ' ' + targetPath
} )
//.map( l => console.log(l))
.map( l => { try { execSync(l) } catch (err) { console.log(err) } } )
}
function umountAll(){
mpath = path.resolve(__dirname, sshfsmounts)
fs.readdirSync( mpath )
// could rely on mountPoints instead
.map( f => {
try {
execSync('umount ' + path.resolve(__dirname, sshfsmounts, f) )
// should update mountPoints instead
} catch (err) {
console.log(err)
}
} ) } )
mountPoints = [] .map( l => execSync(l))
} }
function getSshConfig(){ function getSshConfig(){
@ -414,17 +330,6 @@ function getSshConfig(){
easier to revoke if need be easier to revoke if need be
*/ */
app.get('/mouse/up', (req, res) => { res.json( execConfiguredCommand('mouseup') ) })
app.get('/mouse/down', (req, res) => { res.json( execConfiguredCommand('mousedown') ) })
app.get('/mouse/move', (req, res) => {
if( !req.query.options?.match(utilsCmd['mousemove']) ) { {res.json( {error: 'use /mouse/move?options=100 2000'}); return}}
res.json( execConfiguredCommand('mousemove',req.query.options) ) }
)
app.get('/minimizeall', (req, res) => { res.json( execConfiguredCommand('minimizeall') ) })
app.get('/resolution/high', (req, res) => { res.json( execConfiguredCommand('highresscreen') ) })
app.get('/resolution/low', (req, res) => { res.json( execConfiguredCommand('lowresscreen') ) })
app.get('/resolution/list', (req, res) => { res.json( execConfiguredCommand('availableRes') ) })
app.get('/localprototypes', (req, res) => { app.get('/localprototypes', (req, res) => {
// examples to disentangle own work for cloned existing repositories : // examples to disentangle own work for cloned existing repositories :
// find Prototypes/ -iwholename */.git/config | xargs grep git.benetou.fr // find Prototypes/ -iwholename */.git/config | xargs grep git.benetou.fr
@ -467,9 +372,6 @@ app.get('/editor/save', (req, res) => {
} }
}) })
let routes = app._router.stack.map( r => r.route?.path ).filter( r => typeof(r) == 'string' )
// it's a form of caching but so far routes are not dynamically generated so sufficient solution
const privateKey = fs.readFileSync("naf-key.pem", "utf8"); const privateKey = fs.readFileSync("naf-key.pem", "utf8");
const certificate = fs.readFileSync("naf.pem", "utf8"); const certificate = fs.readFileSync("naf.pem", "utf8");
const credentials = { key: privateKey, cert: certificate }; const credentials = { key: privateKey, cert: certificate };
@ -479,85 +381,30 @@ const webServer = https.createServer(credentials, app);
// const webServer = http.createServer(app); // const webServer = http.createServer(app);
// Listen on port // Listen on port
webServer.listen(port, () => { webServer.listen(port, () => {
console.log("listening on "+protocol+"://0.0.0.0:" + port) console.log("listening on "+protocol+"://localhost:" + port);
getCommand()
}); });
// REPL testing // SSE from https://www.digitalocean.com/community/tutorials/nodejs-server-sent-events-build-realtime-app
const readline = require("readline"); // adapted from jxr-permanence
const rl = readline.createInterface({ let clients = [];
input: process.stdin,
output: process.stdout, function eventsHandler(request, response, next) {
completer: completer const headers = {
}); 'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
// https://nodejs.org/api/readline.html#use-of-the-completer-function 'Cache-Control': 'no-cache'
function completer(line) { };
const completions = help().split('\n'); response.writeHead(200, headers);
const hits = completions.filter((c) => c.startsWith(line)); const clientId = Date.now();
// Show all completions if none found const newClient = { id: clientId, response };
return [hits.length ? hits : completions, line]; clients.push(newClient);
request.on('close', () => { clients = clients.filter(client => client.id !== clientId); });
} }
let command = '' function sendEventsToAll(data) {
function getCommand(){ // function used to broadcast
rl.question(process.title+" REPL: ", function(command) { clients.forEach(client => client.response.write(`data: ${JSON.stringify(data)}\n\n`))
if (command == "close") {
rl.close();
} else {
try {
console.log(command, eval(command) )
} catch (err) {
console.log(err)
} }
getCommand()
// somehow switch to node REPL proper after?!
}
});
}
function help(){
return `
help()
execConfiguredCommand(cmdName)
getSshConfig()
mountAll()
umountAll()
scanpeers()
getUnifiedworkspaces()
foundPeers
port
protocol
subclass
publicKeyPath
publicKey
md5fromPub
process.title
filename
fileSaveFullPath
minfilename
minfileSaveFullPath
sshconfigpath
propath
localServices
configFilePath
utilsCmd
instructions
auth_instructions
process.title
sshfsmounts
mountPoints
workspaces
`
}
rl.on("close", function() {
console.log("\ndone");
umountAll()
process.exit(0);
});
// Demo Day target : app.get('/events', eventsHandler);
// show files from ~/Prototypes as cubes from ssh mounted on a virtual workspace // for example /events.html shows when /scan begings (but not ends)
// sshfs might not even be needed, see allpeers/exec instead
// wouldn't easily get content back though, just meta data

Loading…
Cancel
Save