forked from arj03/ssb-browser-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel-chat.js
63 lines (53 loc) · 1.48 KB
/
tunnel-chat.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
const Notify = require('pull-notify');
exports.manifest = {
tunnelMessage: 'async'
}
exports.permissions = {
anonymous: {allow: ['tunnelMessage']}
}
exports.name = 'tunnelChat'
exports.init = function (sbot, config) {
var messages = Notify()
var remote
sbot.on('rpc:connect', function (rpc, isClient) {
if (!isClient)
remote = rpc
})
return {
acceptMessages: function() {
SSB.net.tunnel.setupIsConnectionOkHandler((remoteId) => {
let isOk = confirm("Allow connection from: " + remoteId + "?")
if (isOk)
messages({user: '', text: remoteId + " connected!"})
return isOk
})
SSB.net.connect(SSB.remoteAddress, (err, rpc) => {
if (err) throw(err)
rpc.tunnel.announce()
})
},
connect: function(remoteId) {
var remoteKey = remoteId.substring(1, remoteId.indexOf('.'))
messages({user: '', text: "waiting for @" + remoteKey + ".ed25519 to accept"})
SSB.net.connect('tunnel:@'+SSB.remoteAddress.split(':')[3]+ ':' + remoteId + '~shs:' + remoteKey, (err, rpc) => {
if (err) throw(err)
remote = rpc
messages({user: '', text: rpc.id + " connected!"})
})
},
sendMessage: function(text) {
try {
remote.tunnelChat.tunnelMessage(text)
messages({user: 'me', text})
} catch (e) {
messages({user: '', text: 'remote end disconnected'})
}
},
tunnelMessage: function(text) {
messages({user: 'remote', text})
},
messages: function() {
return messages.listen()
}
}
}