Populate country select, get provider log, connect to country

This commit is contained in:
billz 2023-10-18 09:50:57 +02:00
parent 47f983a361
commit 99f8838fe2
5 changed files with 67 additions and 22 deletions

View file

@ -88,6 +88,8 @@ License: GNU General Public License v3.0
height: 20rem;
border: 1px solid #d1d3e2;
border-radius: .35rem;
font-family: Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace;
font-size: 0.9rem;
}
.dhcp-static-leases {

View file

@ -26,17 +26,15 @@ function DisplayProviderConfig()
$statusDisplay = $serviceStatus == "down" ? "inactive" : "active";
// fetch provider log
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
$output = shell_exec("sudo $binPath $cmd");
$providerLog = stripArtifacts($output);
$providerLog = getProviderLog($id, $binPath, $country);
// fetch provider version
$providerVersion = shell_exec("sudo $binPath -v");
// fetch account info
$cmd = getCliOverride($id, 'cmd_overrides', 'account');
exec("sudo $binPath $cmd", $output);
$accountInfo = stripArtifacts($output);
exec("sudo $binPath $cmd", $acct);
$accountInfo = stripArtifacts($acct);
// fetch available countries
$countries = getCountries($id, $binPath);
@ -44,20 +42,27 @@ function DisplayProviderConfig()
if (!RASPI_MONITOR_ENABLED) {
if (isset($_POST['SaveProviderSettings'])) {
if (isset($_POST['someVar'])) {
$someVar = strip_tags(trim($_POST['someVar']));
if (isset($_POST['country'])) {
$country = trim($_POST['country']);
if (strlen($country) == 0) {
$status->addMessage('Select a country from the server location list', 'danger');
}
$return = saveProviderConfig($status, $binPath, $country);
}
$return = SaveProviderConfig($status, $someVar);
} elseif (isset($_POST['StartProviderVPN'])) {
$status->addMessage('Attempting to connect VPN provider', 'info');
$cmd = getCliOverride($id, 'cmd_overrides', 'connect');
exec("sudo $binPath $cmd", $return);
sleep(3); // required for connect delay
$return = stripArtifacts($return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
}
} elseif (isset($_POST['StopProviderVPN'])) {
$status->addMessage('Attempting to disconnect VPN provider', 'info');
exec("sudo $binPath disconnect", $return);
$cmd = getCliOverride($id, 'cmd_overrides', 'disconnect');
exec("sudo $binPath $cmd", $return);
$return = stripArtifacts($return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
}
@ -73,6 +78,7 @@ function DisplayProviderConfig()
"providerVersion",
"accountInfo",
"countries",
"country",
"providerLog",
"publicIP",
"ctlState"
@ -86,9 +92,16 @@ function DisplayProviderConfig()
* @param object $status
* @return string $someVar
*/
function SaveProviderConfig($status, $someVar)
function saveProviderConfig($status, $binPath, $country)
{
$status->addMessage('Attempting to connect to '.$country, 'info');
$cmd = getCliOverride($id, 'cmd_overrides', 'connect');
exec("sudo $binPath $cmd $country", $return);
sleep(3); // required for connect delay
$return = stripArtifacts($return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
}
}
/**
@ -136,11 +149,14 @@ function getCliOverride($id, $group, $item)
*/
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;
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
exec("sudo $binPath $cmd", $cmd_raw);
$cmd_raw = stripArtifacts($cmd_raw[0]);
if (preg_match('/Status: (\w+)/', $cmd_raw, $match)) {
$cli = getCliOverride($id, 'status', 'connected');
$status = strtolower($match[1]) == $cli ? "up" : "down";
}
return $status;
}
/**
@ -157,9 +173,33 @@ function getCountries($id, $binPath)
$output = stripArtifacts($output, '\s');
$arrTmp = explode(",", $output);
$countries = array_combine($arrTmp, $arrTmp);
$select = array(' ' => 'Select a country...');
$countries = $select + $countries;
foreach ($countries as $key => $value) {
$countries[$key] = str_replace("_", " ", $value);
}
return $countries;
}
/**
* Retrieves provider log
*
* @param integer $id
* @param string $binPath
* @param string $country
* @return string $log
*/
function getProviderLog($id, $binPath, &$country)
{
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
exec("sudo $binPath $cmd", $cmd_raw);
$output = stripArtifacts($cmd_raw);
foreach ($output as $item) {
if (preg_match('/Country: (\w+)/', $item, $match)) {
$country = $match[1];
}
$providerLog.= ltrim($item) .PHP_EOL;
}
return $providerLog;
}

View file

@ -1,7 +1,7 @@
<?php ob_start() ?>
<?php if (!RASPI_MONITOR_ENABLED) : ?>
<input type="submit" <?php echo $ctlState; ?> class="btn btn-outline btn-primary <?php echo $ctlState; ?>" name="SaveProviderConfig" value="Save settings" />
<?php if ($serviceStatus[0] == 0) : ?>
<input type="submit" <?php echo $ctlState; ?> class="btn btn-outline btn-primary <?php echo $ctlState; ?>" name="SaveProviderSettings" value="Save settings" />
<?php if ($serviceStatus == 'down') : ?>
<input type="submit" <?php echo $ctlState; ?> class="btn btn-success <?php echo $ctlState; ?>" name="StartProviderVPN" value="Connect <?php echo $providerName; ?>" />
<?php else : ?>
<input type="submit" <?php echo $ctlState; ?> class="btn btn-warning <?php echo $ctlState; ?>" name="StopProviderVPN" value="Disconnect <?php echo $providerName; ?>" />
@ -57,7 +57,7 @@
<div class="modal-title" id="ModalLabel"><i class="fas fa-sync mr-2"></i><?php echo sprintf(_("Logout %s"), $providerName); ?></div>
</div>
<div class="modal-body">
<div class="col-md-12 mb-3 mt-1" id="system-reboot-message"><?php echo sprintf(_("Logout now? This will disconnect the %s connection."), $providerName); ?></div>
<div class="col-md-12 mb-3 mt-1" id="system-reboot-message"><?php echo sprintf(_("Logout now? This will disconnect %s."), $providerName); ?></div>
</div>
<div class="modal-footer">
<button type="button" data-message="<?php echo _("Close"); ?>" class="btn btn-outline-secondary" data-dismiss="modal"><?php echo _("Cancel"); ?></button>

View file

@ -6,7 +6,7 @@
<div class="col-lg-12 mt-2 mb-2">
<div class="row ml-1">
<div class="info-item col-xs-3">
<i class="fas fa-globe mr-1"></i><?php echo _("IPv4 Address"); ?>
<i class="fas fa-globe-americas mr-1"></i><?php echo _("IPv4 Address"); ?>
</div>
<div class="info-value col-xs-3">
<?php echo htmlspecialchars($publicIP, ENT_QUOTES); ?><a class="text-gray-500" href="https://ipapi.co/<?php echo($publicIP); ?>" target="_blank" rel="noopener noreferrer"><i class="fas fa-external-link-alt ml-2"></i></a>
@ -33,8 +33,11 @@
<div class="row">
<div class="form-group col-md-6 mt-3">
<h5><?php echo _("Server location"); ?></h5>
<div class="mb-2">
<small><?php echo _("Choosing <strong>Save settings</strong> will connect to the selected country."); ?></small>
</div>
<label for="cbxhwmode"><?php echo _("Country") ;?></label>
<?php SelectorOptions('countries', $countries, $arrConfig['country'], 'cbxcountry'); ?>
<?php SelectorOptions('country', $countries, $country, 'cbxcountry'); ?>
</div>
</div>
</div><!-- /.tab-pane | general tab -->

View file

@ -1,6 +1,6 @@
<!-- logging tab -->
<div class="tab-pane fade" id="providerstatus">
<h4 class="mt-3 mb-3"><?php echo _("Status") ?></h4>
<h4 class="mt-3 mb-3"><?php echo sprintf(_("%s status"), $providerName) ;?></h4>
<p><?php echo _("Installed Linux CLI: <code>".$providerVersion."</code>") ?></p>
<p><?php echo _("Current <code>".strtolower($providerName)."</code> connection status is displayed below.") ?></p>