Files: a53c85dc495dfd37effbe0c6a1676d1965f3cd12 / index.js
2322 bytesRaw
1 | |
2 | var path = require('path') |
3 | var ssbKeys = require('ssb-keys') |
4 | var explain = require('explain-error') |
5 | var path = require('path') |
6 | var fs = require('fs') |
7 | |
8 | var MultiServer = require('multiserver') |
9 | var WS = require('multiserver/plugins/ws') |
10 | var Net = require('multiserver/plugins/net') |
11 | var Shs = require('multiserver/plugins/shs') |
12 | |
13 | var muxrpc = require('muxrpc') |
14 | var pull = require('pull-stream') |
15 | |
16 | function toSodiumKeys(keys) { |
17 | if(!keys || !keys.public) return null |
18 | return { |
19 | publicKey: |
20 | new Buffer(keys.public.replace('.ed25519',''), 'base64'), |
21 | secretKey: |
22 | new Buffer(keys.private.replace('.ed25519',''), 'base64'), |
23 | } |
24 | } |
25 | |
26 | var cap = |
27 | new Buffer('1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=', 'base64') |
28 | |
29 | var createConfig = require('ssb-config/inject') |
30 | |
31 | module.exports = function (keys, opts, cb) { |
32 | var config |
33 | if (typeof keys == 'function') { |
34 | cb = keys |
35 | keys = null |
36 | opts = null |
37 | } |
38 | else if (typeof opts == 'function') { |
39 | cb = opts |
40 | opts = keys |
41 | keys = null |
42 | } |
43 | if(typeof opts === 'string' || opts == null || !keys) |
44 | config = createConfig(typeof opts === 'string' ? opts : null) |
45 | |
46 | keys = keys || ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
47 | opts = opts || {} |
48 | |
49 | var remote |
50 | if(opts.remote) |
51 | remote = opts.remote |
52 | else { |
53 | var host = opts.host || 'localhost' |
54 | var port = opts.port || config.port |
55 | var key = opts.key || keys.id |
56 | |
57 | remote = 'net:'+host+':'+port+'~shs:'+key.substring(1).replace('.ed25519', '') |
58 | } |
59 | |
60 | var manifest = opts.manifest || (function () { |
61 | try { |
62 | return JSON.parse(fs.readFileSync( |
63 | path.join(config.path, 'manifest.json') |
64 | )) |
65 | } catch (err) { |
66 | throw explain(err, 'could not load manifest file') |
67 | } |
68 | })() |
69 | |
70 | var shs = Shs({ |
71 | keys: toSodiumKeys(keys), |
72 | appKey: opts.appKey || cap, |
73 | |
74 | //no client auth. we can't receive connections anyway. |
75 | auth: function (cb) { cb(null, false) }, |
76 | timeout: 1000 |
77 | }) |
78 | |
79 | var ms = MultiServer([ |
80 | [Net({}), shs], |
81 | [WS({}), shs] |
82 | ]) |
83 | |
84 | ms.client(remote, function (err, stream) { |
85 | if(err) return cb(explain(err, 'could not connect to sbot')) |
86 | var sbot = muxrpc(manifest, false)() |
87 | pull(stream, sbot.createStream(), stream) |
88 | cb(null, sbot) |
89 | }) |
90 | } |
91 | |
92 |
Built with git-ssb-web