diff --git a/src/driver/scripts/check-reinstall.bat b/src/driver/scripts/check-reinstall.bat new file mode 100644 index 00000000..39a64838 --- /dev/null +++ b/src/driver/scripts/check-reinstall.bat @@ -0,0 +1,12 @@ +@rem Check existing driver file's timestamp or reinstall it + +@call "%~dp0setup-vars.bat" + + +@if exist "%DSTPATH%" ( + xcopy /L /D /Y "%SRCPATH%" "%DSTPATH%" | findstr /B /C:"0 " >NUL && goto EXIT +) + +"%~dp0reinstall.bat" %* + +:EXIT diff --git a/src/driver/scripts/install.bat b/src/driver/scripts/install.bat index fc71aaf2..edfc8f4e 100644 --- a/src/driver/scripts/install.bat +++ b/src/driver/scripts/install.bat @@ -1,16 +1,6 @@ @rem Install driver -@set ARCH=x86 -@if "%PROCESSOR_ARCHITECTURE%" == "AMD64" @set ARCH=x86_64 -@if "%PROCESSOR_ARCHITECTURE%" == "ARM64" @set ARCH=ARM64 - -@set BASENAME=fortfw -@set FILENAME=%BASENAME%.sys -@set SRCPATH=%~dp0..\%ARCH%\%FILENAME% -@set DSTPATH=%SystemRoot%\System32\drivers\%FILENAME% - -@set DRIVERSVC=%BASENAME% -@set DISPNAME=Fort Firewall Driver +@call "%~dp0setup-vars.bat" @rem Copy driver to system storage @@ -20,7 +10,7 @@ @goto EXIT ) -copy "%SRCPATH%" "%DSTPATH%" +robocopy "%SRCPATH%" "%DSTPATH%" @if ERRORLEVEL 1 ( @echo Error: Cannot copy driver to system @set RCODE=%ERRORLEVEL% diff --git a/src/driver/scripts/setup-vars.bat b/src/driver/scripts/setup-vars.bat new file mode 100644 index 00000000..1ef9bc50 --- /dev/null +++ b/src/driver/scripts/setup-vars.bat @@ -0,0 +1,16 @@ +@rem Setup variables + +@set ARCH=x86 +@if "%PROCESSOR_ARCHITECTURE%" == "AMD64" @set ARCH=x86_64 +@if "%PROCESSOR_ARCHITECTURE%" == "ARM64" @set ARCH=ARM64 + +@set BASENAME=fortfw +@set FILENAME=%BASENAME%.sys + +@set SRCPATH=%~dp0..\%ARCH%\%FILENAME% +@set DSTPATH=%SystemRoot%\System32\drivers\%FILENAME% + +@set DRIVERSVC=%BASENAME% +@set DISPNAME=Fort Firewall Driver + +@set FORTSVC=FortFirewallSvc diff --git a/src/driver/scripts/uninstall.bat b/src/driver/scripts/uninstall.bat index 152c9c27..117a6876 100644 --- a/src/driver/scripts/uninstall.bat +++ b/src/driver/scripts/uninstall.bat @@ -1,10 +1,6 @@ @rem Uninstall driver -@set BASENAME=fortfw -@set DSTPATH=%SystemRoot%\System32\drivers\%BASENAME%.sys - -@set DRIVERSVC=%BASENAME% -@set FORTSVC=FortFirewallSvc +@call "%~dp0setup-vars.bat" @rem Stop and delete the FortFirewall service diff --git a/src/ui/driver/drivermanager.cpp b/src/ui/driver/drivermanager.cpp index 438980b2..fcd6a76a 100644 --- a/src/ui/driver/drivermanager.cpp +++ b/src/ui/driver/drivermanager.cpp @@ -136,6 +136,11 @@ bool DriverManager::writeData(quint32 code, QByteArray &buf) return res; } +bool DriverManager::checkReinstallDriver() +{ + return executeCommand("check-reinstall.bat"); +} + bool DriverManager::reinstallDriver() { return executeCommand("reinstall.bat"); diff --git a/src/ui/driver/drivermanager.h b/src/ui/driver/drivermanager.h index 318bb50d..3fe0be82 100644 --- a/src/ui/driver/drivermanager.h +++ b/src/ui/driver/drivermanager.h @@ -29,6 +29,7 @@ public: void setUp() override; + bool checkReinstallDriver(); bool reinstallDriver(); bool uninstallDriver(); diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 5b6f396b..83e07fc4 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -178,10 +178,12 @@ void FortManager::initialize() setupTaskManager(); setupServiceInfoManager(); + checkReinstallDriver(); setupDriver(); + loadConf(); - checkInstallDriver(); + checkDriverOpened(); checkStartService(); } @@ -281,7 +283,7 @@ bool FortManager::installDriver() IoC()->reinstallDriver(); if (IoC()->hasService()) { - // Re-install the service and app restart required to continue + // Install the service and app restart required to continue StartupUtil::setServiceInstalled(true); processRestartRequired(tr("Driver reinstalled")); } else { @@ -332,19 +334,35 @@ void FortManager::closeDriver() QCoreApplication::sendPostedEvents(this); } -void FortManager::checkInstallDriver() +bool FortManager::canInstallDriver() const { - auto driverManager = IoC(); - - if (driverManager->isDeviceOpened()) - return; - const auto settings = IoC(); const bool canInstallDriver = (settings->canInstallDriver() || settings->isPortable()); const bool isMasterAdmin = (settings->isMaster() && settings->isUserAdmin()); - if (canInstallDriver && isMasterAdmin) { + return (canInstallDriver && isMasterAdmin); +} + +void FortManager::checkReinstallDriver() +{ + if (!canInstallDriver()) + return; + + IoC()->checkReinstallDriver(); + + if (IoC()->hasService()) { + // Install the service + StartupUtil::setServiceInstalled(true); + } +} + +void FortManager::checkDriverOpened() +{ + if (IoC()->isDeviceOpened()) + return; + + if (canInstallDriver()) { installDriver(); } } diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index 1b7deb85..047cb9ff 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -44,7 +44,9 @@ private: bool setupDriver(); void closeDriver(); - void checkInstallDriver(); + bool canInstallDriver() const; + void checkReinstallDriver(); + void checkDriverOpened(); void checkStartService(); void setupEnvManager();