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