diff --git a/pandora_console/extensions/quick_shell.php b/pandora_console/extensions/quick_shell.php index d07342945a..9aee7610d8 100644 --- a/pandora_console/extensions/quick_shell.php +++ b/pandora_console/extensions/quick_shell.php @@ -89,13 +89,10 @@ function quickShell() true ); - if ($method === 'ssh' && (bool) $config['gotty_ssh_enabled'] === false) { - ui_print_error_message(__('Please, enable SSH in %s', $setup_anchor)); - return; - } - - if ($method === 'telnet' && (bool) $config['gotty_telnet_enabled'] === false) { - ui_print_error_message(__('Please, enable Telnet in %s', $setup_anchor)); + if ((bool) $config['gotty_ssh_enabled'] === false + && (bool) $config['gotty_telnet_enabled'] === false + ) { + ui_print_warning_message(__('Please, enable GoTTY in %s', $setup_anchor)); return; } @@ -112,19 +109,13 @@ function quickShell() // Build URL args. if ($method === 'ssh') { // SSH. - $args .= '?arg='.$username.'@'.$agent_address; - //$args = '?arg='.$username.'@172.16.0.1'; - $args .= '&arg=-p%20'.$method_port; + $args .= '&arg='.$agent_address.'&arg='.$method_port.'&arg='.$username; } else if ($method == 'telnet') { // Telnet. - $username = preg_replace('/[^a-zA-Z0-9\-\.]/', '', $username); - $args = '?arg=-l%20'.$username; - $args .= '&arg='.$agent_address; - $args .= '&arg='.$method_port.'&arg=-E'; + $args .= '&arg='.$agent_address.'&arg='.$method_port; } $connectionURL = buildConnectionURL($method); - //$basic_auth_hdr = $config['gotty_ssh_user'].':'.$config['gotty_ssh_pass'].'@'; $gotty_addr = $connectionURL.$args; // Username. Retrieve from form. @@ -132,6 +123,18 @@ function quickShell() // No username provided, ask for it. $wiz = new Wizard(); + $method_fields = []; + + if ($config['gotty_telnet_enabled']) { + $method_fields['telnet'] = __('Telnet'); + $port_value = 23; + } + + if ($config['gotty_ssh_enabled']) { + $method_fields['ssh'] = __('SSH'); + $port_value = 22; + } + $wiz->printForm( [ 'form' => [ @@ -154,7 +157,7 @@ function quickShell() 'type' => 'text', 'id' => 'port', 'name' => 'port', - 'value' => 22, + 'value' => $port_value, ], ], [ @@ -162,10 +165,7 @@ function quickShell() 'arguments' => [ 'type' => 'select', 'name' => 'method', - 'fields' => [ - 'ssh' => __('SSH'), - 'telnet' => __('Telnet'), - ], + 'fields' => $method_fields, 'script' => "p=22; if(this.value == 'telnet') { p=23; } $('#text-port').val(p);", ], ], @@ -190,6 +190,24 @@ function quickShell() return; } + // Check gotty connection before trying to load iframe. + $ch = curl_init($gotty_addr); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + + $response = curl_exec($ch); + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + + curl_close($ch); + + if ($responseCode !== 200) { + ui_print_error_message(__('Connection error. Please check your settings at %s', $setup_anchor)); + exit; + } + ?> -
'; - echo ''.__('SSH connection parameters').''; + echo ''.__('GoTTY general parameters').''; - $test_start = ''; - $test_start .= ' '; + $general_table = new StdClass(); + $general_table->data = []; + $general_table->width = '100%'; + $general_table->class = 'filter-table-adv'; + $general_table->data = []; + $general_table->style = []; + $general_table->style[0] = 'width: 50%;'; + + $general_table->data[0][] = html_print_label_input_block( + __('Address'), + html_print_input_text( + 'gotty_addr', + $config['gotty_addr'], + '', + 30, + 100, + true + ) + ); + + $general_table->data[0][] = html_print_label_input_block( + __('Port'), + html_print_input_text( + 'gotty_port', + $config['gotty_port'], + '', + 30, + 100, + true + ) + ); + + html_print_table($general_table); + echo ''; + + echo '
'; + echo ''.__('GoTTY SSH connection parameters').''; $ssh_table = new StdClass(); $ssh_table->data = []; @@ -471,7 +433,7 @@ function quickShellSettings() $ssh_table->style[0] = 'width: 50%;'; $ssh_table->data[0][] = html_print_label_input_block( - __('Enable SSH GoTTY'), + __('Enable SSH method'), html_print_checkbox_switch( 'gotty_ssh_enabled', 1, @@ -481,54 +443,6 @@ function quickShellSettings() ); $ssh_table->data[1][] = html_print_label_input_block( - __('Gotty address'), - html_print_input_text( - 'gotty_ssh_addr', - $config['gotty_ssh_addr'], - '', - 30, - 100, - true - ) - ); - - $ssh_table->data[1][] = html_print_label_input_block( - __('Gotty port'), - html_print_input_text( - 'gotty_ssh_port', - $config['gotty_ssh_port'], - '', - 30, - 100, - true - ) - ); - - $ssh_table->data[2][] = html_print_label_input_block( - __('Gotty user'), - html_print_input_text( - 'gotty_ssh_user', - $config['gotty_ssh_user'], - '', - 30, - 100, - true - ) - ); - - $ssh_table->data[2][] = html_print_label_input_block( - __('Gotty password'), - html_print_input_password( - 'gotty_ssh_pass', - io_output_password($config['gotty_ssh_pass']), - '', - 30, - 100, - true - ) - ); - - $ssh_table->data[3][] = html_print_label_input_block( __('Use SSL'), html_print_checkbox_switch( 'gotty_ssh_use_ssl', @@ -544,11 +458,6 @@ function quickShellSettings() $test_start = ''; $test_start .= ' '; - /*$tip = ui_print_help_tip( - __('Save configuration before performing test to check if GoTTY process was started successfully'), - true - );*/ - $ssh_table->data[3][] = html_print_button( __('Test'), 'test-gotty-ssh', @@ -567,7 +476,7 @@ function quickShellSettings() echo '
'; echo '
'; - echo ''.__('Telnet connection parameters').''; + echo ''.__('GoTTY telnet connection parameters').''; $telnet_table = new StdClass(); $telnet_table->data = []; @@ -578,7 +487,7 @@ function quickShellSettings() $telnet_table->style[0] = 'width: 50%;'; $telnet_table->data[0][] = html_print_label_input_block( - __('Enable Telnet GoTTY'), + __('Enable telnet method'), html_print_checkbox_switch( 'gotty_telnet_enabled', 1, @@ -588,54 +497,6 @@ function quickShellSettings() ); $telnet_table->data[1][] = html_print_label_input_block( - __('Gotty address'), - html_print_input_text( - 'gotty_telnet_addr', - $config['gotty_telnet_addr'], - '', - 30, - 100, - true - ) - ); - - $telnet_table->data[1][] = html_print_label_input_block( - __('Gotty port'), - html_print_input_text( - 'gotty_telnet_port', - $config['gotty_telnet_port'], - '', - 30, - 100, - true - ) - ); - - $telnet_table->data[2][] = html_print_label_input_block( - __('Gotty user'), - html_print_input_text( - 'gotty_telnet_user', - $config['gotty_telnet_user'], - '', - 30, - 100, - true - ) - ); - - $telnet_table->data[2][] = html_print_label_input_block( - __('Gotty password'), - html_print_input_password( - 'gotty_telnet_pass', - io_output_password($config['gotty_telnet_pass']), - '', - 30, - 100, - true - ) - ); - - $telnet_table->data[3][] = html_print_label_input_block( __('Use SSL'), html_print_checkbox_switch( 'gotty_telnet_use_ssl', @@ -675,6 +536,7 @@ if (is_ajax() === true) { if (empty($method) === false) { $address = buildConnectionURL($method); + $ch = curl_init($address); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -689,7 +551,7 @@ if (is_ajax() === true) { $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - if ($response_code === 200 || $response_code === 401) { + if ($response_code === 200) { $result = ['status' => 'success']; } else { $result = ['status' => 'error']; @@ -755,16 +617,14 @@ echo "function checkAddressReachability(method, callback) { }"; $handle_test_telnet = "var handleTestTelnet = function (event) { - var gotty_telnet_addr = $('input#text-gotty_telnet_addr').val(); - var gotty_telnet_port = $('input#text-gotty_telnet_port').val(); - var gotty_telnet_user = $('input#text-gotty_telnet_user').val(); - var gotty_telnet_password = $('input#password-gotty_telnet_pass').val(); + var gotty_addr = $('input#text-gotty_addr').val(); + var gotty_port = $('input#text-gotty_port').val(); var gotty_telnet_use_ssl = $('input#checkbox-gotty_telnet_use_ssl').is(':checked'); - if (gotty_telnet_addr === '') { - url = (gotty_telnet_use_ssl ? 'https://' : 'http://') + server_addr + ':' + gotty_telnet_port; + if (gotty_addr === '') { + url = (gotty_telnet_use_ssl ? 'https://' : 'http://') + server_addr + ':' + gotty_port; } else { - url = (gotty_telnet_use_ssl ? 'https://' : 'http://') + gotty_telnet_addr + ':' + gotty_telnet_port; + url = (gotty_telnet_use_ssl ? 'https://' : 'http://') + gotty_addr + ':' + gotty_port; } var showLoadingImage = function () { @@ -808,16 +668,14 @@ $handle_test_telnet = "var handleTestTelnet = function (event) { };"; $handle_test_ssh = "var handleTestSSH = function (event) { - var gotty_ssh_addr = $('input#text-gotty_ssh_addr').val(); - var gotty_ssh_port = $('input#text-gotty_ssh_port').val(); - var gotty_ssh_user = $('input#text-gotty_ssh_user').val(); - var gotty_ssh_password = $('input#password-gotty_ssh_pass').val(); + var gotty_addr = $('input#text-gotty_addr').val(); + var gotty_port = $('input#text-gotty_port').val(); var gotty_ssh_use_ssl = $('input#checkbox-gotty_ssh_use_ssl').is(':checked'); - if (gotty_ssh_addr === '') { - url = (gotty_ssh_use_ssl ? 'https://' : 'http://') + server_addr + ':' + gotty_ssh_port; + if (gotty_addr === '') { + url = (gotty_ssh_use_ssl ? 'https://' : 'http://') + server_addr + ':' + gotty_port; } else { - url = (gotty_ssh_use_ssl ? 'https://' : 'http://') + gotty_ssh_addr + ':' + gotty_ssh_port; + url = (gotty_ssh_use_ssl ? 'https://' : 'http://') + gotty_addr + ':' + gotty_port; } var showLoadingImage = function () { diff --git a/pandora_console/include/functions_cron_task.php b/pandora_console/include/functions_cron_task.php index 0962632431..ffc5958d85 100644 --- a/pandora_console/include/functions_cron_task.php +++ b/pandora_console/include/functions_cron_task.php @@ -454,90 +454,50 @@ function cron_task_start_gotty(bool $restart_mode=true) $gotty_ssh_enabled = (bool) $config['gotty_ssh_enabled']; $gotty_telnet_enabled = (bool) $config['gotty_telnet_enabled']; - // Check prev SSH process running and kill it (before changing config parameters). - if (empty($config['restart_gotty_ssh_next_cron_port']) === false) { - config_update_value('restart_gotty_ssh_next_cron_port', ''); + // Check prev process running and kill it (only if port changed in setup params). + if (empty($config['restart_gotty_next_cron_port']) === false) { + config_update_value('restart_gotty_next_cron_port', ''); - $prevProcessRunningSSH = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['restart_gotty_ssh_next_cron_port']."'"); + $prevProcessRunning = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['restart_gotty_next_cron_port']."'"); - if (!empty($prevProcessRunningSSH)) { - shell_exec("pkill -f 'pandora_gotty.*-p ".$config['restart_gotty_ssh_next_cron_port']."'"); + if (empty($prevProcessRunning) === false) { + shell_exec("pkill -f 'pandora_gotty.*-p ".$config['restart_gotty_next_cron_port']."'"); } } - // Check if prev Telnet process running and kill it (before changing config parameters). - if (empty($config['restart_gotty_telnet_next_cron_port']) === false) { - config_update_value('restart_gotty_telnet_next_cron_port', ''); + // Check if gotty is running on the configured port. + $processRunning = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['gotty_port']."'"); - $prevProcessRunningTelnet = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['restart_gotty_telnet_next_cron_port']."'"); + $start_proc = true; - if (!empty($prevProcessRunningTelnet)) { - shell_exec("pkill -f 'pandora_gotty.*-p ".$config['restart_gotty_telnet_next_cron_port']."'"); - } + // If both methods are disabled, do not start process. + if ($gotty_ssh_enabled === false && $gotty_telnet_enabled === false) { + $start_proc = false; } - // Check if pandora_gotty is running on the configured port. - $processRunningSSH = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['gotty_ssh_port']."'"); - $processRunningTelnet = shell_exec("pgrep -f 'pandora_gotty.*-p ".$config['gotty_telnet_port']."'"); - - $start_ssh_proc = true; - $start_telnet_proc = true; - - if ($gotty_ssh_enabled === false) { - $start_ssh_proc = false; - } - - if ($gotty_telnet_enabled === false) { - $start_telnet_proc = false; - } - - if (!empty($processRunningSSH)) { + if (empty($processRunning) === false) { // Process is running. - if ($restart_mode === true || $start_ssh_proc === false) { + if ($restart_mode === true || $start_proc === false) { // Stop the process for restarting or in case GoTTY method is disabled in this iteration. - shell_exec("pkill -f 'pandora_gotty.*-p ".$config['gotty_ssh_port']."'"); + shell_exec("pkill -f 'pandora_gotty.*-p ".$config['gotty_port']."'"); } else { // Prevent starting if already running and must not be restarted or terminated. - $start_ssh_proc = false; + return; } } - if (!empty($processRunningTelnet)) { - // Process is running. - if ($restart_mode === true || $start_telnet_proc === false) { - // Stop the process for restarting or in case GoTTY method is disabled in this iteration. - shell_exec("pkill -f 'pandora_gotty.*-p ".$config['gotty_telnet_port']."'"); - } else { - // Prevent starting if already running and must not be restarted or terminated. - $start_telnet_proc = false; - } - } + if ($start_proc === true) { + $logFilePath = $config['homedir'].'/log/gotty_cron_tmp.log'; + shell_exec('touch '.$logFilePath); - if ($start_ssh_proc === false && $start_telnet_proc === false) { - // Nothing to start. + // Start gotty process and capture the output. + $command = '/usr/bin/nohup /usr/bin/pandora_gotty --config /etc/pandora_gotty/pandora_gotty.conf -p '.$config['gotty_port'].' /usr/bin/pandora_gotty_exec > '.$logFilePath.' 2>&1 &'; + shell_exec($command); + } else { return; } - if ($start_ssh_proc === true) { - $logFilePathSSH = $config['homedir'].'/log/gotty_ssh_cron_tmp.log'; - shell_exec('touch '.$logFilePathSSH); - - // Start pandora_gotty and capture the output. - $command_ssh = '/usr/bin/nohup /usr/bin/pandora_gotty --config /etc/pandora_gotty/pandora_gotty.conf --ws-origin ".*" -p '.$config['gotty_ssh_port'].' ssh > '.$logFilePathSSH.' 2>&1 &'; - shell_exec($command_ssh); - } - - if ($start_telnet_proc === true) { - $logFilePathTelnet = $config['homedir'].'/log/gotty_telnet_cron_tmp.log'; - shell_exec('touch '.$logFilePathTelnet); - - // Start pandora_gotty and capture the output. - $command_telnet = '/usr/bin/nohup /usr/bin/pandora_gotty --config /etc/pandora_gotty/pandora_gotty.conf --ws-origin ".*" -p '.$config['gotty_telnet_port'].' telnet > '.$logFilePathTelnet.' 2>&1 &'; - shell_exec($command_telnet); - } - - $ssh_hash_read = false; - $telnet_hash_read = false; + $hash_read = false; // Maximum wait time to read asynchronously the output of the executed commands (seconds). $maxWaitTime = 10; @@ -547,58 +507,32 @@ function cron_task_start_gotty(bool $restart_mode=true) // Workaround to wait until process inputs data in the log. while (time() - $startTime < $maxWaitTime) { - if ($start_ssh_proc === true) { + if ($start_proc === true) { // Read command output. - $log_content_ssh = file_get_contents($logFilePathSSH); + $log_content = file_get_contents($logFilePath); } - if ($start_ssh_proc === true - && !empty($log_content_ssh) - && $ssh_hash_read === false + if ($start_proc === true + && !empty($log_content) + && $hash_read === false ) { // Extract the URL from the output. - if (preg_match('/.*?HTTP server is listening at:\s+(\S+)/', $log_content_ssh, $matches)) { + if (preg_match('/.*?HTTP server is listening at:\s+(\S+)/', $log_content, $matches)) { $url = $matches[1]; // Extract the hash. $parts = explode('/', $url); $hash = array_slice($parts, -2, 1)[0]; - config_update_value('gotty_ssh_connection_hash', $hash); - $ssh_hash_read = true; + config_update_value('gotty_connection_hash', $hash); + $hash_read = true; } - unlink($logFilePathSSH); + unlink($logFilePath); } - if ($start_telnet_proc === true) { - // Read command output. - $log_content_telnet = file_get_contents($logFilePathTelnet); - } - - if ($start_telnet_proc === true - && !empty($log_content_telnet) - && $telnet_hash_read === false - ) { - // Extract the URL from the output. - if (preg_match('/.*?HTTP server is listening at:\s+(\S+)/', $log_content_telnet, $matches)) { - $url = $matches[1]; - - // Extract the hash. - $parts = explode('/', $url); - $hash = array_slice($parts, -2, 1)[0]; - - config_update_value('gotty_telnet_connection_hash', $hash); - $telnet_hash_read = true; - } - - unlink($logFilePathTelnet); - } - - if (($start_ssh_proc === false || $ssh_hash_read === true) - && ($start_telnet_proc === false || $telnet_hash_read === true) - ) { - // As soon as the reads have completed, the timing loop will terminate. + if ($start_proc === false || $hash_read === true) { + // As soon as the read has completed, the timing loop will terminate. break; }