Compare commits

...

8 Commits

Author SHA1 Message Date
Zack Spear
6edf28c083 refactor: improve error handling in ReplaceKey class
- Removed unnecessary comments and streamlined the cURL request process.
- Enhanced error handling to differentiate between cURL errors and HTTP errors, providing clearer error messages.
- Added HTTP status code to the error log for better debugging.

This change improves the maintainability and clarity of the `ReplaceKey` class while ensuring more informative error reporting.
2025-03-19 19:09:19 -07:00
Zack Spear
893a7930f3 refactor: restructure ServerState class for update checks
- Moved the initialization of `UnraidOsCheck` to a new method `initializeOsCheck()` for better organization.
- Ensured that the update check runs before any other operations to guarantee the latest `var.ini` values are used.

This change enhances the clarity and maintainability of the `ServerState` class.
2025-03-19 19:02:42 -07:00
Zack Spear
d34771a038 fix: correct syntax in ReplaceKey class 2025-03-19 18:59:14 -07:00
Zack Spear
c75774d70d fix: reduce timeout duration in UnraidCheck class 2025-03-19 18:54:09 -07:00
Zack Spear
3bcd7266a4 fix: reduce sleep duration in UnraidCheck class
- Changed the sleep duration between checks from 0.25 seconds to 0.15 seconds in the `UnraidCheck` class to improve responsiveness during checks.

This change optimizes the checking process without altering the overall functionality.
2025-03-19 18:19:23 -07:00
Zack Spear
df1e1f0e34 feat: enhance ServerState class for update checks and license key extensions
- Added functionality to the `ServerState` class to check for updates and new license keys when rendering specific pages like `/Tools/Registration`, `/Tools/Update`, and `/Tools/Downgrade`.
- Introduced a new method `shouldCheckForUpdates()` to determine if an update check is necessary based on the current path.
- Updated relevant pages to reflect these changes, ensuring a more seamless user experience during registration and updates.
2025-03-19 18:16:56 -07:00
Zack Spear
b700278ba4 feat: add ReplaceKey class and integrate into plugin
- Introduced `ReplaceKey` class for handling license key operations.
- Updated `dynamix.unraid.net.plg` to include `ReplaceKey.php` and added it to the list of preserved files.
- This change enhances the plugin's functionality related to license management.
2025-03-19 18:02:05 -07:00
Zack Spear
234661a222 feat: integrate ReplaceKey functionality in Registration and Update pages
- Added `ReplaceKey` class inclusion and instantiation in `Registration.page` and `Update.page`.
- Implemented a call to the `check()` method of `ReplaceKey` to enhance server registration and update processes.
2025-03-18 15:42:12 -07:00
8 changed files with 346 additions and 28 deletions

View File

@@ -384,6 +384,7 @@ if [ -f /tmp/restore-files-dynamix-unraid-net ]; then
"/usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php"
"/usr/local/emhttp/plugins/dynamix/include/ProvisionCert.php"
"/usr/local/emhttp/plugins/dynamix/include/UpdateDNS.php"
"/usr/local/emhttp/plugins/dynamix/include/ReplaceKey.php"
"/usr/local/emhttp/plugins/dynamix/include/Wrappers.php"
"/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page"
"/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page"
@@ -505,6 +506,7 @@ echo
preserveFilesDirs=(
"move:/usr/local/emhttp/plugins/dynamix/Registration.page:preventDowngrade"
"move:/usr/local/emhttp/plugins/dynamix/include/UpdateDNS.php:preventDowngrade"
"move:/usr/local/emhttp/plugins/dynamix/include/ReplaceKey.php:preventDowngrade"
"move:/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page:preventDowngrade"
"move:/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page:preventDowngrade"
"move:/usr/local/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck:preventDowngrade"

View File

@@ -14,6 +14,9 @@ Tag="pencil"
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
/**
* @note ServerState class constructor will check for updates and new license key, bypassing the check window period, when rendering /Tools/Registration
*/
?>
<unraid-i18n-host>
<unraid-registration></unraid-registration>

View File

@@ -35,6 +35,9 @@ class ServerState
{
protected $webguiGlobals;
private const VAR_INI_FILE = '/var/local/emhttp/var.ini';
private const NGINX_INI_FILE = '/var/local/emhttp/nginx.ini';
private $var;
private $apiKey = '';
private $apiVersion = '';
@@ -54,6 +57,7 @@ class ServerState
"nokeyserver" => 'NO_KEY_SERVER',
"withdrawn" => 'WITHDRAWN',
];
/**
* SSO Sub IDs from the my servers config file.
*/
@@ -81,20 +85,39 @@ class ServerState
public $activationCodeData = [];
public $state = 'UNKNOWN';
public $pathsForUpdateOsCheck = [
[
'path' => '/Tools/Registration',
'forceReplaceKeyCheck' => true,
],
[
'path' => '/Tools/Update',
'forceReplaceKeyCheck' => false,
],
[
'path' => '/Tools/Downgrade',
'forceReplaceKeyCheck' => false,
],
];
/**
* Constructor to initialize class properties and gather server information.
*/
public function __construct()
{
/**
* Run update check before any other operations to ensure
* var.ini has latest values after potential key replacements
*/
$this->initializeOsCheck();
/**
* @note necessary evil until full webgui is class based.
* @see - getWebguiGlobal() for usage
* */
global $webguiGlobals;
$this->webguiGlobals = &$webguiGlobals;
// echo "<pre>" . json_encode($this->webguiGlobals, JSON_PRETTY_PRINT) . "</pre>";
$this->var = $webguiGlobals['var'];
$this->var = (array)@parse_ini_file(self::VAR_INI_FILE);
// If we're on a patch, we need to use the combinedVersion to check for updates
if (file_exists('/tmp/Patcher/patches.json')) {
@@ -102,7 +125,7 @@ class ServerState
$this->var['version'] = $patchJson['combinedVersion'] ?? $this->var['version'];
}
$this->nginxCfg = @parse_ini_file('/var/local/emhttp/nginx.ini') ?? [];
$this->nginxCfg = @parse_ini_file(self::NGINX_INI_FILE);
$this->state = strtoupper(empty($this->var['regCheck']) ? $this->var['regTy'] : $this->var['regCheck']);
$this->osVersion = $this->var['version'];
@@ -119,7 +142,6 @@ class ServerState
$this->keyfileBase64UrlSafe = str_replace(['+', '/', '='], ['-', '_', ''], trim($this->keyfileBase64));
}
$this->updateOsCheck = new UnraidOsCheck();
$this->updateOsIgnoredReleases = $this->updateOsCheck->getIgnoredReleases();
$this->updateOsNotificationsEnabled = !empty(@$this->getWebguiGlobal('notify', 'unraidos'));
$this->updateOsResponse = $this->updateOsCheck->getUnraidOSCheckResult();
@@ -128,6 +150,16 @@ class ServerState
$this->detectActivationCode();
}
private function initializeOsCheck(): void
{
$this->updateOsCheck = new UnraidOsCheck();
$updateCheck = $this->shouldCheckForUpdates();
if ($updateCheck) {
$this->updateOsCheck->checkForUpdate($updateCheck['forceReplaceKeyCheck']);
}
}
/**
* Retrieve the value of a webgui global setting.
*/
@@ -268,6 +300,19 @@ class ServerState
$this->activationCodeData = $data;
}
private function shouldCheckForUpdates(): ?array
{
$currentPath = $_SERVER['REQUEST_URI'];
foreach ($this->pathsForUpdateOsCheck as $pathConfig) {
if (strpos($currentPath, $pathConfig['path']) !== false) {
return $pathConfig;
}
}
return null;
}
/**
* Retrieve the server information as an associative array
*

View File

@@ -15,6 +15,7 @@ Tag="upload"
*/
/**
* @note icon-update is rotated via CSS in myservers1.php
* @note ServerState class constructor will check for updates and new license key when rendering /Tools/Registration
*/
require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";

View File

@@ -13,6 +13,9 @@ Tag="upload"
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
/**
* @note ServerState class constructor will check for updates and new license key when rendering /Tools/Update
*/
require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
$rebootDetails = new RebootDetails();
?>

View File

@@ -29,6 +29,7 @@
*/
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/webGui/include/ReplaceKey.php";
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";
class UnraidOsCheck
@@ -38,6 +39,7 @@ class UnraidOsCheck
private const JSON_FILE_IGNORED_KEY = 'updateOsIgnoredReleases';
private const JSON_FILE_RESULT = '/tmp/unraidcheck/result.json';
private const PLG_PATH = '/usr/local/emhttp/plugins/unRAIDServer/unRAIDServer.plg';
private const VAR_INI_FILE = '/var/local/emhttp/var.ini';
public function __construct()
{
@@ -53,7 +55,8 @@ class UnraidOsCheck
{
switch ($_GET['action']) {
case 'check':
$this->checkForUpdate();
$forceReplaceKeyCheck = (isset($_GET['forceReplaceKeyCheck'])) ? true : false;
$this->checkForUpdate($forceReplaceKeyCheck);
break;
case 'removeAllIgnored':
@@ -100,30 +103,51 @@ class UnraidOsCheck
return [];
}
/** @todo clean up this method to be more extensible */
public function checkForUpdate()
/**
* Check for Unraid OS updates and new license keys
*
* @param bool $forceReplaceKeyCheck Force check for license key replacement regardless of expiry window
* @return ?bool Returns:
* - true: Successfully checked for updates with no errors
* - false: Error occurred during update check
* - null: No update check was performed (e.g. invalid params)
* Note: If $_GET['json'] is set, outputs JSON response and exits with code 0 instead of returning
*/
public function checkForUpdate(bool $forceReplaceKeyCheck = false): ?bool
{
// Multi-language support
if (!function_exists('_')) {
function _($text) {return $text;}
$var = (array)@parse_ini_file(self::VAR_INI_FILE);
$initialRegExp = _var($var, 'regExp');
// checking for a new license key created via auto-extension
$replaceKey = new ReplaceKey();
if ($replaceKey->check($forceReplaceKeyCheck)) {
// if we have a new key, we need to wait for emhttp to update var.ini with the new regExp value
$startTime = time();
$timeout = 5; // seconds
while (time() - $startTime < $timeout) {
$currentVar = (array)@parse_ini_file(self::VAR_INI_FILE);
$currentRegExp = _var($currentVar, 'regExp');
// Handle cases where either value might be undefined or different
if ((!$initialRegExp && $currentRegExp) || ($initialRegExp && !$currentRegExp) ||
($initialRegExp && $currentRegExp && $currentRegExp !== $initialRegExp)) {
$var = $currentVar;
break;
}
usleep(150000); // Sleep for 0.15 seconds between checks
}
if (time() - $startTime >= $timeout) { // if we timeout, use the current value
$var = (array)@parse_ini_file(self::VAR_INI_FILE);
}
}
// this command will set the $notify array
extract(parse_plugin_cfg('dynamix', true));
$var = (array)@parse_ini_file('/var/local/emhttp/var.ini');
$params = [];
$params['branch'] = plugin('category', self::PLG_PATH, 'stable');
// Get current version from patches.json if it exists, otherwise fall back to plugin version or var.ini
$patcherVersion = null;
if (file_exists('/tmp/Patcher/patches.json')) {
$patcherData = @json_decode(file_get_contents('/tmp/Patcher/patches.json'), true);
$patcherVersion = $patcherData['combinedVersion'] ?? null;
}
$params['current_version'] = $patcherVersion ?: plugin('version', self::PLG_PATH) ?: _var($var, 'version');
if (_var($var,'regExp')) $params['update_exp'] = date('Y-m-d', _var($var,'regExp')*1);
$params['current_version'] = plugin('version', self::PLG_PATH) ?: _var($var, 'version');
if (_var($var, 'regExp')) $params['update_exp'] = date('Y-m-d', _var($var, 'regExp')*1);
$defaultUrl = self::BASE_RELEASES_URL;
// pass a param of altUrl to use the provided url instead of the default
$parsedAltUrl = (array_key_exists('altUrl',$_GET) && $_GET['altUrl']) ? $_GET['altUrl'] : null;
@@ -161,8 +185,8 @@ class UnraidOsCheck
$isReleaseIgnored = array_key_exists('version',$responseMutated) ? in_array($responseMutated['version'], $this->getIgnoredReleases()) : false;
if ($responseMutated && $isNewerVersion && !$isReleaseIgnored) {
$output = _var($notify,'plugin');
$server = strtoupper(_var($var,'NAME','server'));
$output = _var($notify, 'plugin');
$server = strtoupper(_var($var, 'NAME', 'server'));
$newver = (array_key_exists('version',$responseMutated) && $responseMutated['version']) ? $responseMutated['version'] : 'unknown';
$script = '/usr/local/emhttp/webGui/scripts/notify';
$event = "System - Unraid [$newver]";
@@ -171,7 +195,7 @@ class UnraidOsCheck
exec("$script -e ".escapeshellarg($event)." -s ".escapeshellarg($subject)." -d ".escapeshellarg($description)." -i ".escapeshellarg("normal $output")." -l '/Tools/Update' -x");
}
exit(0);
return !array_key_exists('error', $responseMutated);
}
private function removeAllIgnored()

View File

@@ -9,11 +9,19 @@
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Usage:
* unraidcheck - Normal check for updates
* unraidcheck --force-replace-key-check - Force check for new license key before update check
*/
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/plugins/dynamix.plugin.manager/include/UnraidCheck.php";
$forceReplaceKeyCheck = in_array('--force-replace-key-check', $argv);
$unraidOsCheck = new UnraidOsCheck();
$unraidOsCheck->checkForUpdate();
$unraidOsCheckResult = $unraidOsCheck->checkForUpdate($forceReplaceKeyCheck);
exit($unraidOsCheckResult ? 0 : 1);

View File

@@ -0,0 +1,232 @@
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
class ReplaceKey
{
private const KEY_SERVER_URL = 'https://keys.lime-technology.com';
private $docroot;
private $var;
private $guid;
private $keyfile;
private $regExp;
public function __construct()
{
$this->docroot = $GLOBALS['docroot'] ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$this->var = (array)@parse_ini_file('/var/local/emhttp/var.ini');
$this->guid = @$this->var['regGUID'] ?? null;
$keyfileBase64 = empty($this->var['regFILE']) ? null : @file_get_contents($this->var['regFILE']);
if ($keyfileBase64 !== false) {
$keyfileBase64 = @base64_encode($keyfileBase64);
$this->keyfile = str_replace(['+', '/', '='], ['-', '_', ''], trim($keyfileBase64));
}
$this->regExp = @$this->var['regExp'] ?? null;
}
private function request($url, $method, $payload = null, $headers = null)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($payload !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
if ($headers !== null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Check for cURL errors or non-2xx responses
if (curl_errno($ch) || $httpCode < 200 || $httpCode >= 300) {
$error = [
'heading' => curl_errno($ch) ? 'CurlError' : 'HttpError',
'message' => curl_errno($ch) ? curl_error($ch) : "HTTP Status $httpCode",
'level' => 'error',
'ref' => curl_errno($ch) ? 'curlError' : 'httpError',
'type' => 'request',
'ts' => time(),
'url' => $url,
'httpCode' => $httpCode
];
$this->writeJsonFile('/tmp/ReplaceKey/request_errors.json', $error);
}
curl_close($ch);
return $response;
}
private function validateGuid()
{
$headers = [
'Content-Type: application/x-www-form-urlencoded',
];
$params = [
'guid' => $this->guid,
'keyfile' => $this->keyfile,
];
/**
* returns {JSON}
* hasNewerKeyfile : boolean;
* purchaseable: true;
* registered: false;
* replaceable: false;
* upgradeable: false;
* upgradeAllowed: string[];
* updatesRenewable: false;
*/
$response = $this->request(
self::KEY_SERVER_URL . '/validate/guid',
'POST',
http_build_query($params),
$headers,
);
// Handle the response as needed (parsing JSON, etc.)
$decodedResponse = json_decode($response, true);
if (!empty($decodedResponse)) {
return $decodedResponse;
}
// @todo save error response somewhere
return [];
}
private function getLatestKey()
{
$headers = [
'Content-Type: application/x-www-form-urlencoded',
];
$params = [
'keyfile' => $this->keyfile,
];
/**
* returns {JSON}
* license: string;
*/
$response = $this->request(
self::KEY_SERVER_URL . '/key/latest',
'POST',
http_build_query($params),
$headers,
);
// Handle the response as needed (parsing JSON, etc.)
$decodedResponse = json_decode($response, true);
if (!empty($decodedResponse) && !empty($decodedResponse['license'])) {
return $decodedResponse['license'];
}
return null;
}
private function installNewKey($key): bool
{
require_once "$this->docroot/webGui/include/InstallKey.php";
$KeyInstaller = new KeyInstaller();
$installResponse = $KeyInstaller->installKey($key);
$installSuccess = false;
if (!empty($installResponse)) {
$decodedResponse = json_decode($installResponse, true);
if (isset($decodedResponse['error'])) {
$this->writeJsonFile(
'/tmp/ReplaceKey/error.json',
[
'error' => $decodedResponse['error'],
'ts' => time(),
]
);
$installSuccess = false;
} else {
$installSuccess = true;
}
}
// Set up notification for key installation result
$keyType = basename($key, '.key');
$output = _var($notify,'plugin');
$script = '/usr/local/emhttp/webGui/scripts/notify';
if ($installSuccess) {
$event = "Installed New $keyType License";
$subject = "Your new $keyType license key has been automatically installed";
$description = "";
$importance = "normal $output";
} else {
$event = "Failed to Install New $keyType License";
$subject = "Failed to automatically install your new $keyType license key";
$description = isset($decodedResponse['error']) ? $decodedResponse['error'] : "Unknown error occurred";
$importance = "alert $output";
}
exec("$script -e ".escapeshellarg($event)." -s ".escapeshellarg($subject)." -d ".escapeshellarg($description)." -i ".escapeshellarg($importance)." -l '/Tools/Registration' -x");
return $installSuccess;
}
private function writeJsonFile($file, $data)
{
if (!is_dir(dirname($file))) {
mkdir(dirname($file));
}
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
public function check(bool $forceCheck = false): ?bool
{
// we don't need to check
if (empty($this->guid) || empty($this->keyfile) || empty($this->regExp)) {
return null;
}
// Check if we're within the 7-day window before and after regExp
$now = time();
$sevenDaysBefore = strtotime('-7 days', $this->regExp);
$sevenDaysAfter = strtotime('+7 days', $this->regExp);
$isWithinWindow = ($now >= $sevenDaysBefore && $now <= $sevenDaysAfter);
if (!$forceCheck && !$isWithinWindow) {
return null;
}
// see if we have a new key
$validateGuidResponse = $this->validateGuid();
$hasNewerKeyfile = @$validateGuidResponse['hasNewerKeyfile'] ?? false;
if (!$hasNewerKeyfile) {
return null; // if there is no newer keyfile, we don't need to do anything
}
$latestKey = $this->getLatestKey();
if (!$latestKey) {
// we supposedly have a new key, but didn't get it back…
$this->writeJsonFile(
'/tmp/ReplaceKey/error.json',
[
'error' => 'Failed to retrieve latest key after getting a `hasNewerKeyfile` in the validation response.',
'ts' => time(),
]
);
return null;
}
return $this->installNewKey($latestKey);
}
}