revert changes

This commit is contained in:
alejandro.campos@artica.es 2023-10-10 12:38:56 +02:00
parent 10d94daf30
commit 295654d26b

View File

@ -79,6 +79,8 @@ function quickShell()
return;
}
$method = get_parameter('method', null);
$setup_anchor = html_print_anchor(
[
'href' => 'index.php?sec=gsetup&sec2=godmode/setup/setup&section=quickshell',
@ -87,15 +89,6 @@ function quickShell()
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
&& (bool) $config['gotty_telnet_enabled'] === false
) {
@ -125,9 +118,6 @@ function quickShell()
$connectionURL = buildConnectionURL($method);
$gotty_addr = $connectionURL.$args;
$connectionURLSSH = buildConnectionURL('ssh');
$connectionURLTelnet = buildConnectionURL('telnet');
// Username. Retrieve from form.
if (empty($username) === true) {
// No username provided, ask for it.
@ -170,7 +160,6 @@ function quickShell()
'label' => __('Username'),
'arguments' => [
'type' => 'text',
'id' => 'username',
'name' => 'username',
'required' => true,
],
@ -202,7 +191,7 @@ function quickShell()
html_print_action_buttons(
html_print_submit_button(
__('Connect'),
'submit-btn',
'submit',
false,
[
'icon' => 'cog',
@ -211,57 +200,30 @@ function quickShell()
true
)
);
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;
}
// Prevent the default form submission behavior.
event.preventDefault();
var xhr = new XMLHttpRequest();
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'fetch-result';
// Handle response.
xhr.onreadystatechange = function () {
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;
}
// Check gotty connection before trying to load iframe.
$ch = curl_init($gotty_addr);
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);
$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;
}
?>
<style>#terminal {
width: 100%;
@ -318,6 +280,7 @@ function quickShellSettings()
ui_require_css_file('discovery');
// Gotty settings. Internal communication (WS).
if (isset($config['gotty_ssh_enabled']) === false) {
config_update_value('gotty_ssh_enabled', 1);
}
@ -548,44 +511,92 @@ function quickShellSettings()
html_print_input_hidden('update_config', 1);
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 'var server_addr = "'.$_SERVER['SERVER_ADDR'].'";';
echo "
function isValidIP(ip) {
// Regular expression for IPv4 address pattern
const ipv4Pattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipv4Pattern.test(ip);
}
function checkAddressReachability(method, callback) {
var connectionURL = (method == 'ssh') ? '".buildConnectionURL('ssh')."' : '".buildConnectionURL('telnet')."';
if (isValidIP('".$gotty_addr."') === false) {
callback(false);
}
var xhr = new XMLHttpRequest();
// Initialize the request with a 'HEAD' method, which is faster for checking
xhr.open('HEAD', connectionURL, false); // Synchronous request
try {
// Send the request
xhr.send();
// Check if the request was successful (status code 200)
if (xhr.status === 200) {
echo "function checkAddressReachability(method, callback) {
$.ajax({
url: 'ajax.php',
data: {
page: 'extensions/quick_shell',
method
},
type: 'GET',
async: false,
dataType: 'json',
success: function (data) {
if (data.status === 'success') {
callback(true);
} else {
callback(false);
}
} catch (error) {
// An error occurred.
},
error: function () {
callback(false);
}
});
}";
$handle_test_telnet = "var handleTestTelnet = function (event) {
@ -693,31 +704,5 @@ function quickShellSettings()
echo $handle_test_ssh;
echo $handle_test_telnet;
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');