amt-wsman-comm.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. Copyright 2020-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. @description Intel AMT WSMAN communication module for NodeJS
  13. @author Ylian Saint-Hilaire
  14. @version v0.3.0
  15. */
  16. /*jslint node: true */
  17. /*jshint node: true */
  18. /*jshint strict:false */
  19. /*jshint -W097 */
  20. /*jshint esversion: 6 */
  21. "use strict";
  22. // Construct a WSMAN stack communication object
  23. var CreateWsmanComm = function (host, port, user, pass, tls, tlsoptions, mpsConnection) {
  24. //console.log('CreateWsmanComm', host, port, user, pass, tls, tlsoptions);
  25. var obj = {};
  26. obj.PendingAjax = []; // List of pending AJAX calls. When one frees up, another will start.
  27. obj.ActiveAjaxCount = 0; // Number of currently active AJAX calls
  28. obj.MaxActiveAjaxCount = 1; // Maximum number of activate AJAX calls at the same time.
  29. obj.FailAllError = 0; // Set this to non-zero to fail all AJAX calls with that error status, 999 causes responses to be silent.
  30. obj.challengeParams = null;
  31. obj.noncecounter = 1;
  32. obj.authcounter = 0;
  33. obj.net = require('net');
  34. obj.tls = require('tls');
  35. obj.crypto = require('crypto');
  36. obj.constants = require('constants');
  37. obj.socket = null;
  38. obj.socketState = 0;
  39. obj.kerberosDone = 0;
  40. obj.amtVersion = null;
  41. obj.Address = '/wsman';
  42. obj.cnonce = obj.crypto.randomBytes(16).toString('hex'); // Generate a random client nonce
  43. obj.host = host;
  44. obj.port = port;
  45. obj.user = user;
  46. obj.pass = pass;
  47. obj.xtls = tls;
  48. obj.xtlsoptions = tlsoptions;
  49. obj.mpsConnection = mpsConnection; // Link to a MPS connection, this can be CIRA, Relay or LMS. If null, local sockets are used as transport.
  50. obj.xtlsFingerprint;
  51. obj.xtlsCertificate = null;
  52. obj.xtlsCheck = 0; // 0 = No TLS, 1 = CA Checked, 2 = Pinned, 3 = Untrusted
  53. obj.xtlsSkipHostCheck = 0;
  54. obj.xtlsMethod = 0;
  55. obj.xtlsDataReceived = false;
  56. obj.digestRealmMatch = null;
  57. obj.digestRealm = null;
  58. // Private method
  59. obj.Debug = function (msg) { console.log(msg); }
  60. // Used to add TLS to a steam
  61. function SerialTunnel(options) {
  62. var obj = new require('stream').Duplex(options);
  63. obj.forwardwrite = null;
  64. obj.updateBuffer = function (chunk) { try { this.push(chunk); } catch (ex) { } };
  65. obj._write = function (chunk, encoding, callback) { if (obj.forwardwrite != null) { obj.forwardwrite(chunk); } else { console.err("Failed to fwd _write."); } if (callback) callback(); }; // Pass data written to forward
  66. obj._read = function (size) { }; // Push nothing, anything to read should be pushed from updateBuffer()
  67. return obj;
  68. }
  69. // Private method
  70. // pri = priority, if set to 1, the call is high priority and put on top of the stack.
  71. obj.PerformAjax = function (postdata, callback, tag, pri, url, action) {
  72. if ((obj.ActiveAjaxCount == 0 || ((obj.ActiveAjaxCount < obj.MaxActiveAjaxCount) && (obj.challengeParams != null))) && obj.PendingAjax.length == 0) {
  73. // There are no pending AJAX calls, perform the call now.
  74. obj.PerformAjaxEx(postdata, callback, tag, url, action);
  75. } else {
  76. // If this is a high priority call, put this call in front of the array, otherwise put it in the back.
  77. if (pri == 1) { obj.PendingAjax.unshift([postdata, callback, tag, url, action]); } else { obj.PendingAjax.push([postdata, callback, tag, url, action]); }
  78. }
  79. }
  80. // Private method
  81. obj.PerformNextAjax = function () {
  82. if (obj.ActiveAjaxCount >= obj.MaxActiveAjaxCount || obj.PendingAjax.length == 0) return;
  83. var x = obj.PendingAjax.shift();
  84. obj.PerformAjaxEx(x[0], x[1], x[2], x[3], x[4]);
  85. obj.PerformNextAjax();
  86. }
  87. // Private method
  88. obj.PerformAjaxEx = function (postdata, callback, tag, url, action) {
  89. if (obj.FailAllError != 0) { obj.gotNextMessagesError({ status: obj.FailAllError }, 'error', null, [postdata, callback, tag, url, action]); return; }
  90. if (!postdata) postdata = '';
  91. //obj.Debug('SEND: ' + postdata); // DEBUG
  92. obj.ActiveAjaxCount++;
  93. return obj.PerformAjaxExNodeJS(postdata, callback, tag, url, action);
  94. }
  95. // NODE.js specific private method
  96. obj.pendingAjaxCall = [];
  97. // NODE.js specific private method
  98. obj.PerformAjaxExNodeJS = function (postdata, callback, tag, url, action) { obj.PerformAjaxExNodeJS2(postdata, callback, tag, url, action, 5); }
  99. // NODE.js specific private method
  100. obj.PerformAjaxExNodeJS2 = function (postdata, callback, tag, url, action, retry) {
  101. if ((retry <= 0) || (obj.FailAllError != 0)) {
  102. // Too many retry, fail here.
  103. obj.ActiveAjaxCount--;
  104. if (obj.FailAllError != 999) obj.gotNextMessages(null, 'error', { status: ((obj.FailAllError == 0) ? 408 : obj.FailAllError) }, [postdata, callback, tag, url, action]); // 408 is timeout error
  105. obj.PerformNextAjax();
  106. return;
  107. }
  108. obj.pendingAjaxCall.push([postdata, callback, tag, url, action, retry]);
  109. if (obj.socketState == 0) { obj.xxConnectHttpSocket(); }
  110. else if (obj.socketState == 2) { obj.sendRequest(postdata, url, action); }
  111. }
  112. // NODE.js specific private method
  113. obj.sendRequest = function (postdata, url, action) {
  114. url = url ? url : '/wsman';
  115. action = action ? action : 'POST';
  116. var h = action + ' ' + url + ' HTTP/1.1\r\n';
  117. if (obj.challengeParams != null) {
  118. obj.digestRealm = obj.challengeParams['realm'];
  119. if (obj.digestRealmMatch && (obj.digestRealm != obj.digestRealmMatch)) {
  120. obj.FailAllError = 997; // Cause all new responses to be silent. 997 = Digest Realm check error
  121. obj.CancelAllQueries(997);
  122. return;
  123. }
  124. }
  125. if ((obj.user == '*') && (kerberos != null)) {
  126. // Kerberos Auth
  127. if (obj.kerberosDone == 0) {
  128. var ticketName = 'HTTP' + ((obj.tls == 1) ? 'S' : '') + '/' + ((obj.pass == '') ? (obj.host + ':' + obj.port) : obj.pass);
  129. // Ask for the new Kerberos ticket
  130. //console.log('kerberos.getTicket', ticketName);
  131. var ticketReturn = kerberos.getTicket(ticketName);
  132. if (ticketReturn.returnCode == 0 || ticketReturn.returnCode == 0x90312) {
  133. h += 'Authorization: Negotiate ' + ticketReturn.ticket + '\r\n';
  134. if (process.platform.indexOf('win') >= 0) {
  135. // Clear kerberos tickets on both 32 and 64bit Windows platforms
  136. try { require('child_process').exec('%windir%\\system32\\klist purge', function (error, stdout, stderr) { if (error) { require('child_process').exec('%windir%\\sysnative\\klist purge', function (error, stdout, stderr) { if (error) { console.error('Unable to purge kerberos tickets'); } }); } }); } catch (e) { console.log(e); }
  137. }
  138. } else {
  139. console.log('Unexpected Kerberos error code: ' + ticketReturn.returnCode);
  140. }
  141. obj.kerberosDone = 1;
  142. }
  143. } else if (obj.challengeParams != null) {
  144. var response = hex_md5(hex_md5(obj.user + ':' + obj.challengeParams['realm'] + ':' + obj.pass) + ':' + obj.challengeParams['nonce'] + ':' + nonceHex(obj.noncecounter) + ':' + obj.cnonce + ':' + obj.challengeParams['qop'] + ':' + hex_md5(action + ':' + url + ((obj.challengeParams['qop'] == 'auth-int') ? (':' + hex_md5(postdata)) : '')));
  145. h += 'Authorization: ' + obj.renderDigest({ 'username': obj.user, 'realm': obj.challengeParams['realm'], 'nonce': obj.challengeParams['nonce'], 'uri': url, 'qop': obj.challengeParams['qop'], 'response': response, 'nc': nonceHex(obj.noncecounter++), 'cnonce': obj.cnonce }) + '\r\n';
  146. }
  147. h += 'Host: ' + obj.host + ':' + obj.port + '\r\nContent-Length: ' + postdata.length + '\r\n\r\n' + postdata; // Use Content-Length
  148. //h += 'Host: ' + obj.host + ':' + obj.port + '\r\nTransfer-Encoding: chunked\r\n\r\n' + postdata.length.toString(16).toUpperCase() + '\r\n' + postdata + '\r\n0\r\n\r\n'; // Use Chunked-Encoding
  149. obj.xxSend(h);
  150. //console.log('SEND: ' + h); // Display send packet
  151. }
  152. // Parse the HTTP digest header and return a list of key & values.
  153. obj.parseDigest = function (header) { return correctedQuoteSplit(header.substring(7)).reduce(function (obj, s) { var parts = s.trim().split('='); obj[parts[0]] = parts[1].replace(new RegExp('\"', 'g'), ''); return obj; }, {}) }
  154. // Split a string on quotes but do not do it when in quotes
  155. function correctedQuoteSplit(str) { return str.split(',').reduce(function (a, c) { if (a.ic) { a.st[a.st.length - 1] += ',' + c } else { a.st.push(c) } if (c.split('"').length % 2 == 0) { a.ic = !a.ic } return a; }, { st: [], ic: false }).st }
  156. function nonceHex(v) { var s = ('00000000' + v.toString(16)); return s.substring(s.length - 8); }
  157. // NODE.js specific private method
  158. obj.renderDigest = function (params) {
  159. var paramsnames = [];
  160. for (var i in params) { paramsnames.push(i); }
  161. return 'Digest ' + paramsnames.reduce(function (s1, ii) { return s1 + ',' + (((ii == 'nc') || (ii == 'qop')) ? (ii + '=' + params[ii]) : (ii + '="' + params[ii] + '"')); }, '').substring(1);
  162. }
  163. // NODE.js specific private method
  164. obj.xxConnectHttpSocket = function () {
  165. //obj.Debug("xxConnectHttpSocket");
  166. obj.socketParseState = 0;
  167. obj.socketAccumulator = '';
  168. obj.socketHeader = null;
  169. obj.socketData = '';
  170. obj.socketState = 1;
  171. obj.kerberosDone = 0;
  172. if (obj.mpsConnection != null) {
  173. if (obj.xtls != 1) {
  174. // Setup a new channel using the CIRA/Relay/LMS connection
  175. obj.socket = obj.mpsConnection.SetupChannel(obj.port);
  176. if (obj.socket == null) { obj.xxOnSocketClosed(); return; }
  177. // Connect without TLS
  178. obj.socket.onData = function (ccon, data) { obj.xxOnSocketData(data); }
  179. obj.socket.onStateChange = function (ccon, state) {
  180. if (state == 0) {
  181. // Channel closed
  182. obj.socketParseState = 0;
  183. obj.socketAccumulator = '';
  184. obj.socketHeader = null;
  185. obj.socketData = '';
  186. obj.socketState = 0;
  187. obj.xxOnSocketClosed();
  188. } else if (state == 2) {
  189. // Channel open success
  190. obj.xxOnSocketConnected();
  191. }
  192. }
  193. } else {
  194. // Setup a new channel using the CIRA/Relay/LMS connection
  195. obj.cirasocket = obj.mpsConnection.SetupChannel(obj.port);
  196. if (obj.cirasocket == null) { obj.xxOnSocketClosed(); return; }
  197. // Connect with TLS
  198. var ser = new SerialTunnel();
  199. // let's chain up the TLSSocket <-> SerialTunnel <-> CIRA APF (chnl)
  200. // Anything that needs to be forwarded by SerialTunnel will be encapsulated by chnl write
  201. ser.forwardwrite = function (msg) { try { obj.cirasocket.write(msg); } catch (ex) { } }; // TLS ---> CIRA
  202. // When APF tunnel return something, update SerialTunnel buffer
  203. obj.cirasocket.onData = function (ciraconn, data) { if (data.length > 0) { try { ser.updateBuffer(Buffer.from(data, 'binary')); } catch (e) { } } }; // CIRA ---> TLS
  204. // Handle CIRA tunnel state change
  205. obj.cirasocket.onStateChange = function (ciraconn, state) {
  206. if (state == 0) { obj.xxOnSocketClosed(); }
  207. if (state == 2) {
  208. // TLSSocket to encapsulate TLS communication, which then tunneled via SerialTunnel an then wrapped through CIRA APF
  209. var options = { socket: ser, ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: obj.constants.SSL_OP_NO_SSLv2 | obj.constants.SSL_OP_NO_SSLv3 | obj.constants.SSL_OP_NO_COMPRESSION | obj.constants.SSL_OP_CIPHER_SERVER_PREFERENCE | obj.constants.SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, rejectUnauthorized: false };
  210. if (obj.xtlsMethod == 1) {
  211. options.secureProtocol = 'TLSv1_method';
  212. } else {
  213. options.minVersion = 'TLSv1';
  214. }
  215. if (obj.xtlsoptions) {
  216. if (obj.xtlsoptions.ca) { options.ca = obj.xtlsoptions.ca; }
  217. if (obj.xtlsoptions.cert) { options.cert = obj.xtlsoptions.cert; }
  218. if (obj.xtlsoptions.key) { options.key = obj.xtlsoptions.key; }
  219. }
  220. obj.socket = obj.tls.connect(obj.port, obj.host, options, obj.xxOnSocketConnected);
  221. obj.socket.setEncoding('binary');
  222. obj.socket.setTimeout(60000); // Set socket idle timeout
  223. obj.socket.on('error', function (ex) { obj.xtlsMethod = 1 - obj.xtlsMethod; });
  224. obj.socket.on('close', obj.xxOnSocketClosed);
  225. obj.socket.on('timeout', obj.destroy);
  226. // Decrypted tunnel from TLS communcation to be forwarded to websocket
  227. obj.socket.on('data', function (data) { try { obj.xxOnSocketData(data.toString('binary')); } catch (e) { } }); // AMT/TLS ---> WS
  228. // If TLS is on, forward it through TLSSocket
  229. obj.forwardclient = obj.socket;
  230. obj.forwardclient.xtls = 1;
  231. }
  232. };
  233. }
  234. } else {
  235. // Direct connection
  236. if (obj.xtls != 1) {
  237. // Direct connect without TLS
  238. obj.socket = new obj.net.Socket();
  239. obj.socket.setEncoding('binary');
  240. obj.socket.setTimeout(60000); // Set socket idle timeout
  241. obj.socket.on('data', obj.xxOnSocketData);
  242. obj.socket.on('close', obj.xxOnSocketClosed);
  243. obj.socket.on('timeout', obj.destroy);
  244. obj.socket.on('error', obj.xxOnSocketClosed);
  245. obj.socket.connect(obj.port, obj.host, obj.xxOnSocketConnected);
  246. } else {
  247. // Direct connect with TLS
  248. var options = { ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: obj.constants.SSL_OP_NO_SSLv2 | obj.constants.SSL_OP_NO_SSLv3 | obj.constants.SSL_OP_NO_COMPRESSION | obj.constants.SSL_OP_CIPHER_SERVER_PREFERENCE | obj.constants.SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, rejectUnauthorized: false };
  249. if (obj.xtlsMethod == 1) {
  250. options.secureProtocol = 'TLSv1_method';
  251. } else {
  252. options.minVersion = 'TLSv1';
  253. }
  254. if (obj.xtlsoptions) {
  255. if (obj.xtlsoptions.ca) { options.ca = obj.xtlsoptions.ca; }
  256. if (obj.xtlsoptions.cert) { options.cert = obj.xtlsoptions.cert; }
  257. if (obj.xtlsoptions.key) { options.key = obj.xtlsoptions.key; }
  258. }
  259. obj.socket = obj.tls.connect(obj.port, obj.host, options, obj.xxOnSocketConnected);
  260. obj.socket.setEncoding('binary');
  261. obj.socket.setTimeout(28000); // Set socket idle timeout of 28 seconds
  262. obj.socket.on('data', obj.xxOnSocketData);
  263. obj.socket.on('close', obj.xxOnSocketClosed);
  264. obj.socket.on('timeout', obj.destroy);
  265. obj.socket.on('error', function (ex) { if (ex.message && ex.message.indexOf('sslv3 alert bad record mac') >= 0) { obj.xtlsMethod = 1 - obj.xtlsMethod; } });
  266. }
  267. obj.socket.setNoDelay(true); // Disable nagle. We will encode each WSMAN request as a single send block and want to send it at once. This may help Intel AMT handle pipelining?
  268. }
  269. }
  270. // Get the certificate of Intel AMT
  271. obj.getPeerCertificate = function () { if (obj.xtls == 1) { return obj.socket.getPeerCertificate(); } return null; }
  272. obj.getPeerCertificateFingerprint = function () { if (obj.xtls == 1) { return obj.socket.getPeerCertificate().fingerprint.split(':').join('').toLowerCase(); } return null; }
  273. // Check if the certificate matched the certificate hash.
  274. function checkCertHash(cert, hash) {
  275. // Check not required
  276. if (hash == 0) return true;
  277. // SHA1 compare
  278. if (cert.fingerprint.split(':').join('').toLowerCase() == hash) return true;
  279. // SHA256 compare
  280. if ((hash.length == 64) && (obj.crypto.createHash('sha256').update(cert.raw).digest('hex') == hash)) { return true; }
  281. // SHA384 compare
  282. if ((hash.length == 96) && (obj.crypto.createHash('sha384').update(cert.raw).digest('hex') == hash)) { return true; }
  283. return false;
  284. }
  285. // NODE.js specific private method
  286. obj.xxOnSocketConnected = function () {
  287. if (obj.socket == null) return;
  288. // check TLS certificate for webrelay and direct only
  289. if (obj.xtls == 1) {
  290. obj.xtlsCertificate = obj.socket.getPeerCertificate();
  291. // Setup the forge certificate check
  292. var camatch = 0;
  293. if ((obj.xtlsoptions != null) && (obj.xtlsoptions.ca != null)) {
  294. var forgeCert = forge.pki.certificateFromAsn1(forge.asn1.fromDer(atob(obj.xtlsCertificate.raw.toString('base64'))));
  295. var caStore = forge.pki.createCaStore(obj.xtlsoptions.ca);
  296. // Got thru all certificates in the store and look for a match.
  297. for (var i in caStore.certs) {
  298. if (camatch == 0) {
  299. var c = caStore.certs[i], verified = false;
  300. try { verified = c.verify(forgeCert); } catch (e) { }
  301. if (verified == true) { camatch = c; }
  302. }
  303. }
  304. // We found a match, check that the CommonName matches the hostname
  305. if ((obj.xtlsSkipHostCheck == 0) && (camatch != 0)) {
  306. amtcertname = forgeCert.subject.getField('CN').value;
  307. if (amtcertname.toLowerCase() != obj.host.toLowerCase()) { camatch = 0; }
  308. }
  309. }
  310. if ((camatch == 0) && (checkCertHash(obj.xtlsCertificate, obj.xtlsFingerprint) == false)) {
  311. obj.FailAllError = 998; // Cause all new responses to be silent. 998 = TLS Certificate check error
  312. obj.CancelAllQueries(998);
  313. return;
  314. }
  315. if ((obj.xtlsFingerprint == 0) && (camatch == 0)) { obj.xtlsCheck = 3; } else { obj.xtlsCheck = (camatch == 0) ? 2 : 1; }
  316. } else { obj.xtlsCheck = 0; }
  317. obj.socketState = 2;
  318. obj.socketParseState = 0;
  319. for (i in obj.pendingAjaxCall) { obj.sendRequest(obj.pendingAjaxCall[i][0], obj.pendingAjaxCall[i][3], obj.pendingAjaxCall[i][4]); }
  320. }
  321. // NODE.js specific private method
  322. obj.xxOnSocketData = function (data) {
  323. //console.log('RECV: ' + data);
  324. obj.xtlsDataReceived = true;
  325. if (typeof data === 'object') {
  326. // This is an ArrayBuffer, convert it to a string array (used in IE)
  327. var binary = "", bytes = new Uint8Array(data), length = bytes.byteLength;
  328. for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
  329. data = binary;
  330. }
  331. else if (typeof data !== 'string') return;
  332. obj.socketAccumulator += data;
  333. while (true) {
  334. //console.log('ACC(' + obj.socketAccumulator + '): ' + obj.socketAccumulator);
  335. if (obj.socketParseState == 0) {
  336. var headersize = obj.socketAccumulator.indexOf('\r\n\r\n');
  337. if (headersize < 0) return;
  338. //obj.Debug("Header: "+obj.socketAccumulator.substring(0, headersize)); // Display received HTTP header
  339. obj.socketHeader = obj.socketAccumulator.substring(0, headersize).split('\r\n');
  340. if (obj.amtVersion == null) { for (var i in obj.socketHeader) { if (obj.socketHeader[i].indexOf('Server: Intel(R) Active Management Technology ') == 0) { obj.amtVersion = obj.socketHeader[i].substring(46); } } }
  341. obj.socketAccumulator = obj.socketAccumulator.substring(headersize + 4);
  342. obj.socketParseState = 1;
  343. obj.socketData = '';
  344. obj.socketXHeader = { Directive: obj.socketHeader[0].split(' ') };
  345. for (i in obj.socketHeader) {
  346. if (i != 0) {
  347. var x2 = obj.socketHeader[i].indexOf(':');
  348. obj.socketXHeader[obj.socketHeader[i].substring(0, x2).toLowerCase()] = obj.socketHeader[i].substring(x2 + 2);
  349. }
  350. }
  351. }
  352. if (obj.socketParseState == 1) {
  353. var csize = -1;
  354. if ((obj.socketXHeader['connection'] != undefined) && (obj.socketXHeader['connection'].toLowerCase() == 'close') && ((obj.socketXHeader["transfer-encoding"] == undefined) || (obj.socketXHeader["transfer-encoding"].toLowerCase() != 'chunked'))) {
  355. // The body ends with a close, in this case, we will only process the header
  356. csize = 0;
  357. } else if (obj.socketXHeader['content-length'] != undefined) {
  358. // The body length is specified by the content-length
  359. csize = parseInt(obj.socketXHeader['content-length']);
  360. if (obj.socketAccumulator.length < csize) return;
  361. var data = obj.socketAccumulator.substring(0, csize);
  362. obj.socketAccumulator = obj.socketAccumulator.substring(csize);
  363. obj.socketData = data;
  364. csize = 0;
  365. } else {
  366. // The body is chunked
  367. var clen = obj.socketAccumulator.indexOf('\r\n');
  368. if (clen < 0) return; // Chunk length not found, exit now and get more data.
  369. // Chunk length if found, lets see if we can get the data.
  370. csize = parseInt(obj.socketAccumulator.substring(0, clen), 16);
  371. if (obj.socketAccumulator.length < clen + 2 + csize + 2) return;
  372. // We got a chunk with all of the data, handle the chunck now.
  373. var data = obj.socketAccumulator.substring(clen + 2, clen + 2 + csize);
  374. obj.socketAccumulator = obj.socketAccumulator.substring(clen + 2 + csize + 2);
  375. try { obj.socketData += data; } catch (ex) { console.log(ex, typeof data, data.length); }
  376. }
  377. if (csize == 0) {
  378. //obj.Debug("xxOnSocketData DONE: (" + obj.socketData.length + "): " + obj.socketData);
  379. obj.xxProcessHttpResponse(obj.socketXHeader, obj.socketData);
  380. obj.socketParseState = 0;
  381. obj.socketHeader = null;
  382. }
  383. }
  384. }
  385. }
  386. // NODE.js specific private method
  387. obj.xxProcessHttpResponse = function (header, data) {
  388. //obj.Debug("xxProcessHttpResponse: " + header.Directive[1]);
  389. var s = parseInt(header.Directive[1]);
  390. if (isNaN(s)) s = 500;
  391. if (s == 401 && ++(obj.authcounter) < 3) {
  392. obj.challengeParams = obj.parseDigest(header['www-authenticate']); // Set the digest parameters, after this, the socket will close and we will auto-retry
  393. if (obj.challengeParams['qop'] != null) {
  394. var qopList = obj.challengeParams['qop'].split(',');
  395. for (var i in qopList) { qopList[i] = qopList[i].trim(); }
  396. if (qopList.indexOf('auth-int') >= 0) { obj.challengeParams['qop'] = 'auth-int'; } else { obj.challengeParams['qop'] = 'auth'; }
  397. }
  398. if (obj.mpsConnection == null) { obj.socket.end(); } else { obj.socket.close(); }
  399. } else {
  400. var r = obj.pendingAjaxCall.shift();
  401. if ((r == null) || (r.length < 1)) { /*console.log("pendingAjaxCall error, " + r);*/ return; } // Get a response without any pending requests.
  402. //if (s != 200) { obj.Debug("Error, status=" + s + "\r\n\r\nreq=" + r[0] + "\r\n\r\nresp=" + data); } // Debug: Display the request & response if something did not work.
  403. obj.authcounter = 0;
  404. obj.ActiveAjaxCount--;
  405. obj.gotNextMessages(data, 'success', { status: s }, r);
  406. obj.PerformNextAjax();
  407. }
  408. }
  409. // NODE.js specific private method
  410. obj.xxOnSocketClosed = function () {
  411. //obj.Debug("xxOnSocketClosed");
  412. obj.socketState = 0;
  413. if (obj.socket != null) {
  414. if (obj.socket.removeAllListeners) {
  415. // Do not remove the error handler since it may still get triggered.
  416. obj.socket.removeAllListeners('data');
  417. obj.socket.removeAllListeners('close');
  418. obj.socket.removeAllListeners('timeout');
  419. }
  420. try {
  421. if (obj.mpsConnection == null) {
  422. obj.socket.destroy();
  423. } else {
  424. if (obj.cirasocket != null) { obj.cirasocket.close(); } else { obj.socket.close(); }
  425. }
  426. } catch (ex) { }
  427. obj.socket = null;
  428. obj.cirasocket = null;
  429. }
  430. if (obj.pendingAjaxCall.length > 0) {
  431. var r = obj.pendingAjaxCall.shift(), retry = r[5];
  432. setTimeout(function () { obj.PerformAjaxExNodeJS2(r[0], r[1], r[2], r[3], r[4], --retry) }, 500); // Wait half a second and try again
  433. }
  434. }
  435. obj.destroy = function () {
  436. if (obj.socket != null) {
  437. if (obj.socket.removeAllListeners) {
  438. // Do not remove the error handler since it may still get triggered.
  439. obj.socket.removeAllListeners('data');
  440. obj.socket.removeAllListeners('close');
  441. obj.socket.removeAllListeners('timeout');
  442. }
  443. try {
  444. if (obj.mpsConnection == null) {
  445. obj.socket.destroy();
  446. } else {
  447. if (obj.cirasocket != null) { obj.cirasocket.close(); } else { obj.socket.close(); }
  448. }
  449. } catch (ex) { }
  450. delete obj.socket;
  451. delete obj.cirasocket;
  452. obj.socketState = 0;
  453. }
  454. }
  455. // NODE.js specific private method
  456. obj.xxSend = function (x) {
  457. //console.log('xxSend', x);
  458. if (obj.socketState == 2) { obj.socket.write(Buffer.from(x, 'binary')); }
  459. }
  460. // Cancel all pending queries with given status
  461. obj.CancelAllQueries = function (s) {
  462. obj.FailAllError = s;
  463. while (obj.PendingAjax.length > 0) { var x = obj.PendingAjax.shift(); x[1](null, s, x[2]); }
  464. obj.destroy();
  465. }
  466. // Private method
  467. obj.gotNextMessages = function (data, status, request, callArgs) {
  468. if (obj.FailAllError == 999) return;
  469. if (obj.FailAllError != 0) { try { callArgs[1](null, obj.FailAllError, callArgs[2]); } catch (ex) { console.error(ex); } return; }
  470. if (request.status != 200) { try { callArgs[1](null, request.status, callArgs[2]); } catch (ex) { console.error(ex); } return; }
  471. try { callArgs[1](data, 200, callArgs[2]); } catch (ex) { console.error(ex); }
  472. }
  473. // Private method
  474. obj.gotNextMessagesError = function (request, status, errorThrown, callArgs) {
  475. if (obj.FailAllError == 999) return;
  476. if (obj.FailAllError != 0) { try { callArgs[1](null, obj.FailAllError, callArgs[2]); } catch (ex) { console.error(ex); } return; }
  477. try { callArgs[1](obj, null, { Header: { HttpError: request.status } }, request.status, callArgs[2]); } catch (ex) { console.error(ex); }
  478. }
  479. // MD5 digest hash
  480. function hex_md5(str) { return obj.crypto.createHash('md5').update(str).digest('hex'); }
  481. return obj;
  482. }
  483. module.exports = CreateWsmanComm;