forked from citronneur/node-rdpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.js
More file actions
194 lines (175 loc) · 4.5 KB
/
layer.js
File metadata and controls
194 lines (175 loc) · 4.5 KB
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
189
190
191
192
193
194
/*
* Copyright (c) 2014-2015 Sylvain Peyrefitte
*
* This file is part of node-rdpjs.
*
* node-rdpjs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var inherits = require('util').inherits;
var fs = require('fs');
var type = require('./type');
var log = require('./log');
var starttls = require('starttls');
var tls = require('tls');
var crypto = require('crypto');
var events = require('events');
/**
* Buffer data from socket to present
* well formed packets
*/
function BufferLayer(socket) {
//for ssl connection
this.securePair = null;
this.socket = socket;
var self = this;
// bind event
this.socket.on('data', function(data) {
try {
self.recv(data);
}
catch(e) {
self.socket.destroy();
self.emit('error', e);
}
}).on('close', function() {
self.emit('close');
}).on('error', function (err) {
self.emit('error', err);
});
//buffer data
this.buffers = [];
this.bufferLength = 0;
//expected size
this.expectedSize = 0;
}
inherits(BufferLayer, events.EventEmitter);
/**
* Call from tcp layer
* @param data tcp stream
*/
BufferLayer.prototype.recv = function(data) {
this.buffers[this.buffers.length] = data;
this.bufferLength += data.length;
while(this.bufferLength >= this.expectedSize) {
//linear buffer
var expectedData = new type.Stream(this.expectedSize);
//create expected data
while(expectedData.availableLength() > 0) {
var rest = expectedData.availableLength();
var buffer = this.buffers.shift();
if(buffer.length > expectedData.availableLength()) {
this.buffers.unshift(buffer.slice(rest));
new type.BinaryString(buffer, { readLength : new type.CallableValue(expectedData.availableLength()) }).write(expectedData);
}
else {
new type.BinaryString(buffer).write(expectedData);
}
}
this.bufferLength -= this.expectedSize;
expectedData.offset = 0;
this.emit('data', expectedData);
}
};
/**
* Call tcp socket to write stream
* @param {type.Type} packet
*/
BufferLayer.prototype.send = function(data) {
var s = new type.Stream(data.size());
data.write(s);
if(this.securePair) {
this.securePair.cleartext.write(s.buffer);
}
else {
this.socket.write(s.buffer);
}
};
/**
* Wait expected size data before call callback function
* @param {number} expectSize size expected
*/
BufferLayer.prototype.expect = function(expectedSize) {
this.expectedSize = expectedSize;
};
/**
* Convert connection to TLS connection
* Use nodejs starttls module
* @param callback {func} when connection is done
*/
BufferLayer.prototype.startTLS = function(callback) {
var options = {
socket : this.socket,
pair : tls.createSecurePair(tls.createSecureContext(), false, false, false)
};
var self = this;
this.securePair = starttls(options, function(err) {
log.warn(err);
callback();
})
this.securePair.cleartext.on('data', function(data) {
try {
self.recv(data);
}
catch(e) {
self.socket.destroy();
self.emit('error', e);
}
}).on('error', function (err) {
self.emit('error', err);
});
};
/**
* Convert connection to TLS server
* @param keyFilePath {string} key file path
* @param crtFilePath {string} certificat file path
* @param callback {function}
*/
BufferLayer.prototype.listenTLS = function(keyFilePath, crtFilePath, callback) {
var options = {
socket : this.socket,
pair : tls.createSecurePair(tls.createSecureContext({
key: fs.readFileSync(keyFilePath),
cert: fs.readFileSync(crtFilePath),
}), true, false, false)
};
var self = this;
this.securePair = starttls(options, function(err) {
log.warn(err);
self.cleartext = this.cleartext;
callback();
});
this.securePair.cleartext.on('data', function(data) {
try {
self.recv(data);
}
catch(e) {
self.socket.destroy();
self.emit('error', e);
}
}).on('error', function (err) {
self.emit('error', err);
});
};
/**
* close stack
*/
BufferLayer.prototype.close = function() {
this.socket.end();
};
/**
* Module exports
*/
module.exports = {
BufferLayer : BufferLayer
};