From 8d1c83a385e3b2e6655a36c75f026716a0afc108 Mon Sep 17 00:00:00 2001 From: Stephen J Carnam Date: Wed, 9 Aug 2023 02:44:49 -0700 Subject: [PATCH] wip --- vscode.php | 192 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 115 insertions(+), 77 deletions(-) diff --git a/vscode.php b/vscode.php index bc74ac0..4d4457e 100644 --- a/vscode.php +++ b/vscode.php @@ -27,10 +27,28 @@ public function __construct() { $hcpp->add_action( 'hcpp_rebooted', [ $this, 'hcpp_rebooted' ] ); $hcpp->add_action( 'hcpp_plugin_disabled', [ $this, 'hcpp_plugin_disabled' ] ); $hcpp->add_action( 'hcpp_plugin_enabled', [ $this, 'hcpp_plugin_enabled' ] ); + $hcpp->add_action( 'hcpp_render_page', [ $this, 'hcpp_render_page' ] ); } // Stop services on plugin disabled. public function hcpp_plugin_disabled() { + + // Gather list of all users + $cmd = "/usr/local/hestia/bin/v-list-users json"; + $result = shell_exec( $cmd ); + try { + $result = json_decode( $result, true, 512, JSON_THROW_ON_ERROR ); + } catch (Exception $e) { + var_dump( $e ); + return; + } + + // Remove VSCode for each valid user + foreach( $result as $key=> $value ) { + if ( $key === 'admin') continue; + if ( $value['SHELL'] !== 'bash' ) continue; + unlink( "/home/$key/.openvscode-server/data/token" ); + } $this->stop(); } @@ -60,11 +78,15 @@ public function hcpp_rebooted() { $this->start(); } - // Respond to invoke-plugin vscode_restart. + // Respond to invoke-plugin vscode_restart and vscode_get_token requests. public function hcpp_invoke_plugin( $args ) { if ( $args[0] === 'vscode_restart' ) { $this->restart(); } + if ( $args[0] === 'vscode_get_token' ) { + $user = $args[1]; + echo file_get_contents( "/home/$user/.openvscode-server/data/token" ); + } return $args; } @@ -232,6 +254,9 @@ public function setup( $user ) { symlink( $ssl_conf, $link ); } + // Create VSCode token if it doesn't already exist. + $this->update_token( $user ); + // Start the VSCode service manually (outside of PM2). $cmd = 'runuser -l ' . $user . ' -c "'; $cmd .= "(/opt/vscode/node /opt/vscode/out/server-main.js --port $port) > /dev/null 2>&1 &"; @@ -265,6 +290,95 @@ public function post_delete_user( $args ) { $hcpp->log( $hcpp->run( 'invoke-plugin vscode_restart' ) ); return $args; } + + // Update the VSCode Server access token + public function update_token( $user ) { + global $hcpp; + $token = $hcpp->nodeapp->random_chars( 32 ); + $cmd = "echo \"$token\" > \/home\/$user\/.openvscode-server\/data\/token && "; + $cmd .= "chown $user:$user \/home\/$user\/.openvscode-server\/data\/token && "; + $cmd .= "chmod 600 \/home\/$user\/.openvscode-server\/data\/token"; + $cmd = $hcpp->do_action( 'vscode_update_token', $cmd ); + $hcpp->log( shell_exec( $cmd ) ); + } + + // Add VSCode Server icon to our web domain list and button to domain edit pages. + public function hcpp_render_page( $args ) { + if ( $args['page'] == 'list_web' ) { + $args = $this->render_list_web( $args ); + } + if ( $args['page'] == 'edit_web' ) { + $args = $this->render_edit_web( $args ); + } + return $args; + } + + // Add VSCode Server button to our web domain edit page. + public function render_edit_web( $args ) { + + global $hcpp; + $user = trim( $args['user'], "'" ); + $hostname = trim( $hcpp->delLeftMost( shell_exec( 'hostname -f' ), '.' ) ); + $token = trim( $hcpp->run( "invoke-plugin vscode_get_token $user" ) ); + $domain = $_GET['domain']; + $content = $args['content']; + + // Create blue code icon button to appear before Quick Installer button + $code = 'getLeftMost( $content, $quick ); + $after = $quick . $hcpp->delLeftMost( $content, $quick ); + $after = 'getRightMost( $before, 'delRightMost( $before, 'log("vscode render_list_web"); + $user = trim( $args['user'], "'"); + $content = $args['content']; + $hostname = $hcpp->delLeftMost( $hcpp->getLeftMost( $_SERVER['HTTP_HOST'], ':' ), '.' ); + $token = trim( $hcpp->run( "invoke-plugin vscode_get_token $user" ) ); + + // Create white envelope icon before pencil/edit icon + $div = '
  • '; + $code = '
  • + + + VSCode + +
  • '; + $new = ''; + + // Inject the envelope icon for each domain + while( false !== strpos( $content, $div ) ) { + $new .= $hcpp->getLeftMost( $content, $div ); + $content = $hcpp->delLeftMost( $content, $div ); + $domain = $hcpp->getLeftMost( $hcpp->delLeftMost( $content, '?domain=' ), '&' ); + $folder = "/home/$user/web/$domain"; + $new .= str_replace( + ['%user%', '%hostname%', '%token%', '%folder%'], + [$user, $hostname, $token, $folder], + $code + ); + $new .= $div; + } + $new .= $content; + $args['content'] = $new; + return $args; + } } new VSCode(); } @@ -446,83 +560,7 @@ public function post_delete_user( $args ) { // } -// // Add VSCode Server icon to our web domain list and button to domain edit pages. -// public function hcpp_render_page( $args ) { -// if ( $args['page'] == 'list_web' ) { -// $args = $this->render_list_web( $args ); -// } -// if ( $args['page'] == 'edit_web' ) { -// $args = $this->render_edit_web( $args ); -// } -// return $args; -// } - -// // Add VSCode Server button to our web domain edit page. -// public function render_edit_web( $args ) { - -// global $hcpp; -// $user = trim( $args['user'], "'" ); -// $hostname = trim( $hcpp->delLeftMost( shell_exec( 'hostname -f' ), '.' ) ); -// $token = trim( $hcpp->run( "invoke-plugin vscode_get_token $user" ) ); -// $domain = $_GET['domain']; -// $content = $args['content']; - -// // Create blue code icon button to appear before Quick Installer button -// $code = 'getLeftMost( $content, $quick ); -// $after = $quick . $hcpp->delLeftMost( $content, $quick ); -// $after = 'getRightMost( $before, 'delRightMost( $before, 'log("vscode render_list_web"); -// $user = trim( $args['user'], "'"); -// $content = $args['content']; -// $hostname = $hcpp->delLeftMost( $hcpp->getLeftMost( $_SERVER['HTTP_HOST'], ':' ), '.' ); -// $token = trim( $hcpp->run( "invoke-plugin vscode_get_token $user" ) ); - -// // Create white envelope icon before pencil/edit icon -// $div = '
  • '; -// $code = '
  • -// -// -// VSCode -// -//
  • '; -// $new = ''; - -// // Inject the envelope icon for each domain -// while( false !== strpos( $content, $div ) ) { -// $new .= $hcpp->getLeftMost( $content, $div ); -// $content = $hcpp->delLeftMost( $content, $div ); -// $domain = $hcpp->getLeftMost( $hcpp->delLeftMost( $content, '?domain=' ), '&' ); -// $folder = "/home/$user/web/$domain"; -// $new .= str_replace( -// ['%user%', '%hostname%', '%token%', '%folder%'], -// [$user, $hostname, $token, $folder], -// $code -// ); -// $new .= $div; -// } -// $new .= $content; -// $args['content'] = $new; -// return $args; -// } // } // new VSCode(); // }