layer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. var inherits = require('util').inherits;
  20. var fs = require('fs');
  21. var type = require('./type');
  22. var log = require('./log');
  23. var tls = require('tls');
  24. //var crypto = require('crypto');
  25. var events = require('events');
  26. /**
  27. * Buffer data from socket to present
  28. * well formed packets
  29. */
  30. function BufferLayer(socket) {
  31. //for ssl connection
  32. this.secureSocket = null;
  33. this.socket = socket;
  34. var self = this;
  35. // bind event
  36. this.socket.on('data', function(data) {
  37. try {
  38. self.recv(data);
  39. }
  40. catch(e) {
  41. self.socket.destroy();
  42. self.emit('error', e);
  43. }
  44. }).on('close', function() {
  45. self.emit('close');
  46. }).on('error', function (err) {
  47. self.emit('error', err);
  48. });
  49. //buffer data
  50. this.buffers = [];
  51. this.bufferLength = 0;
  52. //expected size
  53. this.expectedSize = 0;
  54. }
  55. inherits(BufferLayer, events.EventEmitter);
  56. /**
  57. * Call from tcp layer
  58. * @param data tcp stream
  59. */
  60. BufferLayer.prototype.recv = function (data) {
  61. if (this.buffers.length == 0) { this.bufferLength = 0; } // CORRECT
  62. this.buffers[this.buffers.length] = data;
  63. this.bufferLength += data.length;
  64. //console.log('TCP RECV', this.bufferLength, this.expectedSize, data.toString('hex'));
  65. //console.log('this.buffers', this.buffers);
  66. //console.log('this.expectedSize', this.expectedSize);
  67. //console.log('this.bufferLength', this.bufferLength);
  68. if (this.expectedSize == 0) { console.log('this.expectedSize == 0'); return; }
  69. while (this.bufferLength >= this.expectedSize) {
  70. //console.log('this.expectedSize', this.expectedSize);
  71. //console.log('this.bufferLength', this.bufferLength);
  72. //linear buffer
  73. var expectedData = new type.Stream(this.expectedSize);
  74. //create expected data
  75. while (expectedData.availableLength() > 0) {
  76. var rest = expectedData.availableLength();
  77. var buffer = this.buffers.shift();
  78. //console.log('xx', rest, buffer);
  79. if (buffer.length > expectedData.availableLength()) {
  80. this.buffers.unshift(buffer.slice(rest));
  81. new type.BinaryString(buffer, { readLength : new type.CallableValue(expectedData.availableLength()) }).write(expectedData);
  82. } else {
  83. new type.BinaryString(buffer).write(expectedData);
  84. }
  85. }
  86. this.bufferLength -= this.expectedSize;
  87. expectedData.offset = 0;
  88. //console.log('TCP EMIT', expectedData);
  89. this.emit('data', expectedData);
  90. }
  91. };
  92. /**
  93. * Call tcp socket to write stream
  94. * @param {type.Type} packet
  95. */
  96. BufferLayer.prototype.send = function(data) {
  97. var s = new type.Stream(data.size());
  98. data.write(s);
  99. if(this.secureSocket) {
  100. this.secureSocket.write(s.buffer);
  101. }
  102. else {
  103. this.socket.write(s.buffer);
  104. }
  105. };
  106. /**
  107. * Call tcp socket to write a buffer
  108. */
  109. BufferLayer.prototype.sendBuffer = function (buffer) {
  110. if (this.secureSocket) {
  111. //console.log('SSL sendBuffer', buffer.length, buffer.toString('hex'));
  112. this.secureSocket.write(buffer);
  113. }
  114. else {
  115. //console.log('TCP sendBuffer', buffer.length, buffer.toString('hex'));
  116. this.socket.write(buffer);
  117. }
  118. };
  119. /**
  120. * Wait expected size data before call callback function
  121. * @param {number} expectSize size expected
  122. */
  123. BufferLayer.prototype.expect = function(expectedSize) {
  124. this.expectedSize = expectedSize;
  125. };
  126. /**
  127. * Convert connection to TLS connection
  128. * @param callback {func} when connection is done
  129. */
  130. BufferLayer.prototype.startTLS = function(callback) {
  131. var self = this;
  132. this.secureSocket = tls.connect({
  133. socket: this.socket,
  134. secureContext: tls.createSecureContext(),
  135. isServer: false,
  136. requestCert: false,
  137. rejectUnauthorized: false
  138. }, (err) => {
  139. log.warn(err);
  140. callback(err);
  141. });
  142. this.secureSocket.on('data', function (data) {
  143. //console.log('SSL RECV', data.length, data);
  144. try {
  145. self.recv(data);
  146. }
  147. catch (e) {
  148. //console.log('SSL RECV ERR', e);
  149. self.socket.destroy();
  150. self.emit('error', e);
  151. }
  152. }).on('error', function (err) {
  153. self.emit('error', err);
  154. });
  155. };
  156. /**
  157. * Convert connection to TLS server
  158. * @param keyFilePath {string} key file path
  159. * @param crtFilePath {string} certificat file path
  160. * @param callback {function}
  161. */
  162. BufferLayer.prototype.listenTLS = function(keyFilePath, crtFilePath, callback) {
  163. var self = this;
  164. this.secureSocket = tls.connect({
  165. socket: this.socket,
  166. secureContext: tls.createSecureContext({
  167. key: fs.readFileSync(keyFilePath),
  168. cert: fs.readFileSync(crtFilePath),
  169. }),
  170. isServer: true,
  171. requestCert: false,
  172. rejectUnauthorized: false
  173. }, (err) => {
  174. log.warn(err);
  175. callback(err);
  176. });
  177. this.secureSocket.on('data', function(data) {
  178. try {
  179. self.recv(data);
  180. }
  181. catch(e) {
  182. self.socket.destroy();
  183. self.emit('error', e);
  184. }
  185. }).on('error', function (err) {
  186. self.emit('error', err);
  187. });
  188. };
  189. /**
  190. * close stack
  191. */
  192. BufferLayer.prototype.close = function() {
  193. this.socket.end();
  194. };
  195. /**
  196. * Module exports
  197. */
  198. module.exports = {
  199. BufferLayer : BufferLayer
  200. };