rdp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 net = require('net');
  20. var inherits = require('util').inherits;
  21. var events = require('events');
  22. var layer = require('../core').layer;
  23. var error = require('../core').error;
  24. var rle = require('../core').rle;
  25. var log = require('../core').log;
  26. var TPKT = require('./tpkt');
  27. var x224 = require('./x224');
  28. var t125 = require('./t125');
  29. var pdu = require('./pdu');
  30. /**
  31. * decompress bitmap from RLE algorithm
  32. * @param bitmap {object} bitmap object of bitmap event of node-rdpjs
  33. */
  34. function decompress(bitmap) {
  35. var fName = null;
  36. switch (bitmap.bitsPerPixel.value) {
  37. case 15:
  38. fName = 'bitmap_decompress_15';
  39. break;
  40. case 16:
  41. fName = 'bitmap_decompress_16';
  42. break;
  43. case 24:
  44. fName = 'bitmap_decompress_24';
  45. break;
  46. case 32:
  47. fName = 'bitmap_decompress_32';
  48. break;
  49. default:
  50. throw 'invalid bitmap data format';
  51. }
  52. var input = new Uint8Array(bitmap.bitmapDataStream.value);
  53. var inputPtr = rle._malloc(input.length);
  54. var inputHeap = new Uint8Array(rle.HEAPU8.buffer, inputPtr, input.length);
  55. inputHeap.set(input);
  56. var ouputSize = bitmap.width.value * bitmap.height.value * 4;
  57. var outputPtr = rle._malloc(ouputSize);
  58. var outputHeap = new Uint8Array(rle.HEAPU8.buffer, outputPtr, ouputSize);
  59. var res = rle.ccall(fName,
  60. 'number',
  61. ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'],
  62. [outputHeap.byteOffset, bitmap.width.value, bitmap.height.value, bitmap.width.value, bitmap.height.value, inputHeap.byteOffset, input.length]
  63. );
  64. var output = new Uint8ClampedArray(outputHeap.buffer, outputHeap.byteOffset, ouputSize);
  65. rle._free(inputPtr);
  66. rle._free(outputPtr);
  67. return output;
  68. }
  69. /**
  70. * Main RDP module
  71. */
  72. function RdpClient(config) {
  73. config = config || {};
  74. this.connected = false;
  75. this.bufferLayer = new layer.BufferLayer(new net.Socket());
  76. this.tpkt = new TPKT(this.bufferLayer);
  77. this.x224 = new x224.Client(this.tpkt, config);
  78. this.mcs = new t125.mcs.Client(this.x224);
  79. this.sec = new pdu.sec.Client(this.mcs, this.tpkt);
  80. this.cliprdr = new pdu.cliprdr.Client(this.mcs);
  81. this.global = new pdu.global.Client(this.sec, this.sec);
  82. // config log level
  83. log.level = log.Levels[config.logLevel || 'INFO'] || log.Levels.INFO;
  84. // credentials
  85. if (config.domain) {
  86. this.sec.infos.obj.domain.value = Buffer.from(config.domain + '\x00', 'ucs2');
  87. }
  88. if (config.userName) {
  89. this.sec.infos.obj.userName.value = Buffer.from(config.userName + '\x00', 'ucs2');
  90. }
  91. if (config.password) {
  92. this.sec.infos.obj.password.value = Buffer.from(config.password + '\x00', 'ucs2');
  93. }
  94. if (config.workingDir) {
  95. this.sec.infos.obj.workingDir.value = Buffer.from(config.workingDir + '\x00', 'ucs2');
  96. }
  97. if (config.alternateShell) {
  98. this.sec.infos.obj.alternateShell.value = Buffer.from(config.alternateShell + '\x00', 'ucs2');
  99. }
  100. if (config.perfFlags != null) {
  101. this.sec.infos.obj.extendedInfo.obj.performanceFlags.value = config.perfFlags;
  102. } else {
  103. if (config.enablePerf) {
  104. this.sec.infos.obj.extendedInfo.obj.performanceFlags.value =
  105. pdu.sec.PerfFlag.PERF_DISABLE_WALLPAPER
  106. | pdu.sec.PerfFlag.PERF_DISABLE_MENUANIMATIONS
  107. | pdu.sec.PerfFlag.PERF_DISABLE_CURSOR_SHADOW
  108. | pdu.sec.PerfFlag.PERF_DISABLE_THEMING
  109. | pdu.sec.PerfFlag.PERF_DISABLE_FULLWINDOWDRAG;
  110. }
  111. }
  112. if (config.autoLogin) {
  113. this.sec.infos.obj.flag.value |= pdu.sec.InfoFlag.INFO_AUTOLOGON;
  114. }
  115. if (config.screen && config.screen.width && config.screen.height) {
  116. this.mcs.clientCoreData.obj.desktopWidth.value = config.screen.width;
  117. this.mcs.clientCoreData.obj.desktopHeight.value = config.screen.height;
  118. }
  119. log.debug('screen ' + this.mcs.clientCoreData.obj.desktopWidth.value + 'x' + this.mcs.clientCoreData.obj.desktopHeight.value);
  120. // config keyboard layout
  121. switch (config.locale) {
  122. case 'fr':
  123. log.debug('french keyboard layout');
  124. this.mcs.clientCoreData.obj.kbdLayout.value = t125.gcc.KeyboardLayout.FRENCH;
  125. break;
  126. case 'en':
  127. default:
  128. log.debug('english keyboard layout');
  129. this.mcs.clientCoreData.obj.kbdLayout.value = t125.gcc.KeyboardLayout.US;
  130. }
  131. this.cliprdr.on('clipboard', (content) => {
  132. this.emit('clipboard', content)
  133. });
  134. //bind all events
  135. var self = this;
  136. this.global.on('connect', function () {
  137. self.connected = true;
  138. self.emit('connect');
  139. }).on('session', function () {
  140. self.emit('session');
  141. }).on('close', function () {
  142. self.connected = false;
  143. self.emit('close');
  144. }).on('pointer', function (cursorId, cursorStr) {
  145. self.emit('pointer', cursorId, cursorStr);
  146. }).on('bitmap', function (bitmaps) {
  147. for (var bitmap in bitmaps) {
  148. var bitmapData = bitmaps[bitmap].obj.bitmapDataStream.value;
  149. var isCompress = bitmaps[bitmap].obj.flags.value & pdu.data.BitmapFlag.BITMAP_COMPRESSION;
  150. if (isCompress && config.decompress) {
  151. bitmapData = decompress(bitmaps[bitmap].obj);
  152. isCompress = false;
  153. }
  154. self.emit('bitmap', {
  155. destTop: bitmaps[bitmap].obj.destTop.value,
  156. destLeft: bitmaps[bitmap].obj.destLeft.value,
  157. destBottom: bitmaps[bitmap].obj.destBottom.value,
  158. destRight: bitmaps[bitmap].obj.destRight.value,
  159. width: bitmaps[bitmap].obj.width.value,
  160. height: bitmaps[bitmap].obj.height.value,
  161. bitsPerPixel: bitmaps[bitmap].obj.bitsPerPixel.value,
  162. isCompress: isCompress,
  163. data: bitmapData
  164. });
  165. }
  166. }).on('error', function (err) {
  167. log.warn(err.code + '(' + err.message + ')\n' + err.stack);
  168. if (err instanceof error.FatalError) { throw err; } else { self.emit('error', err); }
  169. });
  170. }
  171. inherits(RdpClient, events.EventEmitter);
  172. /**
  173. * Connect RDP client
  174. * @param host {string} destination host
  175. * @param port {integer} destination port
  176. */
  177. RdpClient.prototype.connect = function (host, port) {
  178. log.debug('connect to ' + host + ':' + port);
  179. var self = this;
  180. this.bufferLayer.socket.connect(port, host, function () {
  181. // in client mode connection start from x224 layer
  182. self.x224.connect();
  183. });
  184. return this;
  185. };
  186. /**
  187. * Close RDP client
  188. */
  189. RdpClient.prototype.close = function () {
  190. if (this.connected) {
  191. this.global.close();
  192. }
  193. this.connected = false;
  194. return this;
  195. };
  196. /**
  197. * Send pointer event to server
  198. * @param x {integer} mouse x position
  199. * @param y {integer} mouse y position
  200. * @param button {integer} button number of mouse
  201. * @param isPressed {boolean} state of button
  202. */
  203. RdpClient.prototype.sendPointerEvent = function (x, y, button, isPressed) {
  204. if (!this.connected)
  205. return;
  206. var event = pdu.data.pointerEvent();
  207. if (isPressed) {
  208. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_DOWN;
  209. }
  210. switch (button) {
  211. case 1:
  212. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_BUTTON1;
  213. break;
  214. case 2:
  215. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_BUTTON2;
  216. break;
  217. case 3:
  218. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_BUTTON3
  219. break;
  220. default:
  221. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_MOVE;
  222. }
  223. event.obj.xPos.value = x;
  224. event.obj.yPos.value = y;
  225. this.global.sendInputEvents([event]);
  226. };
  227. /**
  228. * send scancode event
  229. * @param code {integer}
  230. * @param isPressed {boolean}
  231. * @param extended {boolenan} extended keys
  232. */
  233. RdpClient.prototype.sendKeyEventScancode = function (code, isPressed, extended) {
  234. if (!this.connected)
  235. return;
  236. extended = extended || false;
  237. var event = pdu.data.scancodeKeyEvent();
  238. event.obj.keyCode.value = code;
  239. if (!isPressed) {
  240. event.obj.keyboardFlags.value |= pdu.data.KeyboardFlag.KBDFLAGS_RELEASE;
  241. }
  242. if (extended) {
  243. event.obj.keyboardFlags.value |= pdu.data.KeyboardFlag.KBDFLAGS_EXTENDED;
  244. }
  245. this.global.sendInputEvents([event]);
  246. };
  247. /**
  248. * Send key event as unicode
  249. * @param code {integer}
  250. * @param isPressed {boolean}
  251. */
  252. RdpClient.prototype.sendKeyEventUnicode = function (code, isPressed) {
  253. if (!this.connected)
  254. return;
  255. var event = pdu.data.unicodeKeyEvent();
  256. event.obj.unicode.value = code;
  257. if (!isPressed) {
  258. event.obj.keyboardFlags.value |= pdu.data.KeyboardFlag.KBDFLAGS_RELEASE;
  259. }
  260. this.global.sendInputEvents([event]);
  261. }
  262. /**
  263. * Wheel mouse event
  264. * @param x {integer} mouse x position
  265. * @param y {integer} mouse y position
  266. * @param step {integer} wheel step
  267. * @param isNegative {boolean}
  268. * @param isHorizontal {boolean}
  269. */
  270. RdpClient.prototype.sendWheelEvent = function (x, y, step, isNegative, isHorizontal) {
  271. if (!this.connected)
  272. return;
  273. var event = pdu.data.pointerEvent();
  274. if (isHorizontal) {
  275. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_HWHEEL;
  276. }
  277. else {
  278. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_WHEEL;
  279. }
  280. if (isNegative) {
  281. event.obj.pointerFlags.value |= pdu.data.PointerFlag.PTRFLAGS_WHEEL_NEGATIVE;
  282. }
  283. event.obj.pointerFlags.value |= (step & pdu.data.PointerFlag.WheelRotationMask)
  284. event.obj.xPos.value = x;
  285. event.obj.yPos.value = y;
  286. this.global.sendInputEvents([event]);
  287. }
  288. /**
  289. * Clipboard event
  290. * @param data {String} content for clipboard
  291. */
  292. RdpClient.prototype.setClipboardData = function (content) {
  293. this.cliprdr.setClipboardData(content);
  294. }
  295. function createClient(config) {
  296. return new RdpClient(config);
  297. };
  298. /**
  299. * RDP server side protocol
  300. * @param config {object} configuration
  301. * @param socket {net.Socket}
  302. */
  303. function RdpServer(config, socket) {
  304. if (!(config.key && config.cert)) {
  305. throw new error.FatalError('NODE_RDP_PROTOCOL_RDP_SERVER_CONFIG_MISSING', 'missing cryptographic tools')
  306. }
  307. this.connected = false;
  308. this.bufferLayer = new layer.BufferLayer(socket);
  309. this.tpkt = new TPKT(this.bufferLayer);
  310. this.x224 = new x224.Server(this.tpkt, config.key, config.cert);
  311. this.mcs = new t125.mcs.Server(this.x224);
  312. };
  313. inherits(RdpServer, events.EventEmitter);
  314. function createServer(config, next) {
  315. return net.createServer(function (socket) {
  316. next(new RdpServer(config, socket));
  317. });
  318. };
  319. /**
  320. * Module exports
  321. */
  322. module.exports = {
  323. createClient: createClient,
  324. createServer: createServer
  325. };