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", "bin_path": "/usr/bin/mullvad",
"install_page": "https://mullvad.net/en/download/vpn/linux", "install_page": "https://mullvad.net/en/download/vpn/linux",
"cmd_overrides": { "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); $providerLog = getProviderLog($id, $binPath, $country);
// fetch provider version // fetch provider version
$providerVersion = shell_exec("sudo $binPath -v"); $providerVersion = getProviderVersion($id, $binPath);
// fetch account info // fetch account info
$accountInfo = getAccountInfo($id, $binPath, $providerName); $accountInfo = getAccountInfo($id, $binPath, $providerName);
@ -45,7 +45,7 @@ function DisplayProviderConfig()
if (strlen($country) == 0) { if (strlen($country) == 0) {
$status->addMessage('Select a country from the server location list', 'danger'); $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'])) { } elseif (isset($_POST['StartProviderVPN'])) {
$status->addMessage('Attempting to connect VPN provider', 'info'); $status->addMessage('Attempting to connect VPN provider', 'info');
@ -87,18 +87,29 @@ function DisplayProviderConfig()
/** /**
* Validates VPN provider settings * Validates VPN provider settings
* *
* @param object $status * @param object $status
* @return string $someVar * @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'); $status->addMessage('Attempting to connect to '.$country, 'info');
$cmd = getCliOverride($id, 'cmd_overrides', 'connect'); $cmd = getCliOverride($id, 'cmd_overrides', 'connect');
exec("sudo $binPath $cmd $country", $return); if ($id == 2) { // mullvad requires location set
sleep(3); // required for connect delay 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); $return = stripArtifacts($return);
foreach ($return as $line) { 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; $countries[$key] = $value;
} }
break; 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 case 3: // nordvpn
$output = stripArtifacts($output,'\s'); $output = stripArtifacts($output,'\s');
$arrTmp = explode(",", $output[0]); $arrTmp = explode(",", $output[0]);
@ -226,6 +257,20 @@ function getProviderLog($id, $binPath, &$country)
return $providerLog; 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 * Retrieves provider account info
* *