bin.jsView |
---|
1 | | -var fs = require('fs') |
2 | | -var path = require('path') |
3 | | -var ssbKeys = require('ssb-keys') |
4 | | -var stringify = require('pull-stringify') |
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +var fs = require('fs') |
| 4 | +var path = require('path') |
| 5 | +var pull = require('pull-stream') |
| 6 | +var toPull = require('stream-to-pull-stream') |
| 7 | +var File = require('pull-file') |
| 8 | +var explain = require('explain-error') |
| 9 | +var ssbKeys = require('ssb-keys') |
| 10 | +var stringify = require('pull-stringify') |
| 11 | +var createHash = require('multiblob/util').createHash |
| 12 | +var minimist = require('minimist') |
| 13 | +var muxrpcli = require('muxrpcli') |
| 14 | +var cmdAliases = require('scuttlebot/lib/cli-cmd-aliases') |
| 15 | +var ProgressBar = require('scuttlebot/lib/progress') |
| 16 | +var packageJson = require('scuttlebot/package.json') |
5 | 17 | var open = require('opn') |
6 | | -var home = require('os-homedir')() |
7 | | -var nonPrivate = require('non-private-ip') |
8 | | -var muxrpcli = require('muxrpcli') |
9 | 18 | |
10 | | -var SEC = 1e3 |
11 | | -var MIN = 60*SEC |
| 19 | + |
| 20 | + |
| 21 | +var argv = process.argv.slice(2) |
| 22 | +var i = argv.indexOf('--') |
| 23 | +var conf = argv.slice(i+1) |
| 24 | +argv = ~i ? argv.slice(0, i) : argv |
12 | 25 | |
13 | | -var config = require('./config/inject')() |
| 26 | +var config = require('./config/inject')(process.env.ssb_appname, minimist(conf)) |
14 | 27 | |
15 | | -config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
| 28 | +var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
| 29 | +if(keys.curve === 'k256') |
| 30 | + throw new Error('k256 curves are no longer supported,'+ |
| 31 | + 'please delete' + path.join(config.path, 'secret')) |
16 | 32 | |
17 | | -var mvdClient = fs.readFileSync(path.join('./build/index.html')) |
| 33 | +var compiledClient = fs.readFileSync(path.join('./build/index.html')) |
18 | 34 | |
19 | 35 | var manifestFile = path.join(config.path, 'manifest.json') |
20 | 36 | |
21 | | -var argv = process.argv.slice(2) |
22 | | -var i = argv.indexOf('--') |
23 | | -var conf = argv.slice(i+1) |
24 | | -argv = ~i ? argv.slice(0, i) : argv |
| 37 | +if (argv[0] == 'server') { |
| 38 | + console.log(packageJson.name, packageJson.version, config.path, 'logging.level:'+config.logging.level) |
| 39 | + console.log('my key ID:', keys.public) |
25 | 40 | |
26 | | -if (argv[0] == 'server') { |
27 | | - |
| 41 | + |
| 42 | + |
| 43 | + |
28 | 44 | var createSbot = require('scuttlebot') |
| 45 | + .use(require('scuttlebot/plugins/unix-socket')) |
| 46 | + .use(require('scuttlebot/plugins/no-auth')) |
| 47 | + .use(require('scuttlebot/plugins/plugins')) |
29 | 48 | .use(require('scuttlebot/plugins/master')) |
30 | 49 | .use(require('scuttlebot/plugins/gossip')) |
31 | 50 | .use(require('scuttlebot/plugins/replicate')) |
32 | 51 | .use(require('ssb-friends')) |
33 | 52 | .use(require('ssb-blobs')) |
34 | | - .use(require('ssb-backlinks')) |
| 53 | + .use(require('scuttlebot/plugins/invite')) |
| 54 | + .use(require('scuttlebot/plugins/local')) |
| 55 | + .use(require('scuttlebot/plugins/logging')) |
35 | 56 | .use(require('ssb-query')) |
36 | 57 | .use(require('./mvd-indexes')) |
37 | 58 | .use(require('ssb-links')) |
| 59 | + .use(require('ssb-ws')) |
38 | 60 | .use(require('ssb-ebt')) |
39 | | - .use(require('ssb-search')) |
40 | | - .use(require('scuttlebot/plugins/invite')) |
41 | | - .use(require('scuttlebot/plugins/local')) |
42 | | - .use(require('decent-ws')) |
43 | 61 | .use({ |
44 | 62 | name: 'serve', |
45 | 63 | version: '1.0.0', |
46 | 64 | init: function (sbot) { |
47 | 65 | sbot.ws.use(function (req, res, next) { |
48 | 66 | var send = config |
49 | | - delete send.keys |
50 | | - send.address = sbot.ws.getAddress() |
51 | | - sbot.invite.create({modern: true}, function (err, cb) { |
| 67 | + delete send.keys |
| 68 | + |
52 | 69 | send.invite = cb |
53 | | - }) |
| 70 | + })*/ |
| 71 | + send.address = sbot.getAddress() |
54 | 72 | if(req.url == '/') |
55 | | - res.end(mvdClient) |
| 73 | + res.end(compiledClient) |
56 | 74 | if(req.url == '/get-config') |
57 | 75 | res.end(JSON.stringify(send)) |
58 | 76 | else next() |
59 | 77 | }) |
60 | 78 | } |
61 | 79 | }) |
62 | | - |
| 80 | + |
| 81 | + require('scuttlebot/plugins/plugins').loadUserPlugins(createSbot, config) |
| 82 | + |
| 83 | + |
| 84 | + |
63 | 85 | open('http://localhost:' + config.ws.port, {wait: false}) |
64 | | - |
| 86 | + |
| 87 | + config.keys = keys |
65 | 88 | var server = createSbot(config) |
66 | | - |
| 89 | + |
| 90 | + |
67 | 91 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
| 92 | + |
| 93 | + if(process.stdout.isTTY && (config.logging.level != 'info')) |
| 94 | + ProgressBar(server.progress) |
68 | 95 | } else { |
69 | 96 | |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
70 | 101 | var manifest |
71 | 102 | try { |
72 | 103 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
73 | 104 | } catch (err) { |
77 | 108 | ) |
78 | 109 | } |
79 | 110 | |
80 | 111 | |
81 | | - require('ssb-client')(config.keys, { |
| 112 | + require('ssb-client')(keys, { |
82 | 113 | manifest: manifest, |
83 | 114 | port: config.port, |
84 | 115 | host: config.host||'localhost', |
85 | 116 | caps: config.caps, |
86 | | - key: config.key || config.keys.id |
| 117 | + key: config.key || keys.id |
87 | 118 | }, function (err, rpc) { |
88 | 119 | if(err) { |
89 | 120 | if (/could not connect/.test(err.message)) { |
90 | | - console.log('Error: Could not connect to the scuttlebot server.') |
91 | | - console.log('Use the "server" command to start it.') |
| 121 | + var serverAddr = (config.host || 'localhost') + ":" + config.port; |
| 122 | + console.error('Error: Could not connect to the scuttlebot server ' + serverAddr) |
| 123 | + console.error('Use the "server" command to start it.') |
92 | 124 | if(config.verbose) throw err |
93 | 125 | process.exit(1) |
94 | 126 | } |
95 | 127 | throw err |
96 | 128 | } |
97 | 129 | |
| 130 | + |
| 131 | + for (var k in cmdAliases) { |
| 132 | + rpc[k] = rpc[cmdAliases[k]] |
| 133 | + manifest[k] = manifest[cmdAliases[k]] |
| 134 | + } |
| 135 | + |
98 | 136 | |
99 | | - manifest.version = 'async' |
| 137 | + |
100 | 138 | manifest.config = 'sync' |
101 | | - rpc.version = function (cb) { |
102 | | - console.log(require('./package.json').version) |
103 | | - cb() |
104 | | - } |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
105 | 143 | rpc.config = function (cb) { |
106 | 144 | console.log(JSON.stringify(config, null, 2)) |
107 | 145 | cb() |
108 | 146 | } |
109 | 147 | |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + if (process.argv[2] === 'blobs.add') { |
| 153 | + var filename = process.argv[3] |
| 154 | + var source = |
| 155 | + filename ? File(process.argv[3]) |
| 156 | + : !process.stdin.isTTY ? toPull.source(process.stdin) |
| 157 | + : (function () { |
| 158 | + console.error('USAGE:') |
| 159 | + console.error(' blobs.add <filename> # add a file') |
| 160 | + console.error(' source | blobs.add # read from stdin') |
| 161 | + process.exit(1) |
| 162 | + })() |
| 163 | + var hasher = createHash('sha256') |
| 164 | + pull( |
| 165 | + source, |
| 166 | + hasher, |
| 167 | + rpc.blobs.add(function (err) { |
| 168 | + if (err) |
| 169 | + throw err |
| 170 | + console.log('&'+hasher.digest) |
| 171 | + process.exit() |
| 172 | + }) |
| 173 | + ) |
| 174 | + return |
| 175 | + } |
| 176 | + |
110 | 177 | |
111 | 178 | muxrpcli(argv, manifest, rpc, config.verbose) |
112 | 179 | }) |
113 | 180 | } |