Parse mullvad relay list output, add getProviderVersion()

This commit is contained in:
billz 2023-10-20 16:43:59 +01:00
parent e51c6b0968
commit 1e6d94585a
2 changed files with 62 additions and 8 deletions

View file

@ -22,6 +22,15 @@
"bin_path": "/usr/bin/mullvad",
"install_page": "https://mullvad.net/en/download/vpn/linux",
"cmd_overrides": {
"account": "account get",
"countries": "relay list",
"log": "status -v",
"version": "--version"
},
"regex": {
"status": "\/disconnected\/",
"pattern": "\/^(.*),.*$\/",
"replace": "$1"
}
},
{

View file

@ -29,7 +29,7 @@ function DisplayProviderConfig()
$providerLog = getProviderLog($id, $binPath, $country);
// fetch provider version
$providerVersion = shell_exec("sudo $binPath -v");
$providerVersion = getProviderVersion($id, $binPath);
// fetch account info
$accountInfo = getAccountInfo($id, $binPath, $providerName);
@ -45,7 +45,7 @@ function DisplayProviderConfig()
if (strlen($country) == 0) {
$status->addMessage('Select a country from the server location list', 'danger');
}
$return = saveProviderConfig($status, $binPath, $country);
$return = saveProviderConfig($status, $binPath, $country, $id);
}
} elseif (isset($_POST['StartProviderVPN'])) {
$status->addMessage('Attempting to connect VPN provider', 'info');
@ -87,18 +87,29 @@ function DisplayProviderConfig()
/**
* Validates VPN provider settings
*
* @param object $status
* @return string $someVar
* @param object $status
* @param string $binPath
* @param string $country
* @param integer $id (optional)
*/
function saveProviderConfig($status, $binPath, $country)
function saveProviderConfig($status, $binPath, $country, $id = null)
{
$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
if ($id == 2) { // mullvad requires location set
exec("sudo $binPath set location $country", $return);
sleep(1);
exec("sudo $binPath $cmd $country", $return);
sleep(3); // required for connect delay
} else {
exec("sudo $binPath $cmd $country", $return);
sleep(3);
}
$return = stripArtifacts($return);
foreach ($return as $line) {
$status->addMessage($line, 'info');
if ( strlen(trim($line)) >0 ) {
$status->addMessage($line, 'info');
}
}
}
@ -188,6 +199,26 @@ function getCountries($id, $binPath)
$countries[$key] = $value;
}
break;
case 2: // mullvad
foreach ($output as $item) {
$item = preg_replace($pattern, $replace, $item);
if (strlen(trim($item) >0)) {
preg_match('/\s+([a-z0-9-]+)\s.*$/', $item, $match);
if (count($match) > 1) {
$key = $match[1];
$item = str_pad($item, strlen($item)+16,' ', STR_PAD_LEFT);
$countries[$key] = $item;
} else {
preg_match('/\(([a-z]+)\)/', $item, $match);
$key = $match[1];
if (strlen($match[1]) == 3) {
$item = str_pad($item, strlen($item)+8,' ', STR_PAD_LEFT);
}
$countries[$key] = $item;
}
}
}
break;
case 3: // nordvpn
$output = stripArtifacts($output,'\s');
$arrTmp = explode(",", $output[0]);
@ -226,6 +257,20 @@ function getProviderLog($id, $binPath, &$country)
return $providerLog;
}
/**
* Retrieves provider version information
*
* @param integer $id
* @param string $binPath
* @return string $version
*/
function getProviderVersion($id, $binPath)
{
$cmd = getCliOverride($id, 'cmd_overrides', 'version');
$version = shell_exec("sudo $binPath $cmd");
return $version;
}
/**
* Retrieves provider account info
*