Files: 03ba4f6100e6c42a83ad6c770c69bb63a5d6fd7b / bin.js
5555 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('./mvd-indexes')) |
58 | .use(require('ssb-links')) |
59 | .use(require('ssb-ws')) |
60 | .use(require('ssb-ebt')) |
61 | .use({ |
62 | name: 'serve', |
63 | version: '1.0.0', |
64 | init: function (sbot) { |
65 | sbot.ws.use(function (req, res, next) { |
66 | var send = config |
67 | delete send.keys |
68 | /*sbot.invite.create(1, function (err, cb) { |
69 | send.invite = cb |
70 | })*/ |
71 | send.address = sbot.getAddress() |
72 | if(req.url == '/') |
73 | res.end(compiledClient) |
74 | if(req.url == '/get-config') |
75 | res.end(JSON.stringify(send)) |
76 | else next() |
77 | }) |
78 | } |
79 | }) |
80 | // add third-party plugins |
81 | require('scuttlebot/plugins/plugins').loadUserPlugins(createSbot, config) |
82 | |
83 | // start server |
84 | |
85 | open('http://localhost:' + config.ws.port, {wait: false}) |
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 | if(process.stdout.isTTY && (config.logging.level != 'info')) |
94 | ProgressBar(server.progress) |
95 | } else { |
96 | |
97 | // normal command: |
98 | // create a client connection to the server |
99 | |
100 | // read manifest.json |
101 | var manifest |
102 | try { |
103 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
104 | } catch (err) { |
105 | throw explain(err, |
106 | 'no manifest file' |
107 | + '- should be generated first time server is run' |
108 | ) |
109 | } |
110 | |
111 | // connect |
112 | require('ssb-client')(keys, { |
113 | manifest: manifest, |
114 | port: config.port, |
115 | host: config.host||'localhost', |
116 | caps: config.caps, |
117 | key: config.key || keys.id |
118 | }, function (err, rpc) { |
119 | if(err) { |
120 | if (/could not connect/.test(err.message)) { |
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.') |
124 | if(config.verbose) throw err |
125 | process.exit(1) |
126 | } |
127 | throw err |
128 | } |
129 | |
130 | // add aliases |
131 | for (var k in cmdAliases) { |
132 | rpc[k] = rpc[cmdAliases[k]] |
133 | manifest[k] = manifest[cmdAliases[k]] |
134 | } |
135 | |
136 | // add some extra commands |
137 | // manifest.version = 'async' |
138 | manifest.config = 'sync' |
139 | // rpc.version = function (cb) { |
140 | // console.log(packageJson.version) |
141 | // cb() |
142 | // } |
143 | rpc.config = function (cb) { |
144 | console.log(JSON.stringify(config, null, 2)) |
145 | cb() |
146 | } |
147 | |
148 | // HACK |
149 | // we need to output the hash of blobs that are added via blobs.add |
150 | // because muxrpc doesnt support the `sink` callback yet, we need this manual override |
151 | // -prf |
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 | |
177 | // run commandline flow |
178 | muxrpcli(argv, manifest, rpc, config.verbose) |
179 | }) |
180 | } |
181 | |
182 |
Built with git-ssb-web