git ssb

2+

ev / minsbot



Tree: d5c6f9521130fcae13928a86e3f39d88e2586342

Files: d5c6f9521130fcae13928a86e3f39d88e2586342 / decent-ws / index.js

2460 bytesRaw
1var MultiServer = require('multiserver')
2var WS = require('multiserver/plugins/ws')
3var SHS = require('multiserver/plugins/shs')
4var http = require('http')
5var muxrpc = require('muxrpc')
6var pull = require('pull-stream')
7var blobServer = require('./blob-server')
8
9function toSodiumKeys(keys) {
10 return {
11 publicKey:
12 new Buffer(keys.public.replace('.ed25519',''), 'base64'),
13 secretKey:
14 new Buffer(keys.private.replace('.ed25519',''), 'base64'),
15 }
16}
17
18// you need to add manifest items that are allowed below to use them over websockets
19var READ_AND_ADD = [
20 'get',
21 'getLatest',
22 'createLogStream',
23 'createUserStream',
24 'createHistoryStream',
25 'getAddress',
26 'blobs.add',
27 'blobs.size',
28 'blobs.has',
29 'blobs.get',
30 'blobs.changes',
31 'blobs.createWants',
32 'add',
33 'links',
34 'query.read',
35 'backlinks.read',
36 'friends',
37 'search.query'
38]
39
40
41exports.name = 'ws'
42exports.version = '1.0.0'
43exports.manifest = {
44 getAddress: 'sync'
45}
46
47function toId(id) {
48 if (typeof id !== 'string') {
49 return '@' + id.toString('base64') + '.ed25519' // isn't this available somewhere else?
50 } else throw new Error('toId() called on string. todo: clean this your mess.')
51}
52
53exports.init = function (sbot, config) {
54
55 var port
56 if(config.ws)
57 port = config.ws.port
58 if(!port)
59 port = 1024+(~~(Math.random()*(65536-1024)))
60
61 var layers = []
62 var server = http.createServer(blobServer(sbot, layers)).listen(port)
63
64 function _auth (id, cb) {
65 cb(null, {allow: READ_AND_ADD, deny: null})
66 }
67
68 var ms = MultiServer([
69 [
70 WS({server: server, port: port, host: config.host || 'localhost'}),
71 SHS({
72 keys: toSodiumKeys(config.keys),
73 appKey: (config.caps && new Buffer(config.caps.shs, "base64")) || cap,
74 auth: function (id, cb) {
75 sbot.auth(toId(id), function (err, allowed) {
76 if(err || allowed) cb(err, allowed)
77 else _auth(id, cb)
78 })
79 },
80 timeout: config.timeout
81 })
82 ]
83 ])
84
85 var close = ms.server(function (stream) {
86 var manifest = sbot.getManifest()
87 var rpc = muxrpc({}, manifest)(sbot, stream.auth)
88 rpc.id = toId(stream.remote)
89 pull(stream, rpc.createStream(), stream)
90 })
91
92 sbot.close.hook(function (fn, args) {
93 close()
94 fn.apply(this, args)
95 })
96
97 return {
98 getAddress: function () {
99 return ms.stringify()
100 },
101 use: function (handler) {
102 layers.push(handler)
103 }
104
105 }
106}
107
108
109

Built with git-ssb-web