git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 90dfad001a91b412a745a9cad0dccf4a3ef49dc2

Files: 90dfad001a91b412a745a9cad0dccf4a3ef49dc2 / bin.js

4783 bytesRaw
1const path = require('path')
2const fs = require('fs')
3const file = require('pull-file')
4const ssbKeys = require('ssb-keys')
5const stringify = require('pull-stringify')
6const open = require('open')
7const home = require('os-homedir')()
8const nonPrivate = require('non-private-ip')
9const muxrpcli = require('muxrpcli')
10const {pull, values, once} = require('pull-stream')
11const toPull = require('stream-to-pull-stream')
12const webresolve = require('ssb-web-resolver')
13
14var SEC = 1e3
15var MIN = 60*SEC
16
17var network = process.env.ssb_appname || 'ssb'
18var config = require('./config/inject')(network)
19
20var urlIdRegex = /^(?:\/(([%&@]|%25|%26|%40)(?:[A-Za-z0-9\/+]|%2[Ff]|%2[Bb]){43}(?:=|%3[Dd])\.(?:sha256|ed25519))(?:\.([^?]*))?|(\/.*?))(?:\?(.*))?$/
21
22config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
23
24var favicon = fs.readFileSync(path.join('./public/favicon.ico'))
25
26var manifestFile = path.join(config.path, 'manifest.json')
27
28var argv = process.argv.slice(2)
29var i = argv.indexOf('--')
30var conf = argv.slice(i+1)
31argv = ~i ? argv.slice(0, i) : argv
32
33if (argv[0] == 'start') {
34
35 var createSbot = require('./')
36
37 createSbot
38 .use(require('./plugins/master'))
39 .use(require('./plugins/local'))
40 .use(require('ssb-replicate'))
41 .use(require('ssb-invite'))
42 .use(require('ssb-friends'))
43 .use(require('ssb-gossip'))
44 .use(require('ssb-blobs'))
45 .use(require('ssb-backlinks'))
46 .use(require('ssb-query'))
47 .use(require('ssb-links'))
48 .use(require('ssb-ebt'))
49 .use(require('ssb-search'))
50 .use(require('ssb-ws'))
51 .use({
52 name: 'serve',
53 version: '1.0.0',
54 init: function (sbot) {
55 sbot.ws.use(function (req, res, next) {
56 var send = config
57
58 delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast
59
60 send.address = 'ws://100.115.92.2:8989~shs:VelntasZy86CuIihzSpkzPvIOYgyu3FO3NZww/UOirk='
61
62 var m = urlIdRegex.exec(req.url)
63
64 function onError(err) { if (err) console.error('[viewer]', err) }
65
66 if(req.url == '/') {
67 var filePath = path.join(__dirname, 'localhost/build/index.html')
68
69 return pull(file(filePath), toPull(res, onError))
70 }
71 if(req.url == '/mvd') {
72 var filePath = path.join(__dirname, 'build/index.html')
73
74 return pull(file(filePath), toPull(res, onError))
75 }
76 if(req.url.startsWith('/web/')) {
77 return serveWeb(req, res, m[4])
78 }
79 if(req.url == '/get-config') {
80 return res.end(JSON.stringify(send))
81 }
82 if(req.url == '/favicon.ico') {
83 return res.end(favicon)
84 } else next()
85
86 function respond(res, status, message) {
87 res.writeHead(status)
88 res.end(message)
89 }
90
91 function serveWeb (req, res, url) {
92 var self = this
93 var id = decodeURIComponent(url.substr(1))
94
95 var components = url.split('/')
96 if (components[0] === '') components.shift()
97 if (components[0] === 'web') components.shift()
98 components[0] = decodeURIComponent(components[0])
99
100 webresolve(sbot, components, function (err, data) {
101 if (err) return respond(res, 404, 'ERROR: ' + err)
102
103
104 return pull(once(data), toPull(res))
105 })
106 }
107 })
108 }
109 })
110
111 open('http://100.115.92.2:' + config.ws.port, {wait: false})
112
113 // start server
114 var server = createSbot(config)
115
116 fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
117} else {
118
119 var manifest
120 try {
121 manifest = JSON.parse(fs.readFileSync(manifestFile))
122 } catch (err) {
123 throw explain(err,
124 'no manifest file'
125 + '- should be generated first time server is run'
126 )
127 }
128
129 // connect
130 require('ssb-client')(config.keys, {
131 manifest: manifest,
132 port: config.port,
133 host: config.host||'localhost',
134 caps: config.caps,
135 key: config.key || config.keys.id
136 }, function (err, rpc) {
137 if(err) {
138 if (/could not connect/.test(err.message)) {
139 console.log('Error: Could not connect to the scuttlebot server.')
140 console.log('Use the "server" command to start it.')
141 if(config.verbose) throw err
142 process.exit(1)
143 }
144 throw err
145 }
146
147 // add some extra commands
148 manifest.version = 'async'
149 manifest.config = 'sync'
150 rpc.version = function (cb) {
151 console.log(require('./package.json').version)
152 cb()
153 }
154 rpc.config = function (cb) {
155 console.log(JSON.stringify(config, null, 2))
156 cb()
157 }
158
159 // run commandline flow
160 muxrpcli(argv, manifest, rpc, config.verbose)
161 })
162}
163
164

Built with git-ssb-web