@ -9,8 +9,9 @@ const express = require("express"); // could be good to replace with c
// Get port or default to 8082
// Get port or default to 8082
const port = process . env . PORT || 8082 ;
const port = process . env . PORT || 8082 ;
const protocol = process . env . PROTOCOL || 'https'
const protocol = 'https'
const subclass = process . env . SUBCLASS || '192.168.4.'
const subclass = '10.160.168.'
//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 ]
@ -27,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" )
@ -237,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
@ -257,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 => {
@ -279,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 ( ) {
@ -423,86 +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 + "://localhost:" + port )
console . log ( "listening on " + protocol + "://localhost:" + port ) ;
getCommand ( )
} ) ;
// REPL testing
const readline = require ( "readline" ) ;
const rl = readline . createInterface ( {
input : process . stdin ,
output : process . stdout ,
completer : completer
} ) ;
} ) ;
// https://nodejs.org/api/readline.html#use-of-the-completer-function
// SSE from https://www.digitalocean.com/community/tutorials/nodejs-server-sent-events-build-realtime-app
function completer ( line ) {
// adapted from jxr-permanence
const completions = help ( ) . split ( '\n' ) ;
let clients = [ ] ;
const hits = completions . filter ( ( c ) => c . startsWith ( line ) ) ;
// Show all completions if none found
function eventsHandler ( request , response , next ) {
return [ hits . length ? hits : completions , line ] ;
const headers = {
}
'Content-Type' : 'text/event-stream' ,
'Connection' : 'keep-alive' ,
let command = ''
'Cache-Control' : 'no-cache'
function getCommand ( ) {
} ;
rl . question ( process . title + " REPL: " , function ( command ) {
response . writeHead ( 200 , headers ) ;
if ( command == "close" ) {
const clientId = Date . now ( ) ;
rl . close ( ) ;
const newClient = { id : clientId , response } ;
} else {
clients . push ( newClient ) ;
try {
request . on ( 'close' , ( ) => { clients = clients . filter ( client => client . id !== clientId ) ; } ) ;
console . log ( command , eval ( command ) )
} catch ( err ) {
console . log ( err )
}
getCommand ( )
// somehow switch to node REPL proper after?!
}
} ) ;
}
}
function help ( ) {
function sendEventsToAll ( data ) {
return `
// function used to broadcast
help ( )
clients . forEach ( client => client . response . write ( ` data: ${ JSON . stringify ( data ) } \n \n ` ) )
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 ( ) {
app . get ( '/events' , eventsHandler ) ;
console . log ( "\ndone" ) ;
// for example /events.html shows when /scan begings (but not ends)
umountAll ( )
process . exit ( 0 ) ;
} ) ;
// Demo Day target :
// show files from ~/Prototypes as cubes from ssh mounted on a virtual workspace
// sshfs might not even be needed, see allpeers/exec instead
// wouldn't easily get content back though, just meta data