git ssb

3+

ev / decent



Commit ca00c928e720988a29e7dc67b12295c1778c5e98

add ssb-ws to local, and play around with disabling ws auth

Ev Bogue committed on 9/1/2017, 4:17:07 AM
Parent: 595947fb3cdc049d7573e25e12919f4b7506af41

Files changed

build/index.htmlchanged
decent.jschanged
package.jsonchanged
plugins/ssb-ws/.travis.ymladded
plugins/ssb-ws/LICENSEadded
plugins/ssb-ws/README.mdadded
plugins/ssb-ws/index.jsadded
plugins/ssb-ws/json-api.jsadded
plugins/ssb-ws/package.jsonadded
build/index.htmlView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 1226515 bytes
New file size: 1226512 bytes
decent.jsView
@@ -41,9 +41,9 @@
4141 .use(require('./plugins/logging'))
4242 .use(require('./plugins/private'))
4343 .use(require('ssb-query'))
4444 .use(require('ssb-links'))
45- .use(require('ssb-ws'))
45 + .use(require('./plugins/ssb-ws'))
4646 .use(require('ssb-ebt'))
4747 .use(require('./plugins/viewer'))
4848
4949 // add third-party plugins
package.jsonView
@@ -86,9 +86,8 @@
8686 "ssb-mentions": "^0.4.0",
8787 "ssb-query": "^1.0.0",
8888 "ssb-ref": "^2.7.1",
8989 "ssb-sort": "^1.0.0",
90- "ssb-ws": "^1.0.3",
9190 "statistics": "3.0.0",
9291 "stream-to-pull-stream": "^1.6.10",
9392 "suggest-box": "^2.2.1",
9493 "text-node-searcher": "^1.1.0",
plugins/ssb-ws/.travis.ymlView
@@ -1,0 +1,4 @@
1 +language: node_js
2 +node_js:
3 + - 0.6
4 + - 0.8
plugins/ssb-ws/LICENSEView
@@ -1,0 +1,22 @@
1 +Copyright (c) 2016 'Dominic Tarr'
2 +
3 +Permission is hereby granted, free of charge,
4 +to any person obtaining a copy of this software and
5 +associated documentation files (the "Software"), to
6 +deal in the Software without restriction, including
7 +without limitation the rights to use, copy, modify,
8 +merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom
10 +the Software is furnished to do so,
11 +subject to the following conditions:
12 +
13 +The above copyright notice and this permission notice
14 +shall be included in all copies or substantial portions of the Software.
15 +
16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
20 +ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
plugins/ssb-ws/README.mdView
@@ -1,0 +1,21 @@
1 +# ssb-ws
2 +
3 +ssb-ws & http server for ssb.
4 +
5 +Work In Progress
6 +
7 +``` js
8 +sbot plugins.install ssb-ws
9 +```
10 +
11 +make sure you set a port in your config file (~/.ssb/config).
12 +
13 +``` json
14 +{
15 + "ws": {"port": 8989}
16 +}
17 +```
18 +
19 +## License
20 +
21 +MIT
plugins/ssb-ws/index.jsView
@@ -1,0 +1,114 @@
1 +var MultiServer = require('multiserver')
2 +var WS = require('multiserver/plugins/ws')
3 +var SHS = require('multiserver/plugins/shs')
4 +var http = require('http')
5 +var muxrpc = require('muxrpc')
6 +var pull = require('pull-stream')
7 +var JSONApi = require('./json-api')
8 +
9 +function 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 +var READ_AND_ADD = [ //except for add, of course
19 + 'get',
20 + 'getLatest',
21 + 'createLogStream',
22 + 'createUserStream',
23 +
24 + 'createHistoryStream',
25 + 'getAddress',
26 +
27 + 'links',
28 +
29 + 'blobs.add',
30 + 'blobs.size',
31 + 'blobs.has',
32 + 'blobs.get',
33 + 'blobs.changes',
34 + 'blobs.createWants',
35 +
36 +
37 + 'add',
38 +
39 + 'query.read',
40 + 'links2.read'
41 +]
42 +
43 +
44 +exports.name = 'ws'
45 +exports.version = require('./package.json').version
46 +exports.manifest = {
47 + getAddress: 'sync'
48 +}
49 +
50 +function toId(id) {
51 + if (typeof id !== 'string') {
52 + return '@' + id.toString('base64') + '.ed25519' // isn't this available somewhere else?
53 + } else throw new Error('toId() called on string. todo: clean this your mess.')
54 +}
55 +
56 +exports.init = function (sbot, config) {
57 +
58 + var port
59 + if(config.ws)
60 + port = config.ws.port
61 + if(!port)
62 + port = 1024+(~~(Math.random()*(65536-1024)))
63 +
64 + var server = http.createServer(JSONApi(sbot)).listen(port)
65 +
66 + function _auth (id, cb) {
67 + cb(null, {allow: READ_AND_ADD, deny: null})
68 + }
69 +
70 + var ms = MultiServer([
71 + [
72 + WS({server: server, port: port, host: config.host || 'localhost'}),
73 + SHS({
74 + keys: toSodiumKeys(config.keys),
75 + appKey: (config.caps && config.caps.shs) || cap,
76 + auth: function (id, cb) {
77 + sbot.auth(toId(id), function (err, allowed) {
78 + if(err || allowed) cb(err, allowed)
79 + else _auth(id, cb)
80 + })
81 + },
82 + timeout: config.timeout
83 + })
84 + ]
85 + ])
86 +
87 + var close = ms.server(function (stream) {
88 + var manifest = sbot.getManifest()
89 + var rpc = muxrpc({}, manifest)(sbot, stream.auth)
90 + rpc.id = toId(stream.remote)
91 + pull(stream, rpc.createStream(), stream)
92 + })
93 +
94 + //close when the server closes.
95 + sbot.close.hook(function (fn, args) {
96 + close()
97 + fn.apply(this, args)
98 + })
99 +
100 + return {
101 + getAddress: function () {
102 + return ms.stringify()
103 + }
104 +
105 + }
106 +}
107 +
108 +
109 +
110 +
111 +
112 +
113 +
114 +
plugins/ssb-ws/json-api.jsView
@@ -1,0 +1,89 @@
1 +'use strict'
2 +var ref = require('ssb-ref')
3 +var Stack = require('stack')
4 +var BlobsHttp = require('multiblob-http')
5 +var sort = require('ssb-sort')
6 +var pull = require('pull-stream')
7 +var WebBoot = require('web-bootloader/handler')
8 +var URL = require('url')
9 +var Emoji = require('emoji-server')
10 +
11 +function msgHandler(path, handler) {
12 + return function (req, res, next) {
13 + console.log(req.method, req.url)
14 + if(req.method !== 'GET') return next()
15 + if(req.url.indexOf(path) === 0) {
16 + var id = req.url.substring(path.length)
17 + console.log("MSG?", id)
18 + if(!ref.isMsg(id))
19 + next(new Error('not a valid message id:'+id))
20 + else {
21 + req.id = id
22 + handler(req, res, next)
23 + }
24 + }
25 + else
26 + next()
27 + }
28 +}
29 +
30 +function send(res, obj) {
31 + res.writeHead(200, {'Content-Type': 'application/json'})
32 + res.end(JSON.stringify(obj, null, 2))
33 +}
34 +
35 +module.exports = function (sbot) {
36 + var prefix = '/blobs'
37 + return Stack(
38 + WebBoot,
39 + Emoji('/img/emoji'),
40 + function (req, res, next) {
41 + res.setHeader('Access-Control-Allow-Origin', '*')
42 + res.setHeader("Access-Control-Allow-Headers",
43 + "Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since");
44 + res.setHeader("Access-Control-Allow-Methods", "GET", "HEAD");
45 + next()
46 + },
47 + msgHandler('/msg/', function (req, res, next) {
48 + sbot.get(req.id, function (err, msg) {
49 + if(err) return next(err)
50 + send(res, {key: req.id, value: msg})
51 + })
52 + }),
53 + msgHandler('/thread/', function (req, res, next) {
54 + sbot.get(req.id, function (err, value) {
55 + if(err) return next(err)
56 + var msg = {key: req.id, value: value}
57 +
58 + pull(
59 + sbot.links({rel: 'root', dest: req.id, values: true, keys: true}),
60 + pull.collect(function (err, ary) {
61 + if(err) return next(err)
62 + ary.unshift(msg)
63 + send(res, sort(ary))
64 + })
65 + )
66 + })
67 +
68 + }),
69 + function (req, res, next) {
70 + if(!(req.method === "GET" || req.method == 'HEAD')) return next()
71 +
72 + var u = URL.parse('http://makeurlparseright.com'+req.url)
73 + var hash = decodeURIComponent(u.pathname.substring((prefix+'/get/').length))
74 + //check if we don't already have this, tell blobs we want it, if necessary.
75 + sbot.blobs.has(hash, function (err, has) {
76 + if(has) next()
77 + else sbot.blobs.want(hash, function (err, has) { next() })
78 + })
79 + },
80 + BlobsHttp(sbot.blobs, prefix)
81 + )
82 +}
83 +
84 +
85 +
86 +
87 +
88 +
89 +
plugins/ssb-ws/package.jsonView
@@ -1,0 +1,27 @@
1 +{
2 + "name": "ssb-ws",
3 + "description": "websocket & http server for ssb",
4 + "version": "1.0.3",
5 + "homepage": "https://github.com/dominictarr/ssb-ws",
6 + "repository": {
7 + "type": "git",
8 + "url": "git://github.com/dominictarr/ssb-ws.git"
9 + },
10 + "dependencies": {
11 + "emoji-server": "^1.0.0",
12 + "multiblob-http": "^0.2.0",
13 + "multiserver": "^1.2.0",
14 + "muxrpc": "^6.3.3",
15 + "pull-stream": "^3.4.3",
16 + "ssb-ref": "^2.3.0",
17 + "ssb-sort": "0.0.0",
18 + "stack": "^0.1.0",
19 + "web-bootloader": "^0.1.0"
20 + },
21 + "devDependencies": {},
22 + "scripts": {
23 + "test": "set -e; for t in test/*.js; do node $t; done"
24 + },
25 + "author": "'Dominic Tarr' <dominic.tarr@gmail.com> (dominictarr.com)",
26 + "license": "MIT"
27 +}

Built with git-ssb-web