-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
184 lines (148 loc) · 4.68 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const sodium = require('sodium-native')
const discoverySwarm = require('discovery-swarm')
const debug = require('debug')('swarm-peer-server')
const EncryptedSocket = require('./lib/socket')
const crypto = require('./lib/crypto')
const NETWORK_TIMEOUT = 15000
const DISCOVERY_HASH = Buffer.from('swarmserver')
// +1 from Dat protocol default to reduce conflict
const DEFAULT_PORT = 3283
function getDiscoveryKey(tree) {
var digest = Buffer.alloc(32)
sodium.crypto_generichash(digest, DISCOVERY_HASH, tree)
return digest
}
function createSwarm(opts) {
const swarm = discoverySwarm(opts)
swarm.on('error', () => swarm.listen(0))
swarm.listen(opts.port || DEFAULT_PORT)
swarm.join(opts.name, { announce: true })
return swarm
}
async function authConnection(socket, opts) {
return new Promise((resolve, reject) => {
const esocket = new EncryptedSocket(socket, opts.publicKey, opts.secretKey)
esocket.once('connection', () => resolve(esocket))
esocket.once('error', () => {})
esocket.once('close', reject)
// TODO: timeout
esocket.connect(opts.hostPublicKey)
})
}
function listen(opts, connectionHandler) {
let publicKey = opts.publicKey
let secretKey = opts.secretKey
if (opts.convert) {
publicKey = crypto.pub2auth(publicKey)
secretKey = crypto.secret2auth(secretKey)
}
const discoveryKey = getDiscoveryKey(publicKey)
const swarmOpts = Object.assign({}, opts, { name: discoveryKey })
const swarm = createSwarm(swarmOpts)
debug(`Listen ${publicKey.toString('hex')}`)
// Wait for connections to perform auth handshake with
swarm.on('connection', async (socket, info) => {
const address = socket.address().address
debug(`Local swarm connection ${address}`)
let esocket
try {
debug(`Attempting to auth...`)
esocket = await authConnection(socket, {
publicKey: publicKey,
secretKey: secretKey
})
} catch (e) {
debug('Failed to auth peer\n', e)
return
}
debug(`Authed with peer: ${address}`)
connectionHandler(esocket, esocket.peerKey, info)
})
return swarm
}
function connect(opts) {
return new Promise((resolve, reject) => {
let timeoutId, timeout, connected
let publicKey = opts.publicKey
let secretKey = opts.secretKey
let hostPublicKey = opts.hostPublicKey
if (opts.convert) {
publicKey = crypto.pub2auth(publicKey)
secretKey = crypto.secret2auth(secretKey)
hostPublicKey = crypto.pub2auth(hostPublicKey)
}
const discoveryKey = getDiscoveryKey(hostPublicKey)
const swarmOpts = Object.assign({}, opts, { name: discoveryKey })
const swarm = createSwarm(swarmOpts)
debug(`Connecting to remote swarm ${hostPublicKey.toString('hex')}`)
let queue = []
let connecting = false
const cleanup = () => {
if (timeoutId) {
clearTimeout(timeoutId)
timeoutId = null
}
queue.forEach(conn => conn.socket.destroy())
queue = []
swarm.removeListener('connection', onConnection)
if (!connected) {
swarm.close()
}
}
async function attemptConnect() {
connecting = true
let conn
while (!connected && !timeout && (conn = queue.shift())) {
const { socket, info } = conn
let esocket
try {
debug(`Attempting to auth ${hostPublicKey.toString('hex')}...`)
esocket = await authConnection(socket, {
publicKey: publicKey,
secretKey: secretKey,
hostPublicKey
})
} catch (e) {
debug('Failed to auth peer\n', e)
continue
}
const address = socket.address().address
debug(`Authed with host: ${address}`)
if (!timeout && !connected) {
// close swarm when we're done with the socket
esocket.once('close', () => {
swarm.close()
// TODO: unannounce to DHT
// https://github.com/webtorrent/bittorrent-dht/pull/184
})
connected = true
cleanup()
resolve({ socket: esocket, info: info })
} else {
esocket.destroy()
}
}
connecting = false
}
// Wait for connections and attempt to auth with host
const onConnection = (socket, info) => {
const address = socket.address().address
debug(`Remote swarm connection ${address}`)
queue.push({ socket, info })
if (!connecting) {
attemptConnect()
}
}
swarm.on('connection', onConnection)
timeoutId = setTimeout(() => {
cleanup()
timeout = true
reject('Timeout connecting to swarm')
}, opts.timeout || NETWORK_TIMEOUT)
})
}
module.exports = {
listen,
connect,
EncryptedSocket
}