| 1 | /* |
| 2 | Copyright 2018-2021 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | var obj = { meshCoreInfo: 'TinyCore v1' }; |
| 18 | var mesh = null; |
| 19 | |
| 20 | // Replace a string with a number if the string is an exact number |
| 21 | function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) === x)) { x = parseInt(x); } return x; } |
| 22 | |
| 23 | // Split a string taking into account the quoats. Used for command line parsing |
| 24 | function splitArgs(str) |
| 25 | { |
| 26 | var myArray = [], myRegexp = /[^\s"]+|"([^"]*)"/gi; |
| 27 | do { var match = myRegexp.exec(str); if (match != null) { myArray.push(match[1] ? match[1] : match[0]); } } while (match != null); |
| 28 | return myArray; |
| 29 | } |
| 30 | // Parse arguments string array into an object |
| 31 | function parseArgs(argv) |
| 32 | { |
| 33 | var results = { '_': [] }, current = null; |
| 34 | for (var i = 1, len = argv.length; i < len; i++) |
| 35 | { |
| 36 | var x = argv[i]; |
| 37 | if (x.length > 2 && x[0] == '-' && x[1] == '-') |
| 38 | { |
| 39 | if (current != null) { results[current] = true; } |
| 40 | current = x.substring(2); |
| 41 | } else |
| 42 | { |
| 43 | if (current != null) { results[current] = toNumberIfNumber(x); current = null; } else { results['_'].push(toNumberIfNumber(x)); } |
| 44 | } |
| 45 | } |
| 46 | if (current != null) { results[current] = true; } |
| 47 | return results; |
| 48 | } |
| 49 | function sendConsoleText(msg, sessionid) |
| 50 | { |
| 51 | try |
| 52 | { |
| 53 | if (sessionid != null) |
| 54 | { |
| 55 | require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg, sessionid: sessionid }); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg }); |
| 60 | } |
| 61 | } |
| 62 | catch(e) |
| 63 | { |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | function processConsoleCommand(cmd, args, rights, sessionid) |
| 68 | { |
| 69 | try |
| 70 | { |
| 71 | var response = null; |
| 72 | switch (cmd) |
| 73 | { |
| 74 | case 'help': |
| 75 | response = "Available commands are: eval, osinfo, setdebug, versions."; |
| 76 | break; |
| 77 | case 'versions': |
| 78 | response = JSON.stringify(process.versions, null, ' '); |
| 79 | break; |
| 80 | case 'eval': |
| 81 | { // Eval JavaScript |
| 82 | if (args['_'].length < 1) |
| 83 | { |
| 84 | response = 'Proper usage: eval "JavaScript code"'; // Display correct command usage |
| 85 | } else |
| 86 | { |
| 87 | response = JSON.stringify(require('MeshAgent').eval(args['_'][0])); // This can only be run by trusted administrator. |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | case 'setdebug': |
| 92 | { |
| 93 | if (args['_'].length < 1) { response = 'Proper usage: setdebug (target), 0 = Disabled, 1 = StdOut, 2 = This Console, * = All Consoles, 4 = WebLog, 8 = Logfile'; } // Display usage |
| 94 | else { if (args['_'][0] == '*') { console.setDestination(2); } else { console.setDestination(parseInt(args['_'][0]), sessionid); } } |
| 95 | break; |
| 96 | } |
| 97 | case 'osinfo': { // Return the operating system information |
| 98 | var i = 1; |
| 99 | if (args['_'].length > 0) { i = parseInt(args['_'][0]); if (i > 8) { i = 8; } response = 'Calling ' + i + ' times.'; } |
| 100 | for (var j = 0; j < i; j++) |
| 101 | { |
| 102 | var pr = require('os').name(); |
| 103 | pr.sessionid = sessionid; |
| 104 | pr.then(function (v) |
| 105 | { |
| 106 | sendConsoleText("OS: " + v, this.sessionid); |
| 107 | }); |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | default: { // This is an unknown command, return an error message |
| 112 | response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } catch (e) { response = "Command returned an exception error: " + e; console.log(e); } |
| 117 | if (response != null) { sendConsoleText(response, sessionid); } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // Handle a mesh agent command |
| 122 | function handleServerCommand(data) |
| 123 | { |
| 124 | if ((typeof data == 'object') && (data.action == 'msg') && (data.type == 'console') && data.value && data.sessionid) |
| 125 | { |
| 126 | if (data.value && data.sessionid) |
| 127 | { |
| 128 | try |
| 129 | { |
| 130 | var args = splitArgs(data.value); |
| 131 | processConsoleCommand(args[0].toLowerCase(), parseArgs(args), data.rights, data.sessionid); |
| 132 | } |
| 133 | catch(e) |
| 134 | { |
| 135 | sendConsoleText(e); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | console.log(JSON.stringify(data, null, 1)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Called when the server connection state changes |
| 146 | function handleServerConnection(state) |
| 147 | { |
| 148 | if (state == 1) { mesh.SendCommand({ "action": "coreinfo", "value": obj.meshCoreInfo }); } // Server connected, send mesh core information |
| 149 | } |
| 150 | |
| 151 | obj.start = function () |
| 152 | { |
| 153 | // Hook up mesh agent events |
| 154 | mesh.AddCommandHandler(handleServerCommand); |
| 155 | mesh.AddConnectHandler(handleServerConnection); |
| 156 | mesh.SendCommand({ action: 'coreinfo', value: "TinyCore", caps: 0 }); |
| 157 | } |
| 158 | |
| 159 | obj.stop = function () |
| 160 | { |
| 161 | mesh.AddCommandHandler(null); |
| 162 | mesh.AddConnectHandler(null); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | var xexports = null; |
| 167 | try { xexports = module.exports; } catch (e) { } |
| 168 | |
| 169 | if (xexports != null) |
| 170 | { |
| 171 | // If we are running within NodeJS, export the core |
| 172 | module.exports.createMeshCore = function (agent) { mesh = agent.getMeshApi(); return (obj); }; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | // If we are not running in NodeJS, launch the core |
| 177 | sendConsoleText('TinyCore Started...'); |
| 178 | mesh = require('MeshAgent'); |
| 179 | obj.start(); |
| 180 | } |