const path = require('path') const fs = require('fs') const file = require('pull-file') const ssbKeys = require('ssb-keys') const stringify = require('pull-stringify') const open = require('open') const home = require('os-homedir')() const nonPrivate = require('non-private-ip') const muxrpcli = require('muxrpcli') const {pull, values, once} = require('pull-stream') const toPull = require('stream-to-pull-stream') const webresolve = require('ssb-web-resolver') var SEC = 1e3 var MIN = 60*SEC var network = process.env.ssb_appname || 'ssb' var config = require('./config/inject')(network) var urlIdRegex = /^(?:\/(([%&@]|%25|%26|%40)(?:[A-Za-z0-9\/+]|%2[Ff]|%2[Bb]){43}(?:=|%3[Dd])\.(?:sha256|ed25519))(?:\.([^?]*))?|(\/.*?))(?:\?(.*))?$/ config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) var favicon = fs.readFileSync(path.join('./public/favicon.ico')) var manifestFile = path.join(config.path, 'manifest.json') var argv = process.argv.slice(2) var i = argv.indexOf('--') var conf = argv.slice(i+1) argv = ~i ? argv.slice(0, i) : argv if (argv[0] == 'start') { var createSbot = require('./') createSbot .use(require('./plugins/master')) .use(require('./plugins/local')) .use(require('ssb-replicate')) .use(require('ssb-invite')) .use(require('ssb-friends')) .use(require('ssb-gossip')) .use(require('ssb-blobs')) .use(require('ssb-backlinks')) .use(require('ssb-query')) .use(require('ssb-links')) .use(require('ssb-ebt')) .use(require('ssb-search')) .use(require('ssb-ws')) .use({ name: 'serve', version: '1.0.0', init: function (sbot) { sbot.ws.use(function (req, res, next) { var send = config delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast send.address = 'ws://100.115.92.2:8989~shs:VelntasZy86CuIihzSpkzPvIOYgyu3FO3NZww/UOirk=' var m = urlIdRegex.exec(req.url) function onError(err) { if (err) console.error('[viewer]', err) } if(req.url == '/') { var filePath = path.join(__dirname, 'localhost/build/index.html') return pull(file(filePath), toPull(res, onError)) } if(req.url == '/mvd') { var filePath = path.join(__dirname, 'build/index.html') return pull(file(filePath), toPull(res, onError)) } if(req.url.startsWith('/web/')) { return serveWeb(req, res, m[4]) } if(req.url == '/get-config') { return res.end(JSON.stringify(send)) } if(req.url == '/favicon.ico') { return res.end(favicon) } else next() function respond(res, status, message) { res.writeHead(status) res.end(message) } function serveWeb (req, res, url) { var self = this var id = decodeURIComponent(url.substr(1)) var components = url.split('/') if (components[0] === '') components.shift() if (components[0] === 'web') components.shift() components[0] = decodeURIComponent(components[0]) webresolve(sbot, components, function (err, data) { if (err) return respond(res, 404, 'ERROR: ' + err) return pull(once(data), toPull(res)) }) } }) } }) open('http://100.115.92.2:' + config.ws.port, {wait: false}) // start server var server = createSbot(config) fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) } else { var manifest try { manifest = JSON.parse(fs.readFileSync(manifestFile)) } catch (err) { throw explain(err, 'no manifest file' + '- should be generated first time server is run' ) } // connect require('ssb-client')(config.keys, { manifest: manifest, port: config.port, host: config.host||'localhost', caps: config.caps, key: config.key || config.keys.id }, function (err, rpc) { if(err) { if (/could not connect/.test(err.message)) { console.log('Error: Could not connect to the scuttlebot server.') console.log('Use the "server" command to start it.') if(config.verbose) throw err process.exit(1) } throw err } // add some extra commands manifest.version = 'async' manifest.config = 'sync' rpc.version = function (cb) { console.log(require('./package.json').version) cb() } rpc.config = function (cb) { console.log(JSON.stringify(config, null, 2)) cb() } // run commandline flow muxrpcli(argv, manifest, rpc, config.verbose) }) }