Files: d2b5693b01842e3376dae3c3cf5c579e03e490c4 / bin.js
4856 bytesRaw
1 | var fs = require('fs') |
2 | var file = require('pull-file') |
3 | var path = require('path') |
4 | var ssbKeys = require('ssb-keys') |
5 | var stringify = require('pull-stringify') |
6 | var open = require('open') |
7 | var home = require('os-homedir')() |
8 | var nonPrivate = require('non-private-ip') |
9 | var muxrpcli = require('muxrpcli') |
10 | var {pull, values, once} = require('pull-stream') |
11 | var toPull = require('stream-to-pull-stream') |
12 | var through = require('pull-through') |
13 | const webresolve = require('ssb-web-resolver') |
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) { |
66 | if (err) console.error('[viewer]', err) |
67 | } |
68 | |
69 | if(req.url == '/') { |
70 | var filePath = path.join(__dirname, 'localhost/build/index.html') |
71 | console.log(filePath) |
72 | |
73 | return pull( |
74 | file(filePath), |
75 | through(function (data) { |
76 | console.log(data.toString()) |
77 | this.queue(data) |
78 | }), |
79 | toPull(res, onError) |
80 | ) |
81 | } |
82 | if(req.url.startsWith('/web/')) { |
83 | return serveWeb(req, res, m[4]) |
84 | } |
85 | if(req.url == '/get-config') { |
86 | return res.end(JSON.stringify(send)) |
87 | } |
88 | if(req.url == '/favicon.ico') { |
89 | return res.end(favicon) |
90 | } else next() |
91 | |
92 | function respond(res, status, message) { |
93 | res.writeHead(status) |
94 | res.end(message) |
95 | } |
96 | |
97 | function serveWeb (req, res, url) { |
98 | var self = this |
99 | var id = decodeURIComponent(url.substr(1)) |
100 | |
101 | var components = url.split('/') |
102 | if (components[0] === '') components.shift() |
103 | if (components[0] === 'web') components.shift() |
104 | components[0] = decodeURIComponent(components[0]) |
105 | |
106 | webresolve(sbot, components, function (err, data) { |
107 | if (err) return respond(res, 404, 'ERROR: ' + err) |
108 | |
109 | |
110 | return pull(once(data), toPull(res)) |
111 | }) |
112 | } |
113 | }) |
114 | } |
115 | }) |
116 | |
117 | open('http://100.115.92.2:' + config.ws.port, {wait: false}) |
118 | |
119 | // start server |
120 | var server = createSbot(config) |
121 | |
122 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
123 | } |
124 | else { |
125 | |
126 | var manifest |
127 | try { |
128 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
129 | } catch (err) { |
130 | throw explain(err, |
131 | 'no manifest file' |
132 | + '- should be generated first time server is run' |
133 | ) |
134 | } |
135 | |
136 | // connect |
137 | require('ssb-client')(config.keys, { |
138 | manifest: manifest, |
139 | port: config.port, |
140 | host: config.host||'localhost', |
141 | caps: config.caps, |
142 | key: config.key || config.keys.id |
143 | }, function (err, rpc) { |
144 | if(err) { |
145 | if (/could not connect/.test(err.message)) { |
146 | console.log('Error: Could not connect to the scuttlebot server.') |
147 | console.log('Use the "server" command to start it.') |
148 | if(config.verbose) throw err |
149 | process.exit(1) |
150 | } |
151 | throw err |
152 | } |
153 | |
154 | // add some extra commands |
155 | manifest.version = 'async' |
156 | manifest.config = 'sync' |
157 | rpc.version = function (cb) { |
158 | console.log(require('./package.json').version) |
159 | cb() |
160 | } |
161 | rpc.config = function (cb) { |
162 | console.log(JSON.stringify(config, null, 2)) |
163 | cb() |
164 | } |
165 | |
166 | // run commandline flow |
167 | muxrpcli(argv, manifest, rpc, config.verbose) |
168 | }) |
169 | } |
170 | |
171 |
Built with git-ssb-web