Files: fa73006e36dea91f38d49312b44f87bcb421bd08 / bin.js
5149 bytesRaw
1 | |
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('./lib/cli-cmd-aliases') |
15 | var ProgressBar = require('./lib/progress') |
16 | var packageJson = require('./package.json') |
17 | |
18 | //get config as cli options after --, options before that are |
19 | //options to the command. |
20 | var argv = process.argv.slice(2) |
21 | var i = argv.indexOf('--') |
22 | var conf = argv.slice(i+1) |
23 | argv = ~i ? argv.slice(0, i) : argv |
24 | |
25 | var network = 'ssb' |
26 | //var network = 'decent' |
27 | //var network = 'testnet' |
28 | |
29 | var config = require('./config/inject')(network) |
30 | |
31 | var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
32 | if(keys.curve === 'k256') |
33 | throw new Error('k256 curves are no longer supported,'+ |
34 | 'please delete' + path.join(config.path, 'secret')) |
35 | |
36 | var manifestFile = path.join(config.path, 'manifest.json') |
37 | |
38 | if (argv[0] == 'server') { |
39 | console.log('my key ID:', keys.public) |
40 | |
41 | // special server command: |
42 | // import sbot and start the server |
43 | |
44 | var createSbot = require('./') |
45 | .use(require('./plugins/plugins')) |
46 | .use(require('./plugins/master')) |
47 | .use(require('./plugins/gossip')) |
48 | .use(require('./plugins/replicate')) |
49 | .use(require('ssb-friends')) |
50 | .use(require('ssb-blobs')) |
51 | .use(require('./plugins/invite')) |
52 | .use(require('./plugins/local')) |
53 | .use(require('./plugins/logging')) |
54 | .use(require('./query')) |
55 | .use(require('ssb-links')) |
56 | .use(require('ssb-search')) |
57 | .use(require('ssb-backlinks')) |
58 | .use(require('./decent-ws')) |
59 | .use(require('ssb-ebt')) |
60 | .use({ |
61 | name: 'serve', |
62 | version: '1.0.0', |
63 | init: function (sbot) { |
64 | sbot.ws.use(function (req, res, next) { |
65 | res.setHeader('Access-Control-Allow-Origin', '*') |
66 | var send = config |
67 | delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast |
68 | send.address = sbot.ws.getAddress() |
69 | sbot.invite.create({modern: true}, function (err, cb) { |
70 | send.invite = cb |
71 | }) |
72 | //if(req.url == '/') |
73 | // res.end(mvdClient) |
74 | if(req.url == '/get-config') |
75 | res.end(JSON.stringify(send)) |
76 | else next() |
77 | }) |
78 | } |
79 | }) |
80 | |
81 | |
82 | // add third-party plugins |
83 | //require('./plugins/plugins').loadUserPlugins(createSbot, config) |
84 | |
85 | // start server |
86 | |
87 | config.keys = keys |
88 | var server = createSbot(config) |
89 | |
90 | // write RPC manifest to ~/.ssb/manifest.json |
91 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
92 | |
93 | } else { |
94 | |
95 | // normal command: |
96 | // create a client connection to the server |
97 | |
98 | // read manifest.json |
99 | var manifest |
100 | try { |
101 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
102 | } catch (err) { |
103 | throw explain(err, |
104 | 'no manifest file' |
105 | + '- should be generated first time server is run' |
106 | ) |
107 | } |
108 | |
109 | // connect |
110 | require('ssb-client')(keys, { |
111 | manifest: manifest, |
112 | port: config.port, |
113 | host: config.host||'localhost', |
114 | caps: config.caps, |
115 | key: config.key || keys.id |
116 | }, function (err, rpc) { |
117 | if(err) { |
118 | if (/could not connect/.test(err.message)) { |
119 | console.log('Error: Could not connect to the scuttlebot server.') |
120 | console.log('Use the "server" command to start it.') |
121 | if(config.verbose) throw err |
122 | process.exit(1) |
123 | } |
124 | throw err |
125 | } |
126 | |
127 | // add aliases |
128 | for (var k in cmdAliases) { |
129 | rpc[k] = rpc[cmdAliases[k]] |
130 | manifest[k] = manifest[cmdAliases[k]] |
131 | } |
132 | |
133 | // add some extra commands |
134 | manifest.version = 'async' |
135 | manifest.config = 'sync' |
136 | rpc.version = function (cb) { |
137 | console.log(require('./package.json').version) |
138 | cb() |
139 | } |
140 | rpc.config = function (cb) { |
141 | console.log(JSON.stringify(config, null, 2)) |
142 | cb() |
143 | } |
144 | |
145 | // HACK |
146 | // we need to output the hash of blobs that are added via blobs.add |
147 | // because muxrpc doesnt support the `sink` callback yet, we need this manual override |
148 | // -prf |
149 | if (process.argv[2] === 'blobs.add') { |
150 | var filename = process.argv[3] |
151 | var source = |
152 | filename ? File(process.argv[3]) |
153 | : !process.stdin.isTTY ? toPull.source(process.stdin) |
154 | : (function () { |
155 | console.error('USAGE:') |
156 | console.error(' blobs.add <filename> # add a file') |
157 | console.error(' source | blobs.add # read from stdin') |
158 | process.exit(1) |
159 | })() |
160 | var hasher = createHash('sha256') |
161 | pull( |
162 | source, |
163 | hasher, |
164 | rpc.blobs.add(function (err) { |
165 | if (err) |
166 | throw err |
167 | console.log('&'+hasher.digest) |
168 | process.exit() |
169 | }) |
170 | ) |
171 | return |
172 | } |
173 | |
174 | // run commandline flow |
175 | muxrpcli(argv, manifest, rpc, config.verbose) |
176 | }) |
177 | } |
178 | |
179 | |
180 | |
181 | |
182 |
Built with git-ssb-web