2020-08-02 00:12:07 -04:00
/ *
2021-01-09 17:31:09 -05:00
Copyright 2020 - 2021 Intel Corporation
2020-08-02 00:12:07 -04: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
http : //www.apache.org/licenses/LICENSE-2.0
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 .
* /
2020-12-16 15:03:02 -05:00
Object . defineProperty ( Array . prototype , 'getParameterEx' ,
{
value : function ( name , defaultValue )
{
var i , ret ;
for ( i = 0 ; i < this . length ; ++ i )
{
if ( this [ i ] == name ) { return ( null ) ; }
if ( this [ i ] . startsWith ( name + '=' ) )
{
ret = this [ i ] . substring ( name . length + 1 ) ;
if ( ret . startsWith ( '"' ) ) { ret = ret . substring ( 1 , ret . length - 1 ) ; }
return ( ret ) ;
}
}
return ( defaultValue ) ;
}
} ) ;
Object . defineProperty ( Array . prototype , 'getParameter' ,
{
value : function ( name , defaultValue )
{
return ( this . getParameterEx ( '-' + name , defaultValue ) ) ;
}
} ) ;
2020-11-06 16:43:56 -05:00
2021-10-13 14:04:02 -04:00
// The following line just below with 'msh=' needs to stay exactly like this since MeshCentral will replace it with the correct settings.
2020-09-14 16:32:16 -04:00
var msh = { } ;
2020-12-16 15:03:02 -05:00
var translation = JSON . parse ( msh . translation ) ;
var lang = require ( 'util-language' ) . current ;
2022-06-03 17:13:42 -04:00
if ( lang == null ) { lang = 'en' ; }
if ( lang == "C" ) {
lang = 'en' ;
console . log ( "Langauge envronment variable was not set (process.env.LANG). Defaulting to English ('en').\nSee the agent-translations.json file for a list of current languages that are implemented\nUsage: meshcentral -lang=en\n" ) ;
}
2020-12-16 15:03:02 -05:00
if ( process . argv . getParameter ( 'lang' , lang ) == null )
{
console . log ( '\nCurrent Language: ' + lang + '\n' ) ;
process . exit ( ) ;
}
else
{
lang = process . argv . getParameter ( 'lang' , lang ) . toLowerCase ( ) ;
lang = lang . split ( '_' ) . join ( '-' ) ;
if ( translation [ lang ] == null )
{
if ( translation [ lang . split ( '-' ) [ 0 ] ] == null )
{
console . log ( 'Language: ' + lang + ' is not translated.' ) ;
2022-06-04 12:35:31 -04:00
console . log ( "try: './" + process . execPath . split ( '/' ) . pop ( ) + " -lang=en' for English" ) ;
2022-06-03 17:13:42 -04:00
console . log ( "See the agent-translations.json file for a list of current languages that are implemented." )
2020-12-16 15:03:02 -05:00
process . exit ( ) ;
}
else
{
lang = lang . split ( '-' ) [ 0 ] ;
}
}
}
if ( lang != 'en' )
{
for ( var i in translation [ 'en' ] )
{
// If translated entries are missing, substitute the english translation
if ( translation [ lang ] [ i ] == null ) { translation [ lang ] [ i ] = translation [ 'en' ] [ i ] ; }
}
}
2020-11-06 16:43:56 -05:00
var displayName = msh . displayName ? msh . displayName : 'MeshCentral Agent' ;
2020-12-16 15:03:02 -05:00
var s = null , buttons = [ translation [ lang ] . cancel ] , skip = false ;
2020-11-06 16:43:56 -05:00
var serviceName = msh . meshServiceName ? msh . meshServiceName : 'meshagent' ;
try { s = require ( 'service-manager' ) . manager . getService ( serviceName ) ; } catch ( e ) { }
var connectArgs = [ process . execPath . split ( '/' ) . pop ( ) , '--no-embedded=1' , '--disableUpdate=1' ] ;
connectArgs . push ( '--MeshName="' + msh . MeshName + '"' ) ;
connectArgs . push ( '--MeshType="' + msh . MeshType + '"' ) ;
connectArgs . push ( '--MeshID="' + msh . MeshID + '"' ) ;
connectArgs . push ( '--ServerID="' + msh . ServerID + '"' ) ;
connectArgs . push ( '--MeshServer="' + msh . MeshServer + '"' ) ;
connectArgs . push ( '--AgentCapabilities="0x00000020"' ) ;
if ( msh . displayName ) { connectArgs . push ( '--displayName="' + msh . displayName + '"' ) ; }
if ( msh . agentName ) { connectArgs . push ( '--agentName="' + msh . agentName + '"' ) ; }
2020-08-02 00:12:07 -04:00
2020-11-06 16:43:56 -05:00
function _install ( parms )
{
2022-09-22 15:46:00 -04:00
var i ;
2020-08-03 13:51:54 -04:00
var mstr = require ( 'fs' ) . createWriteStream ( process . execPath + '.msh' , { flags : 'wb' } ) ;
2022-09-22 15:46:00 -04:00
for ( i in msh )
{
mstr . write ( i + '=' + msh [ i ] + '\n' ) ;
}
2020-08-03 13:51:54 -04:00
mstr . end ( ) ;
2020-11-06 16:43:56 -05:00
2020-09-10 18:18:33 -04:00
if ( parms == null ) { parms = [ ] ; }
2020-11-06 16:43:56 -05:00
if ( msh . companyName ) { parms . unshift ( '--companyName="' + msh . companyName + '"' ) ; }
if ( msh . displayName ) { parms . unshift ( '--displayName="' + msh . displayName + '"' ) ; }
if ( msh . meshServiceName ) { parms . unshift ( '--meshServiceName="' + msh . meshServiceName + '"' ) ; }
2020-09-10 18:18:33 -04:00
parms . unshift ( '--copy-msh=1' ) ;
parms . unshift ( '--no-embedded=1' ) ;
parms . unshift ( '-fullinstall' ) ;
parms . unshift ( process . execPath . split ( '/' ) . pop ( ) ) ;
2020-11-06 16:43:56 -05:00
2020-09-10 18:18:33 -04:00
global . _child = require ( 'child_process' ) . execFile ( process . execPath , parms ) ;
2020-08-03 13:51:54 -04:00
global . _child . stdout . on ( 'data' , function ( c ) { process . stdout . write ( c . toString ( ) ) ; } ) ;
global . _child . stderr . on ( 'data' , function ( c ) { process . stdout . write ( c . toString ( ) ) ; } ) ;
global . _child . waitExit ( ) ;
}
2020-11-06 16:43:56 -05:00
function _uninstall ( )
{
2020-09-10 18:18:33 -04:00
global . _child = require ( 'child_process' ) . execFile ( process . execPath ,
2020-11-06 16:43:56 -05:00
[ process . execPath . split ( '/' ) . pop ( ) , '-fulluninstall' , '--no-embedded=1' , '--meshServiceName="' + serviceName + '"' ] ) ;
2020-08-03 13:51:54 -04:00
global . _child . stdout . on ( 'data' , function ( c ) { process . stdout . write ( c . toString ( ) ) ; } ) ;
global . _child . stderr . on ( 'data' , function ( c ) { process . stdout . write ( c . toString ( ) ) ; } ) ;
global . _child . waitExit ( ) ;
2020-08-02 00:12:07 -04:00
}
2020-11-06 16:43:56 -05:00
if ( msh . InstallFlags == null )
{
2020-08-03 13:51:54 -04:00
msh . InstallFlags = 3 ;
2020-11-06 16:43:56 -05:00
} else
{
2020-08-03 13:51:54 -04:00
msh . InstallFlags = parseInt ( msh . InstallFlags . toString ( ) ) ;
2020-08-02 00:12:07 -04:00
}
2020-11-06 16:43:56 -05:00
if ( process . argv . includes ( '-mesh' ) )
{
2020-09-14 18:34:55 -04:00
console . log ( JSON . stringify ( msh , null , 2 ) ) ;
process . exit ( ) ;
}
2020-12-16 15:03:02 -05:00
if ( process . argv . includes ( '-translations' ) )
{
console . log ( JSON . stringify ( translation ) ) ;
process . exit ( ) ;
}
2022-09-20 11:35:56 -04:00
if ( process . argv . includes ( '-help' ) || ( process . platform == 'linux' && process . env [ 'XAUTHORITY' ] == null && process . env [ 'DISPLAY' ] == null && process . argv . length == 1 ) )
2020-12-01 20:04:45 -05:00
{
2020-12-16 15:03:02 -05:00
console . log ( "\n" + translation [ lang ] . commands + ": " ) ;
2020-12-01 20:04:45 -05:00
if ( ( msh . InstallFlags & 1 ) == 1 )
{
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -connect' ) ;
}
if ( ( msh . InstallFlags & 2 ) == 2 )
{
if ( s )
{
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -update' ) ;
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -uninstall' ) ;
}
else
{
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -install' ) ;
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -install --installPath="/alternate/path"' ) ;
}
}
console . log ( '' ) ;
process . exit ( ) ;
}
2020-11-06 16:43:56 -05:00
if ( ( msh . InstallFlags & 1 ) == 1 )
{
2020-12-16 15:03:02 -05:00
buttons . unshift ( translation [ lang ] . connect ) ;
2020-11-06 16:43:56 -05:00
if ( process . argv . includes ( '-connect' ) )
{
global . _child = require ( 'child_process' ) . execFile ( process . execPath , connectArgs ) ;
2020-09-14 18:34:55 -04:00
global . _child . stdout . on ( 'data' , function ( c ) { } ) ;
global . _child . stderr . on ( 'data' , function ( c ) { } ) ;
global . _child . on ( 'exit' , function ( code ) { process . exit ( code ) ; } ) ;
2020-12-16 15:03:02 -05:00
console . log ( "\n" + translation [ lang ] . url + ": " + msh . MeshServer ) ;
console . log ( translation [ lang ] . group + ": " + msh . MeshName ) ;
console . log ( '\n' + translation [ lang ] . ctrlc + '\n' ) ;
2020-09-14 18:34:55 -04:00
skip = true ;
}
}
2020-11-06 16:43:56 -05:00
if ( ( ! skip ) && ( ( msh . InstallFlags & 2 ) == 2 ) )
{
if ( ! require ( 'user-sessions' ) . isRoot ( ) )
{
2020-12-16 15:03:02 -05:00
console . log ( '\n' + translation [ lang ] . elevation ) ;
console . log ( translation [ lang ] . sudo ) ;
2020-08-03 14:07:07 -04:00
process . exit ( ) ;
}
2020-11-06 16:43:56 -05:00
if ( s )
{
if ( ( process . platform == 'darwin' ) || require ( 'message-box' ) . kdialog )
{
2020-12-16 15:03:02 -05:00
buttons . unshift ( translation [ lang ] . setup ) ;
2020-11-06 16:43:56 -05:00
} else
{
2020-12-16 15:03:02 -05:00
buttons . unshift ( translation [ lang ] . uninstall ) ;
buttons . unshift ( translation [ lang ] . update ) ;
2020-08-03 13:51:54 -04:00
}
2020-11-06 16:43:56 -05:00
} else
{
2020-12-16 15:03:02 -05:00
buttons . unshift ( translation [ lang ] . install ) ;
2020-08-03 13:51:54 -04:00
}
}
2022-09-01 14:03:00 -04:00
if ( ! skip )
2020-11-06 16:43:56 -05:00
{
2022-10-24 14:33:40 -04:00
if ( process . argv . includes ( '-install' ) || process . argv . includes ( '-update' ) )
2020-11-06 16:43:56 -05:00
{
2022-10-24 14:33:40 -04:00
var p = [ ] ;
for ( var i = 0 ; i < process . argv . length ; ++ i )
2020-11-06 16:43:56 -05:00
{
2022-10-24 14:33:40 -04:00
if ( process . argv [ i ] . startsWith ( '--installPath=' ) )
2020-11-06 16:43:56 -05:00
{
2022-10-24 14:33:40 -04:00
p . push ( '--installPath="' + process . argv [ i ] . split ( '=' ) . pop ( ) + '"' ) ;
}
else if ( process . argv [ i ] . startsWith ( '--' ) )
{
p . push ( process . argv [ i ] ) ;
}
2020-09-10 18:18:33 -04:00
}
_install ( p ) ;
process . exit ( ) ;
}
2020-11-06 16:43:56 -05:00
else if ( process . argv . includes ( '-uninstall' ) )
{
2020-09-10 18:18:33 -04:00
_uninstall ( ) ;
process . exit ( ) ;
}
2020-11-06 16:43:56 -05:00
else
{
if ( ! require ( 'message-box' ) . kdialog && ( ( require ( 'message-box' ) . zenity == null ) || ( ! require ( 'message-box' ) . zenity . extra ) ) )
{
2020-12-16 15:03:02 -05:00
console . log ( '\n' + translation [ lang ] . graphicalerror + '.' ) ;
console . log ( translation [ lang ] . zenity + ".\n" ) ;
console . log ( translation [ lang ] . commands + ": " ) ;
2020-11-06 16:43:56 -05:00
if ( ( msh . InstallFlags & 1 ) == 1 )
{
2020-09-14 18:34:55 -04:00
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -connect' ) ;
2020-09-10 18:18:33 -04:00
}
2020-11-06 16:43:56 -05:00
if ( ( msh . InstallFlags & 2 ) == 2 )
{
if ( s )
{
2020-09-14 18:34:55 -04:00
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -update' ) ;
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -uninstall' ) ;
}
2020-11-06 16:43:56 -05:00
else
{
2020-09-14 18:34:55 -04:00
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -install' ) ;
console . log ( './' + process . execPath . split ( '/' ) . pop ( ) + ' -install --installPath="/alternate/path"' ) ;
}
2020-09-10 18:18:33 -04:00
}
2020-09-14 18:34:55 -04:00
console . log ( '' ) ;
process . exit ( ) ;
2020-09-10 18:18:33 -04:00
}
}
2022-10-24 14:33:40 -04:00
if ( process . platform == 'darwin' )
{
if ( ! require ( 'user-sessions' ) . isRoot ( ) ) { console . log ( '\n' + translation [ lang ] . elevation ) ; process . exit ( ) ; }
}
2020-08-03 13:51:54 -04:00
}
2020-08-02 00:12:07 -04:00
2020-11-06 16:43:56 -05:00
if ( ! skip )
{
if ( ! s )
{
2020-12-16 15:03:02 -05:00
msg = translation [ lang ] . agent + ": " + translation [ lang ] . status [ 0 ] + '\n' ;
2020-11-06 16:43:56 -05:00
} else
{
2020-12-16 15:03:02 -05:00
msg = translation [ lang ] . agent + ": " + ( s . isRunning ( ) ? translation [ lang ] . status [ 1 ] : translation [ lang ] . status [ 2 ] ) + '\n' ;
2020-09-10 18:18:33 -04:00
}
2020-11-06 16:43:56 -05:00
2020-12-16 15:03:02 -05:00
msg += ( translation [ lang ] . group + ": " + msh . MeshName + '\n' ) ;
msg += ( translation [ lang ] . url + ": " + msh . MeshServer + '\n' ) ;
2020-11-06 16:43:56 -05:00
2020-12-16 15:03:02 -05:00
var p = require ( 'message-box' ) . create ( displayName + " " + translation [ lang ] . setup , msg , 99999 , buttons ) ;
2020-11-06 16:43:56 -05:00
p . then ( function ( v )
{
switch ( v )
{
2020-12-16 15:03:02 -05:00
case translation [ lang ] . cancel :
2020-08-03 13:51:54 -04:00
process . exit ( ) ;
2020-09-10 18:18:33 -04:00
break ;
2020-12-16 15:03:02 -05:00
case translation [ lang ] . setup :
var d = require ( 'message-box' ) . create ( displayName , msg , 99999 , [ translation [ lang ] . update , translation [ lang ] . uninstall , translation [ lang ] . cancel ] ) ;
2020-11-06 16:43:56 -05:00
d . then ( function ( v )
{
switch ( v )
{
2020-12-16 15:03:02 -05:00
case translation [ lang ] . update :
case translation [ lang ] . install :
2020-09-10 18:18:33 -04:00
_install ( ) ;
break ;
2020-12-16 15:03:02 -05:00
case translation [ lang ] . uninstall :
2020-09-10 18:18:33 -04:00
_uninstall ( ) ;
break ;
default :
break ;
}
process . exit ( ) ;
} ) . catch ( function ( v ) { process . exit ( ) ; } ) ;
break ;
2020-12-16 15:03:02 -05:00
case translation [ lang ] . connect :
2020-11-06 16:43:56 -05:00
global . _child = require ( 'child_process' ) . execFile ( process . execPath , connectArgs ) ;
2020-09-10 18:18:33 -04:00
global . _child . stdout . on ( 'data' , function ( c ) { } ) ;
global . _child . stderr . on ( 'data' , function ( c ) { } ) ;
global . _child . on ( 'exit' , function ( code ) { process . exit ( code ) ; } ) ;
2020-11-06 16:43:56 -05:00
2020-12-16 15:03:02 -05:00
msg = ( translation [ lang ] . group + ": " + msh . MeshName + '\n' ) ;
msg += ( translation [ lang ] . url + ": " + msh . MeshServer + '\n' ) ;
2020-11-06 16:43:56 -05:00
if ( process . platform != 'darwin' )
{
if ( ! require ( 'message-box' ) . zenity && require ( 'message-box' ) . kdialog )
{
2020-12-16 15:03:02 -05:00
msg += ( '\n' + translation [ lang ] . pressok ) ;
2020-09-10 18:18:33 -04:00
}
2020-08-03 13:51:54 -04:00
}
2020-11-06 16:43:56 -05:00
2020-12-16 15:03:02 -05:00
var d = require ( 'message-box' ) . create ( displayName , msg , 99999 , [ translation [ lang ] . disconnect ] ) ;
2020-09-10 18:18:33 -04:00
d . then ( function ( v ) { process . exit ( ) ; } ) . catch ( function ( v ) { process . exit ( ) ; } ) ;
break ;
2020-12-16 15:03:02 -05:00
case translation [ lang ] . uninstall :
2020-09-10 18:18:33 -04:00
_uninstall ( ) ;
process . exit ( ) ;
break ;
2020-12-16 15:03:02 -05:00
case translation [ lang ] . install :
case translation [ lang ] . update :
2020-09-10 18:18:33 -04:00
_install ( ) ;
process . exit ( ) ;
break ;
default :
console . log ( v ) ;
process . exit ( ) ;
break ;
}
2020-11-06 16:43:56 -05:00
} ) . catch ( function ( e )
{
2020-09-10 18:18:33 -04:00
console . log ( e ) ;
process . exit ( ) ;
} ) ;
2022-06-01 03:58:30 -04:00
}