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