-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
executable file
·170 lines (143 loc) · 4.77 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#! /usr/bin/env node
var fs = require('fs')
var path = require('path')
var pull = require('pull-stream')
var toPull = require('stream-to-pull-stream')
var File = require('pull-file')
var explain = require('explain-error')
var Config = require('ssb-config/inject')
var Client = require('ssb-client')
var createHash = require('multiblob/util').createHash
var minimist = require('minimist')
var muxrpcli = require('muxrpcli')
var cmdAliases = require('./lib/cli-cmd-aliases')
var ProgressBar = require('./lib/progress')
var packageJson = require('./package.json')
//get config as cli options after --, options before that are
//options to the command.
var argv = process.argv.slice(2)
var i = argv.indexOf('--')
var conf = argv.slice(i+1)
argv = ~i ? argv.slice(0, i) : argv
var config = Config(process.env.ssb_appname, minimist(conf))
if (config.keys.curve === 'k256')
throw new Error('k256 curves are no longer supported,'+
'please delete' + path.join(config.path, 'secret'))
var manifestFile = path.join(config.path, 'manifest.json')
if (argv[0] == 'server') {
console.log('WARNING-DEPRECATION: `sbot server` has been renamed to `ssb-server start`')
argv[0] = 'start'
}
if (argv[0] == 'start') {
console.log(packageJson.name, packageJson.version, config.path, 'logging.level:'+config.logging.level)
console.log('my key ID:', config.keys.public)
// special start command:
// import ssbServer and start the server
var createSsbServer = require('./')
.use(require('ssb-onion'))
.use(require('ssb-unix-socket'))
.use(require('ssb-no-auth'))
.use(require('ssb-plugins'))
.use(require('ssb-master'))
.use(require('ssb-legacy-conn'))
.use(require('ssb-replicate'))
.use(require('ssb-friends'))
.use(require('ssb-blobs'))
.use(require('ssb-invite'))
.use(require('ssb-local'))
.use(require('ssb-logging'))
.use(require('ssb-query'))
.use(require('ssb-ws'))
.use(require('ssb-ebt'))
.use(require('ssb-ooo'))
.use(require('ssb-device-address'))
.use(require('ssb-identities'))
.use(require('ssb-peer-invites'))
// add third-party plugins
require('ssb-plugins').loadUserPlugins(createSsbServer, config)
// start server
var server = createSsbServer(config)
// write RPC manifest to ~/.ssb/manifest.json
fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
if(process.stdout.isTTY && (config.logging.level != 'info'))
ProgressBar(server.progress)
} else {
// normal command:
// create a client connection to the server
// read manifest.json
var manifest
try {
manifest = JSON.parse(fs.readFileSync(manifestFile))
} catch (err) {
throw explain(err,
'no manifest file'
+ '- should be generated first time server is run'
)
}
var opts = {
manifest: manifest,
port: config.port,
host: config.host || 'localhost',
caps: config.caps,
key: config.key || config.keys.id
}
// connect
Client(config.keys, opts, function (err, rpc) {
if(err) {
if (/could not connect/.test(err.message)) {
console.error('Error: Could not connect to ssb-server ' + opts.host + ':' + opts.port)
console.error('Use the "start" command to start it.')
console.error('Use --verbose option to see full error')
if(config.verbose) throw err
process.exit(1)
}
throw err
}
// add aliases
for (var k in cmdAliases) {
rpc[k] = rpc[cmdAliases[k]]
manifest[k] = manifest[cmdAliases[k]]
}
// add some extra commands
// manifest.version = 'async'
manifest.config = 'sync'
// rpc.version = function (cb) {
// console.log(packageJson.version)
// cb()
// }
rpc.config = function (cb) {
console.log(JSON.stringify(config, null, 2))
cb()
}
// HACK
// we need to output the hash of blobs that are added via blobs.add
// because muxrpc doesnt support the `sink` callback yet, we need this manual override
// -prf
if (process.argv[2] === 'blobs.add') {
var filename = process.argv[3]
var source =
filename ? File(process.argv[3])
: !process.stdin.isTTY ? toPull.source(process.stdin)
: (function () {
console.error('USAGE:')
console.error(' blobs.add <filename> # add a file')
console.error(' source | blobs.add # read from stdin')
process.exit(1)
})()
var hasher = createHash('sha256')
pull(
source,
hasher,
rpc.blobs.add(function (err) {
if (err)
throw err
console.log('&'+hasher.digest)
process.exit()
})
)
return
}
// run commandline flow
muxrpcli(argv, manifest, rpc, config.verbose)
})
}