From 101eed9dd55330de35d42a20b1109514fc87166e Mon Sep 17 00:00:00 2001 From: Arunas Skirius Date: Mon, 11 Nov 2024 13:13:33 +0200 Subject: [PATCH] scope the file and folder identifiers to the local machine IP address --- src/LogFile.php | 4 ++-- src/LogFolder.php | 2 +- src/Utils/Utils.php | 35 +++++++++++++++++++++++++++++++++++ tests/Unit/LogFileTest.php | 16 ++++++++++++++++ tests/Unit/LogFolderTest.php | 12 ++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/LogFile.php b/src/LogFile.php index f84205df..9e6c517e 100644 --- a/src/LogFile.php +++ b/src/LogFile.php @@ -28,7 +28,7 @@ public function __construct(string $path, ?string $type = null) { $this->path = $path; $this->name = basename($path); - $this->identifier = Utils::shortMd5($path).'-'.$this->name; + $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name; $this->type = $type; // Let's remove the file name because we already know it. @@ -94,7 +94,7 @@ public function sizeFormatted(): string public function subFolderIdentifier(): string { - return Utils::shortMd5($this->subFolder); + return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder); } public function downloadUrl(): string diff --git a/src/LogFolder.php b/src/LogFolder.php index 814286b5..51cc16b3 100644 --- a/src/LogFolder.php +++ b/src/LogFolder.php @@ -16,7 +16,7 @@ public function __construct( public string $path, mixed $files, ) { - $this->identifier = Utils::shortMd5($path); + $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path); $this->files = new LogFileCollection($files); } diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 648988e2..17fb62d0 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -6,6 +6,8 @@ class Utils { + private static string $_cachedLocalIP; + /** * Get a human-friendly readable string of the number of bytes provided. */ @@ -85,4 +87,37 @@ public static function glob_recursive($pattern, $flags = 0): array return $files; } + + public static function getLocalIP(bool $cached = true): string + { + if (isset(self::$_cachedLocalIP) && $cached) { + return self::$_cachedLocalIP; + } + + if (isset($_SERVER['SERVER_ADDR'])) { + self::$_cachedLocalIP = $_SERVER['SERVER_ADDR']; + } else { + $os = php_uname('s'); + + if (stripos($os, 'Linux') !== false) { + $localIP = shell_exec("hostname -I | awk '{print $1}'"); // Linux systems + } elseif (stripos($os, 'Darwin') !== false) { + $localIP = shell_exec("ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1"); // macOS + } else { + $localIP = gethostbyname(gethostname()); // Fallback method + } + + self::$_cachedLocalIP = trim($localIP ?? ''); + } + + return self::$_cachedLocalIP; + } + + /** + * Used for testing only. Do not use in your code. + */ + public static function setCachedLocalIP(string $ip): void + { + self::$_cachedLocalIP = $ip; + } } diff --git a/tests/Unit/LogFileTest.php b/tests/Unit/LogFileTest.php index 4a30406c..d11d92da 100644 --- a/tests/Unit/LogFileTest.php +++ b/tests/Unit/LogFileTest.php @@ -2,6 +2,7 @@ use Opcodes\LogViewer\LogFile; use Opcodes\LogViewer\Logs\LogType; +use Opcodes\LogViewer\Utils\Utils; test('log file can be instantiated with just a path to the file', function () { $path = storage_path('logs/laravel.log'); @@ -23,3 +24,18 @@ ->and($type->value)->toBe(LogType::DEFAULT) ->and($type->name())->toBe('Unknown'); }); + +test('log file identifier is based on server address', function () { + $path = storage_path('logs/laravel.log'); + file_put_contents($path, str_repeat('0', 10)); // 10 bytes + // Set the cached local IP to a known value: + Utils::setCachedLocalIP($serverIp = '123.123.123.123'); + + $logFile = new LogFile($path); + + expect($logFile->identifier)->toBe( + Utils::shortMd5($serverIp . ':' . $path) . '-laravel.log' + )->and($logFile->subFolderIdentifier())->toBe( + Utils::shortMd5($serverIp . ':' . $logFile->subFolder) + ); +}); diff --git a/tests/Unit/LogFolderTest.php b/tests/Unit/LogFolderTest.php index 282a2454..30127587 100644 --- a/tests/Unit/LogFolderTest.php +++ b/tests/Unit/LogFolderTest.php @@ -2,6 +2,7 @@ use Opcodes\LogViewer\LogFile; use Opcodes\LogViewer\LogFolder; +use Opcodes\LogViewer\Utils\Utils; test('LogFolder can get the earliest timestamp of the files it contains', function () { $firstFile = Mockery::mock(new LogFile('folder/test.log')) @@ -22,3 +23,14 @@ expect($folder->latestTimestamp())->toBe($firstFile->latestTimestamp()); }); + +test('log folder identifier is based on server address', function () { + // Set the cached local IP to a known value: + Utils::setCachedLocalIP($serverIp = '123.123.123.123'); + + $folder = new LogFolder('folder', []); + + expect($folder->identifier)->toBe( + Utils::shortMd5($serverIp . ':' . $folder->path) + ); +});