meshctrl.js 199 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179
  1. #!/usr/bin/env node
  2. /**
  3. * @description MeshCentral command line tool
  4. * @author Ylian Saint-Hilaire
  5. * @copyright Intel Corporation 2018-2022
  6. * @license Apache-2.0
  7. * @version v0.0.1
  8. */
  9. // Make sure we have the dependency modules
  10. try { require('minimist'); } catch (ex) { console.log('Missing module "minimist", type "npm install minimist" to install it.'); return; }
  11. try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm install ws" to install it.'); return; }
  12. var settings = {};
  13. const crypto = require('crypto');
  14. const args = require('minimist')(process.argv.slice(2));
  15. const path = require('path');
  16. const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'removedevice', 'editdevice', 'addlocaldevice', 'addamtdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload', 'report', 'grouptoast', 'groupmessage', 'webrelay'];
  17. if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { console.log('Missing module "https-proxy-agent", type "npm install https-proxy-agent" to install it.'); return; } }
  18. if (args['_'].length == 0) {
  19. console.log("MeshCtrl performs command line actions on a MeshCentral server.");
  20. console.log("Information at: https://meshcentral.com");
  21. console.log("No action specified, use MeshCtrl like this:\r\n\r\n meshctrl [action] [arguments]\r\n");
  22. console.log("Supported actions:");
  23. console.log(" Help [action] - Get help on an action.");
  24. console.log(" ServerInfo - Show server information.");
  25. console.log(" UserInfo - Show user information.");
  26. console.log(" ListUsers - List user accounts.");
  27. console.log(" ListUserSessions - List online users.");
  28. console.log(" ListUserGroups - List user groups.");
  29. console.log(" ListDevices - List devices.");
  30. console.log(" ListDeviceGroups - List device groups.");
  31. console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
  32. console.log(" ListEvents - List server events.");
  33. console.log(" LoginTokens - List, create and remove login tokens.");
  34. console.log(" DeviceInfo - Show information about a device.");
  35. console.log(" AddLocalDevice - Add a local device.");
  36. console.log(" AddAmtDevice - Add a AMT device.");
  37. console.log(" EditDevice - Make changes to a device.");
  38. console.log(" RemoveDevice - Delete a device.");
  39. console.log(" Config - Perform operation on config.json file.");
  40. console.log(" AddUser - Create a new user account.");
  41. console.log(" EditUser - Change a user account.");
  42. console.log(" RemoveUser - Delete a user account.");
  43. console.log(" AddUserGroup - Create a new user group.");
  44. console.log(" RemoveUserGroup - Delete a user group.");
  45. console.log(" AddToUserGroup - Add a user, device or device group to a user group.");
  46. console.log(" RemoveFromUserGroup - Remove a user, device or device group from a user group.");
  47. console.log(" RemoveAllUsersFromUserGroup - Remove all users from a user group.");
  48. console.log(" AddDeviceGroup - Create a new device group.");
  49. console.log(" RemoveDeviceGroup - Delete a device group.");
  50. console.log(" EditDeviceGroup - Change a device group values.");
  51. console.log(" MoveToDeviceGroup - Move a device to a different device group.");
  52. console.log(" AddUserToDeviceGroup - Add a user to a device group.");
  53. console.log(" RemoveUserFromDeviceGroup - Remove a user from a device group.");
  54. console.log(" AddUserToDevice - Add a user to a device.");
  55. console.log(" RemoveUserFromDevice - Remove a user from a device.");
  56. console.log(" SendInviteEmail - Send an agent install invitation email.");
  57. console.log(" GenerateInviteLink - Create an invitation link.");
  58. console.log(" Broadcast - Display a message to all online users.");
  59. console.log(" ShowEvents - Display real-time server events in JSON format.");
  60. console.log(" RunCommand - Run a shell command on a remote device.");
  61. console.log(" Shell - Access command shell of a remote device.");
  62. console.log(" Upload - Upload a file to a remote device.");
  63. console.log(" Download - Download a file from a remote device.");
  64. console.log(" WebRelay - Creates a HTTP/HTTPS webrelay link for a remote device.");
  65. console.log(" DeviceOpenUrl - Open a URL on a remote device.");
  66. console.log(" DeviceMessage - Open a message box on a remote device.");
  67. console.log(" DeviceToast - Display a toast notification on a remote device.");
  68. console.log(" GroupMessage - Open a message box on remote devices in a specific device group.");
  69. console.log(" GroupToast - Display a toast notification on remote devices in a specific device group.");
  70. console.log(" DevicePower - Perform wake/sleep/reset/off operations on remote devices.");
  71. console.log(" DeviceSharing - View, add and remove sharing links for a given device.");
  72. console.log(" AgentDownload - Download an agent of a specific type for a device group.");
  73. console.log(" Report - Create and show a CSV report.");
  74. console.log("\r\nSupported login arguments:");
  75. console.log(" --url [wss://server] - Server url, wss://localhost:443 is default.");
  76. console.log(" - Use wss://localhost:443?key=xxx if login key is required.");
  77. console.log(" --loginuser [username] - Login username, admin is default.");
  78. console.log(" --loginpass [password] - Login password OR Leave blank to enter password at prompt");
  79. console.log(" --token [number] - 2nd factor authentication token.");
  80. console.log(" --loginkey [hex] - Server login key in hex.");
  81. console.log(" --loginkeyfile [file] - File containing server login key in hex.");
  82. console.log(" --logindomain [domainid] - Domain id, default is empty, only used with loginkey.");
  83. console.log(" --proxy [http://proxy:123] - Specify an HTTP proxy.");
  84. return;
  85. } else {
  86. settings.cmd = args['_'][0].toLowerCase();
  87. if ((possibleCommands.indexOf(settings.cmd) == -1) && (settings.cmd != 'help')) { console.log("Invalid command. Possible commands are: " + possibleCommands.join(', ') + '.'); return; }
  88. //console.log(settings.cmd);
  89. var ok = false;
  90. switch (settings.cmd) {
  91. case 'config': { performConfigOperations(args); return; }
  92. case 'indexagenterrorlog': { indexAgentErrorLog(); return; }
  93. case 'serverinfo': { ok = true; break; }
  94. case 'userinfo': { ok = true; break; }
  95. case 'listusers': { ok = true; break; }
  96. case 'listusersessions': { ok = true; break; }
  97. case 'listusergroups': { ok = true; break; }
  98. case 'listdevicegroups': { ok = true; break; }
  99. case 'listdevices': { ok = true; break; }
  100. case 'listevents': { ok = true; break; }
  101. case 'logintokens': { ok = true; break; }
  102. case 'listusersofdevicegroup':
  103. case 'deviceinfo':
  104. case 'removedevice':
  105. case 'editdevice': {
  106. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  107. else { ok = true; }
  108. break;
  109. }
  110. case 'addlocaldevice': {
  111. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  112. else if (args.devicename == null) { console.log(winRemoveSingleQuotes("Missing devicename, use --devicename [devicename]")); }
  113. else if (args.hostname == null) { console.log(winRemoveSingleQuotes("Missing hostname, use --hostname [hostname]")); }
  114. else { ok = true; }
  115. break;
  116. }
  117. case 'addamtdevice': {
  118. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  119. else if (args.devicename == null) { console.log(winRemoveSingleQuotes("Missing devicename, use --devicename [devicename]")); }
  120. else if (args.hostname == null) { console.log(winRemoveSingleQuotes("Missing hostname, use --hostname [hostname]")); }
  121. else if (args.user == null) { console.log(winRemoveSingleQuotes("Missing user, use --user [user]")); }
  122. else if (args.pass == null) { console.log(winRemoveSingleQuotes("Missing pass, use --pass [pass]")); }
  123. else { ok = true; }
  124. break;
  125. }
  126. case 'addusertodevicegroup': {
  127. if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
  128. else if (args.userid == null) { console.log("Add user to group missing useid, use --userid [userid]"); }
  129. else { ok = true; }
  130. break;
  131. }
  132. case 'removeuserfromdevicegroup': {
  133. if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
  134. else if (args.userid == null) { console.log("Remove user from group missing useid, use --userid [userid]"); }
  135. else { ok = true; }
  136. break;
  137. }
  138. case 'addusertodevice': {
  139. if (args.userid == null) { console.log("Add user to device missing userid, use --userid [userid]"); }
  140. else if (args.id == null) { console.log(winRemoveSingleQuotes("Add user to device missing device id, use --id '[deviceid]'")); }
  141. else { ok = true; }
  142. break;
  143. }
  144. case 'removeuserfromdevice': {
  145. if (args.userid == null) { console.log("Remove user from device missing userid, use --userid [userid]"); }
  146. else if (args.id == null) { console.log(winRemoveSingleQuotes("Remove user from device missing device id, use --id '[deviceid]'")); }
  147. else { ok = true; }
  148. break;
  149. }
  150. case 'adddevicegroup': {
  151. if (args.name == null) { console.log("Message group name, use --name [name]"); }
  152. else { ok = true; }
  153. break;
  154. }
  155. case 'editdevicegroup':
  156. case 'removedevicegroup': {
  157. if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
  158. else { ok = true; }
  159. break;
  160. }
  161. case 'movetodevicegroup': {
  162. if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
  163. else if (args.devid == null) { console.log(winRemoveSingleQuotes("Device identifier missing, use --devid '[deviceid]'")); }
  164. else { ok = true; }
  165. break;
  166. }
  167. case 'broadcast': {
  168. if (args.msg == null) { console.log("Message missing, use --msg [message]"); }
  169. else { ok = true; }
  170. break;
  171. }
  172. case 'showevents': {
  173. ok = true;
  174. break;
  175. }
  176. case 'adduser': {
  177. if (args.user == null) { console.log("New account name missing, use --user [name]"); }
  178. else if ((args.pass == null) && (args.randompass == null)) { console.log("New account password missing, use --pass [password] or --randompass"); }
  179. else { ok = true; }
  180. break;
  181. }
  182. case 'edituser': {
  183. if (args.userid == null) { console.log("Edit account user missing, use --userid [id]"); }
  184. else { ok = true; }
  185. break;
  186. }
  187. case 'removeuser': {
  188. if (args.userid == null) { console.log("Remove account userid missing, use --userid [id]"); }
  189. else { ok = true; }
  190. break;
  191. }
  192. case 'addusergroup': {
  193. if (args.name == null) { console.log("New user group name missing, use --name [name]"); }
  194. else { ok = true; }
  195. break;
  196. }
  197. case 'removeusergroup': {
  198. if (args.groupid == null) { console.log(winRemoveSingleQuotes("Remove user group id missing, use --groupid '[id]'")); }
  199. else { ok = true; }
  200. break;
  201. }
  202. case 'addtousergroup': {
  203. if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
  204. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing identifier to add, use --id [id]")); }
  205. else { ok = true; }
  206. break;
  207. }
  208. case 'removefromusergroup': {
  209. if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
  210. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing identifier to remove, use --id [id]")); }
  211. else { ok = true; }
  212. break;
  213. }
  214. case 'removeallusersfromusergroup': {
  215. if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
  216. else { ok = true; }
  217. break;
  218. }
  219. case 'sendinviteemail': {
  220. if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id '[groupid]' or --group [groupname]"); }
  221. else if (args.email == null) { console.log("Device email is missing, use --email [email]"); }
  222. else { ok = true; }
  223. break;
  224. }
  225. case 'generateinvitelink': {
  226. if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id '[groupid]' or --group [groupname]"); }
  227. else if (args.hours == null) { console.log("Invitation validity period missing, use --hours [hours]"); }
  228. else { ok = true; }
  229. break;
  230. }
  231. case 'runcommand': {
  232. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  233. else if (args.run == null) { console.log("Missing run, use --run \"command\""); }
  234. else { ok = true; }
  235. break;
  236. }
  237. case 'shell': {
  238. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  239. else { ok = true; }
  240. break;
  241. }
  242. case 'devicepower': {
  243. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  244. else { ok = true; }
  245. break;
  246. }
  247. case 'devicesharing': {
  248. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  249. else if ((args.daily != null) && (args.weekly != null)) { console.log(winRemoveSingleQuotes("Can't specify both --daily and --weekly at the same time.")); }
  250. else { ok = true; }
  251. break;
  252. }
  253. case 'agentdownload': {
  254. if (args.type == null) { console.log(winRemoveSingleQuotes("Missing device type, use --type [agenttype]")); }
  255. else if ((parseInt(args.type) == null) || isNaN(parseInt(args.type)) || (parseInt(args.type) < 1) || (parseInt(args.type) > 11000)) { console.log(winRemoveSingleQuotes("Invalid agent type, must be a number.")); }
  256. else if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[meshid]'")); }
  257. else if ((typeof args.id != 'string') || (args.id.length != 64)) { console.log(winRemoveSingleQuotes("Invalid meshid.")); }
  258. else { ok = true; }
  259. break;
  260. }
  261. case 'upload': {
  262. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  263. else if (args.file == null) { console.log("Local file missing, use --file [file] specify the file to upload"); }
  264. else if (args.target == null) { console.log("Remote target path missing, use --target [path] to specify the remote location"); }
  265. else if (require('fs').existsSync(args.file) == false) { console.log("Local file does not exists, check --file"); }
  266. else { ok = true; }
  267. break;
  268. }
  269. case 'download': {
  270. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  271. else if (args.file == null) { console.log("Remote file missing, use --file [file] specify the remote file to download"); }
  272. else if (args.target == null) { console.log("Target path missing, use --target [path] to specify the local download location"); }
  273. else { ok = true; }
  274. break;
  275. }
  276. case 'webrelay': {
  277. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  278. else if (args.type == null) { console.log(winRemoveSingleQuotes("Missing protocol type, use --type [http,https]")); }
  279. else { ok = true; }
  280. break;
  281. }
  282. case 'deviceopenurl': {
  283. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  284. else if (args.openurl == null) { console.log("Remote URL, use --openurl [url] specify the link to open."); }
  285. else { ok = true; }
  286. break;
  287. }
  288. case 'devicemessage': {
  289. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  290. else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
  291. else { ok = true; }
  292. break;
  293. }
  294. case 'devicetoast': {
  295. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
  296. else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
  297. else { ok = true; }
  298. break;
  299. }
  300. case 'groupmessage': {
  301. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
  302. else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
  303. else { ok = true; }
  304. break;
  305. }
  306. case 'grouptoast': {
  307. if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
  308. else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
  309. else { ok = true; }
  310. break;
  311. }
  312. case 'report': {
  313. if (args.type == null) { console.log(winRemoveSingleQuotes("Missing report type, use --type '[reporttype]'")); }
  314. else { ok = true; }
  315. break;
  316. }
  317. case 'help': {
  318. if (args['_'].length < 2) {
  319. console.log("Get help on an action. Type:\r\n\r\n help [action]\r\n\r\nPossible actions are: " + possibleCommands.join(', ') + '.');
  320. } else {
  321. switch (args['_'][1].toLowerCase()) {
  322. case 'config': {
  323. displayConfigHelp();
  324. break;
  325. }
  326. case 'sendinviteemail': {
  327. console.log("Send invitation email with instructions on how to install the mesh agent for a specific device group. Example usage:\r\n");
  328. console.log(winRemoveSingleQuotes(" MeshCtrl SendInviteEmail --id 'groupid' --message \"msg\" --email [email protected]"));
  329. console.log(winRemoveSingleQuotes(" MeshCtrl SendInviteEmail --group \"My Computers\" --name \"Jack\" --email [email protected]"));
  330. console.log("\r\nRequired arguments:\r\n");
  331. if (process.platform == 'win32') {
  332. console.log(" --id [groupid] - Device group identifier (or --group).");
  333. } else {
  334. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  335. }
  336. console.log(" --group [groupname] - Device group name (or --id).");
  337. console.log(" --email [email] - Email address.");
  338. console.log("\r\nOptional arguments:\r\n");
  339. console.log(" --name (name) - Name of recipient to be included in the email.");
  340. console.log(" --message (msg) - Message to be included in the email.");
  341. break;
  342. }
  343. case 'generateinvitelink': {
  344. console.log("Generate a agent invitation URL for a given group. Example usage:\r\n");
  345. console.log(winRemoveSingleQuotes(" MeshCtrl GenerateInviteLink --id 'groupid' --hours 24"));
  346. console.log(" MeshCtrl GenerateInviteLink --group \"My Computers\" --hours 0");
  347. console.log("\r\nRequired arguments:\r\n");
  348. if (process.platform == 'win32') {
  349. console.log(" --id [groupid] - Device group identifier (or --group).");
  350. } else {
  351. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  352. }
  353. console.log(" --group [groupname] - Device group name (or --id).");
  354. console.log(" --hours [hours] - Validity period in hours or 0 for infinite.");
  355. console.log("\r\nOptional arguments:\r\n");
  356. console.log(" --flags [mode] - Mode flag for link type (0 = both, 1 = interactive only, 2 = background only)");
  357. break;
  358. }
  359. case 'showevents': {
  360. console.log("Show the server's event stream for this user account. Example usage:\r\n");
  361. console.log(" MeshCtrl ShowEvents");
  362. console.log(" MeshCtrl ShowEvents --filter nodeconnect");
  363. console.log(" MeshCtrl ShowEvents --filter uicustomevent,changenode");
  364. console.log("\r\nOptional arguments:\r\n");
  365. console.log(" --filter [actions] - Show only specified actions.");
  366. break;
  367. }
  368. case 'serverinfo': {
  369. console.log("Get information on the MeshCentral server, Example usages:\r\n");
  370. console.log(" MeshCtrl ServerInfo --loginuser myaccountname --loginpass mypassword");
  371. console.log(" MeshCtrl ServerInfo --loginuser myaccountname --loginkeyfile key.txt");
  372. console.log("\r\nOptional arguments:\r\n");
  373. console.log(" --json - Show result as JSON.");
  374. break;
  375. }
  376. case 'userinfo': {
  377. console.log("Get account information for the login account, Example usages:\r\n");
  378. console.log(" MeshCtrl UserInfo --loginuser myaccountname --loginpass mypassword");
  379. console.log(" MeshCtrl UserInfo --loginuser myaccountname --loginkeyfile key.txt");
  380. console.log("\r\nOptional arguments:\r\n");
  381. console.log(" --json - Show result as JSON.");
  382. break;
  383. }
  384. case 'listusers': {
  385. console.log("List the account on the MeshCentral server, Example usages:\r\n");
  386. console.log(" MeshCtrl ListUsers");
  387. console.log(" MeshCtrl ListUsers --json");
  388. console.log(" MeshCtrl ListUsers --nameexists \"bob\"");
  389. console.log(" MeshCtrl ListUsers --filter 2fa");
  390. console.log("\r\nOptional arguments:\r\n");
  391. console.log(" --idexists [id] - Return 1 if id exists, 0 if not.");
  392. console.log(" --nameexists [name] - Return id if name exists.");
  393. console.log(" --filter [filter1,...] - Filter user names: 2FA, NO2FA.");
  394. console.log(" --json - Show result as JSON.");
  395. break;
  396. }
  397. case 'listusersessions': {
  398. console.log("List active user sessions on the MeshCentral server, Example usages:\r\n");
  399. console.log(" MeshCtrl ListUserSessions");
  400. console.log(" MeshCtrl ListUserSessions --json");
  401. break;
  402. }
  403. case 'listusergroups': {
  404. console.log("List user groups on the MeshCentral server, Example usages:\r\n");
  405. console.log(" MeshCtrl ListUserGroups");
  406. console.log(" MeshCtrl ListUserGroups --json");
  407. break;
  408. }
  409. case 'listdevicegroups': {
  410. console.log("List the device groups for this account. Example usages:\r\n");
  411. console.log(" MeshCtrl ListDeviceGroups ");
  412. console.log(" MeshCtrl ListDeviceGroups --json");
  413. console.log("\r\nOptional arguments:\r\n");
  414. console.log(" --idexists [id] - Return 1 if id exists, 0 if not.");
  415. console.log(" --nameexists [name] - Return id if name exists.");
  416. console.log(" --emailexists [email] - Return id if email exists.");
  417. console.log(" --hex - Display meshid in hex format.");
  418. console.log(" --json - Show result as JSON.");
  419. break;
  420. }
  421. case 'listdevices': {
  422. console.log("List devices. Example usages:\r\n");
  423. console.log(" MeshCtrl ListDevices");
  424. console.log(winRemoveSingleQuotes(" MeshCtrl ListDevices -id '[groupid]' --json"));
  425. console.log("\r\nOptional arguments:\r\n");
  426. if (process.platform == 'win32') {
  427. console.log(" --id [groupid] - Filter by group identifier (or --group).");
  428. } else {
  429. console.log(" --id '[groupid]' - Filter by group identifier (or --group).");
  430. }
  431. console.log(" --group [groupname] - Filter by group name (or --id).");
  432. console.log(" --count - Only return the device count.");
  433. console.log(" --json - Show result as JSON.");
  434. console.log(" --csv - Show result as comma separated values.");
  435. console.log(" --filter \"[filter]\" - Filter devices using a filter string.");
  436. console.log(" \"x\" - Devices with \"x\" in the name.");
  437. console.log(" \"user:x or u:x\" - Devices with \"x\" in the name of currently logged in user.");
  438. console.log(" \"ip:x\" - Devices \"x\" IP address.");
  439. console.log(" \"group:x or g:x\" - Devices with \"x\" in device group name.");
  440. console.log(" \"tag:x or t:x\" - Devices with \"x\" in device tag.");
  441. console.log(" \"atag:x or a:x\" - Devices with \"x\" in device agent tag.");
  442. console.log(" \"os:x\" - Devices with \"x\" in the device OS description.");
  443. console.log(" \"amt:x\" - Devices with Intel AMT provisioning state (0, 1, 2).");
  444. console.log(" \"desc:x\" - Devices with \"x\" in device description.");
  445. console.log(" \"wsc:ok\" - Devices with Windows Security Center ok.");
  446. console.log(" \"wsc:noav\" - Devices with Windows Security Center with anti-virus problem.");
  447. console.log(" \"wsc:noupdate\" - Devices with Windows Security Center with update problem.");
  448. console.log(" \"wsc:nofirewall\" - Devices with Windows Security Center with firewall problem.");
  449. console.log(" \"wsc:any\" - Devices with Windows Security Center with any problem.");
  450. console.log(" \"a and b\" - Match both conditions with precedence over OR. For example: \"lab and g:home\".");
  451. console.log(" \"a or b\" - Math one of the conditions, for example: \"lab or g:home\".");
  452. console.log(" --filterid [id,id...] - Show only results for devices with included id.");
  453. console.log(" --details - Show all device details.");
  454. break;
  455. }
  456. case 'listusersofdevicegroup': {
  457. console.log("List users that have permissions for a given device group. Example usage:\r\n");
  458. console.log(" MeshCtrl ListUserOfDeviceGroup ");
  459. console.log("\r\nRequired arguments:\r\n");
  460. if (process.platform == 'win32') {
  461. console.log(" --id [groupid] - Device group identifier.");
  462. } else {
  463. console.log(" --id '[groupid]' - Device group identifier.");
  464. }
  465. console.log("\r\nOptional arguments:\r\n");
  466. console.log(" --json - Show result as JSON.");
  467. break;
  468. }
  469. case 'listevents': {
  470. console.log("List server events optionally filtered by user or device. Example usage:\r\n");
  471. console.log(" MeshCtrl ListEvents ");
  472. console.log("\r\nOptional arguments:\r\n");
  473. console.log(" --userid [name] - User account identifier.");
  474. console.log(" --id [deviceid] - The device identifier.");
  475. console.log(" --limit [number] - Maximum number of events to list.");
  476. console.log(" --raw - Output raw data in JSON format.");
  477. console.log(" --json - Give results in JSON format.");
  478. break;
  479. }
  480. case 'logintokens': {
  481. console.log("List account login tokens and allow addition and removal. Example usage:\r\n");
  482. console.log(" MeshCtrl LoginTokens ");
  483. console.log("\r\nOptional arguments:\r\n");
  484. console.log(" --remove [name] - Remove a login token.");
  485. console.log(" --add [name] - Add a login token.");
  486. console.log(" --expire [minutes] - When adding a token, minutes until expire.");
  487. console.log(" --json - Show login tokens in JSON format.");
  488. break;
  489. }
  490. case 'adduser': {
  491. console.log("Add a new user account. Example usages:\r\n");
  492. console.log(" MeshCtrl AddUser --user newaccountname --pass newpassword");
  493. console.log(" MeshCtrl AddUser --user newaccountname --randompass --rights full");
  494. console.log("\r\nRequired arguments:\r\n");
  495. console.log(" --user [name] - New account name.");
  496. console.log(" --pass [password] - New account password.");
  497. console.log(" --randompass - Create account with a random password.");
  498. console.log("\r\nOptional arguments:\r\n");
  499. console.log(" --domain [domain] - Account domain, only for cross-domain admins.");
  500. console.log(" --email [email] - New account email address.");
  501. console.log(" --emailverified - New account email is verified.");
  502. console.log(" --resetpass - Request password reset on next login.");
  503. console.log(" --realname [name] - Set the real name for this account.");
  504. console.log(" --phone [number] - Set the account phone number.");
  505. console.log(" --rights [none|full|a,b,c] - Comma separated list of server permissions. Possible values:");
  506. console.log(" manageusers,serverbackup,serverrestore,serverupdate,fileaccess,locked,nonewgroups,notools,usergroups,recordings,locksettings,allevents,nonewdevices");
  507. break;
  508. }
  509. case 'edituser': {
  510. console.log("Edit a user account, Example usages:\r\n");
  511. console.log(" MeshCtrl EditUser --userid user --rights locked,locksettings");
  512. console.log(" MeshCtrl EditUser --userid user --realname Jones");
  513. console.log("\r\nRequired arguments:\r\n");
  514. console.log(" --userid [name] - User account identifier.");
  515. console.log("\r\nOptional arguments:\r\n");
  516. console.log(" --domain [domain] - Account domain, only for cross-domain admins.");
  517. console.log(" --email [email] - Account email address.");
  518. console.log(" --emailverified - Account email is verified.");
  519. console.log(" --resetpass - Request password reset on next login.");
  520. console.log(" --realname [name] - Set the real name for this account.");
  521. console.log(" --phone [number] - Set the account phone number.");
  522. console.log(" --rights [none|full|a,b,c] - Comma separated list of server permissions. Possible values:");
  523. console.log(" manageusers,serverbackup,serverrestore,serverupdate,fileaccess,locked,nonewgroups,notools,usergroups,recordings,locksettings,allevents,nonewdevices");
  524. break;
  525. }
  526. case 'removeuser': {
  527. console.log("Delete a user account, Example usages:\r\n");
  528. console.log(" MeshCtrl RemoveUser --userid accountid");
  529. console.log("\r\nRequired arguments:\r\n");
  530. console.log(" --userid [id] - Account identifier.");
  531. break;
  532. }
  533. case 'addusergroup': {
  534. console.log("Create a new user group, Example usages:\r\n");
  535. console.log(" MeshCtrl AddUserGroup --name \"Test Group\"");
  536. console.log("\r\nRequired arguments:\r\n");
  537. console.log(" --name [name] - Name of the user group.");
  538. break;
  539. }
  540. case 'removeusergroup': {
  541. console.log("Remove a user group, Example usages:\r\n");
  542. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserGroup --groupid 'ugrp//abcdf'"));
  543. console.log("\r\nRequired arguments:\r\n");
  544. if (process.platform == 'win32') {
  545. console.log(" --groupid [groupid] - User group identifier.");
  546. } else {
  547. console.log(" --groupid '[groupid]' - User group identifier.");
  548. }
  549. break;
  550. }
  551. case 'addtousergroup': {
  552. console.log("Add a user, device or device group to a user group, Example usages:\r\n");
  553. console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'user//abcdef' --groupid 'ugrp//abcdf'"));
  554. console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'node//abcdef' --groupid 'ugrp//abcdf' --rights [rights]"));
  555. console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'mesh//abcdef' --groupid 'ugrp//abcdf' --rights [rights]"));
  556. console.log("\r\nRequired arguments:\r\n");
  557. if (process.platform == 'win32') {
  558. console.log(" --id [id] - Identifier to add.");
  559. console.log(" --groupid [groupid] - User group identifier.");
  560. } else {
  561. console.log(" --id '[id]' - Identifier to add.");
  562. console.log(" --groupid '[groupid]' - User group identifier.");
  563. }
  564. console.log("\r\nOptional arguments:\r\n");
  565. console.log(" --rights [number] - Rights granted for adding device or device group.");
  566. console.log(" - 4294967295 for full admin or the sum of the following numbers.");
  567. console.log(" 1 = Edit Device Group 2 = Manage Users ");
  568. console.log(" 4 = Manage Computers 8 = Remote Control ");
  569. console.log(" 16 = Agent Console 32 = Server Files ");
  570. console.log(" 64 = Wake Device 128 = Set Notes ");
  571. console.log(" 256 = Remote View Only 512 = No Terminal ");
  572. console.log(" 1024 = No Files 2048 = No Intel AMT ");
  573. console.log(" 4096 = Desktop Limited Input 8192 = Limit Events ");
  574. console.log(" 16384 = Chat / Notify 32768 = Uninstall Agent ");
  575. console.log(" 65536 = No Remote Desktop 131072 = Remote Commands ");
  576. console.log(" 262144 = Reset / Power off ");
  577. break;
  578. }
  579. case 'removefromusergroup': {
  580. console.log("Remove a user, device or device group from a user group, Example usages:\r\n");
  581. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'user//abcdef' --groupid 'ugrp//abcdf'"));
  582. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'node//abcdef' --groupid 'ugrp//abcdf'"));
  583. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'mesh//abcdef' --groupid 'ugrp//abcdf'"));
  584. console.log("\r\nRequired arguments:\r\n");
  585. if (process.platform == 'win32') {
  586. console.log(" --id [userid] - Identifier to remove.");
  587. console.log(" --groupid [groupid] - User group identifier.");
  588. } else {
  589. console.log(" --id '[userid]' - Identifier to remove.");
  590. console.log(" --groupid '[groupid]' - User group identifier.");
  591. }
  592. break;
  593. }
  594. case 'removeallusersfromusergroup': {
  595. console.log("Remove all users from a user group, Example usages:\r\n");
  596. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveAllUsersFromUserGroup --groupid 'ugrp//abcdf'"));
  597. console.log("\r\nRequired arguments:\r\n");
  598. if (process.platform == 'win32') {
  599. console.log(" --groupid [groupid] - User group identifier.");
  600. } else {
  601. console.log(" --groupid '[groupid]' - User group identifier.");
  602. }
  603. break;
  604. }
  605. case 'adddevicegroup': {
  606. console.log("Add a device group, Example usages:\r\n");
  607. console.log(" MeshCtrl AddDeviceGroup --name newgroupname");
  608. console.log(" MeshCtrl AddDeviceGroup --name newgroupname --desc description --amtonly");
  609. console.log(" MeshCtrl AddDeviceGroup --name newgroupname --features 1 --consent 7");
  610. console.log("\r\nRequired arguments:\r\n");
  611. console.log(" --name [name] - Name of the new group.");
  612. console.log("\r\nOptional arguments:\r\n");
  613. console.log(" --desc [description] - New group description.");
  614. console.log(" --amtonly - New group is agent-less, Intel AMT only.");
  615. console.log(" --agentless - New group is agent-less only.");
  616. console.log(" --features [number] - Set device group features, sum of numbers below.");
  617. console.log(" 1 = Auto-Remove 2 = Hostname Sync");
  618. console.log(" 4 = Record Sessions");
  619. console.log(" --consent [number] - Set device group user consent, sum of numbers below.");
  620. console.log(" 1 = Desktop notify user 2 = Terminal notify user ");
  621. console.log(" 4 = Files notify user 8 = Desktop prompt user ");
  622. console.log(" 16 = Terminal prompt user 32 = Files prompt user ");
  623. console.log(" 64 = Desktop Toolbar ");
  624. break;
  625. }
  626. case 'removedevicegroup': {
  627. console.log("Remove a device group, Example usages:\r\n");
  628. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveDeviceGroup --id 'groupid'"));
  629. console.log("\r\nRequired arguments:\r\n");
  630. if (process.platform == 'win32') {
  631. console.log(" --id [groupid] - Device group identifier (or --group).");
  632. } else {
  633. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  634. }
  635. console.log(" --group [groupname] - Device group name (or --id).");
  636. break;
  637. }
  638. case 'editdevicegroup': {
  639. console.log("Edit a device group, Example usages:\r\n");
  640. console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --name \"New Name\""));
  641. console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --desc \"Description\" --consent 63"));
  642. console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --invitecodes \"code1,code2\" --backgroundonly"));
  643. console.log("\r\nRequired arguments:\r\n");
  644. if (process.platform == 'win32') {
  645. console.log(" --id [groupid] - Device group identifier (or --group).");
  646. } else {
  647. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  648. }
  649. console.log(" --group [groupname] - Device group name (or --id).");
  650. console.log("\r\nOptional arguments:\r\n");
  651. console.log(" --name [name] - Set new device group name.");
  652. console.log(" --desc [description] - Set new device group description, blank to clear.");
  653. console.log(" --flags [number] - Set device group flags, sum of the values below, 0 for none.");
  654. console.log(" 1 = Auto remove device on disconnect.");
  655. console.log(" 2 = Sync hostname.");
  656. console.log(" --consent [number] - Set device group consent options, sum of the values below, 0 for none.");
  657. console.log(" 1 = Desktop notify user.");
  658. console.log(" 2 = Terminal notify user.");
  659. console.log(" 4 = Files notify user.");
  660. console.log(" 8 = Desktop prompt for user consent.");
  661. console.log(" 16 = Terminal prompt for user consent.");
  662. console.log(" 32 = Files prompt for user consent.");
  663. console.log(" 64 = Desktop show connection toolbar.");
  664. console.log(" --invitecodes [aa,bb] - Comma separated list of invite codes, blank to clear.");
  665. console.log(" --backgroundonly - When used with invitecodes, set agent to only install in background.");
  666. console.log(" --interactiveonly - When used with invitecodes, set agent to only run on demand.");
  667. break;
  668. }
  669. case 'movetodevicegroup': {
  670. console.log("Move a device to a new device group, Example usages:\r\n");
  671. console.log(winRemoveSingleQuotes(" MeshCtrl MoveToDeviceGroup --devid 'deviceid' --id 'groupid'"));
  672. console.log("\r\nRequired arguments:\r\n");
  673. if (process.platform == 'win32') {
  674. console.log(" --id [groupid] - Device group identifier (or --group).");
  675. } else {
  676. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  677. }
  678. console.log(" --group [groupname] - Device group name (or --id).");
  679. if (process.platform == 'win32') {
  680. console.log(" --devid [deviceid] - Device identifier.");
  681. } else {
  682. console.log(" --devid '[deviceid]' - Device identifier.");
  683. }
  684. break;
  685. }
  686. case 'addusertodevicegroup': {
  687. console.log("Add a user to a device group, Example usages:\r\n");
  688. console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDeviceGroup --id 'groupid' --userid userid --fullrights"));
  689. console.log(" MeshCtrl AddUserToDeviceGroup --group groupname --userid userid --editgroup --manageusers");
  690. console.log("\r\nRequired arguments:\r\n");
  691. if (process.platform == 'win32') {
  692. console.log(" --id [groupid] - Device group identifier (or --group).");
  693. } else {
  694. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  695. }
  696. console.log(" --group [groupname] - Device group name (or --id).");
  697. console.log(" --userid [userid] - The user identifier.");
  698. console.log("\r\nOptional arguments:\r\n");
  699. console.log(" --fullrights - Allow full rights over this device group.");
  700. console.log(" --editgroup - Allow the user to edit group information.");
  701. console.log(" --manageusers - Allow the user to add/remove users.");
  702. console.log(" --managedevices - Allow the user to edit device information.");
  703. console.log(" --remotecontrol - Allow device remote control operations.");
  704. console.log(" --agentconsole - Allow agent console operations.");
  705. console.log(" --serverfiles - Allow access to group server files.");
  706. console.log(" --wakedevices - Allow device wake operation.");
  707. console.log(" --notes - Allow editing of device notes.");
  708. console.log(" --desktopviewonly - Restrict user to view-only remote desktop.");
  709. console.log(" --limiteddesktop - Limit remote desktop keys.");
  710. console.log(" --noterminal - Hide the terminal tab from this user.");
  711. console.log(" --nofiles - Hide the files tab from this user.");
  712. console.log(" --noamt - Hide the Intel AMT tab from this user.");
  713. console.log(" --limitedevents - User can only see his own events.");
  714. console.log(" --chatnotify - Allow chat and notification options.");
  715. console.log(" --uninstall - Allow remote uninstall of the agent.");
  716. if (args.limiteddesktop) { meshrights |= 4096; }
  717. if (args.limitedevents) { meshrights |= 8192; }
  718. if (args.chatnotify) { meshrights |= 16384; }
  719. if (args.uninstall) { meshrights |= 32768; }
  720. break;
  721. }
  722. case 'removeuserfromdevicegroup': {
  723. console.log("Remove a user from a device group, Example usages:\r\n");
  724. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveuserFromDeviceGroup --id 'groupid' --userid userid"));
  725. console.log("\r\nRequired arguments:\r\n");
  726. if (process.platform == 'win32') {
  727. console.log(" --id [groupid] - Device group identifier (or --group).");
  728. } else {
  729. console.log(" --id '[groupid]' - Device group identifier (or --group).");
  730. }
  731. console.log(" --group [groupname] - Device group name (or --id).");
  732. console.log(" --userid [userid] - The user identifier.");
  733. break;
  734. }
  735. case 'addusertodevice': {
  736. console.log("Add a user to a device, Example usages:\r\n");
  737. console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDevice --id 'deviceid' --userid userid --fullrights"));
  738. console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDevice --id 'deviceid' --userid userid --remotecontrol"));
  739. console.log("\r\nRequired arguments:\r\n");
  740. if (process.platform == 'win32') {
  741. console.log(" --id [deviceid] - The device identifier.");
  742. } else {
  743. console.log(" --id '[deviceid]' - The device identifier.");
  744. }
  745. console.log(" --userid [userid] - The user identifier.");
  746. console.log("\r\nOptional arguments:\r\n");
  747. console.log(" --fullrights - Allow full rights over this device.");
  748. console.log(" --remotecontrol - Allow device remote control operations.");
  749. console.log(" --agentconsole - Allow agent console operations.");
  750. console.log(" --serverfiles - Allow access to group server files.");
  751. console.log(" --wakedevices - Allow device wake operation.");
  752. console.log(" --notes - Allow editing of device notes.");
  753. console.log(" --desktopviewonly - Restrict user to view-only remote desktop.");
  754. console.log(" --limiteddesktop - Limit remote desktop keys.");
  755. console.log(" --noterminal - Hide the terminal tab from this user.");
  756. console.log(" --nofiles - Hide the files tab from this user.");
  757. console.log(" --noamt - Hide the Intel AMT tab from this user.");
  758. console.log(" --limitedevents - User can only see his own events.");
  759. console.log(" --chatnotify - Allow chat and notification options.");
  760. console.log(" --uninstall - Allow remote uninstall of the agent.");
  761. break;
  762. }
  763. case 'removeuserfromdevice': {
  764. console.log("Remove a user from a device, Example usages:\r\n");
  765. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveuserFromDeviceGroup --id 'deviceid' --userid userid"));
  766. console.log("\r\nRequired arguments:\r\n");
  767. if (process.platform == 'win32') {
  768. console.log(" --id [deviceid] - The device identifier.");
  769. } else {
  770. console.log(" --id '[deviceid]' - The device identifier.");
  771. }
  772. console.log(" --userid [userid] - The user identifier.");
  773. break;
  774. }
  775. case 'broadcast': {
  776. console.log("Display a message to one or all logged in users, Example usages:\r\n");
  777. console.log(" MeshCtrl Broadcast --msg \"This is a test\"");
  778. console.log("\r\nRequired arguments:\r\n");
  779. console.log(" --msg [message] - Message to display.");
  780. console.log("\r\nOptional arguments:\r\n");
  781. console.log(" --user [userid] - Send the message to the specified user.");
  782. break;
  783. }
  784. case 'deviceinfo': {
  785. console.log("Display information about a device, Example usages:\r\n");
  786. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceInfo --id 'deviceid'"));
  787. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceInfo --id 'deviceid' --json"));
  788. console.log("\r\nRequired arguments:\r\n");
  789. if (process.platform == 'win32') {
  790. console.log(" --id [deviceid] - The device identifier.");
  791. } else {
  792. console.log(" --id '[deviceid]' - The device identifier.");
  793. }
  794. console.log("\r\nOptional arguments:\r\n");
  795. console.log(" --raw - Output raw data in JSON format.");
  796. console.log(" --json - Give results in JSON format.");
  797. break;
  798. }
  799. case 'removedevice': {
  800. console.log("Delete a device, Example usages:\r\n");
  801. console.log(winRemoveSingleQuotes(" MeshCtrl RemoveDevice --id 'deviceid'"));
  802. console.log("\r\nRequired arguments:\r\n");
  803. if (process.platform == 'win32') {
  804. console.log(" --id [deviceid] - The device identifier.");
  805. } else {
  806. console.log(" --id '[deviceid]' - The device identifier.");
  807. }
  808. break;
  809. }
  810. case 'addlocaldevice': {
  811. console.log("Add a Local Device, Example usages:\r\n");
  812. console.log(winRemoveSingleQuotes(" MeshCtrl AddLocalDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname'"));
  813. console.log(winRemoveSingleQuotes(" MeshCtrl AddLocalDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname' --type 6"));
  814. console.log("\r\nRequired arguments:\r\n");
  815. if (process.platform == 'win32') {
  816. console.log(" --id [meshid] - The mesh identifier.");
  817. console.log(" --devicename [devicename] - The device name.");
  818. console.log(" --hostname [hostname] - The devices hostname or ip address.");
  819. } else {
  820. console.log(" --id '[meshid]' - The mesh identifier.");
  821. console.log(" --devicename '[devicename]' - The device name.");
  822. console.log(" --hostname '[hostname]' - The devices hostname or ip address.");
  823. }
  824. console.log("\r\nOptional arguments:\r\n");
  825. console.log(" --type [TypeNumber] - With the following choices:");
  826. console.log(" type 4 - Default, Windows (RDP)");
  827. console.log(" type 6 - Linux (SSH/SCP/VNC)");
  828. console.log(" type 29 - macOS (SSH/SCP/VNC)");
  829. break;
  830. }
  831. case 'addamtdevice': {
  832. console.log("Add an Intel AMT Device, Example usages:\r\n");
  833. console.log(winRemoveSingleQuotes(" MeshCtrl AddAmtDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname --user 'admin' --pass 'admin'"));
  834. console.log(winRemoveSingleQuotes(" MeshCtrl AddAmtDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname --user 'admin' --pass 'admin' --notls"));
  835. console.log("\r\nRequired arguments:\r\n");
  836. if (process.platform == 'win32') {
  837. console.log(" --id [meshid] - The mesh identifier.");
  838. console.log(" --devicename [devicename] - The device name.");
  839. console.log(" --hostname [hostname] - The devices hostname or ip address.");
  840. console.log(" --user [user] - The devices AMT username.");
  841. console.log(" --pass [pass] - The devices AMT password.");
  842. console.log("")
  843. } else {
  844. console.log(" --id '[meshid]' - The mesh identifier.");
  845. console.log(" --devicename '[devicename]' - The device name.");
  846. console.log(" --hostname '[hostname]' - The devices hostname or ip address.");
  847. console.log(" --user '[user]' - The devices AMT username.");
  848. console.log(" --pass '[pass]' - The devices AMT password.");
  849. }
  850. console.log("\r\nOptional arguments:\r\n");
  851. if (process.platform == 'win32') {
  852. console.log(" --notls - Use No TLS Security.");
  853. } else {
  854. console.log(" --notls - Use No TLS Security.");
  855. }
  856. break;
  857. }
  858. case 'editdevice': {
  859. console.log("Change information about a device, Example usages:\r\n");
  860. console.log(winRemoveSingleQuotes(" MeshCtrl EditDevice --id 'deviceid' --name 'device1'"));
  861. console.log("\r\nRequired arguments:\r\n");
  862. if (process.platform == 'win32') {
  863. console.log(" --id [deviceid] - The device identifier.");
  864. } else {
  865. console.log(" --id '[deviceid]' - The device identifier.");
  866. }
  867. console.log("\r\nOptional arguments:\r\n");
  868. if (process.platform == 'win32') {
  869. console.log(" --name [name] - Change device name.");
  870. console.log(" --desc [description] - Change device description.");
  871. console.log(" --tags [tag1,tags2] - Change device tags.");
  872. } else {
  873. console.log(" --name '[name]' - Change device name.");
  874. console.log(" --desc '[description]' - Change device description.");
  875. console.log(" --tags '[tag1,tags2]' - Change device tags.");
  876. }
  877. console.log(" --icon [number] - Change the device icon (1 to 8).");
  878. console.log(" --consent [flags] - Sum of the following numbers:");
  879. console.log(" 1 = Desktop notify 2 = Terminal notify");
  880. console.log(" 4 = Files notify 8 = Desktop prompt");
  881. console.log(" 16 = Terminal prompt 32 = Files prompt");
  882. console.log(" 64 = Desktop privacy bar");
  883. break;
  884. }
  885. case 'runcommand': {
  886. console.log("Run a shell command on a remote device, Example usages:\r\n");
  887. console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\""));
  888. console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --powershell"));
  889. console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --reply"));
  890. console.log("\r\nRequired arguments:\r\n");
  891. if (process.platform == 'win32') {
  892. console.log(" --id [deviceid] - The device identifier.");
  893. } else {
  894. console.log(" --id '[deviceid]' - The device identifier.");
  895. }
  896. console.log(" --run \"[command]\" - Shell command to execute on the remote device.");
  897. console.log("\r\nOptional arguments:\r\n");
  898. console.log(" --powershell - Run in Windows PowerShell.");
  899. console.log(" --runasuser - Attempt to run the command as logged in user.");
  900. console.log(" --runasuseronly - Only run the command as the logged in user.");
  901. console.log(" --reply - Return with the output from running the command.");
  902. break;
  903. }
  904. case 'shell': {
  905. console.log("Access a command shell on a remote device, Example usages:\r\n");
  906. console.log(winRemoveSingleQuotes(" MeshCtrl Shell --id 'deviceid'"));
  907. console.log(winRemoveSingleQuotes(" MeshCtrl Shell --id 'deviceid' --powershell"));
  908. console.log("\r\nRequired arguments:\r\n");
  909. if (process.platform == 'win32') {
  910. console.log(" --id [deviceid] - The device identifier.");
  911. } else {
  912. console.log(" --id '[deviceid]' - The device identifier.");
  913. }
  914. console.log("\r\nOptional arguments:\r\n");
  915. console.log(" --powershell - Run a Windows PowerShell.");
  916. break;
  917. }
  918. case 'devicepower': {
  919. console.log("Perform power operations on remote devices, Example usages:\r\n");
  920. console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --wake --id 'deviceid'"));
  921. console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --sleep --id 'deviceid'"));
  922. console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --reset --id 'deviceid'"));
  923. console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --off --id 'deviceid1,deviceid2'"));
  924. console.log("\r\nNote that some power operations may take up to a minute to execute.\r\n");
  925. console.log("Required arguments:\r\n");
  926. if (process.platform == 'win32') {
  927. console.log(" --id [deviceid1,deviceid2] - Device identifiers.");
  928. } else {
  929. console.log(" --id '[deviceid1,deviceid2]' - Device identifiers.");
  930. }
  931. console.log("\r\nOptional arguments:\r\n");
  932. console.log(" --wake - Attempt to wake up the remote device.");
  933. console.log(" --reset - Attempt to remote the remote device.");
  934. console.log(" --sleep - Attempt to place the remote device in low power mode.");
  935. console.log(" --off - Attempt to power off the remote device.");
  936. console.log(" --amtoff - Attempt to power off the remote device using Intel AMT.");
  937. console.log(" --amton - Attempt to power on the remote device using Intel AMT.");
  938. console.log(" --amtreset - Attempt to reset the remote device using Intel AMT.");
  939. break;
  940. }
  941. case 'devicesharing': {
  942. var tzoffset = (new Date()).getTimezoneOffset() * 60000; // Offset in milliseconds
  943. var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -5);
  944. console.log("List sharing links for a specified device, Example usages:\r\n");
  945. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid'"));
  946. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --remove abcdef"));
  947. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30"));
  948. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30 --daily"));
  949. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type desktop,terminal --consent prompt"));
  950. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type http --port 80"));
  951. console.log("\r\nRequired arguments:\r\n");
  952. if (process.platform == 'win32') {
  953. console.log(" --id [deviceid] - The device identifier.");
  954. } else {
  955. console.log(" --id '[deviceid]' - The device identifier.");
  956. }
  957. console.log("\r\nOptional arguments:\r\n");
  958. console.log(" --remove [shareid] - Remove a device sharing link.");
  959. console.log(" --add [guestname] - Add a device sharing link.");
  960. console.log(" --type [desktop,terminal,files,http,https] - Type of sharing to add, can be combined. default is desktop.");
  961. console.log(" --viewonly - Make desktop sharing view only.");
  962. console.log(" --consent [notify,prompt,none] - Consent flags, default is notify.");
  963. console.log(" --start [yyyy-mm-ddThh:mm:ss] - Start time, default is now.");
  964. console.log(" --end [yyyy-mm-ddThh:mm:ss] - End time.");
  965. console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
  966. console.log(" --daily - Add recurring daily device share.");
  967. console.log(" --weekly - Add recurring weekly device share.");
  968. console.log(" --port [portnumber] - Set alternative port for http or https, default is 80 for http and 443 for https.");
  969. break;
  970. }
  971. case 'agentdownload': {
  972. console.log("Download an agent of a specific type for a given device group, Example usages:\r\n");
  973. console.log(winRemoveSingleQuotes(" MeshCtrl AgentDownload --id 'groupid' --type 3"));
  974. console.log(winRemoveSingleQuotes(" MeshCtrl AgentDownload --id 'groupid' --type 3 --installflags 1"));
  975. console.log("\r\nRequired arguments:\r\n");
  976. console.log(" --type [ArchitectureNumber] - Agent architecture number.");
  977. if (process.platform == 'win32') {
  978. console.log(" --id [groupid] - The device group identifier.");
  979. } else {
  980. console.log(" --id '[groupid]' - The device group identifier.");
  981. }
  982. console.log("\r\nOptional arguments:\r\n");
  983. console.log(" --installflags [InstallFlagsNumber] - With the following choices:");
  984. console.log(" installflags 0 - Default, Interactive & Background, offers connect button & install/uninstall");
  985. console.log(" installflags 1 - Interactive only, offers only connect button, not install/uninstall");
  986. console.log(" installflags 2 - Background only, offers only install/uninstall, not connect");
  987. break;
  988. }
  989. case 'upload': {
  990. console.log("Upload a local file to a remote device, Example usages:\r\n");
  991. console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target c:\\"));
  992. console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target /tmp"));
  993. console.log("\r\nRequired arguments:\r\n");
  994. if (process.platform == 'win32') {
  995. console.log(" --id [deviceid] - The device identifier.");
  996. } else {
  997. console.log(" --id '[deviceid]' - The device identifier.");
  998. }
  999. console.log(" --file [localfile] - The local file to upload.");
  1000. console.log(" --target [remotepath] - The remote path to upload the file to.");
  1001. break;
  1002. }
  1003. case 'download': {
  1004. console.log("Download a file from a remote device, Example usages:\r\n");
  1005. console.log(winRemoveSingleQuotes(" MeshCtrl Download --id 'deviceid' --file C:\\sample.txt --target c:\\temp"));
  1006. console.log(winRemoveSingleQuotes(" MeshCtrl Download --id 'deviceid' --file /tmp/sample.txt --target /tmp"));
  1007. console.log("\r\nRequired arguments:\r\n");
  1008. if (process.platform == 'win32') {
  1009. console.log(" --id [deviceid] - The device identifier.");
  1010. } else {
  1011. console.log(" --id '[deviceid]' - The device identifier.");
  1012. }
  1013. console.log(" --file [remotefile] - The remote file to download.");
  1014. console.log("\r\nOptional arguments:\r\n");
  1015. console.log(" --target [localpath] - The local path to download the file to.");
  1016. break;
  1017. }
  1018. case 'webrelay': {
  1019. console.log("Generate a webrelay URL to access a HTTP/HTTPS service on a remote device, Example usages:\r\n");
  1020. console.log(winRemoveSingleQuotes(" MeshCtrl WebRelay --id 'deviceid' --type http --port 80"));
  1021. console.log(winRemoveSingleQuotes(" MeshCtrl WebRelay --id 'deviceid' --type https --port 443"));
  1022. console.log("\r\nRequired arguments:\r\n");
  1023. if (process.platform == 'win32') {
  1024. console.log(" --id [deviceid] - The device identifier.");
  1025. } else {
  1026. console.log(" --id '[deviceid]' - The device identifier.");
  1027. }
  1028. console.log(" --type [http,https] - Type of relay from remote device, http or https.");
  1029. console.log("\r\nOptional arguments:\r\n");
  1030. console.log(" --port [portnumber] - Set alternative port for http or https, default is 80 for http and 443 for https.");
  1031. break;
  1032. }
  1033. case 'deviceopenurl': {
  1034. console.log("Open a web page on a remote device, Example usages:\r\n");
  1035. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceOpenUrl --id 'deviceid' --openurl http://meshcentral.com"));
  1036. console.log("\r\nRequired arguments:\r\n");
  1037. if (process.platform == 'win32') {
  1038. console.log(" --id [deviceid] - The device identifier.");
  1039. } else {
  1040. console.log(" --id '[deviceid]' - The device identifier.");
  1041. }
  1042. console.log(" --openurl [url] - Link to the web page.");
  1043. break;
  1044. }
  1045. case 'devicemessage': {
  1046. console.log("Display a message on the remote device, Example usages:\r\n");
  1047. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\""));
  1048. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\" --title \"title\""));
  1049. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\" --title \"title\" --timeout 120000"));
  1050. console.log("\r\nRequired arguments:\r\n");
  1051. if (process.platform == 'win32') {
  1052. console.log(" --id [deviceid] - The device identifier.");
  1053. } else {
  1054. console.log(" --id '[deviceid]' - The device identifier.");
  1055. }
  1056. console.log(" --msg [message] - The message to display.");
  1057. console.log("\r\nOptional arguments:\r\n");
  1058. console.log(" --title [title] - Messagebox title, default is \"MeshCentral\".");
  1059. console.log(" --timeout [miliseconds] - After timeout messagebox vanishes, 0 keeps messagebox open until closed manually, default is 120000 (2 Minutes).");
  1060. break;
  1061. }
  1062. case 'devicetoast': {
  1063. console.log("Display a toast message on the remote device, Example usages:\r\n");
  1064. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceToast --id 'deviceid' --msg \"message\""));
  1065. console.log(winRemoveSingleQuotes(" MeshCtrl DeviceToast --id 'deviceid' --msg \"message\" --title \"title\""));
  1066. console.log("\r\nRequired arguments:\r\n");
  1067. if (process.platform == 'win32') {
  1068. console.log(" --id [deviceid] - The device identifier.");
  1069. } else {
  1070. console.log(" --id '[deviceid]' - The device identifier.");
  1071. }
  1072. console.log(" --msg [message] - The message to display.");
  1073. console.log("\r\nOptional arguments:\r\n");
  1074. console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
  1075. break;
  1076. }
  1077. case 'groupmessage': {
  1078. console.log("Open a message box on remote devices in a specific device group, Example usages:\r\n");
  1079. console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\""));
  1080. console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\""));
  1081. console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\" --timeout 120000"));
  1082. console.log("\r\nRequired arguments:\r\n");
  1083. if (process.platform == 'win32') {
  1084. console.log(" --id [devicegroupid] - The device identifier.");
  1085. } else {
  1086. console.log(" --id '[devicegroupid]' - The device identifier.");
  1087. }
  1088. console.log(" --msg [message] - The message to display.");
  1089. console.log("\r\nOptional arguments:\r\n");
  1090. console.log(" --title [title] - Messagebox title, default is \"MeshCentral\".");
  1091. console.log(" --timeout [miliseconds] - After timeout messagebox vanishes, 0 keeps messagebox open until closed manually, default is 120000 (2 Minutes).");
  1092. break;
  1093. }
  1094. case 'grouptoast': {
  1095. console.log("Display a toast notification on remote devices in a specific device group, Example usages:\r\n");
  1096. console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\""));
  1097. console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\" --title \"title\""));
  1098. console.log("\r\nRequired arguments:\r\n");
  1099. if (process.platform == 'win32') {
  1100. console.log(" --id [devicegroupid] - The device identifier.");
  1101. } else {
  1102. console.log(" --id '[devicegroupid]' - The device identifier.");
  1103. }
  1104. console.log(" --msg [message] - The message to display.");
  1105. console.log("\r\nOptional arguments:\r\n");
  1106. console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
  1107. break;
  1108. }
  1109. case 'report': {
  1110. console.log("Generate a CSV report, Example usages:\r\n");
  1111. console.log(" MeshCtrl Report --type sessions --devicegroup mesh//...");
  1112. console.log(" MeshCtrl Report --type traffic --json");
  1113. console.log(" MeshCtrl Report --type logins --groupby day");
  1114. console.log(" MeshCtrl Report --type db");
  1115. console.log("\r\nOptional arguments:\r\n");
  1116. console.log(" --start [yyyy-mm-ddThh:mm:ss] - Filter the results starting at that date. Defaults to last 24h and last week when used with --groupby day. Usable with sessions, traffic and logins");
  1117. console.log(" --end [yyyy-mm-ddThh:mm:ss] - Filter the results ending at that date. Defaults to now. Usable with sessions, traffic and logins");
  1118. console.log(" --groupby [name] - How to group results. Options: user, day, device. Defaults to user. User and day usable in sessions and logins, device usable in sessions.");
  1119. console.log(" --devicegroup [devicegroupid] - Filter the results by device group. Usable in sessions");
  1120. console.log(" --showtraffic - Add traffic data in sessions report");
  1121. break;
  1122. }
  1123. default: {
  1124. console.log("Get help on an action. Type:\r\n\r\n help [action]\r\n\r\nPossible actions are: " + possibleCommands.join(', ') + '.');
  1125. }
  1126. }
  1127. }
  1128. break;
  1129. }
  1130. }
  1131. if (ok) {
  1132. if(args.loginpass===true){
  1133. const readline = require('readline');
  1134. const rl = readline.createInterface({
  1135. input: process.stdin,
  1136. output: process.stdout,
  1137. terminal: false
  1138. });
  1139. process.stdout.write('Enter your password: ');
  1140. const stdin = process.openStdin();
  1141. stdin.setRawMode(true); // Set raw mode to prevent echoing of characters
  1142. stdin.resume();
  1143. args.loginpass = '';
  1144. process.stdin.on('data', (char) => {
  1145. char = char + '';
  1146. switch (char) {
  1147. case '\n':
  1148. case '\r':
  1149. case '\u0004': // They've finished entering their password
  1150. stdin.setRawMode(false);
  1151. stdin.pause();
  1152. process.stdout.clearLine(); process.stdout.cursorTo(0);
  1153. rl.close();
  1154. serverConnect();
  1155. break;
  1156. case '\u0003': // Ctrl+C
  1157. process.stdout.write('\n');
  1158. process.exit();
  1159. break;
  1160. default: // Mask the password with "*"
  1161. args.loginpass += char;
  1162. process.stdout.clearLine(); process.stdout.cursorTo(0);
  1163. process.stdout.write('Enter your password: ' + '*'.repeat(args.loginpass.length));
  1164. break;
  1165. }
  1166. });
  1167. }else{
  1168. serverConnect();
  1169. }
  1170. }
  1171. }
  1172. function displayConfigHelp() {
  1173. console.log("Perform operations on the config.json file. Example usage:\r\n");
  1174. console.log(" MeshCtrl config --show");
  1175. console.log("\r\nOptional arguments:\r\n");
  1176. console.log(" --show - Display the config.json file.");
  1177. console.log(" --listdomains - Display non-default domains.");
  1178. console.log(" --adddomain [domain] - Add a domain.");
  1179. console.log(" --removedomain [domain] - Remove a domain.");
  1180. console.log(" --settodomain [domain] - Set values to the domain.");
  1181. console.log(" --removefromdomain [domain] - Remove values from the domain.");
  1182. console.log("\r\nWith adddomain, removedomain, settodomain and removefromdomain you can add the key and value pair. For example:\r\n");
  1183. console.log(" --adddomain \"MyDomain\" --title \"My Server Name\" --newAccounts false");
  1184. console.log(" --settodomain \"MyDomain\" --title \"My Server Name\"");
  1185. console.log(" --removefromdomain \"MyDomain\" --title");
  1186. }
  1187. function performConfigOperations(args) {
  1188. var domainValues = ['title', 'title2', 'titlepicture', 'trustedcert', 'welcomepicture', 'welcometext', 'userquota', 'meshquota', 'newaccounts', 'usernameisemail', 'newaccountemaildomains', 'newaccountspass', 'newaccountsrights', 'geolocation', 'lockagentdownload', 'userconsentflags', 'Usersessionidletimeout', 'auth', 'ldapoptions', 'ldapusername', 'ldapuserbinarykey', 'ldapuseremail', 'footer', 'certurl', 'loginKey', 'userallowedip', 'agentallowedip', 'agentnoproxy', 'agentconfig', 'orphanagentuser', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording', 'hide', 'customFiles'];
  1189. var domainObjectValues = ['ldapoptions', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording', 'customFiles'];
  1190. var domainArrayValues = ['newaccountemaildomains', 'newaccountsrights', 'loginkey', 'agentconfig'];
  1191. var configChange = false;
  1192. var fs = require('fs');
  1193. var path = require('path');
  1194. var configFile = 'config.json';
  1195. var didSomething = 0;
  1196. if (fs.existsSync(configFile) == false) { configFile = path.join('meshcentral-data', 'config.json'); }
  1197. if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, 'config.json'); }
  1198. if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, 'meshcentral-data', 'config.json'); }
  1199. if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, '..', 'meshcentral-data', 'config.json'); }
  1200. if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, '..', '..', 'meshcentral-data', 'config.json'); }
  1201. if (fs.existsSync(configFile) == false) { console.log("Unable to find config.json."); return; }
  1202. var config = null;
  1203. try { config = fs.readFileSync(configFile).toString('utf8'); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
  1204. try { config = JSON.parse(fs.readFileSync(configFile)); } catch (e) { console.log('ERROR: Unable to parse ' + configFile + '.'); return null; }
  1205. if (args.adddomain != null) {
  1206. didSomething++;
  1207. if (config.domains == null) { config.domains = {}; }
  1208. if (config.domains[args.adddomain] != null) { console.log("Error: Domain \"" + args.adddomain + "\" already exists"); }
  1209. else {
  1210. configChange = true;
  1211. config.domains[args.adddomain] = {};
  1212. for (var i in args) {
  1213. if (domainValues.indexOf(i.toLowerCase()) >= 0) {
  1214. if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
  1215. config.domains[args.adddomain][i] = args[i];
  1216. configChange = true;
  1217. }
  1218. }
  1219. }
  1220. }
  1221. if (args.removedomain != null) {
  1222. didSomething++;
  1223. if (config.domains == null) { config.domains = {}; }
  1224. if (config.domains[args.removedomain] == null) { console.log("Error: Domain \"" + args.removedomain + "\" does not exist"); }
  1225. else { delete config.domains[args.removedomain]; configChange = true; }
  1226. }
  1227. if (args.settodomain != null) {
  1228. didSomething++;
  1229. if (config.domains == null) { config.domains = {}; }
  1230. if (args.settodomain == true) { args.settodomain = ''; }
  1231. if (config.domains[args.settodomain] == null) { console.log("Error: Domain \"" + args.settodomain + "\" does not exist"); }
  1232. else {
  1233. for (var i in args) {
  1234. if ((i == '_') || (i == 'settodomain')) continue;
  1235. if (domainValues.indexOf(i.toLowerCase()) >= 0) {
  1236. var isObj = (domainObjectValues.indexOf(i.toLowerCase()) >= 0);
  1237. var isArr = (domainArrayValues.indexOf(i.toLowerCase()) >= 0);
  1238. if ((isObj == false) && (isArr == false)) {
  1239. // Simple value set
  1240. if (args[i] == '') { delete config.domains[args.settodomain][i]; configChange = true; } else {
  1241. if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
  1242. config.domains[args.settodomain][i] = args[i];
  1243. configChange = true;
  1244. }
  1245. } else if (isObj || isArr) {
  1246. // Set an object/array value
  1247. if (args[i] == '') { delete config.domains[args.settodomain][i]; configChange = true; } else {
  1248. var x = null;
  1249. try { x = JSON.parse(args[i]); } catch (ex) { }
  1250. if ((x == null) || (typeof x != 'object')) { console.log("Unable to parse JSON for " + i + "."); } else {
  1251. if (isArr && Array.isArray(x) == false) {
  1252. console.log("Value " + i + " must be an array.");
  1253. } else if (!isArr && Array.isArray(x) == true) {
  1254. console.log("Value " + i + " must be an object.");
  1255. } else {
  1256. config.domains[args.settodomain][i] = x;
  1257. configChange = true;
  1258. }
  1259. }
  1260. }
  1261. }
  1262. } else {
  1263. console.log('Invalid configuration value: ' + i);
  1264. }
  1265. }
  1266. }
  1267. }
  1268. if (args.removefromdomain != null) {
  1269. didSomething++;
  1270. if (config.domains == null) { config.domains = {}; }
  1271. if (config.domains[args.removefromdomain] == null) { console.log("Error: Domain \"" + args.removefromdomain + "\" does not exist"); }
  1272. else { for (var i in args) { if (domainValues.indexOf(i.toLowerCase()) >= 0) { delete config.domains[args.removefromdomain][i]; configChange = true; } } }
  1273. }
  1274. if (configChange) {
  1275. try { fs.writeFileSync(configFile, JSON.stringify(config, null, 2)); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
  1276. }
  1277. if (args.show == 1) {
  1278. console.log(JSON.stringify(config, null, 2)); return;
  1279. } else if (args.listdomains == 1) {
  1280. if (config.domains == null) {
  1281. console.log('No domains found.'); return;
  1282. } else {
  1283. // Show the list of active domains, skip the default one.
  1284. for (var i in config.domains) { if ((i != '') && (i[0] != '_')) { console.log(i); } } return;
  1285. }
  1286. } else {
  1287. if (didSomething == 0) {
  1288. displayConfigHelp();
  1289. } else {
  1290. console.log("Done.");
  1291. }
  1292. }
  1293. }
  1294. function onVerifyServer(clientName, certs) { return null; }
  1295. function serverConnect() {
  1296. const WebSocket = require('ws');
  1297. var url = 'wss://localhost/control.ashx';
  1298. if (args.url) {
  1299. url = args.url;
  1300. if (url.length < 5) { console.log("Invalid url."); process.exit(); return; }
  1301. if ((url.startsWith('wss://') == false) && (url.startsWith('ws://') == false)) { console.log("Invalid url."); process.exit(); return; }
  1302. var i = url.indexOf('?key='), loginKey = null;
  1303. if (i >= 0) { loginKey = url.substring(i + 5); url = url.substring(0, i); }
  1304. if (url.endsWith('/') == false) { url += '/'; }
  1305. url += 'control.ashx';
  1306. if (loginKey != null) { url += '?key=' + loginKey; }
  1307. }
  1308. // TODO: checkServerIdentity does not work???
  1309. var options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
  1310. // Setup the HTTP proxy if needed
  1311. if (args.proxy != null) {
  1312. const HttpsProxyAgent = require('https-proxy-agent');
  1313. options.agent = new HttpsProxyAgent(require('url').parse(args.proxy));
  1314. }
  1315. // Password authentication
  1316. if (args.loginpass != null) {
  1317. var username = 'admin';
  1318. if (args.loginuser != null) { username = args.loginuser; }
  1319. var token = '';
  1320. if (args.token != null) { token = ',' + Buffer.from('' + args.token).toString('base64'); }
  1321. options.headers = { 'x-meshauth': Buffer.from('' + username).toString('base64') + ',' + Buffer.from('' + args.loginpass).toString('base64') + token }
  1322. }
  1323. // Cookie authentication
  1324. var ckey = null, loginCookie = null;
  1325. if (args.loginkey != null) {
  1326. // User key passed in as argument hex
  1327. if (args.loginkey.length != 160) { loginCookie = args.loginkey; }
  1328. ckey = Buffer.from(args.loginkey, 'hex');
  1329. if (ckey.length != 80) { ckey = null; loginCookie = args.loginkey; }
  1330. } else if (args.loginkeyfile != null) {
  1331. // Load key from hex file
  1332. var fs = require('fs');
  1333. try {
  1334. var keydata = fs.readFileSync(args.loginkeyfile, 'utf8').split(' ').join('').split('\r').join('').split('\n').join('');
  1335. ckey = Buffer.from(keydata, 'hex');
  1336. if (ckey.length != 80) { ckey = null; loginCookie = args.loginkey; }
  1337. } catch (ex) { console.log(ex.message); process.exit(); return; }
  1338. }
  1339. settings.xxurl = url;
  1340. if (ckey != null) {
  1341. var domainid = '', username = 'admin';
  1342. if (args.logindomain != null) { domainid = args.logindomain; }
  1343. if (args.loginuser != null) { username = args.loginuser; }
  1344. url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + encodeCookie({ userid: 'user/' + domainid + '/' + username, domainid: domainid }, ckey);
  1345. } else {
  1346. if (args.logindomain != null) { console.log("--logindomain can only be used along with --loginkey."); process.exit(); return; }
  1347. if (loginCookie != null) { url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + loginCookie; }
  1348. }
  1349. const ws = new WebSocket(url, options);
  1350. //console.log('Connecting to ' + url);
  1351. ws.on('open', function open() {
  1352. //console.log('Connected.');
  1353. switch (settings.cmd) {
  1354. case 'serverinfo': { break; }
  1355. case 'userinfo': { break; }
  1356. case 'listusers': { ws.send(JSON.stringify({ action: 'users', responseid: 'meshctrl' })); break; }
  1357. case 'listusersessions': { ws.send(JSON.stringify({ action: 'wssessioncount', responseid: 'meshctrl' })); break; }
  1358. case 'removeallusersfromusergroup':
  1359. case 'listusergroups': { ws.send(JSON.stringify({ action: 'usergroups', responseid: 'meshctrl' })); break; }
  1360. case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
  1361. case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
  1362. case 'listdevices': {
  1363. if (args.details) {
  1364. // Get list of devices with lots of details
  1365. ws.send(JSON.stringify({ action: 'getDeviceDetails', type: (args.csv) ? 'csv' : 'json' }));
  1366. } else if (args.group) {
  1367. ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' }));
  1368. } else if (args.id) {
  1369. ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
  1370. } else {
  1371. ws.send(JSON.stringify({ action: 'meshes' }));
  1372. ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' }));
  1373. }
  1374. break;
  1375. }
  1376. case 'listevents': {
  1377. limit = null;
  1378. if (args.limit) { limit = parseInt(args.limit); }
  1379. if ((typeof limit != 'number') || (limit < 1)) { limit = null; }
  1380. var cmd = null;
  1381. if (args.userid) {
  1382. cmd = { action: 'events', user: args.userid, responseid: 'meshctrl' };
  1383. } else if (args.id) {
  1384. cmd = { action: 'events', nodeid: args.id, responseid: 'meshctrl' };
  1385. } else {
  1386. cmd = { action: 'events', responseid: 'meshctrl' };
  1387. }
  1388. if (typeof limit == 'number') { cmd.limit = limit; }
  1389. ws.send(JSON.stringify(cmd));
  1390. break;
  1391. }
  1392. case 'logintokens': {
  1393. if (args.add) {
  1394. var cmd = { action: 'createLoginToken', name: args.add, expire: 0, responseid: 'meshctrl' };
  1395. if (args.expire) { cmd.expire = parseInt(args.expire); }
  1396. ws.send(JSON.stringify(cmd));
  1397. } else {
  1398. var cmd = { action: 'loginTokens', responseid: 'meshctrl' };
  1399. if (args.remove) { cmd.remove = [args.remove]; }
  1400. ws.send(JSON.stringify(cmd));
  1401. }
  1402. break;
  1403. }
  1404. case 'adduser': {
  1405. var siteadmin = getSiteAdminRights(args);
  1406. if (args.randompass) { args.pass = getRandomAmtPassword(); }
  1407. var op = { action: 'adduser', username: args.user, pass: args.pass, responseid: 'meshctrl' };
  1408. if (args.email) { op.email = args.email; if (args.emailverified) { op.emailVerified = true; } }
  1409. if (args.resetpass) { op.resetNextLogin = true; }
  1410. if (siteadmin != -1) { op.siteadmin = siteadmin; }
  1411. if (args.domain) { op.domain = args.domain; }
  1412. if (args.phone === true) { op.phone = ''; }
  1413. if (typeof args.phone == 'string') { op.phone = args.phone; }
  1414. if (typeof args.realname == 'string') { op.realname = args.realname; }
  1415. ws.send(JSON.stringify(op));
  1416. break;
  1417. }
  1418. case 'edituser': {
  1419. var userid = args.userid;
  1420. if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
  1421. var siteadmin = getSiteAdminRights(args);
  1422. var op = { action: 'edituser', userid: userid, responseid: 'meshctrl' };
  1423. if (args.email) { op.email = args.email; if (args.emailverified) { op.emailVerified = true; } }
  1424. if (args.resetpass) { op.resetNextLogin = true; }
  1425. if (siteadmin != -1) { op.siteadmin = siteadmin; }
  1426. if (args.domain) { op.domain = args.domain; }
  1427. if (args.phone === true) { op.phone = ''; }
  1428. if (typeof args.phone == 'string') { op.phone = args.phone; }
  1429. if (typeof args.realname == 'string') { op.realname = args.realname; }
  1430. if (args.realname === true) { op.realname = ''; }
  1431. ws.send(JSON.stringify(op));
  1432. break;
  1433. }
  1434. case 'removeuser': {
  1435. var userid = args.userid;
  1436. if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
  1437. ws.send(JSON.stringify({ action: 'deleteuser', userid: userid, responseid: 'meshctrl' }));
  1438. break;
  1439. }
  1440. case 'addusergroup': {
  1441. var op = { action: 'createusergroup', name: args.name, desc: args.desc, responseid: 'meshctrl' };
  1442. if (args.domain) { op.domain = args.domain; }
  1443. ws.send(JSON.stringify(op));
  1444. break;
  1445. }
  1446. case 'removeusergroup': {
  1447. var ugrpid = args.groupid;
  1448. if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
  1449. ws.send(JSON.stringify({ action: 'deleteusergroup', ugrpid: ugrpid, responseid: 'meshctrl' }));
  1450. break;
  1451. }
  1452. case 'addtousergroup': {
  1453. var ugrpid = args.groupid;
  1454. if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
  1455. // Add a user to a user group
  1456. if (args.userid != null) {
  1457. var userid = args.userid;
  1458. if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
  1459. ws.send(JSON.stringify({ action: 'addusertousergroup', ugrpid: ugrpid, usernames: [userid.split('/')[2]], responseid: 'meshctrl' }));
  1460. break;
  1461. }
  1462. if ((args.id != null) && (args.id.startsWith('user/'))) {
  1463. ws.send(JSON.stringify({ action: 'addusertousergroup', ugrpid: ugrpid, usernames: [args.id.split('/')[2]], responseid: 'meshctrl' }));
  1464. break;
  1465. }
  1466. var rights = 0;
  1467. if (args.rights != null) { rights = parseInt(args.rights); }
  1468. // Add a device group to a user group
  1469. if (args.meshid != null) {
  1470. var meshid = args.meshid;
  1471. if ((args.domain != null) && (userid.indexOf('/') < 0)) { meshid = 'mesh/' + args.domain + '/' + meshid; }
  1472. ws.send(JSON.stringify({ action: 'addmeshuser', meshid: meshid, userid: ugrpid, meshadmin: rights, responseid: 'meshctrl' }));
  1473. break;
  1474. }
  1475. if ((args.id != null) && (args.id.startsWith('mesh/'))) {
  1476. ws.send(JSON.stringify({ action: 'addmeshuser', meshid: args.id, userid: ugrpid, meshadmin: rights, responseid: 'meshctrl' }));
  1477. break;
  1478. }
  1479. // Add a device to a user group
  1480. if (args.nodeid != null) {
  1481. var nodeid = args.nodeid;
  1482. if ((args.domain != null) && (userid.indexOf('/') < 0)) { nodeid = 'node/' + args.domain + '/' + nodeid; }
  1483. ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: nodeid, userids: [ugrpid], rights: rights, responseid: 'meshctrl' }));
  1484. break;
  1485. }
  1486. if ((args.id != null) && (args.id.startsWith('node/'))) {
  1487. ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: args.id, userids: [ugrpid], rights: rights, responseid: 'meshctrl' }));
  1488. break;
  1489. }
  1490. break;
  1491. }
  1492. case 'removefromusergroup': {
  1493. var ugrpid = args.groupid;
  1494. if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
  1495. // Remove a user from a user group
  1496. if (args.userid != null) {
  1497. var userid = args.userid;
  1498. if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
  1499. ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: userid, responseid: 'meshctrl' }));
  1500. break;
  1501. }
  1502. if ((args.id != null) && (args.id.startsWith('user/'))) {
  1503. ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: args.id, responseid: 'meshctrl' }));
  1504. break;
  1505. }
  1506. // Remove a device group from a user group
  1507. if (args.meshid != null) {
  1508. var meshid = args.meshid;
  1509. if ((args.domain != null) && (userid.indexOf('/') < 0)) { meshid = 'mesh/' + args.domain + '/' + meshid; }
  1510. ws.send(JSON.stringify({ action: 'removemeshuser', meshid: meshid, userid: ugrpid, responseid: 'meshctrl' }));
  1511. break;
  1512. }
  1513. if ((args.id != null) && (args.id.startsWith('mesh/'))) {
  1514. ws.send(JSON.stringify({ action: 'removemeshuser', meshid: args.id, userid: ugrpid, responseid: 'meshctrl' }));
  1515. break;
  1516. }
  1517. // Remove a device from a user group
  1518. if (args.nodeid != null) {
  1519. var nodeid = args.nodeid;
  1520. if ((args.domain != null) && (userid.indexOf('/') < 0)) { nodeid = 'node/' + args.domain + '/' + nodeid; }
  1521. ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: nodeid, userids: [ugrpid], rights: 0, responseid: 'meshctrl', remove: true }));
  1522. break;
  1523. }
  1524. if ((args.id != null) && (args.id.startsWith('node/'))) {
  1525. ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: args.id, userids: [ugrpid], rights: 0, responseid: 'meshctrl', remove: true }));
  1526. break;
  1527. }
  1528. break;
  1529. }
  1530. case 'adddevicegroup': {
  1531. var op = { action: 'createmesh', meshname: args.name, meshtype: 2, responseid: 'meshctrl' };
  1532. if (args.desc) { op.desc = args.desc; }
  1533. if (args.amtonly) { op.meshtype = 1; }
  1534. if (args.agentless) { op.meshtype = 3; }
  1535. if (args.features) { op.flags = parseInt(args.features); }
  1536. if (args.consent) { op.consent = parseInt(args.consent); }
  1537. ws.send(JSON.stringify(op));
  1538. break;
  1539. }
  1540. case 'removedevicegroup': {
  1541. var op = { action: 'deletemesh', responseid: 'meshctrl' };
  1542. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1543. ws.send(JSON.stringify(op));
  1544. break;
  1545. }
  1546. case 'addamtdevice': {
  1547. var op = { action: 'addamtdevice', amttls: 1, responseid: 'meshctrl' };
  1548. if (args.id) { op.meshid = args.id; }
  1549. if ((typeof args.devicename == 'string') && (args.devicename != '')) { op.devicename = args.devicename; }
  1550. if ((typeof args.hostname == 'string') && (args.hostname != '')) { op.hostname = args.hostname; }
  1551. if ((typeof args.user == 'string') && (args.user != '')) { op.amtusername = args.user; }
  1552. if ((typeof args.pass == 'string') && (args.pass != '')) { op.amtpassword = args.pass; }
  1553. if (args.notls) { op.amttls = 0; }
  1554. ws.send(JSON.stringify(op));
  1555. break;
  1556. }
  1557. case 'addlocaldevice': {
  1558. var op = { action: 'addlocaldevice', type: 4, responseid: 'meshctrl' };
  1559. if (args.id) { op.meshid = args.id; }
  1560. if ((typeof args.devicename == 'string') && (args.devicename != '')) { op.devicename = args.devicename; }
  1561. if ((typeof args.hostname == 'string') && (args.hostname != '')) { op.hostname = args.hostname; }
  1562. if (args.type) {
  1563. if ((typeof parseInt(args.type) != 'number') || isNaN(parseInt(args.type))) { console.log("Invalid type."); process.exit(1); return; }
  1564. op.type = args.type;
  1565. }
  1566. ws.send(JSON.stringify(op));
  1567. break;
  1568. }
  1569. case 'editdevicegroup': {
  1570. var op = { action: 'editmesh', responseid: 'meshctrl' };
  1571. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshidname = args.group; }
  1572. if ((typeof args.name == 'string') && (args.name != '')) { op.meshname = args.name; }
  1573. if (args.desc === true) { op.desc = ""; } else if (typeof args.desc == 'string') { op.desc = args.desc; }
  1574. if (args.invitecodes === true) { op.invite = "*"; } else if (typeof args.invitecodes == 'string') {
  1575. var invitecodes = args.invitecodes.split(','), invitecodes2 = [];
  1576. for (var i in invitecodes) { if (invitecodes[i].length > 0) { invitecodes2.push(invitecodes[i]); } }
  1577. if (invitecodes2.length > 0) {
  1578. op.invite = { codes: invitecodes2, flags: 0 };
  1579. if (args.backgroundonly === true) { op.invite.flags = 2; }
  1580. else if (args.interactiveonly === true) { op.invite.flags = 1; }
  1581. }
  1582. }
  1583. if (args.flags != null) {
  1584. var flags = parseInt(args.flags);
  1585. if (typeof flags == 'number') { op.flags = flags; }
  1586. }
  1587. if (args.consent != null) {
  1588. var consent = parseInt(args.consent);
  1589. if (typeof consent == 'number') { op.consent = consent; }
  1590. }
  1591. ws.send(JSON.stringify(op));
  1592. break;
  1593. }
  1594. case 'movetodevicegroup': {
  1595. var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [args.devid] };
  1596. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1597. ws.send(JSON.stringify(op));
  1598. break;
  1599. }
  1600. case 'addusertodevicegroup': {
  1601. var meshrights = 0;
  1602. if (args.fullrights) { meshrights = 0xFFFFFFFF; }
  1603. if (args.editgroup) { meshrights |= 1; }
  1604. if (args.manageusers) { meshrights |= 2; }
  1605. if (args.managedevices) { meshrights |= 4; }
  1606. if (args.remotecontrol) { meshrights |= 8; }
  1607. if (args.agentconsole) { meshrights |= 16; }
  1608. if (args.serverfiles) { meshrights |= 32; }
  1609. if (args.wakedevices) { meshrights |= 64; }
  1610. if (args.notes) { meshrights |= 128; }
  1611. if (args.desktopviewonly) { meshrights |= 256; }
  1612. if (args.noterminal) { meshrights |= 512; }
  1613. if (args.nofiles) { meshrights |= 1024; }
  1614. if (args.noamt) { meshrights |= 2048; }
  1615. if (args.limiteddesktop) { meshrights |= 4096; }
  1616. if (args.limitedevents) { meshrights |= 8192; }
  1617. if (args.chatnotify) { meshrights |= 16384; }
  1618. if (args.uninstall) { meshrights |= 32768; }
  1619. var op = { action: 'addmeshuser', usernames: [args.userid], meshadmin: meshrights, responseid: 'meshctrl' };
  1620. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1621. ws.send(JSON.stringify(op));
  1622. break;
  1623. }
  1624. case 'removeuserfromdevicegroup': {
  1625. var op = { action: 'removemeshuser', userid: args.userid, responseid: 'meshctrl' };
  1626. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1627. ws.send(JSON.stringify(op));
  1628. break;
  1629. }
  1630. case 'addusertodevice': {
  1631. var meshrights = 0;
  1632. if (args.fullrights) { meshrights = (8 + 16 + 32 + 64 + 128 + 16384 + 32768); }
  1633. if (args.remotecontrol) { meshrights |= 8; }
  1634. if (args.agentconsole) { meshrights |= 16; }
  1635. if (args.serverfiles) { meshrights |= 32; }
  1636. if (args.wakedevices) { meshrights |= 64; }
  1637. if (args.notes) { meshrights |= 128; }
  1638. if (args.desktopviewonly) { meshrights |= 256; }
  1639. if (args.noterminal) { meshrights |= 512; }
  1640. if (args.nofiles) { meshrights |= 1024; }
  1641. if (args.noamt) { meshrights |= 2048; }
  1642. if (args.limiteddesktop) { meshrights |= 4096; }
  1643. if (args.limitedevents) { meshrights |= 8192; }
  1644. if (args.chatnotify) { meshrights |= 16384; }
  1645. if (args.uninstall) { meshrights |= 32768; }
  1646. var op = { action: 'adddeviceuser', nodeid: args.id, usernames: [args.userid], rights: meshrights, responseid: 'meshctrl' };
  1647. ws.send(JSON.stringify(op));
  1648. break;
  1649. }
  1650. case 'removeuserfromdevice': {
  1651. var op = { action: 'adddeviceuser', nodeid: args.id, usernames: [args.userid], rights: 0, remove: true, responseid: 'meshctrl' };
  1652. ws.send(JSON.stringify(op));
  1653. break;
  1654. }
  1655. case 'sendinviteemail': {
  1656. var op = { action: 'inviteAgent', email: args.email, name: '', os: '0', responseid: 'meshctrl' }
  1657. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1658. if (args.name) { op.name = args.name; }
  1659. if (args.message) { op.msg = args.message; }
  1660. ws.send(JSON.stringify(op));
  1661. break;
  1662. }
  1663. case 'generateinvitelink': {
  1664. var op = { action: 'createInviteLink', expire: args.hours, flags: 0, responseid: 'meshctrl' }
  1665. if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
  1666. if (args.flags) { op.flags = args.flags; }
  1667. ws.send(JSON.stringify(op));
  1668. break;
  1669. }
  1670. case 'broadcast': {
  1671. var op = { action: 'userbroadcast', msg: args.msg, responseid: 'meshctrl' };
  1672. if (args.user) { op.userid = args.user; }
  1673. ws.send(JSON.stringify(op));
  1674. break;
  1675. }
  1676. case 'showevents': {
  1677. console.log('Connected. Press ctrl-c to end.');
  1678. break;
  1679. }
  1680. case 'deviceinfo': {
  1681. settings.deviceinfocount = 4;
  1682. ws.send(JSON.stringify({ action: 'nodes' }));
  1683. ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: args.id, responseid: 'meshctrl' }));
  1684. ws.send(JSON.stringify({ action: 'lastconnect', nodeid: args.id, responseid: 'meshctrl' }));
  1685. ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: args.id, nodeinfo: true, responseid: 'meshctrl' }));
  1686. break;
  1687. }
  1688. case 'removedevice': {
  1689. var op = { action: 'removedevices', nodeids: [ args.id ], responseid: 'meshctrl' };
  1690. ws.send(JSON.stringify(op));
  1691. break;
  1692. }
  1693. case 'editdevice': {
  1694. var op = { action: 'changedevice', nodeid: args.id, responseid: 'meshctrl' };
  1695. if (typeof args.name == 'string') { op.name = args.name; }
  1696. if (typeof args.name == 'number') { op.name = '' + args.name; }
  1697. if (args.desc) { if (args.desc === true) { op.desc = ''; } else if (typeof args.desc == 'string') { op.desc = args.desc; } else if (typeof args.desc == 'number') { op.desc = '' + args.desc; } }
  1698. if (args.tags) { if (args.tags === true) { op.tags = ''; } else if (typeof args.tags == 'string') { op.tags = args.tags.split(','); } else if (typeof args.tags == 'number') { op.tags = '' + args.tags; } }
  1699. if (args.icon) { op.icon = parseInt(args.icon); if ((typeof op.icon != 'number') || isNaN(op.icon) || (op.icon < 1) || (op.icon > 8)) { console.log("Icon must be between 1 and 8."); process.exit(1); return; } }
  1700. if (args.consent) { op.consent = parseInt(args.consent); if ((typeof op.consent != 'number') || isNaN(op.consent) || (op.consent < 1)) { console.log("Invalid consent flags."); process.exit(1); return; } }
  1701. ws.send(JSON.stringify(op));
  1702. break;
  1703. }
  1704. case 'runcommand': {
  1705. var runAsUser = 0;
  1706. if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
  1707. var reply = false;
  1708. if (args.reply) { reply = true; }
  1709. ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser, reply: reply }));
  1710. break;
  1711. }
  1712. case 'shell':
  1713. case 'upload':
  1714. case 'download': {
  1715. ws.send("{\"action\":\"authcookie\"}");
  1716. break;
  1717. }
  1718. case 'devicepower': {
  1719. var nodes = args.id.split(',');
  1720. if (args.wake) {
  1721. // Wake operation
  1722. ws.send(JSON.stringify({ action: 'wakedevices', nodeids: nodes, responseid: 'meshctrl' }));
  1723. } else if (args.off) {
  1724. // Power off operation
  1725. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 2, responseid: 'meshctrl' }));
  1726. } else if (args.reset) {
  1727. // Reset operation
  1728. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 3, responseid: 'meshctrl' }));
  1729. } else if (args.sleep) {
  1730. // Sleep operation
  1731. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 4, responseid: 'meshctrl' }));
  1732. } else if (args.amton) {
  1733. // Intel AMT Power on operation
  1734. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 302, responseid: 'meshctrl' }));
  1735. } else if (args.amtoff) {
  1736. // Intel AMT Power off operation
  1737. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 308, responseid: 'meshctrl' }));
  1738. } else if (args.amtreset) {
  1739. // Intel AMT Power reset operation
  1740. ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 310, responseid: 'meshctrl' }));
  1741. } else {
  1742. console.log('No power operation specified.');
  1743. process.exit(1);
  1744. }
  1745. break;
  1746. }
  1747. case 'agentdownload': {
  1748. // Download an agent
  1749. var u = settings.xxurl.replace('wss://', 'https://').replace('/control.ashx', '/meshagents');
  1750. if (u.indexOf('?') > 0) { u += '&'; } else { u += '?'; }
  1751. u += 'id=' + args.type + '&meshid=' + args.id;
  1752. if (args.installflags) {
  1753. if ((typeof parseInt(args.installflags) != 'number') || isNaN(parseInt(args.installflags)) || (parseInt(args.installflags) < 0) || (parseInt(args.installflags) > 2)) { console.log("Invalid Installflags."); process.exit(1); return; }
  1754. u += '&installflags=' + args.installflags;
  1755. }
  1756. const options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
  1757. const fs = require('fs');
  1758. const https = require('https');
  1759. var downloadSize = 0;
  1760. const req = https.request(u, options, function (res) {
  1761. if (res.statusCode != 200) {
  1762. console.log('Download error, statusCode: ' + res.statusCode);
  1763. process.exit(1);
  1764. } else {
  1765. // Agent the agent filename
  1766. var agentFileName = 'meshagent';
  1767. if ((res.headers) && (res.headers['content-disposition'] != null)) {
  1768. var i = res.headers['content-disposition'].indexOf('filename=\"');
  1769. if (i >= 0) {
  1770. agentFileName = res.headers['content-disposition'].substring(i + 10);
  1771. i = agentFileName.indexOf('\"');
  1772. if (i >= 0) { agentFileName = agentFileName.substring(0, i); }
  1773. }
  1774. }
  1775. // Check if this file already exists
  1776. if (fs.existsSync(agentFileName)) { console.log('File \"' + agentFileName + '\" already exists.'); process.exit(1); }
  1777. var fd = fs.openSync(agentFileName, 'w'); // Open the file for writing
  1778. res.on('data', function (d) {
  1779. downloadSize += d.length;
  1780. fs.writeSync(fd, d); // Save to file
  1781. });
  1782. res.on('end', function (d) {
  1783. fs.closeSync(fd); // Close file
  1784. console.log('Downloaded ' + downloadSize + ' byte(s) to \"' + agentFileName + '\"');
  1785. process.exit(1);
  1786. });
  1787. }
  1788. })
  1789. req.on('error', function (error) { console.error(error); process.exit(1); })
  1790. req.end()
  1791. break;
  1792. }
  1793. case 'webrelay': {
  1794. var protocol = null;
  1795. if (args.type != null) {
  1796. if (args.type == 'http') {
  1797. protocol = 1;
  1798. } else if (args.type == 'https') {
  1799. protocol = 2;
  1800. } else {
  1801. console.log("Unknown protocol type: " + args.type); process.exit(1);
  1802. }
  1803. }
  1804. var port = null;
  1805. if (typeof args.port == 'number') {
  1806. if ((args.port < 1) || (args.port > 65535)) { console.log("Port number must be between 1 and 65535."); process.exit(1); }
  1807. port = args.port;
  1808. } else if (protocol == 1) {
  1809. port = 80;
  1810. } else if (protocol == 2) {
  1811. port = 443;
  1812. }
  1813. ws.send(JSON.stringify({ action: 'webrelay', nodeid: args.id, port: port, appid: protocol, responseid: 'meshctrl' }));
  1814. break;
  1815. }
  1816. case 'devicesharing': {
  1817. if (args.add) {
  1818. if (args.add.length == 0) { console.log("Invalid guest name."); process.exit(1); }
  1819. // Sharing type, desktop or terminal
  1820. var p = 0;
  1821. if (args.type != null) {
  1822. var shareTypes = args.type.toLowerCase().split(',');
  1823. for (var i in shareTypes) { if ((shareTypes[i] != 'terminal') && (shareTypes[i] != 'desktop') && (shareTypes[i] != 'files') && (shareTypes[i] != 'http') && (shareTypes[i] != 'https')) { console.log("Unknown sharing type: " + shareTypes[i]); process.exit(1); } }
  1824. if (shareTypes.indexOf('terminal') >= 0) { p |= 1; }
  1825. if (shareTypes.indexOf('desktop') >= 0) { p |= 2; }
  1826. if (shareTypes.indexOf('files') >= 0) { p |= 4; }
  1827. if (shareTypes.indexOf('http') >= 0) { p |= 8; }
  1828. if (shareTypes.indexOf('https') >= 0) { p |= 16; }
  1829. }
  1830. if (p == 0) { p = 2; } // Desktop
  1831. // Sharing view only
  1832. var viewOnly = false;
  1833. if (args.viewonly) { viewOnly = true; }
  1834. // User consent
  1835. var consent = 0;
  1836. if (args.consent == null) {
  1837. if ((p & 1) != 0) { consent = 0x0002; } // Terminal notify
  1838. if ((p & 2) != 0) { consent = 0x0001; } // Desktop notify
  1839. if ((p & 4) != 0) { consent = 0x0004; } // Files notify
  1840. } else {
  1841. if (typeof args.consent == 'string') {
  1842. var flagStrs = args.consent.split(',');
  1843. for (var i in flagStrs) {
  1844. var flagStr = flagStrs[i].toLowerCase();
  1845. if (flagStr == 'none') { consent = 0; }
  1846. else if (flagStr == 'notify') {
  1847. if ((p & 1) != 0) { consent |= 0x0002; } // Terminal notify
  1848. if ((p & 2) != 0) { consent |= 0x0001; } // Desktop notify
  1849. if ((p & 4) != 0) { consent |= 0x0004; } // Files notify
  1850. } else if (flagStr == 'prompt') {
  1851. if ((p & 1) != 0) { consent |= 0x0010; } // Terminal prompt
  1852. if ((p & 2) != 0) { consent |= 0x0008; } // Desktop prompt
  1853. if ((p & 4) != 0) { consent |= 0x0020; } // Files prompt
  1854. } else if (flagStr == 'bar') {
  1855. if ((p & 2) != 0) { consent |= 0x0040; } // Desktop toolbar
  1856. } else { console.log("Unknown consent type."); process.exit(1); return; }
  1857. }
  1858. }
  1859. }
  1860. var port = null;
  1861. // Set Port Number if http or https
  1862. if ((p & 8) || (p & 16)) {
  1863. if (typeof args.port == 'number') {
  1864. if ((args.port < 1) || (args.port > 65535)) { console.log("Port number must be between 1 and 65535."); process.exit(1); }
  1865. port = args.port;
  1866. } else if ((p & 8)) {
  1867. port = 80;
  1868. } else if ((p & 16)) {
  1869. port = 443;
  1870. }
  1871. }
  1872. // Start and end time
  1873. var start = null, end = null;
  1874. if (args.start) { start = Math.floor(Date.parse(args.start) / 1000); end = start + (60 * 60); }
  1875. if (args.end) { if (start == null) { start = Math.floor(Date.now() / 1000) } end = Math.floor(Date.parse(args.end) / 1000); if (end <= start) { console.log("End time must be ahead of start time."); process.exit(1); return; } }
  1876. if (args.duration) { if (start == null) { start = Math.floor(Date.now() / 1000) } end = start + parseInt(args.duration * 60); }
  1877. // Recurring
  1878. var recurring = 0;
  1879. if (args.daily) { recurring = 1; } else if (args.weekly) { recurring = 2; }
  1880. if (recurring > 0) {
  1881. if (args.end != null) { console.log("End time can't be specified for recurring shares, use --duration only."); process.exit(1); return; }
  1882. if (args.duration == null) { args.duration = 60; } else { args.duration = parseInt(args.duration); }
  1883. if (start == null) { start = Math.floor(Date.now() / 1000) }
  1884. if ((typeof args.duration != 'number') || (args.duration < 1)) { console.log("Invalid duration value."); process.exit(1); return; }
  1885. // Recurring sharing
  1886. ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, expire: args.duration, recurring: recurring, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
  1887. } else {
  1888. if ((start == null) && (end == null)) {
  1889. // Unlimited sharing
  1890. ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, expire: 0, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
  1891. } else {
  1892. // Time limited sharing
  1893. ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, end: end, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
  1894. }
  1895. }
  1896. } else if (args.remove) {
  1897. ws.send(JSON.stringify({ action: 'removeDeviceShare', nodeid: args.id, publicid: args.remove, responseid: 'meshctrl' }));
  1898. } else {
  1899. ws.send(JSON.stringify({ action: 'deviceShares', nodeid: args.id, responseid: 'meshctrl' }));
  1900. }
  1901. break;
  1902. }
  1903. case 'deviceopenurl': {
  1904. ws.send(JSON.stringify({ action: 'msg', type: 'openUrl', nodeid: args.id, url: args.openurl, responseid: 'meshctrl' }));
  1905. break;
  1906. }
  1907. case 'devicemessage': {
  1908. ws.send(JSON.stringify({ action: 'msg', type: 'messagebox', nodeid: args.id, title: args.title ? args.title : "MeshCentral", msg: args.msg, timeout: args.timeout ? args.timeout : 120000, responseid: 'meshctrl' }));
  1909. break;
  1910. }
  1911. case 'devicetoast': {
  1912. ws.send(JSON.stringify({ action: 'toast', nodeids: [args.id], title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
  1913. break;
  1914. }
  1915. case 'groupmessage': {
  1916. ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
  1917. break;
  1918. }
  1919. case 'grouptoast': {
  1920. ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
  1921. break;
  1922. }
  1923. case 'report': {
  1924. var reporttype = 1;
  1925. switch(args.type) {
  1926. case 'traffic':
  1927. reporttype = 2;
  1928. break;
  1929. case 'logins':
  1930. reporttype = 3;
  1931. break;
  1932. case 'db':
  1933. reporttype = 4;
  1934. break;
  1935. }
  1936. var reportgroupby = 1;
  1937. if(args.groupby){
  1938. reportgroupby = args.groupby === 'device' ? 2 : args.groupby === 'day' ? 3: 1;
  1939. }
  1940. var start = null, end = null;
  1941. if (args.start) {
  1942. start = Math.floor(Date.parse(args.start) / 1000);
  1943. } else {
  1944. start = reportgroupby === 3 ? Math.round(new Date().getTime() / 1000) - (168 * 3600) : Math.round(new Date().getTime() / 1000) - (24 * 3600);
  1945. }
  1946. if (args.end) {
  1947. end = Math.floor(Date.parse(args.end) / 1000);
  1948. } else {
  1949. end = Math.round(new Date().getTime() / 1000);
  1950. }
  1951. if (end <= start) { console.log("End time must be ahead of start time."); process.exit(1); return; }
  1952. ws.send(JSON.stringify({ action: 'report', type: reporttype, groupBy: reportgroupby, devGroup: args.devicegroup || null, start, end, tz: Intl.DateTimeFormat().resolvedOptions().timeZone, tf: new Date().getTimezoneOffset(), showTraffic: args.hasOwnProperty('showtraffic'), l: 'en', responseid: 'meshctrl' }));
  1953. break;
  1954. }
  1955. }
  1956. });
  1957. function getSiteAdminRights(args) {
  1958. var siteadmin = -1;
  1959. if (typeof args.rights == 'number') {
  1960. siteadmin = args.rights;
  1961. } else if (typeof args.rights == 'string') {
  1962. siteadmin = 0;
  1963. var srights = args.rights.toLowerCase().split(',');
  1964. if (srights.indexOf('full') != -1) { siteadmin = 0xFFFFFFFF; }
  1965. if (srights.indexOf('none') != -1) { siteadmin = 0x00000000; }
  1966. if (srights.indexOf('backup') != -1 || srights.indexOf('serverbackup') != -1) { siteadmin |= 0x00000001; }
  1967. if (srights.indexOf('manageusers') != -1) { siteadmin |= 0x00000002; }
  1968. if (srights.indexOf('restore') != -1 || srights.indexOf('serverrestore') != -1) { siteadmin |= 0x00000004; }
  1969. if (srights.indexOf('fileaccess') != -1) { siteadmin |= 0x00000008; }
  1970. if (srights.indexOf('update') != -1 || srights.indexOf('serverupdate') != -1) { siteadmin |= 0x00000010; }
  1971. if (srights.indexOf('locked') != -1) { siteadmin |= 0x00000020; }
  1972. if (srights.indexOf('nonewgroups') != -1) { siteadmin |= 0x00000040; }
  1973. if (srights.indexOf('notools') != -1) { siteadmin |= 0x00000080; }
  1974. if (srights.indexOf('usergroups') != -1) { siteadmin |= 0x00000100; }
  1975. if (srights.indexOf('recordings') != -1) { siteadmin |= 0x00000200; }
  1976. if (srights.indexOf('locksettings') != -1) { siteadmin |= 0x00000400; }
  1977. if (srights.indexOf('allevents') != -1) { siteadmin |= 0x00000800; }
  1978. if (srights.indexOf('nonewdevices') != -1) { siteadmin |= 0x00001000; }
  1979. }
  1980. if (args.siteadmin) { siteadmin = 0xFFFFFFFF; }
  1981. if (args.manageusers) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 2; }
  1982. if (args.fileaccess) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 8; }
  1983. if (args.serverupdate) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 16; }
  1984. if (args.locked) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 32; }
  1985. if (args.nonewgroups) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 64; }
  1986. if (args.notools) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 128; }
  1987. return siteadmin;
  1988. }
  1989. ws.on('close', function () { process.exit(); });
  1990. ws.on('error', function (err) {
  1991. if (err.code == 'ENOTFOUND') { console.log('Unable to resolve ' + url); }
  1992. else if (err.code == 'ECONNREFUSED') { console.log('Unable to connect to ' + url); }
  1993. else { console.log('Unable to connect to ' + url); }
  1994. process.exit();
  1995. });
  1996. ws.on('message', function incoming(rawdata) {
  1997. var data = null;
  1998. try { data = JSON.parse(rawdata); } catch (ex) { }
  1999. if (data == null) { console.log('Unable to parse data: ' + rawdata); }
  2000. if (settings.cmd == 'showevents') {
  2001. if (args.filter == null) {
  2002. // Display all events
  2003. console.log(JSON.stringify(data, null, 2));
  2004. } else {
  2005. // Display select events
  2006. var filters = args.filter.split(',');
  2007. if (typeof data.event == 'object') {
  2008. if (filters.indexOf(data.event.action) >= 0) { console.log(JSON.stringify(data, null, 2) + '\r\n'); }
  2009. } else {
  2010. if (filters.indexOf(data.action) >= 0) { console.log(JSON.stringify(data, null, 2) + '\r\n'); }
  2011. }
  2012. }
  2013. return;
  2014. }
  2015. switch (data.action) {
  2016. case 'serverinfo': { // SERVERINFO
  2017. settings.currentDomain = data.serverinfo.domain;
  2018. if (settings.cmd == 'serverinfo') {
  2019. if (args.json) {
  2020. console.log(JSON.stringify(data.serverinfo, ' ', 2));
  2021. } else {
  2022. for (var i in data.serverinfo) { console.log(i + ':', data.serverinfo[i]); }
  2023. }
  2024. process.exit();
  2025. }
  2026. break;
  2027. }
  2028. case 'events': {
  2029. if (settings.cmd == 'listevents') {
  2030. if (args.raw) {
  2031. // RAW JSON
  2032. console.log(JSON.stringify(data.events));
  2033. } else if (args.json) {
  2034. // Formatted JSON
  2035. console.log(JSON.stringify(data.events, null, 2));
  2036. } else {
  2037. if ((args.id == null) && (args.userid == null)) {
  2038. // CSV format
  2039. console.log("time,type,action,nodeid,userid,msg");
  2040. for (var i in data.events) {
  2041. var x = [];
  2042. x.push(data.events[i].time);
  2043. x.push(data.events[i].etype);
  2044. x.push(data.events[i].action);
  2045. x.push(data.events[i].nodeid);
  2046. x.push(data.events[i].userid);
  2047. x.push(data.events[i].msg);
  2048. console.log(csvFormatArray(x));
  2049. }
  2050. } else if (args.id != null) {
  2051. // CSV format
  2052. console.log("time,type,action,userid,msg");
  2053. for (var i in data.events) {
  2054. var x = [];
  2055. x.push(data.events[i].time);
  2056. x.push(data.events[i].etype);
  2057. x.push(data.events[i].action);
  2058. x.push(data.events[i].userid);
  2059. x.push(data.events[i].msg);
  2060. console.log(csvFormatArray(x));
  2061. }
  2062. } else if (args.userid != null) {
  2063. // CSV format
  2064. console.log("time,type,action,nodeid,msg");
  2065. for (var i in data.events) {
  2066. var x = [];
  2067. x.push(data.events[i].time);
  2068. x.push(data.events[i].etype);
  2069. x.push(data.events[i].action);
  2070. x.push(data.events[i].nodeid);
  2071. x.push(data.events[i].msg);
  2072. console.log(csvFormatArray(x));
  2073. }
  2074. }
  2075. }
  2076. process.exit();
  2077. }
  2078. break;
  2079. }
  2080. case 'authcookie': { // SHELL, UPLOAD, DOWNLOAD
  2081. if ((settings.cmd == 'shell') || (settings.cmd == 'upload') || (settings.cmd == 'download')) {
  2082. var protocol = 1; // Terminal
  2083. if ((settings.cmd == 'upload') || (settings.cmd == 'download')) { protocol = 5; } // Files
  2084. if ((args.id.split('/') != 3) && (settings.currentDomain != null)) { args.id = 'node/' + settings.currentDomain + '/' + args.id; }
  2085. var id = getRandomHex(6);
  2086. ws.send(JSON.stringify({ action: 'msg', nodeid: args.id, type: 'tunnel', usage: 1, value: '*/meshrelay.ashx?p=' + protocol + '&nodeid=' + args.id + '&id=' + id + '&rauth=' + data.rcookie, responseid: 'meshctrl' }));
  2087. connectTunnel(url.replace('/control.ashx', '/meshrelay.ashx?browser=1&p=' + protocol + '&nodeid=' + encodeURIComponent(args.id) + '&id=' + id + '&auth=' + data.cookie));
  2088. }
  2089. break;
  2090. }
  2091. case 'deviceShares': { // DEVICESHARING
  2092. if (data.result != null) {
  2093. console.log(data.result);
  2094. } else {
  2095. if ((data.deviceShares == null) || (data.deviceShares.length == 0)) {
  2096. console.log('No device sharing links for this device.');
  2097. } else {
  2098. if (args.json) {
  2099. console.log(data.deviceShares);
  2100. } else {
  2101. for (var i in data.deviceShares) {
  2102. var share = data.deviceShares[i];
  2103. var shareType = [];
  2104. if ((share.p & 1) != 0) { shareType.push("Terminal"); }
  2105. if ((share.p & 2) != 0) { if (share.viewOnly) { shareType.push("View Only Desktop"); } else { shareType.push("Desktop"); } }
  2106. if ((share.p & 4) != 0) { shareType.push("Files"); }
  2107. shareType = shareType.join(' + ');
  2108. if (shareType == '') { shareType = "Unknown"; }
  2109. var consent = [];
  2110. if ((share.consent & 0x0001) != 0) { consent.push("Desktop Notify"); }
  2111. if ((share.consent & 0x0008) != 0) { consent.push("Desktop Prompt"); }
  2112. if ((share.consent & 0x0040) != 0) { consent.push("Desktop Connection Toolbar"); }
  2113. if ((share.consent & 0x0002) != 0) { consent.push("Terminal Notify"); }
  2114. if ((share.consent & 0x0010) != 0) { consent.push("Terminal Prompt"); }
  2115. if ((share.consent & 0x0004) != 0) { consent.push("Files Notify"); }
  2116. if ((share.consent & 0x0020) != 0) { consent.push("Files Prompt"); }
  2117. console.log('----------');
  2118. console.log('Identifier: ' + share.publicid);
  2119. console.log('Type: ' + shareType);
  2120. console.log('UserId: ' + share.userid);
  2121. console.log('Guest Name: ' + share.guestName);
  2122. console.log('User Consent: ' + consent.join(', '));
  2123. if (share.startTime) { console.log('Start Time: ' + new Date(share.startTime).toLocaleString()); }
  2124. if (share.expireTime) { console.log('Expire Time: ' + new Date(share.expireTime).toLocaleString()); }
  2125. if (share.duration) { console.log('Duration: ' + share.duration + ' minute' + ((share.duration > 1) ? 's' : '')); }
  2126. if (share.recurring == 1) { console.log('Recurring: ' + 'Daily'); }
  2127. if (share.recurring == 2) { console.log('Recurring: ' + 'Weekly'); }
  2128. console.log('URL: ' + share.url);
  2129. }
  2130. }
  2131. }
  2132. }
  2133. process.exit();
  2134. break;
  2135. }
  2136. case 'userinfo': { // USERINFO
  2137. if (settings.cmd == 'userinfo') {
  2138. if (args.json) {
  2139. console.log(JSON.stringify(data.userinfo, ' ', 2));
  2140. } else {
  2141. for (var i in data.userinfo) { console.log(i + ':', data.userinfo[i]); }
  2142. }
  2143. process.exit();
  2144. }
  2145. break;
  2146. }
  2147. case 'getsysinfo': { // DEVICEINFO
  2148. if (settings.cmd == 'deviceinfo') {
  2149. settings.sysinfo = (data.result) ? null : data;
  2150. if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
  2151. }
  2152. break;
  2153. }
  2154. case 'lastconnect': {
  2155. if (settings.cmd == 'deviceinfo') {
  2156. settings.lastconnect = (data.result) ? null : data;
  2157. if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
  2158. }
  2159. break;
  2160. }
  2161. case 'getnetworkinfo': {
  2162. if (settings.cmd == 'deviceinfo') {
  2163. settings.networking = (data.result) ? null : data;
  2164. if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
  2165. }
  2166. break;
  2167. }
  2168. case 'msg': // SHELL
  2169. case 'toast': // TOAST
  2170. case 'adduser': // ADDUSER
  2171. case 'edituser': // EDITUSER
  2172. case 'addamtdevice': // ADDAMTDEVICE
  2173. case 'addlocaldevice': // ADDLOCALDEVICE
  2174. case 'removedevices': // REMOVEDEVICE
  2175. case 'changedevice': // EDITDEVICE
  2176. case 'deleteuser': // REMOVEUSER
  2177. case 'createmesh': // ADDDEVICEGROUP
  2178. case 'deletemesh': // REMOVEDEVICEGROUP
  2179. case 'editmesh': // EDITDEVICEGROUP
  2180. case 'wakedevices':
  2181. case 'changeDeviceMesh':
  2182. case 'addmeshuser': //
  2183. case 'removemeshuser': //
  2184. case 'wakedevices': //
  2185. case 'inviteAgent': //
  2186. case 'adddeviceuser': //
  2187. case 'createusergroup': //
  2188. case 'deleteusergroup': //
  2189. case 'runcommands':
  2190. case 'poweraction':
  2191. case 'addusertousergroup':
  2192. case 'removeuserfromusergroup':
  2193. case 'removeDeviceShare':
  2194. case 'userbroadcast': { // BROADCAST
  2195. if (((settings.cmd == 'shell') || (settings.cmd == 'upload') || (settings.cmd == 'download')) && (data.result == 'OK')) return;
  2196. if ((data.type == 'runcommands') && (settings.cmd != 'runcommand')) return;
  2197. if ((settings.multiresponse != null) && (settings.multiresponse > 1)) { settings.multiresponse--; break; }
  2198. if (data.responseid == 'meshctrl') {
  2199. if (data.meshid) { console.log(data.result, data.meshid); }
  2200. else if (data.userid) { console.log(data.result, data.userid); }
  2201. else console.log(data.result);
  2202. process.exit();
  2203. }
  2204. break;
  2205. }
  2206. case 'createDeviceShareLink':
  2207. case 'webrelay':
  2208. if (data.result == 'OK') {
  2209. if (data.publicid) { console.log('ID: ' + data.publicid); }
  2210. console.log('URL: ' + data.url);
  2211. } else {
  2212. console.log(data.result);
  2213. }
  2214. process.exit();
  2215. break;
  2216. case 'createInviteLink':
  2217. if (data.responseid == 'meshctrl') {
  2218. if (data.url) { console.log(data.url); }
  2219. else console.log(data.result);
  2220. process.exit();
  2221. }
  2222. break;
  2223. case 'wssessioncount': { // LIST USER SESSIONS
  2224. if (args.json) {
  2225. console.log(JSON.stringify(data.wssessions, ' ', 2));
  2226. } else {
  2227. for (var i in data.wssessions) { console.log(i + ', ' + ((data.wssessions[i] > 1) ? (data.wssessions[i] + ' sessions.') : ("1 session."))); }
  2228. }
  2229. process.exit();
  2230. break;
  2231. }
  2232. case 'usergroups': { // LIST USER GROUPS
  2233. if (settings.cmd == 'listusergroups') {
  2234. if (args.json) {
  2235. console.log(JSON.stringify(data.ugroups, ' ', 2));
  2236. } else {
  2237. for (var i in data.ugroups) {
  2238. var x = i + ', ' + data.ugroups[i].name;
  2239. if (data.ugroups[i].desc && (data.ugroups[i].desc != '')) { x += ', ' + data.ugroups[i].desc; }
  2240. console.log(x);
  2241. var mesh = [], user = [], node = [];
  2242. if (data.ugroups[i].links != null) { for (var j in data.ugroups[i].links) { if (j.startsWith('mesh/')) { mesh.push(j); } if (j.startsWith('user/')) { user.push(j); } if (j.startsWith('node/')) { node.push(j); } } }
  2243. console.log(' Users:');
  2244. if (user.length > 0) { for (var j in user) { console.log(' ' + user[j]); } } else { console.log(' (None)'); }
  2245. console.log(' Device Groups:');
  2246. if (mesh.length > 0) { for (var j in mesh) { console.log(' ' + mesh[j] + ', ' + data.ugroups[i].links[mesh[j]].rights); } } else { console.log(' (None)'); }
  2247. console.log(' Devices:');
  2248. if (node.length > 0) { for (var j in node) { console.log(' ' + node[j] + ', ' + data.ugroups[i].links[node[j]].rights); } } else { console.log(' (None)'); }
  2249. }
  2250. }
  2251. process.exit();
  2252. } else if (settings.cmd == 'removeallusersfromusergroup') {
  2253. var ugrpid = args.groupid, exit = false;
  2254. if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
  2255. var ugroup = data.ugroups[ugrpid];
  2256. if (ugroup == null) {
  2257. console.log('User group not found.');
  2258. exit = true;
  2259. } else {
  2260. var usercount = 0;
  2261. if (ugroup.links) {
  2262. for (var i in ugroup.links) {
  2263. if (i.startsWith('user/')) {
  2264. usercount++;
  2265. ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: i, responseid: 'meshctrl' }));
  2266. console.log('Removing ' + i);
  2267. }
  2268. }
  2269. }
  2270. if (usercount == 0) { console.log('No users in this user group.'); exit = true; } else { settings.multiresponse = usercount; }
  2271. }
  2272. if (exit) { process.exit(); }
  2273. }
  2274. break;
  2275. }
  2276. case 'users': { // LISTUSERS
  2277. if (data.result) { console.log(data.result); process.exit(); return; }
  2278. if (args.filter) {
  2279. // Filter the list of users
  2280. var filters = args.filter.toLowerCase().split(',');
  2281. var filteredusers = [];
  2282. for (var i in data.users) {
  2283. var ok = false;
  2284. if ((filters.indexOf('2fa') >= 0) && ((data.users[i].otphkeys != null) || (data.users[i].otpkeys != null) || (data.users[i].otpsecret != null))) { ok = true; }
  2285. if ((filters.indexOf('no2fa') >= 0) && ((data.users[i].otphkeys == null) && (data.users[i].otpkeys == null) && (data.users[i].otpsecret == null))) { ok = true; }
  2286. if (ok == true) { filteredusers.push(data.users[i]); }
  2287. }
  2288. data.users = filteredusers;
  2289. }
  2290. if (args.json) {
  2291. console.log(JSON.stringify(data.users, ' ', 2));
  2292. } else {
  2293. if (args.idexists) { for (var i in data.users) { const u = data.users[i]; if ((u._id == args.idexists) || (u._id.split('/')[2] == args.idexists)) { console.log('1'); process.exit(); return; } } console.log('0'); process.exit(); return; }
  2294. if (args.nameexists) { for (var i in data.users) { const u = data.users[i]; if (u.name == args.nameexists) { console.log(u._id); process.exit(); return; } } process.exit(); return; }
  2295. console.log('id, name, email\r\n---------------');
  2296. for (var i in data.users) {
  2297. const u = data.users[i];
  2298. var t = "\"" + u._id.split('/')[2] + "\", \"" + u.name + "\"";
  2299. if (u.email != null) { t += ", \"" + u.email + "\""; }
  2300. console.log(t);
  2301. }
  2302. }
  2303. process.exit();
  2304. break;
  2305. }
  2306. case 'nodes': {
  2307. if (settings.cmd == 'deviceinfo') {
  2308. settings.nodes = (data.result) ? null : data;
  2309. if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
  2310. }
  2311. if ((settings.cmd == 'listdevices') && (data.responseid == 'meshctrl')) {
  2312. if ((data.result != null) && (data.result != 'ok')) {
  2313. console.log(data.result);
  2314. } else {
  2315. // Filter devices based on device id.
  2316. if (args.filterid) {
  2317. var filteridSplit = args.filterid.split(','), filters = [];
  2318. for (var i in filteridSplit) {
  2319. var f = filteridSplit[i].trim();
  2320. var g = f.split('/'); // If there is any / in the id, just grab the last part.
  2321. if (g.length > 0) { f = g[g.length - 1]; }
  2322. if (f != '') { filters.push(f); }
  2323. }
  2324. if (filters.length > 0) {
  2325. for (var mid in data.nodes) {
  2326. var filteredNodes = [];
  2327. for (var nid in data.nodes[mid]) {
  2328. var n = data.nodes[mid][nid], match = false;
  2329. for (var f in filters) { if (n._id.indexOf(filters[f]) >= 0) { match = true; } }
  2330. if (match) { filteredNodes.push(n); }
  2331. }
  2332. data.nodes[mid] = filteredNodes;
  2333. }
  2334. }
  2335. }
  2336. // Filter devices based on filter string
  2337. if (args.filter != null) {
  2338. for (var meshid in data.nodes) {
  2339. for (var d in data.nodes[meshid]) { data.nodes[meshid][d].meshid = meshid; }
  2340. data.nodes[meshid] = parseSearchOrInput(data.nodes[meshid], args.filter.toString().toLowerCase());
  2341. }
  2342. }
  2343. if (args.csv) {
  2344. // Return a flat list
  2345. var nodecount = 0;
  2346. for (var i in data.nodes) {
  2347. var devicesInMesh = data.nodes[i];
  2348. for (var j in devicesInMesh) {
  2349. var n = devicesInMesh[j];
  2350. nodecount++;
  2351. if (settings.xmeshes && settings.xmeshes[i]) {
  2352. console.log('\"' + settings.xmeshes[i]._id.split('/')[2] + '\",\"' + settings.xmeshes[i].name.split('\"').join('') + '\",\"' + n._id.split('/')[2] + '\",\"' + n.name.split('\"').join('') + '\",' + (n.icon ? n.icon : 0) + ',' + (n.conn ? n.conn : 0) + ',' + (n.pwr ? n.pwr : 0));
  2353. } else {
  2354. console.log('\"\",\"\",\"' + n._id.split('/')[2] + '\",\"' + n.name.split('\"').join('') + '\",' + (n.icon ? n.icon : 0) + ',' + (n.conn ? n.conn : 0) + ',' + (n.pwr ? n.pwr : 0));
  2355. }
  2356. }
  2357. }
  2358. if (nodecount == 0) { console.log('None'); }
  2359. } else if (args.count) {
  2360. // Return how many devices are in this group
  2361. var nodes = [];
  2362. for (var i in data.nodes) { var devicesInMesh = data.nodes[i]; for (var j in devicesInMesh) { nodes.push(devicesInMesh[j]); } }
  2363. console.log(nodes.length);
  2364. } else if (args.json) {
  2365. // Return all devices in JSON format
  2366. var nodes = [];
  2367. for (var i in data.nodes) {
  2368. const devicesInMesh = data.nodes[i];
  2369. for (var j in devicesInMesh) {
  2370. devicesInMesh[j].meshid = i; // Add device group id
  2371. if (settings.xmeshes && settings.xmeshes[i] && settings.xmeshes[i].name) { devicesInMesh[j].groupname = settings.xmeshes[i].name; } // Add device group name
  2372. nodes.push(devicesInMesh[j]);
  2373. }
  2374. }
  2375. console.log(JSON.stringify(nodes, ' ', 2));
  2376. } else {
  2377. // Display the list of nodes in text format
  2378. var nodecount = 0;
  2379. for (var i in data.nodes) {
  2380. var devicesInMesh = data.nodes[i];
  2381. if (devicesInMesh.length > 0) {
  2382. if (settings.xmeshes && settings.xmeshes[i] && settings.xmeshes[i].name) { console.log('\r\nDevice group: \"' + settings.xmeshes[i].name.split('\"').join('') + '\"'); }
  2383. console.log('id, name, icon, conn, pwr\r\n-------------------------');
  2384. for (var j in devicesInMesh) {
  2385. var n = devicesInMesh[j];
  2386. nodecount++;
  2387. console.log('\"' + n._id.split('/')[2] + '\", \"' + n.name.split('\"').join('') + '\", ' + (n.icon ? n.icon : 0) + ', ' + (n.conn ? n.conn : 0) + ', ' + (n.pwr ? n.pwr : 0));
  2388. }
  2389. }
  2390. }
  2391. if (nodecount == 0) { console.log('None'); }
  2392. }
  2393. }
  2394. process.exit();
  2395. }
  2396. if ((settings.cmd == 'groupmessage') && (data.responseid == 'meshctrl')) {
  2397. if ((data.nodes != null)) {
  2398. for (var i in data.nodes) {
  2399. for (let index = 0; index < data.nodes[i].length; index++) {
  2400. const element = data.nodes[i][index];
  2401. ws.send(JSON.stringify({ action: 'msg', type: 'messagebox', nodeid: element._id, title: args.title ? args.title : "MeshCentral", msg: args.msg, timeout: args.timeout ? args.timeout : 120000 }));
  2402. }
  2403. }
  2404. }
  2405. setTimeout(function(){ console.log('ok'); process.exit(); }, 1000);
  2406. }
  2407. if ((settings.cmd == 'grouptoast') && (data.responseid == 'meshctrl')) {
  2408. if (data.nodes != null) {
  2409. for (var i in data.nodes) {
  2410. var nodes = [];
  2411. for (let index = 0; index < data.nodes[i].length; index++) {
  2412. const element = data.nodes[i][index];
  2413. nodes.push(element._id);
  2414. }
  2415. ws.send(JSON.stringify({ action: 'toast', nodeids: nodes, title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
  2416. }
  2417. }
  2418. }
  2419. break;
  2420. }
  2421. case 'meshes': { // LISTDEVICEGROUPS
  2422. if (settings.cmd == 'listdevices') {
  2423. // Store the list of device groups for later use
  2424. settings.xmeshes = {}
  2425. for (var i in data.meshes) { settings.xmeshes[data.meshes[i]._id] = data.meshes[i]; }
  2426. } else if (settings.cmd == 'listdevicegroups') {
  2427. if (args.json) {
  2428. // If asked, add the MeshID hex encoding to the JSON.
  2429. if (args.hex) { for (var i in data.meshes) { data.meshes[i]._idhex = '0x' + Buffer.from(data.meshes[i]._id.split('/')[2].replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase(); } }
  2430. console.log(JSON.stringify(data.meshes, ' ', 2));
  2431. } else {
  2432. if (args.idexists) { for (var i in data.meshes) { const u = data.meshes[i]; if ((u._id == args.idexists) || (u._id.split('/')[2] == args.idexists)) { console.log('1'); process.exit(); return; } } console.log('0'); process.exit(); return; }
  2433. if (args.nameexists) { for (var i in data.meshes) { const u = data.meshes[i]; if (u.name == args.nameexists) { console.log(u._id); process.exit(); return; } } process.exit(); return; }
  2434. console.log('id, name\r\n---------------');
  2435. for (var i in data.meshes) {
  2436. const m = data.meshes[i];
  2437. var mid = m._id.split('/')[2];
  2438. if (args.hex) { mid = '0x' + Buffer.from(mid.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase(); }
  2439. var t = "\"" + mid + "\", \"" + m.name + "\"";
  2440. console.log(t);
  2441. }
  2442. }
  2443. process.exit();
  2444. } else if (settings.cmd == 'listusersofdevicegroup') {
  2445. for (var i in data.meshes) {
  2446. const m = data.meshes[i];
  2447. var mid = m._id.split('/')[2];
  2448. if (mid == args.id) {
  2449. if (args.json) {
  2450. console.log(JSON.stringify(m.links, ' ', 2));
  2451. } else {
  2452. console.log('userid, rights\r\n---------------');
  2453. for (var l in m.links) {
  2454. var rights = m.links[l].rights;
  2455. var rightsstr = [];
  2456. if (rights == 4294967295) { rightsstr = ['FullAdministrator']; } else {
  2457. if (rights & 1) { rightsstr.push('EditMesh'); }
  2458. if (rights & 2) { rightsstr.push('ManageUsers'); }
  2459. if (rights & 4) { rightsstr.push('ManageComputers'); }
  2460. if (rights & 8) { rightsstr.push('RemoteControl'); }
  2461. if (rights & 16) { rightsstr.push('AgentConsole'); }
  2462. if (rights & 32) { rightsstr.push('ServerFiles'); }
  2463. if (rights & 64) { rightsstr.push('WakeDevice'); }
  2464. if (rights & 128) { rightsstr.push('SetNotes'); }
  2465. if (rights & 256) { rightsstr.push('RemoteViewOnly'); }
  2466. if (rights & 512) { rightsstr.push('NoTerminal'); }
  2467. if (rights & 1024) { rightsstr.push('NoFiles'); }
  2468. if (rights & 2048) { rightsstr.push('NoAMT'); }
  2469. if (rights & 4096) { rightsstr.push('DesktopLimitedInput'); }
  2470. }
  2471. console.log(l.split('/')[2] + ', ' + rightsstr.join(', '));
  2472. }
  2473. }
  2474. process.exit();
  2475. return;
  2476. }
  2477. }
  2478. console.log('Group id not found');
  2479. process.exit();
  2480. }
  2481. break;
  2482. }
  2483. case 'close': {
  2484. if (data.cause == 'noauth') {
  2485. if (data.msg == 'tokenrequired') {
  2486. console.log('Authentication token required, use --token [number].');
  2487. } else if (data.msg == 'nokey') {
  2488. console.log('URL key is invalid or missing, please specify ?key=xxx in url');
  2489. } else {
  2490. if ((args.loginkeyfile != null) || (args.loginkey != null)) {
  2491. console.log('Invalid login, check the login key and that this computer has the correct time.');
  2492. } else {
  2493. console.log('Invalid login.');
  2494. }
  2495. }
  2496. }
  2497. process.exit();
  2498. break;
  2499. }
  2500. case 'createLoginToken': {
  2501. if (data.result != null) {
  2502. console.log(data.result);
  2503. process.exit();
  2504. } else {
  2505. if (args.json) {
  2506. console.log(data);
  2507. } else {
  2508. console.log("New login token created.");
  2509. if (data.name) { console.log("Token name: " + data.name); }
  2510. if (data.created) { console.log("Created: " + new Date(data.created).toLocaleString()); }
  2511. if (data.expire) { console.log("Expire: " + new Date(data.expire).toLocaleString()); }
  2512. if (data.tokenUser) { console.log("Username: " + data.tokenUser); }
  2513. if (data.tokenPass) { console.log("Password: " + data.tokenPass); }
  2514. }
  2515. }
  2516. process.exit();
  2517. break;
  2518. }
  2519. case 'loginTokens': {
  2520. if (args.json) {
  2521. console.log(data.loginTokens);
  2522. } else {
  2523. console.log("Name Username Expire");
  2524. console.log("-------------------------------------------------------------------------------------");
  2525. if (data.loginTokens.length == 0) {
  2526. console.log("No login tokens");
  2527. } else {
  2528. for (var i in data.loginTokens) {
  2529. var t = data.loginTokens[i];
  2530. var e = (t.expire == 0) ? "Unlimited" : new Date(t.expire).toLocaleString();
  2531. console.log(padString(t.name, 28) + padString(t.tokenUser, 28) + e);
  2532. }
  2533. }
  2534. }
  2535. process.exit();
  2536. break;
  2537. }
  2538. case 'getDeviceDetails': {
  2539. console.log(data.data);
  2540. process.exit();
  2541. }
  2542. case 'report': {
  2543. console.log('group,' + data.data.columns.flatMap(c => c.id).join(','));
  2544. Object.keys(data.data.groups).forEach(gk => {
  2545. data.data.groups[gk].entries.forEach(e => {
  2546. console.log(gk + ',' + Object.values(e).join(','));
  2547. });
  2548. });
  2549. process.exit();
  2550. }
  2551. default: { break; }
  2552. }
  2553. //console.log('Data', data);
  2554. //setTimeout(function timeout() { ws.send(Date.now()); }, 500);
  2555. });
  2556. }
  2557. // String padding function
  2558. function padString(str, pad) {
  2559. var xpad = ' ';
  2560. if (str.length >= pad) return str; return str + xpad.substring(0, pad - str.length)
  2561. }
  2562. function parseSearchAndInput(nodes, x) {
  2563. var s = x.split(' ' + "and" + ' '), r = null;
  2564. for (var i in s) {
  2565. var r2 = getDevicesThatMatchFilter(nodes, s[i]);
  2566. if (r == null) { r = r2; } else { var r3 = []; for (var j in r2) { if (r.indexOf(r2[j]) >= 0) { r3.push(r2[j]); } } r = r3; }
  2567. }
  2568. return r;
  2569. }
  2570. function parseSearchOrInput(nodes, x) {
  2571. var s = x.split(' ' + "or" + ' '), r = null;
  2572. for (var i in s) { var r2 = parseSearchAndInput(nodes, s[i]); if (r == null) { r = r2; } else { for (var j in r2) { if (r.indexOf(r2[j] >= 0)) { r.push(r2[j]); } } } }
  2573. return r;
  2574. }
  2575. function getDevicesThatMatchFilter(nodes, x) {
  2576. var r = [];
  2577. var userSearch = null, ipSearch = null, groupSearch = null, tagSearch = null, agentTagSearch = null, wscSearch = null, osSearch = null, amtSearch = null, descSearch = null;
  2578. if (x.startsWith("user:".toLowerCase())) { userSearch = x.substring("user:".length); }
  2579. else if (x.startsWith("u:".toLowerCase())) { userSearch = x.substring("u:".length); }
  2580. else if (x.startsWith("ip:".toLowerCase())) { ipSearch = x.substring("ip:".length); }
  2581. else if (x.startsWith("group:".toLowerCase())) { groupSearch = x.substring("group:".length); }
  2582. else if (x.startsWith("g:".toLowerCase())) { groupSearch = x.substring("g:".length); }
  2583. else if (x.startsWith("tag:".toLowerCase())) { tagSearch = x.substring("tag:".length); }
  2584. else if (x.startsWith("t:".toLowerCase())) { tagSearch = x.substring("t:".length); }
  2585. else if (x.startsWith("atag:".toLowerCase())) { agentTagSearch = x.substring("atag:".length); }
  2586. else if (x.startsWith("a:".toLowerCase())) { agentTagSearch = x.substring("a:".length); }
  2587. else if (x.startsWith("os:".toLowerCase())) { osSearch = x.substring("os:".length); }
  2588. else if (x.startsWith("amt:".toLowerCase())) { amtSearch = x.substring("amt:".length); }
  2589. else if (x.startsWith("desc:".toLowerCase())) { descSearch = x.substring("desc:".length); }
  2590. else if (x == 'wsc:ok') { wscSearch = 1; }
  2591. else if (x == 'wsc:noav') { wscSearch = 2; }
  2592. else if (x == 'wsc:noupdate') { wscSearch = 3; }
  2593. else if (x == 'wsc:nofirewall') { wscSearch = 4; }
  2594. else if (x == 'wsc:any') { wscSearch = 5; }
  2595. if (x == '') {
  2596. // No search
  2597. for (var d in nodes) { r.push(nodes[d]); }
  2598. } else if (ipSearch != null) {
  2599. // IP address search
  2600. for (var d in nodes) { if ((nodes[d].ip != null) && (nodes[d].ip.indexOf(ipSearch) >= 0)) { r.push(nodes[d]); } }
  2601. } else if (groupSearch != null) {
  2602. // Group filter
  2603. if (settings.xmeshes) { for (var d in nodes) { if (settings.xmeshes[nodes[d].meshid] && settings.xmeshes[nodes[d].meshid].name.toLowerCase().indexOf(groupSearch) >= 0) { r.push(nodes[d]); } } }
  2604. } else if (tagSearch != null) {
  2605. // Tag filter
  2606. for (var d in nodes) {
  2607. if ((nodes[d].tags == null) && (tagSearch == '')) { r.push(nodes[d]); }
  2608. else if (nodes[d].tags != null) { for (var j in nodes[d].tags) { if (nodes[d].tags[j].toLowerCase() == tagSearch) { r.push(nodes[d]); break; } } }
  2609. }
  2610. } else if (agentTagSearch != null) {
  2611. // Agent Tag filter
  2612. for (var d in nodes) {
  2613. if ((((nodes[d].agent != null) && (nodes[d].agent.tag == null)) && (agentTagSearch == '')) || ((nodes[d].agent != null) && (nodes[d].agent.tag != null) && (nodes[d].agent.tag.toLowerCase().indexOf(agentTagSearch) >= 0))) { r.push(nodes[d]); };
  2614. }
  2615. } else if (userSearch != null) {
  2616. // User search
  2617. for (var d in nodes) {
  2618. if (nodes[d].users && nodes[d].users.length > 0) { for (var i in nodes[d].users) { if (nodes[d].users[i].toLowerCase().indexOf(userSearch) >= 0) { r.push(nodes[d]); } } }
  2619. }
  2620. } else if (osSearch != null) {
  2621. // OS search
  2622. for (var d in nodes) { if ((nodes[d].osdesc != null) && (nodes[d].osdesc.toLowerCase().indexOf(osSearch) >= 0)) { r.push(nodes[d]); }; }
  2623. } else if (amtSearch != null) {
  2624. // Intel AMT search
  2625. for (var d in nodes) { if ((nodes[d].intelamt != null) && ((amtSearch == '') || (nodes[d].intelamt.state == amtSearch))) { r.push(nodes[d]); } }
  2626. } else if (descSearch != null) {
  2627. // Device description search
  2628. for (var d in nodes) { if ((nodes[d].desc != null) && (nodes[d].desc != '') && ((descSearch == '') || (nodes[d].desc.toLowerCase().indexOf(descSearch) >= 0))) { r.push(nodes[d]); } }
  2629. } else if (wscSearch != null) {
  2630. // Windows Security Center
  2631. for (var d in nodes) {
  2632. if (nodes[d].wsc) {
  2633. if ((wscSearch == 1) && (nodes[d].wsc.antiVirus == 'OK') && (nodes[d].wsc.autoUpdate == 'OK') && (nodes[d].wsc.firewall == 'OK')) { r.push(nodes[d]); }
  2634. else if (((wscSearch == 2) || (wscSearch == 5)) && (nodes[d].wsc.antiVirus != 'OK')) { r.push(nodes[d]); }
  2635. else if (((wscSearch == 3) || (wscSearch == 5)) && (nodes[d].wsc.autoUpdate != 'OK')) { r.push(nodes[d]); }
  2636. else if (((wscSearch == 4) || (wscSearch == 5)) && (nodes[d].wsc.firewall != 'OK')) { r.push(nodes[d]); }
  2637. }
  2638. }
  2639. } else if (x == '*') {
  2640. // Star filter
  2641. for (var d in nodes) { if (stars[nodes[d]._id] == 1) { r.push(nodes[d]); } }
  2642. } else {
  2643. // Device name search
  2644. try {
  2645. var rs = x.split(/\s+/).join('|'), rx = new RegExp(rs); // In some cases (like +), this can throw an exception.
  2646. for (var d in nodes) {
  2647. //if (showRealNames) {
  2648. //if (nodes[d].rnamel != null && rx.test(nodes[d].rnamel.toLowerCase())) { r.push(nodes[d]); }
  2649. //} else {
  2650. if (rx.test(nodes[d].name.toLowerCase())) { r.push(nodes[d]); }
  2651. //}
  2652. }
  2653. } catch (ex) { for (var d in nodes) { r.push(nodes[d]); } }
  2654. }
  2655. return r;
  2656. }
  2657. // Connect tunnel to a remote agent
  2658. function connectTunnel(url) {
  2659. // Setup WebSocket options
  2660. var options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
  2661. // Setup the HTTP proxy if needed
  2662. if (args.proxy != null) { const HttpsProxyAgent = require('https-proxy-agent'); options.agent = new HttpsProxyAgent(require('url').parse(args.proxy)); }
  2663. // Connect the WebSocket
  2664. console.log('Connecting...');
  2665. const WebSocket = require('ws');
  2666. settings.tunnelwsstate = 0;
  2667. settings.tunnelws = new WebSocket(url, options);
  2668. settings.tunnelws.on('open', function () { console.log('Waiting for Agent...'); }); // Wait for agent connection
  2669. settings.tunnelws.on('close', function () { console.log('Connection Closed.'); process.exit(); });
  2670. settings.tunnelws.on('error', function (err) { console.log(err); process.exit(); });
  2671. if (settings.cmd == 'shell') {
  2672. // This code does all of the work for a shell command
  2673. settings.tunnelws.on('message', function (rawdata) {
  2674. var data = rawdata.toString();
  2675. if (settings.tunnelwsstate == 1) {
  2676. // If the incoming text looks exactly like a control command, ignore it.
  2677. if ((typeof data == 'string') && (data.startsWith('{"ctrlChannel":"102938","type":"'))) {
  2678. var ctrlCmd = null;
  2679. try { ctrlCmd = JSON.parse(data); } catch (ex) { }
  2680. if ((ctrlCmd != null) && (ctrlCmd.ctrlChannel == '102938') && (ctrlCmd.type != null)) return; // This is a control command, like ping/pong. Ignore it.
  2681. }
  2682. process.stdout.write(data);
  2683. } else if (settings.tunnelwsstate == 0) {
  2684. if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
  2685. // Send terminal size
  2686. var termSize = null;
  2687. if (typeof process.stdout.getWindowSize == 'function') { termSize = process.stdout.getWindowSize(); }
  2688. if (termSize != null) { settings.tunnelws.send(JSON.stringify({ ctrlChannel: '102938', type: 'options', cols: termSize[0], rows: termSize[1] })); }
  2689. settings.tunnelwsstate = 1;
  2690. settings.tunnelws.send('1'); // Terminal
  2691. process.stdin.setEncoding('utf8');
  2692. process.stdin.setRawMode(true);
  2693. process.stdout.setEncoding('utf8');
  2694. process.stdin.unpipe(process.stdout);
  2695. process.stdout.unpipe(process.stdin);
  2696. process.stdin.on('data', function (data) { settings.tunnelws.send(Buffer.from(data)); });
  2697. //process.stdin.on('readable', function () { var chunk; while ((chunk = process.stdin.read()) !== null) { settings.tunnelws.send(Buffer.from(chunk)); } });
  2698. process.stdin.on('end', function () { process.exit(); });
  2699. process.stdout.on('resize', function () {
  2700. var termSize = null;
  2701. if (typeof process.stdout.getWindowSize == 'function') { termSize = process.stdout.getWindowSize(); }
  2702. if (termSize != null) { settings.tunnelws.send(JSON.stringify({ ctrlChannel: '102938', type: 'termsize', cols: termSize[0], rows: termSize[1] })); }
  2703. });
  2704. }
  2705. });
  2706. } else if (settings.cmd == 'upload') {
  2707. // This code does all of the work for a file upload
  2708. // node meshctrl upload --id oL4Y6Eg0qjnpHFrp1AxfxnBPenbDGnDSkC@HSOnAheIyd51pKhqSCUgJZakzwfKl --file readme.md --target c:\
  2709. settings.tunnelws.on('message', function (rawdata) {
  2710. if (settings.tunnelwsstate == 1) {
  2711. var cmd = null;
  2712. try { cmd = JSON.parse(rawdata.toString()); } catch (ex) { return; }
  2713. if (cmd.reqid == 'up') {
  2714. if ((cmd.action == 'uploadack') || (cmd.action == 'uploadstart')) {
  2715. settings.inFlight--;
  2716. if (settings.uploadFile == null) { if (settings.inFlight == 0) { process.exit(); } return; } // If the file is closed and there is no more in-flight data, exit.
  2717. var loops = (cmd.action == 'uploadstart') ? 16 : 1; // If this is the first data to be sent, hot start now. We are going to have 16 blocks of data in-flight.
  2718. for (var i = 0; i < loops; i++) {
  2719. if (settings.uploadFile == null) continue;
  2720. var buf = Buffer.alloc(65565);
  2721. var len = require('fs').readSync(settings.uploadFile, buf, 1, 65564, settings.uploadPtr);
  2722. var start = 1;
  2723. settings.uploadPtr += len;
  2724. if (len > 0) {
  2725. if ((buf[1] == 0) || (buf[1] == 123)) { start = 0; buf[0] = 0; len++; } // If the buffer starts with 0 or 123, we must add an extra 0 at the start of the buffer
  2726. settings.inFlight++;
  2727. settings.tunnelws.send(buf.slice(start, start + len));
  2728. } else {
  2729. console.log('Upload done, ' + settings.uploadPtr + ' bytes sent.');
  2730. if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); delete settings.uploadFile; }
  2731. if (settings.inFlight == 0) { process.exit(); return; } // File is closed, if there is no more in-flight data, exit.
  2732. }
  2733. }
  2734. } else if (cmd.action == 'uploaderror') {
  2735. if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); }
  2736. console.log('Upload error.');
  2737. process.exit();
  2738. }
  2739. }
  2740. } else if (settings.tunnelwsstate == 0) {
  2741. var data = rawdata.toString();
  2742. if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
  2743. settings.tunnelwsstate = 1;
  2744. settings.tunnelws.send('5'); // Files
  2745. settings.uploadSize = require('fs').statSync(args.file).size;
  2746. settings.uploadFile = require('fs').openSync(args.file, 'r');
  2747. settings.uploadPtr = 0;
  2748. settings.inFlight = 1;
  2749. console.log('Uploading...');
  2750. settings.tunnelws.send(JSON.stringify({ action: 'upload', reqid: 'up', path: args.target, name: require('path').basename(args.file), size: settings.uploadSize }));
  2751. }
  2752. });
  2753. } else if (settings.cmd == 'download') {
  2754. // This code does all of the work for a file download
  2755. // node meshctrl download --id oL4Y6Eg0qjnpHFrp1AxfxnBPenbDGnDSkC@HSOnAheIyd51pKhqSCUgJZakzwfKl --file c:\temp\MC-8Languages.png --target c:\temp\bob.png
  2756. settings.tunnelws.on('message', function (rawdata) {
  2757. if (settings.tunnelwsstate == 1) {
  2758. if ((rawdata.length > 0) && (rawdata.toString()[0] != '{')) {
  2759. // This is binary data, this test is ok because 4 first bytes is a control value.
  2760. if ((rawdata.length > 4) && (settings.downloadFile != null)) { settings.downloadSize += (rawdata.length - 4); require('fs').writeSync(settings.downloadFile, rawdata, 4, rawdata.length - 4); }
  2761. if ((rawdata[3] & 1) != 0) { // Check end flag
  2762. // File is done, close everything.
  2763. if (settings.downloadFile != null) { require('fs').closeSync(settings.downloadFile); }
  2764. console.log('Download completed, ' + settings.downloadSize + ' bytes written.');
  2765. process.exit();
  2766. } else {
  2767. settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'ack', id: args.file })); // Send the ACK
  2768. }
  2769. } else {
  2770. // This is text data
  2771. var cmd = null;
  2772. try { cmd = JSON.parse(rawdata.toString()); } catch (ex) { return; }
  2773. if (cmd.action == 'download') {
  2774. if (cmd.id != args.file) return;
  2775. if (cmd.sub == 'start') {
  2776. if ((args.target.endsWith('\\')) || (args.target.endsWith('/'))) { args.target += path.parse(args.file).name; }
  2777. try { settings.downloadFile = require('fs').openSync(args.target, 'w'); } catch (ex) { console.log("Unable to create file: " + args.target); process.exit(); return; }
  2778. settings.downloadSize = 0;
  2779. settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'startack', id: args.file }));
  2780. console.log('Download started: ' + args.target);
  2781. } else if (cmd.sub == 'cancel') {
  2782. if (settings.downloadFile != null) { require('fs').closeSync(settings.downloadFile); }
  2783. console.log('Download canceled.');
  2784. process.exit();
  2785. }
  2786. }
  2787. }
  2788. } else if (settings.tunnelwsstate == 0) {
  2789. var data = rawdata.toString();
  2790. if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
  2791. settings.tunnelwsstate = 1;
  2792. settings.tunnelws.send('5'); // Files
  2793. settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'start', id: args.file, path: args.file }));
  2794. }
  2795. });
  2796. }
  2797. }
  2798. // Encode an object as a cookie using a key using AES-GCM. (key must be 32 bytes or more)
  2799. function encodeCookie(o, key) {
  2800. try {
  2801. if (key == null) { return null; }
  2802. o.time = Math.floor(Date.now() / 1000); // Add the cookie creation time
  2803. const iv = Buffer.from(crypto.randomBytes(12), 'binary'), cipher = crypto.createCipheriv('aes-256-gcm', key.slice(0, 32), iv);
  2804. const crypted = Buffer.concat([cipher.update(JSON.stringify(o), 'utf8'), cipher.final()]);
  2805. return Buffer.concat([iv, cipher.getAuthTag(), crypted]).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
  2806. } catch (e) { return null; }
  2807. }
  2808. // Generate a random Intel AMT password
  2809. function checkAmtPassword(p) { return (p.length > 7) && (/\d/.test(p)) && (/[a-z]/.test(p)) && (/[A-Z]/.test(p)) && (/\W/.test(p)); }
  2810. function getRandomAmtPassword() { var p; do { p = Buffer.from(crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); } while (checkAmtPassword(p) == false); return p; }
  2811. function getRandomHex(count) { return Buffer.from(crypto.randomBytes(count), 'binary').toString('hex'); }
  2812. function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
  2813. function winRemoveSingleQuotes(str) { if (process.platform != 'win32') return str; else return str.split('\'').join(''); }
  2814. function csvFormatArray(x) {
  2815. var y = [];
  2816. for (var i in x) { if ((x[i] == null) || (x[i] == '')) { y.push(''); } else { y.push('"' + x[i].split('"').join('') + '"'); } }
  2817. return y.join(',');
  2818. }
  2819. function displayDeviceInfo(sysinfo, lastconnect, network, nodes) {
  2820. //console.log('displayDeviceInfo', sysinfo, lastconnect, network, nodes);
  2821. // Fetch the node information
  2822. var node = null;;
  2823. if (sysinfo != null && (sysinfo.node != null)) {
  2824. // Node information came with system information
  2825. node = sysinfo.node;
  2826. } else {
  2827. // This device does not have system information, get node information from the nodes list.
  2828. for (var m in nodes.nodes) {
  2829. for (var n in nodes.nodes[m]) {
  2830. if (nodes.nodes[m][n]._id.indexOf(args.id) >= 0) { node = nodes.nodes[m][n]; }
  2831. }
  2832. }
  2833. }
  2834. if ((sysinfo == null && lastconnect == null && network == null) || (node == null)) {
  2835. console.log("Invalid device id");
  2836. process.exit(); return;
  2837. }
  2838. var info = {};
  2839. //if (network != null) { sysinfo.netif = network.netif; }
  2840. if (lastconnect != null) { node.lastconnect = lastconnect.time; node.lastaddr = lastconnect.addr; }
  2841. if (args.raw) { console.log(JSON.stringify(sysinfo, ' ', 2)); return; }
  2842. // General
  2843. var output = {}, outputCount = 0;
  2844. if (node.name) { output["Server Name"] = node.name; outputCount++; }
  2845. if (node.rname) { output["Computer Name"] = node.rname; outputCount++; }
  2846. if (node.host != null) { output["Hostname"] = node.host; outputCount++; }
  2847. if (node.ip != null) { output["IP Address"] = node.ip; outputCount++; }
  2848. if (node.desc != null) { output["Description"] = node.desc; outputCount++; }
  2849. if (node.icon != null) { output["Icon"] = node.icon; outputCount++; }
  2850. if (node.tags) { output["Tags"] = node.tags; outputCount++; }
  2851. if (node.av) {
  2852. var av = [];
  2853. for (var i in node.av) {
  2854. if (typeof node.av[i]['product'] == 'string') {
  2855. var n = node.av[i]['product'];
  2856. if (node.av[i]['updated'] === true) { n += ', updated'; }
  2857. if (node.av[i]['updated'] === false) { n += ', not updated'; }
  2858. if (node.av[i]['enabled'] === true) { n += ', enabled'; }
  2859. if (node.av[i]['enabled'] === false) { n += ', disabled'; }
  2860. av.push(n);
  2861. }
  2862. }
  2863. output["AntiVirus"] = av; outputCount++;
  2864. }
  2865. if (typeof node.wsc == 'object') {
  2866. output["WindowsSecurityCenter"] = node.wsc; outputCount++;
  2867. }
  2868. if (outputCount > 0) { info["General"] = output; }
  2869. // Operating System
  2870. var hardware = null;
  2871. if ((sysinfo != null) && (sysinfo.hardware != null)) { hardware = sysinfo.hardware; }
  2872. if ((hardware && hardware.windows && hardware.windows.osinfo) || node.osdesc) {
  2873. var output = {}, outputCount = 0;
  2874. if (node.rname) { output["Name"] = node.rname; outputCount++; }
  2875. if (node.osdesc) { output["Version"] = node.osdesc; outputCount++; }
  2876. if (hardware && hardware.windows && hardware.windows.osinfo) { var m = hardware.windows.osinfo; if (m.OSArchitecture) { output["Architecture"] = m.OSArchitecture; outputCount++; } }
  2877. if (outputCount > 0) { info["Operating System"] = output; }
  2878. }
  2879. // MeshAgent
  2880. if (node.agent) {
  2881. var output = {}, outputCount = 0;
  2882. var agentsStr = ["Unknown", "Windows 32bit console", "Windows 64bit console", "Windows 32bit service", "Windows 64bit service", "Linux 32bit", "Linux 64bit", "MIPS", "XENx86", "Android", "Linux ARM", "macOS x86-32bit", "Android x86", "PogoPlug ARM", "Android", "Linux Poky x86-32bit", "macOS x86-64bit", "ChromeOS", "Linux Poky x86-64bit", "Linux NoKVM x86-32bit", "Linux NoKVM x86-64bit", "Windows MinCore console", "Windows MinCore service", "NodeJS", "ARM-Linaro", "ARMv6l / ARMv7l", "ARMv8 64bit", "ARMv6l / ARMv7l / NoKVM", "MIPS24KC (OpenWRT)", "Apple Silicon", "FreeBSD x86-64", "Unknown", "Linux ARM 64 bit (glibc/2.24 NOKVM)", "Alpine Linux x86 64 Bit (MUSL)", "Assistant (Windows)", "Armada370 - ARM32/HF (libc/2.26)", "OpenWRT x86-64", "OpenBSD x86-64", "Unknown", "Unknown", "MIPSEL24KC (OpenWRT)", "ARMADA/CORTEX-A53/MUSL (OpenWRT)", "Windows ARM 64bit console", "Windows ARM 64bit service", "ARMVIRT32 (OpenWRT)", "RISC-V x86-64"];
  2883. if ((node.agent != null) && (node.agent.id != null) && (node.agent.ver != null)) {
  2884. var str = '';
  2885. if (node.agent.id <= agentsStr.length) { str = agentsStr[node.agent.id]; } else { str = agentsStr[0]; }
  2886. if (node.agent.ver != 0) { str += ' v' + node.agent.ver; }
  2887. output["Mesh Agent"] = str; outputCount++;
  2888. }
  2889. if ((node.conn & 1) != 0) {
  2890. output["Last agent connection"] = "Connected now"; outputCount++;
  2891. } else {
  2892. if (node.lastconnect) { output["Last agent connection"] = new Date(node.lastconnect).toLocaleString(); outputCount++; }
  2893. }
  2894. if (node.lastaddr) {
  2895. var splitip = node.lastaddr.split(':');
  2896. if (splitip.length > 2) {
  2897. output["Last agent address"] = node.lastaddr; outputCount++; // IPv6
  2898. } else {
  2899. output["Last agent address"] = splitip[0]; outputCount++; // IPv4
  2900. }
  2901. }
  2902. if ((node.agent != null) && (node.agent.tag != null)) {
  2903. output["Tag"] = node.agent.tag; outputCount++;
  2904. }
  2905. if (outputCount > 0) { info["Mesh Agent"] = output; }
  2906. }
  2907. // Networking
  2908. if (network.netif != null) {
  2909. var output = {}, outputCount = 0, minfo = {};
  2910. for (var i in network.netif) {
  2911. var m = network.netif[i], moutput = {}, moutputCount = 0;
  2912. if (m.desc) { moutput["Description"] = m.desc; moutputCount++; }
  2913. if (m.mac) {
  2914. if (m.gatewaymac) {
  2915. moutput["MAC Layer"] = format("MAC: {0}, Gateway: {1}", m.mac, m.gatewaymac); moutputCount++;
  2916. } else {
  2917. moutput["MAC Layer"] = format("MAC: {0}", m.mac); moutputCount++;
  2918. }
  2919. }
  2920. if (m.v4addr && (m.v4addr != '0.0.0.0')) {
  2921. if (m.v4gateway && m.v4mask) {
  2922. moutput["IPv4 Layer"] = format("IP: {0}, Mask: {1}, Gateway: {2}", m.v4addr, m.v4mask, m.v4gateway); moutputCount++;
  2923. } else {
  2924. moutput["IPv4 Layer"] = format("IP: {0}", m.v4addr); moutputCount++;
  2925. }
  2926. }
  2927. if (moutputCount > 0) { minfo[m.name + (m.dnssuffix ? (', ' + m.dnssuffix) : '')] = moutput; info["Networking"] = minfo; }
  2928. }
  2929. }
  2930. if (network.netif2 != null) {
  2931. var minfo = {};
  2932. for (var i in network.netif2) {
  2933. var m = network.netif2[i], moutput = {}, moutputCount = 0;
  2934. if (Array.isArray(m) == false ||
  2935. m.length < 1 ||
  2936. m[0] == null ||
  2937. ((typeof m[0].mac == 'string') && (m[0].mac.startsWith('00:00:00:00')))
  2938. )
  2939. continue;
  2940. var ifTitle = '' + i;
  2941. if (m[0].fqdn != null && m[0].fqdn != '') ifTitle += ', ' + m[0].fqdn;
  2942. if (typeof m[0].mac == 'string') {
  2943. if (m[0].gatewaymac) {
  2944. moutput['MAC Layer'] = format("MAC: {0}, Gateway: {1}", m[0].mac, m[0].gatewaymac);
  2945. } else {
  2946. moutput['MAC Layer'] = format("MAC: {0}", m[0].mac);
  2947. }
  2948. moutputCount++;
  2949. }
  2950. moutput['IPv4 Layer'] = '';
  2951. moutput['IPv6 Layer'] = '';
  2952. for (var j = 0; j < m.length; j++) {
  2953. var iplayer = m[j];
  2954. if (iplayer.family == 'IPv4' || iplayer.family == 'IPv6') {
  2955. if (iplayer.gateway && iplayer.netmask) {
  2956. moutput[iplayer.family + ' Layer'] += format("IP: {0}, Mask: {1}, Gateway: {2} ", iplayer.address, iplayer.netmask, iplayer.gateway);
  2957. moutputCount++;
  2958. } else {
  2959. if (iplayer.address) {
  2960. moutput[iplayer.family + ' Layer'] += format("IP: {0} ", iplayer.address);
  2961. moutputCount++;
  2962. }
  2963. }
  2964. }
  2965. }
  2966. if (moutput['IPv4 Layer'] == '') delete moutput['IPv4 Layer'];
  2967. if (moutput['IPv6 Layer'] == '') delete moutput['IPv6 Layer'];
  2968. if (moutputCount > 0) {
  2969. minfo[ifTitle] = moutput;
  2970. info["Networking"] = minfo;
  2971. }
  2972. }
  2973. }
  2974. // Intel AMT
  2975. if (node.intelamt != null) {
  2976. var output = {}, outputCount = 0;
  2977. output["Version"] = (node.intelamt.ver) ? ('v' + node.intelamt.ver) : ('<i>' + "Unknown" + '</i>'); outputCount++;
  2978. var provisioningStates = { 0: "Not Activated (Pre)", 1: "Not Activated (In)", 2: "Activated" };
  2979. var provisioningMode = '';
  2980. if ((node.intelamt.state == 2) && node.intelamt.flags) { if (node.intelamt.flags & 2) { provisioningMode = (', ' + "Client Control Mode (CCM)"); } else if (node.intelamt.flags & 4) { provisioningMode = (', ' + "Admin Control Mode (ACM)"); } }
  2981. output["Provisioning State"] = ((node.intelamt.state) ? (provisioningStates[node.intelamt.state]) : ('<i>' + "Unknown" + '</i>')) + provisioningMode; outputCount++;
  2982. output["Security"] = (node.intelamt.tls == 1) ? "Secured using TLS" : "TLS is not setup"; outputCount++;
  2983. output["Admin Credentials"] = (node.intelamt.user == null || node.intelamt.user == '') ? "Not Known" : "Known"; outputCount++;
  2984. if (outputCount > 0) { info["Intel Active Management Technology (Intel AMT)"] = output; }
  2985. }
  2986. if (hardware != null) {
  2987. if (hardware.identifiers) {
  2988. var output = {}, outputCount = 0, ident = hardware.identifiers;
  2989. // BIOS
  2990. if (ident.bios_vendor) { output["Vendor"] = ident.bios_vendor; outputCount++; }
  2991. if (ident.bios_version) { output["Version"] = ident.bios_version; outputCount++; }
  2992. if (outputCount > 0) { info["BIOS"] = output; }
  2993. output = {}, outputCount = 0;
  2994. // Motherboard
  2995. if (ident.board_vendor) { output["Vendor"] = ident.board_vendor; outputCount++; }
  2996. if (ident.board_name) { output["Name"] = ident.board_name; outputCount++; }
  2997. if (ident.board_serial && (ident.board_serial != '')) { output["Serial"] = ident.board_serial; outputCount++; }
  2998. if (ident.board_version) { output["Version"] = ident.board_version; }
  2999. if (ident.product_uuid) { output["Identifier"] = ident.product_uuid; }
  3000. if (ident.cpu_name) { output["CPU"] = ident.cpu_name; }
  3001. if (ident.gpu_name) { for (var i in ident.gpu_name) { output["GPU" + (parseInt(i) + 1)] = ident.gpu_name[i]; } }
  3002. if (outputCount > 0) { info["Motherboard"] = output; }
  3003. }
  3004. // Memory
  3005. if (hardware.windows) {
  3006. if (hardware.windows.memory) {
  3007. var output = {}, outputCount = 0, minfo = {};
  3008. hardware.windows.memory.sort(function (a, b) { if (a.BankLabel > b.BankLabel) return 1; if (a.BankLabel < b.BankLabel) return -1; return 0; });
  3009. for (var i in hardware.windows.memory) {
  3010. var m = hardware.windows.memory[i], moutput = {}, moutputCount = 0;
  3011. if (m.Capacity) { moutput["Capacity/Speed"] = (m.Capacity / 1024 / 1024) + " Mb, " + m.Speed + " Mhz"; moutputCount++; }
  3012. if (m.PartNumber) { moutput["Part Number"] = ((m.Manufacturer && m.Manufacturer != 'Undefined') ? (m.Manufacturer + ', ') : '') + m.PartNumber; moutputCount++; }
  3013. if (moutputCount > 0) { minfo[m.BankLabel] = moutput; info["Memory"] = minfo; }
  3014. }
  3015. }
  3016. }
  3017. // Storage
  3018. if (hardware.identifiers && ident.storage_devices) {
  3019. var output = {}, outputCount = 0, minfo = {};
  3020. // Sort Storage
  3021. ident.storage_devices.sort(function (a, b) { if (a.Caption > b.Caption) return 1; if (a.Caption < b.Caption) return -1; return 0; });
  3022. for (var i in ident.storage_devices) {
  3023. var m = ident.storage_devices[i], moutput = {};
  3024. if (m.Size) {
  3025. if (m.Model && (m.Model != m.Caption)) { moutput["Model"] = m.Model; outputCount++; }
  3026. if ((typeof m.Size == 'string') && (parseInt(m.Size) == m.Size)) { m.Size = parseInt(m.Size); }
  3027. if (typeof m.Size == 'number') { moutput["Capacity"] = Math.floor(m.Size / 1024 / 1024) + 'Mb'; outputCount++; }
  3028. if (typeof m.Size == 'string') { moutput["Capacity"] = m.Size; outputCount++; }
  3029. if (moutputCount > 0) { minfo[m.Caption] = moutput; info["Storage"] = minfo; }
  3030. }
  3031. }
  3032. }
  3033. }
  3034. // Display everything
  3035. if (args.json) {
  3036. console.log(JSON.stringify(info, ' ', 2));
  3037. } else {
  3038. for (var i in info) {
  3039. console.log('--- ' + i + ' ---');
  3040. for (var j in info[i]) {
  3041. if ((typeof info[i][j] == 'string') || (typeof info[i][j] == 'number')) {
  3042. console.log(' ' + j + ': ' + info[i][j]);
  3043. } else {
  3044. console.log(' ' + j + ':');
  3045. for (var k in info[i][j]) {
  3046. console.log(' ' + k + ': ' + info[i][j][k]);
  3047. }
  3048. }
  3049. }
  3050. }
  3051. }
  3052. }
  3053. // Read the Mesh Agent error log and index it.
  3054. function indexAgentErrorLog() {
  3055. // Index the messages
  3056. const lines = require('fs').readFileSync('../meshcentral-data/agenterrorlogs.txt', { encoding: 'utf8', flag: 'r' }).split('\r\n');
  3057. var errorIndex = {}; // "msg" --> [ { lineNumber, elemenetNumber } ]
  3058. for (var i = 0; i < lines.length; i++) {
  3059. const line = lines[i];
  3060. if (line.length > 88) {
  3061. var nodeid = line.substring(0, 70);
  3062. var fetchTime = parseInt(line.substring(72, 85));
  3063. var data = JSON.parse(line.substring(87));
  3064. if ((data != null) && (data.action == 'errorlog') && (Array.isArray(data.log))) {
  3065. for (var j = 0; j < data.log.length; j++) {
  3066. var entry = data.log[j];
  3067. if ((entry != null) && (typeof entry.t == 'number') && (typeof entry.m == 'string')) {
  3068. const msg = entry.m;
  3069. if (errorIndex[msg] == null) { errorIndex[msg] = []; }
  3070. errorIndex[msg].push({ l: i, e: j });
  3071. }
  3072. }
  3073. }
  3074. }
  3075. }
  3076. // Sort the messages by frequency
  3077. var errorIndexCount = []; // [ { m: "msg", c: count } ]
  3078. for (var i in errorIndex) { errorIndexCount.push({ m: i, c: errorIndex[i].length }); }
  3079. errorIndexCount = errorIndexCount.sort(function (a, b) { return b.c - a.c })
  3080. // Display the results
  3081. for (var i = 0; i < errorIndexCount.length; i++) {
  3082. const m = errorIndexCount[i].m;
  3083. if ((m.indexOf('STUCK') >= 0) || (m.indexOf('FATAL') >= 0)) { console.log(errorIndexCount[i].c, m); }
  3084. }
  3085. }