git ssb

2+

ev / minsbot



Tree: 68ebbe79904bf073bc8afdedfb5f6fff70fdc733

Files: 68ebbe79904bf073bc8afdedfb5f6fff70fdc733 / bin.js

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

Built with git-ssb-web