2017-08-28 19:27:45 +03:00
/ *
2022-01-13 10:44:47 +03:00
Copyright 2018 - 2022 Intel Corporation
2017-08-28 19:27:45 +03:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2018-01-17 04:30:34 +03:00
http : //www.apache.org/licenses/LICENSE-2.0
2017-08-28 19:27:45 +03:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2021-01-25 08:51:22 +03:00
process . on ( 'uncaughtException' , function ( ex ) {
2020-01-08 00:04:36 +03:00
require ( 'MeshAgent' ) . SendCommand ( { action : 'msg' , type : 'console' , value : "uncaughtException1: " + ex } ) ;
2018-09-20 21:45:12 +03:00
} ) ;
2021-04-24 20:53:13 +03:00
if ( process . platform == 'win32' && require ( 'user-sessions' ) . getDomain == null ) {
require ( 'user-sessions' ) . getDomain = function getDomain ( uid ) {
2021-04-19 20:55:09 +03:00
return ( this . getSessionAttribute ( uid , this . InfoClass . WTSDomainName ) ) ;
} ;
}
2018-09-20 21:45:12 +03:00
2022-08-26 06:11:47 +03:00
var promise = require ( 'promise' ) ;
2018-09-20 21:45:12 +03:00
2018-11-28 04:13:01 +03:00
// Mesh Rights
2019-06-15 02:33:53 +03:00
var MNG _ERROR = 65 ;
2018-12-30 02:24:33 +03:00
var MESHRIGHT _EDITMESH = 1 ;
var MESHRIGHT _MANAGEUSERS = 2 ;
var MESHRIGHT _MANAGECOMPUTERS = 4 ;
var MESHRIGHT _REMOTECONTROL = 8 ;
var MESHRIGHT _AGENTCONSOLE = 16 ;
var MESHRIGHT _SERVERFILES = 32 ;
var MESHRIGHT _WAKEDEVICE = 64 ;
var MESHRIGHT _SETNOTES = 128 ;
2021-07-18 08:16:57 +03:00
var MESHRIGHT _REMOTEVIEW = 256 ; // Remote View Only
2019-03-01 22:47:58 +03:00
var MESHRIGHT _NOTERMINAL = 512 ;
var MESHRIGHT _NOFILES = 1024 ;
var MESHRIGHT _NOAMT = 2048 ;
2019-03-10 01:28:08 +03:00
var MESHRIGHT _LIMITEDINPUT = 4096 ;
2020-03-07 01:06:33 +03:00
var MESHRIGHT _LIMITEVENTS = 8192 ;
var MESHRIGHT _CHATNOTIFY = 16384 ;
var MESHRIGHT _UNINSTALL = 32768 ;
var MESHRIGHT _NODESKTOP = 65536 ;
2021-01-15 10:57:52 +03:00
2022-08-28 21:32:54 +03:00
var pendingSetClip = false ; // This is a temporary hack to prevent multiple setclips at the same time to stop the agent from crashing.
2022-03-29 08:15:45 +03:00
2022-10-20 09:28:13 +03:00
//
// This is a helper function used by the 32 bit Windows Agent, when running on 64 bit windows. It will check if the agent is already patched for this
// and will use this helper if it is not. This helper will inject 'sysnative' into the results when calling readdirSync() on %windir%.
//
2022-10-14 21:18:49 +03:00
function _ _readdirSync _fix ( path )
{
var sysnative = false ;
pathstr = require ( 'fs' ) . _fixwinpath ( path ) ;
if ( pathstr . split ( '\\*' ) . join ( '' ) . toLowerCase ( ) == process . env [ 'windir' ] . toLowerCase ( ) ) { sysnative = true ; }
2022-10-24 23:26:29 +03:00
var ret = require ( 'fs' ) . _ _readdirSync _old ( path ) ;
2022-10-14 21:18:49 +03:00
if ( sysnative ) { ret . push ( 'sysnative' ) ; }
return ( ret ) ;
}
if ( process . platform == 'win32' && require ( '_GenericMarshal' ) . PointerSize == 4 && require ( 'os' ) . arch ( ) == 'x64' )
{
2022-10-20 09:28:13 +03:00
if ( require ( 'fs' ) . readdirSync . version == null )
2022-10-14 21:18:49 +03:00
{
2022-10-20 09:28:13 +03:00
//
// 32 Bit Windows Agent on 64 bit Windows has not been patched for sysnative issue, so lets use our own solution
//
2022-10-14 21:18:49 +03:00
require ( 'fs' ) . _ _readdirSync _old = require ( 'fs' ) . readdirSync ;
require ( 'fs' ) . readdirSync = _ _readdirSync _fix ;
}
}
2022-08-04 11:52:29 +03:00
function bcdOK ( ) {
2022-03-29 08:15:45 +03:00
if ( process . platform != 'win32' ) { return ( false ) ; }
2022-08-04 11:52:29 +03:00
if ( require ( 'os' ) . arch ( ) == 'x64' ) {
2022-03-29 08:15:45 +03:00
return ( require ( '_GenericMarshal' ) . PointerSize == 8 ) ;
}
return ( true ) ;
}
2022-08-04 11:52:29 +03:00
function getDomainInfo ( ) {
2022-06-06 02:50:39 +03:00
var hostname = require ( 'os' ) . hostname ( ) ;
var ret = { Name : hostname , Domain : "" } ;
2022-08-04 11:52:29 +03:00
switch ( process . platform ) {
2022-06-06 02:50:39 +03:00
case 'win32' :
2022-08-04 11:52:29 +03:00
try {
ret = require ( 'win-wmi' ) . query ( 'ROOT\\CIMV2' , 'SELECT * FROM Win32_ComputerSystem' , [ 'Name' , 'Domain' ] ) [ 0 ] ;
2022-06-06 02:50:39 +03:00
}
2022-08-04 11:52:29 +03:00
catch ( x ) {
2022-06-06 02:50:39 +03:00
}
break ;
case 'linux' :
var hasrealm = false ;
2022-08-04 11:52:29 +03:00
try {
2022-06-06 02:50:39 +03:00
hasrealm = require ( 'lib-finder' ) . hasBinary ( 'realm' ) ;
}
2022-08-04 11:52:29 +03:00
catch ( x ) {
2022-06-06 02:50:39 +03:00
}
2022-08-04 11:52:29 +03:00
if ( hasrealm ) {
2022-06-06 02:50:39 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stdin . write ( "realm list | grep domain-name: | tr '\\n' '`' | " ) ;
child . stdin . write ( "awk -F'`' '{ " ) ;
child . stdin . write ( ' printf("[");' ) ;
child . stdin . write ( ' ST="";' ) ;
child . stdin . write ( ' for(i=1;i<NF;++i)' ) ;
child . stdin . write ( ' {' ) ;
child . stdin . write ( ' match($i,/domain-name: /);' ) ;
child . stdin . write ( ' printf("%s\\"%s\\"", ST, substr($i, RSTART+RLENGTH));' ) ;
child . stdin . write ( ' ST=",";' ) ;
child . stdin . write ( ' }' ) ;
child . stdin . write ( ' printf("]");' ) ;
child . stdin . write ( " }'" ) ;
child . stdin . write ( '\nexit\n' ) ;
child . waitExit ( ) ;
var names = [ ] ;
2022-08-04 11:52:29 +03:00
try {
2022-06-06 02:50:39 +03:00
names = JSON . parse ( child . stdout . str ) ;
}
2022-08-04 11:52:29 +03:00
catch ( e ) {
2022-06-06 02:50:39 +03:00
}
2022-08-04 11:52:29 +03:00
while ( names . length > 0 ) {
if ( hostname . endsWith ( '.' + names . peek ( ) ) ) {
2022-06-06 02:50:39 +03:00
ret = { Name : hostname . substring ( 0 , hostname . length - names . peek ( ) . length - 1 ) , Domain : names . peek ( ) } ;
break ;
}
names . pop ( ) ;
}
}
break ;
}
return ( ret ) ;
}
2022-03-29 08:15:45 +03:00
2022-01-17 00:37:26 +03:00
try {
2021-08-16 22:57:17 +03:00
Object . defineProperty ( Array . prototype , 'findIndex' , {
2022-01-17 00:37:26 +03:00
value : function ( func ) {
2021-08-16 22:57:17 +03:00
var i = 0 ;
2022-01-17 00:37:26 +03:00
for ( i = 0 ; i < this . length ; ++ i ) {
if ( func ( this [ i ] , i , this ) ) {
2021-08-16 22:57:17 +03:00
return ( i ) ;
}
}
return ( - 1 ) ;
}
} ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-08-16 22:57:17 +03:00
2021-07-24 19:28:41 +03:00
if ( require ( 'MeshAgent' ) . ARCHID == null ) {
2021-01-14 12:18:28 +03:00
var id = null ;
2021-07-24 19:28:41 +03:00
switch ( process . platform ) {
2021-01-14 12:18:28 +03:00
case 'win32' :
id = require ( '_GenericMarshal' ) . PointerSize == 4 ? 3 : 4 ;
break ;
case 'freebsd' :
id = require ( '_GenericMarshal' ) . PointerSize == 4 ? 31 : 30 ;
break ;
case 'darwin' :
2021-07-24 19:28:41 +03:00
try {
2021-01-17 01:33:06 +03:00
id = require ( 'os' ) . arch ( ) == 'x64' ? 16 : 29 ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { id = 16 ; }
2021-05-16 00:53:35 +03:00
break ;
2021-01-14 12:18:28 +03:00
}
if ( id != null ) { Object . defineProperty ( require ( 'MeshAgent' ) , 'ARCHID' , { value : id } ) ; }
}
2022-01-13 10:44:47 +03:00
2022-01-17 00:37:26 +03:00
function setDefaultCoreTranslation ( obj , field , value ) {
2022-01-13 10:44:47 +03:00
if ( obj [ field ] == null || obj [ field ] == '' ) { obj [ field ] = value ; }
}
2022-01-17 00:37:26 +03:00
function getCoreTranslation ( ) {
2022-01-13 10:44:47 +03:00
var ret = { } ;
2022-01-17 00:37:26 +03:00
if ( global . coretranslations != null ) {
try {
var lang = require ( 'util-language' ) . current ;
2022-01-13 10:47:39 +03:00
if ( coretranslations [ lang ] == null ) { lang = lang . split ( '-' ) [ 0 ] ; }
if ( coretranslations [ lang ] == null ) { lang = 'en' ; }
if ( coretranslations [ lang ] != null ) { ret = coretranslations [ lang ] ; }
}
2022-01-14 01:38:28 +03:00
catch ( ex ) { }
2022-01-13 10:44:47 +03:00
}
setDefaultCoreTranslation ( ret , 'allow' , 'Allow' ) ;
setDefaultCoreTranslation ( ret , 'deny' , 'Deny' ) ;
setDefaultCoreTranslation ( ret , 'autoAllowForFive' , 'Auto accept all connections for next 5 minutes' ) ;
setDefaultCoreTranslation ( ret , 'terminalConsent' , '{0} requesting remote terminal access. Grant access?' ) ;
setDefaultCoreTranslation ( ret , 'desktopConsent' , '{0} requesting remote desktop access. Grant access?' ) ;
setDefaultCoreTranslation ( ret , 'fileConsent' , '{0} requesting remote file Access. Grant access?' ) ;
2022-01-15 02:54:12 +03:00
setDefaultCoreTranslation ( ret , 'terminalNotify' , '{0} started a remote terminal session.' ) ;
setDefaultCoreTranslation ( ret , 'desktopNotify' , '{0} started a remote desktop session.' ) ;
setDefaultCoreTranslation ( ret , 'fileNotify' , '{0} started a remote file session.' ) ;
setDefaultCoreTranslation ( ret , 'privacyBar' , 'Sharing desktop with: {0}' ) ;
2022-01-13 10:44:47 +03:00
return ( ret ) ;
}
var currentTranslation = getCoreTranslation ( ) ;
2022-08-04 11:52:29 +03:00
try {
2022-03-29 08:15:45 +03:00
require ( 'kvm-helper' ) ;
}
2022-08-04 11:52:29 +03:00
catch ( e ) {
2022-03-29 08:15:45 +03:00
var j =
{
2022-08-04 11:52:29 +03:00
users : function ( ) {
2022-03-29 08:15:45 +03:00
var r = { } ;
require ( 'user-sessions' ) . Current ( function ( c ) { r = c ; } ) ;
2022-08-04 11:52:29 +03:00
if ( process . platform != 'win32' ) {
for ( var i in r ) {
2022-03-29 08:15:45 +03:00
r [ i ] . SessionId = r [ i ] . uid ;
}
}
return ( r ) ;
}
} ;
addModuleObject ( 'kvm-helper' , j ) ;
}
2022-01-17 00:37:26 +03:00
function lockDesktop ( uid ) {
switch ( process . platform ) {
2021-09-03 10:55:00 +03:00
case 'linux' :
2022-01-17 00:37:26 +03:00
if ( uid != null ) {
2021-09-03 10:55:00 +03:00
var name = require ( 'user-sessions' ) . getUsername ( uid ) ;
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stderr . str = '' ; child . stderr . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stdin . write ( 'loginctl show-user -p Sessions ' + name + " | awk '{" ) ;
child . stdin . write ( 'gsub(/^Sessions=/,"",$0);' ) ;
child . stdin . write ( 'cmd = sprintf("loginctl lock-session %s",$0);' ) ;
child . stdin . write ( 'system(cmd);' ) ;
child . stdin . write ( "}'\nexit\n" ) ;
child . waitExit ( ) ;
}
2022-01-17 00:37:26 +03:00
else {
2021-09-03 10:55:00 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stderr . str = '' ; child . stderr . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stdin . write ( 'loginctl lock-sessions\nexit\n' ) ;
child . waitExit ( ) ;
}
break ;
case 'win32' :
{
var options = { type : 1 , uid : uid } ;
var child = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\system32\\cmd.exe' , [ '/c' , 'RunDll32.exe user32.dll,LockWorkStation' ] , options ) ;
child . waitExit ( ) ;
}
break ;
default :
break ;
}
}
var writable = require ( 'stream' ) . Writable ;
2022-01-17 00:37:26 +03:00
function destopLockHelper _pipe ( httprequest ) {
2021-09-03 10:55:00 +03:00
if ( process . platform != 'linux' && process . platform != 'freebsd' ) { return ; }
2022-01-17 00:37:26 +03:00
if ( httprequest . unlockerHelper == null && httprequest . desktop != null && httprequest . desktop . kvm != null ) {
2021-09-03 10:55:00 +03:00
httprequest . unlockerHelper = new writable (
{
2022-01-17 00:37:26 +03:00
'write' : function ( chunk , flush ) {
if ( chunk . readUInt16BE ( 0 ) == 65 ) {
delete this . request . autolock ;
}
flush ( ) ;
return ( true ) ;
} ,
'final' : function ( flush ) {
flush ( ) ;
2021-09-03 10:55:00 +03:00
}
2022-01-17 00:37:26 +03:00
} ) ;
2021-09-03 10:55:00 +03:00
httprequest . unlockerHelper . request = httprequest ;
httprequest . desktop . kvm . pipe ( httprequest . unlockerHelper ) ;
}
}
2021-08-20 00:10:03 +03:00
var obj = { serverInfo : { } } ;
2021-01-25 02:19:29 +03:00
var agentFileHttpRequests = { } ; // Currently active agent HTTPS GET requests from the server.
var agentFileHttpPendingRequests = [ ] ; // Pending HTTPS GET requests from the server.
2021-06-08 10:11:17 +03:00
var debugConsole = ( global . _MSH && ( _MSH ( ) . debugConsole == 1 ) ) ;
2021-01-05 20:47:47 +03:00
2022-02-15 06:33:36 +03:00
var color _options =
{
background : ( global . _MSH != null ) ? global . _MSH ( ) . background : '0,54,105' ,
foreground : ( global . _MSH != null ) ? global . _MSH ( ) . foreground : '255,255,255'
} ;
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' && require ( 'user-sessions' ) . isRoot ( ) ) {
2021-01-25 02:19:29 +03:00
// Check the Agent Uninstall MetaData for correctness, as the installer may have written an incorrect value
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var writtenSize = 0 , actualSize = Math . floor ( require ( 'fs' ) . statSync ( process . execPath ) . size / 1024 ) ;
2022-01-17 05:28:26 +03:00
try { writtenSize = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent' , 'EstimatedSize' ) ; } catch ( ex ) { }
if ( writtenSize != actualSize ) { try { require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent' , 'EstimatedSize' , actualSize ) ; } catch ( ex ) { } }
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
// Check to see if we are the Installed Mesh Agent Service, if we are, make sure we can run in Safe Mode
var svcname = process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
svcname = require ( 'MeshAgent' ) . serviceName ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var meshCheck = false ;
2022-01-17 05:28:26 +03:00
try { meshCheck = require ( 'service-manager' ) . manager . getService ( svcname ) . isMe ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
if ( meshCheck && require ( 'win-bcd' ) . isSafeModeService && ! require ( 'win-bcd' ) . isSafeModeService ( svcname ) ) { require ( 'win-bcd' ) . enableSafeModeService ( svcname ) ; }
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
2018-09-15 03:42:39 +03:00
2022-01-17 00:37:26 +03:00
if ( process . platform != 'win32' ) {
2021-08-11 01:00:12 +03:00
var ch = require ( 'child_process' ) ;
ch . _execFile = ch . execFile ;
2022-01-17 00:37:26 +03:00
ch . execFile = function execFile ( path , args , options ) {
if ( options && options . type && options . type == ch . SpawnTypes . TERM && options . env ) {
2021-08-11 01:00:12 +03:00
options . env [ 'TERM' ] = 'xterm-256color' ;
}
return ( this . _execFile ( path , args , options ) ) ;
} ;
}
2021-01-25 08:51:22 +03:00
if ( process . platform == 'darwin' && ! process . versions ) {
2021-01-25 02:19:29 +03:00
// This is an older MacOS Agent, so we'll need to check the service definition so that Auto-Update will function correctly
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ;
child . stdout . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stdin . write ( "cat /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist | tr '\n' '\.' | awk '{split($0, a, \"<key>KeepAlive</key>\"); split(a[2], b, \"<\"); split(b[2], c, \">\"); " ) ;
child . stdin . write ( " if(c[1]==\"dict\"){ split(a[2], d, \"</dict>\"); if(split(d[1], truval, \"<true/>\")>1) { split(truval[1], kn1, \"<key>\"); split(kn1[2], kn2, \"</key>\"); print kn2[1]; } }" ) ;
child . stdin . write ( " else { split(c[1], ka, \"/\"); if(ka[1]==\"true\") {print \"ALWAYS\";} } }'\nexit\n" ) ;
child . waitExit ( ) ;
2021-01-25 08:51:22 +03:00
if ( child . stdout . str . trim ( ) == 'Crashed' ) {
2021-01-25 02:19:29 +03:00
child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
2019-05-14 00:27:40 +03:00
child . stdout . str = '' ;
child . stdout . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
2021-01-25 02:19:29 +03:00
child . stdin . write ( "launchctl list | grep 'meshagent' | awk '{ if($3==\"meshagent\"){print $1;}}'\nexit\n" ) ;
2019-05-14 00:27:40 +03:00
child . waitExit ( ) ;
2021-01-25 02:19:29 +03:00
2021-01-25 08:51:22 +03:00
if ( parseInt ( child . stdout . str . trim ( ) ) == process . pid ) {
2021-01-25 02:19:29 +03:00
// The currently running MeshAgent is us, so we can continue with the update
var plist = require ( 'fs' ) . readFileSync ( '/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist' ) . toString ( ) ;
var tokens = plist . split ( '<key>KeepAlive</key>' ) ;
2021-01-25 08:51:22 +03:00
if ( tokens [ 1 ] . split ( '>' ) [ 0 ] . split ( '<' ) [ 1 ] == 'dict' ) {
2021-01-25 02:19:29 +03:00
var tmp = tokens [ 1 ] . split ( '</dict>' ) ;
tmp . shift ( ) ;
tokens [ 1 ] = '\n <true/>' + tmp . join ( '</dict>' ) ;
tokens = tokens . join ( '<key>KeepAlive</key>' ) ;
require ( 'fs' ) . writeFileSync ( '/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist' , tokens ) ;
var fix = '' ;
fix += ( "function macosRepair()\n" ) ;
fix += ( "{\n" ) ;
fix += ( " var child = require('child_process').execFile('/bin/sh', ['sh']);\n" ) ;
fix += ( " child.stdout.str = '';\n" ) ;
fix += ( " child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });\n" ) ;
fix += ( " child.stderr.on('data', function (chunk) { });\n" ) ;
fix += ( " child.stdin.write('launchctl unload /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n" ) ;
fix += ( " child.stdin.write('launchctl load /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n" ) ;
fix += ( " child.stdin.write('rm /Library/LaunchDaemons/meshagentRepair.plist\\n');\n" ) ;
fix += ( " child.stdin.write('rm " + process . cwd ( ) + "/macosRepair.js\\n');\n" ) ;
fix += ( " child.stdin.write('launchctl stop meshagentRepair\\nexit\\n');\n" ) ;
fix += ( " child.waitExit();\n" ) ;
fix += ( "}\n" ) ;
fix += ( "macosRepair();\n" ) ;
fix += ( "process.exit();\n" ) ;
require ( 'fs' ) . writeFileSync ( process . cwd ( ) + '/macosRepair.js' , fix ) ;
var plist = '<?xml version="1.0" encoding="UTF-8"?>\n' ;
plist += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n' ;
plist += '<plist version="1.0">\n' ;
plist += ' <dict>\n' ;
plist += ' <key>Label</key>\n' ;
plist += ( ' <string>meshagentRepair</string>\n' ) ;
plist += ' <key>ProgramArguments</key>\n' ;
plist += ' <array>\n' ;
plist += ( ' <string>' + process . execPath + '</string>\n' ) ;
plist += ' <string>macosRepair.js</string>\n' ;
plist += ' </array>\n' ;
plist += ' <key>WorkingDirectory</key>\n' ;
plist += ( ' <string>' + process . cwd ( ) + '</string>\n' ) ;
plist += ' <key>RunAtLoad</key>\n' ;
plist += ' <true/>\n' ;
plist += ' </dict>\n' ;
plist += '</plist>' ;
require ( 'fs' ) . writeFileSync ( '/Library/LaunchDaemons/meshagentRepair.plist' , plist ) ;
child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ;
child . stdout . on ( 'data' , function ( chunk ) { this . str += chunk . toString ( ) ; } ) ;
child . stdin . write ( "launchctl load /Library/LaunchDaemons/meshagentRepair.plist\nexit\n" ) ;
child . waitExit ( ) ;
2019-05-14 00:27:40 +03:00
}
}
}
2021-01-25 02:19:29 +03:00
}
2019-05-14 00:27:40 +03:00
2021-01-25 02:19:29 +03:00
// Add an Intel AMT event to the log
2021-01-25 08:51:22 +03:00
function addAmtEvent ( msg ) {
2021-01-25 02:19:29 +03:00
if ( obj . amtevents == null ) { obj . amtevents = [ ] ; }
2021-07-30 23:23:36 +03:00
var d = new Date ( ) , e = zeroPad ( d . getHours ( ) , 2 ) + ':' + zeroPad ( d . getMinutes ( ) , 2 ) + ':' + zeroPad ( d . getSeconds ( ) , 2 ) + ', ' + msg ;
obj . amtevents . push ( e ) ;
2021-01-25 02:19:29 +03:00
if ( obj . amtevents . length > 100 ) { obj . amtevents . splice ( 0 , obj . amtevents . length - 100 ) ; }
2021-07-30 23:23:36 +03:00
if ( obj . showamtevent ) { require ( 'MeshAgent' ) . SendCommand ( { action : 'msg' , type : 'console' , value : e } ) ; }
2021-01-25 02:19:29 +03:00
}
function zeroPad ( num , size ) { var s = '000000000' + num ; return s . substr ( s . length - size ) ; }
// Create Secure IPC for Diagnostic Agent Communications
obj . DAIPC = require ( 'net' ) . createServer ( ) ;
2022-01-17 05:28:26 +03:00
if ( process . platform != 'win32' ) { try { require ( 'fs' ) . unlinkSync ( process . cwd ( ) + '/DAIPC' ) ; } catch ( ex ) { } }
2021-01-25 02:19:29 +03:00
obj . DAIPC . IPCPATH = process . platform == 'win32' ? ( '\\\\.\\pipe\\' + require ( '_agentNodeId' ) ( ) + '-DAIPC' ) : ( process . cwd ( ) + '/DAIPC' ) ;
2022-01-17 05:28:26 +03:00
try { obj . DAIPC . listen ( { path : obj . DAIPC . IPCPATH , writableAll : true , maxConnections : 5 } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
obj . DAIPC . _daipc = [ ] ;
2021-01-25 08:51:22 +03:00
obj . DAIPC . on ( 'connection' , function ( c ) {
c . _send = function ( j ) {
2021-01-25 02:19:29 +03:00
var data = JSON . stringify ( j ) ;
var packet = Buffer . alloc ( data . length + 4 ) ;
packet . writeUInt32LE ( data . length + 4 , 0 ) ;
Buffer . from ( data ) . copy ( packet , 4 ) ;
this . write ( packet ) ;
} ;
this . _daipc . push ( c ) ;
c . parent = this ;
c . on ( 'end' , function ( ) { removeRegisteredApp ( this ) ; } ) ;
2021-01-25 08:51:22 +03:00
c . on ( 'data' , function ( chunk ) {
2021-01-25 02:19:29 +03:00
if ( chunk . length < 4 ) { this . unshift ( chunk ) ; return ; }
var len = chunk . readUInt32LE ( 0 ) ;
if ( len > 8192 ) { removeRegisteredApp ( this ) ; this . end ( ) ; return ; }
if ( chunk . length < len ) { this . unshift ( chunk ) ; return ; }
var data = chunk . slice ( 4 , len ) ;
2022-01-17 05:28:26 +03:00
try { data = JSON . parse ( data . toString ( ) ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
if ( ( data == null ) || ( typeof data . cmd != 'string' ) ) return ;
2021-01-25 08:51:22 +03:00
try {
switch ( data . cmd ) {
2021-01-25 02:19:29 +03:00
case 'requesthelp' :
if ( this . _registered == null ) return ;
sendConsoleText ( 'Request Help (' + this . _registered + '): ' + data . value ) ;
var help = { } ;
help [ this . _registered ] = data . value ;
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'help' , value : help } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 98 , [ this . _registered , data . value ] , "Help Requested, user: " + this . _registered + ", details: " + data . value , null ) ;
break ;
case 'cancelhelp' :
if ( this . _registered == null ) return ;
sendConsoleText ( 'Cancel Help (' + this . _registered + ')' ) ;
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'help' , value : { } } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
case 'register' :
2021-01-25 08:51:22 +03:00
if ( typeof data . value == 'string' ) {
2021-01-25 02:19:29 +03:00
this . _registered = data . value ;
var apps = { } ;
apps [ data . value ] = 1 ;
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'app' , value : apps } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
this . _send ( { cmd : 'serverstate' , value : meshServerConnectionState , url : require ( 'MeshAgent' ) . ConnectedServer , amt : ( amt != null ) } ) ;
}
break ;
case 'query' :
2021-01-25 08:51:22 +03:00
switch ( data . value ) {
2021-01-25 02:19:29 +03:00
case 'connection' :
data . result = require ( 'MeshAgent' ) . ConnectedServer ;
this . _send ( data ) ;
break ;
case 'descriptors' :
2021-01-25 08:51:22 +03:00
require ( 'ChainViewer' ) . getSnapshot ( ) . then ( function ( f ) {
2021-01-25 02:19:29 +03:00
this . tag . payload . result = f ;
this . tag . ipc . _send ( this . tag . payload ) ;
} ) . parentPromise . tag = { ipc : this , payload : data } ;
break ;
case 'timerinfo' :
data . result = require ( 'ChainViewer' ) . getTimerInfo ( ) ;
this . _send ( data ) ;
break ;
}
break ;
case 'amtstate' :
if ( amt == null ) return ;
var func = function amtStateFunc ( state ) { if ( state != null ) { amtStateFunc . pipe . _send ( { cmd : 'amtstate' , value : state } ) ; } }
func . pipe = this ;
amt . getMeiState ( 11 , func ) ;
break ;
case 'sessions' :
this . _send ( { cmd : 'sessions' , sessions : tunnelUserCount } ) ;
break ;
case 'meshToolInfo' :
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'meshToolInfo' , name : data . name , hash : data . hash , cookie : data . cookie ? true : false , pipe : true } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
2021-05-16 11:25:58 +03:00
case 'getUserImage' :
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'getUserImage' , userid : data . userid , pipe : true } ) ; } catch ( ex ) { }
2021-05-16 11:25:58 +03:00
break ;
2021-01-25 02:19:29 +03:00
case 'console' :
2021-01-25 08:51:22 +03:00
if ( debugConsole ) {
2021-01-25 02:19:29 +03:00
var args = splitArgs ( data . value ) ;
processConsoleCommand ( args [ 0 ] . toLowerCase ( ) , parseArgs ( args ) , 0 , 'pipe' ) ;
}
break ;
2019-06-15 02:33:53 +03:00
}
2021-01-25 02:19:29 +03:00
}
2022-01-17 05:28:26 +03:00
catch ( ex ) { removeRegisteredApp ( this ) ; this . end ( ) ; return ; }
2019-06-15 02:33:53 +03:00
} ) ;
2021-01-25 02:19:29 +03:00
} ) ;
2020-10-12 08:24:30 +03:00
2021-01-25 02:19:29 +03:00
// Send current sessions to registered apps
2021-07-24 19:28:41 +03:00
function broadcastSessionsToRegisteredApps ( x ) {
2021-06-08 10:11:17 +03:00
var p = { } , i ;
2021-07-24 19:28:41 +03:00
for ( i = 0 ; sendAgentMessage . messages != null && i < sendAgentMessage . messages . length ; ++ i ) {
2021-06-08 10:11:17 +03:00
p [ i ] = sendAgentMessage . messages [ i ] ;
}
tunnelUserCount . msg = p ;
2021-01-25 02:19:29 +03:00
broadcastToRegisteredApps ( { cmd : 'sessions' , sessions : tunnelUserCount } ) ;
2021-06-08 10:11:17 +03:00
tunnelUserCount . msg = { } ;
2021-01-25 02:19:29 +03:00
}
2020-10-12 08:24:30 +03:00
2021-01-25 02:19:29 +03:00
// Send this object to all registered local applications
2021-01-25 08:51:22 +03:00
function broadcastToRegisteredApps ( x ) {
2021-01-25 02:19:29 +03:00
if ( ( obj . DAIPC == null ) || ( obj . DAIPC . _daipc == null ) ) return ;
2021-05-16 11:25:58 +03:00
for ( var i in obj . DAIPC . _daipc ) {
if ( obj . DAIPC . _daipc [ i ] . _registered != null ) { obj . DAIPC . _daipc [ i ] . _send ( x ) ; }
}
2021-01-25 02:19:29 +03:00
}
2020-10-12 08:24:30 +03:00
2021-01-25 02:19:29 +03:00
// Send this object to a specific registered local applications
2021-01-25 08:51:22 +03:00
function sendToRegisteredApp ( appid , x ) {
2021-01-25 02:19:29 +03:00
if ( ( obj . DAIPC == null ) || ( obj . DAIPC . _daipc == null ) ) return ;
for ( var i in obj . DAIPC . _daipc ) { if ( obj . DAIPC . _daipc [ i ] . _registered == appid ) { obj . DAIPC . _daipc [ i ] . _send ( x ) ; } }
}
2020-11-28 08:40:30 +03:00
2021-01-25 02:19:29 +03:00
// Send list of registered apps to the server
2021-01-25 08:51:22 +03:00
function updateRegisteredAppsToServer ( ) {
2021-01-25 02:19:29 +03:00
if ( ( obj . DAIPC == null ) || ( obj . DAIPC . _daipc == null ) ) return ;
var apps = { } ;
for ( var i in obj . DAIPC . _daipc ) { if ( apps [ obj . DAIPC . _daipc [ i ] . _registered ] == null ) { apps [ obj . DAIPC . _daipc [ i ] . _registered ] = 1 ; } else { apps [ obj . DAIPC . _daipc [ i ] . _registered ] ++ ; } }
2022-01-17 00:37:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'app' , value : apps } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
// Remove a registered app
2021-01-25 08:51:22 +03:00
function removeRegisteredApp ( pipe ) {
2021-01-25 02:19:29 +03:00
for ( var i = obj . DAIPC . _daipc . length - 1 ; i >= 0 ; i -- ) { if ( obj . DAIPC . _daipc [ i ] === pipe ) { obj . DAIPC . _daipc . splice ( i , 1 ) ; } }
if ( pipe . _registered != null ) updateRegisteredAppsToServer ( ) ;
}
2020-10-12 08:24:30 +03:00
2021-01-25 08:51:22 +03:00
function diagnosticAgent _uninstall ( ) {
2021-01-25 02:19:29 +03:00
require ( 'service-manager' ) . manager . uninstallService ( 'meshagentDiagnostic' ) ;
2022-01-17 00:37:26 +03:00
require ( 'task-scheduler' ) . delete ( 'meshagentDiagnostic/periodicStart' ) ; // TODO: Using "delete" here breaks the minifier since this is a reserved keyword
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
function diagnosticAgent _installCheck ( install ) {
try {
2021-01-25 02:19:29 +03:00
var diag = require ( 'service-manager' ) . manager . getService ( 'meshagentDiagnostic' ) ;
return ( diag ) ;
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
if ( ! install ) { return null ; }
2020-10-12 08:24:30 +03:00
2021-01-25 02:19:29 +03:00
var svc = null ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
require ( 'service-manager' ) . manager . installService (
{
name : 'meshagentDiagnostic' ,
displayName : "Mesh Agent Diagnostic Service" ,
description : "Mesh Agent Diagnostic Service" ,
servicePath : process . execPath ,
parameters : [ '-recovery' ]
//files: [{ newName: 'diagnostic.js', _buffer: Buffer.from('LyoNCkNvcHlyaWdodCAyMDE5IEludGVsIENvcnBvcmF0aW9uDQoNCkxpY2Vuc2VkIHVuZGVyIHRoZSBBcGFjaGUgTGljZW5zZSwgVmVyc2lvbiAyLjAgKHRoZSAiTGljZW5zZSIpOw0KeW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLg0KWW91IG1heSBvYnRhaW4gYSBjb3B5IG9mIHRoZSBMaWNlbnNlIGF0DQoNCiAgICBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5TRS0yLjANCg0KVW5sZXNzIHJlcXVpcmVkIGJ5IGFwcGxpY2FibGUgbGF3IG9yIGFncmVlZCB0byBpbiB3cml0aW5nLCBzb2Z0d2FyZQ0KZGlzdHJpYnV0ZWQgdW5kZXIgdGhlIExpY2Vuc2UgaXMgZGlzdHJpYnV0ZWQgb24gYW4gIkFTIElTIiBCQVNJUywNCldJVEhPVVQgV0FSUkFOVElFUyBPUiBDT05ESVRJT05TIE9GIEFOWSBLSU5ELCBlaXRoZXIgZXhwcmVzcyBvciBpbXBsaWVkLg0KU2VlIHRoZSBMaWNlbnNlIGZvciB0aGUgc3BlY2lmaWMgbGFuZ3VhZ2UgZ292ZXJuaW5nIHBlcm1pc3Npb25zIGFuZA0KbGltaXRhdGlvbnMgdW5kZXIgdGhlIExpY2Vuc2UuDQoqLw0KDQp2YXIgaG9zdCA9IHJlcXVpcmUoJ3NlcnZpY2UtaG9zdCcpLmNyZWF0ZSgnbWVzaGFnZW50RGlhZ25vc3RpYycpOw0KdmFyIFJlY292ZXJ5QWdlbnQgPSByZXF1aXJlKCdNZXNoQWdlbnQnKTsNCg0KaG9zdC5vbignc2VydmljZVN0YXJ0JywgZnVuY3Rpb24gKCkNCnsNCiAgICBjb25zb2xlLnNldERlc3RpbmF0aW9uKGNvbnNvbGUuRGVzdGluYXRpb25zLkxPR0ZJTEUpOw0KICAgIGhvc3Quc3RvcCA9IGZ1bmN0aW9uKCkNCiAgICB7DQogICAgICAgIHJlcXVpcmUoJ3NlcnZpY2UtbWFuYWdlcicpLm1hbmFnZXIuZ2V0U2VydmljZSgnbWVzaGFnZW50RGlhZ25vc3RpYycpLnN0b3AoKTsNCiAgICB9DQogICAgUmVjb3ZlcnlBZ2VudC5vbignQ29ubmVjdGVkJywgZnVuY3Rpb24gKHN0YXR1cykNCiAgICB7DQogICAgICAgIGlmIChzdGF0dXMgPT0gMCkNCiAgICAgICAgew0KICAgICAgICAgICAgY29uc29sZS5sb2coJ0RpYWdub3N0aWMgQWdlbnQ6IFNlcnZlciBjb25uZWN0aW9uIGxvc3QuLi4nKTsNCiAgICAgICAgICAgIHJldHVybjsNCiAgICAgICAgfQ0KICAgICAgICBjb25zb2xlLmxvZygnRGlhZ25vc3RpYyBBZ2VudDogQ29ubmVjdGlvbiBFc3RhYmxpc2hlZCB3aXRoIFNlcnZlcicpOw0KICAgICAgICBzdGFydCgpOw0KICAgIH0pOw0KfSk7DQpob3N0Lm9uKCdub3JtYWxTdGFydCcsIGZ1bmN0aW9uICgpDQp7DQogICAgaG9zdC5zdG9wID0gZnVuY3Rpb24gKCkNCiAgICB7DQogICAgICAgIHByb2Nlc3MuZXhpdCgpOw0KICAgIH0NCiAgICBjb25zb2xlLmxvZygnTm9uIFNlcnZpY2UgTW9kZScpOw0KICAgIFJlY292ZXJ5QWdlbnQub24oJ0Nvbm5lY3RlZCcsIGZ1bmN0aW9uIChzdGF0dXMpDQogICAgew0KICAgICAgICBpZiAoc3RhdHVzID09IDApDQogICAgICAgIHsNCiAgICAgICAgICAgIGNvbnNvbGUubG9nKCdEaWFnbm9zdGljIEFnZW50OiBTZXJ2ZXIgY29ubmVjdGlvbiBsb3N0Li4uJyk7DQogICAgICAgICAgICByZXR1cm47DQogICAgICAgIH0NCiAgICAgICAgY29uc29sZS5sb2coJ0RpYWdub3N0aWMgQWdlbnQ6IENvbm5lY3Rpb24gRXN0YWJsaXNoZWQgd2l0aCBTZXJ2ZXInKTsNCiAgICAgICAgc3RhcnQoKTsNCiAgICB9KTsNCn0pOw0KaG9zdC5vbignc2VydmljZVN0b3AnLCBmdW5jdGlvbiAoKSB7IHByb2Nlc3MuZXhpdCgpOyB9KTsNCmhvc3QucnVuKCk7DQoNCg0KZnVuY3Rpb24gc3RhcnQoKQ0Kew0KDQp9Ow0K', 'base64') }]
} ) ;
svc = require ( 'service-manager' ) . manager . getService ( 'meshagentDiagnostic' ) ;
}
2022-01-17 00:37:26 +03:00
catch ( ex ) { return null ; }
2021-01-25 02:19:29 +03:00
var proxyConfig = require ( 'global-tunnel' ) . proxyConfig ;
var cert = require ( 'MeshAgent' ) . GenerateAgentCertificate ( 'CN=MeshNodeDiagnosticCertificate' ) ;
var nodeid = require ( 'tls' ) . loadCertificate ( cert . root ) . getKeyHash ( ) . toString ( 'base64' ) ;
ddb = require ( 'SimpleDataStore' ) . Create ( svc . appWorkingDirectory ( ) . replace ( '\\' , '/' ) + '/meshagentDiagnostic.db' ) ;
ddb . Put ( 'disableUpdate' , '1' ) ;
ddb . Put ( 'MeshID' , Buffer . from ( require ( 'MeshAgent' ) . ServerInfo . MeshID , 'hex' ) ) ;
ddb . Put ( 'ServerID' , require ( 'MeshAgent' ) . ServerInfo . ServerID ) ;
ddb . Put ( 'MeshServer' , require ( 'MeshAgent' ) . ServerInfo . ServerUri ) ;
if ( cert . root . pfx ) { ddb . Put ( 'SelfNodeCert' , cert . root . pfx ) ; }
if ( cert . tls ) { ddb . Put ( 'SelfNodeTlsCert' , cert . tls . pfx ) ; }
2021-01-25 08:51:22 +03:00
if ( proxyConfig ) {
2021-01-25 02:19:29 +03:00
ddb . Put ( 'WebProxy' , proxyConfig . host + ':' + proxyConfig . port ) ;
2022-01-17 00:37:26 +03:00
} else {
2021-01-25 02:19:29 +03:00
ddb . Put ( 'ignoreProxyFile' , '1' ) ;
}
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . SendCommand ( { action : 'diagnostic' , value : { command : 'register' , value : nodeid } } ) ;
require ( 'MeshAgent' ) . SendCommand ( { action : 'msg' , type : 'console' , value : "Diagnostic Agent Registered [" + nodeid . length + "/" + nodeid + "]" } ) ;
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
delete ddb ;
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
// Set a recurrent task, to run the Diagnostic Agent every 2 days
require ( 'task-scheduler' ) . create ( { name : 'meshagentDiagnostic/periodicStart' , daily : 2 , time : require ( 'tls' ) . generateRandomInteger ( '0' , '23' ) + ':' + require ( 'tls' ) . generateRandomInteger ( '0' , '59' ) . padStart ( 2 , '0' ) , service : 'meshagentDiagnostic' } ) ;
//require('task-scheduler').create({ name: 'meshagentDiagnostic/periodicStart', daily: '1', time: '17:16', service: 'meshagentDiagnostic' });
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
return ( svc ) ;
}
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
// Monitor the file 'batterystate.txt' in the agent's folder and sends battery update when this file is changed.
2021-01-25 08:51:22 +03:00
if ( ( require ( 'fs' ) . existsSync ( process . cwd ( ) + 'batterystate.txt' ) ) && ( require ( 'fs' ) . watch != null ) ) {
2021-01-25 02:19:29 +03:00
// Setup manual battery monitoring
2021-01-25 08:51:22 +03:00
require ( 'MeshAgent' ) . _batteryFileWatcher = require ( 'fs' ) . watch ( process . cwd ( ) , function ( ) {
2021-01-25 02:19:29 +03:00
if ( require ( 'MeshAgent' ) . _batteryFileTimer != null ) return ;
2021-01-25 08:51:22 +03:00
require ( 'MeshAgent' ) . _batteryFileTimer = setTimeout ( function ( ) {
try {
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . _batteryFileTimer = null ;
var data = null ;
2022-01-17 00:37:26 +03:00
try { data = require ( 'fs' ) . readFileSync ( process . cwd ( ) + 'batterystate.txt' ) . toString ( ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( ( data != null ) && ( data . length < 10 ) ) {
2021-01-25 02:19:29 +03:00
data = data . split ( ',' ) ;
2021-01-25 08:51:22 +03:00
if ( ( data . length == 2 ) && ( ( data [ 0 ] == 'ac' ) || ( data [ 0 ] == 'dc' ) ) ) {
2021-01-25 02:19:29 +03:00
var level = parseInt ( data [ 1 ] ) ;
if ( ( level >= 0 ) && ( level <= 100 ) ) { require ( 'MeshAgent' ) . SendCommand ( { action : 'battery' , state : data [ 0 ] , level : level } ) ; }
2020-08-29 06:01:06 +03:00
}
}
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
} , 1000 ) ;
} ) ;
}
2022-01-17 00:37:26 +03:00
else {
try {
2021-08-16 22:57:17 +03:00
// Setup normal battery monitoring
2023-12-23 02:18:17 +03:00
if ( require ( 'computer-identifiers' ) . isBatteryPowered && require ( 'computer-identifiers' ) . isBatteryPowered ( ) ) {
2021-08-16 22:57:17 +03:00
require ( 'MeshAgent' ) . _battLevelChanged = function _battLevelChanged ( val ) {
_battLevelChanged . self . _currentBatteryLevel = val ;
_battLevelChanged . self . SendCommand ( { action : 'battery' , state : _battLevelChanged . self . _currentPowerState , level : val } ) ;
} ;
require ( 'MeshAgent' ) . _battLevelChanged . self = require ( 'MeshAgent' ) ;
require ( 'MeshAgent' ) . _powerChanged = function _powerChanged ( val ) {
_powerChanged . self . _currentPowerState = ( val == 'AC' ? 'ac' : 'dc' ) ;
_powerChanged . self . SendCommand ( { action : 'battery' , state : ( val == 'AC' ? 'ac' : 'dc' ) , level : _powerChanged . self . _currentBatteryLevel } ) ;
} ;
require ( 'MeshAgent' ) . _powerChanged . self = require ( 'MeshAgent' ) ;
require ( 'MeshAgent' ) . on ( 'Connected' , function ( status ) {
if ( status == 0 ) {
require ( 'power-monitor' ) . removeListener ( 'acdc' , this . _powerChanged ) ;
require ( 'power-monitor' ) . removeListener ( 'batteryLevel' , this . _battLevelChanged ) ;
} else {
require ( 'power-monitor' ) . on ( 'acdc' , this . _powerChanged ) ;
require ( 'power-monitor' ) . on ( 'batteryLevel' , this . _battLevelChanged ) ;
}
} ) ;
}
}
2022-01-17 00:37:26 +03:00
catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
2020-05-31 21:24:34 +03:00
2021-01-25 02:19:29 +03:00
// MeshAgent JavaScript Core Module. This code is sent to and running on the mesh agent.
2022-01-17 00:37:26 +03:00
var meshCoreObj = { action : 'coreinfo' , value : ( require ( 'MeshAgent' ) . coreHash ? ( ( process . versions . compileTime ? process . versions . compileTime : '' ) . split ( ', ' ) [ 1 ] . replace ( ' ' , ' ' ) + ', ' + crc32c ( require ( 'MeshAgent' ) . coreHash ) ) : ( 'MeshCore v6' ) ) , caps : 14 , root : require ( 'user-sessions' ) . isRoot ( ) } ; // Capability bitmask: 1 = Desktop, 2 = Terminal, 4 = Files, 8 = Console, 16 = JavaScript, 32 = Temporary Agent, 64 = Recovery Agent
2021-01-25 02:19:29 +03:00
// Get the operating system description string
2022-01-17 00:37:26 +03:00
try { require ( 'os' ) . name ( ) . then ( function ( v ) { meshCoreObj . osdesc = v ; meshCoreObjChanged ( ) ; } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
// Setup logged in user monitoring (THIS IS BROKEN IN WIN7)
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var userSession = require ( 'user-sessions' ) ;
2021-01-25 08:51:22 +03:00
userSession . on ( 'changed' , function onUserSessionChanged ( ) {
userSession . enumerateUsers ( ) . then ( function ( users ) {
if ( process . platform == 'linux' ) {
if ( userSession . _startTime == null ) {
2021-01-25 02:19:29 +03:00
userSession . _startTime = Date . now ( ) ;
userSession . _count = users . length ;
}
2021-01-25 08:51:22 +03:00
else if ( Date . now ( ) - userSession . _startTime < 10000 && users . length == userSession . _count ) {
2021-01-25 02:19:29 +03:00
userSession . removeAllListeners ( 'changed' ) ;
return ;
2018-09-01 01:23:42 +03:00
}
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
var u = [ ] , a = users . Active ;
2021-01-25 08:51:22 +03:00
for ( var i = 0 ; i < a . length ; i ++ ) {
2021-01-25 02:19:29 +03:00
var un = a [ i ] . Domain ? ( a [ i ] . Domain + '\\' + a [ i ] . Username ) : ( a [ i ] . Username ) ;
if ( u . indexOf ( un ) == - 1 ) { u . push ( un ) ; } // Only push users in the list once.
}
meshCoreObj . users = u ;
meshCoreObjChanged ( ) ;
2020-11-02 11:44:07 +03:00
} ) ;
2021-01-25 02:19:29 +03:00
} ) ;
userSession . emit ( 'changed' ) ;
//userSession.on('locked', function (user) { sendConsoleText('[' + (user.Domain ? user.Domain + '\\' : '') + user.Username + '] has LOCKED the desktop'); });
//userSession.on('unlocked', function (user) { sendConsoleText('[' + (user.Domain ? user.Domain + '\\' : '') + user.Username + '] has UNLOCKED the desktop'); });
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
var meshServerConnectionState = 0 ;
var tunnels = { } ;
var lastNetworkInfo = null ;
var lastPublicLocationInfo = null ;
var selfInfoUpdateTimer = null ;
var http = require ( 'http' ) ;
var net = require ( 'net' ) ;
var fs = require ( 'fs' ) ;
var rtc = require ( 'ILibWebRTC' ) ;
var amt = null ;
var processManager = require ( 'process-manager' ) ;
var wifiScannerLib = null ;
var wifiScanner = null ;
var networkMonitor = null ;
var nextTunnelIndex = 1 ;
var apftunnel = null ;
var tunnelUserCount = { terminal : { } , files : { } , tcp : { } , udp : { } , msg : { } } ; // List of userid->count sessions for terminal, files and TCP/UDP routing
// Add to the server event log
2021-01-25 08:51:22 +03:00
function MeshServerLog ( msg , state ) {
2021-01-25 02:19:29 +03:00
if ( typeof msg == 'string' ) { msg = { action : 'log' , msg : msg } ; } else { msg . action = 'log' ; }
2021-01-25 08:51:22 +03:00
if ( state ) {
2021-01-25 02:19:29 +03:00
if ( state . userid ) { msg . userid = state . userid ; }
if ( state . username ) { msg . username = state . username ; }
if ( state . sessionid ) { msg . sessionid = state . sessionid ; }
if ( state . remoteaddr ) { msg . remoteaddr = state . remoteaddr ; }
2021-07-29 22:01:38 +03:00
if ( state . guestname ) { msg . guestname = state . guestname ; }
2021-01-25 02:19:29 +03:00
}
mesh . SendCommand ( msg ) ;
}
2018-08-30 04:47:22 +03:00
2021-01-25 02:19:29 +03:00
// Add to the server event log, use internationalized events
2021-01-25 08:51:22 +03:00
function MeshServerLogEx ( id , args , msg , state ) {
2021-01-25 02:19:29 +03:00
var msg = { action : 'log' , msgid : id , msgArgs : args , msg : msg } ;
2021-01-25 08:51:22 +03:00
if ( state ) {
2021-01-25 02:19:29 +03:00
if ( state . userid ) { msg . userid = state . userid ; }
2022-01-11 04:42:54 +03:00
if ( state . xuserid ) { msg . xuserid = state . xuserid ; }
2021-01-25 02:19:29 +03:00
if ( state . username ) { msg . username = state . username ; }
if ( state . sessionid ) { msg . sessionid = state . sessionid ; }
if ( state . remoteaddr ) { msg . remoteaddr = state . remoteaddr ; }
2021-07-29 22:01:38 +03:00
if ( state . guestname ) { msg . guestname = state . guestname ; }
2020-09-09 04:59:58 +03:00
}
2021-01-25 02:19:29 +03:00
mesh . SendCommand ( msg ) ;
}
2020-09-09 04:59:58 +03:00
2021-01-25 02:19:29 +03:00
// Import libraries
db = require ( 'SimpleDataStore' ) . Shared ( ) ;
sha = require ( 'SHA256Stream' ) ;
mesh = require ( 'MeshAgent' ) ;
childProcess = require ( 'child_process' ) ;
2021-01-25 08:51:22 +03:00
if ( mesh . hasKVM == 1 ) { // if the agent is compiled with KVM support
2021-01-25 02:19:29 +03:00
// Check if this computer supports a desktop
2021-01-25 08:51:22 +03:00
try {
if ( ( process . platform == 'win32' ) || ( process . platform == 'darwin' ) || ( require ( 'monitor-info' ) . kvm _x11 _support ) ) {
2021-01-25 02:19:29 +03:00
meshCoreObj . caps |= 1 ; meshCoreObjChanged ( ) ;
2021-01-25 08:51:22 +03:00
} else if ( process . platform == 'linux' || process . platform == 'freebsd' ) {
2021-01-25 02:19:29 +03:00
require ( 'monitor-info' ) . on ( 'kvmSupportDetected' , function ( value ) { meshCoreObj . caps |= 1 ; meshCoreObjChanged ( ) ; } ) ;
2019-07-30 02:35:48 +03:00
}
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
mesh . DAIPC = obj . DAIPC ;
2019-07-30 02:35:48 +03:00
2021-01-25 02:19:29 +03:00
/ *
// Try to load up the network monitor
try {
networkMonitor = require ( 'NetworkMonitor' ) ;
networkMonitor . on ( 'change' , function ( ) { sendNetworkUpdateNagle ( ) ; } ) ;
networkMonitor . on ( 'add' , function ( addr ) { sendNetworkUpdateNagle ( ) ; } ) ;
networkMonitor . on ( 'remove' , function ( addr ) { sendNetworkUpdateNagle ( ) ; } ) ;
2022-01-17 00:37:26 +03:00
} catch ( ex ) { networkMonitor = null ; }
2021-01-25 02:19:29 +03:00
* /
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
// Fetch the SMBios Tables
var SMBiosTables = null ;
var SMBiosTablesRaw = null ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var SMBiosModule = null ;
2022-01-17 00:37:26 +03:00
try { SMBiosModule = require ( 'smbios' ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( SMBiosModule != null ) {
SMBiosModule . get ( function ( data ) {
if ( data != null ) {
2021-01-25 02:19:29 +03:00
SMBiosTablesRaw = data ;
SMBiosTables = require ( 'smbios' ) . parse ( data )
if ( mesh . isControlChannelConnected ) { mesh . SendCommand ( { action : 'smbios' , value : SMBiosTablesRaw } ) ; }
2018-01-10 07:13:41 +03:00
2021-01-25 02:19:29 +03:00
// If SMBios tables say that Intel AMT is present, try to connect MEI
2021-01-25 08:51:22 +03:00
if ( SMBiosTables . amtInfo && ( SMBiosTables . amtInfo . AMT == true ) ) {
2021-01-25 02:19:29 +03:00
var amtmodule = require ( 'amt-manage' ) ;
amt = new amtmodule ( mesh , db , false ) ;
amt . on ( 'portBinding_LMS' , function ( map ) { mesh . SendCommand ( { action : 'lmsinfo' , value : { ports : map . keys ( ) } } ) ; } ) ;
amt . on ( 'stateChange_LMS' , function ( v ) { if ( ! meshCoreObj . intelamt ) { meshCoreObj . intelamt = { } ; } meshCoreObj . intelamt . microlms = v ; meshCoreObjChanged ( ) ; } ) ; // 0 = Disabled, 1 = Connecting, 2 = Connected
amt . onStateChange = function ( state ) { if ( state == 2 ) { sendPeriodicServerUpdate ( 1 ) ; } } // MEI State
amt . reset ( ) ;
2018-12-31 00:32:40 +03:00
}
2021-01-25 02:19:29 +03:00
}
} ) ;
2017-08-28 22:48:53 +03:00
}
2022-01-17 00:37:26 +03:00
} catch ( ex ) { sendConsoleText ( "ex1: " + ex ) ; }
2021-01-25 02:19:29 +03:00
// Try to load up the WIFI scanner
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var wifiScannerLib = require ( 'wifi-scanner' ) ;
wifiScanner = new wifiScannerLib ( ) ;
wifiScanner . on ( 'accessPoint' , function ( data ) { sendConsoleText ( "wifiScanner: " + data ) ; } ) ;
2022-01-17 00:37:26 +03:00
} catch ( ex ) { wifiScannerLib = null ; wifiScanner = null ; }
2021-01-25 02:19:29 +03:00
// Get our location (lat/long) using our public IP address
var getIpLocationDataExInProgress = false ;
var getIpLocationDataExCounts = [ 0 , 0 ] ;
2021-01-25 08:51:22 +03:00
function getIpLocationDataEx ( func ) {
2021-01-25 02:19:29 +03:00
if ( getIpLocationDataExInProgress == true ) { return false ; }
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
getIpLocationDataExInProgress = true ;
getIpLocationDataExCounts [ 0 ] ++ ;
var options = http . parseUri ( "http://ipinfo.io/json" ) ;
options . method = 'GET' ;
2021-01-25 08:51:22 +03:00
http . request ( options , function ( resp ) {
if ( resp . statusCode == 200 ) {
2021-01-25 02:19:29 +03:00
var geoData = '' ;
resp . data = function ( geoipdata ) { geoData += geoipdata ; } ;
2021-01-25 08:51:22 +03:00
resp . end = function ( ) {
2021-01-25 02:19:29 +03:00
var location = null ;
2021-01-25 08:51:22 +03:00
try {
if ( typeof geoData == 'string' ) {
2021-01-25 02:19:29 +03:00
var result = JSON . parse ( geoData ) ;
if ( result . ip && result . loc ) { location = result ; }
}
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
if ( func ) { getIpLocationDataExCounts [ 1 ] ++ ; func ( location ) ; }
}
} else
{ func ( null ) ; }
getIpLocationDataExInProgress = false ;
} ) . end ( ) ;
return true ;
2017-08-28 22:48:53 +03:00
}
2022-01-17 00:37:26 +03:00
catch ( ex ) { return false ; }
2021-01-25 02:19:29 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Remove all Gateway MAC addresses for interface list. This is useful because the gateway MAC is not always populated reliably.
2021-01-25 08:51:22 +03:00
function clearGatewayMac ( str ) {
2021-02-03 01:08:30 +03:00
if ( typeof str != 'string' ) return null ;
2021-01-25 02:19:29 +03:00
var x = JSON . parse ( str ) ;
2021-02-03 01:08:30 +03:00
for ( var i in x . netif ) { try { if ( x . netif [ i ] . gatewaymac ) { delete x . netif [ i ] . gatewaymac } } catch ( ex ) { } }
2021-01-25 02:19:29 +03:00
return JSON . stringify ( x ) ;
}
2021-01-25 08:51:22 +03:00
function getIpLocationData ( func ) {
2021-01-25 02:19:29 +03:00
// Get the location information for the cache if possible
var publicLocationInfo = db . Get ( 'publicLocationInfo' ) ;
if ( publicLocationInfo != null ) { publicLocationInfo = JSON . parse ( publicLocationInfo ) ; }
2021-01-25 08:51:22 +03:00
if ( publicLocationInfo == null ) {
2021-01-25 02:19:29 +03:00
// Nothing in the cache, fetch the data
2021-01-25 08:51:22 +03:00
getIpLocationDataEx ( function ( locationData ) {
if ( locationData != null ) {
2021-01-25 02:19:29 +03:00
publicLocationInfo = { } ;
publicLocationInfo . netInfoStr = lastNetworkInfo ;
publicLocationInfo . locationData = locationData ;
var x = db . Put ( 'publicLocationInfo' , JSON . stringify ( publicLocationInfo ) ) ; // Save to database
if ( func ) func ( locationData ) ; // Report the new location
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
if ( func ) func ( null ) ; // Report no location
}
} ) ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
// Check the cache
2021-01-25 08:51:22 +03:00
if ( clearGatewayMac ( publicLocationInfo . netInfoStr ) == clearGatewayMac ( lastNetworkInfo ) ) {
2021-01-25 02:19:29 +03:00
// Cache match
if ( func ) func ( publicLocationInfo . locationData ) ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
// Cache mismatch
2021-01-25 08:51:22 +03:00
getIpLocationDataEx ( function ( locationData ) {
if ( locationData != null ) {
2017-09-02 03:34:02 +03:00
publicLocationInfo = { } ;
publicLocationInfo . netInfoStr = lastNetworkInfo ;
publicLocationInfo . locationData = locationData ;
var x = db . Put ( 'publicLocationInfo' , JSON . stringify ( publicLocationInfo ) ) ; // Save to database
if ( func ) func ( locationData ) ; // Report the new location
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
if ( func ) func ( publicLocationInfo . locationData ) ; // Can't get new location, report the old location
2017-09-02 03:34:02 +03:00
}
2017-08-28 22:48:53 +03:00
} ) ;
}
}
2021-01-25 02:19:29 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Polyfill String.endsWith
2021-01-25 08:51:22 +03:00
if ( ! String . prototype . endsWith ) {
String . prototype . endsWith = function ( searchString , position ) {
2021-01-25 02:19:29 +03:00
var subjectString = this . toString ( ) ;
if ( typeof position !== 'number' || ! isFinite ( position ) || Math . floor ( position ) !== position || position > subjectString . length ) { position = subjectString . length ; }
position -= searchString . length ;
var lastIndex = subjectString . lastIndexOf ( searchString , position ) ;
return lastIndex !== - 1 && lastIndex === position ;
} ;
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Polyfill path.join
obj . path =
{
2021-01-25 08:51:22 +03:00
join : function ( ) {
2017-08-28 19:27:45 +03:00
var x = [ ] ;
2021-01-25 08:51:22 +03:00
for ( var i in arguments ) {
2017-08-28 19:27:45 +03:00
var w = arguments [ i ] ;
2021-01-25 08:51:22 +03:00
if ( w != null ) {
2017-08-28 19:27:45 +03:00
while ( w . endsWith ( '/' ) || w . endsWith ( '\\' ) ) { w = w . substring ( 0 , w . length - 1 ) ; }
2021-01-25 08:51:22 +03:00
if ( i != 0 ) {
2017-10-24 00:09:58 +03:00
while ( w . startsWith ( '/' ) || w . startsWith ( '\\' ) ) { w = w . substring ( 1 ) ; }
}
2017-08-28 19:27:45 +03:00
x . push ( w ) ;
}
}
if ( x . length == 0 ) return '/' ;
return x . join ( '/' ) ;
}
} ;
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Replace a string with a number if the string is an exact number
function toNumberIfNumber ( x ) { if ( ( typeof x == 'string' ) && ( + parseInt ( x ) === x ) ) { x = parseInt ( x ) ; } return x ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Convert decimal to hex
function char2hex ( i ) { return ( i + 0x100 ) . toString ( 16 ) . substr ( - 2 ) . toUpperCase ( ) ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Convert a raw string to a hex string
function rstr2hex ( input ) { var r = '' , i ; for ( i = 0 ; i < input . length ; i ++ ) { r += char2hex ( input . charCodeAt ( i ) ) ; } return r ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Convert a buffer into a string
function buf2rstr ( buf ) { var r = '' ; for ( var i = 0 ; i < buf . length ; i ++ ) { r += String . fromCharCode ( buf [ i ] ) ; } return r ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Convert a hex string to a raw string // TODO: Do this using Buffer(), will be MUCH faster
2021-01-25 08:51:22 +03:00
function hex2rstr ( d ) {
2021-01-25 02:19:29 +03:00
if ( typeof d != "string" || d . length == 0 ) return '' ;
var r = '' , m = ( '' + d ) . match ( /../g ) , t ;
while ( t = m . shift ( ) ) r += String . fromCharCode ( '0x' + t ) ;
return r
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Convert an object to string with all functions
2021-01-25 08:51:22 +03:00
function objToString ( x , p , pad , ret ) {
2021-01-25 02:19:29 +03:00
if ( ret == undefined ) ret = '' ;
if ( p == undefined ) p = 0 ;
if ( x == null ) { return '[null]' ; }
if ( p > 8 ) { return '[...]' ; }
if ( x == undefined ) { return '[undefined]' ; }
if ( typeof x == 'string' ) { if ( p == 0 ) return x ; return '"' + x + '"' ; }
if ( typeof x == 'buffer' ) { return '[buffer]' ; }
if ( typeof x != 'object' ) { return x ; }
var r = '{' + ( ret ? '\r\n' : ' ' ) ;
for ( var i in x ) { if ( i != '_ObjectID' ) { r += ( addPad ( p + 2 , pad ) + i + ': ' + objToString ( x [ i ] , p + 2 , pad , ret ) + ( ret ? '\r\n' : ' ' ) ) ; } }
return r + addPad ( p , pad ) + '}' ;
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Return p number of spaces
function addPad ( p , ret ) { var r = '' ; for ( var i = 0 ; i < p ; i ++ ) { r += ret ; } return r ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Split a string taking into account the quoats. Used for command line parsing
2021-01-25 08:51:22 +03:00
function splitArgs ( str ) {
2021-01-25 02:19:29 +03:00
var myArray = [ ] , myRegexp = /[^\s"]+|"([^"]*)"/gi ;
do { var match = myRegexp . exec ( str ) ; if ( match != null ) { myArray . push ( match [ 1 ] ? match [ 1 ] : match [ 0 ] ) ; } } while ( match != null ) ;
return myArray ;
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Parse arguments string array into an object
2021-01-25 08:51:22 +03:00
function parseArgs ( argv ) {
2021-01-25 02:19:29 +03:00
var results = { '_' : [ ] } , current = null ;
2021-01-25 08:51:22 +03:00
for ( var i = 1 , len = argv . length ; i < len ; i ++ ) {
2021-01-25 02:19:29 +03:00
var x = argv [ i ] ;
2021-01-25 08:51:22 +03:00
if ( x . length > 2 && x [ 0 ] == '-' && x [ 1 ] == '-' ) {
2021-01-25 02:19:29 +03:00
if ( current != null ) { results [ current ] = true ; }
current = x . substring ( 2 ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
if ( current != null ) { results [ current ] = toNumberIfNumber ( x ) ; current = null ; } else { results [ '_' ] . push ( toNumberIfNumber ( x ) ) ; }
2017-08-28 19:27:45 +03:00
}
}
2021-01-25 02:19:29 +03:00
if ( current != null ) { results [ current ] = true ; }
return results ;
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Get server target url with a custom path
2021-01-25 08:51:22 +03:00
function getServerTargetUrl ( path ) {
2021-01-25 02:19:29 +03:00
var x = mesh . ServerUrl ;
//sendConsoleText("mesh.ServerUrl: " + mesh.ServerUrl);
if ( x == null ) { return null ; }
if ( path == null ) { path = '' ; }
x = http . parseUri ( x ) ;
if ( x == null ) return null ;
return x . protocol + '//' + x . host + ':' + x . port + '/' + path ;
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Get server url. If the url starts with "*/..." change it, it not use the url as is.
2021-01-25 08:51:22 +03:00
function getServerTargetUrlEx ( url ) {
2021-01-25 02:19:29 +03:00
if ( url . substring ( 0 , 2 ) == '*/' ) { return getServerTargetUrl ( url . substring ( 2 ) ) ; }
return url ;
}
2019-10-10 21:13:25 +03:00
2021-04-24 20:53:13 +03:00
function sendWakeOnLanEx _interval ( ) {
2021-04-24 20:20:35 +03:00
var t = require ( 'MeshAgent' ) . wakesockets ;
2021-04-24 20:53:13 +03:00
if ( t . list . length == 0 ) {
2021-04-24 20:20:35 +03:00
clearInterval ( t ) ;
2021-04-24 20:53:13 +03:00
delete require ( 'MeshAgent' ) . wakesockets ;
2021-04-24 20:20:35 +03:00
return ;
}
var mac = t . list . shift ( ) . split ( ':' ) . join ( '' )
var magic = 'FFFFFFFFFFFF' ;
for ( var x = 1 ; x <= 16 ; ++ x ) { magic += mac ; }
var magicbin = Buffer . from ( magic , 'hex' ) ;
2021-01-25 02:19:29 +03:00
2021-04-24 20:53:13 +03:00
for ( var i in t . sockets ) {
2021-04-24 20:20:35 +03:00
t . sockets [ i ] . send ( magicbin , 7 , '255.255.255.255' ) ;
//sendConsoleText('Sending wake packet on ' + JSON.stringify(t.sockets[i].address()));
}
}
2021-04-24 20:53:13 +03:00
function sendWakeOnLanEx ( hexMacList ) {
2021-04-24 20:20:35 +03:00
var ret = 0 ;
2021-04-24 20:53:13 +03:00
if ( require ( 'MeshAgent' ) . wakesockets == null ) {
2021-04-24 20:20:35 +03:00
// Create a new interval timer
require ( 'MeshAgent' ) . wakesockets = setInterval ( sendWakeOnLanEx _interval , 10 ) ;
require ( 'MeshAgent' ) . wakesockets . sockets = [ ] ;
require ( 'MeshAgent' ) . wakesockets . list = hexMacList ;
var interfaces = require ( 'os' ) . networkInterfaces ( ) ;
2021-04-24 20:53:13 +03:00
for ( var adapter in interfaces ) {
if ( interfaces . hasOwnProperty ( adapter ) ) {
for ( var i = 0 ; i < interfaces [ adapter ] . length ; ++ i ) {
2021-01-25 02:19:29 +03:00
var addr = interfaces [ adapter ] [ i ] ;
2021-04-24 20:53:13 +03:00
if ( ( addr . family == 'IPv4' ) && ( addr . mac != '00:00:00:00:00:00' ) ) {
try {
2021-01-25 02:19:29 +03:00
var socket = require ( 'dgram' ) . createSocket ( { type : 'udp4' } ) ;
socket . bind ( { address : addr . address } ) ;
socket . setBroadcast ( true ) ;
socket . setMulticastInterface ( addr . address ) ;
socket . setMulticastTTL ( 1 ) ;
2021-04-24 20:20:35 +03:00
socket . descriptorMetadata = 'WoL (' + addr . address + ')' ;
require ( 'MeshAgent' ) . wakesockets . sockets . push ( socket ) ;
++ ret ;
2017-09-21 00:44:22 +03:00
}
2022-01-17 00:37:26 +03:00
catch ( ex ) { }
2017-09-21 00:44:22 +03:00
}
}
}
2021-01-25 02:19:29 +03:00
}
2021-04-24 20:20:35 +03:00
}
2021-04-24 20:53:13 +03:00
else {
2021-04-24 20:20:35 +03:00
// Append to an existing interval timer
2021-04-24 20:53:13 +03:00
for ( var i in hexMacList ) {
2021-04-24 20:20:35 +03:00
require ( 'MeshAgent' ) . wakesockets . list . push ( hexMacList [ i ] ) ;
}
ret = require ( 'MeshAgent' ) . wakesockets . sockets . length ;
}
2022-01-17 00:37:26 +03:00
return ret ;
2021-01-25 02:19:29 +03:00
}
2019-10-10 21:13:25 +03:00
2022-01-17 00:37:26 +03:00
function server _promise _default ( res , rej ) {
2021-12-14 04:37:22 +03:00
this . resolve = res ;
this . reject = rej ;
}
2022-01-17 00:37:26 +03:00
function server _getUserImage ( userid ) {
var xpromise = require ( 'promise' ) ;
var ret = new xpromise ( server _promise _default ) ;
2021-12-14 04:37:22 +03:00
if ( require ( 'MeshAgent' ) . _promises == null ) { require ( 'MeshAgent' ) . _promises = { } ; }
require ( 'MeshAgent' ) . _promises [ ret . _hashCode ( ) ] = ret ;
2022-01-17 00:37:26 +03:00
require ( 'MeshAgent' ) . SendCommand ( { action : 'getUserImage' , userid : userid , promise : ret . _hashCode ( ) , sentDefault : true } ) ;
return ret ;
2021-12-14 04:37:22 +03:00
}
require ( 'MeshAgent' ) . _consentTimers = { } ;
2022-01-17 00:37:26 +03:00
function server _set _consentTimer ( id ) {
2021-12-14 04:37:22 +03:00
require ( 'MeshAgent' ) . _consentTimers [ id ] = new Date ( ) ;
}
2022-01-17 00:37:26 +03:00
function server _check _consentTimer ( id ) {
if ( require ( 'MeshAgent' ) . _consentTimers [ id ] != null ) {
if ( ( new Date ( ) ) - require ( 'MeshAgent' ) . _consentTimers [ id ] < ( 60000 * 5 ) ) return true ;
require ( 'MeshAgent' ) . _consentTimers [ id ] = null ;
2021-12-14 04:37:22 +03:00
}
2022-01-17 00:37:26 +03:00
return false ;
2021-12-14 04:37:22 +03:00
}
2022-08-26 06:11:47 +03:00
function tunnel _finalized ( )
{
console . info1 ( 'Tunnel Request Finalized' ) ;
}
function tunnel _checkServerIdentity ( certs )
{
/ *
try { sendConsoleText ( "certs[0].digest: " + certs [ 0 ] . digest ) ; } catch ( ex ) { sendConsoleText ( ex ) ; }
try { sendConsoleText ( "certs[0].fingerprint: " + certs [ 0 ] . fingerprint ) ; } catch ( ex ) { sendConsoleText ( ex ) ; }
try { sendConsoleText ( "control-digest: " + require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . digest ) ; } catch ( ex ) { sendConsoleText ( ex ) ; }
try { sendConsoleText ( "control-fingerprint: " + require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . fingerprint ) ; } catch ( ex ) { sendConsoleText ( ex ) ; }
* /
// Check if this is an old agent, no certificate checks are possible in this situation. Display a warning.
if ( ( require ( 'MeshAgent' ) . ServerInfo == null ) || ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate == null ) || ( certs [ 0 ] . digest == null ) ) { sendAgentMessage ( "This agent is using insecure tunnels, consider updating." , 3 , 119 , true ) ; return ; }
// If the tunnel certificate matches the control channel certificate, accept the connection
if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . digest == certs [ 0 ] . digest ) return ; // Control channel certificate matches using full cert hash
if ( ( certs [ 0 ] . fingerprint != null ) && ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . fingerprint == certs [ 0 ] . fingerprint ) ) return ; // Control channel certificate matches using public key hash
// Check that the certificate is the one expected by the server, fail if not.
if ( ( tunnel _checkServerIdentity . servertlshash != null ) && ( tunnel _checkServerIdentity . servertlshash . toLowerCase ( ) != certs [ 0 ] . digest . split ( ':' ) . join ( '' ) . toLowerCase ( ) ) ) { throw new Error ( 'BadCert' ) }
}
function tunnel _onError ( )
{
sendConsoleText ( "ERROR: Unable to connect relay tunnel to: " + this . url + ", " + JSON . stringify ( e ) ) ;
}
2021-01-25 02:19:29 +03:00
// Handle a mesh agent command
2021-01-25 08:51:22 +03:00
function handleServerCommand ( data ) {
if ( typeof data == 'object' ) {
2021-01-25 02:19:29 +03:00
// If this is a console command, parse it and call the console handler
2021-01-25 08:51:22 +03:00
switch ( data . action ) {
2021-01-25 02:19:29 +03:00
case 'agentupdate' :
agentUpdate _Start ( data . url , { hash : data . hash , tlshash : data . servertlshash , sessionid : data . sessionid } ) ;
break ;
case 'msg' : {
2021-01-25 08:51:22 +03:00
switch ( data . type ) {
2021-01-25 02:19:29 +03:00
case 'console' : { // Process a console command
2021-08-28 19:51:03 +03:00
if ( ( typeof data . rights != 'number' ) || ( ( data . rights & 8 ) == 0 ) || ( ( data . rights & 16 ) == 0 ) ) break ; // Check console rights (Remote Control and Console)
2021-01-25 08:51:22 +03:00
if ( data . value && data . sessionid ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 17 , [ data . value ] , "Processing console command: " + data . value , data ) ;
var args = splitArgs ( data . value ) ;
processConsoleCommand ( args [ 0 ] . toLowerCase ( ) , parseArgs ( args ) , data . rights , data . sessionid ) ;
2017-08-28 22:48:53 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2022-08-26 06:11:47 +03:00
case 'tunnel' :
{
2021-01-25 08:51:22 +03:00
if ( data . value != null ) { // Process a new tunnel connection request
2021-01-25 02:19:29 +03:00
// Create a new tunnel object
var xurl = getServerTargetUrlEx ( data . value ) ;
2021-01-25 08:51:22 +03:00
if ( xurl != null ) {
2021-01-25 02:19:29 +03:00
xurl = xurl . split ( '$' ) . join ( '%24' ) . split ( '@' ) . join ( '%40' ) ; // Escape the $ and @ characters
var woptions = http . parseUri ( xurl ) ;
woptions . perMessageDeflate = false ;
if ( typeof data . perMessageDeflate == 'boolean' ) { woptions . perMessageDeflate = data . perMessageDeflate ; }
// Perform manual server TLS certificate checking based on the certificate hash given by the server.
woptions . rejectUnauthorized = 0 ;
2022-08-26 06:11:47 +03:00
woptions . checkServerIdentity = tunnel _checkServerIdentity ;
2021-01-25 02:19:29 +03:00
woptions . checkServerIdentity . servertlshash = data . servertlshash ;
//sendConsoleText(JSON.stringify(woptions));
2022-02-24 04:17:28 +03:00
//sendConsoleText('TUNNEL: ' + JSON.stringify(data, null, 2));
2022-08-26 06:11:47 +03:00
2021-01-25 02:19:29 +03:00
var tunnel = http . request ( woptions ) ;
tunnel . upgrade = onTunnelUpgrade ;
2022-08-26 06:11:47 +03:00
tunnel . on ( 'error' , tunnel _onError ) ;
2021-01-25 02:19:29 +03:00
tunnel . sessionid = data . sessionid ;
tunnel . rights = data . rights ;
tunnel . consent = data . consent ;
2021-06-11 08:54:53 +03:00
if ( global . _MSH && _MSH ( ) . LocalConsent != null ) { tunnel . consent |= parseInt ( _MSH ( ) . LocalConsent ) ; }
2022-01-15 02:54:12 +03:00
tunnel . privacybartext = data . privacybartext ? data . privacybartext : currentTranslation [ 'privacyBar' ] ;
2021-01-25 02:19:29 +03:00
tunnel . username = data . username + ( data . guestname ? ( ' - ' + data . guestname ) : '' ) ;
tunnel . realname = ( data . realname ? data . realname : data . username ) + ( data . guestname ? ( ' - ' + data . guestname ) : '' ) ;
2022-02-24 04:17:28 +03:00
tunnel . guestuserid = data . guestuserid ;
2021-06-25 21:49:58 +03:00
tunnel . guestname = data . guestname ;
2021-01-25 02:19:29 +03:00
tunnel . userid = data . userid ;
2022-02-22 23:32:49 +03:00
if ( server _check _consentTimer ( tunnel . userid ) ) { tunnel . consent = ( tunnel . consent & - 57 ) ; } // Deleting Consent Requirement
2021-07-18 08:16:57 +03:00
tunnel . desktopviewonly = data . desktopviewonly ;
2021-01-25 02:19:29 +03:00
tunnel . remoteaddr = data . remoteaddr ;
tunnel . state = 0 ;
tunnel . url = xurl ;
tunnel . protocol = 0 ;
tunnel . soptions = data . soptions ;
2022-02-22 23:32:49 +03:00
tunnel . consentTimeout = ( tunnel . soptions && tunnel . soptions . consentTimeout ) ? tunnel . soptions . consentTimeout : 30 ;
tunnel . consentAutoAccept = ( tunnel . soptions && ( tunnel . soptions . consentAutoAccept === true ) ) ;
2021-01-25 02:19:29 +03:00
tunnel . tcpaddr = data . tcpaddr ;
tunnel . tcpport = data . tcpport ;
tunnel . udpaddr = data . udpaddr ;
tunnel . udpport = data . udpport ;
2022-08-26 06:11:47 +03:00
2021-01-25 02:19:29 +03:00
// Put the tunnel in the tunnels list
var index = nextTunnelIndex ++ ;
tunnel . index = index ;
tunnels [ index ] = tunnel ;
2022-08-26 06:11:47 +03:00
tunnel . once ( '~' , tunnel _finalized ) ;
tunnel . end ( ) ;
2021-01-25 02:19:29 +03:00
//sendConsoleText('New tunnel connection #' + index + ': ' + tunnel.url + ', rights: ' + tunnel.rights, data.sessionid);
2018-04-11 23:49:05 +03:00
}
2018-12-08 03:36:27 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2021-10-19 21:47:39 +03:00
case 'endtunnel' : {
// Terminate one or more tunnels
if ( ( data . rights != 4294967295 ) && ( data . xuserid != data . userid ) ) return ; // This command requires full admin rights on the device or user self-closes it's own sessions
for ( var i in tunnels ) {
if ( ( tunnels [ i ] . userid == data . xuserid ) && ( tunnels [ i ] . guestname == data . guestname ) ) {
2022-01-11 04:42:54 +03:00
var disconnect = false , msgid = 0 ;
if ( ( data . protocol == 'kvm' ) && ( tunnels [ i ] . protocol == 2 ) ) { msgid = 134 ; disconnect = true ; }
else if ( ( data . protocol == 'terminal' ) && ( tunnels [ i ] . protocol == 1 ) ) { msgid = 135 ; disconnect = true ; }
else if ( ( data . protocol == 'files' ) && ( tunnels [ i ] . protocol == 5 ) ) { msgid = 136 ; disconnect = true ; }
else if ( ( data . protocol == 'tcp' ) && ( tunnels [ i ] . tcpport != null ) ) { msgid = 137 ; disconnect = true ; }
else if ( ( data . protocol == 'udp' ) && ( tunnels [ i ] . udpport != null ) ) { msgid = 137 ; disconnect = true ; }
2022-01-11 02:28:46 +03:00
if ( disconnect ) {
if ( tunnels [ i ] . s != null ) { tunnels [ i ] . s . end ( ) ; } else { tunnels [ i ] . end ( ) ; }
2022-01-11 04:42:54 +03:00
// Log tunnel disconnection
var xusername = data . xuserid . split ( '/' ) [ 2 ] ;
if ( data . guestname != null ) { xusername += '/' + guestname ; }
MeshServerLogEx ( msgid , [ xusername ] , "Forcibly disconnected session of user: " + xusername , data ) ;
2022-01-11 02:28:46 +03:00
}
2021-10-19 21:47:39 +03:00
}
}
break ;
}
2021-01-25 02:19:29 +03:00
case 'messagebox' : {
// Display a message box
2022-08-04 11:52:29 +03:00
if ( data . title && data . msg ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 18 , [ data . title , data . msg ] , "Displaying message box, title=" + data . title + ", message=" + data . msg , data ) ;
2022-08-04 11:52:29 +03:00
if ( process . platform == 'win32' ) {
if ( global . _clientmessage ) {
2022-01-31 21:19:17 +03:00
global . _clientmessage . addMessage ( data . msg ) ;
}
2022-08-04 11:52:29 +03:00
else {
try {
2022-01-31 21:19:17 +03:00
require ( 'win-dialog' ) ;
var ipr = server _getUserImage ( data . userid ) ;
ipr . title = data . title ;
ipr . message = data . msg ;
ipr . username = data . username ;
2022-02-15 22:10:55 +03:00
if ( data . realname && ( data . realname != '' ) ) { ipr . username = data . realname ; }
2024-01-13 01:46:12 +03:00
ipr . timeout = ( typeof data . timeout === 'number' ? data . timeout : 120000 ) ;
2022-08-04 11:52:29 +03:00
global . _clientmessage = ipr . then ( function ( img ) {
2024-01-13 01:46:12 +03:00
var options = { b64Image : img . split ( ',' ) . pop ( ) , background : color _options . background , foreground : color _options . foreground }
if ( this . timeout != 0 ) { options . timeout = this . timeout ; }
this . messagebox = require ( 'win-dialog' ) . create ( this . title , this . message , this . username , options ) ;
2022-01-31 21:19:17 +03:00
this . _ _childPromise . addMessage = this . messagebox . addMessage . bind ( this . messagebox ) ;
return ( this . messagebox ) ;
} ) ;
global . _clientmessage . then ( function ( ) { global . _clientmessage = null ; } ) ;
}
2022-08-04 11:52:29 +03:00
catch ( z ) {
2022-01-31 21:19:17 +03:00
try { require ( 'message-box' ) . create ( data . title , data . msg , 120 ) . then ( function ( ) { } ) . catch ( function ( ) { } ) ; } catch ( ex ) { }
}
}
}
2022-08-04 11:52:29 +03:00
else {
2022-01-31 21:19:17 +03:00
try { require ( 'message-box' ) . create ( data . title , data . msg , 120 ) . then ( function ( ) { } ) . catch ( function ( ) { } ) ; } catch ( ex ) { }
}
2019-08-01 02:49:23 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'ps' : {
// Return the list of running processes
2021-01-25 08:51:22 +03:00
if ( data . sessionid ) {
processManager . getProcesses ( function ( plist ) {
2021-01-25 02:19:29 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'ps' , value : JSON . stringify ( plist ) , sessionid : data . sessionid } ) ;
} ) ;
2019-08-01 02:49:23 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2021-06-14 00:52:59 +03:00
case 'psinfo' : {
// Requestion details information about a process
if ( data . pid ) {
var info = { } ; // TODO: Replace with real data. Feel free not to give all values if not available.
2023-10-28 21:29:05 +03:00
try {
info = processManager . getProcessInfo ( data . pid ) ;
} catch ( e ) { }
2021-06-14 22:37:26 +03:00
/ *
info . processUser = "User" ; // String
info . processDomain = "Domain" ; // String
info . cmd = "abc" ; // String
2021-06-14 00:52:59 +03:00
info . processName = "dummydata" ;
2021-06-14 22:37:26 +03:00
info . privateMemorySize = 123 ; // Bytes
info . virtualMemorySize = 123 ; // Bytes
info . workingSet = 123 ; // Bytes
2021-06-14 00:52:59 +03:00
info . totalProcessorTime = 123 ; // Seconds
info . userProcessorTime = 123 ; // Seconds
info . startTime = "2012-12-30T23:59:59.000Z" ; // Time in UTC ISO format
2021-06-14 22:37:26 +03:00
info . sessionId = 123 ; // Number
2021-06-14 00:52:59 +03:00
info . privilegedProcessorTime = 123 ; // Seconds
2021-06-14 22:37:26 +03:00
info . PriorityBoostEnabled = true ; // Boolean
info . peakWorkingSet = 123 ; // Bytes
info . peakVirtualMemorySize = 123 ; // Bytes
info . peakPagedMemorySize = 123 ; // Bytes
info . pagedSystemMemorySize = 123 ; // Bytes
info . pagedMemorySize = 123 ; // Bytes
info . nonpagedSystemMemorySize = 123 ; // Bytes
info . mainWindowTitle = "dummydata" ; // String
2021-06-14 00:52:59 +03:00
info . machineName = "dummydata" ; // Only set this if machine name is not "."
2021-06-14 22:37:26 +03:00
info . handleCount = 123 ; // Number
* /
2021-06-14 00:52:59 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'psinfo' , pid : data . pid , sessionid : data . sessionid , value : info } ) ;
}
break ;
}
2021-01-25 02:19:29 +03:00
case 'pskill' : {
// Kill a process
2021-01-25 08:51:22 +03:00
if ( data . value ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 19 , [ data . value ] , "Killing process " + data . value , data ) ;
2022-01-17 06:12:13 +03:00
try { process . kill ( data . value ) ; } catch ( ex ) { sendConsoleText ( "pskill: " + JSON . stringify ( ex ) ) ; }
2019-08-01 02:49:23 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2024-01-24 14:51:28 +03:00
case 'service' : {
// return information about the service
try {
var service = require ( 'service-manager' ) . manager . getService ( data . serviceName ) ;
if ( service != null ) {
var reply = {
name : ( service . name ? service . name : '' ) ,
status : ( service . status ? service . status : '' ) ,
startType : ( service . startType ? service . startType : '' ) ,
failureActions : ( service . failureActions ? service . failureActions : '' ) ,
installedDate : ( service . installedDate ? service . installedDate : '' ) ,
installedBy : ( service . installedBy ? service . installedBy : '' ) ,
user : ( service . user ? service . user : '' )
} ;
if ( reply . installedBy . indexOf ( 'S-1-5' ) != - 1 ) {
var cmd = "(Get-WmiObject -Class win32_userAccount -Filter \"SID='" + service . installedBy + "'\").Caption" ;
var replydata = "" ;
var pws = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' , [ 'powershell' , '-noprofile' , '-nologo' , '-command' , '-' ] , { } ) ;
pws . descriptorMetadata = 'UserSIDPowerShell' ;
pws . stdout . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
pws . stderr . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
pws . stdin . write ( cmd + '\r\nexit\r\n' ) ;
pws . on ( 'exit' , function ( ) {
if ( replydata != "" ) reply . installedBy = replydata ;
mesh . SendCommand ( { action : 'msg' , type : 'service' , value : JSON . stringify ( reply ) , sessionid : data . sessionid } ) ;
delete pws ;
} ) ;
} else {
mesh . SendCommand ( { action : 'msg' , type : 'service' , value : JSON . stringify ( reply ) , sessionid : data . sessionid } ) ;
}
}
} catch ( ex ) {
mesh . SendCommand ( { action : 'msg' , type : 'service' , error : ex , sessionid : data . sessionid } )
}
}
2021-01-25 02:19:29 +03:00
case 'services' : {
// Return the list of installed services
var services = null ;
2022-01-17 00:37:26 +03:00
try { services = require ( 'service-manager' ) . manager . enumerateService ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
if ( services != null ) { mesh . SendCommand ( { action : 'msg' , type : 'services' , value : JSON . stringify ( services ) , sessionid : data . sessionid } ) ; }
break ;
}
case 'serviceStop' : {
// Stop a service
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var service = require ( 'service-manager' ) . manager . getService ( data . serviceName ) ;
if ( service != null ) { service . stop ( ) ; }
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
}
case 'serviceStart' : {
// Start a service
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var service = require ( 'service-manager' ) . manager . getService ( data . serviceName ) ;
if ( service != null ) { service . start ( ) ; }
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
}
case 'serviceRestart' : {
// Restart a service
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var service = require ( 'service-manager' ) . manager . getService ( data . serviceName ) ;
if ( service != null ) { service . restart ( ) ; }
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
}
case 'deskBackground' :
{
// Toggle desktop background
2021-01-25 08:51:22 +03:00
try {
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
var stype = require ( 'user-sessions' ) . getProcessOwnerName ( process . pid ) . tsid == 0 ? 1 : 0 ;
var sid = undefined ;
2021-01-25 08:51:22 +03:00
if ( stype == 1 ) {
if ( require ( 'MeshAgent' ) . _tsid != null ) {
2021-01-25 02:19:29 +03:00
stype = 5 ;
sid = require ( 'MeshAgent' ) . _tsid ;
2019-12-10 21:24:10 +03:00
}
}
2021-01-25 02:19:29 +03:00
var id = require ( 'user-sessions' ) . getProcessOwnerName ( process . pid ) . tsid == 0 ? 1 : 0 ;
var child = require ( 'child_process' ) . execFile ( process . execPath , [ process . execPath . split ( '\\' ) . pop ( ) , '-b64exec' , 'dmFyIFNQSV9HRVRERVNLV0FMTFBBUEVSID0gMHgwMDczOwp2YXIgU1BJX1NFVERFU0tXQUxMUEFQRVIgPSAweDAwMTQ7CnZhciBHTSA9IHJlcXVpcmUoJ19HZW5lcmljTWFyc2hhbCcpOwp2YXIgdXNlcjMyID0gR00uQ3JlYXRlTmF0aXZlUHJveHkoJ3VzZXIzMi5kbGwnKTsKdXNlcjMyLkNyZWF0ZU1ldGhvZCgnU3lzdGVtUGFyYW1ldGVyc0luZm9BJyk7CgppZiAocHJvY2Vzcy5hcmd2Lmxlbmd0aCA9PSAzKQp7CiAgICB2YXIgdiA9IEdNLkNyZWF0ZVZhcmlhYmxlKDEwMjQpOwogICAgdXNlcjMyLlN5c3RlbVBhcmFtZXRlcnNJbmZvQShTUElfR0VUREVTS1dBTExQQVBFUiwgdi5fc2l6ZSwgdiwgMCk7CiAgICBjb25zb2xlLmxvZyh2LlN0cmluZyk7CiAgICBwcm9jZXNzLmV4aXQoKTsKfQplbHNlCnsKICAgIHZhciBuYiA9IEdNLkNyZWF0ZVZhcmlhYmxlKHByb2Nlc3MuYXJndlszXSk7CiAgICB1c2VyMzIuU3lzdGVtUGFyYW1ldGVyc0luZm9BKFNQSV9TRVRERVNLV0FMTFBBUEVSLCBuYi5fc2l6ZSwgbmIsIDApOwogICAgcHJvY2Vzcy5leGl0KCk7Cn0=' ] , { type : stype , uid : sid } ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
var current = child . stdout . str . trim ( ) ;
if ( current != '' ) { require ( 'MeshAgent' ) . _wallpaper = current ; }
child = require ( 'child_process' ) . execFile ( process . execPath , [ process . execPath . split ( '\\' ) . pop ( ) , '-b64exec' , 'dmFyIFNQSV9HRVRERVNLV0FMTFBBUEVSID0gMHgwMDczOwp2YXIgU1BJX1NFVERFU0tXQUxMUEFQRVIgPSAweDAwMTQ7CnZhciBHTSA9IHJlcXVpcmUoJ19HZW5lcmljTWFyc2hhbCcpOwp2YXIgdXNlcjMyID0gR00uQ3JlYXRlTmF0aXZlUHJveHkoJ3VzZXIzMi5kbGwnKTsKdXNlcjMyLkNyZWF0ZU1ldGhvZCgnU3lzdGVtUGFyYW1ldGVyc0luZm9BJyk7CgppZiAocHJvY2Vzcy5hcmd2Lmxlbmd0aCA9PSAzKQp7CiAgICB2YXIgdiA9IEdNLkNyZWF0ZVZhcmlhYmxlKDEwMjQpOwogICAgdXNlcjMyLlN5c3RlbVBhcmFtZXRlcnNJbmZvQShTUElfR0VUREVTS1dBTExQQVBFUiwgdi5fc2l6ZSwgdiwgMCk7CiAgICBjb25zb2xlLmxvZyh2LlN0cmluZyk7CiAgICBwcm9jZXNzLmV4aXQoKTsKfQplbHNlCnsKICAgIHZhciBuYiA9IEdNLkNyZWF0ZVZhcmlhYmxlKHByb2Nlc3MuYXJndlszXSk7CiAgICB1c2VyMzIuU3lzdGVtUGFyYW1ldGVyc0luZm9BKFNQSV9TRVRERVNLV0FMTFBBUEVSLCBuYi5fc2l6ZSwgbmIsIDApOwogICAgcHJvY2Vzcy5leGl0KCk7Cn0=' , current != '' ? '""' : require ( 'MeshAgent' ) . _wallpaper ] , { type : stype , uid : sid } ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
2023-11-11 05:36:03 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'deskBackground' , sessionid : data . sessionid , data : ( current != '' ? "" : require ( 'MeshAgent' ) . _wallpaper ) , } ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var id = require ( 'user-sessions' ) . consoleUid ( ) ;
var current = require ( 'linux-gnome-helpers' ) . getDesktopWallpaper ( id ) ;
if ( current != '/dev/null' ) { require ( 'MeshAgent' ) . _wallpaper = current ; }
require ( 'linux-gnome-helpers' ) . setDesktopWallpaper ( id , current != '/dev/null' ? undefined : require ( 'MeshAgent' ) . _wallpaper ) ;
2023-11-11 05:36:03 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'deskBackground' , sessionid : data . sessionid , data : ( current != '/dev/null' ? "" : require ( 'MeshAgent' ) . _wallpaper ) , } ) ;
2019-11-28 23:27:44 +03:00
}
2022-01-17 00:37:26 +03:00
} catch ( ex ) {
2022-01-17 06:12:13 +03:00
sendConsoleText ( ex ) ;
2019-03-15 23:55:53 +03:00
}
2019-02-17 08:16:39 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'openUrl' : {
// Open a local web browser and return success/fail
MeshServerLogEx ( 20 , [ data . url ] , "Opening: " + data . url , data ) ;
sendConsoleText ( "OpenURL: " + data . url ) ;
if ( data . url ) { mesh . SendCommand ( { action : 'msg' , type : 'openUrl' , url : data . url , sessionid : data . sessionid , success : ( openUserDesktopUrl ( data . url ) != null ) } ) ; }
break ;
}
case 'getclip' : {
// Send the load clipboard back to the user
//sendConsoleText('getClip: ' + JSON.stringify(data));
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . isService ) {
require ( 'clipboard' ) . dispatchRead ( ) . then ( function ( str ) {
if ( str ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 21 , [ str . length ] , "Getting clipboard content, " + str . length + " byte(s)" , data ) ;
mesh . SendCommand ( { action : 'msg' , type : 'getclip' , sessionid : data . sessionid , data : str , tag : data . tag } ) ;
}
} ) ;
2021-01-25 08:51:22 +03:00
} else {
2022-08-28 01:47:19 +03:00
require ( 'clipboard' ) . read ( ) . then ( function ( str ) {
2021-01-25 08:51:22 +03:00
if ( str ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 21 , [ str . length ] , "Getting clipboard content, " + str . length + " byte(s)" , data ) ;
mesh . SendCommand ( { action : 'msg' , type : 'getclip' , sessionid : data . sessionid , data : str , tag : data . tag } ) ;
}
} ) ;
2019-02-17 08:16:39 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'setclip' : {
2022-08-28 21:32:54 +03:00
if ( pendingSetClip ) return ;
2021-01-25 02:19:29 +03:00
// Set the load clipboard to a user value
2022-08-04 11:52:29 +03:00
if ( typeof data . data == 'string' ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 22 , [ data . data . length ] , "Setting clipboard content, " + data . data . length + " byte(s)" , data ) ;
2022-08-04 11:52:29 +03:00
if ( require ( 'MeshAgent' ) . isService ) {
if ( process . platform != 'win32' ) {
2021-02-28 03:19:13 +03:00
require ( 'clipboard' ) . dispatchWrite ( data . data ) ;
2022-06-30 00:18:24 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'setclip' , sessionid : data . sessionid , success : true } ) ;
2021-02-28 03:19:13 +03:00
}
2022-08-04 11:52:29 +03:00
else {
2021-05-11 09:43:48 +03:00
var clipargs = data . data ;
2021-04-19 20:55:09 +03:00
var uid = require ( 'user-sessions' ) . consoleUid ( ) ;
2021-05-11 09:43:48 +03:00
var user = require ( 'user-sessions' ) . getUsername ( uid ) ;
2021-04-19 20:55:09 +03:00
var domain = require ( 'user-sessions' ) . getDomain ( uid ) ;
2021-05-11 09:43:48 +03:00
user = ( domain + '\\' + user ) ;
2022-08-28 01:01:22 +03:00
if ( this . _dispatcher ) { this . _dispatcher . close ( ) ; }
2021-05-11 09:43:48 +03:00
this . _dispatcher = require ( 'win-dispatcher' ) . dispatch ( { user : user , modules : [ { name : 'clip-dispatch' , script : "module.exports = { dispatch: function dispatch(val) { require('clipboard')(val); process.exit(); } };" } ] , launch : { module : 'clip-dispatch' , method : 'dispatch' , args : [ clipargs ] } } ) ;
this . _dispatcher . parent = this ;
//require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
2022-08-28 21:32:54 +03:00
pendingSetClip = true ;
2022-08-04 11:52:29 +03:00
this . _dispatcher . on ( 'connection' , function ( c ) {
2021-05-11 09:43:48 +03:00
this . _c = c ;
this . _c . root = this . parent ;
2022-08-26 06:11:47 +03:00
this . _c . on ( 'end' , function ( )
{
2022-08-28 21:32:54 +03:00
pendingSetClip = false ;
2022-08-28 01:47:19 +03:00
try { this . root . _dispatcher . close ( ) ; } catch ( ex ) { }
2021-05-11 09:43:48 +03:00
this . root . _dispatcher = null ;
this . root = null ;
2022-06-30 00:18:24 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'setclip' , sessionid : data . sessionid , success : true } ) ;
2021-05-11 09:43:48 +03:00
} ) ;
} ) ;
2021-02-28 03:19:13 +03:00
}
}
2022-08-04 11:52:29 +03:00
else {
2022-08-28 01:47:19 +03:00
require ( 'clipboard' ) ( data . data ) ;
2022-06-30 00:18:24 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'setclip' , sessionid : data . sessionid , success : true } ) ;
2021-02-28 03:19:13 +03:00
} // Set the clipboard
2019-12-13 04:45:42 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
case 'userSessions' : {
2022-03-29 08:15:45 +03:00
mesh . SendCommand ( { action : 'msg' , type : 'userSessions' , sessionid : data . sessionid , data : require ( 'kvm-helper' ) . users ( ) , tag : data . tag } ) ;
2021-01-25 02:19:29 +03:00
break ;
2019-07-30 02:35:48 +03:00
}
2021-01-25 02:19:29 +03:00
case 'cpuinfo' :
// CPU & memory utilization
var cpuuse = require ( 'sysinfo' ) . cpuUtilization ( ) ;
cpuuse . sessionid = data . sessionid ;
cpuuse . tag = data . tag ;
2021-01-25 08:51:22 +03:00
cpuuse . then ( function ( data ) {
2021-01-25 02:19:29 +03:00
mesh . SendCommand ( JSON . stringify (
{
action : 'msg' ,
type : 'cpuinfo' ,
cpu : data ,
memory : require ( 'sysinfo' ) . memUtilization ( ) ,
thermals : require ( 'sysinfo' ) . thermals == null ? [ ] : require ( 'sysinfo' ) . thermals ( ) ,
sessionid : this . sessionid ,
tag : this . tag
} ) ) ;
} , function ( ex ) { } ) ;
break ;
case 'localapp' :
// Send a message to a local application
sendConsoleText ( 'localappMsg: ' + data . appid + ', ' + JSON . stringify ( data . value ) ) ;
if ( data . appid != null ) { sendToRegisteredApp ( data . appid , data . value ) ; } else { broadcastToRegisteredApps ( data . value ) ; }
break ;
2023-12-14 17:24:55 +03:00
case 'alertbox' : {
// Display an old style alert box
if ( data . title && data . msg ) {
2023-12-14 18:51:11 +03:00
MeshServerLogEx ( 158 , [ data . title , data . msg ] , "Displaying alert box, title=" + data . title + ", message=" + data . msg , data ) ;
2023-12-14 17:24:55 +03:00
try { require ( 'message-box' ) . create ( data . title , data . msg , 9999 , 1 ) . then ( function ( ) { } ) . catch ( function ( ) { } ) ; } catch ( ex ) { }
}
break ;
}
2021-01-25 02:19:29 +03:00
default :
// Unknown action, ignore it.
break ;
2019-06-20 03:16:50 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'acmactivate' : {
2021-01-25 08:51:22 +03:00
if ( amt != null ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 23 , null , "Attempting Intel AMT ACM mode activation" , data ) ;
amt . setAcmResponse ( data ) ;
2017-08-28 22:48:53 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'wakeonlan' : {
// Send wake-on-lan on all interfaces for all MAC addresses in data.macs array. The array is a list of HEX MAC addresses.
//sendConsoleText("Server requesting wake-on-lan for: " + data.macs.join(', '));
2021-04-24 20:20:35 +03:00
sendWakeOnLanEx ( data . macs ) ;
2021-04-24 20:34:29 +03:00
sendWakeOnLanEx ( data . macs ) ;
sendWakeOnLanEx ( data . macs ) ;
2021-01-25 02:19:29 +03:00
break ;
}
case 'runcommands' : {
if ( mesh . cmdchild != null ) { sendConsoleText ( "Run commands can't execute, already busy." ) ; break ; }
2024-03-14 17:33:02 +03:00
if ( ! data . reply ) sendConsoleText ( "Run commands (" + data . runAsUser + "): " + data . cmds ) ;
2020-09-29 23:20:08 +03:00
2021-01-25 02:19:29 +03:00
// data.runAsUser: 0=Agent,1=UserOrAgent,2=UserOnly
var options = { } ;
2021-01-25 08:51:22 +03:00
if ( data . runAsUser > 0 ) {
2022-01-17 00:37:26 +03:00
try { options . uid = require ( 'user-sessions' ) . consoleUid ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
options . type = require ( 'child_process' ) . SpawnTypes . TERM ;
}
2021-01-25 08:51:22 +03:00
if ( data . runAsUser == 2 ) {
2021-01-25 02:19:29 +03:00
if ( options . uid == null ) break ;
if ( ( ( require ( 'user-sessions' ) . minUid != null ) && ( options . uid < require ( 'user-sessions' ) . minUid ( ) ) ) ) break ; // This command can only run as user.
}
2023-10-28 21:28:30 +03:00
var replydata = "" ;
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
if ( data . type == 1 ) {
2021-01-25 02:19:29 +03:00
// Windows command shell
mesh . cmdchild = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\system32\\cmd.exe' , [ 'cmd' ] , options ) ;
2020-07-11 09:12:43 +03:00
mesh . cmdchild . descriptorMetadata = 'UserCommandsShell' ;
2023-10-28 21:28:30 +03:00
mesh . cmdchild . stdout . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
mesh . cmdchild . stderr . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
2021-01-25 02:19:29 +03:00
mesh . cmdchild . stdin . write ( data . cmds + '\r\nexit\r\n' ) ;
2024-03-14 17:33:02 +03:00
mesh . cmdchild . on ( 'exit' , function ( ) {
if ( data . reply ) {
mesh . SendCommand ( { action : 'msg' , type : 'runcommands' , result : replydata , sessionid : data . sessionid , responseid : data . responseid } ) ;
} else {
sendConsoleText ( replydata ) ;
sendConsoleText ( "Run commands completed." ) ;
}
delete mesh . cmdchild ;
} ) ;
2021-01-25 08:51:22 +03:00
} else if ( data . type == 2 ) {
2021-01-25 02:19:29 +03:00
// Windows Powershell
mesh . cmdchild = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' , [ 'powershell' , '-noprofile' , '-nologo' , '-command' , '-' ] , options ) ;
mesh . cmdchild . descriptorMetadata = 'UserCommandsPowerShell' ;
2023-10-28 21:28:30 +03:00
mesh . cmdchild . stdout . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
mesh . cmdchild . stderr . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
2021-01-25 02:19:29 +03:00
mesh . cmdchild . stdin . write ( data . cmds + '\r\nexit\r\n' ) ;
2024-03-14 17:33:02 +03:00
mesh . cmdchild . on ( 'exit' , function ( ) {
if ( data . reply ) {
mesh . SendCommand ( { action : 'msg' , type : 'runcommands' , result : replydata , sessionid : data . sessionid , responseid : data . responseid } ) ;
} else {
sendConsoleText ( replydata ) ;
sendConsoleText ( "Run commands completed." ) ;
}
delete mesh . cmdchild ;
} ) ;
2020-07-11 09:12:43 +03:00
}
2021-01-25 08:51:22 +03:00
} else if ( data . type == 3 ) {
2021-01-25 02:19:29 +03:00
// Linux shell
mesh . cmdchild = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] , options ) ;
mesh . cmdchild . descriptorMetadata = 'UserCommandsShell' ;
2023-10-28 21:28:30 +03:00
mesh . cmdchild . stdout . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
mesh . cmdchild . stderr . on ( 'data' , function ( c ) { replydata + c . toString ( ) ; } ) ;
2021-01-25 02:19:29 +03:00
mesh . cmdchild . stdin . write ( data . cmds . split ( '\r' ) . join ( '' ) + '\nexit\n' ) ;
2024-03-14 17:33:02 +03:00
mesh . cmdchild . on ( 'exit' , function ( ) {
if ( data . reply ) {
mesh . SendCommand ( { action : 'msg' , type : 'runcommands' , result : replydata , sessionid : data . sessionid , responseid : data . responseid } ) ;
} else {
sendConsoleText ( replydata ) ;
sendConsoleText ( "Run commands completed." ) ;
}
delete mesh . cmdchild ;
} ) ;
2018-04-20 04:19:15 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'uninstallagent' :
// Uninstall this agent
var agentName = process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
agentName = require ( 'MeshAgent' ) . serviceName ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
2021-01-25 08:51:22 +03:00
if ( require ( 'service-manager' ) . manager . getService ( agentName ) . isMe ( ) ) {
2022-01-17 00:37:26 +03:00
try { diagnosticAgent _uninstall ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
var js = "require('service-manager').manager.getService('" + agentName + "').stop(); require('service-manager').manager.uninstallService('" + agentName + "'); process.exit();" ;
this . child = require ( 'child_process' ) . execFile ( process . execPath , [ process . platform == 'win32' ? ( process . execPath . split ( '\\' ) . pop ( ) ) : ( process . execPath . split ( '/' ) . pop ( ) ) , '-b64exec' , Buffer . from ( js ) . toString ( 'base64' ) ] , { type : 4 , detached : true } ) ;
2019-02-08 07:06:01 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'poweraction' : {
// Server telling us to execute a power action
2021-01-25 08:51:22 +03:00
if ( ( mesh . ExecPowerState != undefined ) && ( data . actiontype ) ) {
2021-01-25 02:19:29 +03:00
var forced = 0 ;
if ( data . forced == 1 ) { forced = 1 ; }
data . actiontype = parseInt ( data . actiontype ) ;
MeshServerLogEx ( 25 , [ data . actiontype , forced ] , "Performing power action=" + data . actiontype + ", forced=" + forced , data ) ;
sendConsoleText ( "Performing power action=" + data . actiontype + ", forced=" + forced + '.' ) ;
var r = mesh . ExecPowerState ( data . actiontype , forced ) ;
sendConsoleText ( "ExecPowerState returned code: " + r ) ;
2019-08-07 03:58:29 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'iplocation' : {
// Update the IP location information of this node. Only do this when requested by the server since we have a limited amount of time we can call this per day
getIpLocationData ( function ( location ) { mesh . SendCommand ( { action : 'iplocation' , type : 'publicip' , value : location } ) ; } ) ;
break ;
}
case 'toast' : {
// Display a toast message
2021-01-25 08:51:22 +03:00
if ( data . title && data . msg ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 26 , [ data . title , data . msg ] , "Displaying toast message, title=" + data . title + ", message=" + data . msg , data ) ;
data . msg = data . msg . split ( '\r' ) . join ( '\\r' ) . split ( '\n' ) . join ( '\\n' ) ;
2022-01-17 00:37:26 +03:00
try { require ( 'toaster' ) . Toast ( data . title , data . msg ) ; } catch ( ex ) { }
2019-10-10 21:13:25 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'openUrl' : {
// Open a local web browser and return success/fail
//sendConsoleText('OpenURL: ' + data.url);
MeshServerLogEx ( 20 , [ data . url ] , "Opening: " + data . url , data ) ;
if ( data . url ) { mesh . SendCommand ( { action : 'openUrl' , url : data . url , sessionid : data . sessionid , success : ( openUserDesktopUrl ( data . url ) != null ) } ) ; }
break ;
}
case 'amtconfig' : {
// Perform Intel AMT activation and/or configuration
if ( ( apftunnel != null ) || ( amt == null ) || ( typeof data . user != 'string' ) || ( typeof data . pass != 'string' ) ) break ;
2021-01-25 08:51:22 +03:00
amt . getMeiState ( 15 , function ( state ) {
2021-01-25 02:19:29 +03:00
if ( ( apftunnel != null ) || ( amt == null ) ) return ;
if ( ( state == null ) || ( state . ProvisioningState == null ) ) return ;
if ( ( state . UUID == null ) || ( state . UUID . length != 36 ) ) return ; // Bad UUID
2021-07-24 20:56:53 +03:00
getAmtOsDnsSuffix ( state , function ( ) {
var apfarg = {
mpsurl : mesh . ServerUrl . replace ( '/agent.ashx' , '/apf.ashx' ) ,
mpsuser : data . user , // Agent user name
mpspass : data . pass , // Encrypted login cookie
mpskeepalive : 60000 ,
clientname : state . OsHostname ,
clientaddress : '127.0.0.1' ,
clientuuid : state . UUID ,
conntype : 2 , // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
meiState : state // MEI state will be passed to MPS server
} ;
addAmtEvent ( 'LMS tunnel start.' ) ;
apftunnel = require ( 'amt-apfclient' ) ( { debug : false } , apfarg ) ;
apftunnel . onJsonControl = handleApfJsonControl ;
apftunnel . onChannelClosed = function ( ) { addAmtEvent ( 'LMS tunnel closed.' ) ; apftunnel = null ; }
try { apftunnel . connect ( ) ; } catch ( ex ) { }
} ) ;
2021-01-25 02:19:29 +03:00
} ) ;
break ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
case 'getScript' : {
// Received a configuration script from the server
sendConsoleText ( 'getScript: ' + JSON . stringify ( data ) ) ;
break ;
2020-11-10 03:51:48 +03:00
}
2021-01-25 02:19:29 +03:00
case 'sysinfo' : {
// Fetch system information
2021-01-25 08:51:22 +03:00
getSystemInformation ( function ( results ) {
2021-01-25 02:19:29 +03:00
if ( ( results != null ) && ( data . hash != results . hash ) ) { mesh . SendCommand ( { action : 'sysinfo' , sessionid : this . sessionid , data : results } ) ; }
} ) ;
break ;
2019-09-24 02:46:26 +03:00
}
2021-01-25 02:19:29 +03:00
case 'ping' : { mesh . SendCommand ( '{"action":"pong"}' ) ; break ; }
case 'pong' : { break ; }
case 'plugin' : {
2022-01-17 00:37:26 +03:00
try { require ( data . plugin ) . consoleaction ( data , data . rights , data . sessionid , this ) ; } catch ( ex ) { throw ex ; }
2021-01-25 02:19:29 +03:00
break ;
}
case 'coredump' :
2021-03-06 01:58:00 +03:00
// Set the current agent coredump situation.s
2021-01-25 08:51:22 +03:00
if ( data . value === true ) {
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
// TODO: This replace() below is not ideal, would be better to remove the .exe at the end instead of replace.
process . coreDumpLocation = process . execPath . replace ( '.exe' , '.dmp' ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
process . coreDumpLocation = ( process . cwd ( ) != '//' ) ? ( process . cwd ( ) + 'core' ) : null ;
2021-01-20 23:49:15 +03:00
}
2021-01-25 08:51:22 +03:00
} else if ( data . value === false ) {
2021-01-25 02:19:29 +03:00
process . coreDumpLocation = null ;
2021-01-20 23:49:15 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'getcoredump' :
// Ask the agent if a core dump is currently available, if yes, also return the hash of the agent.
var r = { action : 'getcoredump' , value : ( process . coreDumpLocation != null ) } ;
var coreDumpPath = null ;
if ( process . platform == 'win32' ) { coreDumpPath = process . coreDumpLocation ; } else { coreDumpPath = ( process . cwd ( ) != '//' ) ? fs . existsSync ( process . cwd ( ) + 'core' ) : null ; }
2021-01-25 08:51:22 +03:00
if ( ( coreDumpPath != null ) && ( fs . existsSync ( coreDumpPath ) ) ) {
try {
2021-01-25 02:19:29 +03:00
var coredate = fs . statSync ( coreDumpPath ) . mtime ;
var coretime = new Date ( coredate ) . getTime ( ) ;
var agenttime = new Date ( fs . statSync ( process . execPath ) . mtime ) . getTime ( ) ;
if ( coretime > agenttime ) { r . exists = ( db . Get ( 'CoreDumpTime' ) != coredate ) ; }
} catch ( ex ) { }
2017-08-28 19:27:45 +03:00
}
2021-01-25 08:51:22 +03:00
if ( r . exists == true ) {
2021-01-25 02:19:29 +03:00
r . agenthashhex = getSHA384FileHash ( process . execPath ) . toString ( 'hex' ) ; // Hash of current agent
r . corehashhex = getSHA384FileHash ( coreDumpPath ) . toString ( 'hex' ) ; // Hash of core dump file
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
mesh . SendCommand ( JSON . stringify ( r ) ) ;
break ;
case 'meshToolInfo' :
if ( data . pipe == true ) { delete data . pipe ; delete data . action ; data . cmd = 'meshToolInfo' ; broadcastToRegisteredApps ( data ) ; }
2021-04-27 10:53:04 +03:00
if ( data . tag == 'info' ) { sendConsoleText ( JSON . stringify ( data , null , 2 ) ) ; }
if ( data . tag == 'install' ) {
data . func = function ( options , success ) {
2021-07-24 19:28:41 +03:00
sendConsoleText ( 'Download of MeshCentral Assistant ' + ( success ? 'succeed' : 'failed' ) ) ;
2021-04-27 10:53:04 +03:00
if ( success ) {
// TODO: Install & Run
}
}
data . filename = 'MeshAssistant.exe' ;
downloadFile ( data ) ;
}
2021-01-25 02:19:29 +03:00
break ;
2021-05-16 11:25:58 +03:00
case 'getUserImage' :
if ( data . pipe == true ) { delete data . pipe ; delete data . action ; data . cmd = 'getUserImage' ; broadcastToRegisteredApps ( data ) ; }
if ( data . tag == 'info' ) { sendConsoleText ( JSON . stringify ( data , null , 2 ) ) ; }
2022-01-17 00:37:26 +03:00
if ( data . promise != null && require ( 'MeshAgent' ) . _promises [ data . promise ] != null ) {
2021-12-14 04:37:22 +03:00
var p = require ( 'MeshAgent' ) . _promises [ data . promise ] ;
delete require ( 'MeshAgent' ) . _promises [ data . promise ] ;
p . resolve ( data . image ) ;
}
2021-05-16 11:25:58 +03:00
break ;
2021-01-25 02:19:29 +03:00
case 'wget' : // Server uses this command to tell the agent to download a file using HTTPS/GET and place it in a given path. This is used for one-to-many file uploads.
agentFileHttpPendingRequests . push ( data ) ;
serverFetchFile ( ) ;
break ;
2021-08-20 00:10:03 +03:00
case 'serverInfo' : // Server information
obj . serverInfo = data ;
delete obj . serverInfo . action ;
break ;
2021-04-18 00:44:19 +03:00
case 'errorlog' : // Return agent error log
try { mesh . SendCommand ( JSON . stringify ( { action : 'errorlog' , log : require ( 'util-agentlog' ) . read ( data . startTime ) } ) ) ; } catch ( ex ) { }
break ;
2021-01-25 02:19:29 +03:00
default :
// Unknown action, ignore it.
break ;
2017-08-28 19:27:45 +03:00
}
}
2021-01-25 02:19:29 +03:00
}
2021-07-24 20:56:53 +03:00
// On non-Windows platforms, we need to query the DHCP server for the DNS suffix
function getAmtOsDnsSuffix ( mestate , func ) {
if ( ( process . platform == 'win32' ) || ( mestate . net0 == null ) || ( mestate . net0 . mac == null ) ) { func ( mestate ) ; return ; }
try { require ( 'linux-dhcp' ) } catch ( ex ) { func ( mestate ) ; return ; }
require ( 'linux-dhcp' ) . client . info ( mestate . net0 . mac ) . then ( function ( d ) {
if ( ( typeof d . options == 'object' ) && ( typeof d . options . domainname == 'string' ) ) { mestate . OsDnsSuffix = d . options . domainname ; }
func ( mestate ) ;
} , function ( e ) {
console . log ( 'DHCP error' , e ) ;
func ( mestate ) ;
} ) ;
}
2021-04-27 10:53:04 +03:00
// Download a file from the server and check the hash.
// This download is similar to the one used for meshcore self-update.
var trustedDownloads = { } ;
function downloadFile ( downloadoptions ) {
var options = require ( 'http' ) . parseUri ( downloadoptions . url ) ;
options . rejectUnauthorized = false ;
options . checkServerIdentity = function checkServerIdentity ( certs ) {
// If the tunnel certificate matches the control channel certificate, accept the connection
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . digest == certs [ 0 ] . digest ) return ; } catch ( ex ) { }
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . fingerprint == certs [ 0 ] . fingerprint ) return ; } catch ( ex ) { }
// Check that the certificate is the one expected by the server, fail if not.
if ( checkServerIdentity . servertlshash == null ) { if ( require ( 'MeshAgent' ) . ServerInfo == null || require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate == null ) return ; throw new Error ( 'BadCert' ) ; }
if ( certs [ 0 ] . digest == null ) return ;
if ( ( checkServerIdentity . servertlshash != null ) && ( checkServerIdentity . servertlshash . toLowerCase ( ) != certs [ 0 ] . digest . split ( ':' ) . join ( '' ) . toLowerCase ( ) ) ) { throw new Error ( 'BadCert' ) }
}
//options.checkServerIdentity.servertlshash = downloadoptions.serverhash;
trustedDownloads [ downloadoptions . name ] = downloadoptions ;
trustedDownloads [ downloadoptions . name ] . dl = require ( 'https' ) . get ( options ) ;
trustedDownloads [ downloadoptions . name ] . dl . on ( 'error' , function ( e ) { downloadoptions . func ( downloadoptions , false ) ; delete trustedDownloads [ downloadoptions . name ] ; } ) ;
trustedDownloads [ downloadoptions . name ] . dl . on ( 'response' , function ( img ) {
this . _file = require ( 'fs' ) . createWriteStream ( trustedDownloads [ downloadoptions . name ] . filename , { flags : 'wb' } ) ;
this . _filehash = require ( 'SHA384Stream' ) . create ( ) ;
this . _filehash . on ( 'hash' , function ( h ) { if ( ( downloadoptions . hash != null ) && ( downloadoptions . hash . toLowerCase ( ) != h . toString ( 'hex' ) . toLowerCase ( ) ) ) { downloadoptions . func ( downloadoptions , false ) ; delete trustedDownloads [ downloadoptions . name ] ; return ; } downloadoptions . func ( downloadoptions , true ) ; } ) ;
img . pipe ( this . _file ) ;
img . pipe ( this . _filehash ) ;
} ) ;
}
2021-03-06 04:45:17 +03:00
// Handle APF JSON control commands
function handleApfJsonControl ( data ) {
if ( data . action == 'console' ) { addAmtEvent ( data . msg ) ; } // Add console message to AMT event log
if ( data . action == 'mestate' ) { amt . getMeiState ( 15 , function ( state ) { apftunnel . updateMeiState ( state ) ; } ) ; } // Update the MEI state
2022-01-17 00:37:26 +03:00
if ( data . action == 'close' ) { try { apftunnel . disconnect ( ) ; } catch ( ex ) { } apftunnel = null ; } // Close the CIRA-LMS connection
2021-03-06 04:45:17 +03:00
if ( amt . amtMei != null ) {
if ( data . action == 'deactivate' ) { // Request CCM deactivation
amt . amtMei . unprovision ( 1 , function ( status ) { if ( apftunnel ) apftunnel . sendMeiDeactivationState ( status ) ; } ) ; // 0 = Success
}
if ( data . action == 'startTlsHostConfig' ) { // Request start of host based TLS ACM activation
amt . amtMei . startConfigurationHBased ( Buffer . from ( data . hash , 'hex' ) , data . hostVpn , data . dnsSuffixList , function ( response ) { apftunnel . sendStartTlsHostConfigResponse ( response ) ; } ) ;
}
if ( data . action == 'stopConfiguration' ) { // Request Intel AMT stop configuration.
amt . amtMei . stopConfiguration ( function ( status ) { apftunnel . sendStopConfigurationResponse ( status ) ; } ) ;
}
}
}
2021-01-25 02:19:29 +03:00
// Agent just get a file from the server and save it locally.
2021-01-25 08:51:22 +03:00
function serverFetchFile ( ) {
2021-01-25 02:19:29 +03:00
if ( ( Object . keys ( agentFileHttpRequests ) . length > 4 ) || ( agentFileHttpPendingRequests . length == 0 ) ) return ; // No more than 4 active HTTPS requests to the server.
var data = agentFileHttpPendingRequests . shift ( ) ;
if ( ( data . overwrite !== true ) && fs . existsSync ( data . path ) ) return ; // Don't overwrite an existing file.
if ( data . createFolder ) { try { fs . mkdirSync ( data . folder ) ; } catch ( ex ) { } } // If requested, create the local folder.
data . url = 'http' + getServerTargetUrlEx ( '*/' ) . substring ( 2 ) ;
var agentFileHttpOptions = http . parseUri ( data . url ) ;
agentFileHttpOptions . path = data . urlpath ;
// Perform manual server TLS certificate checking based on the certificate hash given by the server.
agentFileHttpOptions . rejectUnauthorized = 0 ;
2021-01-25 08:51:22 +03:00
agentFileHttpOptions . checkServerIdentity = function checkServerIdentity ( certs ) {
2021-01-25 02:19:29 +03:00
// If the tunnel certificate matches the control channel certificate, accept the connection
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . digest == certs [ 0 ] . digest ) return ; } catch ( ex ) { }
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . fingerprint == certs [ 0 ] . fingerprint ) return ; } catch ( ex ) { }
// Check that the certificate is the one expected by the server, fail if not.
if ( ( checkServerIdentity . servertlshash != null ) && ( checkServerIdentity . servertlshash . toLowerCase ( ) != certs [ 0 ] . digest . split ( ':' ) . join ( '' ) . toLowerCase ( ) ) ) { throw new Error ( 'BadCert' ) }
}
agentFileHttpOptions . checkServerIdentity . servertlshash = data . servertlshash ;
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
if ( agentFileHttpOptions == null ) return ;
var agentFileHttpRequest = http . request ( agentFileHttpOptions ,
2021-01-25 08:51:22 +03:00
function ( response ) {
2021-01-25 02:19:29 +03:00
response . xparent = this ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
response . xfile = fs . createWriteStream ( this . xpath , { flags : 'wbN' } )
response . pipe ( response . xfile ) ;
response . end = function ( ) { delete agentFileHttpRequests [ this . xparent . xurlpath ] ; delete this . xparent ; serverFetchFile ( ) ; }
} catch ( ex ) { delete agentFileHttpRequests [ this . xurlpath ] ; delete response . xparent ; serverFetchFile ( ) ; return ; }
2020-02-01 08:35:47 +03:00
}
2021-01-25 02:19:29 +03:00
) ;
2021-06-15 05:12:19 +03:00
agentFileHttpRequest . on ( 'error' , function ( ex ) { sendConsoleText ( ex ) ; delete agentFileHttpRequests [ this . xurlpath ] ; serverFetchFile ( ) ; } ) ;
2021-01-25 02:19:29 +03:00
agentFileHttpRequest . end ( ) ;
agentFileHttpRequest . xurlpath = data . urlpath ;
agentFileHttpRequest . xpath = data . path ;
agentFileHttpRequests [ data . urlpath ] = agentFileHttpRequest ;
}
2020-02-01 08:35:47 +03:00
2021-01-25 02:19:29 +03:00
// Called when a file changed in the file system
/ *
function onFileWatcher ( a , b ) {
console . log ( 'onFileWatcher' , a , b , this . path ) ;
var response = getDirectoryInfo ( this . path ) ;
if ( ( response != undefined ) && ( response != null ) ) { this . tunnel . s . write ( JSON . stringify ( response ) ) ; }
}
* /
2022-08-31 01:30:09 +03:00
// Replace all key name spaces with _ in an object recursively.
2023-12-23 02:18:17 +03:00
// This is a workaround since require('computer-identifiers').get() returns key names with spaces in them on Linux.
2022-08-31 01:30:09 +03:00
function replaceSpacesWithUnderscoresRec ( o ) {
if ( typeof o != 'object' ) return ;
for ( var i in o ) { if ( i . indexOf ( ' ' ) >= 0 ) { o [ i . split ( ' ' ) . join ( '_' ) ] = o [ i ] ; delete o [ i ] ; } replaceSpacesWithUnderscoresRec ( o [ i ] ) ; }
}
2021-01-25 08:51:22 +03:00
function getSystemInformation ( func ) {
2021-04-24 20:53:13 +03:00
try {
2023-12-23 02:18:17 +03:00
var results = { hardware : require ( 'computer-identifiers' ) . get ( ) } ; // Hardware info
2021-01-25 08:51:22 +03:00
if ( results . hardware && results . hardware . windows ) {
2021-01-25 02:19:29 +03:00
// Remove extra entries and things that change quickly
var x = results . hardware . windows . osinfo ;
2022-01-17 00:37:26 +03:00
try { delete x . FreePhysicalMemory ; } catch ( ex ) { }
try { delete x . FreeSpaceInPagingFiles ; } catch ( ex ) { }
try { delete x . FreeVirtualMemory ; } catch ( ex ) { }
try { delete x . LocalDateTime ; } catch ( ex ) { }
try { delete x . MaxProcessMemorySize ; } catch ( ex ) { }
try { delete x . TotalVirtualMemorySize ; } catch ( ex ) { }
try { delete x . TotalVisibleMemorySize ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
if ( results . hardware . windows . memory ) { for ( var i in results . hardware . windows . memory ) { delete results . hardware . windows . memory [ i ] . Node ; } }
if ( results . hardware . windows . osinfo ) { delete results . hardware . windows . osinfo . Node ; }
if ( results . hardware . windows . partitions ) { for ( var i in results . hardware . windows . partitions ) { delete results . hardware . windows . partitions [ i ] . Node ; } }
2022-01-17 00:37:26 +03:00
} catch ( ex ) { }
2023-10-08 16:27:39 +03:00
if ( ! results . hardware . identifiers [ 'bios_serial' ] ) {
try {
var values = require ( 'win-wmi' ) . query ( 'ROOT\\CIMV2' , "SELECT * FROM Win32_Bios" , [ 'SerialNumber' ] ) ;
results . hardware . identifiers [ 'bios_serial' ] = values [ 0 ] [ 'SerialNumber' ] ;
} catch ( ex ) { }
}
if ( ! results . hardware . identifiers [ 'bios_mode' ] ) {
try {
results . hardware . identifiers [ 'bios_mode' ] = 'Legacy' ;
for ( var i in results . hardware . windows . partitions ) {
if ( results . hardware . windows . partitions [ i ] . Description == 'GPT: System' ) {
results . hardware . identifiers [ 'bios_mode' ] = 'UEFI' ;
}
2023-10-08 13:47:51 +03:00
}
2023-10-08 16:27:39 +03:00
} catch ( ex ) { results . hardware . identifiers [ 'bios_mode' ] = 'Legacy' ; }
}
2023-11-04 21:44:34 +03:00
if ( ! results . hardware . tpm ) {
IntToStr = function ( v ) { return String . fromCharCode ( ( v >> 24 ) & 0xFF , ( v >> 16 ) & 0xFF , ( v >> 8 ) & 0xFF , v & 0xFF ) ; } ;
try {
var values = require ( 'win-wmi' ) . query ( 'ROOT\\CIMV2\\Security\\MicrosoftTpm' , "SELECT * FROM Win32_Tpm" , [ 'IsActivated_InitialValue' , 'IsEnabled_InitialValue' , 'IsOwned_InitialValue' , 'ManufacturerId' , 'ManufacturerVersion' , 'SpecVersion' ] ) ;
if ( values [ 0 ] ) {
results . hardware . tpm = {
SpecVersion : values [ 0 ] . SpecVersion . split ( "," ) [ 0 ] ,
ManufacturerId : IntToStr ( values [ 0 ] . ManufacturerId ) . replace ( /[^\x00-\x7F]/g , "" ) ,
ManufacturerVersion : values [ 0 ] . ManufacturerVersion ,
IsActivated : values [ 0 ] . IsActivated _InitialValue ,
IsEnabled : values [ 0 ] . IsEnabled _InitialValue ,
IsOwned : values [ 0 ] . IsOwned _InitialValue ,
}
}
} catch ( ex ) { }
}
2021-01-25 02:19:29 +03:00
}
2023-10-04 22:51:59 +03:00
if ( results . hardware && results . hardware . linux ) {
2023-10-08 16:27:39 +03:00
if ( ! results . hardware . identifiers [ 'bios_serial' ] ) {
2023-12-23 02:18:17 +03:00
try {
if ( require ( 'fs' ) . statSync ( '/sys/class/dmi/id/product_serial' ) . isFile ( ) ) {
results . hardware . identifiers [ 'bios_serial' ] = require ( 'fs' ) . readFileSync ( '/sys/class/dmi/id/product_serial' ) . toString ( ) . trim ( ) ;
}
} catch ( ex ) { }
2023-10-08 16:27:39 +03:00
}
if ( ! results . hardware . identifiers [ 'bios_mode' ] ) {
try {
results . hardware . identifiers [ 'bios_mode' ] = ( require ( 'fs' ) . statSync ( '/sys/firmware/efi' ) . isDirectory ( ) ? 'UEFI' : 'Legacy' ) ;
} catch ( ex ) { results . hardware . identifiers [ 'bios_mode' ] = 'Legacy' ; }
2023-10-04 22:51:59 +03:00
}
2023-11-04 21:44:34 +03:00
if ( ! results . hardware . tpm ) {
IntToStr = function ( v ) { return String . fromCharCode ( ( v >> 24 ) & 0xFF , ( v >> 16 ) & 0xFF , ( v >> 8 ) & 0xFF , v & 0xFF ) ; } ;
try {
if ( require ( 'fs' ) . statSync ( '/sys/class/tpm/tpm0' ) . isDirectory ( ) ) {
results . hardware . tpm = {
SpecVersion : require ( 'fs' ) . readFileSync ( '/sys/class/tpm/tpm0/tpm_version_major' ) . toString ( ) . trim ( )
}
}
} catch ( ex ) { }
}
2023-11-26 21:15:49 +03:00
if ( ! results . hardware . linux . LastBootUpTime ) {
try {
var child = require ( 'child_process' ) . execFile ( '/usr/bin/uptime' , [ '' , '-s' ] ) ; // must include blank value at begining for some reason?
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
results . hardware . linux . LastBootUpTime = child . stdout . str . trim ( ) ;
} catch ( ex ) { }
}
}
if ( process . platform == 'darwin' ) {
try {
var child = require ( 'child_process' ) . execFile ( '/usr/sbin/sysctl' , [ '' , 'kern.boottime' ] ) ; // must include blank value at begining for some reason?
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
const timestampMatch = /\{ sec = (\d+), usec = \d+ \}/ . exec ( child . stdout . str . trim ( ) ) ;
if ( ! results . hardware . darwin ) {
results . hardware . darwin = { LastBootUpTime : parseInt ( timestampMatch [ 1 ] ) } ;
} else {
results . hardware . darwin . LastBootUpTime = parseInt ( timestampMatch [ 1 ] ) ;
}
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
2021-01-26 23:08:17 +03:00
results . hardware . agentvers = process . versions ;
2024-01-19 00:42:17 +03:00
results . hardware . network = { dns : require ( 'os' ) . dns ( ) } ;
2022-08-31 01:30:09 +03:00
replaceSpacesWithUnderscoresRec ( results ) ;
2021-04-02 07:51:25 +03:00
var hasher = require ( 'SHA384Stream' ) . create ( ) ;
2021-01-25 02:19:29 +03:00
2021-02-09 00:52:22 +03:00
// On Windows platforms, get volume information - Needs more testing.
2021-02-05 10:24:58 +03:00
if ( process . platform == 'win32' )
{
results . pendingReboot = require ( 'win-info' ) . pendingReboot ( ) ; // Pending reboot
2023-12-23 02:18:17 +03:00
if ( require ( 'computer-identifiers' ) . volumes _promise != null )
2021-02-05 10:24:58 +03:00
{
2023-12-23 02:18:17 +03:00
var p = require ( 'computer-identifiers' ) . volumes _promise ( ) ;
2021-02-05 10:24:58 +03:00
p . then ( function ( res )
{
2024-02-18 04:51:15 +03:00
results . hardware . windows . volumes = cleanGetBitLockerVolumeInfo ( res ) ;
2021-04-02 07:51:25 +03:00
results . hash = hasher . syncHash ( JSON . stringify ( results ) ) . toString ( 'hex' ) ;
2021-01-25 02:19:29 +03:00
func ( results ) ;
2021-02-05 10:24:58 +03:00
} ) ;
2020-07-25 00:00:49 +03:00
}
2023-12-23 02:18:17 +03:00
else if ( require ( 'computer-identifiers' ) . volumes != null )
2021-02-05 10:24:58 +03:00
{
2024-02-18 04:51:15 +03:00
results . hardware . windows . volumes = cleanGetBitLockerVolumeInfo ( require ( 'computer-identifiers' ) . volumes ( ) ) ;
2021-04-02 07:51:25 +03:00
results . hash = hasher . syncHash ( JSON . stringify ( results ) ) . toString ( 'hex' ) ;
2021-02-05 10:24:58 +03:00
func ( results ) ;
}
else
{
2021-04-02 07:51:25 +03:00
results . hash = hasher . syncHash ( JSON . stringify ( results ) ) . toString ( 'hex' ) ;
2021-02-05 10:24:58 +03:00
func ( results ) ;
}
}
else
{
2021-04-02 07:51:25 +03:00
results . hash = hasher . syncHash ( JSON . stringify ( results ) ) . toString ( 'hex' ) ;
2021-02-05 10:24:58 +03:00
func ( results ) ;
}
2023-10-01 21:43:23 +03:00
2022-01-17 06:12:13 +03:00
} catch ( ex ) { func ( null , ex ) ; }
2021-01-25 02:19:29 +03:00
}
// Get a formated response for a given directory path
2021-01-25 08:51:22 +03:00
function getDirectoryInfo ( reqpath ) {
2021-01-25 02:19:29 +03:00
var response = { path : reqpath , dir : [ ] } ;
2021-01-25 08:51:22 +03:00
if ( ( ( reqpath == undefined ) || ( reqpath == '' ) ) && ( process . platform == 'win32' ) ) {
2021-01-25 02:19:29 +03:00
// List all the drives in the root, or the root itself
var results = null ;
2024-01-31 17:06:02 +03:00
try { results = fs . readDrivesSync ( ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( results != null ) {
for ( var i = 0 ; i < results . length ; ++ i ) {
2024-01-31 17:06:02 +03:00
var drive = { n : results [ i ] . name , t : 1 , dt : results [ i ] . type , s : ( results [ i ] . size ? results [ i ] . size : 0 ) , f : ( results [ i ] . free ? results [ i ] . free : 0 ) } ;
2021-01-25 02:19:29 +03:00
response . dir . push ( drive ) ;
}
}
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
// List all the files and folders in this path
if ( reqpath == '' ) { reqpath = '/' ; }
var results = null , xpath = obj . path . join ( reqpath , '*' ) ;
//if (process.platform == "win32") { xpath = xpath.split('/').join('\\'); }
2022-01-17 00:37:26 +03:00
try { results = fs . readdirSync ( xpath ) ; } catch ( ex ) { }
try { if ( ( results != null ) && ( results . length == 0 ) && ( fs . existsSync ( reqpath ) == false ) ) { results = null ; } } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( results != null ) {
for ( var i = 0 ; i < results . length ; ++ i ) {
if ( ( results [ i ] != '.' ) && ( results [ i ] != '..' ) ) {
2021-01-25 02:19:29 +03:00
var stat = null , p = obj . path . join ( reqpath , results [ i ] ) ;
//if (process.platform == "win32") { p = p.split('/').join('\\'); }
2022-01-17 00:37:26 +03:00
try { stat = fs . statSync ( p ) ; } catch ( ex ) { } // TODO: Get file size/date
2021-01-25 08:51:22 +03:00
if ( ( stat != null ) && ( stat != undefined ) ) {
if ( stat . isDirectory ( ) == true ) {
2021-01-25 02:19:29 +03:00
response . dir . push ( { n : results [ i ] , t : 2 , d : stat . mtime } ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response . dir . push ( { n : results [ i ] , t : 3 , s : stat . size , d : stat . mtime } ) ;
}
}
}
}
2021-10-21 00:04:10 +03:00
} else {
response . dir = null ;
2017-10-24 00:09:58 +03:00
}
}
2021-01-25 02:19:29 +03:00
return response ;
}
2022-08-26 06:11:47 +03:00
function tunnel _s _finalized ( )
{
console . info1 ( 'Tunnel Socket Finalized' ) ;
}
function tunnel _onIdleTimeout ( )
{
this . ping ( ) ;
this . setTimeout ( require ( 'MeshAgent' ) . idleTimeout * 1000 ) ;
}
2021-01-25 02:19:29 +03:00
// Tunnel callback operations
2022-08-26 06:11:47 +03:00
function onTunnelUpgrade ( response , s , head )
{
2021-01-25 02:19:29 +03:00
this . s = s ;
2022-08-26 06:11:47 +03:00
s . once ( '~' , tunnel _s _finalized ) ;
2021-01-25 02:19:29 +03:00
s . httprequest = this ;
s . end = onTunnelClosed ;
s . tunnel = this ;
s . descriptorMetadata = "MeshAgent_relayTunnel" ;
2019-05-05 08:53:57 +03:00
2022-08-26 06:11:47 +03:00
if ( require ( 'MeshAgent' ) . idleTimeout != null )
{
2021-01-25 02:19:29 +03:00
s . setTimeout ( require ( 'MeshAgent' ) . idleTimeout * 1000 ) ;
2022-08-26 06:11:47 +03:00
s . on ( 'timeout' , tunnel _onIdleTimeout ) ;
2019-05-05 08:53:57 +03:00
}
2021-01-25 02:19:29 +03:00
//sendConsoleText('onTunnelUpgrade - ' + this.tcpport + ' - ' + this.udpport);
2022-08-04 11:52:29 +03:00
if ( this . tcpport != null ) {
2021-01-25 02:19:29 +03:00
// This is a TCP relay connection, pause now and try to connect to the target.
s . pause ( ) ;
s . data = onTcpRelayServerTunnelData ;
var connectionOptions = { port : parseInt ( this . tcpport ) } ;
if ( this . tcpaddr != null ) { connectionOptions . host = this . tcpaddr ; } else { connectionOptions . host = '127.0.0.1' ; }
s . tcprelay = net . createConnection ( connectionOptions , onTcpRelayTargetTunnelConnect ) ;
s . tcprelay . peerindex = this . index ;
// Add the TCP session to the count and update the server
2021-01-25 08:51:22 +03:00
if ( s . httprequest . userid != null ) {
2021-06-25 21:49:58 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( s . httprequest ) ;
if ( tunnelUserCount . tcp [ userid ] == null ) { tunnelUserCount . tcp [ userid ] = 1 ; } else { tunnelUserCount . tcp [ userid ] ++ ; }
2022-01-17 00:37:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'tcp' , value : tunnelUserCount . tcp } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
}
2022-07-27 06:03:24 +03:00
}
2022-08-04 11:52:29 +03:00
if ( this . udpport != null ) {
2021-01-25 02:19:29 +03:00
// This is a UDP relay connection, get the UDP socket setup. // TODO: ***************
s . data = onUdpRelayServerTunnelData ;
s . udprelay = require ( 'dgram' ) . createSocket ( { type : 'udp4' } ) ;
s . udprelay . bind ( { port : 0 } ) ;
s . udprelay . peerindex = this . index ;
s . udprelay . on ( 'message' , onUdpRelayTargetTunnelConnect ) ;
s . udprelay . udpport = this . udpport ;
s . udprelay . udpaddr = this . udpaddr ;
s . udprelay . first = true ;
// Add the UDP session to the count and update the server
2021-01-25 08:51:22 +03:00
if ( s . httprequest . userid != null ) {
2021-06-25 21:49:58 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( s . httprequest ) ;
if ( tunnelUserCount . udp [ userid ] == null ) { tunnelUserCount . udp [ userid ] = 1 ; } else { tunnelUserCount . udp [ userid ] ++ ; }
2022-01-17 00:37:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'udp' , value : tunnelUserCount . tcp } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2019-05-07 04:44:23 +03:00
}
2022-07-27 06:03:24 +03:00
}
2022-08-04 11:52:29 +03:00
else {
2021-01-25 02:19:29 +03:00
// This is a normal connect for KVM/Terminal/Files
s . data = onTunnelData ;
2019-05-05 08:53:57 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-06-25 21:49:58 +03:00
// If the HTTP Request has a guest name, we need to form a userid that includes the guest name in hex.
// This is so we can tell the server that a session is for a given userid/guest sharing pair.
function getUserIdAndGuestNameFromHttpRequest ( request ) {
2022-02-24 04:17:28 +03:00
if ( request . guestname == null ) return request . userid ; else return request . guestuserid + '/guest:' + Buffer . from ( request . guestname ) . toString ( 'base64' ) ;
2021-06-25 21:49:58 +03:00
}
2021-01-25 02:19:29 +03:00
// Called when UDP relay data is received // TODO****
2021-01-25 08:51:22 +03:00
function onUdpRelayTargetTunnelConnect ( data ) {
2021-01-25 02:19:29 +03:00
var peerTunnel = tunnels [ this . peerindex ] ;
peerTunnel . s . write ( data ) ;
}
2019-05-05 08:53:57 +03:00
2021-01-25 02:19:29 +03:00
// Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest)
2021-01-25 08:51:22 +03:00
function onUdpRelayServerTunnelData ( data ) {
if ( this . udprelay . first === true ) {
2021-01-25 02:19:29 +03:00
delete this . udprelay . first ; // Skip the first 'c' that is received.
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
this . udprelay . send ( data , parseInt ( this . udprelay . udpport ) , this . udprelay . udpaddr ? this . udprelay . udpaddr : '127.0.0.1' ) ;
2017-10-24 00:09:58 +03:00
}
2021-01-25 02:19:29 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Called when the TCP relay target is connected
2021-01-25 08:51:22 +03:00
function onTcpRelayTargetTunnelConnect ( ) {
2021-01-25 02:19:29 +03:00
var peerTunnel = tunnels [ this . peerindex ] ;
this . pipe ( peerTunnel . s ) ; // Pipe Target --> Server
peerTunnel . s . first = true ;
peerTunnel . s . resume ( ) ;
}
// Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest)
2021-01-25 08:51:22 +03:00
function onTcpRelayServerTunnelData ( data ) {
if ( this . first == true ) {
2021-01-25 02:19:29 +03:00
this . first = false ;
this . pipe ( this . tcprelay , { dataTypeSkip : 1 } ) ; // Pipe Server --> Target (don't pipe text type websocket frames)
2017-10-24 00:09:58 +03:00
}
2021-01-25 02:19:29 +03:00
}
2019-05-05 08:53:57 +03:00
2022-08-26 06:11:47 +03:00
function onTunnelClosed ( )
{
if ( this . httprequest . _dispatcher != null && this . httprequest . term == null )
{
// Windows Dispatcher was created to spawn a child connection, but the child didn't connect yet, so we have to shutdown the dispatcher, otherwise the child may end up hanging
if ( this . httprequest . _dispatcher . close ) { this . httprequest . _dispatcher . close ( ) ; }
this . httprequest . _dispatcher = null ;
}
if ( this . tunnel )
{
if ( tunnels [ this . httprequest . index ] == null )
{
this . tunnel . s = null ;
this . tunnel = null ;
return ;
}
}
2021-01-25 02:19:29 +03:00
var tunnel = tunnels [ this . httprequest . index ] ;
if ( tunnel == null ) return ; // Stop duplicate calls.
2020-07-18 05:55:49 +03:00
2021-09-01 22:07:09 +03:00
// Perform display locking on disconnect
if ( ( this . httprequest . protocol == 2 ) && ( this . httprequest . autolock === true ) ) {
// Look for a TSID
var tsid = null ;
if ( ( this . httprequest . xoptions != null ) && ( typeof this . httprequest . xoptions . tsid == 'number' ) ) { tsid = this . httprequest . xoptions . tsid ; }
// Lock the current user out of the desktop
2021-09-03 10:55:00 +03:00
MeshServerLogEx ( 53 , null , "Locking remote user out of desktop" , this . httprequest ) ;
lockDesktop ( tsid ) ;
2021-09-01 22:07:09 +03:00
}
2021-01-25 02:19:29 +03:00
// If this is a routing session, clean up and send the new session counts.
2021-01-25 08:51:22 +03:00
if ( this . httprequest . userid != null ) {
if ( this . httprequest . tcpport != null ) {
2021-07-02 19:32:28 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest ) ;
2021-06-25 21:49:58 +03:00
if ( tunnelUserCount . tcp [ userid ] != null ) { tunnelUserCount . tcp [ userid ] -- ; if ( tunnelUserCount . tcp [ userid ] <= 0 ) { delete tunnelUserCount . tcp [ userid ] ; } }
2022-01-17 00:37:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'tcp' , value : tunnelUserCount . tcp } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . udpport != null ) {
2021-07-02 19:32:28 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest ) ;
2021-06-25 21:49:58 +03:00
if ( tunnelUserCount . udp [ userid ] != null ) { tunnelUserCount . udp [ userid ] -- ; if ( tunnelUserCount . udp [ userid ] <= 0 ) { delete tunnelUserCount . udp [ userid ] ; } }
2022-01-17 00:37:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'udp' , value : tunnelUserCount . udp } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2020-07-25 00:00:49 +03:00
}
2021-01-25 02:19:29 +03:00
}
2020-07-25 00:00:49 +03:00
2022-01-17 00:37:26 +03:00
try {
2021-10-18 08:13:13 +03:00
// Sent tunnel statistics to the server, only send this if compression was used.
if ( ( this . bytesSent _uncompressed ) && ( this . bytesSent _uncompressed . toString ( ) != this . bytesSent _actual . toString ( ) ) ) {
mesh . SendCommand ( {
action : 'tunnelCloseStats' ,
url : tunnel . url ,
userid : tunnel . userid ,
protocol : tunnel . protocol ,
sessionid : tunnel . sessionid ,
sent : this . bytesSent _uncompressed . toString ( ) ,
sentActual : this . bytesSent _actual . toString ( ) ,
sentRatio : this . bytesSent _ratio ,
received : this . bytesReceived _uncompressed . toString ( ) ,
receivedActual : this . bytesReceived _actual . toString ( ) ,
receivedRatio : this . bytesReceived _ratio
} ) ;
}
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2020-07-17 09:08:59 +03:00
2021-01-25 02:19:29 +03:00
//sendConsoleText("Tunnel #" + this.httprequest.index + " closed. Sent -> " + this.bytesSent_uncompressed + ' bytes (uncompressed), ' + this.bytesSent_actual + ' bytes (actual), ' + this.bytesSent_ratio + '% compression', this.httprequest.sessionid);
2022-08-26 06:11:47 +03:00
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
/ *
// Close the watcher if required
if ( this . httprequest . watcher != undefined ) {
//console.log('Closing watcher: ' + this.httprequest.watcher.path);
//this.httprequest.watcher.close(); // TODO: This line causes the agent to crash!!!!
delete this . httprequest . watcher ;
}
* /
// If there is a upload or download active on this connection, close the file
2021-01-25 09:46:54 +03:00
if ( this . httprequest . uploadFile ) { fs . closeSync ( this . httprequest . uploadFile ) ; delete this . httprequest . uploadFile ; delete this . httprequest . uploadFileid ; delete this . httprequest . uploadFilePath ; delete this . httprequest . uploadFileSize ; }
2021-01-25 02:19:29 +03:00
if ( this . httprequest . downloadFile ) { delete this . httprequest . downloadFile ; }
// Clean up WebRTC
2021-01-25 08:51:22 +03:00
if ( this . webrtc != null ) {
2022-01-17 00:37:26 +03:00
if ( this . webrtc . rtcchannel ) { try { this . webrtc . rtcchannel . close ( ) ; } catch ( ex ) { } this . webrtc . rtcchannel . removeAllListeners ( 'data' ) ; this . webrtc . rtcchannel . removeAllListeners ( 'end' ) ; delete this . webrtc . rtcchannel ; }
2021-01-25 02:19:29 +03:00
if ( this . webrtc . websocket ) { delete this . webrtc . websocket ; }
2022-01-17 00:37:26 +03:00
try { this . webrtc . close ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
this . webrtc . removeAllListeners ( 'connected' ) ;
this . webrtc . removeAllListeners ( 'disconnected' ) ;
this . webrtc . removeAllListeners ( 'dataChannel' ) ;
delete this . webrtc ;
}
2017-08-28 19:27:45 +03:00
2021-01-25 02:19:29 +03:00
// Clean up WebSocket
2022-08-26 06:11:47 +03:00
delete tunnels [ this . httprequest . index ] ;
tunnel = null ;
this . tunnel . s = null ;
this . tunnel = null ;
2021-01-25 02:19:29 +03:00
this . removeAllListeners ( 'data' ) ;
}
function onTunnelSendOk ( ) { /*sendConsoleText("Tunnel #" + this.index + " SendOK.", this.sessionid);*/ }
2022-08-26 06:11:47 +03:00
function terminal _onconnection ( c )
{
if ( this . httprequest . connectionPromise . completed )
{
c . end ( ) ;
}
else
{
this . httprequest . connectionPromise . _res ( c ) ;
}
}
function terminal _user _onconnection ( c )
{
console . info1 ( 'completed-2: ' + this . connectionPromise . completed ) ;
if ( this . connectionPromise . completed )
{
c . end ( ) ;
}
else
{
this . connectionPromise . _res ( c ) ;
}
}
function terminal _stderr _ondata ( c )
{
this . stdout . write ( c ) ;
}
function terminal _onend ( )
{
this . httprequest . process . kill ( ) ;
}
function terminal _onexit ( )
{
this . tunnel . end ( ) ;
}
function terminal _onfinalized ( )
{
this . httprequest = null ;
console . info1 ( 'Dispatcher Finalized' ) ;
}
function terminal _end ( )
{
if ( this . httprequest == null ) { return ; }
if ( this . httprequest . tpromise . _consent ) { this . httprequest . tpromise . _consent . close ( ) ; }
if ( this . httprequest . connectionPromise ) { this . httprequest . connectionPromise . _rej ( 'Closed' ) ; }
// Remove the terminal session to the count to update the server
if ( this . httprequest . userid != null )
{
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest ) ;
if ( tunnelUserCount . terminal [ userid ] != null ) { tunnelUserCount . terminal [ userid ] -- ; if ( tunnelUserCount . terminal [ userid ] <= 0 ) { delete tunnelUserCount . terminal [ userid ] ; } }
try { mesh . SendCommand ( { action : 'sessions' , type : 'terminal' , value : tunnelUserCount . terminal } ) ; } catch ( ex ) { }
broadcastSessionsToRegisteredApps ( ) ;
}
if ( process . platform == 'win32' )
{
// Unpipe the web socket
this . unpipe ( this . httprequest . _term ) ;
if ( this . httprequest . _term ) { this . httprequest . _term . unpipe ( this ) ; }
// Unpipe the WebRTC channel if needed (This will also be done when the WebRTC channel ends).
if ( this . rtcchannel )
{
this . rtcchannel . unpipe ( this . httprequest . _term ) ;
if ( this . httprequest . _term ) { this . httprequest . _term . unpipe ( this . rtcchannel ) ; }
}
// Clean up
if ( this . httprequest . _term ) { this . httprequest . _term . end ( ) ; }
this . httprequest . _term = null ;
this . httprequest . _dispatcher = null ;
}
this . httprequest = null ;
}
function terminal _promise _connection _rejected ( e )
{
// FAILED to connect terminal
this . ws . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : e . toString ( ) , msgid : 2 } ) ) ;
this . ws . end ( ) ;
}
function terminal _promise _connection _resolved ( term )
{
this . _internal . completedArgs = [ ] ;
// SUCCESS
var stdoutstream ;
var stdinstream ;
if ( process . platform == 'win32' )
{
this . ws . httprequest . _term = term ;
this . ws . httprequest . _term . tunnel = this . ws ;
stdoutstream = stdinstream = term ;
}
else
{
term . descriptorMetadata = 'Remote Terminal' ;
this . ws . httprequest . process = term ;
this . ws . httprequest . process . tunnel = this . ws ;
term . stderr . stdout = term . stdout ;
term . stderr . on ( 'data' , terminal _stderr _ondata ) ;
stdoutstream = term . stdout ;
stdinstream = term . stdin ;
this . ws . prependListener ( 'end' , terminal _onend ) ;
term . prependListener ( 'exit' , terminal _onexit ) ;
}
this . ws . removeAllListeners ( 'data' ) ;
this . ws . on ( 'data' , onTunnelControlData ) ;
stdoutstream . pipe ( this . ws , { dataTypeSkip : 1 } ) ; // 0 = Binary, 1 = Text.
this . ws . pipe ( stdinstream , { dataTypeSkip : 1 , end : false } ) ; // 0 = Binary, 1 = Text.
// Add the terminal session to the count to update the server
if ( this . ws . httprequest . userid != null )
{
var userid = getUserIdAndGuestNameFromHttpRequest ( this . ws . httprequest ) ;
if ( tunnelUserCount . terminal [ userid ] == null ) { tunnelUserCount . terminal [ userid ] = 1 ; } else { tunnelUserCount . terminal [ userid ] ++ ; }
try { mesh . SendCommand ( { action : 'sessions' , type : 'terminal' , value : tunnelUserCount . terminal } ) ; } catch ( ex ) { }
broadcastSessionsToRegisteredApps ( ) ;
}
// Toast Notification, if required
if ( this . ws . httprequest . consent && ( this . ws . httprequest . consent & 2 ) )
{
// User Notifications is required
2024-03-14 17:51:33 +03:00
var notifyMessage = currentTranslation [ 'terminalNotify' ] . replace ( '{0}' , this . ws . httprequest . realname ? this . ws . httprequest . realname : this . ws . httprequest . username ) ;
2022-08-26 06:11:47 +03:00
var notifyTitle = "MeshCentral" ;
if ( this . ws . httprequest . soptions != null )
{
if ( this . ws . httprequest . soptions . notifyTitle != null ) { notifyTitle = this . ws . httprequest . soptions . notifyTitle ; }
if ( this . ws . httprequest . soptions . notifyMsgTerminal != null ) { notifyMessage = this . ws . httprequest . soptions . notifyMsgTerminal . replace ( '{0}' , this . ws . httprequest . realname ) . replace ( '{1}' , this . ws . httprequest . username ) ; }
}
try { require ( 'toaster' ) . Toast ( notifyTitle , notifyMessage ) ; } catch ( ex ) { }
}
this . ws = null ;
}
function terminal _promise _consent _rejected ( e )
{
// DO NOT start terminal
this . that . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : e . toString ( ) , msgid : 2 } ) ) ;
this . that . end ( ) ;
this . that = null ;
this . httprequest = null ;
}
function promise _init ( res , rej ) { this . _res = res ; this . _rej = rej ; }
function terminal _userpromise _resolved ( u )
{
var that = this . that ;
if ( u . Active . length > 0 )
{
var tmp ;
var username = '"' + u . Active [ 0 ] . Domain + '\\' + u . Active [ 0 ] . Username + '"' ;
if ( require ( 'win-virtual-terminal' ) . supported )
{
// ConPTY PseudoTerminal
tmp = require ( 'win-dispatcher' ) . dispatch ( { user : username , modules : [ { name : 'win-virtual-terminal' , script : getJSModule ( 'win-virtual-terminal' ) } ] , launch : { module : 'win-virtual-terminal' , method : ( that . httprequest . protocol == 9 ? 'StartPowerShell' : 'Start' ) , args : [ this . cols , this . rows ] } } ) ;
}
else
{
// Legacy Terminal
tmp = require ( 'win-dispatcher' ) . dispatch ( { user : username , modules : [ { name : 'win-terminal' , script : getJSModule ( 'win-terminal' ) } ] , launch : { module : 'win-terminal' , method : ( that . httprequest . protocol == 9 ? 'StartPowerShell' : 'Start' ) , args : [ this . cols , this . rows ] } } ) ;
}
that . httprequest . _dispatcher = tmp ;
that . httprequest . _dispatcher . connectionPromise = that . httprequest . connectionPromise ;
that . httprequest . _dispatcher . on ( 'connection' , terminal _user _onconnection ) ;
that . httprequest . _dispatcher . on ( '~' , terminal _onfinalized ) ;
}
this . that = null ;
that = null ;
}
function terminal _promise _consent _resolved ( )
{
this . httprequest . connectionPromise = new promise ( promise _init ) ;
this . httprequest . connectionPromise . ws = this . that ;
// Start Terminal
if ( process . platform == 'win32' )
{
try
{
var cols = 80 , rows = 25 ;
if ( this . httprequest . xoptions )
{
if ( this . httprequest . xoptions . rows ) { rows = this . httprequest . xoptions . rows ; }
if ( this . httprequest . xoptions . cols ) { cols = this . httprequest . xoptions . cols ; }
}
if ( ( this . httprequest . protocol == 1 ) || ( this . httprequest . protocol == 6 ) )
{
// Admin Terminal
if ( require ( 'win-virtual-terminal' ) . supported )
{
// ConPTY PseudoTerminal
// this.httprequest._term = require('win-virtual-terminal')[this.httprequest.protocol == 6 ? 'StartPowerShell' : 'Start'](80, 25);
// The above line is commented out, because there is a bug with ClosePseudoConsole() API, so this is the workaround
this . httprequest . _dispatcher = require ( 'win-dispatcher' ) . dispatch ( { modules : [ { name : 'win-virtual-terminal' , script : getJSModule ( 'win-virtual-terminal' ) } ] , launch : { module : 'win-virtual-terminal' , method : ( this . httprequest . protocol == 6 ? 'StartPowerShell' : 'Start' ) , args : [ cols , rows ] } } ) ;
this . httprequest . _dispatcher . httprequest = this . httprequest ;
this . httprequest . _dispatcher . on ( 'connection' , terminal _onconnection ) ;
this . httprequest . _dispatcher . on ( '~' , terminal _onfinalized ) ;
}
else
{
// Legacy Terminal
this . httprequest . connectionPromise . _res ( require ( 'win-terminal' ) [ this . httprequest . protocol == 6 ? 'StartPowerShell' : 'Start' ] ( cols , rows ) ) ;
}
}
else
{
// Logged in user
var userPromise = require ( 'user-sessions' ) . enumerateUsers ( ) ;
userPromise . that = this ;
userPromise . cols = cols ;
userPromise . rows = rows ;
userPromise . then ( terminal _userpromise _resolved ) ;
}
} catch ( ex )
{
this . httprequest . connectionPromise . _rej ( 'Failed to start remote terminal session, ' + ex . toString ( ) ) ;
}
}
else
{
try
{
var bash = fs . existsSync ( '/bin/bash' ) ? '/bin/bash' : false ;
var sh = fs . existsSync ( '/bin/sh' ) ? '/bin/sh' : false ;
var login = process . platform == 'linux' ? '/bin/login' : '/usr/bin/login' ;
var env = { HISTCONTROL : 'ignoreboth' } ;
if ( process . env [ 'LANG' ] ) { env [ 'LANG' ] = process . env [ 'LANG' ] ; }
if ( process . env [ 'PATH' ] ) { env [ 'PATH' ] = process . env [ 'PATH' ] ; }
if ( this . httprequest . xoptions )
{
if ( this . httprequest . xoptions . rows ) { env . LINES = ( '' + this . httprequest . xoptions . rows ) ; }
if ( this . httprequest . xoptions . cols ) { env . COLUMNS = ( '' + this . httprequest . xoptions . cols ) ; }
}
var options = { type : childProcess . SpawnTypes . TERM , uid : ( this . httprequest . protocol == 8 ) ? require ( 'user-sessions' ) . consoleUid ( ) : null , env : env } ;
if ( this . httprequest . xoptions && this . httprequest . xoptions . requireLogin )
{
if ( ! require ( 'fs' ) . existsSync ( login ) ) { throw ( 'Unable to spawn login process' ) ; }
this . httprequest . connectionPromise . _res ( childProcess . execFile ( login , [ 'login' ] , options ) ) ; // Start login shell
}
else if ( bash )
{
var p = childProcess . execFile ( bash , [ 'bash' ] , options ) ; // Start bash
// Spaces at the beginning of lines are needed to hide commands from the command history
if ( ( obj . serverInfo . termlaunchcommand != null ) && ( typeof obj . serverInfo . termlaunchcommand [ process . platform ] == 'string' ) )
{
if ( obj . serverInfo . termlaunchcommand [ process . platform ] != '' ) { p . stdin . write ( obj . serverInfo . termlaunchcommand [ process . platform ] ) ; }
} else if ( process . platform == 'linux' ) { p . stdin . write ( ' alias ls=\'ls --color=auto\';clear\n' ) ; }
this . httprequest . connectionPromise . _res ( p ) ;
}
else if ( sh )
{
var p = childProcess . execFile ( sh , [ 'sh' ] , options ) ; // Start sh
// Spaces at the beginning of lines are needed to hide commands from the command history
if ( ( obj . serverInfo . termlaunchcommand != null ) && ( typeof obj . serverInfo . termlaunchcommand [ process . platform ] == 'string' ) )
{
if ( obj . serverInfo . termlaunchcommand [ process . platform ] != '' ) { p . stdin . write ( obj . serverInfo . termlaunchcommand [ process . platform ] ) ; }
} else if ( process . platform == 'linux' ) { p . stdin . write ( ' alias ls=\'ls --color=auto\';clear\n' ) ; }
this . httprequest . connectionPromise . _res ( p ) ;
}
else
{
this . httprequest . connectionPromise . _rej ( 'Failed to start remote terminal session, no shell found' ) ;
}
} catch ( ex )
{
this . httprequest . connectionPromise . _rej ( 'Failed to start remote terminal session, ' + ex . toString ( ) ) ;
}
}
this . httprequest . connectionPromise . then ( terminal _promise _connection _resolved , terminal _promise _connection _rejected ) ;
this . that = null ;
this . httprequest = null ;
}
function tunnel _kvm _end ( )
{
-- this . desktop . kvm . connectionCount ;
// Remove ourself from the list of remote desktop session
var i = this . desktop . kvm . tunnels . indexOf ( this ) ;
if ( i >= 0 ) { this . desktop . kvm . tunnels . splice ( i , 1 ) ; }
// Send a metadata update to all desktop sessions
var users = { } ;
if ( this . httprequest . desktop . kvm . tunnels != null )
{
for ( var i in this . httprequest . desktop . kvm . tunnels )
{
try
{
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest . desktop . kvm . tunnels [ i ] . httprequest ) ;
if ( users [ userid ] == null ) { users [ userid ] = 1 ; } else { users [ userid ] ++ ; }
} catch ( ex ) { sendConsoleText ( ex ) ; }
}
for ( var i in this . httprequest . desktop . kvm . tunnels )
{
try { this . httprequest . desktop . kvm . tunnels [ i ] . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'metadata' , users : users } ) ) ; } catch ( ex ) { }
}
tunnelUserCount . desktop = users ;
try { mesh . SendCommand ( { action : 'sessions' , type : 'kvm' , value : users } ) ; } catch ( ex ) { }
broadcastSessionsToRegisteredApps ( ) ;
}
// Unpipe the web socket
try
{
this . unpipe ( this . httprequest . desktop . kvm ) ;
this . httprequest . desktop . kvm . unpipe ( this ) ;
} catch ( ex ) { }
// Unpipe the WebRTC channel if needed (This will also be done when the WebRTC channel ends).
if ( this . rtcchannel )
{
try
{
this . rtcchannel . unpipe ( this . httprequest . desktop . kvm ) ;
this . httprequest . desktop . kvm . unpipe ( this . rtcchannel ) ;
}
catch ( ex ) { }
}
// Place wallpaper back if needed
// TODO
if ( this . desktop . kvm . connectionCount == 0 )
{
// Display a toast message. This may not be supported on all platforms.
// try { require('toaster').Toast('MeshCentral', 'Remote Desktop Control Ended.'); } catch (ex) { }
this . httprequest . desktop . kvm . end ( ) ;
if ( this . httprequest . desktop . kvm . connectionBar )
{
this . httprequest . desktop . kvm . connectionBar . removeAllListeners ( 'close' ) ;
this . httprequest . desktop . kvm . connectionBar . close ( ) ;
this . httprequest . desktop . kvm . connectionBar = null ;
}
} else
{
for ( var i in this . httprequest . desktop . kvm . users )
{
if ( ( this . httprequest . desktop . kvm . users [ i ] == this . httprequest . username ) && this . httprequest . desktop . kvm . connectionBar )
{
for ( var j in this . httprequest . desktop . kvm . rusers ) { if ( this . httprequest . desktop . kvm . rusers [ j ] == this . httprequest . realname ) { this . httprequest . desktop . kvm . rusers . splice ( j , 1 ) ; break ; } }
this . httprequest . desktop . kvm . users . splice ( i , 1 ) ;
this . httprequest . desktop . kvm . connectionBar . removeAllListeners ( 'close' ) ;
this . httprequest . desktop . kvm . connectionBar . close ( ) ;
2024-01-13 01:42:26 +03:00
this . httprequest . desktop . kvm . connectionBar = require ( 'notifybar-desktop' ) ( this . httprequest . privacybartext . replace ( '{0}' , this . httprequest . desktop . kvm . rusers . join ( ', ' ) ) . replace ( '{1}' , this . httprequest . desktop . kvm . users . join ( ', ' ) ) . replace ( /'/g , "\\'\\" ) , require ( 'MeshAgent' ) . _tsid , color _options ) ;
2022-08-26 06:11:47 +03:00
this . httprequest . desktop . kvm . connectionBar . httprequest = this . httprequest ;
this . httprequest . desktop . kvm . connectionBar . on ( 'close' , function ( )
{
MeshServerLogEx ( 29 , null , "Remote Desktop Connection forcefully closed by local user (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
for ( var i in this . httprequest . desktop . kvm . _pipedStreams )
{
this . httprequest . desktop . kvm . _pipedStreams [ i ] . end ( ) ;
}
this . httprequest . desktop . kvm . end ( ) ;
} ) ;
break ;
}
}
}
if ( this . httprequest . desktop . kvm . connectionBar )
{
console . info1 ( 'Setting ConnectionBar request to NULL' ) ;
this . httprequest . desktop . kvm . connectionBar . httprequest = null ;
}
this . httprequest = null ;
this . desktop . tunnel = null ;
}
function kvm _tunnel _consentpromise _closehandler ( )
{
if ( this . _consentpromise && this . _consentpromise . close ) { this . _consentpromise . close ( ) ; }
}
function kvm _consentpromise _rejected ( e )
{
// User Consent Denied/Failed
this . ws . _consentpromise = null ;
MeshServerLogEx ( 34 , null , "Failed to start remote desktop after local user rejected (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
this . ws . end ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : e . toString ( ) , msgid : 2 } ) ) ;
this . ws = null ;
}
function kvm _consentpromise _resolved ( always )
{
if ( always && process . platform == 'win32' ) { server _set _consentTimer ( this . ws . httprequest . userid ) ; }
// Success
this . ws . _consentpromise = null ;
MeshServerLogEx ( 30 , null , "Starting remote desktop after local user accepted (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
this . ws . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : null , msgid : 0 } ) ) ;
if ( this . ws . httprequest . consent && ( this . ws . httprequest . consent & 1 ) )
{
// User Notifications is required
var notifyMessage = currentTranslation [ 'desktopNotify' ] . replace ( '{0}' , this . ws . httprequest . realname ) ;
var notifyTitle = "MeshCentral" ;
if ( this . ws . httprequest . soptions != null )
{
if ( this . ws . httprequest . soptions . notifyTitle != null ) { notifyTitle = this . ws . httprequest . soptions . notifyTitle ; }
if ( this . ws . httprequest . soptions . notifyMsgDesktop != null ) { notifyMessage = this . ws . httprequest . soptions . notifyMsgDesktop . replace ( '{0}' , this . ws . httprequest . realname ) . replace ( '{1}' , this . ws . httprequest . username ) ; }
}
try { require ( 'toaster' ) . Toast ( notifyTitle , notifyMessage , tsid ) ; } catch ( ex ) { }
}
if ( this . ws . httprequest . consent && ( this . ws . httprequest . consent & 0x40 ) )
{
// Connection Bar is required
if ( this . ws . httprequest . desktop . kvm . connectionBar )
{
this . ws . httprequest . desktop . kvm . connectionBar . removeAllListeners ( 'close' ) ;
this . ws . httprequest . desktop . kvm . connectionBar . close ( ) ;
}
try
{
2024-01-13 01:42:26 +03:00
this . ws . httprequest . desktop . kvm . connectionBar = require ( 'notifybar-desktop' ) ( this . ws . httprequest . privacybartext . replace ( '{0}' , this . ws . httprequest . desktop . kvm . rusers . join ( ', ' ) ) . replace ( '{1}' , this . ws . httprequest . desktop . kvm . users . join ( ', ' ) ) . replace ( /'/g , "\\'\\" ) , require ( 'MeshAgent' ) . _tsid , color _options ) ;
2022-08-26 06:11:47 +03:00
MeshServerLogEx ( 31 , null , "Remote Desktop Connection Bar Activated/Updated (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
} catch ( ex )
{
if ( process . platform != 'darwin' )
{
MeshServerLogEx ( 32 , null , "Remote Desktop Connection Bar Failed or Not Supported (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
}
}
2023-10-05 14:05:23 +03:00
try {
if ( this . ws . httprequest . desktop . kvm . connectionBar ) {
this . ws . httprequest . desktop . kvm . connectionBar . httprequest = this . ws . httprequest ;
this . ws . httprequest . desktop . kvm . connectionBar . on ( 'close' , function ( ) {
MeshServerLogEx ( 29 , null , "Remote Desktop Connection forcefully closed by local user (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
for ( var i in this . httprequest . desktop . kvm . _pipedStreams ) {
this . httprequest . desktop . kvm . _pipedStreams [ i ] . end ( ) ;
}
this . httprequest . desktop . kvm . end ( ) ;
} ) ;
}
}
catch ( ex )
2022-08-26 06:11:47 +03:00
{
2023-10-05 14:05:23 +03:00
if ( process . platform != 'darwin' )
2022-08-26 06:11:47 +03:00
{
2023-10-05 14:05:23 +03:00
MeshServerLogEx ( 32 , null , "Failed2(" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
}
2022-08-26 06:11:47 +03:00
}
}
this . ws . httprequest . desktop . kvm . pipe ( this . ws , { dataTypeSkip : 1 } ) ;
if ( this . ws . httprequest . autolock )
{
destopLockHelper _pipe ( this . ws . httprequest ) ;
}
this . ws . resume ( ) ;
this . ws = null ;
}
function files _consentpromise _resolved ( always )
{
if ( always && process . platform == 'win32' ) { server _set _consentTimer ( this . ws . httprequest . userid ) ; }
// Success
this . ws . _consentpromise = null ;
MeshServerLogEx ( 40 , null , "Starting remote files after local user accepted (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
this . ws . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : null } ) ) ;
if ( this . ws . httprequest . consent && ( this . ws . httprequest . consent & 4 ) )
{
// User Notifications is required
var notifyMessage = currentTranslation [ 'fileNotify' ] . replace ( '{0}' , this . ws . httprequest . realname ) ;
var notifyTitle = "MeshCentral" ;
if ( this . ws . httprequest . soptions != null )
{
if ( this . ws . httprequest . soptions . notifyTitle != null ) { notifyTitle = this . ws . httprequest . soptions . notifyTitle ; }
if ( this . ws . httprequest . soptions . notifyMsgFiles != null ) { notifyMessage = this . ws . httprequest . soptions . notifyMsgFiles . replace ( '{0}' , this . ws . httprequest . realname ) . replace ( '{1}' , this . ws . httprequest . username ) ; }
}
try { require ( 'toaster' ) . Toast ( notifyTitle , notifyMessage ) ; } catch ( ex ) { }
}
this . ws . resume ( ) ;
this . ws = null ;
}
function files _consentpromise _rejected ( e )
{
// User Consent Denied/Failed
this . ws . _consentpromise = null ;
MeshServerLogEx ( 41 , null , "Failed to start remote files after local user rejected (" + this . ws . httprequest . remoteaddr + ")" , this . ws . httprequest ) ;
this . ws . end ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : e . toString ( ) , msgid : 2 } ) ) ;
this . ws = null ;
}
function files _tunnel _endhandler ( )
{
if ( this . _consentpromise && this . _consentpromise . close ) { this . _consentpromise . close ( ) ; }
}
function onTunnelData ( data )
{
2021-04-27 05:01:40 +03:00
//sendConsoleText('OnTunnelData, ' + data.length + ', ' + typeof data + ', ' + data);
2021-01-25 02:19:29 +03:00
// If this is upload data, save it to file
2021-01-25 08:51:22 +03:00
if ( ( this . httprequest . uploadFile ) && ( typeof data == 'object' ) && ( data [ 0 ] != 123 ) ) {
2021-01-25 02:19:29 +03:00
// Save the data to file being uploaded.
2021-01-25 08:51:22 +03:00
if ( data [ 0 ] == 0 ) {
2021-01-25 02:19:29 +03:00
// If data starts with zero, skip the first byte. This is used to escape binary file data from JSON.
2021-01-25 09:46:54 +03:00
this . httprequest . uploadFileSize += ( data . length - 1 ) ;
2022-01-17 00:37:26 +03:00
try { fs . writeSync ( this . httprequest . uploadFile , data , 1 , data . length - 1 ) ; } catch ( ex ) { sendConsoleText ( 'FileUpload Error' ) ; this . write ( Buffer . from ( JSON . stringify ( { action : 'uploaderror' } ) ) ) ; return ; } // Write to the file, if there is a problem, error out.
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
// If data does not start with zero, save as-is.
2021-01-25 09:46:54 +03:00
this . httprequest . uploadFileSize += data . length ;
2022-01-17 00:37:26 +03:00
try { fs . writeSync ( this . httprequest . uploadFile , data ) ; } catch ( ex ) { sendConsoleText ( 'FileUpload Error' ) ; this . write ( Buffer . from ( JSON . stringify ( { action : 'uploaderror' } ) ) ) ; return ; } // Write to the file, if there is a problem, error out.
2018-01-19 02:43:43 +03:00
}
2021-01-25 02:19:29 +03:00
this . write ( Buffer . from ( JSON . stringify ( { action : 'uploadack' , reqid : this . httprequest . uploadFileid } ) ) ) ; // Ask for more data.
return ;
}
2018-01-19 02:43:43 +03:00
2022-08-04 11:52:29 +03:00
if ( this . httprequest . state == 0 ) {
2021-01-25 02:19:29 +03:00
// Check if this is a relay connection
2022-08-04 11:52:29 +03:00
if ( ( data == 'c' ) || ( data == 'cr' ) ) {
2022-08-04 21:40:11 +03:00
this . httprequest . state = 1 ;
//sendConsoleText("Tunnel #" + this.httprequest.index + " now active", this.httprequest.sessionid);
2022-07-27 06:03:24 +03:00
}
}
2022-08-04 11:52:29 +03:00
else {
2021-01-25 02:19:29 +03:00
// Handle tunnel data
2021-01-25 08:51:22 +03:00
if ( this . httprequest . protocol == 0 ) { // 1 = Terminal (admin), 2 = Desktop, 5 = Files, 6 = PowerShell (admin), 7 = Plugin Data Exchange, 8 = Terminal (user), 9 = PowerShell (user), 10 = FileTransfer
2021-01-25 02:19:29 +03:00
// Take a look at the protocol
if ( ( data . length > 3 ) && ( data [ 0 ] == '{' ) ) { onTunnelControlData ( data , this ) ; return ; }
this . httprequest . protocol = parseInt ( data ) ;
if ( typeof this . httprequest . protocol != 'number' ) { this . httprequest . protocol = 0 ; }
2021-04-09 23:27:21 +03:00
// See if this protocol request is allowed.
if ( ( this . httprequest . soptions != null ) && ( this . httprequest . soptions . usages != null ) && ( this . httprequest . soptions . usages . indexOf ( this . httprequest . protocol ) == - 1 ) ) { this . httprequest . protocol = 0 ; }
2021-01-25 08:51:22 +03:00
if ( this . httprequest . protocol == 10 ) {
2021-01-25 02:19:29 +03:00
//
// Basic file transfer
//
var stats = null ;
if ( ( process . platform != 'win32' ) && ( this . httprequest . xoptions . file . startsWith ( '/' ) == false ) ) { this . httprequest . xoptions . file = '/' + this . httprequest . xoptions . file ; }
2022-01-17 00:37:26 +03:00
try { stats = require ( 'fs' ) . statSync ( this . httprequest . xoptions . file ) } catch ( ex ) { }
try { if ( stats ) { this . httprequest . downloadFile = fs . createReadStream ( this . httprequest . xoptions . file , { flags : 'rbN' } ) ; } } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( this . httprequest . downloadFile ) {
2021-01-25 09:46:54 +03:00
MeshServerLogEx ( 106 , [ this . httprequest . xoptions . file , stats . size ] , 'Download: \"' + this . httprequest . xoptions . file + '\", Size: ' + stats . size , this . httprequest ) ;
2021-01-25 02:19:29 +03:00
//sendConsoleText('BasicFileTransfer, ok, ' + this.httprequest.xoptions.file + ', ' + JSON.stringify(stats));
this . write ( JSON . stringify ( { op : 'ok' , size : stats . size } ) ) ;
this . httprequest . downloadFile . pipe ( this ) ;
this . httprequest . downloadFile . end = function ( ) { }
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
//sendConsoleText('BasicFileTransfer, cancel, ' + this.httprequest.xoptions.file);
this . write ( JSON . stringify ( { op : 'cancel' } ) ) ;
}
2020-04-21 06:12:35 +03:00
}
2021-01-25 08:51:22 +03:00
else if ( ( this . httprequest . protocol == 1 ) || ( this . httprequest . protocol == 6 ) || ( this . httprequest . protocol == 8 ) || ( this . httprequest . protocol == 9 ) ) {
2021-01-25 02:19:29 +03:00
//
// Remote Terminal
//
2018-02-12 04:13:26 +03:00
2021-01-25 02:19:29 +03:00
// Check user access rights for terminal
2022-08-26 06:11:47 +03:00
if ( ( ( this . httprequest . rights & MESHRIGHT _REMOTECONTROL ) == 0 ) || ( ( this . httprequest . rights != 0xFFFFFFFF ) && ( ( this . httprequest . rights & MESHRIGHT _NOTERMINAL ) != 0 ) ) )
{
2021-01-25 02:19:29 +03:00
// Disengage this tunnel, user does not have the rights to do this!!
this . httprequest . protocol = 999999 ;
this . httprequest . s . end ( ) ;
sendConsoleText ( "Error: No Terminal Control Rights." ) ;
return ;
2020-08-27 04:42:41 +03:00
}
2018-11-28 04:13:01 +03:00
2021-01-25 02:19:29 +03:00
this . descriptorMetadata = "Remote Terminal" ;
2020-05-21 23:22:58 +03:00
2022-08-26 06:11:47 +03:00
if ( process . platform == 'win32' )
{
2021-01-25 08:51:22 +03:00
if ( ! require ( 'win-terminal' ) . PowerShellCapable ( ) && ( this . httprequest . protocol == 6 || this . httprequest . protocol == 9 ) ) {
2021-01-25 02:19:29 +03:00
this . httprequest . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : 'PowerShell is not supported on this version of windows' , msgid : 1 } ) ) ;
this . httprequest . s . end ( ) ;
return ;
2020-05-17 07:10:11 +03:00
}
2021-01-25 02:19:29 +03:00
}
2020-05-17 07:10:11 +03:00
2021-01-25 02:19:29 +03:00
var prom = require ( 'promise' ) ;
2022-08-26 06:11:47 +03:00
this . httprequest . tpromise = new prom ( promise _init ) ;
2021-01-25 02:19:29 +03:00
this . httprequest . tpromise . that = this ;
this . httprequest . tpromise . httprequest = this . httprequest ;
2022-08-26 06:11:47 +03:00
this . end = terminal _end ;
2021-01-25 02:19:29 +03:00
// Perform User-Consent if needed.
2022-08-26 06:11:47 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 16 ) )
{
2021-01-25 02:19:29 +03:00
this . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : "Waiting for user to grant access..." , msgid : 1 } ) ) ;
2022-01-13 10:44:47 +03:00
var consentMessage = currentTranslation [ 'terminalConsent' ] . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ;
var consentTitle = 'MeshCentral' ;
2022-08-26 06:11:47 +03:00
if ( this . httprequest . soptions != null )
{
2021-01-25 02:19:29 +03:00
if ( this . httprequest . soptions . consentTitle != null ) { consentTitle = this . httprequest . soptions . consentTitle ; }
if ( this . httprequest . soptions . consentMsgTerminal != null ) { consentMessage = this . httprequest . soptions . consentMsgTerminal . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ; }
2020-05-17 07:10:11 +03:00
}
2022-08-26 06:11:47 +03:00
if ( process . platform == 'win32' )
{
2021-12-14 04:37:22 +03:00
var enhanced = false ;
2022-02-22 23:32:49 +03:00
try { require ( 'win-userconsent' ) ; enhanced = true ; } catch ( ex ) { }
2022-08-26 06:11:47 +03:00
if ( enhanced )
{
2021-12-14 04:37:22 +03:00
var ipr = server _getUserImage ( this . httprequest . userid ) ;
ipr . consentTitle = consentTitle ;
ipr . consentMessage = consentMessage ;
2022-02-22 23:32:49 +03:00
ipr . consentTimeout = this . httprequest . consentTimeout ;
2022-08-04 11:52:29 +03:00
ipr . consentAutoAccept = this . httprequest . consentAutoAccept ;
2021-12-14 04:37:22 +03:00
ipr . username = this . httprequest . realname ;
2022-01-13 10:44:47 +03:00
ipr . translations = { Allow : currentTranslation [ 'allow' ] , Deny : currentTranslation [ 'deny' ] , Auto : currentTranslation [ 'autoAllowForFive' ] , Caption : consentMessage } ;
2022-08-26 06:11:47 +03:00
this . httprequest . tpromise . _consent = ipr . then ( function ( img )
{
2022-02-23 02:52:15 +03:00
this . consent = require ( 'win-userconsent' ) . create ( this . consentTitle , this . consentMessage , this . username , { b64Image : img . split ( ',' ) . pop ( ) , timeout : this . consentTimeout * 1000 , timeoutAutoAccept : this . consentAutoAccept , translations : this . translations , background : color _options . background , foreground : color _options . foreground } ) ;
2021-12-14 04:37:22 +03:00
this . _ _childPromise . close = this . consent . close . bind ( this . consent ) ;
return ( this . consent ) ;
} ) ;
2022-08-26 06:11:47 +03:00
} else
{
this . httprequest . tpromise . _consent = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout ) ;
2021-12-14 04:37:22 +03:00
}
2022-08-26 06:11:47 +03:00
} else
{
this . httprequest . tpromise . _consent = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout ) ;
2021-12-14 04:37:22 +03:00
}
2021-01-25 02:19:29 +03:00
this . httprequest . tpromise . _consent . retPromise = this . httprequest . tpromise ;
this . httprequest . tpromise . _consent . then (
2022-08-26 06:11:47 +03:00
function ( always )
{
if ( always && process . platform == 'win32' ) { server _set _consentTimer ( this . retPromise . httprequest . userid ) ; }
2021-12-14 04:37:22 +03:00
2021-01-25 02:19:29 +03:00
// Success
MeshServerLogEx ( 27 , null , "Local user accepted remote terminal request (" + this . retPromise . httprequest . remoteaddr + ")" , this . retPromise . that . httprequest ) ;
this . retPromise . that . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : null , msgid : 0 } ) ) ;
this . retPromise . _consent = null ;
this . retPromise . _res ( ) ;
} ,
2021-01-25 08:51:22 +03:00
function ( e ) {
2021-01-25 02:19:29 +03:00
// Denied
MeshServerLogEx ( 28 , null , "Local user rejected remote terminal request (" + this . retPromise . that . httprequest . remoteaddr + ")" , this . retPromise . that . httprequest ) ;
this . retPromise . that . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : e . toString ( ) , msgid : 2 } ) ) ;
2022-08-26 06:11:47 +03:00
this . retPromise . _consent = null ;
2021-01-25 02:19:29 +03:00
this . retPromise . _rej ( e . toString ( ) ) ;
} ) ;
}
2022-08-26 06:11:47 +03:00
else
{
2021-01-25 02:19:29 +03:00
// User-Consent is not required, so just resolve this promise
this . httprequest . tpromise . _res ( ) ;
}
2020-01-29 02:08:51 +03:00
2022-08-26 06:11:47 +03:00
this . httprequest . tpromise . then ( terminal _promise _consent _resolved , terminal _promise _consent _rejected ) ;
2021-01-25 02:19:29 +03:00
}
2022-08-26 06:11:47 +03:00
else if ( this . httprequest . protocol == 2 )
{
2021-01-25 02:19:29 +03:00
//
2021-04-09 23:27:21 +03:00
// Remote Desktop
2021-01-25 02:19:29 +03:00
//
// Check user access rights for desktop
2021-01-25 08:51:22 +03:00
if ( ( ( ( this . httprequest . rights & MESHRIGHT _REMOTECONTROL ) == 0 ) && ( ( this . httprequest . rights & MESHRIGHT _REMOTEVIEW ) == 0 ) ) || ( ( this . httprequest . rights != 0xFFFFFFFF ) && ( ( this . httprequest . rights & MESHRIGHT _NODESKTOP ) != 0 ) ) ) {
2021-01-25 02:19:29 +03:00
// Disengage this tunnel, user does not have the rights to do this!!
this . httprequest . protocol = 999999 ;
this . httprequest . s . end ( ) ;
sendConsoleText ( "Error: No Desktop Control Rights." ) ;
return ;
2020-05-17 07:10:11 +03:00
}
2018-11-28 04:13:01 +03:00
2021-01-25 02:19:29 +03:00
this . descriptorMetadata = "Remote KVM" ;
// Look for a TSID
var tsid = null ;
if ( ( this . httprequest . xoptions != null ) && ( typeof this . httprequest . xoptions . tsid == 'number' ) ) { tsid = this . httprequest . xoptions . tsid ; }
require ( 'MeshAgent' ) . _tsid = tsid ;
2020-05-21 23:22:58 +03:00
2024-03-15 15:13:19 +03:00
// If MacOS, Wake up device with caffeinate
if ( process . platform == 'darwin' ) {
try {
var options = { } ;
try { options . uid = require ( 'user-sessions' ) . consoleUid ( ) ; } catch ( ex ) { }
options . type = require ( 'child_process' ) . SpawnTypes . TERM ;
var replydata = "" ;
var cmdchild = require ( 'child_process' ) . execFile ( '/usr/bin/caffeinate' , [ 'caffeinate' , '-u' , '-t' , '10' ] , options ) ;
cmdchild . descriptorMetadata = 'UserCommandsShell' ;
cmdchild . stdout . on ( 'data' , function ( c ) { replydata += c . toString ( ) ; } ) ;
cmdchild . stderr . on ( 'data' , function ( c ) { replydata + c . toString ( ) ; } ) ;
cmdchild . on ( 'exit' , function ( ) { delete cmdchild ; } ) ;
} catch ( err ) { }
}
2021-01-25 02:19:29 +03:00
// Remote desktop using native pipes
this . httprequest . desktop = { state : 0 , kvm : mesh . getRemoteDesktopStream ( tsid ) , tunnel : this } ;
this . httprequest . desktop . kvm . parent = this . httprequest . desktop ;
this . desktop = this . httprequest . desktop ;
2019-12-13 04:45:42 +03:00
2021-01-25 02:19:29 +03:00
// Add ourself to the list of remote desktop sessions
if ( this . httprequest . desktop . kvm . tunnels == null ) { this . httprequest . desktop . kvm . tunnels = [ ] ; }
this . httprequest . desktop . kvm . tunnels . push ( this ) ;
2018-04-20 04:19:15 +03:00
2021-01-25 02:19:29 +03:00
// Send a metadata update to all desktop sessions
var users = { } ;
2022-08-26 06:11:47 +03:00
if ( this . httprequest . desktop . kvm . tunnels != null )
{
for ( var i in this . httprequest . desktop . kvm . tunnels )
{
2021-06-25 21:49:58 +03:00
try {
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest . desktop . kvm . tunnels [ i ] . httprequest ) ;
if ( users [ userid ] == null ) { users [ userid ] = 1 ; } else { users [ userid ] ++ ; }
} catch ( ex ) { sendConsoleText ( ex ) ; }
}
2022-08-26 06:11:47 +03:00
for ( var i in this . httprequest . desktop . kvm . tunnels )
{
2022-01-17 05:28:26 +03:00
try { this . httprequest . desktop . kvm . tunnels [ i ] . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'metadata' , users : users } ) ) ; } catch ( ex ) { }
2021-06-25 21:49:58 +03:00
}
2021-01-25 02:19:29 +03:00
tunnelUserCount . desktop = users ;
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'kvm' , value : users } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
}
2022-08-26 06:11:47 +03:00
this . end = tunnel _kvm _end ;
2019-10-10 21:13:25 +03:00
2021-01-25 08:51:22 +03:00
if ( this . httprequest . desktop . kvm . hasOwnProperty ( 'connectionCount' ) ) {
2021-01-25 02:19:29 +03:00
this . httprequest . desktop . kvm . connectionCount ++ ;
this . httprequest . desktop . kvm . rusers . push ( this . httprequest . realname ) ;
this . httprequest . desktop . kvm . users . push ( this . httprequest . username ) ;
this . httprequest . desktop . kvm . rusers . sort ( ) ;
this . httprequest . desktop . kvm . users . sort ( ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
this . httprequest . desktop . kvm . connectionCount = 1 ;
this . httprequest . desktop . kvm . rusers = [ this . httprequest . realname ] ;
this . httprequest . desktop . kvm . users = [ this . httprequest . username ] ;
}
2018-11-28 04:13:01 +03:00
2022-08-26 06:11:47 +03:00
if ( ( this . httprequest . desktopviewonly != true ) && ( ( this . httprequest . rights == 0xFFFFFFFF ) || ( ( ( this . httprequest . rights & MESHRIGHT _REMOTECONTROL ) != 0 ) && ( ( this . httprequest . rights & MESHRIGHT _REMOTEVIEW ) == 0 ) ) ) )
{
2021-01-25 02:19:29 +03:00
// If we have remote control rights, pipe the KVM input
this . pipe ( this . httprequest . desktop . kvm , { dataTypeSkip : 1 , end : false } ) ; // 0 = Binary, 1 = Text. Pipe the Browser --> KVM input.
2022-08-26 06:11:47 +03:00
}
else
{
2021-01-25 02:19:29 +03:00
// We need to only pipe non-mouse & non-keyboard inputs.
2021-06-30 10:01:44 +03:00
// sendConsoleText('Warning: No Remote Desktop Input Rights.');
2021-01-25 02:19:29 +03:00
// TODO!!!
}
2018-11-28 04:13:01 +03:00
2021-01-25 02:19:29 +03:00
// Perform notification if needed. Toast messages may not be supported on all platforms.
2022-08-26 06:11:47 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 8 ) )
{
2021-01-25 02:19:29 +03:00
// User Consent Prompt is required
// Send a console message back using the console channel, "\n" is supported.
this . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : "Waiting for user to grant access..." , msgid : 1 } ) ) ;
2022-01-13 10:44:47 +03:00
var consentMessage = currentTranslation [ 'desktopConsent' ] . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ;
var consentTitle = 'MeshCentral' ;
2022-08-26 06:11:47 +03:00
if ( this . httprequest . soptions != null )
{
2021-01-25 02:19:29 +03:00
if ( this . httprequest . soptions . consentTitle != null ) { consentTitle = this . httprequest . soptions . consentTitle ; }
if ( this . httprequest . soptions . consentMsgDesktop != null ) { consentMessage = this . httprequest . soptions . consentMsgDesktop . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ; }
}
2021-12-14 04:37:22 +03:00
var pr ;
2022-08-26 06:11:47 +03:00
if ( process . platform == 'win32' )
{
2021-12-14 04:37:22 +03:00
var enhanced = false ;
2022-02-22 23:32:49 +03:00
try { require ( 'win-userconsent' ) ; enhanced = true ; } catch ( ex ) { }
2022-08-26 06:11:47 +03:00
if ( enhanced )
{
2021-12-14 04:37:22 +03:00
var ipr = server _getUserImage ( this . httprequest . userid ) ;
ipr . consentTitle = consentTitle ;
ipr . consentMessage = consentMessage ;
2022-02-22 23:32:49 +03:00
ipr . consentTimeout = this . httprequest . consentTimeout ;
2022-08-04 11:52:29 +03:00
ipr . consentAutoAccept = this . httprequest . consentAutoAccept ;
2021-12-14 04:37:22 +03:00
ipr . tsid = tsid ;
ipr . username = this . httprequest . realname ;
2022-01-13 10:44:47 +03:00
ipr . translation = { Allow : currentTranslation [ 'allow' ] , Deny : currentTranslation [ 'deny' ] , Auto : currentTranslation [ 'autoAllowForFive' ] , Caption : consentMessage } ;
2022-08-26 06:11:47 +03:00
pr = ipr . then ( function ( img )
{
2022-02-23 02:52:15 +03:00
this . consent = require ( 'win-userconsent' ) . create ( this . consentTitle , this . consentMessage , this . username , { b64Image : img . split ( ',' ) . pop ( ) , uid : this . tsid , timeout : this . consentTimeout * 1000 , timeoutAutoAccept : this . consentAutoAccept , translations : this . translation , background : color _options . background , foreground : color _options . foreground } ) ;
2021-12-14 04:37:22 +03:00
this . _ _childPromise . close = this . consent . close . bind ( this . consent ) ;
return ( this . consent ) ;
} ) ;
}
2022-08-26 06:11:47 +03:00
else
{
pr = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout , null , tsid ) ;
2021-12-14 04:37:22 +03:00
}
}
2022-08-26 06:11:47 +03:00
else
{
pr = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout , null , tsid ) ;
2021-12-14 04:37:22 +03:00
}
2021-01-25 02:19:29 +03:00
pr . ws = this ;
this . pause ( ) ;
this . _consentpromise = pr ;
2022-08-26 06:11:47 +03:00
this . prependOnceListener ( 'end' , kvm _tunnel _consentpromise _closehandler ) ;
pr . then ( kvm _consentpromise _resolved , kvm _consentpromise _rejected ) ;
}
else
{
2021-01-25 02:19:29 +03:00
// User Consent Prompt is not required
2022-08-26 06:11:47 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 1 ) )
{
2021-01-25 02:19:29 +03:00
// User Notifications is required
MeshServerLogEx ( 35 , null , "Started remote desktop with toast notification (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
2022-01-17 08:30:38 +03:00
var notifyMessage = currentTranslation [ 'desktopNotify' ] . replace ( '{0}' , this . httprequest . realname ) ;
2022-01-15 02:54:12 +03:00
var notifyTitle = "MeshCentral" ;
2021-01-25 08:51:22 +03:00
if ( this . httprequest . soptions != null ) {
2021-01-25 02:19:29 +03:00
if ( this . httprequest . soptions . notifyTitle != null ) { notifyTitle = this . httprequest . soptions . notifyTitle ; }
if ( this . httprequest . soptions . notifyMsgDesktop != null ) { notifyMessage = this . httprequest . soptions . notifyMsgDesktop . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ; }
2019-06-15 02:33:53 +03:00
}
2022-01-17 05:28:26 +03:00
try { require ( 'toaster' ) . Toast ( notifyTitle , notifyMessage , tsid ) ; } catch ( ex ) { }
2022-08-26 06:11:47 +03:00
} else
{
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 36 , null , "Started remote desktop without notification (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
}
2022-08-26 06:11:47 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 0x40 ) )
{
2021-01-25 02:19:29 +03:00
// Connection Bar is required
2022-08-26 06:11:47 +03:00
if ( this . httprequest . desktop . kvm . connectionBar )
{
2021-01-25 02:19:29 +03:00
this . httprequest . desktop . kvm . connectionBar . removeAllListeners ( 'close' ) ;
this . httprequest . desktop . kvm . connectionBar . close ( ) ;
}
2022-08-26 06:11:47 +03:00
try
{
2024-01-13 01:42:26 +03:00
this . httprequest . desktop . kvm . connectionBar = require ( 'notifybar-desktop' ) ( this . httprequest . privacybartext . replace ( '{0}' , this . httprequest . desktop . kvm . rusers . join ( ', ' ) ) . replace ( '{1}' , this . httprequest . desktop . kvm . users . join ( ', ' ) ) . replace ( /'/g , "\\'\\" ) , require ( 'MeshAgent' ) . _tsid , color _options ) ;
2021-07-29 22:01:38 +03:00
MeshServerLogEx ( 31 , null , "Remote Desktop Connection Bar Activated/Updated (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
2022-01-17 06:12:13 +03:00
} catch ( ex ) {
2021-07-29 22:01:38 +03:00
MeshServerLogEx ( 32 , null , "Remote Desktop Connection Bar Failed or not Supported (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
2021-01-25 02:19:29 +03:00
}
2022-08-26 06:11:47 +03:00
if ( this . httprequest . desktop . kvm . connectionBar )
{
this . httprequest . desktop . kvm . connectionBar . state =
{
userid : this . httprequest . userid ,
xuserid : this . httprequest . xuserid ,
username : this . httprequest . username ,
sessionid : this . httprequest . sessionid ,
remoteaddr : this . httprequest . remoteaddr ,
guestname : this . httprequest . guestname ,
desktop : this . httprequest . desktop
} ;
this . httprequest . desktop . kvm . connectionBar . on ( 'close' , function ( )
{
console . info1 ( 'Connection Bar Forcefully closed' ) ;
MeshServerLogEx ( 29 , null , "Remote Desktop Connection forcefully closed by local user (" + this . state . remoteaddr + ")" , this . state ) ;
for ( var i in this . state . desktop . kvm . _pipedStreams )
{
this . state . desktop . kvm . _pipedStreams [ i ] . end ( ) ;
2021-01-25 02:19:29 +03:00
}
2022-08-26 06:11:47 +03:00
this . state . desktop . kvm . end ( ) ;
2021-01-25 02:19:29 +03:00
} ) ;
2019-10-11 20:53:42 +03:00
}
2019-06-15 02:33:53 +03:00
}
2021-01-25 02:19:29 +03:00
this . httprequest . desktop . kvm . pipe ( this , { dataTypeSkip : 1 } ) ;
2022-08-26 06:11:47 +03:00
if ( this . httprequest . autolock )
{
2021-09-03 10:55:00 +03:00
destopLockHelper _pipe ( this . httprequest ) ;
}
2021-01-25 02:19:29 +03:00
}
2019-06-15 02:33:53 +03:00
2021-01-25 02:19:29 +03:00
this . removeAllListeners ( 'data' ) ;
this . on ( 'data' , onTunnelControlData ) ;
//this.write('MeshCore KVM Hello!1');
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . protocol == 5 ) {
2021-01-25 02:19:29 +03:00
//
// Remote Files
//
2020-05-21 23:22:58 +03:00
2021-01-25 02:19:29 +03:00
// Check user access rights for files
2021-01-25 08:51:22 +03:00
if ( ( ( this . httprequest . rights & MESHRIGHT _REMOTECONTROL ) == 0 ) || ( ( this . httprequest . rights != 0xFFFFFFFF ) && ( ( this . httprequest . rights & MESHRIGHT _NOFILES ) != 0 ) ) ) {
2021-01-25 02:19:29 +03:00
// Disengage this tunnel, user does not have the rights to do this!!
this . httprequest . protocol = 999999 ;
this . httprequest . s . end ( ) ;
sendConsoleText ( "Error: No files control rights." ) ;
return ;
}
2018-11-28 04:13:01 +03:00
2021-01-25 02:19:29 +03:00
this . descriptorMetadata = "Remote Files" ;
2020-05-21 23:22:58 +03:00
2021-01-25 02:19:29 +03:00
// Add the files session to the count to update the server
2021-01-25 08:51:22 +03:00
if ( this . httprequest . userid != null ) {
2021-06-25 21:49:58 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest ) ;
if ( tunnelUserCount . files [ userid ] == null ) { tunnelUserCount . files [ userid ] = 1 ; } else { tunnelUserCount . files [ userid ] ++ ; }
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'files' , value : tunnelUserCount . files } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
}
2022-08-26 06:11:47 +03:00
this . end = function ( )
{
2021-01-25 02:19:29 +03:00
// Remove the files session from the count to update the server
2021-01-25 08:51:22 +03:00
if ( this . httprequest . userid != null ) {
2021-06-25 21:49:58 +03:00
var userid = getUserIdAndGuestNameFromHttpRequest ( this . httprequest ) ;
if ( tunnelUserCount . files [ userid ] != null ) { tunnelUserCount . files [ userid ] -- ; if ( tunnelUserCount . files [ userid ] <= 0 ) { delete tunnelUserCount . files [ userid ] ; } }
2022-01-17 05:28:26 +03:00
try { mesh . SendCommand ( { action : 'sessions' , type : 'files' , value : tunnelUserCount . files } ) ; } catch ( ex ) { }
2020-10-12 08:24:30 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2020-05-16 01:23:37 +03:00
}
2021-01-25 02:19:29 +03:00
} ;
2020-05-16 01:23:37 +03:00
2021-01-25 02:19:29 +03:00
// Perform notification if needed. Toast messages may not be supported on all platforms.
2022-08-26 06:11:47 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 32 ) )
{
2021-01-25 02:19:29 +03:00
// User Consent Prompt is required
// Send a console message back using the console channel, "\n" is supported.
this . write ( JSON . stringify ( { ctrlChannel : '102938' , type : 'console' , msg : "Waiting for user to grant access..." , msgid : 1 } ) ) ;
2022-01-13 10:44:47 +03:00
var consentMessage = currentTranslation [ 'fileConsent' ] . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ;
var consentTitle = 'MeshCentral' ;
2022-08-26 06:11:47 +03:00
if ( this . httprequest . soptions != null )
{
2021-01-25 02:19:29 +03:00
if ( this . httprequest . soptions . consentTitle != null ) { consentTitle = this . httprequest . soptions . consentTitle ; }
if ( this . httprequest . soptions . consentMsgFiles != null ) { consentMessage = this . httprequest . soptions . consentMsgFiles . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ; }
}
2021-12-14 04:37:22 +03:00
var pr ;
2022-08-26 06:11:47 +03:00
if ( process . platform == 'win32' )
{
2021-12-14 04:37:22 +03:00
var enhanced = false ;
2022-02-22 23:32:49 +03:00
try { require ( 'win-userconsent' ) ; enhanced = true ; } catch ( ex ) { }
2022-08-26 06:11:47 +03:00
if ( enhanced )
{
2021-12-14 04:37:22 +03:00
var ipr = server _getUserImage ( this . httprequest . userid ) ;
ipr . consentTitle = consentTitle ;
ipr . consentMessage = consentMessage ;
2022-02-22 23:32:49 +03:00
ipr . consentTimeout = this . httprequest . consentTimeout ;
2022-08-04 11:52:29 +03:00
ipr . consentAutoAccept = this . httprequest . consentAutoAccept ;
2021-12-14 04:37:22 +03:00
ipr . username = this . httprequest . realname ;
2022-01-13 10:44:47 +03:00
ipr . translations = { Allow : currentTranslation [ 'allow' ] , Deny : currentTranslation [ 'deny' ] , Auto : currentTranslation [ 'autoAllowForFive' ] , Caption : consentMessage } ;
2022-08-26 06:11:47 +03:00
pr = ipr . then ( function ( img )
{
2022-02-23 02:52:15 +03:00
this . consent = require ( 'win-userconsent' ) . create ( this . consentTitle , this . consentMessage , this . username , { b64Image : img . split ( ',' ) . pop ( ) , timeout : this . consentTimeout * 1000 , timeoutAutoAccept : this . consentAutoAccept , translations : this . translations , background : color _options . background , foreground : color _options . foreground } ) ;
2021-12-14 04:37:22 +03:00
this . _ _childPromise . close = this . consent . close . bind ( this . consent ) ;
return ( this . consent ) ;
} ) ;
2022-08-26 06:11:47 +03:00
} else
{
pr = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout , null ) ;
2021-12-14 04:37:22 +03:00
}
2022-08-26 06:11:47 +03:00
}
else
{
pr = require ( 'message-box' ) . create ( consentTitle , consentMessage , this . httprequest . consentTimeout , null ) ;
2021-12-14 04:37:22 +03:00
}
2021-01-25 02:19:29 +03:00
pr . ws = this ;
this . pause ( ) ;
this . _consentpromise = pr ;
2022-08-26 06:11:47 +03:00
this . prependOnceListener ( 'end' , files _tunnel _endhandler ) ;
pr . then ( files _consentpromise _resolved , files _consentpromise _rejected ) ;
2022-01-15 02:54:12 +03:00
}
2022-08-26 06:11:47 +03:00
else
{
2021-01-25 02:19:29 +03:00
// User Consent Prompt is not required
2022-01-17 00:37:26 +03:00
if ( this . httprequest . consent && ( this . httprequest . consent & 4 ) ) {
2021-01-25 02:19:29 +03:00
// User Notifications is required
MeshServerLogEx ( 42 , null , "Started remote files with toast notification (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
2022-02-02 07:29:28 +03:00
var notifyMessage = currentTranslation [ 'fileNotify' ] . replace ( '{0}' , this . httprequest . realname ) ;
2022-01-15 02:54:12 +03:00
var notifyTitle = "MeshCentral" ;
2022-01-17 00:37:26 +03:00
if ( this . httprequest . soptions != null ) {
2021-01-25 02:19:29 +03:00
if ( this . httprequest . soptions . notifyTitle != null ) { notifyTitle = this . httprequest . soptions . notifyTitle ; }
if ( this . httprequest . soptions . notifyMsgFiles != null ) { notifyMessage = this . httprequest . soptions . notifyMsgFiles . replace ( '{0}' , this . httprequest . realname ) . replace ( '{1}' , this . httprequest . username ) ; }
2019-06-15 02:33:53 +03:00
}
2022-01-17 05:28:26 +03:00
try { require ( 'toaster' ) . Toast ( notifyTitle , notifyMessage ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 43 , null , "Started remote files without notification (" + this . httprequest . remoteaddr + ")" , this . httprequest ) ;
2019-04-13 00:19:03 +03:00
}
2021-01-25 02:19:29 +03:00
this . resume ( ) ;
}
2019-04-13 00:19:03 +03:00
2021-01-25 02:19:29 +03:00
// Setup files
// NOP
}
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . protocol == 1 ) {
2021-01-25 02:19:29 +03:00
// Send data into terminal stdin
//this.write(data); // Echo back the keys (Does not seem to be a good idea)
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . protocol == 2 ) {
2021-01-25 02:19:29 +03:00
// Send data into remote desktop
2021-01-25 08:51:22 +03:00
if ( this . httprequest . desktop . state == 0 ) {
2021-01-25 02:19:29 +03:00
this . write ( Buffer . from ( String . fromCharCode ( 0x11 , 0xFE , 0x00 , 0x00 , 0x4D , 0x45 , 0x53 , 0x48 , 0x00 , 0x00 , 0x00 , 0x00 , 0x02 ) ) ) ;
this . httprequest . desktop . state = 1 ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
this . httprequest . desktop . write ( data ) ;
}
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . protocol == 5 ) {
2021-01-25 02:19:29 +03:00
// Process files commands
var cmd = null ;
2022-01-17 05:28:26 +03:00
try { cmd = JSON . parse ( data ) ; } catch ( ex ) { } ;
2021-01-25 02:19:29 +03:00
if ( cmd == null ) { return ; }
if ( ( cmd . ctrlChannel == '102938' ) || ( ( cmd . type == 'offer' ) && ( cmd . sdp != null ) ) ) { onTunnelControlData ( cmd , this ) ; return ; } // If this is control data, handle it now.
if ( cmd . action == undefined ) { return ; }
//sendConsoleText('CMD: ' + JSON.stringify(cmd));
if ( ( cmd . path != null ) && ( process . platform != 'win32' ) && ( cmd . path [ 0 ] != '/' ) ) { cmd . path = '/' + cmd . path ; } // Add '/' to paths on non-windows
//console.log(objToString(cmd, 0, ' '));
2021-01-25 08:51:22 +03:00
switch ( cmd . action ) {
2021-01-25 02:19:29 +03:00
case 'ls' : {
/ *
// Close the watcher if required
var samepath = ( ( this . httprequest . watcher != undefined ) && ( cmd . path == this . httprequest . watcher . path ) ) ;
if ( ( this . httprequest . watcher != undefined ) && ( samepath == false ) ) {
//console.log('Closing watcher: ' + this.httprequest.watcher.path);
//this.httprequest.watcher.close(); // TODO: This line causes the agent to crash!!!!
delete this . httprequest . watcher ;
}
* /
// Send the folder content to the browser
var response = getDirectoryInfo ( cmd . path ) ;
2021-10-21 00:04:10 +03:00
response . reqid = cmd . reqid ;
2021-01-25 02:19:29 +03:00
this . write ( Buffer . from ( JSON . stringify ( response ) ) ) ;
/ *
// Start the directory watcher
if ( ( cmd . path != '' ) && ( samepath == false ) ) {
var watcher = fs . watch ( cmd . path , onFileWatcher ) ;
watcher . tunnel = this . httprequest ;
watcher . path = cmd . path ;
this . httprequest . watcher = watcher ;
//console.log('Starting watcher: ' + this.httprequest.watcher.path);
}
* /
break ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
case 'mkdir' : {
// Create a new empty folder
fs . mkdirSync ( cmd . path ) ;
MeshServerLogEx ( 44 , [ cmd . path ] , "Create folder: \"" + cmd . path + "\"" , this . httprequest ) ;
break ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
case 'rm' : {
// Delete, possibly recursive delete
2021-01-25 08:51:22 +03:00
for ( var i in cmd . delfiles ) {
2021-01-25 02:19:29 +03:00
var p = obj . path . join ( cmd . path , cmd . delfiles [ i ] ) , delcount = 0 ;
2022-01-17 05:28:26 +03:00
try { delcount = deleteFolderRecursive ( p , cmd . rec ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( ( delcount == 1 ) && ! cmd . rec ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 45 , [ p ] , "Delete: \"" + p + "\"" , this . httprequest ) ;
2021-01-25 08:51:22 +03:00
} else {
if ( cmd . rec ) {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 46 , [ p , delcount ] , "Delete recursive: \"" + p + "\", " + delcount + " element(s) removed" , this . httprequest ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( 47 , [ p , delcount ] , "Delete: \"" + p + "\", " + delcount + " element(s) removed" , this . httprequest ) ;
}
2017-08-28 19:27:45 +03:00
}
}
2021-01-25 02:19:29 +03:00
break ;
}
2024-04-03 11:51:18 +03:00
case 'open' : {
// Open the local file/folder on the users desktop
if ( cmd . path ) {
MeshServerLogEx ( 20 , [ cmd . path ] , "Opening: " + cmd . path , cmd ) ;
openFileOnDesktop ( cmd . path ) ;
}
}
2021-01-25 02:19:29 +03:00
case 'markcoredump' : {
// If we are asking for the coredump file, set the right path.
var coreDumpPath = null ;
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
if ( fs . existsSync ( process . coreDumpLocation ) ) { coreDumpPath = process . coreDumpLocation ; }
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
if ( ( process . cwd ( ) != '//' ) && fs . existsSync ( process . cwd ( ) + 'core' ) ) { coreDumpPath = process . cwd ( ) + 'core' ; }
}
if ( coreDumpPath != null ) { db . Put ( 'CoreDumpTime' , require ( 'fs' ) . statSync ( coreDumpPath ) . mtime ) ; }
break ;
}
case 'rename' :
{
// Rename a file or folder
var oldfullpath = obj . path . join ( cmd . path , cmd . oldname ) ;
var newfullpath = obj . path . join ( cmd . path , cmd . newname ) ;
MeshServerLogEx ( 48 , [ oldfullpath , cmd . newname ] , 'Rename: \"' + oldfullpath + '\" to \"' + cmd . newname + '\"' , this . httprequest ) ;
2022-01-17 05:28:26 +03:00
try { fs . renameSync ( oldfullpath , newfullpath ) ; } catch ( ex ) { console . log ( ex ) ; }
2017-08-28 19:27:45 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'findfile' :
{
// Search for files
var r = require ( 'file-search' ) . find ( '"' + cmd . path + '"' , cmd . filter ) ;
if ( ! r . cancel ) { r . cancel = function cancel ( ) { this . child . kill ( ) ; } ; }
this . _search = r ;
r . socket = this ;
r . socket . reqid = cmd . reqid ; // Search request id. This is used to send responses and cancel the request.
r . socket . path = cmd . path ; // Search path
r . on ( 'result' , function ( str ) { try { this . socket . write ( Buffer . from ( JSON . stringify ( { action : 'findfile' , r : str . substring ( this . socket . path . length ) , reqid : this . socket . reqid } ) ) ) ; } catch ( ex ) { } } ) ;
r . then ( function ( ) { try { this . socket . write ( Buffer . from ( JSON . stringify ( { action : 'findfile' , r : null , reqid : this . socket . reqid } ) ) ) ; } catch ( ex ) { } } ) ;
2017-08-28 19:27:45 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'cancelfindfile' :
{
if ( this . _search ) { this . _search . cancel ( ) ; this . _search = null ; }
2020-07-08 09:56:08 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'download' :
{
// Download a file
var sendNextBlock = 0 ;
2021-01-25 08:51:22 +03:00
if ( cmd . sub == 'start' ) { // Setup the download
if ( ( cmd . path == null ) && ( cmd . ask == 'coredump' ) ) { // If we are asking for the coredump file, set the right path.
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
if ( fs . existsSync ( process . coreDumpLocation ) ) { cmd . path = process . coreDumpLocation ; }
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
if ( ( process . cwd ( ) != '//' ) && fs . existsSync ( process . cwd ( ) + 'core' ) ) { cmd . path = process . cwd ( ) + 'core' ; }
2021-01-18 05:36:53 +03:00
}
}
2021-01-25 02:19:29 +03:00
MeshServerLogEx ( ( cmd . ask == 'coredump' ) ? 104 : 49 , [ cmd . path ] , 'Download: \"' + cmd . path + '\"' , this . httprequest ) ;
if ( ( cmd . path == null ) || ( this . filedownload != null ) ) { this . write ( { action : 'download' , sub : 'cancel' , id : this . filedownload . id } ) ; delete this . filedownload ; }
this . filedownload = { id : cmd . id , path : cmd . path , ptr : 0 }
2022-01-17 05:28:26 +03:00
try { this . filedownload . f = fs . openSync ( this . filedownload . path , 'rbN' ) ; } catch ( ex ) { this . write ( { action : 'download' , sub : 'cancel' , id : this . filedownload . id } ) ; delete this . filedownload ; }
2021-01-25 02:19:29 +03:00
if ( this . filedownload ) { this . write ( { action : 'download' , sub : 'start' , id : cmd . id } ) ; }
2021-01-25 08:51:22 +03:00
} else if ( ( this . filedownload != null ) && ( cmd . id == this . filedownload . id ) ) { // Download commands
2021-01-25 02:19:29 +03:00
if ( cmd . sub == 'startack' ) { sendNextBlock = ( ( typeof cmd . ack == 'number' ) ? cmd . ack : 8 ) ; } else if ( cmd . sub == 'stop' ) { delete this . filedownload ; } else if ( cmd . sub == 'ack' ) { sendNextBlock = 1 ; }
2021-01-18 05:36:53 +03:00
}
2021-01-25 02:19:29 +03:00
// Send the next download block(s)
2021-01-25 08:51:22 +03:00
while ( sendNextBlock > 0 ) {
2021-01-25 02:19:29 +03:00
sendNextBlock -- ;
var buf = Buffer . alloc ( 16384 ) ;
var len = fs . readSync ( this . filedownload . f , buf , 4 , 16380 , null ) ;
this . filedownload . ptr += len ;
if ( len < 16380 ) { buf . writeInt32BE ( 0x01000001 , 0 ) ; fs . closeSync ( this . filedownload . f ) ; delete this . filedownload ; sendNextBlock = 0 ; } else { buf . writeInt32BE ( 0x01000000 , 0 ) ; }
this . write ( buf . slice ( 0 , len + 4 ) ) ; // Write as binary
2021-01-15 10:57:52 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'upload' :
{
// Upload a file, browser to agent
if ( this . httprequest . uploadFile != null ) { fs . closeSync ( this . httprequest . uploadFile ) ; delete this . httprequest . uploadFile ; }
if ( cmd . path == undefined ) break ;
var filepath = cmd . name ? obj . path . join ( cmd . path , cmd . name ) : cmd . path ;
this . httprequest . uploadFilePath = filepath ;
2021-01-25 09:46:54 +03:00
this . httprequest . uploadFileSize = 0 ;
2022-01-17 05:28:26 +03:00
try { this . httprequest . uploadFile = fs . openSync ( filepath , cmd . append ? 'abN' : 'wbN' ) ; } catch ( ex ) { this . write ( Buffer . from ( JSON . stringify ( { action : 'uploaderror' , reqid : cmd . reqid } ) ) ) ; break ; }
2021-01-25 02:19:29 +03:00
this . httprequest . uploadFileid = cmd . reqid ;
if ( this . httprequest . uploadFile ) { this . write ( Buffer . from ( JSON . stringify ( { action : 'uploadstart' , reqid : this . httprequest . uploadFileid } ) ) ) ; }
break ;
}
case 'uploaddone' :
{
// Indicates that an upload is done
2021-01-25 08:51:22 +03:00
if ( this . httprequest . uploadFile ) {
2021-01-25 09:46:54 +03:00
MeshServerLogEx ( 105 , [ this . httprequest . uploadFilePath , this . httprequest . uploadFileSize ] , 'Upload: \"' + this . httprequest . uploadFilePath + '\", Size: ' + this . httprequest . uploadFileSize , this . httprequest ) ;
2021-01-25 02:19:29 +03:00
fs . closeSync ( this . httprequest . uploadFile ) ;
this . write ( Buffer . from ( JSON . stringify ( { action : 'uploaddone' , reqid : this . httprequest . uploadFileid } ) ) ) ; // Indicate that we closed the file.
delete this . httprequest . uploadFile ;
delete this . httprequest . uploadFileid ;
delete this . httprequest . uploadFilePath ;
2021-01-25 09:46:54 +03:00
delete this . httprequest . uploadFileSize ;
2020-08-13 22:29:18 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'uploadcancel' :
{
// Indicates that an upload is canceled
2021-01-25 08:51:22 +03:00
if ( this . httprequest . uploadFile ) {
2021-01-25 02:19:29 +03:00
fs . closeSync ( this . httprequest . uploadFile ) ;
fs . unlinkSync ( this . httprequest . uploadFilePath ) ;
this . write ( Buffer . from ( JSON . stringify ( { action : 'uploadcancel' , reqid : this . httprequest . uploadFileid } ) ) ) ; // Indicate that we closed the file.
delete this . httprequest . uploadFile ;
delete this . httprequest . uploadFileid ;
delete this . httprequest . uploadFilePath ;
2021-01-25 09:46:54 +03:00
delete this . httprequest . uploadFileSize ;
2020-08-13 22:29:18 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2021-10-16 00:46:56 +03:00
case 'uploadhash' :
{
// Hash a file
var filepath = cmd . name ? obj . path . join ( cmd . path , cmd . name ) : cmd . path ;
var h = null ;
try { h = getSHA384FileHash ( filepath ) ; } catch ( ex ) { sendConsoleText ( ex ) ; }
this . write ( Buffer . from ( JSON . stringify ( { action : 'uploadhash' , reqid : cmd . reqid , path : cmd . path , name : cmd . name , tag : cmd . tag , hash : ( h ? h . toString ( 'hex' ) : null ) } ) ) ) ;
break
}
2021-01-25 02:19:29 +03:00
case 'copy' :
{
// Copy a bunch of files from scpath to dspath
2021-01-25 08:51:22 +03:00
for ( var i in cmd . names ) {
2021-01-25 02:19:29 +03:00
var sc = obj . path . join ( cmd . scpath , cmd . names [ i ] ) , ds = obj . path . join ( cmd . dspath , cmd . names [ i ] ) ;
MeshServerLogEx ( 51 , [ sc , ds ] , 'Copy: \"' + sc + '\" to \"' + ds + '\"' , this . httprequest ) ;
2022-01-17 05:28:26 +03:00
if ( sc != ds ) { try { fs . copyFileSync ( sc , ds ) ; } catch ( ex ) { } }
2018-04-03 01:34:32 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'move' :
{
// Move a bunch of files from scpath to dspath
2021-01-25 08:51:22 +03:00
for ( var i in cmd . names ) {
2021-01-25 02:19:29 +03:00
var sc = obj . path . join ( cmd . scpath , cmd . names [ i ] ) , ds = obj . path . join ( cmd . dspath , cmd . names [ i ] ) ;
MeshServerLogEx ( 52 , [ sc , ds ] , 'Move: \"' + sc + '\" to \"' + ds + '\"' , this . httprequest ) ;
2022-01-17 05:28:26 +03:00
if ( sc != ds ) { try { fs . copyFileSync ( sc , ds ) ; fs . unlinkSync ( sc ) ; } catch ( ex ) { } }
2018-04-03 01:34:32 +03:00
}
2019-10-10 21:13:25 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'zip' :
// Zip a bunch of files
if ( this . zip != null ) return ; // Zip operating is currently running, exit now.
// Check that the specified files exist & build full paths
var fp , stat , p = [ ] ;
2022-01-17 05:28:26 +03:00
for ( var i in cmd . files ) { fp = cmd . path + '/' + cmd . files [ i ] ; stat = null ; try { stat = fs . statSync ( fp ) ; } catch ( ex ) { } if ( stat != null ) { p . push ( fp ) ; } }
2021-01-25 02:19:29 +03:00
if ( p . length == 0 ) return ; // No files, quit now.
// Setup file compression
var ofile = cmd . path + '/' + cmd . output ;
this . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : 'zipping' } ) ) ) ;
this . zipfile = ofile ;
delete this . zipcancel ;
var out = require ( 'fs' ) . createWriteStream ( ofile , { flags : 'wb' } ) ;
out . xws = this ;
2021-01-25 08:51:22 +03:00
out . on ( 'close' , function ( ) {
2021-01-25 02:19:29 +03:00
this . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : null } ) ) ) ;
this . xws . write ( Buffer . from ( JSON . stringify ( { action : 'refresh' } ) ) ) ;
if ( this . xws . zipcancel === true ) { fs . unlinkSync ( this . xws . zipfile ) ; } // Delete the complete file.
delete this . xws . zipcancel ;
delete this . xws . zipfile ;
delete this . xws . zip ;
} ) ;
this . zip = require ( 'zip-writer' ) . write ( { files : p , basePath : cmd . path } ) ;
this . zip . xws = this ;
this . zip . on ( 'progress' , require ( 'events' ) . moderated ( function ( name , p ) { this . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : 'zippingFile' , file : ( ( process . platform == 'win32' ) ? ( name . split ( '/' ) . join ( '\\' ) ) : name ) , progress : p } ) ) ) ; } , 1000 ) ) ;
this . zip . pipe ( out ) ;
break ;
2024-03-02 18:16:06 +03:00
case 'unzip' :
if ( this . unzip != null ) return ; // Unzip operating is currently running, exit now.
this . unzip = require ( 'zip-reader' ) . read ( cmd . input ) ;
this . unzip . _dest = cmd . dest ;
this . unzip . xws = this ;
this . unzip . then ( function ( zipped ) {
this . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : 'unzipping' } ) ) ) ;
zipped . xws = this . xws ;
zipped . extractAll ( this . _dest ) . then ( function ( ) { // finished extracting
zipped . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : null } ) ) ) ;
zipped . xws . write ( Buffer . from ( JSON . stringify ( { action : 'refresh' } ) ) ) ;
delete zipped . xws . unzip ;
} , function ( e ) { // error extracting
zipped . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : 'unziperror' , error : e } ) ) ) ;
delete zipped . xws . unzip ;
} ) ;
} , function ( e ) { this . xws . write ( Buffer . from ( JSON . stringify ( { action : 'dialogmessage' , msg : 'unziperror' , error : e } ) ) ) ; delete this . xws . unzip } ) ;
break ;
2021-01-25 02:19:29 +03:00
case 'cancel' :
// Cancel zip operation if present
2022-01-17 05:28:26 +03:00
try { this . zipcancel = true ; this . zip . cancel ( function ( ) { } ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
this . zip = null ;
break ;
default :
// Unknown action, ignore it.
break ;
}
2021-01-25 08:51:22 +03:00
} else if ( this . httprequest . protocol == 7 ) { // Plugin data exchange
2021-01-25 02:19:29 +03:00
var cmd = null ;
2022-01-17 05:28:26 +03:00
try { cmd = JSON . parse ( data ) ; } catch ( ex ) { } ;
2021-01-25 02:19:29 +03:00
if ( cmd == null ) { return ; }
if ( ( cmd . ctrlChannel == '102938' ) || ( ( cmd . type == 'offer' ) && ( cmd . sdp != null ) ) ) { onTunnelControlData ( cmd , this ) ; return ; } // If this is control data, handle it now.
if ( cmd . action == undefined ) return ;
2021-01-25 08:51:22 +03:00
switch ( cmd . action ) {
2021-01-25 02:19:29 +03:00
case 'plugin' : {
2022-01-17 06:12:13 +03:00
try { require ( cmd . plugin ) . consoleaction ( cmd , null , null , this ) ; } catch ( ex ) { throw ex ; }
2021-01-25 02:19:29 +03:00
break ;
}
default : {
// probably shouldn't happen, but just in case this feature is expanded
2019-10-10 21:13:25 +03:00
}
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
//sendConsoleText("Got tunnel #" + this.httprequest.index + " data: " + data, this.httprequest.sessionid);
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
}
2018-01-17 04:30:34 +03:00
2021-01-25 02:19:29 +03:00
// Delete a directory with a files and directories within it
2021-01-25 08:51:22 +03:00
function deleteFolderRecursive ( path , rec ) {
2021-01-25 02:19:29 +03:00
var count = 0 ;
2021-01-25 08:51:22 +03:00
if ( fs . existsSync ( path ) ) {
if ( rec == true ) {
fs . readdirSync ( obj . path . join ( path , '*' ) ) . forEach ( function ( file , index ) {
2021-01-25 02:19:29 +03:00
var curPath = obj . path . join ( path , file ) ;
2021-01-25 08:51:22 +03:00
if ( fs . statSync ( curPath ) . isDirectory ( ) ) { // recurse
2021-01-25 02:19:29 +03:00
count += deleteFolderRecursive ( curPath , true ) ;
2021-01-25 08:51:22 +03:00
} else { // delete file
2021-01-25 02:19:29 +03:00
fs . unlinkSync ( curPath ) ;
count ++ ;
}
} ) ;
2019-03-14 22:28:58 +03:00
}
2021-01-25 02:19:29 +03:00
fs . unlinkSync ( path ) ;
count ++ ;
}
return count ;
}
2019-03-14 22:28:58 +03:00
2021-01-25 02:19:29 +03:00
// Called when receiving control data on WebRTC
2021-01-25 08:51:22 +03:00
function onTunnelWebRTCControlData ( data ) {
2021-01-25 02:19:29 +03:00
if ( typeof data != 'string' ) return ;
var obj ;
2022-01-17 05:28:26 +03:00
try { obj = JSON . parse ( data ) ; } catch ( ex ) { sendConsoleText ( 'Invalid control JSON on WebRTC: ' + data ) ; return ; }
2021-01-25 08:51:22 +03:00
if ( obj . type == 'close' ) {
2021-01-25 02:19:29 +03:00
//sendConsoleText('Tunnel #' + this.xrtc.websocket.tunnel.index + ' WebRTC control close');
2022-01-17 05:28:26 +03:00
try { this . close ( ) ; } catch ( ex ) { }
try { this . xrtc . close ( ) ; } catch ( ex ) { }
2018-01-19 02:43:43 +03:00
}
2021-01-25 02:19:29 +03:00
}
2018-01-19 02:43:43 +03:00
2022-08-26 06:11:47 +03:00
function tunnel _webrtc _onEnd ( )
{
// The WebRTC channel closed, unpipe the KVM now. This is also done when the web socket closes.
//sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC data channel closed');
if ( this . websocket . desktop && this . websocket . desktop . kvm )
{
try
{
this . unpipe ( this . websocket . desktop . kvm ) ;
this . websocket . httprequest . desktop . kvm . unpipe ( this ) ;
} catch ( ex ) { }
}
this . httprequest = null ;
this . websocket = null ;
}
function tunnel _webrtc _DataChannel _OnFinalized ( )
{
console . info1 ( 'WebRTC DataChannel Finalized' ) ;
}
function tunnel _webrtc _OnDataChannel ( rtcchannel )
{
//sendConsoleText('WebRTC Datachannel open, protocol: ' + this.websocket.httprequest.protocol);
//rtcchannel.maxFragmentSize = 32768;
rtcchannel . xrtc = this ;
rtcchannel . websocket = this . websocket ;
this . rtcchannel = rtcchannel ;
this . rtcchannel . once ( '~' , tunnel _webrtc _DataChannel _OnFinalized ) ;
this . websocket . rtcchannel = rtcchannel ;
this . websocket . rtcchannel . on ( 'data' , onTunnelWebRTCControlData ) ;
this . websocket . rtcchannel . on ( 'end' , tunnel _webrtc _onEnd ) ;
this . websocket . write ( '{\"ctrlChannel\":\"102938\",\"type\":\"webrtc0\"}' ) ; // Indicate we are ready for WebRTC switch-over.
}
function tunnel _webrtc _OnFinalized ( )
{
console . info1 ( 'WebRTC Connection Finalized' ) ;
}
2021-01-25 02:19:29 +03:00
// Called when receiving control data on websocket
2021-01-25 08:51:22 +03:00
function onTunnelControlData ( data , ws ) {
2021-01-25 02:19:29 +03:00
var obj ;
if ( ws == null ) { ws = this ; }
2022-01-17 05:28:26 +03:00
if ( typeof data == 'string' ) { try { obj = JSON . parse ( data ) ; } catch ( ex ) { sendConsoleText ( 'Invalid control JSON: ' + data ) ; return ; } }
2021-01-25 02:19:29 +03:00
else if ( typeof data == 'object' ) { obj = data ; } else { return ; }
2021-04-27 05:01:40 +03:00
//sendConsoleText('onTunnelControlData(' + ws.httprequest.protocol + '): ' + JSON.stringify(data));
2021-01-25 02:19:29 +03:00
//console.log('onTunnelControlData: ' + JSON.stringify(data));
2021-01-25 08:51:22 +03:00
switch ( obj . type ) {
2021-04-27 04:27:51 +03:00
case 'lock' : {
// Look for a TSID
var tsid = null ;
if ( ( ws . httprequest . xoptions != null ) && ( typeof ws . httprequest . xoptions . tsid == 'number' ) ) { tsid = ws . httprequest . xoptions . tsid ; }
// Lock the current user out of the desktop
2021-09-03 10:55:00 +03:00
MeshServerLogEx ( 53 , null , "Locking remote user out of desktop" , ws . httprequest ) ;
lockDesktop ( tsid ) ;
2021-04-27 04:27:51 +03:00
break ;
}
2021-09-01 22:07:09 +03:00
case 'autolock' : {
// Set the session to auto lock on disconnect
2022-01-17 00:37:26 +03:00
if ( obj . value === true ) {
2021-09-03 10:55:00 +03:00
ws . httprequest . autolock = true ;
2022-01-17 00:37:26 +03:00
if ( ws . httprequest . unlockerHelper == null ) {
2021-09-03 10:55:00 +03:00
destopLockHelper _pipe ( ws . httprequest ) ;
}
}
2022-01-17 00:37:26 +03:00
else {
2021-09-03 10:55:00 +03:00
delete ws . httprequest . autolock ;
}
2021-09-01 22:07:09 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'options' : {
// These are additional connection options passed in the control channel.
//sendConsoleText('options: ' + JSON.stringify(obj));
delete obj . type ;
ws . httprequest . xoptions = obj ;
2020-04-15 05:12:14 +03:00
2021-01-25 02:19:29 +03:00
// Set additional user consent options if present
if ( ( obj != null ) && ( typeof obj . consent == 'number' ) ) { ws . httprequest . consent |= obj . consent ; }
2020-04-15 05:12:14 +03:00
2021-09-01 22:07:09 +03:00
// Set autolock
2022-01-17 00:37:26 +03:00
if ( ( obj != null ) && ( obj . autolock === true ) ) {
2021-09-03 10:55:00 +03:00
ws . httprequest . autolock = true ;
2022-01-17 00:37:26 +03:00
if ( ws . httprequest . unlockerHelper == null ) {
2021-09-03 10:55:00 +03:00
destopLockHelper _pipe ( ws . httprequest ) ;
}
}
2021-09-01 22:07:09 +03:00
2021-01-25 02:19:29 +03:00
break ;
}
case 'close' : {
// We received the close on the websocket
//sendConsoleText('Tunnel #' + ws.tunnel.index + ' WebSocket control close');
2022-01-17 05:28:26 +03:00
try { ws . close ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
break ;
}
case 'termsize' : {
// Indicates a change in terminal size
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
if ( ws . httprequest . _dispatcher == null ) return ;
//sendConsoleText('Win32-TermSize: ' + obj.cols + 'x' + obj.rows);
if ( ws . httprequest . _dispatcher . invoke ) { ws . httprequest . _dispatcher . invoke ( 'resizeTerminal' , [ obj . cols , obj . rows ] ) ; }
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
if ( ws . httprequest . process == null || ws . httprequest . process . pty == 0 ) return ;
//sendConsoleText('Linux Resize: ' + obj.cols + 'x' + obj.rows);
if ( ws . httprequest . process . tcsetsize ) { ws . httprequest . process . tcsetsize ( obj . rows , obj . cols ) ; }
2018-02-05 22:56:29 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'webrtc0' : { // Browser indicates we can start WebRTC switch-over.
2022-08-26 06:11:47 +03:00
if ( ws . httprequest . protocol == 1 )
{ // Terminal
2021-01-25 02:19:29 +03:00
// This is a terminal data stream, unpipe the terminal now and indicate to the other side that terminal data will no longer be received over WebSocket
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
ws . httprequest . _term . unpipe ( ws ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
ws . httprequest . process . stdout . unpipe ( ws ) ;
ws . httprequest . process . stderr . unpipe ( ws ) ;
}
2021-01-25 08:51:22 +03:00
} else if ( ws . httprequest . protocol == 2 ) { // Desktop
2021-01-25 02:19:29 +03:00
// This is a KVM data stream, unpipe the KVM now and indicate to the other side that KVM data will no longer be received over WebSocket
ws . httprequest . desktop . kvm . unpipe ( ws ) ;
2022-08-26 06:11:47 +03:00
} else
{
2021-01-25 02:19:29 +03:00
// Switch things around so all WebRTC data goes to onTunnelData().
ws . rtcchannel . httprequest = ws . httprequest ;
ws . rtcchannel . removeAllListeners ( 'data' ) ;
ws . rtcchannel . on ( 'data' , onTunnelData ) ;
2020-01-24 23:49:41 +03:00
}
2021-01-25 02:19:29 +03:00
ws . write ( "{\"ctrlChannel\":\"102938\",\"type\":\"webrtc1\"}" ) ; // End of data marker
break ;
}
2022-08-26 06:11:47 +03:00
case 'webrtc1' :
{
if ( ( ws . httprequest . protocol == 1 ) || ( ws . httprequest . protocol == 6 ) )
{ // Terminal
2021-01-25 02:19:29 +03:00
// Switch the user input from websocket to webrtc at this point.
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
ws . unpipe ( ws . httprequest . _term ) ;
ws . rtcchannel . pipe ( ws . httprequest . _term , { dataTypeSkip : 1 } ) ; // 0 = Binary, 1 = Text.
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
ws . unpipe ( ws . httprequest . process . stdin ) ;
ws . rtcchannel . pipe ( ws . httprequest . process . stdin , { dataTypeSkip : 1 } ) ; // 0 = Binary, 1 = Text.
2018-12-13 02:34:42 +03:00
}
2021-01-25 02:19:29 +03:00
ws . resume ( ) ; // Resume the websocket to keep receiving control data
2022-08-26 06:11:47 +03:00
}
else if ( ws . httprequest . protocol == 2 )
{ // Desktop
2021-01-25 02:19:29 +03:00
// Switch the user input from websocket to webrtc at this point.
ws . unpipe ( ws . httprequest . desktop . kvm ) ;
2022-01-17 05:28:26 +03:00
try { ws . webrtc . rtcchannel . pipe ( ws . httprequest . desktop . kvm , { dataTypeSkip : 1 , end : false } ) ; } catch ( ex ) { sendConsoleText ( 'EX2' ) ; } // 0 = Binary, 1 = Text.
2021-01-25 02:19:29 +03:00
ws . resume ( ) ; // Resume the websocket to keep receiving control data
2018-01-19 02:43:43 +03:00
}
2021-01-25 02:19:29 +03:00
ws . write ( '{\"ctrlChannel\":\"102938\",\"type\":\"webrtc2\"}' ) ; // Indicates we will no longer get any data on websocket, switching to WebRTC at this point.
break ;
}
case 'webrtc2' : {
// Other side received websocket end of data marker, start sending data on WebRTC channel
2021-01-25 08:51:22 +03:00
if ( ( ws . httprequest . protocol == 1 ) || ( ws . httprequest . protocol == 6 ) ) { // Terminal
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
ws . httprequest . _term . pipe ( ws . webrtc . rtcchannel , { dataTypeSkip : 1 , end : false } ) ; // 0 = Binary, 1 = Text.
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
ws . httprequest . process . stdout . pipe ( ws . webrtc . rtcchannel , { dataTypeSkip : 1 , end : false } ) ; // 0 = Binary, 1 = Text.
ws . httprequest . process . stderr . pipe ( ws . webrtc . rtcchannel , { dataTypeSkip : 1 , end : false } ) ; // 0 = Binary, 1 = Text.
2018-12-13 02:34:42 +03:00
}
2021-01-25 08:51:22 +03:00
} else if ( ws . httprequest . protocol == 2 ) { // Desktop
2021-01-25 02:19:29 +03:00
ws . httprequest . desktop . kvm . pipe ( ws . webrtc . rtcchannel , { dataTypeSkip : 1 } ) ; // 0 = Binary, 1 = Text.
2020-01-24 23:49:41 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'offer' : {
// This is a WebRTC offer.
if ( ( ws . httprequest . protocol == 1 ) || ( ws . httprequest . protocol == 6 ) ) return ; // TODO: Terminal is currently broken with WebRTC. Reject WebRTC upgrade for now.
ws . webrtc = rtc . createConnection ( ) ;
2022-08-26 06:11:47 +03:00
ws . webrtc . once ( '~' , tunnel _webrtc _OnFinalized ) ;
2021-01-25 02:19:29 +03:00
ws . webrtc . websocket = ws ;
2022-08-26 06:11:47 +03:00
//ws.webrtc.on('connected', function () { /*sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC connected');*/ });
//ws.webrtc.on('disconnected', function () { /*sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC disconnected');*/ });
ws . webrtc . on ( 'dataChannel' , tunnel _webrtc _OnDataChannel ) ;
2021-01-25 02:19:29 +03:00
var sdp = null ;
2022-01-17 05:28:26 +03:00
try { sdp = ws . webrtc . setOffer ( obj . sdp ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
if ( sdp != null ) { ws . write ( { type : 'answer' , ctrlChannel : '102938' , sdp : sdp } ) ; }
break ;
}
case 'ping' : {
ws . write ( "{\"ctrlChannel\":\"102938\",\"type\":\"pong\"}" ) ; // Send pong response
break ;
}
case 'pong' : { // NOP
break ;
}
case 'rtt' : {
ws . write ( { type : 'rtt' , ctrlChannel : '102938' , time : obj . time } ) ;
break ;
}
}
}
// Console state
var consoleWebSockets = { } ;
var consoleHttpRequest = null ;
// Console HTTP response
2021-01-25 08:51:22 +03:00
function consoleHttpResponse ( response ) {
2021-01-25 02:19:29 +03:00
response . data = function ( data ) { sendConsoleText ( rstr2hex ( buf2rstr ( data ) ) , this . sessionid ) ; consoleHttpRequest = null ; }
response . close = function ( ) { sendConsoleText ( 'httprequest.response.close' , this . sessionid ) ; consoleHttpRequest = null ; }
}
2024-04-03 11:51:18 +03:00
// Open a local file on current user's desktop
function openFileOnDesktop ( file ) {
var child = null ;
try {
switch ( process . platform ) {
case 'win32' :
var uid = require ( 'user-sessions' ) . consoleUid ( ) ;
var user = require ( 'user-sessions' ) . getUsername ( uid ) ;
var domain = require ( 'user-sessions' ) . getDomain ( uid ) ;
var task = { name : 'MeshChatTask' , user : user , domain : domain , execPath : ( require ( 'fs' ) . statSync ( file ) . isDirectory ( ) ? process . env [ 'windir' ] + '\\explorer.exe' : file ) } ;
if ( require ( 'fs' ) . statSync ( file ) . isDirectory ( ) ) task . arguments = [ file ] ;
try {
require ( 'win-tasks' ) . addTask ( task ) ;
require ( 'win-tasks' ) . getTask ( { name : 'MeshChatTask' } ) . run ( ) ;
require ( 'win-tasks' ) . deleteTask ( 'MeshChatTask' ) ;
return ( true ) ;
}
catch ( ex ) {
var taskoptions = { env : { _target : ( require ( 'fs' ) . statSync ( file ) . isDirectory ( ) ? process . env [ 'windir' ] + '\\explorer.exe' : file ) , _user : '"' + domain + '\\' + user + '"' } , _args : "" } ;
if ( require ( 'fs' ) . statSync ( file ) . isDirectory ( ) ) taskoptions . env . _args = file ;
for ( var c1e in process . env ) {
taskoptions . env [ c1e ] = process . env [ c1e ] ;
}
var child = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' , [ 'powershell' , '-noprofile' , '-nologo' , '-command' , '-' ] , taskoptions ) ;
child . stderr . on ( 'data' , function ( c ) { } ) ;
child . stdout . on ( 'data' , function ( c ) { } ) ;
child . stdin . write ( 'SCHTASKS /CREATE /F /TN MeshChatTask /SC ONCE /ST 00:00 ' ) ;
if ( user ) { child . stdin . write ( '/RU $env:_user ' ) ; }
child . stdin . write ( '/TR "$env:_target $env:_args"\r\n' ) ;
child . stdin . write ( '$ts = New-Object -ComObject Schedule.service\r\n' ) ;
child . stdin . write ( '$ts.connect()\r\n' ) ;
child . stdin . write ( '$tsfolder = $ts.getfolder("\\")\r\n' ) ;
child . stdin . write ( '$task = $tsfolder.GetTask("MeshChatTask")\r\n' ) ;
child . stdin . write ( '$taskdef = $task.Definition\r\n' ) ;
child . stdin . write ( '$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n' ) ;
child . stdin . write ( '$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n' ) ;
child . stdin . write ( '$taskdef.Actions.Item(1).Path = $env:_target\r\n' ) ;
child . stdin . write ( '$taskdef.Actions.Item(1).Arguments = $env:_args\r\n' ) ;
child . stdin . write ( '$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n' ) ;
child . stdin . write ( 'SCHTASKS /RUN /TN MeshChatTask\r\n' ) ;
child . stdin . write ( 'SCHTASKS /DELETE /F /TN MeshChatTask\r\nexit\r\n' ) ;
child . waitExit ( ) ;
}
break ;
case 'linux' :
child = require ( 'child_process' ) . execFile ( '/usr/bin/xdg-open' , [ 'xdg-open' , file ] , { uid : require ( 'user-sessions' ) . consoleUid ( ) } ) ;
break ;
case 'darwin' :
child = require ( 'child_process' ) . execFile ( '/usr/bin/open' , [ 'open' , file ] ) ;
break ;
default :
// Unknown platform, ignore this command.
break ;
}
} catch ( ex ) { }
return child ;
}
2021-01-25 02:19:29 +03:00
// Open a web browser to a specified URL on current user's desktop
2021-01-25 08:51:22 +03:00
function openUserDesktopUrl ( url ) {
2021-01-25 02:19:29 +03:00
if ( ( url . toLowerCase ( ) . startsWith ( 'http://' ) == false ) && ( url . toLowerCase ( ) . startsWith ( 'https://' ) == false ) ) { return null ; }
var child = null ;
2021-01-25 08:51:22 +03:00
try {
switch ( process . platform ) {
2021-01-25 02:19:29 +03:00
case 'win32' :
2021-04-19 20:55:09 +03:00
var uid = require ( 'user-sessions' ) . consoleUid ( ) ;
var user = require ( 'user-sessions' ) . getUsername ( uid ) ;
var domain = require ( 'user-sessions' ) . getDomain ( uid ) ;
2022-02-04 02:14:35 +03:00
var task = { name : 'MeshChatTask' , user : user , domain : domain , execPath : process . env [ 'windir' ] + '\\system32\\cmd.exe' , arguments : [ '/C START ' + url . split ( '&' ) . join ( '^&' ) ] } ;
2022-08-04 11:52:29 +03:00
try {
2022-02-04 02:14:35 +03:00
require ( 'win-tasks' ) . addTask ( task ) ;
require ( 'win-tasks' ) . getTask ( { name : 'MeshChatTask' } ) . run ( ) ;
require ( 'win-tasks' ) . deleteTask ( 'MeshChatTask' ) ;
return ( true ) ;
}
2022-08-04 11:52:29 +03:00
catch ( ex ) {
2022-02-04 02:14:35 +03:00
var taskoptions = { env : { _target : process . env [ 'windir' ] + '\\system32\\cmd.exe' , _args : '/C START ' + url . split ( '&' ) . join ( '^&' ) , _user : '"' + domain + '\\' + user + '"' } } ;
2022-08-04 11:52:29 +03:00
for ( var c1e in process . env ) {
2022-02-04 02:14:35 +03:00
taskoptions . env [ c1e ] = process . env [ c1e ] ;
}
var child = require ( 'child_process' ) . execFile ( process . env [ 'windir' ] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' , [ 'powershell' , '-noprofile' , '-nologo' , '-command' , '-' ] , taskoptions ) ;
child . stderr . on ( 'data' , function ( c ) { } ) ;
child . stdout . on ( 'data' , function ( c ) { } ) ;
child . stdin . write ( 'SCHTASKS /CREATE /F /TN MeshChatTask /SC ONCE /ST 00:00 ' ) ;
if ( user ) { child . stdin . write ( '/RU $env:_user ' ) ; }
child . stdin . write ( '/TR "$env:_target $env:_args"\r\n' ) ;
child . stdin . write ( '$ts = New-Object -ComObject Schedule.service\r\n' ) ;
child . stdin . write ( '$ts.connect()\r\n' ) ;
child . stdin . write ( '$tsfolder = $ts.getfolder("\\")\r\n' ) ;
child . stdin . write ( '$task = $tsfolder.GetTask("MeshChatTask")\r\n' ) ;
child . stdin . write ( '$taskdef = $task.Definition\r\n' ) ;
child . stdin . write ( '$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n' ) ;
child . stdin . write ( '$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n' ) ;
child . stdin . write ( '$taskdef.Actions.Item(1).Path = $env:_target\r\n' ) ;
child . stdin . write ( '$taskdef.Actions.Item(1).Arguments = $env:_args\r\n' ) ;
child . stdin . write ( '$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n' ) ;
child . stdin . write ( 'SCHTASKS /RUN /TN MeshChatTask\r\n' ) ;
child . stdin . write ( 'SCHTASKS /DELETE /F /TN MeshChatTask\r\nexit\r\n' ) ;
child . waitExit ( ) ;
2021-02-03 22:21:08 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'linux' :
child = require ( 'child_process' ) . execFile ( '/usr/bin/xdg-open' , [ 'xdg-open' , url ] , { uid : require ( 'user-sessions' ) . consoleUid ( ) } ) ;
break ;
case 'darwin' :
child = require ( 'child_process' ) . execFile ( '/usr/bin/open' , [ 'open' , url ] , { uid : require ( 'user-sessions' ) . consoleUid ( ) } ) ;
break ;
default :
// Unknown platform, ignore this command.
break ;
}
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
return child ;
}
// Process a mesh agent console command
2021-01-25 08:51:22 +03:00
function processConsoleCommand ( cmd , args , rights , sessionid ) {
try {
2021-01-25 02:19:29 +03:00
var response = null ;
2021-01-25 08:51:22 +03:00
switch ( cmd ) {
2021-01-25 02:19:29 +03:00
case 'help' : { // Displays available commands
2024-04-03 11:51:18 +03:00
var fin = '' , f = '' , availcommands = 'domain,translations,agentupdate,errorlog,msh,timerinfo,coreinfo,coreinfoupdate,coredump,service,fdsnapshot,fdcount,startupoptions,alert,agentsize,versions,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,wslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,openurl,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,wallpaper,agentmsg,task,uninstallagent,display,openfile' ;
2021-01-27 01:39:39 +03:00
if ( require ( 'os' ) . dns != null ) { availcommands += ',dnsinfo' ; }
2022-01-17 05:28:26 +03:00
try { require ( 'linux-dhcp' ) ; availcommands += ',dhcp' ; } catch ( ex ) { }
2022-08-04 11:52:29 +03:00
if ( process . platform == 'win32' ) {
2024-02-18 04:51:15 +03:00
availcommands += ',bitlocker,cs,wpfhwacceleration,uac,volumes,rdpport' ;
2022-03-29 08:15:45 +03:00
if ( bcdOK ( ) ) { availcommands += ',safemode' ; }
2022-07-18 20:47:48 +03:00
if ( require ( 'notifybar-desktop' ) . DefaultPinned != null ) { availcommands += ',privacybar' ; }
2022-08-04 11:52:29 +03:00
try { require ( 'win-utils' ) ; availcommands += ',taskbar' ; } catch ( ex ) { }
2023-11-09 21:20:32 +03:00
try { require ( 'win-info' ) ; availcommands += ',installedapps' ; } catch ( ex ) { }
2022-03-29 08:15:45 +03:00
}
2021-01-25 02:19:29 +03:00
if ( amt != null ) { availcommands += ',amt,amtconfig,amtevents' ; }
if ( process . platform != 'freebsd' ) { availcommands += ',vm' ; }
if ( require ( 'MeshAgent' ) . maxKvmTileSize != null ) { availcommands += ',kvmmode' ; }
2022-01-17 05:28:26 +03:00
try { require ( 'zip-reader' ) ; availcommands += ',zip,unzip' ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
availcommands = availcommands . split ( ',' ) . sort ( ) ;
2021-01-25 08:51:22 +03:00
while ( availcommands . length > 0 ) {
2021-01-25 02:19:29 +03:00
if ( f . length > 90 ) { fin += ( f + ',\r\n' ) ; f = '' ; }
f += ( ( ( f != '' ) ? ', ' : ' ' ) + availcommands . shift ( ) ) ;
2018-12-13 02:34:42 +03:00
}
2021-01-25 02:19:29 +03:00
if ( f != '' ) { fin += f ; }
response = "Available commands: \r\n" + fin + "." ;
2020-01-24 23:49:41 +03:00
break ;
2018-01-19 02:43:43 +03:00
}
2022-11-02 06:01:26 +03:00
case 'mousetrails' :
2022-12-16 17:03:45 +03:00
try { require ( 'win-deskutils' ) ; } catch ( ex ) { response = 'Unknown command "mousetrails", type "help" for list of available commands.' ; break ; }
2022-11-02 06:01:26 +03:00
var id = require ( 'user-sessions' ) . getProcessOwnerName ( process . pid ) . tsid == 0 ? 1 : null ;
switch ( args [ '_' ] . length )
{
case 0 :
var trails = require ( 'win-deskutils' ) . mouse . getTrails ( id ) ;
response = trails == 0 ? 'MouseTrails Disabled' : ( 'MouseTrails enabled (' + trails + ')' ) ;
response += '\nTo change setting, specify a positive integer, where 0 is disable: mousetrails [n]' ;
break ;
case 1 :
var trails = parseInt ( args [ '_' ] [ 0 ] ) ;
require ( 'win-deskutils' ) . mouse . setTrails ( trails , id ) ;
trails = require ( 'win-deskutils' ) . mouse . getTrails ( id ) ;
response = trails == 0 ? 'MouseTrails Disabled' : ( 'MouseTrails enabled (' + trails + ')' ) ;
break ;
default :
response = 'Proper usage: mousetrails [n]' ;
break ;
}
break ;
case 'deskbackground' :
2022-12-16 17:03:45 +03:00
try { require ( 'win-deskutils' ) ; } catch ( ex ) { response = 'Unknown command "deskbackground", type "help" for list of available commands.' ; break ; }
2022-11-02 06:01:26 +03:00
var id = require ( 'user-sessions' ) . getProcessOwnerName ( process . pid ) . tsid == 0 ? 1 : null ;
switch ( args [ '_' ] . length )
{
case 0 :
response = 'Desktop Background: ' + require ( 'win-deskutils' ) . background . get ( id ) ;
break ;
case 1 :
require ( 'win-deskutils' ) . background . set ( args [ '_' ] [ 0 ] , id ) ;
response = 'Desktop Background: ' + require ( 'win-deskutils' ) . background . get ( id ) ;
break ;
default :
response = 'Proper usage: deskbackground [path]' ;
break ;
}
break ;
2022-08-04 11:01:59 +03:00
case 'taskbar' :
2022-12-16 17:03:45 +03:00
try { require ( 'win-utils' ) ; } catch ( ex ) { response = 'Unknown command "taskbar", type "help" for list of available commands.' ; break ; }
2022-08-04 11:52:29 +03:00
switch ( args [ '_' ] . length ) {
2022-08-04 11:01:59 +03:00
case 1 :
case 2 :
{
var tsid = parseInt ( args [ '_' ] [ 1 ] ) ;
if ( isNaN ( tsid ) ) { tsid = require ( 'user-sessions' ) . consoleUid ( ) ; }
sendConsoleText ( 'Changing TaskBar AutoHide status. Please wait...' , sessionid ) ;
2022-08-04 11:52:29 +03:00
try {
2022-08-04 11:01:59 +03:00
var result = require ( 'win-utils' ) . taskBar . autoHide ( tsid , args [ '_' ] [ 0 ] . toLowerCase ( ) == 'hide' ) ;
response = 'Current Status of TaskBar AutoHide: ' + result ;
2022-08-04 11:52:29 +03:00
} catch ( ex ) { response = 'Unable to change TaskBar settings' ; }
2022-08-04 11:01:59 +03:00
}
break ;
2022-08-04 11:52:29 +03:00
default :
{
response = 'Proper usage: taskbar HIDE|SHOW [TSID]' ;
break ;
}
2022-08-04 11:01:59 +03:00
}
break ;
2022-07-18 20:47:48 +03:00
case 'privacybar' :
2022-08-04 11:52:29 +03:00
if ( process . platform != 'win32' || require ( 'notifybar-desktop' ) . DefaultPinned == null ) {
2022-12-16 17:03:45 +03:00
response = 'Unknown command "privacybar", type "help" for list of available commands.' ;
2022-07-18 20:47:48 +03:00
}
2022-08-04 11:52:29 +03:00
else {
switch ( args [ '_' ] . length ) {
2022-07-18 20:47:48 +03:00
default :
// Show Help
response = "Current Default Pinned State: " + ( require ( 'notifybar-desktop' ) . DefaultPinned ? "PINNED" : "UNPINNED" ) + '\r\n' ;
response += "To set default pinned state:\r\n privacybar [PINNED|UNPINNED]\r\n" ;
break ;
case 1 :
2022-08-04 11:52:29 +03:00
switch ( args [ '_' ] [ 0 ] . toUpperCase ( ) ) {
2022-07-18 20:47:48 +03:00
case 'PINNED' :
require ( 'notifybar-desktop' ) . DefaultPinned = true ;
response = "privacybar default pinned state is: PINNED" ;
break ;
case 'UNPINNED' :
require ( 'notifybar-desktop' ) . DefaultPinned = false ;
response = "privacybar default pinned state is: UNPINNED" ;
break ;
default :
response = "INVALID parameter: " + args [ '_' ] [ 0 ] . toUpperCase ( ) ;
break ;
}
break ;
}
}
break ;
2022-06-06 02:50:39 +03:00
case 'domain' :
response = getDomainInfo ( ) ;
break ;
case 'domaininfo' :
{
2022-08-04 11:52:29 +03:00
if ( process . platform != 'win32' ) {
2022-12-16 17:03:45 +03:00
response = 'Unknown command "cs", type "help" for list of available commands.' ;
2022-06-06 02:50:39 +03:00
break ;
}
2022-08-04 11:52:29 +03:00
if ( global . _domainQuery != null ) {
2022-06-06 02:50:39 +03:00
response = "There is already an outstanding Domain Controller Query... Please try again later..." ;
break ;
}
sendConsoleText ( 'Querying Domain Controller... This can take up to 60 seconds. Please wait...' , sessionid ) ;
global . _domainQuery = require ( 'win-wmi' ) . queryAsync ( 'ROOT\\CIMV2' , 'SELECT * FROM Win32_NTDomain' ) ;
global . _domainQuery . session = sessionid ;
2022-08-04 11:52:29 +03:00
global . _domainQuery . then ( function ( v ) {
2022-06-06 02:50:39 +03:00
var results = [ ] ;
2022-08-04 11:52:29 +03:00
if ( Array . isArray ( v ) ) {
2022-06-06 02:50:39 +03:00
var i ;
var r ;
2022-08-04 11:52:29 +03:00
for ( i = 0 ; i < v . length ; ++ i ) {
2022-06-06 02:50:39 +03:00
r = { } ;
if ( v [ i ] . DomainControllerAddress != null ) { r . DomainControllerAddress = v [ i ] . DomainControllerAddress . split ( '\\' ) . pop ( ) ; }
if ( r . DomainControllerName != null ) { r . DomainControllerName = v [ i ] . DomainControllerName . split ( '\\' ) . pop ( ) ; }
r . DomainGuid = v [ i ] . DomainGuid ;
r . DomainName = v [ i ] . DomainName ;
2022-08-04 11:52:29 +03:00
if ( r . DomainGuid != null ) {
2022-06-06 02:50:39 +03:00
results . push ( r ) ;
}
}
}
2022-08-04 11:52:29 +03:00
if ( results . length > 0 ) {
2022-06-06 02:50:39 +03:00
sendConsoleText ( 'Domain Controller Results:' , this . session ) ;
sendConsoleText ( JSON . stringify ( results , null , 1 ) , this . session ) ;
sendConsoleText ( 'End of results...' , this . session ) ;
}
2022-08-04 11:52:29 +03:00
else {
2022-06-06 02:50:39 +03:00
sendConsoleText ( 'Domain Controller: No results returned. Is the domain controller reachable?' , this . session ) ;
}
global . _domainQuery = null ;
} ) ;
break ;
}
2022-01-13 02:12:30 +03:00
case 'translations' : {
2022-01-16 23:34:19 +03:00
response = JSON . stringify ( coretranslations , null , 2 ) ;
2022-01-13 02:12:30 +03:00
break ;
}
2022-03-15 08:43:47 +03:00
case 'volumes' :
response = JSON . stringify ( require ( 'win-volumes' ) . getVolumes ( ) , null , 1 ) ;
break ;
2024-02-18 04:51:15 +03:00
case 'bitlocker' :
if ( process . platform == 'win32' ) {
if ( require ( 'computer-identifiers' ) . volumes _promise != null ) {
var p = require ( 'computer-identifiers' ) . volumes _promise ( ) ;
p . then ( function ( res ) { sendConsoleText ( JSON . stringify ( cleanGetBitLockerVolumeInfo ( res ) , null , 1 ) , this . session ) ; } ) ;
response = "Please wait..." ;
} else if ( require ( 'computer-identifiers' ) . volumes != null ) {
sendConsoleText ( JSON . stringify ( cleanGetBitLockerVolumeInfo ( require ( 'computer-identifiers' ) . volumes ( ) ) , null , 1 ) , this . session ) ;
}
}
break ;
2021-07-24 19:28:41 +03:00
case 'dhcp' : // This command is only supported on Linux, this is because Linux does not give us the DNS suffix for each network adapter independently so we have to ask the DHCP server.
2021-07-24 02:33:40 +03:00
{
2022-12-16 17:03:45 +03:00
try { require ( 'linux-dhcp' ) ; } catch ( ex ) { response = 'Unknown command "dhcp", type "help" for list of available commands.' ; break ; }
2021-07-24 19:28:41 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-07-24 02:33:40 +03:00
var j = require ( 'os' ) . networkInterfaces ( ) ;
var ifcs = [ ] ;
2021-07-24 19:28:41 +03:00
for ( var i in j ) {
for ( var z in j [ i ] ) {
if ( j [ i ] [ z ] . status == 'up' && j [ i ] [ z ] . type != 'loopback' && j [ i ] [ z ] . address != null ) {
2021-07-24 02:33:40 +03:00
ifcs . push ( '"' + i + '"' ) ;
break ;
}
}
}
response = 'Proper usage: dhcp [' + ifcs . join ( ' | ' ) + ']' ;
}
2021-07-24 19:28:41 +03:00
else {
require ( 'linux-dhcp' ) . client . info ( args [ '_' ] [ 0 ] ) .
then ( function ( d ) {
2021-07-24 02:33:40 +03:00
sendConsoleText ( JSON . stringify ( d , null , 1 ) , sessionid ) ;
} ,
2021-07-24 19:28:41 +03:00
function ( e ) {
2021-07-24 02:33:40 +03:00
sendConsoleText ( e , sessionid ) ;
} ) ;
}
break ;
}
2021-04-24 04:23:18 +03:00
case 'cs' :
2021-04-24 20:53:13 +03:00
if ( process . platform != 'win32' ) {
2022-12-16 17:03:45 +03:00
response = 'Unknown command "cs", type "help" for list of available commands.' ;
2021-04-24 04:23:18 +03:00
break ;
}
2021-04-24 20:53:13 +03:00
switch ( args [ '_' ] . length ) {
2021-04-24 04:23:18 +03:00
case 0 :
2021-04-24 20:53:13 +03:00
try {
2021-04-24 04:23:18 +03:00
var cs = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Power' , 'CsEnabled' ) ;
response = "Connected Standby: " + ( cs == 1 ? "ENABLED" : "DISABLED" ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-04-24 04:23:18 +03:00
response = "This machine does not support Connected Standby" ;
}
break ;
case 1 :
2021-04-24 20:53:13 +03:00
if ( ( args [ '_' ] [ 0 ] . toUpperCase ( ) != 'ENABLE' && args [ '_' ] [ 0 ] . toUpperCase ( ) != 'DISABLE' ) ) {
2021-04-24 04:23:18 +03:00
response = "Proper usage:\r\n cs [ENABLE|DISABLE]" ;
}
2021-04-24 20:53:13 +03:00
else {
try {
2021-04-24 04:23:18 +03:00
var cs = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Power' , 'CsEnabled' ) ;
require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Power' , 'CsEnabled' , args [ '_' ] [ 0 ] . toUpperCase ( ) == 'ENABLE' ? 1 : 0 ) ;
cs = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Power' , 'CsEnabled' ) ;
response = "Connected Standby: " + ( cs == 1 ? "ENABLED" : "DISABLED" ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-04-24 04:23:18 +03:00
response = "This machine does not support Connected Standby" ;
}
}
break ;
default :
response = "Proper usage:\r\n cs [ENABLE|DISABLE]" ;
break ;
}
break ;
2021-04-27 10:53:04 +03:00
case 'assistant' :
if ( process . platform == 'win32' ) {
// Install MeshCentral Assistant on this device
response = "Usage: Assistant [info|install|uninstall]" ;
if ( args [ '_' ] . length == 1 ) {
if ( ( args [ '_' ] [ 0 ] == 'install' ) || ( args [ '_' ] [ 0 ] == 'info' ) ) { response = '' ; require ( 'MeshAgent' ) . SendCommand ( { action : 'meshToolInfo' , sessionid : sessionid , name : 'MeshCentralAssistant' , cookie : true , tag : args [ '_' ] [ 0 ] } ) ; }
// TODO: Uninstall
}
} else {
response = "MeshCentral Assistant is not supported on this platform." ;
}
break ;
2021-05-16 11:25:58 +03:00
case 'userimage' :
require ( 'MeshAgent' ) . SendCommand ( { action : 'getUserImage' , sessionid : sessionid , userid : args [ '_' ] [ 0 ] , tag : 'info' } ) ;
response = 'ok' ;
break ;
2021-01-25 02:19:29 +03:00
case 'agentupdate' :
require ( 'MeshAgent' ) . SendCommand ( { action : 'agentupdate' , sessionid : sessionid } ) ;
break ;
case 'agentupdateex' :
// Perform an direct agent update without requesting any information from the server, this should not typically be used.
2021-03-05 03:27:43 +03:00
if ( args [ '_' ] . length == 1 ) {
if ( args [ '_' ] [ 0 ] . startsWith ( 'https://' ) ) { agentUpdate _Start ( args [ '_' ] [ 0 ] , { sessionid : sessionid } ) ; } else { response = "Usage: agentupdateex https://server/path" ; }
} else {
agentUpdate _Start ( null , { sessionid : sessionid } ) ;
}
2021-01-25 02:19:29 +03:00
break ;
2021-04-14 04:14:13 +03:00
case 'errorlog' :
2021-04-24 20:53:13 +03:00
switch ( args [ '_' ] . length ) {
2021-04-14 04:14:13 +03:00
case 0 :
// All Error Logs
response = JSON . stringify ( require ( 'util-agentlog' ) . read ( ) , null , 1 ) ;
break ;
case 1 :
// Error Logs, by either count or timestamp
response = JSON . stringify ( require ( 'util-agentlog' ) . read ( parseInt ( args [ '_' ] [ 0 ] ) ) , null , 1 ) ;
break ;
default :
response = "Proper usage:\r\n errorlog [lastCount|linuxEpoch]" ;
break ;
}
break ;
2021-01-25 02:19:29 +03:00
case 'msh' :
response = JSON . stringify ( _MSH ( ) , null , 2 ) ;
break ;
2021-01-27 01:39:39 +03:00
case 'dnsinfo' :
2021-04-24 20:53:13 +03:00
if ( require ( 'os' ) . dns == null ) {
2022-12-16 17:03:45 +03:00
response = "Unknown command \"" + cmd + "\", type \"help\" for list of available commands." ;
2021-01-27 01:39:39 +03:00
}
2021-04-24 20:53:13 +03:00
else {
2021-01-27 01:39:39 +03:00
response = 'DNS Servers: ' ;
var dns = require ( 'os' ) . dns ( ) ;
2021-04-24 20:53:13 +03:00
for ( var i = 0 ; i < dns . length ; ++ i ) {
if ( i > 0 ) { response += ', ' ; }
2021-01-27 01:39:39 +03:00
response += dns [ i ] ;
}
}
break ;
2021-01-25 02:19:29 +03:00
case 'timerinfo' :
response = require ( 'ChainViewer' ) . getTimerInfo ( ) ;
break ;
2024-01-19 05:56:54 +03:00
case 'rdpport' :
if ( process . platform != 'win32' ) {
response = 'Unknown command "rdpport", type "help" for list of available commands.' ;
return ;
}
if ( args [ '_' ] . length == 0 ) {
response = 'Proper usage: rdpport [get|default|PORTNUMBER]' ;
} else {
switch ( args [ '_' ] [ 0 ] . toLocaleLowerCase ( ) ) {
case 'get' :
var rdpport = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp' , 'PortNumber' ) ;
response = "Current RDP Port Set To: " + rdpport + '\r\n' ;
break ;
case 'default' :
try {
require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp' , 'PortNumber' , 3389 ) ;
response = 'RDP Port Set To 3389, Please Dont Forget To Restart Your Computer To Fully Apply' ;
} catch ( ex ) {
response = 'Unable to Set RDP Port To: 3389' ;
}
break ;
default :
if ( isNaN ( parseFloat ( args [ '_' ] [ 0 ] ) ) ) {
response = 'Proper usage: rdpport [get|default|PORTNUMBER]' ;
} else if ( parseFloat ( args [ '_' ] [ 0 ] ) < 0 || args [ '_' ] [ 0 ] > 65535 ) {
response = 'RDP Port Must Be More Than 0 And Less Than 65535' ;
} else {
try {
require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp' , 'PortNumber' , parseFloat ( args [ '_' ] [ 0 ] ) ) ;
response = 'RDP Port Set To ' + args [ '_' ] [ 0 ] + ', Please Dont Forget To Restart Your Computer To Fully Apply' ;
} catch ( ex ) {
response = 'Unable to Set RDP Port To: ' + args [ '_' ] [ 0 ] ;
}
}
break ;
}
}
break ;
2021-01-25 02:19:29 +03:00
case 'find' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length <= 1 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage:\r\n find root criteria [criteria2] [criteria n...]" ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var root = args [ '_' ] [ 0 ] ;
var p = args [ '_' ] . slice ( 1 ) ;
var r = require ( 'file-search' ) . find ( root , p ) ;
r . sid = sessionid ;
r . on ( 'result' , function ( str ) { sendConsoleText ( str , this . sid ) ; } ) ;
r . then ( function ( ) { sendConsoleText ( '*** End Results ***' , this . sid ) ; } ) ;
response = "Find: [" + root + "] " + JSON . stringify ( p ) ;
2020-01-24 23:49:41 +03:00
}
break ;
2021-01-25 02:19:29 +03:00
case 'coreinfo' : {
response = JSON . stringify ( meshCoreObj , null , 2 ) ;
2020-01-24 23:49:41 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'coreinfoupdate' : {
2024-01-29 18:13:24 +03:00
sendPeriodicServerUpdate ( null , true ) ;
response = "Core Info Update Requested"
2020-07-20 00:45:29 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'agentmsg' : {
2021-07-24 19:28:41 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage:\r\n agentmsg add \"[message]\" [iconIndex]\r\n agentmsg remove [index]\r\n agentmsg list" ; // Display usage
2021-07-24 19:28:41 +03:00
} else {
if ( ( args [ '_' ] [ 0 ] == 'add' ) && ( args [ '_' ] . length > 1 ) ) {
2021-06-08 10:11:17 +03:00
var msgID , iconIndex = 0 ;
2022-01-17 05:28:26 +03:00
if ( args [ '_' ] . length >= 3 ) { try { iconIndex = parseInt ( args [ '_' ] [ 2 ] ) ; } catch ( ex ) { } }
2021-01-25 02:19:29 +03:00
if ( typeof iconIndex != 'number' ) { iconIndex = 0 ; }
2021-06-08 10:11:17 +03:00
msgID = sendAgentMessage ( args [ '_' ] [ 1 ] , iconIndex ) ;
response = 'Agent message: ' + msgID + ' added.' ;
2021-07-24 19:28:41 +03:00
} else if ( ( args [ '_' ] [ 0 ] == 'remove' ) && ( args [ '_' ] . length > 1 ) ) {
2021-06-08 10:11:17 +03:00
var r = removeAgentMessage ( args [ '_' ] [ 1 ] ) ;
response = 'Message ' + ( r ? 'removed' : 'NOT FOUND' ) ;
2021-07-24 19:28:41 +03:00
} else if ( args [ '_' ] [ 0 ] == 'list' ) {
2021-06-08 10:11:17 +03:00
response = JSON . stringify ( sendAgentMessage ( ) , null , 2 ) ;
2021-01-25 02:19:29 +03:00
}
broadcastSessionsToRegisteredApps ( ) ;
}
2020-07-20 00:45:29 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'clearagentmsg' : {
2021-06-08 10:11:17 +03:00
removeAgentMessage ( ) ;
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2020-04-14 08:48:20 +03:00
break ;
}
2021-01-25 02:19:29 +03:00
case 'coredump' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: coredump on|off|status|clear" ; // Display usage
2021-01-25 08:51:22 +03:00
} else {
switch ( args [ '_' ] [ 0 ] . toLowerCase ( ) ) {
2021-01-25 02:19:29 +03:00
case 'on' :
process . coreDumpLocation = ( process . platform == 'win32' ) ? ( process . execPath . replace ( '.exe' , '.dmp' ) ) : ( process . execPath + '.dmp' ) ;
response = 'coredump is now on' ;
break ;
case 'off' :
process . coreDumpLocation = null ;
response = 'coredump is now off' ;
break ;
case 'status' :
response = 'coredump is: ' + ( ( process . coreDumpLocation == null ) ? 'off' : 'on' ) ;
2021-01-25 08:51:22 +03:00
if ( process . coreDumpLocation != null ) {
if ( process . platform == 'win32' ) {
if ( fs . existsSync ( process . coreDumpLocation ) ) {
2021-01-25 02:19:29 +03:00
response += '\r\n CoreDump present at: ' + process . coreDumpLocation ;
response += '\r\n CoreDump Time: ' + new Date ( fs . statSync ( process . coreDumpLocation ) . mtime ) . getTime ( ) ;
response += '\r\n Agent Time : ' + new Date ( fs . statSync ( process . execPath ) . mtime ) . getTime ( ) ;
}
2021-01-25 08:51:22 +03:00
} else {
if ( ( process . cwd ( ) != '//' ) && fs . existsSync ( process . cwd ( ) + 'core' ) ) {
2021-01-25 02:19:29 +03:00
response += '\r\n CoreDump present at: ' + process . cwd ( ) + 'core' ;
response += '\r\n CoreDump Time: ' + new Date ( fs . statSync ( process . cwd ( ) + 'core' ) . mtime ) . getTime ( ) ;
response += '\r\n Agent Time : ' + new Date ( fs . statSync ( process . execPath ) . mtime ) . getTime ( ) ;
}
}
}
break ;
case 'clear' :
db . Put ( 'CoreDumpTime' , null ) ;
response = 'coredump db cleared' ;
break ;
default :
response = "Proper usage: coredump on|off|status" ; // Display usage
break ;
2019-09-27 20:30:00 +03:00
}
2018-04-20 04:19:15 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'service' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: service status|restart" ; // Display usage
2022-01-17 05:28:26 +03:00
} else {
2021-01-25 02:19:29 +03:00
var svcname = process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
svcname = require ( 'MeshAgent' ) . serviceName ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
var s = require ( 'service-manager' ) . manager . getService ( svcname ) ;
2021-01-25 08:51:22 +03:00
switch ( args [ '_' ] [ 0 ] . toLowerCase ( ) ) {
2021-01-25 02:19:29 +03:00
case 'status' :
response = 'Service ' + ( s . isRunning ( ) ? ( s . isMe ( ) ? '[SELF]' : '[RUNNING]' ) : ( '[NOT RUNNING]' ) ) ;
break ;
case 'restart' :
2021-01-25 08:51:22 +03:00
if ( s . isMe ( ) ) {
2021-01-25 02:19:29 +03:00
s . restart ( ) ;
2022-01-17 05:28:26 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Restarting another agent instance is not allowed' ;
}
break ;
default :
response = "Proper usage: service status|restart" ; // Display usage
break ;
}
if ( process . platform == 'win32' ) { s . close ( ) ; }
2020-10-31 03:08:07 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'zip' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: zip (output file name), input1 [, input n]" ; // Display usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var p = args [ '_' ] . join ( ' ' ) . split ( ',' ) ;
var ofile = p . shift ( ) ;
sendConsoleText ( 'Writing ' + ofile + '...' ) ;
var out = require ( 'fs' ) . createWriteStream ( ofile , { flags : 'wb' } ) ;
out . fname = ofile ;
out . sessionid = sessionid ;
out . on ( 'close' , function ( ) { sendConsoleText ( 'DONE writing ' + this . fname , this . sessionid ) ; } ) ;
var zip = require ( 'zip-writer' ) . write ( { files : p } ) ;
zip . pipe ( out ) ;
2020-11-02 11:44:07 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'unzip' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2024-03-02 18:16:06 +03:00
response = "Proper usage: unzip input,destination" ; // Display usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var p = args [ '_' ] . join ( ' ' ) . split ( ',' ) ;
2024-03-02 18:16:06 +03:00
if ( p . length != 2 ) { response = "Proper usage: unzip input,destination" ; break ; } // Display usage
var prom = require ( 'zip-reader' ) . read ( p [ 0 ] . trim ( ) ) ;
prom . _dest = p [ 1 ] . trim ( ) ;
2021-01-25 02:19:29 +03:00
prom . self = this ;
prom . sessionid = sessionid ;
2021-01-25 08:51:22 +03:00
prom . then ( function ( zipped ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Extracting to ' + this . _dest + '...' , this . sessionid ) ;
zipped . extractAll ( this . _dest ) . then ( function ( ) { sendConsoleText ( 'finished unzipping' , this . sessionid ) ; } , function ( e ) { sendConsoleText ( 'Error unzipping: ' + e , this . sessionid ) ; } ) . parentPromise . sessionid = this . sessionid ;
} , function ( e ) { sendConsoleText ( 'Error unzipping: ' + e , this . sessionid ) ; } ) ;
2020-07-29 04:45:17 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'setbattery' :
// require('MeshAgent').SendCommand({ action: 'battery', state: 'dc', level: 55 });
2021-01-25 08:51:22 +03:00
if ( ( args [ '_' ] . length > 0 ) && ( ( args [ '_' ] [ 0 ] == 'ac' ) || ( args [ '_' ] [ 0 ] == 'dc' ) ) ) {
2021-01-25 02:19:29 +03:00
var b = { action : 'battery' , state : args [ '_' ] [ 0 ] } ;
if ( args [ '_' ] . length == 2 ) { b . level = parseInt ( args [ '_' ] [ 1 ] ) ; }
require ( 'MeshAgent' ) . SendCommand ( b ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . SendCommand ( { action : 'battery' } ) ;
}
break ;
case 'fdsnapshot' :
require ( 'ChainViewer' ) . getSnapshot ( ) . then ( function ( c ) { sendConsoleText ( c , this . sessionid ) ; } ) . parentPromise . sessionid = sessionid ;
break ;
case 'fdcount' :
require ( 'DescriptorEvents' ) . getDescriptorCount ( ) . then (
2021-01-25 08:51:22 +03:00
function ( c ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Descriptor Count: ' + c , this . sessionid ) ;
2021-01-25 08:51:22 +03:00
} , function ( e ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Error fetching descriptor count: ' + e , this . sessionid ) ;
} ) . parentPromise . sessionid = sessionid ;
break ;
case 'uac' :
2021-01-25 08:51:22 +03:00
if ( process . platform != 'win32' ) {
2022-12-16 17:03:45 +03:00
response = 'Unknown command "uac", type "help" for list of available commands.' ;
2020-07-29 04:45:17 +03:00
break ;
}
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: uac [get|interactive|secure]' ;
}
2021-01-25 08:51:22 +03:00
else {
switch ( args [ '_' ] [ 0 ] . toUpperCase ( ) ) {
2021-01-25 02:19:29 +03:00
case 'GET' :
var secd = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System' , 'PromptOnSecureDesktop' ) ;
response = "UAC mode: " + ( secd == 0 ? "Interactive Desktop" : "Secure Desktop" ) ;
break ;
case 'INTERACTIVE' :
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System' , 'PromptOnSecureDesktop' , 0 ) ;
response = 'UAC mode changed to: Interactive Desktop' ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
response = "Unable to change UAC Mode" ;
}
break ;
case 'SECURE' :
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System' , 'PromptOnSecureDesktop' , 1 ) ;
response = 'UAC mode changed to: Secure Desktop' ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
response = "Unable to change UAC Mode" ;
}
break ;
default :
response = 'Proper usage: uac [get|interactive|secure]' ;
break ;
2020-07-07 00:19:16 +03:00
}
2021-01-25 02:19:29 +03:00
}
break ;
case 'vm' :
2023-12-23 02:18:17 +03:00
response = 'Virtual Machine = ' + require ( 'computer-identifiers' ) . isVM ( ) ;
2021-01-25 02:19:29 +03:00
break ;
case 'startupoptions' :
response = JSON . stringify ( require ( 'MeshAgent' ) . getStartupOptions ( ) ) ;
break ;
case 'kvmmode' :
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . maxKvmTileSize == null ) {
2022-12-16 17:03:45 +03:00
response = "Unknown command \"kvmmode\", type \"help\" for list of available commands." ;
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
else {
if ( require ( 'MeshAgent' ) . maxKvmTileSize == 0 ) {
2021-01-25 02:19:29 +03:00
response = 'KVM Mode: Full JUMBO' ;
2020-07-01 23:15:07 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
response = 'KVM Mode: ' + ( require ( 'MeshAgent' ) . maxKvmTileSize <= 65500 ? 'NO JUMBO' : 'Partial JUMBO' ) ;
response += ( ', TileLimit: ' + ( require ( 'MeshAgent' ) . maxKvmTileSize < 1024 ? ( require ( 'MeshAgent' ) . maxKvmTileSize + ' bytes' ) : ( Math . round ( require ( 'MeshAgent' ) . maxKvmTileSize / 1024 ) + ' Kbytes' ) ) ) ;
2020-07-01 23:15:07 +03:00
}
2021-01-25 02:19:29 +03:00
}
break ;
case 'alert' :
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: alert TITLE, CAPTION [, TIMEOUT]" ; // Display usage
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var p = args [ '_' ] . join ( ' ' ) . split ( ',' ) ;
2021-01-25 08:51:22 +03:00
if ( p . length < 2 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: alert TITLE, CAPTION [, TIMEOUT]" ; // Display usage
2020-06-25 02:04:52 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
this . _alert = require ( 'message-box' ) . create ( p [ 0 ] , p [ 1 ] , p . length == 3 ? parseInt ( p [ 2 ] ) : 9999 , 1 ) ;
2020-06-25 02:04:52 +03:00
}
2021-01-25 02:19:29 +03:00
}
break ;
case 'agentsize' :
var actualSize = Math . floor ( require ( 'fs' ) . statSync ( process . execPath ) . size / 1024 ) ;
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
// Check the Agent Uninstall MetaData for correctness, as the installer may have written an incorrect value
var writtenSize = 0 ;
2022-01-17 06:12:13 +03:00
try { writtenSize = require ( 'win-registry' ) . QueryKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent' , 'EstimatedSize' ) ; } catch ( ex ) { response = ex ; }
2021-01-25 08:51:22 +03:00
if ( writtenSize != actualSize ) {
2021-01-25 02:19:29 +03:00
response = "Size updated from: " + writtenSize + " to: " + actualSize ;
2022-01-17 06:12:13 +03:00
try { require ( 'win-registry' ) . WriteKey ( require ( 'win-registry' ) . HKEY . LocalMachine , 'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent' , 'EstimatedSize' , actualSize ) ; } catch ( ex ) { response = ex ; }
2021-01-25 02:19:29 +03:00
} else
{ response = "Agent Size: " + actualSize + " kb" ; }
} else
{ response = "Agent Size: " + actualSize + " kb" ; }
break ;
case 'versions' :
response = JSON . stringify ( process . versions , null , ' ' ) ;
break ;
case 'wpfhwacceleration' :
if ( process . platform != 'win32' ) { throw ( "wpfhwacceleration setting is only supported on Windows" ) ; }
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: wpfhwacceleration (ON|OFF|STATUS)" ; // Display usage
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var reg = require ( 'win-registry' ) ;
var uname = require ( 'user-sessions' ) . getUsername ( require ( 'user-sessions' ) . consoleUid ( ) ) ;
var key = reg . usernameToUserKey ( uname ) ;
2021-01-25 08:51:22 +03:00
switch ( args [ '_' ] [ 0 ] . toUpperCase ( ) ) {
2021-01-25 02:19:29 +03:00
default :
response = "Proper usage: wpfhwacceleration (ON|OFF|STATUS|DEFAULT)" ; // Display usage
break ;
case 'ON' :
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
reg . WriteKey ( reg . HKEY . Users , key + '\\SOFTWARE\\Microsoft\\Avalon.Graphics' , 'DisableHWAcceleration' , 0 ) ;
response = "OK" ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { response = "FAILED" ; }
2021-01-25 02:19:29 +03:00
break ;
case 'OFF' :
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
reg . WriteKey ( reg . HKEY . Users , key + '\\SOFTWARE\\Microsoft\\Avalon.Graphics' , 'DisableHWAcceleration' , 1 ) ;
response = 'OK' ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { response = 'FAILED' ; }
2021-01-25 02:19:29 +03:00
break ;
case 'STATUS' :
var s ;
2022-01-17 05:28:26 +03:00
try { s = reg . QueryKey ( reg . HKEY . Users , key + '\\SOFTWARE\\Microsoft\\Avalon.Graphics' , 'DisableHWAcceleration' ) == 1 ? 'DISABLED' : 'ENABLED' ; } catch ( ex ) { s = 'DEFAULT' ; }
2021-01-25 02:19:29 +03:00
response = "WPF Hardware Acceleration: " + s ;
break ;
case 'DEFAULT' :
2022-01-17 05:28:26 +03:00
try { reg . DeleteKey ( reg . HKEY . Users , key + '\\SOFTWARE\\Microsoft\\Avalon.Graphics' , 'DisableHWAcceleration' ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
response = 'OK' ;
break ;
2020-05-31 03:21:45 +03:00
}
2021-01-25 02:19:29 +03:00
}
break ;
case 'tsid' :
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = "TSID: " + ( require ( 'MeshAgent' ) . _tsid == null ? "console" : require ( 'MeshAgent' ) . _tsid ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var i = parseInt ( args [ '_' ] [ 0 ] ) ;
require ( 'MeshAgent' ) . _tsid = ( isNaN ( i ) ? null : i ) ;
response = "TSID set to: " + ( require ( 'MeshAgent' ) . _tsid == null ? "console" : require ( 'MeshAgent' ) . _tsid ) ;
2020-05-17 10:22:26 +03:00
}
2021-01-25 02:19:29 +03:00
} else
{ response = "TSID command only supported on Windows" ; }
break ;
case 'activeusers' :
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
var p = require ( 'user-sessions' ) . enumerateUsers ( ) ;
p . sessionid = sessionid ;
2021-01-25 08:51:22 +03:00
p . then ( function ( u ) {
2021-01-25 02:19:29 +03:00
var v = [ ] ;
2021-01-25 08:51:22 +03:00
for ( var i in u ) {
2021-01-25 02:19:29 +03:00
if ( u [ i ] . State == 'Active' ) { v . push ( { tsid : i , type : u [ i ] . StationName , user : u [ i ] . Username , domain : u [ i ] . Domain } ) ; }
}
sendConsoleText ( JSON . stringify ( v , null , 1 ) , this . sessionid ) ;
} ) ;
} else
{ response = "activeusers command only supported on Windows" ; }
break ;
case 'wallpaper' :
2021-01-25 08:51:22 +03:00
if ( process . platform != 'win32' && ! ( process . platform == 'linux' && require ( 'linux-gnome-helpers' ) . available ) ) {
2021-01-25 02:19:29 +03:00
response = "wallpaper command not supported on this platform" ;
}
2021-01-25 08:51:22 +03:00
else {
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: wallpaper (GET|TOGGLE)' ; // Display usage
2020-05-17 10:22:26 +03:00
}
2021-01-25 08:51:22 +03:00
else {
switch ( args [ '_' ] [ 0 ] . toUpperCase ( ) ) {
2021-01-25 02:19:29 +03:00
default :
response = 'Proper usage: wallpaper (GET|TOGGLE)' ; // Display usage
2020-05-17 10:22:26 +03:00
break ;
2021-01-25 02:19:29 +03:00
case 'GET' :
case 'TOGGLE' :
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
var id = require ( 'user-sessions' ) . getProcessOwnerName ( process . pid ) . tsid == 0 ? 1 : 0 ;
var child = require ( 'child_process' ) . execFile ( process . execPath , [ process . execPath . split ( '\\' ) . pop ( ) , '-b64exec' , 'dmFyIFNQSV9HRVRERVNLV0FMTFBBUEVSID0gMHgwMDczOwp2YXIgU1BJX1NFVERFU0tXQUxMUEFQRVIgPSAweDAwMTQ7CnZhciBHTSA9IHJlcXVpcmUoJ19HZW5lcmljTWFyc2hhbCcpOwp2YXIgdXNlcjMyID0gR00uQ3JlYXRlTmF0aXZlUHJveHkoJ3VzZXIzMi5kbGwnKTsKdXNlcjMyLkNyZWF0ZU1ldGhvZCgnU3lzdGVtUGFyYW1ldGVyc0luZm9BJyk7CgppZiAocHJvY2Vzcy5hcmd2Lmxlbmd0aCA9PSAzKQp7CiAgICB2YXIgdiA9IEdNLkNyZWF0ZVZhcmlhYmxlKDEwMjQpOwogICAgdXNlcjMyLlN5c3RlbVBhcmFtZXRlcnNJbmZvQShTUElfR0VUREVTS1dBTExQQVBFUiwgdi5fc2l6ZSwgdiwgMCk7CiAgICBjb25zb2xlLmxvZyh2LlN0cmluZyk7CiAgICBwcm9jZXNzLmV4aXQoKTsKfQplbHNlCnsKICAgIHZhciBuYiA9IEdNLkNyZWF0ZVZhcmlhYmxlKHByb2Nlc3MuYXJndlszXSk7CiAgICB1c2VyMzIuU3lzdGVtUGFyYW1ldGVyc0luZm9BKFNQSV9TRVRERVNLV0FMTFBBUEVSLCBuYi5fc2l6ZSwgbmIsIDApOwogICAgcHJvY2Vzcy5leGl0KCk7Cn0=' ] , { type : id } ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
var current = child . stdout . str . trim ( ) ;
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] [ 0 ] . toUpperCase ( ) == 'GET' ) {
2021-01-25 02:19:29 +03:00
response = current ;
break ;
}
2021-01-25 08:51:22 +03:00
if ( current != '' ) {
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . _wallpaper = current ;
response = 'Wallpaper cleared' ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Wallpaper restored' ;
}
child = require ( 'child_process' ) . execFile ( process . execPath , [ process . execPath . split ( '\\' ) . pop ( ) , '-b64exec' , 'dmFyIFNQSV9HRVRERVNLV0FMTFBBUEVSID0gMHgwMDczOwp2YXIgU1BJX1NFVERFU0tXQUxMUEFQRVIgPSAweDAwMTQ7CnZhciBHTSA9IHJlcXVpcmUoJ19HZW5lcmljTWFyc2hhbCcpOwp2YXIgdXNlcjMyID0gR00uQ3JlYXRlTmF0aXZlUHJveHkoJ3VzZXIzMi5kbGwnKTsKdXNlcjMyLkNyZWF0ZU1ldGhvZCgnU3lzdGVtUGFyYW1ldGVyc0luZm9BJyk7CgppZiAocHJvY2Vzcy5hcmd2Lmxlbmd0aCA9PSAzKQp7CiAgICB2YXIgdiA9IEdNLkNyZWF0ZVZhcmlhYmxlKDEwMjQpOwogICAgdXNlcjMyLlN5c3RlbVBhcmFtZXRlcnNJbmZvQShTUElfR0VUREVTS1dBTExQQVBFUiwgdi5fc2l6ZSwgdiwgMCk7CiAgICBjb25zb2xlLmxvZyh2LlN0cmluZyk7CiAgICBwcm9jZXNzLmV4aXQoKTsKfQplbHNlCnsKICAgIHZhciBuYiA9IEdNLkNyZWF0ZVZhcmlhYmxlKHByb2Nlc3MuYXJndlszXSk7CiAgICB1c2VyMzIuU3lzdGVtUGFyYW1ldGVyc0luZm9BKFNQSV9TRVRERVNLV0FMTFBBUEVSLCBuYi5fc2l6ZSwgbmIsIDApOwogICAgcHJvY2Vzcy5leGl0KCk7Cn0=' , current != '' ? '""' : require ( 'MeshAgent' ) . _wallpaper ] , { type : id } ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( ) { } ) ;
child . waitExit ( ) ;
2020-05-17 10:22:26 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var id = require ( 'user-sessions' ) . consoleUid ( ) ;
var current = require ( 'linux-gnome-helpers' ) . getDesktopWallpaper ( id ) ;
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] [ 0 ] . toUpperCase ( ) == 'GET' ) {
2021-01-25 02:19:29 +03:00
response = current ;
break ;
}
2021-01-25 08:51:22 +03:00
if ( current != '/dev/null' ) {
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . _wallpaper = current ;
response = 'Wallpaper cleared' ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Wallpaper restored' ;
}
require ( 'linux-gnome-helpers' ) . setDesktopWallpaper ( id , current != '/dev/null' ? undefined : require ( 'MeshAgent' ) . _wallpaper ) ;
2020-05-17 10:22:26 +03:00
}
break ;
2020-04-13 08:53:49 +03:00
}
}
2021-01-25 02:19:29 +03:00
}
break ;
case 'safemode' :
2022-08-04 11:52:29 +03:00
if ( process . platform != 'win32' ) {
2021-01-25 02:19:29 +03:00
response = 'safemode only supported on Windows Platforms'
2022-03-29 08:15:45 +03:00
}
2022-08-04 11:52:29 +03:00
else {
if ( ! bcdOK ( ) ) {
2022-03-29 08:15:45 +03:00
response = 'safemode not supported on 64 bit Windows from a 32 bit process'
break ;
}
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: safemode (ON|OFF|STATUS)' ; // Display usage
2020-03-12 07:25:03 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var svcname = process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
svcname = require ( 'MeshAgent' ) . serviceName ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2019-12-11 01:15:14 +03:00
2021-01-25 08:51:22 +03:00
switch ( args [ '_' ] [ 0 ] . toUpperCase ( ) ) {
2019-12-11 01:15:14 +03:00
default :
2021-01-25 02:19:29 +03:00
response = 'Proper usage: safemode (ON|OFF|STATUS)' ; // Display usage
2019-12-11 01:15:14 +03:00
break ;
case 'ON' :
2021-01-25 02:19:29 +03:00
require ( 'win-bcd' ) . setKey ( 'safeboot' , 'Network' ) ;
require ( 'win-bcd' ) . enableSafeModeService ( svcname ) ;
2019-12-11 01:15:14 +03:00
break ;
case 'OFF' :
2021-01-25 02:19:29 +03:00
require ( 'win-bcd' ) . deleteKey ( 'safeboot' ) ;
2019-12-11 01:15:14 +03:00
break ;
case 'STATUS' :
2021-01-25 02:19:29 +03:00
var nextboot = require ( 'win-bcd' ) . getKey ( 'safeboot' ) ;
2021-01-25 08:51:22 +03:00
if ( nextboot ) {
switch ( nextboot ) {
2021-01-25 02:19:29 +03:00
case 'Network' :
case 'network' :
nextboot = 'SAFE_MODE_NETWORK' ;
2019-11-15 02:22:00 +03:00
break ;
2021-01-25 02:19:29 +03:00
default :
nextboot = 'SAFE_MODE' ;
2019-11-15 02:22:00 +03:00
break ;
2019-10-29 21:05:33 +03:00
}
2021-01-25 02:19:29 +03:00
}
response = 'Current: ' + require ( 'win-bcd' ) . bootMode + ', NextBoot: ' + ( nextboot ? nextboot : 'NORMAL' ) ;
break ;
2019-10-25 20:33:10 +03:00
}
}
2021-01-25 02:19:29 +03:00
}
break ;
2021-01-25 08:51:22 +03:00
/ *
case 'border' :
{
if ( ( args [ '_' ] . length == 1 ) && ( args [ '_' ] [ 0 ] == 'on' ) ) {
if ( meshCoreObj . users . length > 0 ) {
obj . borderManager . Start ( meshCoreObj . users [ 0 ] ) ;
response = 'Border blinking is on.' ;
2021-01-19 03:11:15 +03:00
} else {
2021-01-25 08:51:22 +03:00
response = 'Cannot turn on border blinking, no logged in users.' ;
2018-08-30 04:47:22 +03:00
}
2021-01-25 08:51:22 +03:00
} else if ( ( args [ '_' ] . length == 1 ) && ( args [ '_' ] [ 0 ] == 'off' ) ) {
obj . borderManager . Stop ( ) ;
response = 'Border blinking is off.' ;
} else {
response = 'Proper usage: border "on|off"' ; // Display correct command usage
2021-01-19 03:11:15 +03:00
}
2021-01-25 08:51:22 +03:00
}
break ;
* /
2021-01-25 02:19:29 +03:00
case 'av' :
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
// Windows Command: "wmic /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /FORMAT:CSV"
response = JSON . stringify ( require ( 'win-info' ) . av ( ) , null , 1 ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Not supported on the platform' ;
2018-12-08 03:36:27 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'log' :
if ( args [ '_' ] . length != 1 ) { response = 'Proper usage: log "sample text"' ; } else { MeshServerLog ( args [ '_' ] [ 0 ] ) ; response = 'ok' ; }
break ;
case 'getclip' :
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . isService ) {
2021-01-25 02:19:29 +03:00
require ( 'clipboard' ) . dispatchRead ( ) . then ( function ( str ) { sendConsoleText ( str , sessionid ) ; } ) ;
2021-01-25 08:51:22 +03:00
} else {
2022-08-28 01:47:19 +03:00
require ( 'clipboard' ) . read ( ) . then ( function ( str ) { sendConsoleText ( str , sessionid ) ; } ) ;
2018-11-25 23:59:42 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'setclip' : {
2022-08-28 21:32:54 +03:00
if ( pendingSetClip ) {
response = 'Busy' ;
} else if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: setclip "sample text"' ;
2021-04-24 20:53:13 +03:00
} else {
if ( require ( 'MeshAgent' ) . isService ) {
if ( process . platform != 'win32' ) {
2021-02-28 03:19:13 +03:00
require ( 'clipboard' ) . dispatchWrite ( args [ '_' ] [ 0 ] ) ;
}
2021-07-24 19:28:41 +03:00
else {
2021-05-11 09:43:48 +03:00
var clipargs = args [ '_' ] [ 0 ] ;
2021-04-19 20:55:09 +03:00
var uid = require ( 'user-sessions' ) . consoleUid ( ) ;
var user = require ( 'user-sessions' ) . getUsername ( uid ) ;
var domain = require ( 'user-sessions' ) . getDomain ( uid ) ;
2021-05-11 09:43:48 +03:00
user = ( domain + '\\' + user ) ;
2022-08-28 01:01:22 +03:00
if ( this . _dispatcher ) { this . _dispatcher . close ( ) ; }
2021-05-11 09:43:48 +03:00
this . _dispatcher = require ( 'win-dispatcher' ) . dispatch ( { user : user , modules : [ { name : 'clip-dispatch' , script : "module.exports = { dispatch: function dispatch(val) { require('clipboard')(val); process.exit(); } };" } ] , launch : { module : 'clip-dispatch' , method : 'dispatch' , args : [ clipargs ] } } ) ;
this . _dispatcher . parent = this ;
//require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
2022-08-28 21:32:54 +03:00
pendingSetClip = true ;
2021-07-24 19:28:41 +03:00
this . _dispatcher . on ( 'connection' , function ( c ) {
2021-05-11 09:43:48 +03:00
this . _c = c ;
this . _c . root = this . parent ;
2022-08-26 06:11:47 +03:00
this . _c . on ( 'end' , function ( )
{
2022-08-28 21:32:54 +03:00
pendingSetClip = false ;
2022-08-28 01:47:19 +03:00
try { this . root . _dispatcher . close ( ) ; } catch ( ex ) { }
2021-05-11 09:43:48 +03:00
this . root . _dispatcher = null ;
this . root = null ;
} ) ;
} ) ;
2021-02-28 03:19:13 +03:00
}
2021-01-25 02:19:29 +03:00
response = 'Setting clipboard to: "' + args [ '_' ] [ 0 ] + '"' ;
}
2021-07-24 19:28:41 +03:00
else {
2022-08-28 01:47:19 +03:00
require ( 'clipboard' ) ( args [ '_' ] [ 0 ] ) ; response = 'Setting clipboard to: "' + args [ '_' ] [ 0 ] + '"' ;
2018-04-21 02:05:06 +03:00
}
2018-04-11 23:49:05 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'openurl' : {
if ( args [ '_' ] . length != 1 ) { response = 'Proper usage: openurl (url)' ; } // Display usage
else { if ( openUserDesktopUrl ( args [ '_' ] [ 0 ] ) == null ) { response = 'Failed.' ; } else { response = 'Success.' ; } }
break ;
}
2024-04-03 11:51:18 +03:00
case 'openfile' : {
if ( args [ '_' ] . length != 1 ) { response = 'Proper usage: openfile (filepath)' ; } // Display usage
else { if ( openFileOnDesktop ( args [ '_' ] [ 0 ] ) == null ) { response = 'Failed.' ; } else { response = 'Success.' ; } }
break ;
}
2021-01-25 02:19:29 +03:00
case 'users' : {
if ( meshCoreObj . users == null ) { response = 'Active users are unknown.' ; } else { response = 'Active Users: ' + meshCoreObj . users . join ( ', ' ) + '.' ; }
require ( 'user-sessions' ) . enumerateUsers ( ) . then ( function ( u ) { for ( var i in u ) { sendConsoleText ( u [ i ] ) ; } } ) ;
break ;
}
2022-03-29 08:15:45 +03:00
case 'kvmusers' :
response = JSON . stringify ( require ( 'kvm-helper' ) . users ( ) , null , 1 ) ;
break ;
2021-01-25 02:19:29 +03:00
case 'toast' : {
if ( args [ '_' ] . length < 1 ) { response = 'Proper usage: toast "message"' ; } else {
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . _tsid == null ) {
2021-01-25 02:19:29 +03:00
require ( 'toaster' ) . Toast ( 'MeshCentral' , args [ '_' ] [ 0 ] ) . then ( sendConsoleText , sendConsoleText ) ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
require ( 'toaster' ) . Toast ( 'MeshCentral' , args [ '_' ] [ 0 ] , require ( 'MeshAgent' ) . _tsid ) . then ( sendConsoleText , sendConsoleText ) ;
2018-04-11 23:49:05 +03:00
}
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'setdebug' : {
if ( args [ '_' ] . length < 1 ) { response = 'Proper usage: setdebug (target), 0 = Disabled, 1 = StdOut, 2 = This Console, * = All Consoles, 4 = WebLog, 8 = Logfile' ; } // Display usage
else { if ( args [ '_' ] [ 0 ] == '*' ) { console . setDestination ( 2 ) ; } else { console . setDestination ( parseInt ( args [ '_' ] [ 0 ] ) , sessionid ) ; } }
break ;
}
case 'ps' : {
2021-01-25 08:51:22 +03:00
processManager . getProcesses ( function ( plist ) {
2021-01-25 02:19:29 +03:00
var x = '' ;
for ( var i in plist ) { x += i + ( ( plist [ i ] . user ) ? ( ', ' + plist [ i ] . user ) : '' ) + ', ' + plist [ i ] . cmd + '\r\n' ; }
sendConsoleText ( x , sessionid ) ;
} ) ;
break ;
}
case 'kill' : {
2021-01-25 08:51:22 +03:00
if ( ( args [ '_' ] . length < 1 ) ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: kill [pid]' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
process . kill ( parseInt ( args [ '_' ] [ 0 ] ) ) ;
response = 'Killed process ' + args [ '_' ] [ 0 ] + '.' ;
2018-03-13 04:16:06 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'smbios' : {
if ( SMBiosTables == null ) { response = 'SMBios tables not available.' ; } else { response = objToString ( SMBiosTables , 0 , ' ' , true ) ; }
break ;
}
case 'rawsmbios' : {
if ( SMBiosTablesRaw == null ) { response = 'SMBios tables not available.' ; } else {
response = '' ;
2021-01-25 08:51:22 +03:00
for ( var i in SMBiosTablesRaw ) {
2021-01-25 02:19:29 +03:00
var header = false ;
2021-01-25 08:51:22 +03:00
for ( var j in SMBiosTablesRaw [ i ] ) {
if ( SMBiosTablesRaw [ i ] [ j ] . length > 0 ) {
2021-01-25 02:19:29 +03:00
if ( header == false ) { response += ( 'Table type #' + i + ( ( require ( 'smbios' ) . smTableTypes [ i ] == null ) ? '' : ( ', ' + require ( 'smbios' ) . smTableTypes [ i ] ) ) ) + '\r\n' ; header = true ; }
response += ( ' ' + SMBiosTablesRaw [ i ] [ j ] . toString ( 'hex' ) ) + '\r\n' ;
2018-03-13 04:16:06 +03:00
}
2018-09-28 02:17:05 +03:00
}
}
2018-03-13 04:16:06 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'eval' : { // Eval JavaScript
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length < 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: eval "JavaScript code"' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = JSON . stringify ( mesh . eval ( args [ '_' ] [ 0 ] ) ) ; // This can only be run by trusted administrator.
}
break ;
}
case 'uninstallagent' : // Uninstall this agent
var agentName = process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
agentName = require ( 'MeshAgent' ) . serviceName ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-05 20:47:47 +03:00
2021-01-25 08:51:22 +03:00
if ( ! require ( 'service-manager' ) . manager . getService ( agentName ) . isMe ( ) ) {
2021-01-25 02:19:29 +03:00
response = 'Uininstall failed, this instance is not the service instance' ;
2021-01-25 08:51:22 +03:00
} else {
2022-01-17 05:28:26 +03:00
try { diagnosticAgent _uninstall ( ) ; } catch ( ex ) { }
2021-01-25 02:19:29 +03:00
var js = "require('service-manager').manager.getService('" + agentName + "').stop(); require('service-manager').manager.uninstallService('" + agentName + "'); process.exit();" ;
this . child = require ( 'child_process' ) . execFile ( process . execPath , [ process . platform == 'win32' ? ( process . execPath . split ( '\\' ) . pop ( ) ) : ( process . execPath . split ( '/' ) . pop ( ) ) , '-b64exec' , Buffer . from ( js ) . toString ( 'base64' ) ] , { type : 4 , detached : true } ) ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
case 'notify' : { // Send a notification message to the mesh
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: notify "message" [--session]' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var notification = { action : 'msg' , type : 'notify' , value : args [ '_' ] [ 0 ] , tag : 'console' } ;
if ( args . session ) { notification . sessionid = sessionid ; } // If "--session" is specified, notify only this session, if not, the server will notify the mesh
mesh . SendCommand ( notification ) ; // no sessionid or userid specified, notification will go to the entire mesh
response = "ok" ;
}
break ;
}
case 'cpuinfo' : { // Return system information
// CPU & memory utilization
pr = require ( 'sysinfo' ) . cpuUtilization ( ) ;
pr . sessionid = sessionid ;
2021-01-25 08:51:22 +03:00
pr . then ( function ( data ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( JSON . stringify (
{
cpu : data ,
memory : require ( 'sysinfo' ) . memUtilization ( ) ,
thermals : require ( 'sysinfo' ) . thermals == null ? [ ] : require ( 'sysinfo' ) . thermals ( )
} , null , 1 ) , this . sessionid ) ;
2021-01-25 08:51:22 +03:00
} , function ( e ) {
2021-04-24 20:53:13 +03:00
sendConsoleText ( e ) ;
} ) ;
2021-01-25 02:19:29 +03:00
break ;
}
case 'sysinfo' : { // Return system information
2021-01-25 08:51:22 +03:00
getSystemInformation ( function ( results , err ) {
2024-02-22 20:57:12 +03:00
if ( results == null ) {
sendConsoleText ( err , this . sessionid ) ;
} else {
2021-01-25 02:19:29 +03:00
sendConsoleText ( JSON . stringify ( results , null , 1 ) , this . sessionid ) ;
2024-02-22 20:57:12 +03:00
mesh . SendCommand ( { action : 'sysinfo' , sessionid : this . sessionid , data : results } ) ;
2021-01-25 02:19:29 +03:00
}
} ) ;
break ;
}
case 'info' : { // Return information about the agent and agent core module
response = 'Current Core: ' + meshCoreObj . value + '\r\nAgent Time: ' + Date ( ) + '.\r\nUser Rights: 0x' + rights . toString ( 16 ) + '.\r\nPlatform: ' + process . platform + '.\r\nCapabilities: ' + meshCoreObj . caps + '.\r\nServer URL: ' + mesh . ServerUrl + '.' ;
if ( amt != null ) { response += '\r\nBuilt-in LMS: ' + [ 'Disabled' , 'Connecting..' , 'Connected' ] [ amt . lmsstate ] + '.' ; }
if ( meshCoreObj . osdesc ) { response += '\r\nOS: ' + meshCoreObj . osdesc + '.' ; }
response += '\r\nModules: ' + addedModules . join ( ', ' ) + '.' ;
response += '\r\nServer Connection: ' + mesh . isControlChannelConnected + ', State: ' + meshServerConnectionState + '.' ;
var oldNodeId = db . Get ( 'OldNodeId' ) ;
if ( oldNodeId != null ) { response += '\r\nOldNodeID: ' + oldNodeId + '.' ; }
if ( process . platform == 'linux' || process . platform == 'freebsd' ) { response += '\r\nX11 support: ' + require ( 'monitor-info' ) . kvm _x11 _support + '.' ; }
2024-01-26 17:59:34 +03:00
response += '\r\nApplication Location: ' + process . cwd ( ) ;
2021-01-25 02:19:29 +03:00
//response += '\r\Debug Console: ' + debugConsole + '.';
break ;
}
case 'osinfo' : { // Return the operating system information
var i = 1 ;
if ( args [ '_' ] . length > 0 ) { i = parseInt ( args [ '_' ] [ 0 ] ) ; if ( i > 8 ) { i = 8 ; } response = 'Calling ' + i + ' times.' ; }
2021-01-25 08:51:22 +03:00
for ( var j = 0 ; j < i ; j ++ ) {
2021-01-25 02:19:29 +03:00
var pr = require ( 'os' ) . name ( ) ;
2019-08-07 03:58:29 +03:00
pr . sessionid = sessionid ;
2021-01-25 08:51:22 +03:00
pr . then ( function ( v ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( "OS: " + v + ( process . platform == 'win32' ? ( require ( 'win-virtual-terminal' ) . supported ? ' [ConPTY: YES]' : ' [ConPTY: NO]' ) : '' ) , this . sessionid ) ;
2019-09-24 02:46:26 +03:00
} ) ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'args' : { // Displays parsed command arguments
response = 'args ' + objToString ( args , 0 , ' ' , true ) ;
break ;
}
case 'print' : { // Print a message on the mesh agent console, does nothing when running in the background
var r = [ ] ;
for ( var i in args [ '_' ] ) { r . push ( args [ '_' ] [ i ] ) ; }
console . log ( r . join ( ' ' ) ) ;
response = 'Message printed on agent console.' ;
break ;
}
case 'type' : { // Returns the content of a file
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: type (filepath) [maxlength]' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var max = 4096 ;
if ( ( args [ '_' ] . length > 1 ) && ( typeof args [ '_' ] [ 1 ] == 'number' ) ) { max = args [ '_' ] [ 1 ] ; }
if ( max > 4096 ) max = 4096 ;
var buf = Buffer . alloc ( max ) , fd = fs . openSync ( args [ '_' ] [ 0 ] , "r" ) , r = fs . readSync ( fd , buf , 0 , max ) ; // Read the file content
response = buf . toString ( ) ;
var i = response . indexOf ( '\n' ) ;
if ( ( i > 0 ) && ( response [ i - 1 ] != '\r' ) ) { response = response . split ( '\n' ) . join ( '\r\n' ) ; }
if ( r == max ) response += '...' ;
fs . closeSync ( fd ) ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'dbkeys' : { // Return all data store keys
response = JSON . stringify ( db . Keys ) ;
break ;
}
case 'dbget' : { // Return the data store value for a given key
if ( db == null ) { response = 'Database not accessible.' ; break ; }
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: dbget (key)' ; // Display the value for a given database key
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = db . Get ( args [ '_' ] [ 0 ] ) ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'dbset' : { // Set a data store key and value pair
if ( db == null ) { response = 'Database not accessible.' ; break ; }
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 2 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: dbset (key) (value)' ; // Set a database key
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var r = db . Put ( args [ '_' ] [ 0 ] , args [ '_' ] [ 1 ] ) ;
response = 'Key set: ' + r ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'dbcompact' : { // Compact the data store
if ( db == null ) { response = 'Database not accessible.' ; break ; }
var r = db . Compact ( ) ;
response = 'Database compacted: ' + r ;
break ;
}
case 'httpget' : {
2021-01-25 08:51:22 +03:00
if ( consoleHttpRequest != null ) {
2021-01-25 02:19:29 +03:00
response = 'HTTP operation already in progress.' ;
2021-01-25 08:51:22 +03:00
} else {
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: httpget (url)' ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var options = http . parseUri ( args [ '_' ] [ 0 ] ) ;
options . method = 'GET' ;
2021-01-25 08:51:22 +03:00
if ( options == null ) {
2021-01-25 02:19:29 +03:00
response = 'Invalid url.' ;
2021-01-25 08:51:22 +03:00
} else {
2022-01-17 05:28:26 +03:00
try { consoleHttpRequest = http . request ( options , consoleHttpResponse ) ; } catch ( ex ) { response = 'Invalid HTTP GET request' ; }
2021-01-25 02:19:29 +03:00
consoleHttpRequest . sessionid = sessionid ;
2021-01-25 08:51:22 +03:00
if ( consoleHttpRequest != null ) {
2021-01-25 02:19:29 +03:00
consoleHttpRequest . end ( ) ;
response = 'HTTPGET ' + options . protocol + '//' + options . host + ':' + options . port + options . path ;
2017-08-28 19:27:45 +03:00
}
}
}
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'wslist' : { // List all web sockets
response = '' ;
2021-01-25 08:51:22 +03:00
for ( var i in consoleWebSockets ) {
2021-01-25 02:19:29 +03:00
var httprequest = consoleWebSockets [ i ] ;
response += 'Websocket #' + i + ', ' + httprequest . url + '\r\n' ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
if ( response == '' ) { response = 'no websocket sessions.' ; }
break ;
}
case 'wsconnect' : { // Setup a web socket
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: wsconnect (url)\r\nFor example: wsconnect wss://localhost:443/meshrelay.ashx?id=abc' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var httprequest = null ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var options = http . parseUri ( args [ '_' ] [ 0 ] . split ( '$' ) . join ( '%24' ) . split ( '@' ) . join ( '%40' ) ) ; // Escape the $ and @ characters in the URL
options . rejectUnauthorized = 0 ;
httprequest = http . request ( options ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { response = 'Invalid HTTP websocket request' ; }
2021-01-25 08:51:22 +03:00
if ( httprequest != null ) {
2021-01-25 02:19:29 +03:00
httprequest . upgrade = onWebSocketUpgrade ;
httprequest . on ( 'error' , function ( e ) { sendConsoleText ( "ERROR: Unable to connect to: " + this . url + ", " + JSON . stringify ( e ) ) ; } ) ;
var index = 1 ;
while ( consoleWebSockets [ index ] ) { index ++ ; }
httprequest . sessionid = sessionid ;
httprequest . index = index ;
httprequest . url = args [ '_' ] [ 0 ] ;
consoleWebSockets [ index ] = httprequest ;
response = 'New websocket session #' + index ;
2017-08-28 19:27:45 +03:00
}
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'wssend' : { // Send data on a web socket
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: wssend (socketnumber)\r\n' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
for ( var i in consoleWebSockets ) {
2017-08-28 19:27:45 +03:00
var httprequest = consoleWebSockets [ i ] ;
2021-01-25 02:19:29 +03:00
response += 'Websocket #' + i + ', ' + httprequest . url + '\r\n' ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var i = parseInt ( args [ '_' ] [ 0 ] ) ;
var httprequest = consoleWebSockets [ i ] ;
2021-01-25 08:51:22 +03:00
if ( httprequest != undefined ) {
2021-01-25 02:19:29 +03:00
httprequest . s . write ( args [ '_' ] [ 1 ] ) ;
response = 'ok' ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Invalid web socket number' ;
2019-03-05 10:48:45 +03:00
}
2018-03-21 03:48:03 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'wsclose' : { // Close a websocket
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length == 0 ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: wsclose (socketnumber)' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var i = parseInt ( args [ '_' ] [ 0 ] ) ;
var httprequest = consoleWebSockets [ i ] ;
2021-01-25 08:51:22 +03:00
if ( httprequest != undefined ) {
2021-01-25 02:19:29 +03:00
if ( httprequest . s != null ) { httprequest . s . end ( ) ; } else { httprequest . end ( ) ; }
response = 'ok' ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = 'Invalid web socket number' ;
2017-09-21 00:44:22 +03:00
}
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'tunnels' : { // Show the list of current tunnels
response = '' ;
2021-10-19 21:47:39 +03:00
for ( var i in tunnels ) {
response += 'Tunnel #' + i + ', ' + tunnels [ i ] . protocol ; //tunnels[i].url
if ( tunnels [ i ] . userid ) { response += ', ' + tunnels [ i ] . userid ; }
if ( tunnels [ i ] . guestname ) { response += '/' + tunnels [ i ] . guestname ; }
response += '\r\n'
}
2021-01-25 02:19:29 +03:00
if ( response == '' ) { response = 'No websocket sessions.' ; }
break ;
}
case 'ls' : { // Show list of files and folders
response = '' ;
var xpath = '*' ;
if ( args [ '_' ] . length > 0 ) { xpath = obj . path . join ( args [ '_' ] [ 0 ] , '*' ) ; }
response = 'List of ' + xpath + '\r\n' ;
var results = fs . readdirSync ( xpath ) ;
2021-01-25 08:51:22 +03:00
for ( var i = 0 ; i < results . length ; ++ i ) {
2021-01-25 02:19:29 +03:00
var stat = null , p = obj . path . join ( args [ '_' ] [ 0 ] , results [ i ] ) ;
2022-01-17 05:28:26 +03:00
try { stat = fs . statSync ( p ) ; } catch ( ex ) { }
2021-01-25 08:51:22 +03:00
if ( ( stat == null ) || ( stat == undefined ) ) {
2021-01-25 02:19:29 +03:00
response += ( results [ i ] + "\r\n" ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response += ( results [ i ] + " " + ( ( stat . isDirectory ( ) ) ? "(Folder)" : "(File)" ) + "\r\n" ) ;
2017-09-01 21:23:22 +03:00
}
2017-08-28 22:48:53 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'lsx' : { // Show list of files and folders
response = objToString ( getDirectoryInfo ( args [ '_' ] [ 0 ] ) , 0 , ' ' , true ) ;
break ;
}
case 'lock' : { // Lock the current user out of the desktop
2021-09-03 10:55:00 +03:00
lockDesktop ( ) ;
2021-01-25 02:19:29 +03:00
break ;
}
case 'amt' : { // Show Intel AMT status
2021-01-25 08:51:22 +03:00
if ( amt != null ) {
amt . getMeiState ( 9 , function ( state ) {
2021-01-25 02:19:29 +03:00
var resp = "Intel AMT not detected." ;
if ( state != null ) { resp = objToString ( state , 0 , ' ' , true ) ; }
sendConsoleText ( resp , sessionid ) ;
2017-09-13 21:25:57 +03:00
} ) ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = "Intel AMT not detected." ;
2017-09-13 21:25:57 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'netinfo' : { // Show network interface information
var interfaces = require ( 'os' ) . networkInterfaces ( ) ;
response = objToString ( interfaces , 0 , ' ' , true ) ;
break ;
}
case 'wakeonlan' : { // Send wake-on-lan
2021-04-24 20:53:13 +03:00
if ( ( args [ '_' ] . length != 1 ) || ( args [ '_' ] [ 0 ] . length != 12 ) ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: wakeonlan [mac], for example "wakeonlan 010203040506".' ;
2021-04-24 20:53:13 +03:00
} else {
2021-04-24 20:20:35 +03:00
var count = sendWakeOnLanEx ( [ args [ '_' ] [ 0 ] ] ) ;
2021-04-24 20:34:29 +03:00
sendWakeOnLanEx ( [ args [ '_' ] [ 0 ] ] ) ;
sendWakeOnLanEx ( [ args [ '_' ] [ 0 ] ] ) ;
2021-04-24 20:20:35 +03:00
response = 'Sending wake-on-lan on ' + count + ' interface(s).' ;
2017-09-21 00:44:22 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
2023-11-12 22:04:59 +03:00
case 'display' : {
if ( args [ '_' ] . length != 1 ) {
2023-11-12 22:23:26 +03:00
response = 'Proper usage: display (sleep | awake)' ;
2023-11-12 22:04:59 +03:00
} else {
var sleepawake = [ args [ '_' ] [ 0 ] ] ;
if ( sleepawake == 'sleep' ) {
require ( 'power-monitor' ) . sleepDisplay ( )
} else if ( sleepawake == 'awake' ) {
require ( 'power-monitor' ) . wakeDisplay ( )
}
response = 'Setting Display To ' + sleepawake ;
}
break ;
}
2021-01-25 02:19:29 +03:00
case 'sendall' : { // Send a message to all consoles on this mesh
sendConsoleText ( args [ '_' ] . join ( ' ' ) ) ;
break ;
}
case 'power' : { // Execute a power action on this computer
2021-01-25 08:51:22 +03:00
if ( mesh . ExecPowerState == undefined ) {
2021-01-25 02:19:29 +03:00
response = 'Power command not supported on this agent.' ;
2021-01-25 08:51:22 +03:00
} else {
if ( ( args [ '_' ] . length == 0 ) || isNaN ( Number ( args [ '_' ] [ 0 ] ) ) ) {
2021-01-25 02:19:29 +03:00
response = 'Proper usage: power (actionNumber), where actionNumber is:\r\n LOGOFF = 1\r\n SHUTDOWN = 2\r\n REBOOT = 3\r\n SLEEP = 4\r\n HIBERNATE = 5\r\n DISPLAYON = 6\r\n KEEPAWAKE = 7\r\n BEEP = 8\r\n CTRLALTDEL = 9\r\n VIBRATE = 13\r\n FLASH = 14' ; // Display correct command usage
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
var r = mesh . ExecPowerState ( Number ( args [ '_' ] [ 0 ] ) , Number ( args [ '_' ] [ 1 ] ) ) ;
response = 'Power action executed with return code: ' + r + '.' ;
}
2019-02-08 07:06:01 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'location' : {
2021-01-25 08:51:22 +03:00
getIpLocationData ( function ( location ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( objToString ( { action : 'iplocation' , type : 'publicip' , value : location } , 0 , ' ' ) ) ;
} ) ;
break ;
}
case 'parseuri' : {
response = JSON . stringify ( http . parseUri ( args [ '_' ] [ 0 ] ) ) ;
break ;
}
case 'scanwifi' : {
2021-01-25 08:51:22 +03:00
if ( wifiScanner != null ) {
2021-01-25 02:19:29 +03:00
var wifiPresent = wifiScanner . hasWireless ;
if ( wifiPresent ) { response = "Perfoming Wifi scan..." ; wifiScanner . Scan ( ) ; } else { response = "Wifi absent." ; }
} else
{ response = "Wifi module not present." ; }
break ;
}
case 'modules' : {
response = JSON . stringify ( addedModules ) ;
break ;
}
case 'listservices' : {
var services = require ( 'service-manager' ) . manager . enumerateService ( ) ;
response = JSON . stringify ( services , null , 1 ) ;
break ;
}
case 'getscript' : {
2021-01-25 08:51:22 +03:00
if ( args [ '_' ] . length != 1 ) {
2021-01-25 02:19:29 +03:00
response = "Proper usage: getscript [scriptNumber]." ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
mesh . SendCommand ( { action : 'getScript' , type : args [ '_' ] [ 0 ] } ) ;
2019-08-01 02:49:23 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'diagnostic' :
{
2021-01-25 08:51:22 +03:00
if ( ! mesh . DAIPC . listening ) {
2021-01-25 02:19:29 +03:00
response = 'Unable to bind to Diagnostic IPC, most likely because the path (' + process . cwd ( ) + ') is not on a local file system' ;
break ;
2019-02-08 07:06:01 +03:00
}
2021-01-25 02:19:29 +03:00
var diag = diagnosticAgent _installCheck ( ) ;
2021-01-25 08:51:22 +03:00
if ( diag ) {
if ( args [ '_' ] . length == 1 && args [ '_' ] [ 0 ] == 'uninstall' ) {
2021-01-25 02:19:29 +03:00
diagnosticAgent _uninstall ( ) ;
response = 'Diagnostic Agent uninstalled' ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
response = 'Diagnostic Agent installed at: ' + diag . appLocation ( ) ;
2019-06-15 02:33:53 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
else {
if ( args [ '_' ] . length == 1 && args [ '_' ] [ 0 ] == 'install' ) {
2021-01-25 02:19:29 +03:00
diag = diagnosticAgent _installCheck ( true ) ;
2021-01-25 08:51:22 +03:00
if ( diag ) {
2021-01-25 02:19:29 +03:00
response = 'Diagnostic agent was installed at: ' + diag . appLocation ( ) ;
2019-06-15 02:33:53 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
response = 'Diagnostic agent installation failed' ;
2019-06-15 02:33:53 +03:00
}
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
response = 'Diagnostic Agent Not installed. To install: diagnostic install' ;
}
}
if ( diag ) { diag . close ( ) ; diag = null ; }
break ;
}
case 'amtevents' : {
2021-07-30 23:23:36 +03:00
if ( ( args [ '_' ] . length == 1 ) && ( args [ '_' ] [ 0 ] == 'on' ) ) { obj . showamtevent = true ; response = 'Intel AMT configuration events live view enabled.' ; }
else if ( ( args [ '_' ] . length == 1 ) && ( args [ '_' ] [ 0 ] == 'off' ) ) { delete obj . showamtevent ; response = 'Intel AMT configuration events live view disabled.' ; }
else if ( obj . amtevents == null ) { response = 'No events.' ; } else { response = obj . amtevents . join ( '\r\n' ) ; }
2021-01-25 02:19:29 +03:00
break ;
}
case 'amtconfig' : {
2021-07-30 23:23:36 +03:00
if ( amt == null ) { response = 'Intel AMT not detected.' ; break ; }
if ( ! obj . showamtevent ) { obj . showamtevent = true ; require ( 'MeshAgent' ) . SendCommand ( { action : 'msg' , type : 'console' , value : 'Enabled live view of Intel AMT configuration events, \"amtevents off\" to disable.' } ) ; }
2022-04-12 01:05:23 +03:00
if ( apftunnel != null ) { response = 'Intel AMT server tunnel already active' ; break ; }
require ( 'MeshAgent' ) . SendCommand ( { action : 'amtconfig' } ) ; // Request that the server give us a server authentication cookie to start the APF session.
2021-01-25 02:19:29 +03:00
break ;
}
case 'apf' : {
2021-01-25 08:51:22 +03:00
if ( meshCoreObj . intelamt !== null ) {
if ( args [ '_' ] . length == 1 ) {
2021-01-25 02:19:29 +03:00
var connType = - 1 , connTypeStr = args [ '_' ] [ 0 ] . toLowerCase ( ) ;
if ( connTypeStr == 'lms' ) { connType = 2 ; }
if ( connTypeStr == 'relay' ) { connType = 1 ; }
if ( connTypeStr == 'cira' ) { connType = 0 ; }
if ( connTypeStr == 'off' ) { connType = - 2 ; }
2021-01-25 08:51:22 +03:00
if ( connType >= 0 ) { // Connect
2020-10-21 04:14:00 +03:00
var apfarg = {
mpsurl : mesh . ServerUrl . replace ( 'agent.ashx' , 'apf.ashx' ) ,
2021-07-30 01:51:40 +03:00
mpsuser : Buffer . from ( mesh . ServerInfo . MeshID , 'hex' ) . toString ( 'base64' ) . substring ( 0 , 16 ) . replace ( /\+/g , '@' ) . replace ( /\//g , '$' ) ,
mpspass : Buffer . from ( mesh . ServerInfo . MeshID , 'hex' ) . toString ( 'base64' ) . substring ( 0 , 16 ) . replace ( /\+/g , '@' ) . replace ( /\//g , '$' ) ,
2020-10-21 04:14:00 +03:00
mpskeepalive : 60000 ,
2021-01-25 02:19:29 +03:00
clientname : require ( 'os' ) . hostname ( ) ,
2020-10-21 04:14:00 +03:00
clientaddress : '127.0.0.1' ,
2021-08-18 19:59:38 +03:00
clientuuid : meshCoreObj . intelamt . UUID ,
2021-01-25 02:19:29 +03:00
conntype : connType // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
2020-10-21 04:14:00 +03:00
} ;
2021-01-25 08:51:22 +03:00
if ( ( apfarg . clientuuid == null ) || ( apfarg . clientuuid . length != 36 ) ) {
2021-01-25 02:19:29 +03:00
response = "Unable to get Intel AMT UUID: " + apfarg . clientuuid ;
2021-01-25 08:51:22 +03:00
} else {
2020-10-28 09:55:00 +03:00
apftunnel = require ( 'amt-apfclient' ) ( { debug : false } , apfarg ) ;
2021-03-06 04:45:17 +03:00
apftunnel . onJsonControl = handleApfJsonControl ;
2021-01-25 02:19:29 +03:00
apftunnel . onChannelClosed = function ( ) { apftunnel = null ; }
2021-01-25 08:51:22 +03:00
try {
2020-10-21 04:14:00 +03:00
apftunnel . connect ( ) ;
2021-01-25 02:19:29 +03:00
response = "Started APF tunnel" ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
response = JSON . stringify ( ex ) ;
2019-09-17 23:11:22 +03:00
}
}
2021-01-25 08:51:22 +03:00
} else if ( connType == - 2 ) { // Disconnect
try {
2021-01-25 02:19:29 +03:00
apftunnel . disconnect ( ) ;
response = "Stopped APF tunnel" ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
response = JSON . stringify ( ex ) ;
2021-01-25 02:19:29 +03:00
}
apftunnel = null ;
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = "Invalid command.\r\nUse: apf lms|relay|cira|off" ;
2019-09-17 23:11:22 +03:00
}
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = "APF tunnel is " + ( apftunnel == null ? "off" : "on" ) + "\r\nUse: apf lms|relay|cira|off" ;
2019-09-17 23:11:22 +03:00
}
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = "APF tunnel requires Intel AMT" ;
2019-09-17 23:11:22 +03:00
}
2021-01-25 02:19:29 +03:00
break ;
}
case 'plugin' : {
2021-01-25 08:51:22 +03:00
if ( typeof args [ '_' ] [ 0 ] == 'string' ) {
try {
2021-01-25 02:19:29 +03:00
// Pass off the action to the plugin
// for plugin creators, you'll want to have a plugindir/modules_meshcore/plugin.js
// to control the output / actions here.
response = require ( args [ '_' ] [ 0 ] ) . consoleaction ( args , rights , sessionid , mesh ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
response = "There was an error in the plugin (" + ex + ")" ;
2019-10-10 21:13:25 +03:00
}
2021-01-25 08:51:22 +03:00
} else {
2021-01-25 02:19:29 +03:00
response = "Proper usage: plugin [pluginName] [args]." ;
2017-08-28 19:27:45 +03:00
}
2021-01-19 06:04:53 +03:00
break ;
}
2023-11-09 21:20:32 +03:00
case 'installedapps' : {
if ( process . platform == 'win32' ) {
require ( 'win-info' ) . installedApps ( ) . then ( function ( apps ) { sendConsoleText ( JSON . stringify ( apps , null , 1 ) ) ; } ) ;
}
break ;
}
2021-01-25 02:19:29 +03:00
default : { // This is an unknown command, return an error message
2022-12-16 17:03:45 +03:00
response = "Unknown command \"" + cmd + "\", type \"help\" for list of available commands." ;
2021-01-25 02:19:29 +03:00
break ;
2021-01-19 06:04:53 +03:00
}
}
2022-01-17 05:28:26 +03:00
} catch ( ex ) { response = "Command returned an exception error: " + ex ; console . log ( ex ) ; }
2021-01-25 02:19:29 +03:00
if ( response != null ) { sendConsoleText ( response , sessionid ) ; }
}
2021-01-19 06:04:53 +03:00
2021-01-25 02:19:29 +03:00
// Send a mesh agent console command
2021-01-25 08:51:22 +03:00
function sendConsoleText ( text , sessionid ) {
2021-01-25 02:19:29 +03:00
if ( typeof text == 'object' ) { text = JSON . stringify ( text ) ; }
if ( debugConsole && ( ( sessionid == null ) || ( sessionid == 'pipe' ) ) ) { broadcastToRegisteredApps ( { cmd : 'console' , value : text } ) ; }
if ( sessionid != 'pipe' ) { require ( 'MeshAgent' ) . SendCommand ( { action : 'msg' , type : 'console' , value : text , sessionid : sessionid } ) ; }
}
2021-01-19 06:04:53 +03:00
2021-07-24 19:28:41 +03:00
function removeAgentMessage ( msgid ) {
2021-06-08 10:11:17 +03:00
var ret = false ;
2021-07-24 19:28:41 +03:00
if ( msgid == null ) {
2021-06-08 10:11:17 +03:00
// Delete all messages
sendAgentMessage . messages = [ ] ;
ret = true ;
}
2021-07-24 19:28:41 +03:00
else {
2021-06-08 10:11:17 +03:00
var i = sendAgentMessage . messages . findIndex ( function ( v ) { return ( v . id == msgid ) ; } ) ;
2021-07-24 19:28:41 +03:00
if ( i >= 0 ) {
2021-06-08 10:11:17 +03:00
sendAgentMessage . messages . splice ( i , 1 ) ;
ret = true ;
}
}
if ( ret ) { sendAgentMessage ( ) ; }
return ( ret ) ;
}
2021-01-25 02:19:29 +03:00
// Send a mesh agent message to server, placing a bubble/badge on the agent device
2021-07-24 19:28:41 +03:00
function sendAgentMessage ( msg , icon , serverid , first ) {
if ( sendAgentMessage . messages == null ) {
2021-06-08 10:11:17 +03:00
sendAgentMessage . messages = [ ] ;
}
2021-07-24 19:28:41 +03:00
if ( arguments . length > 0 ) {
if ( first == null || ( serverid && first && sendAgentMessage . messages . findIndex ( function ( v ) { return ( v . msgid == serverid ) ; } ) < 0 ) ) {
2021-06-08 10:11:17 +03:00
sendAgentMessage . messages . push ( { msg : msg , icon : icon , msgid : serverid } ) ;
sendAgentMessage . messages . peek ( ) . id = sendAgentMessage . messages . peek ( ) . _hashCode ( ) ;
}
}
var p = { } , i ;
2021-07-24 19:28:41 +03:00
for ( i = 0 ; i < sendAgentMessage . messages . length ; ++ i ) {
2021-06-08 10:11:17 +03:00
p [ i ] = sendAgentMessage . messages [ i ] ;
}
2021-07-24 19:28:41 +03:00
try {
2021-06-08 10:11:17 +03:00
require ( 'MeshAgent' ) . SendCommand ( { action : 'sessions' , type : 'msg' , value : p } ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-06-08 10:11:17 +03:00
return ( arguments . length > 0 ? sendAgentMessage . messages . peek ( ) . id : sendAgentMessage . messages ) ;
2021-01-25 02:19:29 +03:00
}
2021-04-24 20:53:13 +03:00
function getOpenDescriptors ( ) {
2021-07-30 23:23:36 +03:00
var r = [ ] ;
2021-04-24 20:53:13 +03:00
switch ( process . platform ) {
2021-07-30 23:23:36 +03:00
case "freebsd" : {
2021-01-27 11:43:58 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( c ) { } ) ;
child . stdin . write ( "procstat -f " + process . pid + " | tr '\\n' '`' | awk -F'`' '" ) ;
child . stdin . write ( '{' ) ;
child . stdin . write ( ' DEL="";' ) ;
child . stdin . write ( ' printf "[";' ) ;
child . stdin . write ( ' for(i=1;i<NF;++i)' ) ;
child . stdin . write ( ' {' ) ;
child . stdin . write ( ' A=split($i,B," ");' ) ;
child . stdin . write ( ' if(B[3] ~ /^[0-9]/)' ) ;
child . stdin . write ( ' {' ) ;
child . stdin . write ( ' printf "%s%s", DEL, B[3];' ) ;
child . stdin . write ( ' DEL=",";' ) ;
child . stdin . write ( ' }' ) ;
child . stdin . write ( ' }' ) ;
child . stdin . write ( ' printf "]";' ) ;
child . stdin . write ( "}'" ) ;
child . stdin . write ( '\nexit\n' ) ;
child . waitExit ( ) ;
2021-07-30 23:23:36 +03:00
try { r = JSON . parse ( child . stdout . str . trim ( ) ) ; } catch ( ex ) { }
2021-01-27 11:43:58 +03:00
break ;
2021-07-30 23:23:36 +03:00
}
case "linux" : {
2021-01-27 11:43:58 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . on ( 'data' , function ( c ) { } ) ;
child . stdin . write ( "ls /proc/" + process . pid + "/fd | tr '\\n' '`' | awk -F'`' '" ) ;
child . stdin . write ( '{' ) ;
child . stdin . write ( ' printf "[";' ) ;
child . stdin . write ( ' DEL="";' ) ;
child . stdin . write ( ' for(i=1;i<NF;++i)' ) ;
child . stdin . write ( ' {' ) ;
child . stdin . write ( ' printf "%s%s",DEL,$i;' ) ;
child . stdin . write ( ' DEL=",";' ) ;
child . stdin . write ( ' }' ) ;
child . stdin . write ( ' printf "]";' ) ;
child . stdin . write ( "}'" ) ;
child . stdin . write ( '\nexit\n' ) ;
child . waitExit ( ) ;
2021-07-30 23:23:36 +03:00
try { r = JSON . parse ( child . stdout . str . trim ( ) ) ; } catch ( ex ) { }
2021-01-27 11:43:58 +03:00
break ;
2021-07-30 23:23:36 +03:00
}
2021-01-27 11:43:58 +03:00
}
2021-07-30 23:23:36 +03:00
return r ;
2021-01-27 11:43:58 +03:00
}
2021-04-24 20:53:13 +03:00
function closeDescriptors ( libc , descriptors ) {
2021-01-27 11:43:58 +03:00
var fd = null ;
2021-04-24 20:53:13 +03:00
while ( descriptors . length > 0 ) {
2021-01-27 11:43:58 +03:00
fd = descriptors . pop ( ) ;
2021-04-24 20:53:13 +03:00
if ( fd > 2 ) {
2021-01-27 11:43:58 +03:00
libc . close ( fd ) ;
}
}
}
2021-04-24 20:53:13 +03:00
function linux _execv ( name , agentfilename , sessionid ) {
2021-01-25 02:19:29 +03:00
var libs = require ( 'monitor-info' ) . getLibInfo ( 'libc' ) ;
var libc = null ;
2021-01-19 06:04:53 +03:00
2021-04-24 20:53:13 +03:00
if ( ( libs . length == 0 || libs . length == null ) && require ( 'MeshAgent' ) . ARCHID == 33 ) {
2021-01-29 08:32:59 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . str = '' ; child . stderr . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stdin . write ( "ls /lib/libc.* | tr '\\n' '`' | awk -F'`' '{ " + ' printf "["; DEL=""; for(i=1;i<NF;++i) { printf "%s{\\"path\\":\\"%s\\"}",DEL,$i; DEL=""; } printf "]"; }\'\nexit\n' ) ;
child . waitExit ( ) ;
2021-04-24 20:53:13 +03:00
try {
2021-01-29 08:32:59 +03:00
libs = JSON . parse ( child . stdout . str . trim ( ) ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2021-01-29 08:32:59 +03:00
}
2021-04-24 20:53:13 +03:00
while ( libs . length > 0 ) {
try {
2021-01-25 02:19:29 +03:00
libc = require ( '_GenericMarshal' ) . CreateNativeProxy ( libs . pop ( ) . path ) ;
break ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
libc = null ;
continue ;
2021-01-19 06:04:53 +03:00
}
}
2021-01-25 08:51:22 +03:00
if ( libc != null ) {
try {
2021-01-19 06:04:53 +03:00
libc . CreateMethod ( 'execv' ) ;
2021-01-27 11:43:58 +03:00
libc . CreateMethod ( 'close' ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
libc = null ;
2021-01-19 06:04:53 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-01-19 06:04:53 +03:00
2021-01-25 08:51:22 +03:00
if ( libc == null ) {
2021-01-25 02:19:29 +03:00
// Couldn't find libc.so, fallback to using service manager to restart agent
if ( sessionid != null ) { sendConsoleText ( 'Restarting service via service-manager...' , sessionid ) }
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
// restart service
var s = require ( 'service-manager' ) . manager . getService ( name ) ;
s . restart ( ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update encountered an error trying to restart service' , sessionid ) ;
sendAgentMessage ( 'Self Update encountered an error trying to restart service' , 3 ) ;
2021-01-19 06:04:53 +03:00
}
2021-01-25 02:19:29 +03:00
return ;
2021-01-19 06:04:53 +03:00
}
2021-01-25 02:19:29 +03:00
if ( sessionid != null ) { sendConsoleText ( 'Restarting service via execv()...' , sessionid ) }
var i ;
var args ;
2021-01-27 11:43:58 +03:00
var argarr = [ process . execPath ] ;
2021-02-03 09:32:09 +03:00
var argtmp = [ ] ;
2021-01-25 02:19:29 +03:00
var path = require ( '_GenericMarshal' ) . CreateVariable ( process . execPath ) ;
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . getStartupOptions != null ) {
2021-01-25 02:19:29 +03:00
var options = require ( 'MeshAgent' ) . getStartupOptions ( ) ;
2021-01-25 08:51:22 +03:00
for ( i in options ) {
2021-01-25 02:19:29 +03:00
argarr . push ( '--' + i + '="' + options [ i ] + '"' ) ;
2021-01-16 13:00:40 +03:00
}
2021-01-25 02:19:29 +03:00
}
args = require ( '_GenericMarshal' ) . CreateVariable ( ( 1 + argarr . length ) * require ( '_GenericMarshal' ) . PointerSize ) ;
2021-01-25 08:51:22 +03:00
for ( i = 0 ; i < argarr . length ; ++ i ) {
2021-01-25 02:19:29 +03:00
var arg = require ( '_GenericMarshal' ) . CreateVariable ( argarr [ i ] ) ;
2021-02-03 09:32:09 +03:00
argtmp . push ( arg ) ;
2021-01-25 02:19:29 +03:00
arg . pointerBuffer ( ) . copy ( args . toBuffer ( ) , i * require ( '_GenericMarshal' ) . PointerSize ) ;
}
2021-01-27 11:43:58 +03:00
var descriptors = getOpenDescriptors ( ) ;
closeDescriptors ( libc , descriptors ) ;
2021-01-25 02:19:29 +03:00
libc . execv ( path , args ) ;
if ( sessionid != null ) { sendConsoleText ( 'Self Update failed because execv() failed' , sessionid ) }
sendAgentMessage ( 'Self Update failed because execv() failed' , 3 ) ;
}
2021-01-16 13:00:40 +03:00
2021-01-25 08:51:22 +03:00
function bsd _execv ( name , agentfilename , sessionid ) {
2021-01-25 02:19:29 +03:00
var child = require ( 'child_process' ) . execFile ( '/bin/sh' , [ 'sh' ] ) ;
child . stdout . str = '' ; child . stdout . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stderr . str = '' ; child . stderr . on ( 'data' , function ( c ) { this . str += c . toString ( ) ; } ) ;
child . stdin . write ( "cat /usr/lib/libc.so | awk '" ) ;
child . stdin . write ( '{' ) ;
child . stdin . write ( ' a=split($0, tok, "(");' ) ;
child . stdin . write ( ' if(a>1)' ) ;
child . stdin . write ( ' {' ) ;
child . stdin . write ( ' split(tok[2], b, ")");' ) ;
child . stdin . write ( ' split(b[1], c, " ");' ) ;
child . stdin . write ( ' print c[1];' ) ;
child . stdin . write ( ' }' ) ;
child . stdin . write ( "}'\nexit\n" ) ;
child . waitExit ( ) ;
2021-01-25 08:51:22 +03:00
if ( child . stdout . str . trim ( ) == '' ) {
2021-01-25 02:19:29 +03:00
if ( sessionid != null ) { sendConsoleText ( 'Self Update failed because cannot find libc.so' , sessionid ) }
sendAgentMessage ( 'Self Update failed because cannot find libc.so' , 3 ) ;
return ;
}
2021-01-16 13:00:40 +03:00
2021-01-25 02:19:29 +03:00
var libc = null ;
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
libc = require ( '_GenericMarshal' ) . CreateNativeProxy ( child . stdout . str . trim ( ) ) ;
libc . CreateMethod ( 'execv' ) ;
2021-01-27 11:43:58 +03:00
libc . CreateMethod ( 'close' ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
if ( sessionid != null ) { sendConsoleText ( 'Self Update failed: ' + ex . toString ( ) , sessionid ) }
sendAgentMessage ( 'Self Update failed: ' + ex . toString ( ) , 3 ) ;
2021-01-25 02:19:29 +03:00
return ;
}
2021-01-16 13:00:40 +03:00
2021-01-25 02:19:29 +03:00
var path = require ( '_GenericMarshal' ) . CreateVariable ( process . execPath ) ;
2021-01-27 11:43:58 +03:00
var argarr = [ process . execPath ] ;
2022-01-17 05:28:26 +03:00
var args , i , argtmp = [ ] ;
2021-01-25 02:19:29 +03:00
var options = require ( 'MeshAgent' ) . getStartupOptions ( ) ;
2021-01-25 08:51:22 +03:00
for ( i in options ) {
2021-01-25 02:19:29 +03:00
argarr . push ( '--' + i + '="' + options [ i ] + '"' ) ;
}
args = require ( '_GenericMarshal' ) . CreateVariable ( ( 1 + argarr . length ) * require ( '_GenericMarshal' ) . PointerSize ) ;
2021-01-25 08:51:22 +03:00
for ( i = 0 ; i < argarr . length ; ++ i ) {
2021-01-25 02:19:29 +03:00
var arg = require ( '_GenericMarshal' ) . CreateVariable ( argarr [ i ] ) ;
2021-02-03 09:32:09 +03:00
argtmp . push ( arg ) ;
2021-01-25 02:19:29 +03:00
arg . pointerBuffer ( ) . copy ( args . toBuffer ( ) , i * require ( '_GenericMarshal' ) . PointerSize ) ;
2021-01-16 13:00:40 +03:00
}
2021-01-27 08:53:20 +03:00
if ( sessionid != null ) { sendConsoleText ( 'Restarting service via execv()' , sessionid ) }
2021-01-27 11:43:58 +03:00
var descriptors = getOpenDescriptors ( ) ;
closeDescriptors ( libc , descriptors ) ;
2021-01-25 02:19:29 +03:00
libc . execv ( path , args ) ;
if ( sessionid != null ) { sendConsoleText ( 'Self Update failed because execv() failed' , sessionid ) }
sendAgentMessage ( 'Self Update failed because execv() failed' , 3 ) ;
}
2021-04-24 20:53:13 +03:00
function windows _execve ( name , agentfilename , sessionid ) {
2021-01-25 02:19:29 +03:00
var libc ;
2021-04-24 20:53:13 +03:00
try {
2021-01-25 02:19:29 +03:00
libc = require ( '_GenericMarshal' ) . CreateNativeProxy ( 'msvcrt.dll' ) ;
libc . CreateMethod ( '_wexecve' ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update failed because msvcrt.dll is missing' , sessionid ) ;
sendAgentMessage ( 'Self Update failed because msvcrt.dll is missing' , 3 ) ;
return ;
}
var cmd = require ( '_GenericMarshal' ) . CreateVariable ( process . env [ 'windir' ] + '\\system32\\cmd.exe' , { wide : true } ) ;
var args = require ( '_GenericMarshal' ) . CreateVariable ( 3 * require ( '_GenericMarshal' ) . PointerSize ) ;
var arg1 = require ( '_GenericMarshal' ) . CreateVariable ( 'cmd.exe' , { wide : true } ) ;
2021-11-17 00:31:00 +03:00
var arg2 = require ( '_GenericMarshal' ) . CreateVariable ( '/C wmic service "' + name + '" call stopservice & "' + process . cwd ( ) + agentfilename + '.update.exe" -b64exec ' + 'dHJ5CnsKICAgIHZhciBzZXJ2aWNlTG9jYXRpb24gPSBwcm9jZXNzLmFyZ3YucG9wKCkudG9Mb3dlckNhc2UoKTsKICAgIHJlcXVpcmUoJ3Byb2Nlc3MtbWFuYWdlcicpLmVudW1lcmF0ZVByb2Nlc3NlcygpLnRoZW4oZnVuY3Rpb24gKHByb2MpCiAgICB7CiAgICAgICAgZm9yICh2YXIgcCBpbiBwcm9jKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHByb2NbcF0ucGF0aCAmJiAocHJvY1twXS5wYXRoLnRvTG93ZXJDYXNlKCkgPT0gc2VydmljZUxvY2F0aW9uKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcHJvY2Vzcy5raWxsKHByb2NbcF0ucGlkKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBwcm9jZXNzLmV4aXQoKTsKICAgIH0pOwp9CmNhdGNoIChlKQp7CiAgICBwcm9jZXNzLmV4aXQoKTsKfQ==' +
2021-02-06 01:45:43 +03:00
' "' + process . execPath + '" & copy "' + process . cwd ( ) + agentfilename + '.update.exe" "' + process . execPath + '" & wmic service "' + name + '" call startservice & erase "' + process . cwd ( ) + agentfilename + '.update.exe"' , { wide : true } ) ;
2021-01-25 02:19:29 +03:00
arg1 . pointerBuffer ( ) . copy ( args . toBuffer ( ) ) ;
arg2 . pointerBuffer ( ) . copy ( args . toBuffer ( ) , require ( '_GenericMarshal' ) . PointerSize ) ;
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
libc . _wexecve ( cmd , args , 0 ) ;
}
// Start a JavaScript based Agent Self-Update
2021-01-25 08:51:22 +03:00
function agentUpdate _Start ( updateurl , updateoptions ) {
2021-01-25 02:19:29 +03:00
// If this value is null
var sessionid = ( updateoptions != null ) ? updateoptions . sessionid : null ; // If this is null, messages will be broadcast. Otherwise they will be unicasted
2021-01-29 02:49:42 +03:00
// If the url starts with *, switch it to use the same protoco, host and port as the control channel.
2021-03-05 03:27:43 +03:00
if ( updateurl != null ) {
updateurl = getServerTargetUrlEx ( updateurl ) ;
if ( updateurl . startsWith ( "wss://" ) ) { updateurl = "https://" + updateurl . substring ( 6 ) ; }
}
2021-01-29 02:49:42 +03:00
2021-01-25 08:51:22 +03:00
if ( agentUpdate _Start . _selfupdate != null ) {
2021-01-25 02:19:29 +03:00
// We were already called, so we will ignore this duplicate request
if ( sessionid != null ) { sendConsoleText ( 'Self update already in progress...' , sessionid ) ; }
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
if ( agentUpdate _Start . _retryCount == null ) { agentUpdate _Start . _retryCount = 0 ; }
2021-01-25 08:51:22 +03:00
if ( require ( 'MeshAgent' ) . ARCHID == null && updateurl == null ) {
2021-01-25 02:19:29 +03:00
// This agent doesn't have the ability to tell us which ARCHID it is, so we don't know which agent to pull
sendConsoleText ( 'Unable to initiate update, agent ARCHID is not defined' , sessionid ) ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var agentfilename = process . execPath . split ( process . platform == 'win32' ? '\\' : '/' ) . pop ( ) ; // Local File Name, ie: MeshAgent.exe
var name = require ( 'MeshAgent' ) . serviceName ;
2021-01-29 02:49:42 +03:00
if ( name == null ) { name = ( process . platform == 'win32' ? 'Mesh Agent' : 'meshagent' ) ; } // This is an older agent that doesn't expose the service name, so use the default
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
var s = require ( 'service-manager' ) . manager . getService ( name ) ;
2021-01-25 08:51:22 +03:00
if ( ! s . isMe ( ) ) {
2021-01-15 10:57:52 +03:00
if ( process . platform == 'win32' ) { s . close ( ) ; }
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update cannot continue, this agent is not an instance of (' + name + ')' , sessionid ) ;
2021-01-15 10:57:52 +03:00
return ;
}
2021-01-25 02:19:29 +03:00
if ( process . platform == 'win32' ) { s . close ( ) ; }
}
2022-01-17 05:28:26 +03:00
catch ( ex ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update Failed because this agent is not an instance of (' + name + ')' , sessionid ) ;
sendAgentMessage ( 'Self Update Failed because this agent is not an instance of (' + name + ')' , 3 ) ;
return ;
}
2021-01-15 10:57:52 +03:00
2021-03-05 03:27:43 +03:00
if ( ( sessionid != null ) && ( updateurl != null ) ) { sendConsoleText ( 'Downloading update from: ' + updateurl , sessionid ) ; }
2021-01-25 02:19:29 +03:00
var options = require ( 'http' ) . parseUri ( updateurl != null ? updateurl : require ( 'MeshAgent' ) . ServerUrl ) ;
options . protocol = 'https:' ;
2021-03-05 03:27:43 +03:00
if ( updateurl == null ) { options . path = ( '/meshagents?id=' + require ( 'MeshAgent' ) . ARCHID ) ; sendConsoleText ( 'Downloading update from: ' + options . path , sessionid ) ; }
2021-01-25 02:19:29 +03:00
options . rejectUnauthorized = false ;
2021-01-25 08:51:22 +03:00
options . checkServerIdentity = function checkServerIdentity ( certs ) {
2021-01-25 02:19:29 +03:00
// If the tunnel certificate matches the control channel certificate, accept the connection
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . digest == certs [ 0 ] . digest ) return ; } catch ( ex ) { }
try { if ( require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate . fingerprint == certs [ 0 ] . fingerprint ) return ; } catch ( ex ) { }
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
// Check that the certificate is the one expected by the server, fail if not.
2021-01-25 08:51:22 +03:00
if ( checkServerIdentity . servertlshash == null ) {
2021-01-25 02:19:29 +03:00
if ( require ( 'MeshAgent' ) . ServerInfo == null || require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate == null ) { return ; }
2021-01-29 02:49:42 +03:00
sendConsoleText ( 'Self Update failed, because the url cannot be verified: ' + updateurl , sessionid ) ;
sendAgentMessage ( 'Self Update failed, because the url cannot be verified: ' + updateurl , 3 ) ;
2021-01-25 02:19:29 +03:00
throw new Error ( 'BadCert' ) ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 02:19:29 +03:00
if ( certs [ 0 ] . digest == null ) { return ; }
2021-01-25 08:51:22 +03:00
if ( ( checkServerIdentity . servertlshash != null ) && ( checkServerIdentity . servertlshash . toLowerCase ( ) != certs [ 0 ] . digest . split ( ':' ) . join ( '' ) . toLowerCase ( ) ) ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update failed, because the supplied certificate does not match' , sessionid ) ;
sendAgentMessage ( 'Self Update failed, because the supplied certificate does not match' , 3 ) ;
throw new Error ( 'BadCert' )
}
}
options . checkServerIdentity . servertlshash = ( updateoptions != null ? updateoptions . tlshash : null ) ;
agentUpdate _Start . _selfupdate = require ( 'https' ) . get ( options ) ;
2021-01-25 08:51:22 +03:00
agentUpdate _Start . _selfupdate . on ( 'error' , function ( e ) {
2021-01-29 02:49:42 +03:00
sendConsoleText ( 'Self Update failed, because there was a problem trying to download the update from ' + updateurl , sessionid ) ;
sendAgentMessage ( 'Self Update failed, because there was a problem trying to download the update from ' + updateurl , 3 ) ;
2021-01-29 00:41:19 +03:00
agentUpdate _Start . _selfupdate = null ;
2021-01-25 02:19:29 +03:00
} ) ;
2021-01-25 08:51:22 +03:00
agentUpdate _Start . _selfupdate . on ( 'response' , function ( img ) {
2021-02-09 10:28:49 +03:00
this . _file = require ( 'fs' ) . createWriteStream ( agentfilename + ( process . platform == 'win32' ? '.update.exe' : '.update' ) , { flags : 'wb' } ) ;
2021-01-25 02:19:29 +03:00
this . _filehash = require ( 'SHA384Stream' ) . create ( ) ;
2021-01-25 08:51:22 +03:00
this . _filehash . on ( 'hash' , function ( h ) {
if ( updateoptions != null && updateoptions . hash != null ) {
if ( updateoptions . hash . toLowerCase ( ) == h . toString ( 'hex' ) . toLowerCase ( ) ) {
2021-01-25 02:19:29 +03:00
if ( sessionid != null ) { sendConsoleText ( 'Download complete. HASH verified.' , sessionid ) ; }
2021-08-17 09:58:34 +03:00
} else {
2021-01-25 02:19:29 +03:00
agentUpdate _Start . _retryCount ++ ;
2021-01-29 02:49:42 +03:00
sendConsoleText ( 'Self Update FAILED because the downloaded agent FAILED hash check (' + agentUpdate _Start . _retryCount + '), URL: ' + updateurl , sessionid ) ;
2021-08-17 09:58:34 +03:00
sendConsoleText ( updateoptions . hash + " != " + h . toString ( 'hex' ) ) ;
2021-01-29 02:49:42 +03:00
sendAgentMessage ( 'Self Update FAILED because the downloaded agent FAILED hash check (' + agentUpdate _Start . _retryCount + '), URL: ' + updateurl , 3 ) ;
2021-01-25 02:19:29 +03:00
agentUpdate _Start . _selfupdate = null ;
2021-01-25 08:51:22 +03:00
if ( agentUpdate _Start . _retryCount < 4 ) {
2021-01-25 02:19:29 +03:00
// Retry the download again
sendConsoleText ( 'Self Update will try again in 60 seconds...' , sessionid ) ;
agentUpdate _Start . _timeout = setTimeout ( agentUpdate _Start , 60000 , updateurl , updateoptions ) ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update giving up, too many failures...' , sessionid ) ;
sendAgentMessage ( 'Self Update giving up, too many failures...' , 3 ) ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 02:19:29 +03:00
return ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Download complete. HASH=' + h . toString ( 'hex' ) , sessionid ) ;
}
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
// Send an indication to the server that we got the update download correctly.
2022-01-17 05:28:26 +03:00
try { require ( 'MeshAgent' ) . SendCommand ( { action : 'agentupdatedownloaded' } ) ; } catch ( ex ) { }
2021-01-17 10:35:39 +03:00
2021-01-25 02:19:29 +03:00
if ( sessionid != null ) { sendConsoleText ( 'Updating and restarting agent...' , sessionid ) ; }
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
2021-01-25 02:19:29 +03:00
// Use _wexecve() equivalent to perform the update
windows _execve ( name , agentfilename , sessionid ) ;
}
2021-01-25 08:51:22 +03:00
else {
2021-01-25 02:19:29 +03:00
var m = require ( 'fs' ) . statSync ( process . execPath ) . mode ;
require ( 'fs' ) . chmodSync ( process . cwd ( ) + agentfilename + '.update' , m ) ;
2021-01-19 06:04:53 +03:00
2021-01-25 02:19:29 +03:00
// remove binary
require ( 'fs' ) . unlinkSync ( process . execPath ) ;
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
// copy update
require ( 'fs' ) . copyFileSync ( process . cwd ( ) + agentfilename + '.update' , process . execPath ) ;
require ( 'fs' ) . chmodSync ( process . execPath , m ) ;
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
// erase update
require ( 'fs' ) . unlinkSync ( process . cwd ( ) + agentfilename + '.update' ) ;
2021-01-15 10:57:52 +03:00
2021-01-25 08:51:22 +03:00
switch ( process . platform ) {
2021-01-25 02:19:29 +03:00
case 'freebsd' :
bsd _execv ( name , agentfilename , sessionid ) ;
break ;
case 'linux' :
linux _execv ( name , agentfilename , sessionid ) ;
break ;
default :
2021-01-25 08:51:22 +03:00
try {
2021-01-25 02:19:29 +03:00
// restart service
var s = require ( 'service-manager' ) . manager . getService ( name ) ;
s . restart ( ) ;
}
2022-01-17 05:28:26 +03:00
catch ( ex ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( 'Self Update encountered an error trying to restart service' , sessionid ) ;
sendAgentMessage ( 'Self Update encountered an error trying to restart service' , 3 ) ;
}
break ;
2021-01-15 10:57:52 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-01-15 10:57:52 +03:00
} ) ;
2021-01-25 02:19:29 +03:00
img . pipe ( this . _file ) ;
img . pipe ( this . _filehash ) ;
} ) ;
2021-01-15 10:57:52 +03:00
}
}
2021-01-25 02:19:29 +03:00
}
2021-01-15 10:57:52 +03:00
2021-01-25 02:19:29 +03:00
// Called before the process exits
//process.exit = function (code) { console.log("Exit with code: " + code.toString()); }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Called when the server connection state changes
2021-07-24 19:28:41 +03:00
function handleServerConnection ( state ) {
2021-01-25 02:19:29 +03:00
meshServerConnectionState = state ;
2021-07-24 19:28:41 +03:00
if ( meshServerConnectionState == 0 ) {
2021-01-25 02:19:29 +03:00
// Server disconnected
if ( selfInfoUpdateTimer != null ) { clearInterval ( selfInfoUpdateTimer ) ; selfInfoUpdateTimer = null ; }
lastSelfInfo = null ;
2021-07-24 19:28:41 +03:00
} else {
2021-01-25 02:19:29 +03:00
// Server connected, send mesh core information
2021-07-24 19:28:41 +03:00
if ( require ( 'MeshAgent' ) . ServerInfo == null || require ( 'MeshAgent' ) . ServerInfo . ControlChannelCertificate == null ) {
2021-06-08 10:11:17 +03:00
// Outdated Agent, will have insecure tunnels
sendAgentMessage ( "This agent has an outdated certificate validation mechanism, consider updating." , 3 , 118 ) ;
}
2021-07-24 19:28:41 +03:00
else if ( global . _MSH == null ) {
2021-06-09 23:43:11 +03:00
sendAgentMessage ( "This is an old agent version, consider updating." , 3 , 117 ) ;
2021-06-08 10:11:17 +03:00
}
2021-01-25 02:19:29 +03:00
var oldNodeId = db . Get ( 'OldNodeId' ) ;
if ( oldNodeId != null ) { mesh . SendCommand ( { action : 'mc1migration' , oldnodeid : oldNodeId } ) ; }
2020-07-31 22:56:39 +03:00
2021-01-25 02:19:29 +03:00
// Send SMBios tables if present
if ( SMBiosTablesRaw != null ) { mesh . SendCommand ( { action : 'smbios' , value : SMBiosTablesRaw } ) ; }
2020-10-01 20:30:26 +03:00
2021-01-25 02:19:29 +03:00
// Update the server on with basic info, logged in users and more advanced stuff, like Intel ME and Network Settings
meInfoStr = null ;
sendPeriodicServerUpdate ( null , true ) ;
2021-07-24 19:28:41 +03:00
if ( selfInfoUpdateTimer == null ) {
2021-01-25 02:19:29 +03:00
selfInfoUpdateTimer = setInterval ( sendPeriodicServerUpdate , 1200000 ) ; // 20 minutes
selfInfoUpdateTimer . metadata = 'meshcore (InfoUpdate Timer)' ;
2020-10-01 20:30:26 +03:00
}
2020-10-12 08:24:30 +03:00
2021-01-25 02:19:29 +03:00
// Send any state messages
2021-07-24 19:28:41 +03:00
if ( Object . keys ( tunnelUserCount . msg ) . length > 0 ) {
2021-06-08 10:11:17 +03:00
sendAgentMessage ( ) ;
2021-01-25 02:19:29 +03:00
broadcastSessionsToRegisteredApps ( ) ;
2019-05-09 02:18:44 +03:00
}
2021-01-25 02:19:29 +03:00
// Send update of registered applications to the server
updateRegisteredAppsToServer ( ) ;
2017-08-28 19:27:45 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Send server state update to registered applications
broadcastToRegisteredApps ( { cmd : 'serverstate' , value : meshServerConnectionState , url : require ( 'MeshAgent' ) . ConnectedServer } ) ;
}
// Update the server with the latest network interface information
var sendNetworkUpdateNagleTimer = null ;
function sendNetworkUpdateNagle ( ) { if ( sendNetworkUpdateNagleTimer != null ) { clearTimeout ( sendNetworkUpdateNagleTimer ) ; sendNetworkUpdateNagleTimer = null ; } sendNetworkUpdateNagleTimer = setTimeout ( sendNetworkUpdate , 5000 ) ; }
2021-01-25 08:51:22 +03:00
function sendNetworkUpdate ( force ) {
2021-01-25 02:19:29 +03:00
sendNetworkUpdateNagleTimer = null ;
2021-02-03 01:08:30 +03:00
try {
// Update the network interfaces information data
var netInfo = { netif2 : require ( 'os' ) . networkInterfaces ( ) } ;
if ( netInfo . netif2 ) {
netInfo . action = 'netinfo' ;
var netInfoStr = JSON . stringify ( netInfo ) ;
if ( ( force == true ) || ( clearGatewayMac ( netInfoStr ) != clearGatewayMac ( lastNetworkInfo ) ) ) { mesh . SendCommand ( netInfo ) ; lastNetworkInfo = netInfoStr ; }
}
} catch ( ex ) { }
2021-01-25 02:19:29 +03:00
}
2018-09-22 02:34:35 +03:00
2021-01-25 02:19:29 +03:00
// Called periodically to check if we need to send updates to the server
2021-01-25 08:51:22 +03:00
function sendPeriodicServerUpdate ( flags , force ) {
2021-01-25 02:19:29 +03:00
if ( meshServerConnectionState == 0 ) return ; // Not connected to server, do nothing.
if ( ! flags ) { flags = 0xFFFFFFFF ; }
2024-01-29 18:13:24 +03:00
if ( ! force ) { force = false ; }
2021-01-25 02:19:29 +03:00
// If we have a connected MEI, get Intel ME information
2021-01-25 08:51:22 +03:00
if ( ( flags & 1 ) && ( amt != null ) && ( amt . state == 2 ) ) {
2021-01-25 02:19:29 +03:00
delete meshCoreObj . intelamt ;
2021-01-25 08:51:22 +03:00
amt . getMeiState ( 9 , function ( meinfo ) {
2021-01-25 02:19:29 +03:00
meshCoreObj . intelamt = meinfo ;
meshCoreObj . intelamt . microlms = amt . lmsstate ;
meshCoreObjChanged ( ) ;
} ) ;
}
2018-09-22 02:34:35 +03:00
2021-01-25 02:19:29 +03:00
// Update network information
if ( flags & 2 ) { sendNetworkUpdateNagle ( false ) ; }
2019-08-02 21:35:59 +03:00
2021-01-25 02:19:29 +03:00
// Update anti-virus information
2021-01-25 08:51:22 +03:00
if ( ( flags & 4 ) && ( process . platform == 'win32' ) ) {
2021-01-25 02:19:29 +03:00
// Windows Command: "wmic /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /FORMAT:CSV"
2022-01-17 05:28:26 +03:00
try { meshCoreObj . av = require ( 'win-info' ) . av ( ) ; meshCoreObjChanged ( ) ; } catch ( ex ) { av = null ; } // Antivirus
//if (process.platform == 'win32') { try { meshCoreObj.pr = require('win-info').pendingReboot(); meshCoreObjChanged(); } catch (ex) { meshCoreObj.pr = null; } } // Pending reboot
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
if ( process . platform == 'win32' ) {
if ( require ( 'MeshAgent' ) . _securitycenter == null ) {
try {
2021-01-25 02:19:29 +03:00
require ( 'MeshAgent' ) . _securitycenter = require ( 'win-securitycenter' ) . status ( ) ;
2021-01-25 08:51:22 +03:00
meshCoreObj [ 'wsc' ] = require ( 'MeshAgent' ) . _securitycenter ; // Windows Security Central (WSC)
require ( 'win-securitycenter' ) . on ( 'changed' , function ( ) {
2021-01-21 21:01:09 +03:00
require ( 'MeshAgent' ) . _securitycenter = require ( 'win-securitycenter' ) . status ( ) ;
2021-01-25 08:51:22 +03:00
meshCoreObj [ 'wsc' ] = require ( 'MeshAgent' ) . _securitycenter ; // Windows Security Central (WSC)
require ( 'MeshAgent' ) . SendCommand ( { action : 'coreinfo' , wsc : require ( 'MeshAgent' ) . _securitycenter } ) ;
2021-01-25 02:19:29 +03:00
} ) ;
2022-01-17 05:28:26 +03:00
} catch ( ex ) { }
2019-08-02 21:35:59 +03:00
}
2024-02-18 04:51:15 +03:00
2024-01-04 05:16:41 +03:00
// Get Defender for Windows Server
try {
var d = require ( 'win-info' ) . defender ( ) ;
d . then ( function ( res ) {
meshCoreObj . defender = res ;
meshCoreObjChanged ( ) ;
} ) ;
2024-02-18 04:51:15 +03:00
} catch ( ex ) { }
2017-12-13 03:04:54 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
// Send available data right now
2021-01-25 08:51:22 +03:00
if ( force ) {
2020-11-02 11:44:07 +03:00
meshCoreObj = sortObjRec ( meshCoreObj ) ;
var x = JSON . stringify ( meshCoreObj ) ;
2021-01-25 08:51:22 +03:00
if ( x != LastPeriodicServerUpdate ) {
2021-01-25 02:19:29 +03:00
LastPeriodicServerUpdate = x ;
mesh . SendCommand ( meshCoreObj ) ;
2020-11-03 13:58:29 +03:00
}
2020-11-02 11:44:07 +03:00
}
2021-01-25 02:19:29 +03:00
}
2020-11-02 11:44:07 +03:00
2024-02-18 04:51:15 +03:00
// Sort the names in an object
function sortObject ( obj ) { return Object . keys ( obj ) . sort ( ) . reduce ( function ( a , v ) { a [ v ] = obj [ v ] ; return a ; } , { } ) ; }
// Fix the incoming data and cut down how much data we use
function cleanGetBitLockerVolumeInfo ( volumes ) {
for ( var i in volumes ) {
const v = volumes [ i ] ;
if ( typeof v . size == 'string' ) { v . size = parseInt ( v . size ) ; }
2024-02-22 20:09:28 +03:00
if ( typeof v . sizeremaining == 'string' ) { v . sizeremaining = parseInt ( v . sizeremaining ) ; }
2024-02-18 04:51:15 +03:00
if ( v . identifier == '' ) { delete v . identifier ; }
if ( v . name == '' ) { delete v . name ; }
if ( v . removable != true ) { delete v . removable ; }
2024-02-22 20:55:48 +03:00
if ( v . cdrom != true ) { delete v . cdrom ; }
2024-02-18 04:51:15 +03:00
if ( v . protectionStatus == 'On' ) { v . protectionStatus = true ; } else { delete v . protectionStatus ; }
if ( v . volumeStatus == 'FullyDecrypted' ) { delete v . volumeStatus ; }
if ( v . recoveryPassword == '' ) { delete v . recoveryPassword ; }
}
return sortObject ( volumes ) ;
}
2021-01-25 02:19:29 +03:00
// Once we are done collecting all the data, send to server if needed
var LastPeriodicServerUpdate = null ;
var PeriodicServerUpdateNagleTimer = null ;
2021-01-25 08:51:22 +03:00
function meshCoreObjChanged ( ) {
if ( PeriodicServerUpdateNagleTimer == null ) {
2021-01-25 02:19:29 +03:00
PeriodicServerUpdateNagleTimer = setTimeout ( meshCoreObjChangedEx , 500 ) ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
}
2021-01-25 08:51:22 +03:00
function meshCoreObjChangedEx ( ) {
2021-01-25 02:19:29 +03:00
PeriodicServerUpdateNagleTimer = null ;
meshCoreObj = sortObjRec ( meshCoreObj ) ;
var x = JSON . stringify ( meshCoreObj ) ;
2021-01-25 08:51:22 +03:00
if ( x != LastPeriodicServerUpdate ) {
2021-01-25 02:19:29 +03:00
try { LastPeriodicServerUpdate = x ; mesh . SendCommand ( meshCoreObj ) ; } catch ( ex ) { }
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
}
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
function sortObjRec ( o ) { if ( ( typeof o != 'object' ) || ( Array . isArray ( o ) ) ) return o ; for ( var i in o ) { if ( typeof o [ i ] == 'object' ) { o [ i ] = sortObjRec ( o [ i ] ) ; } } return sortObj ( o ) ; }
function sortObj ( o ) { return Object . keys ( o ) . sort ( ) . reduce ( function ( result , key ) { result [ key ] = o [ key ] ; return result ; } , { } ) ; }
2019-10-10 21:13:25 +03:00
2021-01-25 02:19:29 +03:00
function onWebSocketClosed ( ) { sendConsoleText ( "WebSocket #" + this . httprequest . index + " closed." , this . httprequest . sessionid ) ; delete consoleWebSockets [ this . httprequest . index ] ; }
function onWebSocketData ( data ) { sendConsoleText ( "Got WebSocket #" + this . httprequest . index + " data: " + data , this . httprequest . sessionid ) ; }
function onWebSocketSendOk ( ) { sendConsoleText ( "WebSocket #" + this . index + " SendOK." , this . sessionid ) ; }
2018-07-03 00:34:10 +03:00
2021-01-25 08:51:22 +03:00
function onWebSocketUpgrade ( response , s , head ) {
2021-01-25 02:19:29 +03:00
sendConsoleText ( "WebSocket #" + this . index + " connected." , this . sessionid ) ;
this . s = s ;
s . httprequest = this ;
s . end = onWebSocketClosed ;
s . data = onWebSocketData ;
2017-08-28 19:27:45 +03:00
}
2021-01-25 02:19:29 +03:00
mesh . AddCommandHandler ( handleServerCommand ) ;
mesh . AddConnectHandler ( handleServerConnection ) ;