-
Notifications
You must be signed in to change notification settings - Fork 13
/
utp-nx.js
48 lines (40 loc) · 1.25 KB
/
utp-nx.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
// Example for NX pattern
// This is pattern means the client is anonymous, but the server sends its
// public key over a forward secret channel. This is kinda like SSL/TLS with
// HPKP
var utp = require('utp-native')
var pump = require('pump')
var peer = require('..')
var serverKeys = peer.keygen()
console.log('generated server key', serverKeys.publicKey)
var server = utp.createServer(function onconnection (rawStream) {
var sec = peer(rawStream, false, {
pattern: 'NX',
// Normally these keys would be stored long-term
staticKeyPair: serverKeys
})
pump(sec, sec, function (err) {
if (err) throw err
})
})
server.listen(function () {
var port = server.address().port
var clientRawStream = utp.connect(port)
var clientSec = peer(clientRawStream, true, {
pattern: 'NX',
onstatickey: function (serverStaticKey, done) {
// Here you can validate the server publicKey, eg. against a preexchanged
// public key.
console.log('received server key ', serverStaticKey)
done() // Call with err to abort connection
}
})
clientSec.on('data', function (data) {
console.log(data.toString())
})
clientSec.on('end', function () {
server.close()
})
clientSec.write('Hello world')
clientSec.end()
})