Files: 2ed88ea19c5cb2ca092820512a6bf8138f9da73c / bin.js
5032 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 config = require('ssb-config/inject')(process.env.ssb_appname, minimist(conf)) |
26 | |
27 | var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
28 | if(keys.curve === 'k256') |
29 | throw new Error('k256 curves are no longer supported,'+ |
30 | 'please delete' + path.join(config.path, 'secret')) |
31 | |
32 | var manifestFile = path.join(config.path, 'manifest.json') |
33 | |
34 | if (argv[0] == 'server') { |
35 | console.log(packageJson.name, packageJson.version, config.path, 'logging.level:'+config.logging.level) |
36 | console.log('my key ID:', keys.public) |
37 | |
38 | // special server command: |
39 | // import sbot and start the server |
40 | |
41 | var createSbot = require('./') |
42 | .use(require('./plugins/onion')) |
43 | .use(require('./plugins/unix-socket')) |
44 | .use(require('./plugins/no-auth')) |
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('ssb-query')) |
55 | .use(require('ssb-ws')) |
56 | .use(require('ssb-ebt')) |
57 | // add third-party plugins |
58 | require('./plugins/plugins').loadUserPlugins(createSbot, config) |
59 | |
60 | if (argv[1] != '--disable-ssb-links') { |
61 | if (!createSbot.plugins.find(p => p.name == 'links2')) { |
62 | console.log("WARNING-DEPRECATION: ssb-links not installed as a plugin. If you are using git-ssb, ssb-npm or patchfoo please consider installing it") |
63 | createSbot.use(require('ssb-links')) |
64 | } |
65 | } |
66 | |
67 | // start server |
68 | |
69 | config.keys = keys |
70 | var server = createSbot(config) |
71 | |
72 | // write RPC manifest to ~/.ssb/manifest.json |
73 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
74 | |
75 | if(process.stdout.isTTY && (config.logging.level != 'info')) |
76 | ProgressBar(server.progress) |
77 | } else { |
78 | |
79 | // normal command: |
80 | // create a client connection to the server |
81 | |
82 | // read manifest.json |
83 | var manifest |
84 | try { |
85 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
86 | } catch (err) { |
87 | throw explain(err, |
88 | 'no manifest file' |
89 | + '- should be generated first time server is run' |
90 | ) |
91 | } |
92 | |
93 | // connect |
94 | require('ssb-client')(keys, { |
95 | manifest: manifest, |
96 | port: config.port, |
97 | host: config.host||'localhost', |
98 | caps: config.caps, |
99 | key: config.key || keys.id |
100 | }, function (err, rpc) { |
101 | if(err) { |
102 | if (/could not connect/.test(err.message)) { |
103 | var serverAddr = (config.host || 'localhost') + ":" + config.port; |
104 | console.error('Error: Could not connect to the scuttlebot server ' + serverAddr) |
105 | console.error('Use the "server" command to start it.') |
106 | if(config.verbose) throw err |
107 | process.exit(1) |
108 | } |
109 | throw err |
110 | } |
111 | |
112 | // add aliases |
113 | for (var k in cmdAliases) { |
114 | rpc[k] = rpc[cmdAliases[k]] |
115 | manifest[k] = manifest[cmdAliases[k]] |
116 | } |
117 | |
118 | // add some extra commands |
119 | // manifest.version = 'async' |
120 | manifest.config = 'sync' |
121 | // rpc.version = function (cb) { |
122 | // console.log(packageJson.version) |
123 | // cb() |
124 | // } |
125 | rpc.config = function (cb) { |
126 | console.log(JSON.stringify(config, null, 2)) |
127 | cb() |
128 | } |
129 | |
130 | // HACK |
131 | // we need to output the hash of blobs that are added via blobs.add |
132 | // because muxrpc doesnt support the `sink` callback yet, we need this manual override |
133 | // -prf |
134 | if (process.argv[2] === 'blobs.add') { |
135 | var filename = process.argv[3] |
136 | var source = |
137 | filename ? File(process.argv[3]) |
138 | : !process.stdin.isTTY ? toPull.source(process.stdin) |
139 | : (function () { |
140 | console.error('USAGE:') |
141 | console.error(' blobs.add <filename> # add a file') |
142 | console.error(' source | blobs.add # read from stdin') |
143 | process.exit(1) |
144 | })() |
145 | var hasher = createHash('sha256') |
146 | pull( |
147 | source, |
148 | hasher, |
149 | rpc.blobs.add(function (err) { |
150 | if (err) |
151 | throw err |
152 | console.log('&'+hasher.digest) |
153 | process.exit() |
154 | }) |
155 | ) |
156 | return |
157 | } |
158 | |
159 | // run commandline flow |
160 | muxrpcli(argv, manifest, rpc, config.verbose) |
161 | }) |
162 | } |
163 | |
164 |
Built with git-ssb-web