Files: 5285e716749369d47e938077f6d87389eb282ba9 / bin.js
5957 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-search')) |
51 | .use(require('ssb-ws')) |
52 | .use({ |
53 | name: 'serve', |
54 | version: '1.0.0', |
55 | init: function (sbot) { |
56 | sbot.ws.use(function (req, res, next) { |
57 | var send = config |
58 | |
59 | delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast |
60 | |
61 | send.address = 'ws://100.115.92.2:8989~shs:VelntasZy86CuIihzSpkzPvIOYgyu3FO3NZww/UOirk=' |
62 | |
63 | var m = urlIdRegex.exec(req.url) |
64 | |
65 | function onError(err) { if (err) console.error('ERROR', err) } |
66 | |
67 | if(req.url == '/') { |
68 | var filePath = path.join(__dirname, 'localhost/build/index.html') |
69 | |
70 | return pull(file(filePath), toPull(res, onError)) |
71 | } |
72 | if(req.url == '/mvd') { |
73 | var filePath = path.join(__dirname, 'mvd/build/index.html') |
74 | |
75 | return pull(file(filePath), toPull(res, onError)) |
76 | } |
77 | if(req.url.startsWith('/web/')) { |
78 | return serveWeb(req, res, m[4]) |
79 | } |
80 | if(req.url == '/get-config') { |
81 | return res.end(JSON.stringify(send)) |
82 | } |
83 | if(req.url == '/favicon.ico') { |
84 | return res.end(favicon) |
85 | } else next() |
86 | |
87 | function respond(res, status, message) { |
88 | res.writeHead(status) |
89 | res.end(message) |
90 | } |
91 | |
92 | function serveWeb (req, res, url) { |
93 | var self = this |
94 | var id = decodeURIComponent(url.substr(1)) |
95 | var components = url.split('/') |
96 | |
97 | if (components[0] === '') components.shift() |
98 | if (components[0] === 'web') components.shift() |
99 | |
100 | components[0] = decodeURIComponent(components[0]) |
101 | |
102 | var initId = components[0] |
103 | var pathArray = [...components].slice(1) |
104 | var restOfPath = pathArray[0] ? pathArray.join('/') + '/' : '' |
105 | |
106 | webresolve(sbot, components, function (err, data) { |
107 | if (err) return respond(res, 404, 'ERROR: ' + err) |
108 | |
109 | if (data.slice(2, 7).toString() === 'links') { |
110 | const dir = JSON.parse(data) |
111 | const entries = Object.keys(dir.links).map(key => { |
112 | const urlPath = '/web/' |
113 | + encodeURIComponent(initId) |
114 | + '/' |
115 | + restOfPath |
116 | + key |
117 | |
118 | return h('div', [ |
119 | h('img', { |
120 | src: (key.indexOf('.') > -1) |
121 | ? '/img/emoji/page_with_curl.png' |
122 | : '/img/emoji/file_folder.png', |
123 | style: { |
124 | width: '15px', |
125 | height: 'auto' |
126 | } |
127 | }), |
128 | h('a', {href: urlPath}, key) |
129 | ]) |
130 | }) |
131 | |
132 | var html = h('div', entries) |
133 | |
134 | return pull(once(html.outerHTML), toPull(res, onError)) |
135 | } |
136 | return pull(once(data), toPull(res)) |
137 | }) |
138 | } |
139 | }) |
140 | } |
141 | }) |
142 | |
143 | open('http://100.115.92.2:' + config.ws.port, {wait: false}) |
144 | |
145 | // start server |
146 | var server = createSbot(config) |
147 | |
148 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
149 | } else { |
150 | |
151 | var manifest |
152 | try { |
153 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
154 | } catch (err) { |
155 | throw explain(err, |
156 | 'no manifest file' |
157 | + '- should be generated first time server is run' |
158 | ) |
159 | } |
160 | |
161 | // connect |
162 | require('ssb-client')(config.keys, { |
163 | manifest: manifest, |
164 | port: config.port, |
165 | host: config.host||'localhost', |
166 | caps: config.caps, |
167 | key: config.key || config.keys.id |
168 | }, function (err, rpc) { |
169 | if(err) { |
170 | if (/could not connect/.test(err.message)) { |
171 | console.log('Error: Could not connect to the scuttlebot server.') |
172 | console.log('Use the "server" command to start it.') |
173 | if(config.verbose) throw err |
174 | process.exit(1) |
175 | } |
176 | throw err |
177 | } |
178 | |
179 | // add some extra commands |
180 | manifest.version = 'async' |
181 | manifest.config = 'sync' |
182 | rpc.version = function (cb) { |
183 | console.log(require('./package.json').version) |
184 | cb() |
185 | } |
186 | rpc.config = function (cb) { |
187 | console.log(JSON.stringify(config, null, 2)) |
188 | cb() |
189 | } |
190 | |
191 | // run commandline flow |
192 | muxrpcli(argv, manifest, rpc, config.verbose) |
193 | }) |
194 | } |
195 | |
196 |
Built with git-ssb-web