| 1 | /* |
| 2 | * Copyright (c) 2014-2015 Sylvain Peyrefitte |
| 3 | * |
| 4 | * This file is part of node-rdpjs. |
| 5 | * |
| 6 | * node-rdpjs is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | var inherits = require('util').inherits; |
| 21 | var fs = require('fs'); |
| 22 | var type = require('./type'); |
| 23 | var log = require('./log'); |
| 24 | var tls = require('tls'); |
| 25 | //var crypto = require('crypto'); |
| 26 | var events = require('events'); |
| 27 | |
| 28 | /** |
| 29 | * Buffer data from socket to present |
| 30 | * well formed packets |
| 31 | */ |
| 32 | function BufferLayer(socket) { |
| 33 | //for ssl connection |
| 34 | this.secureSocket = null; |
| 35 | this.socket = socket; |
| 36 | |
| 37 | var self = this; |
| 38 | // bind event |
| 39 | this.socket.on('data', function(data) { |
| 40 | try { |
| 41 | self.recv(data); |
| 42 | } |
| 43 | catch(e) { |
| 44 | self.socket.destroy(); |
| 45 | self.emit('error', e); |
| 46 | } |
| 47 | }).on('close', function() { |
| 48 | self.emit('close'); |
| 49 | }).on('error', function (err) { |
| 50 | self.emit('error', err); |
| 51 | }); |
| 52 | |
| 53 | //buffer data |
| 54 | this.buffers = []; |
| 55 | this.bufferLength = 0; |
| 56 | //expected size |
| 57 | this.expectedSize = 0; |
| 58 | } |
| 59 | |
| 60 | inherits(BufferLayer, events.EventEmitter); |
| 61 | |
| 62 | /** |
| 63 | * Call from tcp layer |
| 64 | * @param data tcp stream |
| 65 | */ |
| 66 | BufferLayer.prototype.recv = function (data) { |
| 67 | if (this.buffers.length == 0) { this.bufferLength = 0; } // CORRECT |
| 68 | this.buffers[this.buffers.length] = data; |
| 69 | this.bufferLength += data.length; |
| 70 | |
| 71 | //console.log('TCP RECV', this.bufferLength, this.expectedSize, data.toString('hex')); |
| 72 | //console.log('this.buffers', this.buffers); |
| 73 | //console.log('this.expectedSize', this.expectedSize); |
| 74 | //console.log('this.bufferLength', this.bufferLength); |
| 75 | |
| 76 | if (this.expectedSize == 0) { console.log('this.expectedSize == 0'); return; } |
| 77 | |
| 78 | while (this.bufferLength >= this.expectedSize) { |
| 79 | //console.log('this.expectedSize', this.expectedSize); |
| 80 | //console.log('this.bufferLength', this.bufferLength); |
| 81 | |
| 82 | //linear buffer |
| 83 | var expectedData = new type.Stream(this.expectedSize); |
| 84 | |
| 85 | //create expected data |
| 86 | while (expectedData.availableLength() > 0) { |
| 87 | |
| 88 | var rest = expectedData.availableLength(); |
| 89 | var buffer = this.buffers.shift(); |
| 90 | |
| 91 | //console.log('xx', rest, buffer); |
| 92 | |
| 93 | if (buffer.length > expectedData.availableLength()) { |
| 94 | this.buffers.unshift(buffer.slice(rest)); |
| 95 | new type.BinaryString(buffer, { readLength : new type.CallableValue(expectedData.availableLength()) }).write(expectedData); |
| 96 | } else { |
| 97 | new type.BinaryString(buffer).write(expectedData); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | this.bufferLength -= this.expectedSize; |
| 102 | expectedData.offset = 0; |
| 103 | |
| 104 | //console.log('TCP EMIT', expectedData); |
| 105 | this.emit('data', expectedData); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * Call tcp socket to write stream |
| 111 | * @param {type.Type} packet |
| 112 | */ |
| 113 | BufferLayer.prototype.send = function(data) { |
| 114 | var s = new type.Stream(data.size()); |
| 115 | data.write(s); |
| 116 | if(this.secureSocket) { |
| 117 | this.secureSocket.write(s.buffer); |
| 118 | } |
| 119 | else { |
| 120 | this.socket.write(s.buffer); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Call tcp socket to write a buffer |
| 126 | */ |
| 127 | BufferLayer.prototype.sendBuffer = function (buffer) { |
| 128 | if (this.secureSocket) { |
| 129 | //console.log('SSL sendBuffer', buffer.length, buffer.toString('hex')); |
| 130 | this.secureSocket.write(buffer); |
| 131 | } |
| 132 | else { |
| 133 | //console.log('TCP sendBuffer', buffer.length, buffer.toString('hex')); |
| 134 | this.socket.write(buffer); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * Wait expected size data before call callback function |
| 140 | * @param {number} expectSize size expected |
| 141 | */ |
| 142 | BufferLayer.prototype.expect = function(expectedSize) { |
| 143 | this.expectedSize = expectedSize; |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * Convert connection to TLS connection |
| 148 | * @param callback {func} when connection is done |
| 149 | */ |
| 150 | BufferLayer.prototype.startTLS = function(callback) { |
| 151 | var self = this; |
| 152 | |
| 153 | this.secureSocket = tls.connect({ |
| 154 | socket: this.socket, |
| 155 | secureContext: tls.createSecureContext(), |
| 156 | isServer: false, |
| 157 | requestCert: false, |
| 158 | rejectUnauthorized: false |
| 159 | }, (err) => { |
| 160 | log.warn(err); |
| 161 | callback(err); |
| 162 | }); |
| 163 | |
| 164 | this.secureSocket.on('data', function (data) { |
| 165 | |
| 166 | //console.log('SSL RECV', data.length, data); |
| 167 | |
| 168 | try { |
| 169 | self.recv(data); |
| 170 | } |
| 171 | catch (e) { |
| 172 | //console.log('SSL RECV ERR', e); |
| 173 | self.socket.destroy(); |
| 174 | self.emit('error', e); |
| 175 | } |
| 176 | }).on('error', function (err) { |
| 177 | self.emit('error', err); |
| 178 | }); |
| 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * Convert connection to TLS server |
| 183 | * @param keyFilePath {string} key file path |
| 184 | * @param crtFilePath {string} certificat file path |
| 185 | * @param callback {function} |
| 186 | */ |
| 187 | BufferLayer.prototype.listenTLS = function(keyFilePath, crtFilePath, callback) { |
| 188 | var self = this; |
| 189 | |
| 190 | this.secureSocket = tls.connect({ |
| 191 | socket: this.socket, |
| 192 | secureContext: tls.createSecureContext({ |
| 193 | key: fs.readFileSync(keyFilePath), |
| 194 | cert: fs.readFileSync(crtFilePath), |
| 195 | }), |
| 196 | isServer: true, |
| 197 | requestCert: false, |
| 198 | rejectUnauthorized: false |
| 199 | }, (err) => { |
| 200 | log.warn(err); |
| 201 | callback(err); |
| 202 | }); |
| 203 | |
| 204 | this.secureSocket.on('data', function(data) { |
| 205 | try { |
| 206 | self.recv(data); |
| 207 | } |
| 208 | catch(e) { |
| 209 | self.socket.destroy(); |
| 210 | self.emit('error', e); |
| 211 | } |
| 212 | }).on('error', function (err) { |
| 213 | self.emit('error', err); |
| 214 | }); |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * close stack |
| 219 | */ |
| 220 | BufferLayer.prototype.close = function() { |
| 221 | this.socket.end(); |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * Module exports |
| 226 | */ |
| 227 | module.exports = { |
| 228 | BufferLayer : BufferLayer |
| 229 | }; |