some changes in gotty

This commit is contained in:
alejandro.campos@artica.es 2023-10-09 18:56:02 +02:00
parent 0e0b5bba43
commit c059f83420
2 changed files with 205 additions and 188 deletions

View File

@ -79,8 +79,6 @@ function quickShell()
return; return;
} }
$method = get_parameter('method', null);
$setup_anchor = html_print_anchor( $setup_anchor = html_print_anchor(
[ [
'href' => 'index.php?sec=gsetup&sec2=godmode/setup/setup&section=quickshell', 'href' => 'index.php?sec=gsetup&sec2=godmode/setup/setup&section=quickshell',
@ -89,6 +87,15 @@ function quickShell()
true true
); );
$fetch_result = get_parameter('fetch-result', null);
if (isset($fetch_result) === true && $fetch_result === '0') {
ui_print_error_message(__('Connection error. Please check your settings at %s', $setup_anchor));
return;
}
$method = get_parameter('method', null);
if ((bool) $config['gotty_ssh_enabled'] === false if ((bool) $config['gotty_ssh_enabled'] === false
&& (bool) $config['gotty_telnet_enabled'] === false && (bool) $config['gotty_telnet_enabled'] === false
) { ) {
@ -118,6 +125,9 @@ function quickShell()
$connectionURL = buildConnectionURL($method); $connectionURL = buildConnectionURL($method);
$gotty_addr = $connectionURL.$args; $gotty_addr = $connectionURL.$args;
$connectionURLSSH = buildConnectionURL('ssh');
$connectionURLTelnet = buildConnectionURL('telnet');
// Username. Retrieve from form. // Username. Retrieve from form.
if (empty($username) === true) { if (empty($username) === true) {
// No username provided, ask for it. // No username provided, ask for it.
@ -160,6 +170,7 @@ function quickShell()
'label' => __('Username'), 'label' => __('Username'),
'arguments' => [ 'arguments' => [
'type' => 'text', 'type' => 'text',
'id' => 'username',
'name' => 'username', 'name' => 'username',
'required' => true, 'required' => true,
], ],
@ -191,7 +202,7 @@ function quickShell()
html_print_action_buttons( html_print_action_buttons(
html_print_submit_button( html_print_submit_button(
__('Connect'), __('Connect'),
'submit', 'submit-btn',
false, false,
[ [
'icon' => 'cog', 'icon' => 'cog',
@ -200,28 +211,55 @@ function quickShell()
true true
) )
); );
return;
echo "<script>
$(document).ready(function() {
// Intercept form submission.
var connectForm = document.getElementById('connect_form');
connectForm.addEventListener('submit', function (event) {
var username = $('#text-username').val();
var port = $('#text-port').val();
var method = $('#method').val();
var connectionURL = '';
if (method == 'ssh') {
connectionURL = '".$connectionURLSSH."'+'&arg=".$agent_address."&arg='+port+'&arg='+username;
} else if (method == 'telnet') {
connectionURL = '&arg=".$agent_address."&arg='+port;
} }
// Check gotty connection before trying to load iframe. // Prevent the default form submission behavior.
$ch = curl_init($gotty_addr); event.preventDefault();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Maximum time for the entire request.
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
// Maximum time to establish a connection.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$response = curl_exec($ch); var xhr = new XMLHttpRequest();
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch); var input = document.createElement('input');
input.type = 'hidden';
input.name = 'fetch-result';
if ($responseCode !== 200) { // Handle response.
ui_print_error_message(__('Connection error. Please check your settings at %s', $setup_anchor)); xhr.onreadystatechange = function () {
exit; if (xhr.readyState === 4) {
input.value = (xhr.status === 200) ? 1 : 0;
connectForm.appendChild(input);
connectForm.submit();
}
};
// Open and send the request.
try {
xhr.open('GET', connectionURL, true);
} catch (error) {
input.value = 0;
connectForm.appendChild(input);
connectForm.submit();
}
xhr.send();
});
});
</script>";
return;
} }
?> ?>
@ -511,92 +549,34 @@ function quickShellSettings()
html_print_input_hidden('update_config', 1); html_print_input_hidden('update_config', 1);
echo '</fieldset>'; echo '</fieldset>';
}
if (is_ajax() === true) {
$method = (string) get_parameter('method', '');
if (empty($method) === false) {
$address = buildConnectionURL($method);
$ch = curl_init($address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Maximum time for the entire request.
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
// Maximum time to establish a connection.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code === 200) {
$result = ['status' => 'success'];
} else {
$result = ['status' => 'error'];
}
echo json_encode($result);
return;
}
$result = ['status' => 'error'];
return;
}
// This extension is useful only if the agent has associated IP.
$agent_id = get_parameter('id_agente');
if (empty($agent_id) === false
&& get_parameter('sec2', '') == 'operation/agentes/ver_agente'
) {
$address = agents_get_address($agent_id);
if (empty($address) === false) {
// Extension registration.
extensions_add_opemode_tab_agent(
// TabId.
'quick_shell',
// TabName.
__('QuickShell'),
// TabIcon.
'images/quick-shell@svg.svg',
// TabFunction.
'quickShell',
// Version.
'N/A',
// Acl.
'PM'
);
}
}
echo '<script>'; echo '<script>';
echo 'var server_addr = "'.$_SERVER['SERVER_ADDR'].'";'; echo 'var server_addr = "'.$_SERVER['SERVER_ADDR'].'";';
echo "function checkAddressReachability(method, callback) { echo "function checkAddressReachability(method, callback) {
$.ajax({ var connectionURL = (method == 'ssh') ? '".buildConnectionURL('ssh')."' : '".buildConnectionURL('telnet')."';
url: 'ajax.php',
data: { var xhr = new XMLHttpRequest();
page: 'extensions/quick_shell', console.log(connectionURL);
method
},
type: 'GET', // Initialize the request with a 'HEAD' method, which is faster for checking
async: false, xhr.open('HEAD', connectionURL, false); // Synchronous request
dataType: 'json',
success: function (data) { try {
if (data.status === 'success') { // Send the request
xhr.send();
// Check if the request was successful (status code 200)
if (xhr.status === 200) {
callback(true); callback(true);
} else { } else {
callback(false); callback(false);
} }
}, } catch (error) {
error: function () { // An error occurred.
callback(false); callback(false);
} }
});
}"; }";
$handle_test_telnet = "var handleTestTelnet = function (event) { $handle_test_telnet = "var handleTestTelnet = function (event) {
@ -704,5 +684,31 @@ $handle_test_ssh = "var handleTestSSH = function (event) {
echo $handle_test_ssh; echo $handle_test_ssh;
echo $handle_test_telnet; echo $handle_test_telnet;
echo '</script>'; echo '</script>';
}
// This extension is useful only if the agent has associated IP.
$agent_id = get_parameter('id_agente');
if (empty($agent_id) === false
&& get_parameter('sec2', '') == 'operation/agentes/ver_agente'
) {
$address = agents_get_address($agent_id);
if (empty($address) === false) {
// Extension registration.
extensions_add_opemode_tab_agent(
// TabId.
'quick_shell',
// TabName.
__('QuickShell'),
// TabIcon.
'images/quick-shell@svg.svg',
// TabFunction.
'quickShell',
// Version.
'N/A',
// Acl.
'PM'
);
}
}
extensions_add_godmode_function('quickShellSettings'); extensions_add_godmode_function('quickShellSettings');

View File

@ -1497,7 +1497,6 @@ function config_update_config()
$interval_values = implode(',', $interval_values_array); $interval_values = implode(',', $interval_values_array);
} }
hd($interval_values, true);
if (config_update_value('interval_values', $interval_values, true) === false) { if (config_update_value('interval_values', $interval_values, true) === false) {
$error_update[] = __('Delete interval'); $error_update[] = __('Delete interval');
} }
@ -2490,6 +2489,18 @@ function config_process_config()
config_update_value('2Fa_auth', ''); config_update_value('2Fa_auth', '');
} }
if (!isset($config['gotty_ssh_enabled'])) {
config_update_value('gotty_ssh_enabled', 1);
}
if (!isset($config['gotty_telnet_enabled'])) {
config_update_value('gotty_telnet_enabled', 0);
}
if (!isset($config['gotty_port'])) {
config_update_value('gotty_port', 8080);
}
if (isset($config['performance_variables_control']) === false) { if (isset($config['performance_variables_control']) === false) {
config_update_value( config_update_value(
'performance_variables_control', 'performance_variables_control',