-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
140 lines (116 loc) · 3.77 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var debug = require('debug')('webcat')
var ghsign = require('ghsign')
var signalhub = require('signalhub')
var SimplePeer = require('simple-peer')
var wrtc = require('wrtc')
var fs = require('fs')
var path = require('path')
var duplexify = require('duplexify')
var through = require('through2')
var pump = require('pump')
var noop = function () {}
var conf = {}
try {
conf = JSON.parse(fs.readFileSync(path.join(process.env.HOME || process.env.USERPROFILE, '.config/webcat.json')))
} catch (err) {
// do nothing
}
module.exports = function (username, opts) {
if (!opts) opts = {}
if (!opts.username) opts.username = conf.username
if (!opts.username) throw new Error('You must specify options.username or run webcat --configure')
debug('new instance', username, opts)
var stream = duplexify()
var hub = signalhub(opts.signalhub || 'http://dev.mathiasbuus.eu:8080')
var sign = ghsign.signer(opts.username)
var verify = ghsign.verifier(username)
var signMessage = function (message, cb) {
debug('signing message', message)
message = JSON.stringify(message)
sign(message, 'base64', function (err, sig) {
if (err) return cb(err)
cb(null, [sig, message])
})
}
var verifyMessage = function (data, cb) {
if (!Array.isArray(data) || data.length !== 2) return cb()
debug('verifying message', data)
verify(data[1], data[0], 'base64', function (err, verified) {
debug('message verified?', err, verified)
if (err) return cb(err)
if (verified) return cb(null, JSON.parse(data[1]))
cb()
})
}
var peer = null
var subs = hub.subscribe(opts.username)
var syn = {type: 'syn', nounce: Math.random()}
var isInitiator = false
var connected = false
var sendSyn = function (cb) {
if (!cb) cb = noop
signMessage(syn, function (err, message) {
if (err) return cb(err)
debug('sendSyn', message)
hub.broadcast(username, message, cb)
})
}
var createPeer = function (initiator) {
if (peer) return
isInitiator = initiator
peer = new SimplePeer({initiator: initiator, trickle: false, wrtc: wrtc})
peer.on('connect', function () {
connected = true
subs.destroy()
debug('connected')
stream.emit('connect')
stream.setReadable(peer)
stream.setWritable(peer)
})
peer.on('close', function () {
stream.destroy()
})
peer.on('signal', function (signal) {
debug('local signal', signal)
stream.emit('signal', signal)
signMessage(signal, function (err, message) {
if (err) return stream.destroy(err)
hub.broadcast(username, message, function (err) {
if (err) stream.destroy(err)
})
})
})
}
pump(subs, through.obj(function (data, enc, cb) {
debug('received message', data)
verifyMessage(data, function (err, message) {
if (err) return cb(err)
if (!message) return cb()
if (message.type === 'syn' && peer) return cb()
if (message.type === 'syn') {
if (message.nouce) message.nounce = message.nouce
if (message.nounce === syn.nounce) return cb()
if (message.nounce < syn.nounce) return sendSyn(cb)
createPeer(true)
return cb()
}
if (isInitiator && message.type === 'offer') return cb()
if (!isInitiator && message.type === 'answer') return cb()
if (message.type === 'offer' || message.type === 'answer') {
debug('remote signal', message)
stream.emit('receive-signal', data)
createPeer(false)
peer.signal(message)
return cb()
}
cb() // accept everything else
})
}), function (err) {
debug('after pipe', err)
if (!connected) stream.destroy(err)
})
sendSyn(function (err) {
if (err) return stream.destroy(err)
})
return stream
}