git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: bea0d6f86662e5db7348deb37bef697f1226edae

Files: bea0d6f86662e5db7348deb37bef697f1226edae / bin.js

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

Built with git-ssb-web