-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclient_socket.js
188 lines (151 loc) · 4.55 KB
/
client_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
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
185
186
187
188
'use strict';
const stream = require('stream');
const dgram = require('dgram');
const fs = require('fs');
const mbed = require('./build/Release/node_mbed_dtls');
const MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY = -0x7880;
var send_safety_check = function(obj) {
console.log("send_safety_check()\n");
// make absolutely sure the socket will let us send
if (obj.dgramSocket && obj.dgramSocket._handle) {
obj.mbedSocket.connect();
}
else {
process.nextTick(() => {
send_safety_check(obj);
});
}
}
class DtlsClientSocket extends stream.Duplex {
constructor(options) {
super({ allowHalfOpen: false });
if(!options) options = {}; // Support no-parameter construction.
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 : false;
const peerPublicKey = Buffer.isBuffer(options.peerPublicKey) ? options.peerPublicKey : false;
const ca_cert = Buffer.isBuffer(options.CACert) ? options.CACert : false;
const psk = Buffer.isBuffer(options.psk) ? options.psk : false;
const psk_ident = Buffer.isBuffer(options.PSKIdent) ? options.PSKIdent : false;
this.mbedSocket = new mbed.DtlsClientSocket(
privateKey, peerPublicKey, // Keys (Buffers or FS paths)
ca_cert, // CA (Buffer)
psk, // PSK (Buffer)
psk_ident, // PSK ident (Buffer)
this._sendEncrypted.bind(this), // Callback
this._handshakeComplete.bind(this), // Callback
this._error.bind(this), // Callback
options.debug); // Verbosity (integer)
this.send = function(msg, offset, length, port, host, callback) {
this.mbedSocket.send(msg);
}
process.nextTick(() => {
send_safety_check(this);
});
}
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);
console.log('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 = DtlsClientSocket;