Files: ae6cce37542b03065170efd2ac72e8ea8c8ae37a / index.js
2786 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 Onion = require('multiserver/plugins/onion') |
12 | var Shs = require('multiserver/plugins/shs') |
13 | |
14 | var muxrpc = require('muxrpc') |
15 | var pull = require('pull-stream') |
16 | |
17 | function toSodiumKeys(keys) { |
18 | if(!keys || !keys.public) return null |
19 | return { |
20 | publicKey: |
21 | new Buffer(keys.public.replace('.ed25519',''), 'base64'), |
22 | secretKey: |
23 | new Buffer(keys.private.replace('.ed25519',''), 'base64'), |
24 | } |
25 | } |
26 | |
27 | //load cap from config instead! |
28 | var cap = '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=' |
29 | |
30 | var createConfig = require('ssb-config/inject') |
31 | |
32 | module.exports = function (keys, opts, cb) { |
33 | var config |
34 | if (typeof keys == 'function') { |
35 | cb = keys |
36 | keys = null |
37 | opts = null |
38 | } |
39 | else if (typeof opts == 'function') { |
40 | cb = opts |
41 | opts = keys |
42 | keys = null |
43 | } |
44 | if(typeof opts === 'string' || opts == null || !keys) |
45 | config = createConfig((typeof opts === 'string' ? opts : null) || process.env.ssb_appname) |
46 | else if(opts && 'object' === typeof opts) |
47 | config = opts |
48 | |
49 | keys = keys || ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
50 | opts = opts || {} |
51 | |
52 | var appKey = new Buffer((opts.caps && opts.caps.shs) || cap, 'base64') |
53 | |
54 | var remote |
55 | if(opts.remote) |
56 | remote = opts.remote |
57 | else { |
58 | var host = opts.host || 'localhost' |
59 | var port = opts.port || config.port || 8008 |
60 | var key = opts.key || keys.id |
61 | |
62 | var protocol = 'net:' |
63 | if (host.endsWith(".onion")) |
64 | protocol = 'onion:' |
65 | remote = protocol+host+':'+port+'~shs:'+key.substring(1).replace('.ed25519', '') |
66 | } |
67 | |
68 | var manifest = opts.manifest || (function () { |
69 | try { |
70 | return JSON.parse(fs.readFileSync( |
71 | path.join(config.path, 'manifest.json') |
72 | )) |
73 | } catch (err) { |
74 | throw explain(err, 'could not load manifest file') |
75 | } |
76 | })() |
77 | |
78 | var shs = Shs({ |
79 | keys: toSodiumKeys(keys), |
80 | appKey: opts.appKey || appKey, |
81 | |
82 | //no client auth. we can't receive connections anyway. |
83 | auth: function (cb) { cb(null, false) }, |
84 | timeout: config.timers && config.timers.handshake || 3000 |
85 | }) |
86 | |
87 | var ms = MultiServer([ |
88 | [Net({}), shs], |
89 | [Onion({}), shs], |
90 | [WS({}), shs] |
91 | ]) |
92 | |
93 | ms.client(remote, function (err, stream) { |
94 | if(err) return cb(explain(err, 'could not connect to sbot')) |
95 | var sbot = muxrpc(manifest, false)() |
96 | sbot.id = '@'+stream.remote.toString('base64')+'.ed25519' |
97 | pull(stream, sbot.createStream(), stream) |
98 | cb(null, sbot, config) |
99 | }) |
100 | } |
101 | |
102 |
Built with git-ssb-web