mirror of
https://github.com/billz/raspap-webgui.git
synced 2025-03-01 10:31:47 +00:00
Refactor getCountries w/ regexes, add getAccountInfo()
This commit is contained in:
parent
99f8838fe2
commit
9a1457c6c8
@ -89,7 +89,7 @@ License: GNU General Public License v3.0
|
|||||||
border: 1px solid #d1d3e2;
|
border: 1px solid #d1d3e2;
|
||||||
border-radius: .35rem;
|
border-radius: .35rem;
|
||||||
font-family: Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace;
|
font-family: Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace;
|
||||||
font-size: 0.9rem;
|
font-size: 0.8rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dhcp-static-leases {
|
.dhcp-static-leases {
|
||||||
|
@ -6,10 +6,14 @@
|
|||||||
"bin_path": "/usr/bin/expressvpn",
|
"bin_path": "/usr/bin/expressvpn",
|
||||||
"install_page": "https://www.expressvpn.com/support/vpn-setup/app-for-linux/",
|
"install_page": "https://www.expressvpn.com/support/vpn-setup/app-for-linux/",
|
||||||
"cmd_overrides": {
|
"cmd_overrides": {
|
||||||
"countries": "list"
|
"countries": "list all",
|
||||||
|
"log": "diagnostics"
|
||||||
},
|
},
|
||||||
"status": {
|
"regex": {
|
||||||
"connected": "connected"
|
"status": "\/not connected\/",
|
||||||
|
"pattern": "\/^(.{2,5})\\s(?:.{1,28})(.{1,26}).*$\/",
|
||||||
|
"replace": "$1,$2",
|
||||||
|
"slice": 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -26,10 +30,12 @@
|
|||||||
"bin_path": "/usr/bin/nordvpn",
|
"bin_path": "/usr/bin/nordvpn",
|
||||||
"install_page": "https://nordvpn.com/download/linux/",
|
"install_page": "https://nordvpn.com/download/linux/",
|
||||||
"cmd_overrides": {
|
"cmd_overrides": {
|
||||||
|
"log": "status"
|
||||||
},
|
},
|
||||||
"status": {
|
"regex": {
|
||||||
"connected": "connected",
|
"status": "\/status: (\\w+)\/",
|
||||||
"disconnected": "disconnected"
|
"pattern": "(\\w+)\\s+",
|
||||||
|
"replace": "$1,$1\\n"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -32,9 +32,7 @@ function DisplayProviderConfig()
|
|||||||
$providerVersion = shell_exec("sudo $binPath -v");
|
$providerVersion = shell_exec("sudo $binPath -v");
|
||||||
|
|
||||||
// fetch account info
|
// fetch account info
|
||||||
$cmd = getCliOverride($id, 'cmd_overrides', 'account');
|
$accountInfo = getAccountInfo($id, $binPath, $providerName);
|
||||||
exec("sudo $binPath $cmd", $acct);
|
|
||||||
$accountInfo = stripArtifacts($acct);
|
|
||||||
|
|
||||||
// fetch available countries
|
// fetch available countries
|
||||||
$countries = getCountries($id, $binPath);
|
$countries = getCountries($id, $binPath);
|
||||||
@ -150,11 +148,13 @@ function getCliOverride($id, $group, $item)
|
|||||||
function getProviderStatus($id, $binPath)
|
function getProviderStatus($id, $binPath)
|
||||||
{
|
{
|
||||||
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
|
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
|
||||||
|
$pattern = getCliOverride($id, 'regex', 'status');
|
||||||
exec("sudo $binPath $cmd", $cmd_raw);
|
exec("sudo $binPath $cmd", $cmd_raw);
|
||||||
$cmd_raw = stripArtifacts($cmd_raw[0]);
|
$cmd_raw = strtolower(stripArtifacts($cmd_raw[0]));
|
||||||
if (preg_match('/Status: (\w+)/', $cmd_raw, $match)) {
|
if (preg_match($pattern, $cmd_raw, $match)) {
|
||||||
$cli = getCliOverride($id, 'status', 'connected');
|
$status = "down";
|
||||||
$status = strtolower($match[1]) == $cli ? "up" : "down";
|
} else {
|
||||||
|
$status = "up";
|
||||||
}
|
}
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
@ -168,16 +168,39 @@ function getProviderStatus($id, $binPath)
|
|||||||
*/
|
*/
|
||||||
function getCountries($id, $binPath)
|
function getCountries($id, $binPath)
|
||||||
{
|
{
|
||||||
|
$countries = [];
|
||||||
$cmd = getCliOverride($id, 'cmd_overrides', 'countries');
|
$cmd = getCliOverride($id, 'cmd_overrides', 'countries');
|
||||||
$output = shell_exec("sudo $binPath $cmd");
|
$pattern = getCliOverride($id, 'regex', 'pattern');
|
||||||
$output = stripArtifacts($output, '\s');
|
$replace = getCliOverride($id, 'regex', 'replace');
|
||||||
$arrTmp = explode(",", $output);
|
$slice = getCliOverride($id, 'regex', 'slice');
|
||||||
$countries = array_combine($arrTmp, $arrTmp);
|
exec("sudo $binPath $cmd", $output);
|
||||||
|
|
||||||
|
// CLI country output differs considerably between different providers.
|
||||||
|
// Ideally, custom parsing would be avoided in favor of a pure regex solution
|
||||||
|
switch ($id) {
|
||||||
|
case 1: // expressvpn
|
||||||
|
$output = array_slice($output, $slice);
|
||||||
|
foreach ($output as $item) {
|
||||||
|
$item = preg_replace($pattern, $replace, $item);
|
||||||
|
$parts = explode(',', $item);
|
||||||
|
$key = trim($parts[0]);
|
||||||
|
$value = trim($parts[1]);
|
||||||
|
$countries[$key] = $value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3: // nordvpn
|
||||||
|
$output = stripArtifacts($output,'\s');
|
||||||
|
$arrTmp = explode(",", $output[0]);
|
||||||
|
$countries = array_combine($arrTmp, $arrTmp);
|
||||||
|
foreach ($countries as $key => $value) {
|
||||||
|
$countries[$key] = str_replace("_", " ", $value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
$select = array(' ' => 'Select a country...');
|
$select = array(' ' => 'Select a country...');
|
||||||
$countries = $select + $countries;
|
$countries = $select + $countries;
|
||||||
foreach ($countries as $key => $value) {
|
|
||||||
$countries[$key] = str_replace("_", " ", $value);
|
|
||||||
}
|
|
||||||
return $countries;
|
return $countries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +214,7 @@ function getCountries($id, $binPath)
|
|||||||
*/
|
*/
|
||||||
function getProviderLog($id, $binPath, &$country)
|
function getProviderLog($id, $binPath, &$country)
|
||||||
{
|
{
|
||||||
$cmd = getCliOverride($id, 'cmd_overrides', 'status');
|
$cmd = getCliOverride($id, 'cmd_overrides', 'log');
|
||||||
exec("sudo $binPath $cmd", $cmd_raw);
|
exec("sudo $binPath $cmd", $cmd_raw);
|
||||||
$output = stripArtifacts($cmd_raw);
|
$output = stripArtifacts($cmd_raw);
|
||||||
foreach ($output as $item) {
|
foreach ($output as $item) {
|
||||||
@ -203,3 +226,23 @@ function getProviderLog($id, $binPath, &$country)
|
|||||||
return $providerLog;
|
return $providerLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves provider account info
|
||||||
|
*
|
||||||
|
* @param integer $id
|
||||||
|
* @param string $binPath
|
||||||
|
* @param string $providerName
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function getAccountInfo($id, $binPath, $providerName)
|
||||||
|
{
|
||||||
|
$cmd = getCliOverride($id, 'cmd_overrides', 'account');
|
||||||
|
exec("sudo $binPath $cmd", $acct);
|
||||||
|
$accountInfo = stripArtifacts($acct);
|
||||||
|
if (empty($accountInfo)) {
|
||||||
|
$msg = sprintf(_("Account details not available from %s's Linux CLI."), $providerName);
|
||||||
|
$accountInfo[] = $msg;
|
||||||
|
}
|
||||||
|
return $accountInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user