cliprdr.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. const type = require('../../core').type;
  2. const EventEmitter = require('events').EventEmitter;
  3. const caps = require('./caps');
  4. const log = require('../../core').log;
  5. const data = require('./data');
  6. /**
  7. * Cliprdr channel for all clipboard
  8. * capabilities exchange
  9. */
  10. class Cliprdr extends EventEmitter {
  11. constructor(transport) {
  12. super();
  13. this.transport = transport;
  14. // must be init via connect event
  15. this.userId = 0;
  16. this.serverCapabilities = [];
  17. this.clientCapabilities = [];
  18. }
  19. }
  20. /**
  21. * Client side of Cliprdr channel automata
  22. * @param transport
  23. */
  24. class Client extends Cliprdr {
  25. constructor(transport, fastPathTransport) {
  26. super(transport, fastPathTransport);
  27. this.transport.once('connect', (gccCore, userId, channelId) => {
  28. this.connect(gccCore, userId, channelId);
  29. }).on('close', function () {
  30. //this.emit('close');
  31. }).on('error', function (err) {
  32. //this.emit('error', err);
  33. });
  34. this.content = '';
  35. }
  36. /**
  37. * connect function
  38. * @param gccCore {type.Component(clientCoreData)}
  39. */
  40. connect(gccCore, userId, channelId) {
  41. this.gccCore = gccCore;
  42. this.userId = userId;
  43. this.channelId = channelId;
  44. this.transport.once('cliprdr', (s) => {
  45. this.recv(s);
  46. });
  47. }
  48. send(message) {
  49. this.transport.send('cliprdr', new type.Component([
  50. // Channel PDU Header
  51. new type.UInt32Le(message.size()),
  52. // CHANNEL_FLAG_FIRST | CHANNEL_FLAG_LAST | CHANNEL_FLAG_SHOW_PROTOCOL
  53. new type.UInt32Le(0x13),
  54. message
  55. ]));
  56. };
  57. recv(s) {
  58. s.offset = 18;
  59. const pdu = data.clipPDU().read(s), type = data.ClipPDUMsgType;
  60. switch (pdu.obj.header.obj.msgType.value) {
  61. case type.CB_MONITOR_READY:
  62. this.recvMonitorReadyPDU(s);
  63. break;
  64. case type.CB_FORMAT_LIST:
  65. this.recvFormatListPDU(s);
  66. break;
  67. case type.CB_FORMAT_LIST_RESPONSE:
  68. this.recvFormatListResponsePDU(s);
  69. break;
  70. case type.CB_FORMAT_DATA_REQUEST:
  71. this.recvFormatDataRequestPDU(s);
  72. break;
  73. case type.CB_FORMAT_DATA_RESPONSE:
  74. this.recvFormatDataResponsePDU(s);
  75. break;
  76. case type.CB_TEMP_DIRECTORY:
  77. break;
  78. case type.CB_CLIP_CAPS:
  79. this.recvClipboardCapsPDU(s);
  80. break;
  81. case type.CB_FILECONTENTS_REQUEST:
  82. }
  83. this.transport.once('cliprdr', (s) => {
  84. this.recv(s);
  85. });
  86. }
  87. /**
  88. * Receive capabilities from server
  89. * @param s {type.Stream}
  90. */
  91. recvClipboardCapsPDU(s) {
  92. // Start at 18
  93. s.offset = 18;
  94. // const pdu = data.clipPDU().read(s);
  95. // console.log('recvClipboardCapsPDU', s);
  96. }
  97. /**
  98. * Receive monitor ready from server
  99. * @param s {type.Stream}
  100. */
  101. recvMonitorReadyPDU(s) {
  102. s.offset = 18;
  103. // const pdu = data.clipPDU().read(s);
  104. // console.log('recvMonitorReadyPDU', s);
  105. this.sendClipboardCapsPDU();
  106. // this.sendClientTemporaryDirectoryPDU();
  107. this.sendFormatListPDU();
  108. }
  109. /**
  110. * Send clipboard capabilities PDU
  111. */
  112. sendClipboardCapsPDU() {
  113. this.send(new type.Component({
  114. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_CLIP_CAPS),
  115. msgFlags: new type.UInt16Le(0x00),
  116. dataLen: new type.UInt32Le(0x10),
  117. cCapabilitiesSets: new type.UInt16Le(0x01),
  118. pad1: new type.UInt16Le(0x00),
  119. capabilitySetType: new type.UInt16Le(0x01),
  120. lengthCapability: new type.UInt16Le(0x0c),
  121. version: new type.UInt32Le(0x02),
  122. capabilityFlags: new type.UInt32Le(0x02)
  123. }));
  124. }
  125. /**
  126. * Send client temporary directory PDU
  127. */
  128. sendClientTemporaryDirectoryPDU(path = '') {
  129. // TODO
  130. this.send(new type.Component({
  131. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_TEMP_DIRECTORY),
  132. msgFlags: new type.UInt16Le(0x00),
  133. dataLen: new type.UInt32Le(0x0208),
  134. wszTempDir: new type.BinaryString(Buffer.from('D:\\Vectors' + Array(251).join('\x00'), 'ucs2'), { readLength: new type.CallableValue(520) })
  135. }));
  136. }
  137. /**
  138. * Send format list PDU
  139. */
  140. sendFormatListPDU() {
  141. this.send(new type.Component({
  142. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_FORMAT_LIST),
  143. msgFlags: new type.UInt16Le(0x00),
  144. dataLen: new type.UInt32Le(0x24),
  145. formatId6: new type.UInt32Le(0xc004),
  146. formatName6: new type.BinaryString(Buffer.from('Native\x00', 'ucs2'), { readLength: new type.CallableValue(14) }),
  147. formatId8: new type.UInt32Le(0x0d),
  148. formatName8: new type.UInt16Le(0x00),
  149. formatId9: new type.UInt32Le(0x10),
  150. formatName9: new type.UInt16Le(0x00),
  151. formatId0: new type.UInt32Le(0x01),
  152. formatName0: new type.UInt16Le(0x00),
  153. // dataLen: new type.UInt32Le(0xe0),
  154. // formatId1: new type.UInt32Le(0xc08a),
  155. // formatName1: new type.BinaryString(Buffer.from('Rich Text Format\x00' , 'ucs2'), { readLength : new type.CallableValue(34)}),
  156. // formatId2: new type.UInt32Le(0xc145),
  157. // formatName2: new type.BinaryString(Buffer.from('Rich Text Format Without Objects\x00' , 'ucs2'), { readLength : new type.CallableValue(66)}),
  158. // formatId3: new type.UInt32Le(0xc143),
  159. // formatName3: new type.BinaryString(Buffer.from('RTF As Text\x00' , 'ucs2'), { readLength : new type.CallableValue(24)}),
  160. // formatId4: new type.UInt32Le(0x01),
  161. // formatName4: new type.BinaryString(0x00),
  162. formatId5: new type.UInt32Le(0x07),
  163. formatName5: new type.UInt16Le(0x00),
  164. // formatId6: new type.UInt32Le(0xc004),
  165. // formatName6: new type.BinaryString(Buffer.from('Native\x00' , 'ucs2'), { readLength : new type.CallableValue(14)}),
  166. // formatId7: new type.UInt32Le(0xc00e),
  167. // formatName7: new type.BinaryString(Buffer.from('Object Descriptor\x00' , 'ucs2'), { readLength : new type.CallableValue(36)}),
  168. // formatId8: new type.UInt32Le(0x03),
  169. // formatName8: new type.UInt16Le(0x00),
  170. // formatId9: new type.UInt32Le(0x10),
  171. // formatName9: new type.UInt16Le(0x00),
  172. // formatId0: new type.UInt32Le(0x07),
  173. // formatName0: new type.UInt16Le(0x00),
  174. }));
  175. }
  176. /**
  177. * Recvie format list PDU from server
  178. * @param {type.Stream} s
  179. */
  180. recvFormatListPDU(s) {
  181. s.offset = 18;
  182. // const pdu = data.clipPDU().read(s);
  183. // console.log('recvFormatListPDU', s);
  184. this.sendFormatListResponsePDU();
  185. }
  186. /**
  187. * Send format list reesponse
  188. */
  189. sendFormatListResponsePDU() {
  190. this.send(new type.Component({
  191. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_FORMAT_LIST_RESPONSE),
  192. msgFlags: new type.UInt16Le(0x01),
  193. dataLen: new type.UInt32Le(0x00),
  194. }));
  195. this.sendFormatDataRequestPDU();
  196. }
  197. /**
  198. * Receive format list response from server
  199. * @param s {type.Stream}
  200. */
  201. recvFormatListResponsePDU(s) {
  202. s.offset = 18;
  203. // const pdu = data.clipPDU().read(s);
  204. // console.log('recvFormatListResponsePDU', s);
  205. // this.sendFormatDataRequestPDU();
  206. }
  207. /**
  208. * Send format data request PDU
  209. */
  210. sendFormatDataRequestPDU(formartId = 0x0d) {
  211. this.send(new type.Component({
  212. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_FORMAT_DATA_REQUEST),
  213. msgFlags: new type.UInt16Le(0x00),
  214. dataLen: new type.UInt32Le(0x04),
  215. requestedFormatId: new type.UInt32Le(formartId),
  216. }));
  217. }
  218. /**
  219. * Receive format data request PDU from server
  220. * @param s {type.Stream}
  221. */
  222. recvFormatDataRequestPDU(s) {
  223. s.offset = 18;
  224. // const pdu = data.clipPDU().read(s);
  225. // console.log('recvFormatDataRequestPDU', s);
  226. this.sendFormatDataResponsePDU();
  227. }
  228. /**
  229. * Send format data reesponse PDU
  230. */
  231. sendFormatDataResponsePDU() {
  232. const bufs = Buffer.from(this.content + '\x00', 'ucs2');
  233. this.send(new type.Component({
  234. msgType: new type.UInt16Le(data.ClipPDUMsgType.CB_FORMAT_DATA_RESPONSE),
  235. msgFlags: new type.UInt16Le(0x01),
  236. dataLen: new type.UInt32Le(bufs.length),
  237. requestedFormatData: new type.BinaryString(bufs, { readLength: new type.CallableValue(bufs.length) })
  238. }));
  239. }
  240. /**
  241. * Receive format data response PDU from server
  242. * @param s {type.Stream}
  243. */
  244. recvFormatDataResponsePDU(s) {
  245. s.offset = 18;
  246. // const pdu = data.clipPDU().read(s);
  247. const str = s.buffer.toString('ucs2', 26, s.buffer.length - 2);
  248. // console.log('recvFormatDataResponsePDU', str);
  249. this.content = str;
  250. this.emit('clipboard', str)
  251. }
  252. // =====================================================================================
  253. setClipboardData(content) {
  254. this.content = content;
  255. this.sendFormatListPDU();
  256. }
  257. }
  258. module.exports = {
  259. Client
  260. }