const fs = require ( "fs" ) ;
const http = require ( "http" ) ;
const https = require ( "https" ) ;
const path = require ( "path" ) ;
const { execSync } = require ( 'child_process' ) ;
const express = require ( "express" ) ; // could be good to replace with code, no dep
// Get port or default to 8082
const port = process . env . PORT || 8082 ;
// Setup and configure Express http server.
const app = express ( ) ;
app . use ( express . static ( path . resolve ( _ _dirname , "." , "examples" ) ) ) ;
const instructions = `
/ h o m e / d e c k / . s s h /
trusted context , i . e on closed WiFi and over https , still . could check as bearer
/ h o m e / d e c k / . s s h / c o n f i g
could limit to known IP range or class e . g cat . ssh / config | grep 192.168 . 4. - C 3
could also re - add new entries rather than extend the format
/ h o m e / d e c k / . s s h / a u t h o r i z e d _ k e y s
/ h o m e / d e c k / . s s h / k n o w n _ h o s t s
/ h o m e / d e c k / . s s h / i d _ r s a _ s t e a m d e c k
/ h o m e / d e c k / . s s h / i d _ r s a _ s t e a m d e c k . p u b
/ h o m e / d e c k / s e r v e r . l o c a t e d b
seems to be plain text with metadata
/ h o m e / d e c k / d e s k t o p . p l o c a t e . d b
seems to be a specific format , binary or maybe compressed
both should be queriable via http with json output
ssh remarkable2 to get drawing preview
conversion should be done , if necessary , on device
for typed text
cat 43 * . rm | sed "s/[^a-zA-z0-9 ]//g" | sed "s/[OT,EC]//g"
util functions
modify WiFi parameters , including AP if available
shutdown / reboot
`
app . get ( '/pwa' , ( req , res ) => {
// for offline use on mobile or VR headset
// should try to sync back when devices connect back
// same when itself connects back to (Internet) own server e.g benetou.fr
// can be cascading or federatede or properly P2P
// responsive programming also, not "just" design
res . redirect ( 'pwa/index.html' )
} )
const utilsCmd = {
'shutdown' : { cmd : 'shutdown -h now' } ,
'listprototypes' : { cmd : 'ls' , context : { cwd : '/home/deck/Prototypes' } ,
// more reliably path.join(__dirname,'')
format : res => res . toString ( ) . split ( '\n' )
} ,
}
app . get ( '/exec' , ( req , res ) => {
if ( ! req . query . cmdName ) {
res . json ( utilsCmd )
} else {
let resultFromExecution = execSync ( utilsCmd [ req . query . cmdName ] . cmd , utilsCmd [ req . query . cmdName ] . context )
let formatter = utilsCmd [ req . query . cmdName ] . format
if ( formatter ) resultFromExecution = formatter ( resultFromExecution )
res . json ( resultFromExecution )
}
} )
// app.get('/interface/unregister', (req, res) => {
// app.get('/interface/register', (req, res) => {
// could be use for e.g reMarkable2, Quest2, etc with specifically accepted or prefered formats
app . get ( '/services/unregister' , ( req , res ) => {
res . json ( { msg : 'not yet implemented' } )
} )
app . get ( '/services/register' , ( req , res ) => {
res . json ( { msg : 'not yet implemented' } )
// example {name:'hmdlink', desc:'share URL between local devices', protocol:'http', port:8082, path: '/hmdlink', url:'http://192.168.4.3:8082/hmdlink'},
} )
app . get ( '/updates' , ( req , res ) => {
// could rely on a git reachable by all peers
// this might be feasible from this very https server as read-only
res . json ( { msg : 'not yet implemented' } )
} )
app . get ( '/recentfiles' , ( req , res ) => {
// e.g lsof | grep home | grep vim | grep -v firefox
// or history | grep vi
// should be available after (ideally local) conversion if needed, e.g rm -> .svg on reMarkable
res . json ( { msg : 'not yet implemented' } )
} )
app . get ( '/services' , ( req , res ) => {
// could be probbed first to check for availability
// should be updated also via register/unregister
res . json ( [
{ name : 'kiwix' , desc : 'offline Wikipedia, Stackoverflow, etc' , protocol : 'http' , port : 8080 , url : 'http://192.168.4.3:8080' } ,
{ name : 'hmdlink' , desc : 'share URL between local devices' , protocol : 'http' , port : 8082 , path : '/hmdlink' , url : 'http://192.168.4.3:8082/hmdlink' } ,
] )
} )
let dynURL = 'https://192.168.4.1/offline.html'
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
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') )
// 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' } )
} )
/ *
* example of finding past local solution , here shiki for syntax highlighting
*
( deck @ steamdeck serverhome ) $ find . - iname shiki
. / fabien / web / future _of _text _demo / content / shiki
( deck @ steamdeck serverhome ) $ ls . / fabien / web / future _of _text _demo / content / shiki / dist /
onig . wasm
see syntax - highlighting branch in SpaSca git repository
in / h o m e / d e c k / s e r v e r h o m e / f a b i e n / w e b / f u t u r e _ o f _ t e x t _ d e m o / e n g i n e /
* /
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 privateKey = fs . readFileSync ( "naf-key.pem" , "utf8" ) ;
const certificate = fs . readFileSync ( "naf.pem" , "utf8" ) ;
const credentials = { key : privateKey , cert : certificate } ;
const webServer = https . createServer ( credentials , app ) ;
// Start Express http server
// const webServer = http.createServer(app);
// Listen on port
webServer . listen ( port , ( ) => {
console . log ( "listening on http://localhost:" + port ) ;
} ) ;
// returns connections string for e.g sshfs
function loadSshConfig ( ) {
// here assume user deck
let txt = f . readFileSync ( '/home/deck/.ssh/config' ) . toString ( )
return txt . split ( 'Host ' )
. filter ( m => m . match ( '192.168.4.' ) )
. map ( c => {
p = c . replaceAll ( ' ' , '' )
. split ( '\n' )
. filter ( i => i . match ( /^[a-zA-Z]/ ) ) ;
return p . filter ( pm => pm . match ( 'User ' ) ) [ 0 ] . replace ( 'User ' , '' ) +
p . filter ( pm => pm . match ( 'HostName' ) ) [ 0 ] . replace ( 'HostName ' , '@' )
} )
}
/ *
e . g AFTER mounting
f . readdirSync ( './sshfsmounts/' ) . map ( d => f . readdirSync ( './sshfsmounts/' + d ) )
location : / h o m e / d e c k / P r o t o t y p e s / o f f l i n e - o c t o p u s / s s h f s m o u n t s
sshfs remarkable2 : / h o m e / r o o t / x o c h i t l - d a t a / r e m a r k a b l e 2 /
works if available
sshfs fabien @ 192.168 . 4.1 : / h o m e / f a b i e n / r p i 0 /
still prompts for password , need manual login
ls rpi0 /
ls remarkable2 /
* /