-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocket.js
159 lines (128 loc) · 3.23 KB
/
socket.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
'use strict';
const stream = require('stream');
const dgram = require('dgram');
const fs = require('fs');
const mbed = require('./build/Release/node_mbed_dtls_client');
const MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY = -0x7880;
class DtlsSocket extends stream.Duplex {
constructor(options) {
super({ allowHalfOpen: false });
options = options || {};
this.remoteAddress = options.host;
this.remotePort = options.port;
this.dgramSocket = options.socket || dgram.createSocket('udp4');
this._onMessage = this._onMessage.bind(this);
this.dgramSocket.on('message', this._onMessage);
this.dgramSocket.once('error', err => {
this.emit('error', err);
this._end();
});
this.dgramSocket.once('close', () => {
this._socketClosed();
});
const privateKey = Buffer.isBuffer(options.key) ? options.key : fs.readFileSync(options.key);
const peerPublicKey = Buffer.isBuffer(options.peerPublicKey) ? options.peerPublicKey : fs.readFileSync(options.peerPublicKey);
this.mbedSocket = new mbed.DtlsSocket(privateKey, peerPublicKey,
this._sendEncrypted.bind(this),
this._handshakeComplete.bind(this),
this._error.bind(this),
options.debug);
process.nextTick(() => {
this.mbedSocket.connect();
});
}
bind(port, address, callback) {
this.dgramSocket.bind(port, address, callback);
}
address() {
return this.dgramSocket.address();
}
_onMessage(msg) {
if (!this.mbedSocket) {
return;
}
const data = this.mbedSocket.receiveData(msg);
if (data) {
this.push(data);
}
}
_read() {
// do nothing!
}
_write(chunk, encoding, callback) {
this._sendCallback = callback;
this.mbedSocket.send(chunk);
}
_sendEncrypted(msg) {
// store the callback here because '_write' might be called
// again before the underlying socket finishes sending
const sendCb = this._sendCallback;
this._sendCallback = null;
const sendFinished = (err) => {
if (sendCb) {
sendCb(err);
}
if (this._sendNotify) {
this._closeSocket();
}
};
if (!this.dgramSocket || !this.dgramSocket._handle) {
process.nextTick(() => {
sendFinished(new Error('no underlying socket'));
});
return;
}
this.dgramSocket.send(msg, 0, msg.length, this.remotePort, this.remoteAddress, sendFinished);
}
_handshakeComplete() {
this.connected = true;
this.emit('secureConnect', this);
}
_error(code, msg) {
if (code === MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
this._end();
return;
}
this._hadError = true;
if (this._sendCallback) {
this._sendCallback(code);
this._sendCallback = null;
} else {
this.emit('error', code, msg);
}
this._end();
}
end() {
this._sendNotify = true;
this._end();
}
_end() {
if (this._ending) {
return;
}
this._ending = true;
if (this.dgramSocket) {
this.dgramSocket.removeListener('message', this._onMessage);
}
super.end();
this.push(null);
const noSend = this.mbedSocket.close();
this.mbedSocket = null;
if (noSend || !this._sendNotify) {
this._closeSocket();
}
}
_closeSocket() {
if (!this.dgramSocket) {
this._socketClosed();
return;
}
this.dgramSocket.close();
}
_socketClosed() {
this.dgramSocket = null;
this.emit('close', this._hadError);
this.removeAllListeners();
}
}
module.exports = DtlsSocket;