From c4bdda02aaf9203a63fc334266afce5b579032fb Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Thu, 4 May 2023 07:43:13 +0200 Subject: [PATCH] p2p scan/avail and editor basis core --- index.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 7c93134..ec2fbe2 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,11 @@ -// Load required modules -// note that for WebXR, e.g SpaSca, https is required, even if using own certifcate - // see how it's done for offline NAF -const http = require("http"); // http server core module +const fs = require("fs"); +const http = require("http"); +const https = require("https"); const path = require("path"); -const express = require("express"); // web framework external module const {execSync} = require('child_process'); +const express = require("express"); // could be good to replace with code, no dep -// Get port or default to 8080 +// Get port or default to 8082 const port = process.env.PORT || 8082; // Setup and configure Express http server. @@ -91,21 +90,73 @@ app.get('/hmdlink/set', (req, res) => { // could be a PUT instead dynURL = req.query.url res.redirect( dynURL ) }) +app.get('/webxr', (req, res) => { + res.redirect( '/local-metaverse-tooling/local-aframe-test.html' ) +}) app.get('/hmdlink', (req, res) => { res.redirect( dynURL ) }) app.get('/json', (req, res) => { res.json( instructions.split('\n') ) }) +app.get('/available', (req, res) => { + res.json( true ) +}) +app.get('/foundpeers', (req, res) => { + res.json( foundPeers ) +}) +let foundPeers = [] +app.get('/scan', (req, res) => { + for (let i=1;i<25;i++){ // async so blasting, gives very quick result for positives + let url='https://192.168.4.'+i+':8082/available' + let opt={rejectUnauthorized: false} + https.get(url, opt, res => { + let data = ''; + res.on('data', chunk => { + data += chunk; + }); + res.on('end', () => { + data = JSON.parse(data); + foundPeers.push(i) + // could also register there and then + }) + }).on('error', err => { + //console.log(err.message); usually ECONNREFUSED or EHOSTUNREACH + }).end() + } + res.json( {msg: 'started'} ) // could redirect('/foundpeers') too +}) app.get('/', (req, res) => { res.send( instructions ) }) app.get('/localprototypes', (req, res) => { - res.json( spawn('find Prototypes/ -iwholename */.git/config | xargs grep git.benetou.fr') ) + // res.json( spawn('find Prototypes/ -iwholename */.git/config | xargs grep git.benetou.fr') ) + // should use execSync now +}) +app.get('/editor/recover', (req, res) => { + // could move the previous file with time stamp + fs.copyFileSync(minfileSaveFullPath, fileSaveFullPath ) + res.json( {msg: 'copied'} ) +}) +let filename = 'editor.html' +let fileSaveFullPath = path.join(__dirname,'examples', filename) +let minfilename = 'editor.html.minimal' +let minfileSaveFullPath = path.join(__dirname,'examples', minfilename) +app.get('/editor/read', (req, res) => { + content = fs.readFileSync(fileSaveFullPath ).toString() + res.json( {msg: content} ) +}) +app.get('/editor/save', (req, res) => { + let content = req.query.content // does not escape, loses newlines + if (!content){ + res.json( {msg: 'missing content'} ) + } else { + console.log('writting', content) + fs.writeFileSync(fileSaveFullPath, content) + res.json( {msg: 'written to '+fileSaveFullPath} ) + } }) -const https = require("https"); -const fs = require("fs"); const privateKey = fs.readFileSync("naf-key.pem", "utf8"); const certificate = fs.readFileSync("naf.pem", "utf8"); const credentials = { key: privateKey, cert: certificate }; @@ -117,3 +168,15 @@ const webServer = https.createServer(credentials, app); webServer.listen(port, () => { console.log("listening on http://localhost:" + port); }); +/* + e.g AFTER mounting + f.readdirSync('./sshfsmounts/').map( d=>f.readdirSync('./sshfsmounts/'+d) ) + location : /home/deck/Prototypes/offline-octopus/sshfsmounts + sshfs remarkable2:/home/root/xochitl-data/ remarkable2/ + works if available + sshfs fabien@192.168.4.1:/home/fabien/ rpi0/ + still prompts for password, need manual login + ls rpi0/ + ls remarkable2/ + */ +