Implement CLI overrides, get countries + status functions

This commit is contained in:
billz 2023-10-16 10:44:46 +02:00
parent ac863f5c8a
commit 53f4645c2f

View file

@ -8,39 +8,38 @@ require_once 'includes/config.php';
function DisplayProviderConfig() function DisplayProviderConfig()
{ {
$status = new \RaspAP\Messages\StatusMessage; $status = new \RaspAP\Messages\StatusMessage;
$providerName = getProviderValue($_SESSION["providerID"], "name");
$binPath = getProviderValue($_SESSION["providerID"], "bin_path"); $id = $_SESSION["providerID"];
$public_ip = get_public_ip(); $providerName = getProviderValue($id, "name");
$binPath = getProviderValue($id, "bin_path");
$publicIP = get_public_ip();
if (!file_exists($binPath)) { if (!file_exists($binPath)) {
$installPage = getProviderValue($_SESSION["providerID"], "install_page"); $installPage = getProviderValue($id, "install_page");
$status->addMessage('Expected '.$providerName.' binary not found at: '.$binPath, 'warning'); $status->addMessage('Expected '.$providerName.' binary not found at: '.$binPath, 'warning');
$status->addMessage('Visit the <a href="'.$installPage.'" target="_blank">installation instructions</a> for '.$providerName.'\'s Linux CLI.', 'warning'); $status->addMessage('Visit the <a href="'.$installPage.'" target="_blank">installation instructions</a> for '.$providerName.'\'s Linux CLI.', 'warning');
$ctlState = 'disabled'; $ctlState = 'disabled';
$providerVersion = 'not found'; $providerVersion = 'not found';
} else { } else {
// fetch provider status // fetch provider status
$output = shell_exec("sudo $binPath status"); $serviceStatus = getProviderStatus($id, $binPath);
$serviceStatus = strtolower($output) == 0 ? "inactive" : "active"; $statusDisplay = $serviceStatus == "down" ? "inactive" : "active";
$result = strtolower(($lastSpacePos = strrpos($output, ' ')) ? substr($output, $lastSpacePos + 1) : $output);
// fetch provider log
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
$output = shell_exec("sudo $binPath $cmd");
$providerLog = stripArtifacts($output); $providerLog = stripArtifacts($output);
//echo '<br>status = '.$result;
// fetch provider version // fetch provider version
$providerVersion = shell_exec("sudo $binPath -v"); $providerVersion = shell_exec("sudo $binPath -v");
// fetch account info // fetch account info
exec("sudo $binPath account", $output); $cmd = getCliOverride($id, 'cmd_overrides', 'account');
exec("sudo $binPath $cmd", $output);
$accountInfo = stripArtifacts($output); $accountInfo = stripArtifacts($output);
// fetch available countries // fetch available countries
$output = shell_exec("sudo $binPath countries"); $countries = getCountries($id, $binPath);
$output = stripArtifacts($output, '\s');
$arrTmp = explode(",", $output);
$countries = array_combine($arrTmp, $arrTmp);
foreach ($countries as $key => $value) {
$countries[$key] = str_replace("_", " ", $value);
}
} }
if (!RASPI_MONITOR_ENABLED) { if (!RASPI_MONITOR_ENABLED) {
@ -51,7 +50,8 @@ function DisplayProviderConfig()
$return = SaveProviderConfig($status, $someVar); $return = SaveProviderConfig($status, $someVar);
} elseif (isset($_POST['StartProviderVPN'])) { } elseif (isset($_POST['StartProviderVPN'])) {
$status->addMessage('Attempting to connect VPN provider', 'info'); $status->addMessage('Attempting to connect VPN provider', 'info');
exec("sudo $binPath connect", $return); $cmd = getCliOverride($id, 'cmd_overrides', 'connect');
exec("sudo $binPath $cmd", $return);
foreach ($return as $line) { foreach ($return as $line) {
$status->addMessage($line, 'info'); $status->addMessage($line, 'info');
} }
@ -68,12 +68,13 @@ function DisplayProviderConfig()
"provider", compact( "provider", compact(
"status", "status",
"serviceStatus", "serviceStatus",
"statusDisplay",
"providerName", "providerName",
"providerVersion", "providerVersion",
"accountInfo", "accountInfo",
"countries", "countries",
"providerLog", "providerLog",
"public_ip", "publicIP",
"ctlState" "ctlState"
) )
); );
@ -103,3 +104,62 @@ function stripArtifacts($output, $pattern = null)
return $result; return $result;
} }
/**
* Retrieves an override for provider CLI
*
* @param integer $id
* @param string $group
* @param string $item
* @return string $override
*/
function getCliOverride($id, $group, $item)
{
$obj = json_decode(file_get_contents(RASPI_CONFIG_PROVIDERS), true);
if ($obj === null) {
return false;
} else {
$id--;
if ($obj['providers'][$id][$group][$item] === null) {
return $item;
} else {
return $obj['providers'][$id][$group][$item];
}
}
}
/**
* Retreives VPN provider status
*
* @param integer $id
* @param string $binPath
* @return string $status
*/
function getProviderStatus($id, $binPath)
{
$output = shell_exec("sudo $binPath status");
$output = strtolower(($lastSpace = strrpos($output, ' ')) ? substr($output, $lastSpace + 1) : $output);
$return = getCliOverride($id, 'status', 'connected');
$status = strtolower($return) == 'connected' ? "up" : "down";
return $status;
}
/**
* Retrieves available countries
*
* @param integer $id
* @param string $binPath
* @return array $countries
*/
function getCountries($id, $binPath)
{
$cmd = getCliOverride($id, 'cmd_overrides', 'countries');
$output = shell_exec("sudo $binPath $cmd");
$output = stripArtifacts($output, '\s');
$arrTmp = explode(",", $output);
$countries = array_combine($arrTmp, $arrTmp);
foreach ($countries as $key => $value) {
$countries[$key] = str_replace("_", " ", $value);
}
return $countries;
}