diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 24a3d25f..f6b352b3 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,201 +1,208 @@ -# Welcome to CDPI UI repo! +# Contributing to CDPI UI -Here some rules and tips for you to make contributing easier +This document tells you how the application operates, how to build it and how to change it. +Each project has a README with more data. The list of these documents is at the end of this page. -## How application is works? +## How the application operates -Application has GUI part and background worker. -Background worker – CDPIUI.TrayIcon & CDPIUI.Shared -GUI – CDPIUI, CDPIUI.AddOns, CDPIUI.Core & CDPIUI.Shared +CDPI UI has two processes. The two processes send messages to each other through a named pipe. -## Build +| Part | Projects | User account | Function | +|---|---|---|---| +| GUI | `CDPIUI`, `CDPIUI.AddOns`, `CDPIUI.Core`, `CDPIUI.Shared` | The signed-in user, without elevation | Shows the windows. Controls the Store, the presets, the add-ons and the settings. | +| Tray | `CDPIUI.TrayIcon`, `CDPIUI.Shared` | Administrator | Starts and stops the components. Controls the system proxy, the autorun, the notifications, Conditional Launch and the MSI installation. | -To build you own CDPIUI you must build and run CDPIUI.TrayIcon as admin, than run as default user GUI (it's important! Some features are broken in elevated mode in GUI). +Two more tools use .NET Framework 4.7.2: -Also: -Recommended Visual Studio version – 2022. +- `Update` applies the application patches. +- `EditHostFile` changes the hosts file with administrator rights. -Here is few steps after NuGet restore you must to do: -- Create `Secret.cs` in `/CDPIUI.Shared/Secrets/`. Create static class Secret. [Ref](https://github.com/Storik4pro/cdpiui/blob/dev/CDPIUI.Shared/Secrets/SecretExample.cs) -- Project using CommunityToolkit.Labs. Connect it from Azure, to restore experimental packages. [Ref](https://marketplace.visualstudio.com/items?itemName=ISWIX-LLC.IsWiX) +The `NewSetup` project makes the MSI installer. -> Project uses IsWix for MSI-installer building. [Ref](https://marketplace.visualstudio.com/items?itemName=ISWIX-LLC.IsWiX) +The GUI does not start the components. The GUI sends a request to the tray. The tray starts the component and sends the process state and the output to the GUI. -## Add/edit something +When the GUI starts and the tray does not operate, the GUI starts the tray and stops. Then the tray starts the GUI again with the initial arguments. -### Localization -- On EN localization ready settings file for components called "Config", on RU localization "Пресет". +## Build the application -#### Add localization for new language -- Create resource files with `.resw` extension in `CDPIUI/Strings/`. `` must follow format like `en-us` or `ru`. You must set "Build Action" as "Content" and set "Copy if never" flag. -- Create `.md` file for localize ELUA `CDPIUI/ELUA/`. `` must follow Store-like format like `EN` or `RU`. -- Create `.md` files for localize WIKI `CDPIUI/Help/` `` must follow Store-like format like `EN` or `RU`. -- To localize Store visit [Store repo](https://github.com/Sotrik4pro/CDPIUI-Store). -- Add key `` to all `.resw` files with localized localization name for existed localizations. -- In `CDPIUI.Helper.UserExperience` add key to `Languages` ObservableCollection -- In `CDPIUI.App` add pair in `InitializeLocalizer` +You must have these items: -To localize TrayIcon: -- Add dict to `CDPIUI.TrayIcon.Helper.Locale.LocaleHelper`. -- In `CDPIUI.TrayIcon.Helper.Locale.LocaleHelper.GetLocaleString` add pair for `` in ISO format with two letters. +- Windows 10 or Windows 11, x64. +- Visual Studio 2022 with the .NET desktop and WinUI workloads. Alternatively, the .NET 8 SDK and MSBuild from Visual Studio 2022. +- PowerShell 7. -### Add a feature +Do these steps: -#### Before we start -If feature you added changing existing feature or application core follow "Edit an application code" -Otherwise, follow "Create new AddOn" +1. Make the secrets class `CDPIUI.Shared/Secrets/Secret.cs`. + - The class must be `public static class Secret` in the namespace `CDPIUI.Shared.Secrets`. + - The class must have the members of the `ISecret` interface in [SecretExample.cs](../CDPIUI.Shared/Secrets/SecretExample.cs). + - Copy only the members. Do not copy the interface, because it is already in that file. + - For a local build without Store access, run `pwsh .github/scripts/create_secret_stub.ps1`. The script makes the class with empty tokens and a random `AuthGuid`. + - `AuthGuid` must not be empty. With an empty `AuthGuid`, the tray authorizes no pipe client, and the GUI cannot connect. + - Git ignores this file. Do not add it to the repository. +2. Restore the NuGet packages. + - The GUI uses CommunityToolkit Labs packages. + - `CDPIUI/nuget.config` adds the [Labs feed](https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-Labs/nuget/v3/index.json) and nuget.org. A usual restore finds all packages. +3. Build `CDPI.sln` for the `x64` platform. The application does not support other platforms. +4. Start `CDPIUI.TrayIcon` as administrator. +5. Start the GUI (`CDPIUI`) as a usual user. +CAUTION: Do not start the GUI as administrator. Some GUI functions do not operate with elevation. For example, you cannot drop files from Explorer into an elevated window. + +### Other commands + +| Task | Command | +|---|---| +| Run the unit tests | `dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj` | +| Run the unit tests on Linux or macOS | `dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj -p:EnableWindowsTargeting=true` | +| Build the release layout | `pwsh .github/scripts/build_portable.ps1` | +| Compare the installer file list with the release layout | `pwsh .github/scripts/check_setup_files.ps1` | +| Build the MSI installer | `dotnet build NewSetup/NewSetup.wixproj -c Release -p:Platform=x64` | + +Some notes about these commands: + +- You cannot build the GUI project on Linux or macOS. +- `build_portable.ps1` puts the GUI, the single-file tray, `Update.exe` and `EditHostFile.exe` into `CDPIUI/bin/x64/Release/net8.0-windows10.0.22621.0/publish`. +- Build the MSI installer after `build_portable.ps1`. The installer uses [WiX Toolset 6](https://wixtoolset.org/). +- `NewSetup/NewSetupFiles.wxs` must list all files of the release layout. `check_setup_files.ps1` shows the differences. +- The `build_and_patch.yml` workflow makes the patches (`.cdpipatch`). Refer to [.github/README.md](README.md). + +### Continuous integration + +The workflow `.github/workflows/ci.yml` does these checks: + +- It builds the libraries and runs the unit tests (job `libraries`). +- It builds the GUI and the release layout, compares the installer file list with the release layout and builds `Setup.msi` (job `gui`). +- It builds `Update` and `EditHostFile` (job `tools`). + +The `libraries` and `tools` jobs must pass before you ask for a review. The `gui` job has `continue-on-error`. Its result gives only information. Refer to [.github/README.md](README.md). + +## Project structure -#### Project structure ->Make sure you use correct naming. My grammar is too bad, sorry :( ```text cdpiui/ -├── .github/ # GitHub workflows and CI/CD configurations -├── CDPIUI/ # GUI source code -| ├── Assets/ # Images of application -| ├── Commands/ # Handlers of commands -| | └── CommandsUriMapper.cs # Mapper for external cdpiui:// links -| ├── ConditionalLaunch/ # Localized commands catalogues for ConditionalLaunch utility -| ├── Controls/ # Custom controls -| | └── Dialogs/ # Custom dialogs -| ├── Converters/ # UI converters (e.g. BoolToVisibilityConverter) -| ├── ELUA/ # Localized ELUA .md files -| ├── Extensions/ # Extensions -| ├── Help/ # Localized wiki .md files -| ├── Helper/ # UI helpers (classes, who required localization or XAML elements) -| ├── Properties/ # VisualStudio 2022 project settings -| ├── Strings/ # UI elements localization -| ├── Template/ # Template files to copy to build directory -| ├── ViewModels/ # UI ViewModels and associated helpers (build-in) -| ├── Views/ # Pages. Store pages for window in folders with same name as window name -| └── Windows/ # Windows. IMPORTANT! Namespace for windows is CDPIUI.*, DO NOT use CDPIUI.Windows.* -| ├── App.xaml # Main styles -| ├── App.xaml.cs # Main GUI application code -| └── Program.cs # Entry point of application -├── CDPIUI.AddOns # AddOns. Store AddOns files in same as AddOn name folder -├── CDPIUI.Core/ # Core application/business logic -│ ├── Basic/ # Basic core abstractions and utilities -│ ├── Communication/ # Core communication infrastructure -│ ├── ComponentServices/ # Services responsible for application components -│ ├── Data/ # Core data structures/services -│ ├── Features/ # Core feature implementations -│ ├── JSON/ # JSON-related functionality -│ ├── LScript/ # LScript subsystem -│ ├── Proxy/ # Proxy-related core functionality -│ ├── Security/ # Security-related services and utilities -│ ├── Store/ # Core store subsystem -│ ├── System/ # Operating-system/system integration -│ ├── Application.cs # Core application abstraction/state -│ ├── CoreEvents.cs # Shared core application events -│ ├── SettingsManager.cs # Application settings management -│ ├── State.cs # Core application state -│ └── CDPIUI.Core.csproj # Core project definition -├── CDPIUI.Shared/ # Code shared between GUI, Core and helper applications -│ ├── Basic/ # Common/basic helpers -│ ├── ComponentsTask/ # Shared component-task infrastructure -│ ├── ConditionalLaunch/ # Shared ConditionalLaunch implementation -│ ├── Exceptions/ # Shared exception types -│ ├── Extentions/ # Shared extension methods -│ │ # NOTE: repository currently spells this "Extentions" -│ ├── Logger/ # Shared logging infrastructure -│ ├── Migration/ # Data/settings migration functionality -│ ├── Models/ # Models shared across projects -│ ├── Pipe/ # Inter-process pipe communication -│ ├── PrettyErrorConvertionService/ # User-friendly error conversion -│ │ # NOTE: repository currently uses "Convertion" -│ ├── Secrets/ # Secret/value handling infrastructure -│ ├── System/ # Shared system/OS helpers -│ ├── SharedConstants.cs # Constants shared across applications -│ ├── Utils.cs # General shared utilities -│ └── CDPIUI.Shared.csproj # Shared library project definition -│ -├── CDPIUI.TrayIcon/ # System-tray companion application -│ ├── Assets/ # Tray application assets/icons -│ ├── ConditionalLaunch/ # Tray ConditionalLaunch integration -│ ├── Controls/ # Tray UI controls -│ ├── Forms/ # Windows Forms used by tray application -│ ├── Helper/ # Tray-specific helpers -│ ├── Properties/ # Project properties/resources -│ ├── Program.cs # Tray application entry point -│ ├── app.manifest # Tray executable manifest -│ └── CDPIUI.TrayIcon.csproj # Tray application project definition -│ -├── EditHostFile/ # Elevated helper used for hosts-file modification -│ ├── Data/ # Helper data/resources -│ ├── Properties/ # Project properties -│ ├── App.config # Application configuration -│ ├── Program.cs # Helper entry point -│ ├── app.manifest # Executable/UAC manifest -│ └── EditHostFile.csproj # EditHostFile project definition -│ -├── NewSetup/ # WiX-based application installer -│ ├── Resources/ # Installer resources -│ ├── NewSetup.wixproj # WiX installer project -│ ├── NewSetupFiles.wxs # Installed file/component definitions -│ ├── Product.wxs # Main installer/product definition -│ └── UI.wxs # Installer UI definition -│ -├── Update/ # Standalone updater application -│ ├── Properties/ # Updater project properties -│ ├── App.config # Updater configuration -│ ├── Form1.cs # Updater Windows Form logic -│ ├── Form1.Designer.cs # Generated Windows Form layout -│ ├── Form1.resx # Form resources -│ ├── InstallHelper.cs # Update/install helper logic -│ ├── Program.cs # Updater entry point -│ ├── packages.config # NuGet package configuration -│ └── Update.csproj # Updater project definition -│ -├── .editorconfig # Repository-wide editor/code formatting rules -├── .gitattributes # Git attribute configuration -├── .gitignore # Git ignore rules -├── CDPI.sln # Main Visual Studio solution -├── LICENSE.txt # Project license -└── README.md # Repository user-friendly README +├── .github/ # CI workflows and build scripts. Refer to .github/README.md. +├── CDPIUI/ # The GUI (WinUI 3) +│ ├── Assets/ # Images +│ ├── Commands/ # The cdpiui:// links: CommandUriMapper and CommandsHandler +│ ├── ConditionalLaunch/ # Translated lists of Conditional Launch triggers and actions +│ ├── Controls/ # User controls. Controls/Dialogs contains the content dialogs. +│ ├── Converters/ # XAML value converters +│ ├── ELUA/ # The license agreement. One Markdown file for each language (EN, RU). +│ ├── Extensions/ # Extension methods of the GUI +│ ├── Help/ # The help articles. One folder for each language (EN, RU). +│ ├── Helper/ # GUI helpers that use translations or XAML types +│ ├── Strings/ # The UI translations (.resw). One folder for each language (en-us, ru). +│ ├── Template/ # Files that the build copies to the output folder +│ ├── ViewModels/ # View models +│ ├── Views/ # Pages. The pages of one window are in a folder with the window name. +│ ├── Windows/ # Windows. The namespace is CDPIUI, not CDPIUI.Windows. +│ ├── App.xaml(.cs) # Application resources and start-up +│ └── Program.cs # Entry point. Controls the single instance. +├── CDPIUI.AddOns/ # Internal add-ons. One folder for each add-on. +├── CDPIUI.Core/ # Application logic of the GUI: components, presets, Store, LScript, proxy, updates +├── CDPIUI.Shared/ # Code for the GUI, Core and the tray: pipe, tasks, logs, settings, errors +├── CDPIUI.Tests/ # xUnit tests for Shared, Core and AddOns +├── CDPIUI.TrayIcon/ # The tray application (WinForms). It operates as administrator. +├── EditHostFile/ # The hosts file editor (.NET Framework 4.7.2). It operates as administrator. +├── NewSetup/ # The WiX 6 installer +├── Update/ # The patch installer (.NET Framework 4.7.2) +├── CDPI.sln +├── LICENSE.txt +├── README.md # The README for users (in Russian) +└── THIRD-PARTY-NOTICES.md ``` +## Change the application + +### General rules + +- Make small changes that have one purpose. +- Do not change these items without a good reason: the entry points (`Program.cs` of each project), the `cdpiui://` handler and the pipe protocol. +- If possible, add new code. Do not change code that operates correctly. +- Put new code in the folder for its function. +- Use correct English names. +- Some names go to the disk or through the pipe. Examples: settings keys, Store JSON fields, preset JSON fields and enum values in files. If you change the C# name, you must keep the stored value. Use `[JsonProperty]`, `[JsonPropertyName]` or a map. If you change a settings key, read the old key too. +- The pipe sends message types by name (`cdpiui:///`). If you change the name of a pipe enum member, you change the protocol. The GUI and the tray must then come from the same build. The other side cannot read a message with an unknown name. +- Add unit tests to `CDPIUI.Tests` for the logic in Shared, Core and AddOns. + +WARNING: The tray operates as administrator. Run files only from the installation folder or a CDPIUI data folder. Make sure that the files pass the checks in `CDPIUI.TrayIcon` (trusted paths, package signatures). Do not open files or links through the shell from the tray. + +### CDPIUI.TrayIcon + +The tray operates all the time and keeps the components in operation. The tray must be fast and it must always respond. + +- Do not block the UI thread of the tray. +- Keep the memory use of the tray low. + +### CDPIUI.Shared -#### Edit an application code +Shared is the common library of the GUI and the tray. -Just don't broke everything. DO NOT edit application entry point, uri handler or any other existed code without reason. If something not implemented best way is create something new except of editing something. -If it works – don't touch! +- Add code to Shared only when the GUI and the tray both use it. +- Put code for the GUI only in Core or in the GUI project. +- Before you change Shared, make sure that the other process does not use the old behavior. -#### Create new AddOn +### CDPIUI.Core -Application supports external and internal AddOns. External and internal AddOns needed some application code to work. +- Put new functions into the applicable folder, for example `Store`, `ComponentServices`, `LScript` or `Proxy`. +- The namespace `CDPIUI.Core.System` hides the namespace `System` in Core. Use `global::System` when necessary. -Difference is how much code you need to add in application. -External AddOns (like GoodCheck) usually has main (worker) part as compiled executable. In application you need to implement some code and UI to control AddOn settings and handle it work. +### Add-ons -Internal AddOns fully stored in CDPIUI.AddOns project. One AddOn – one project folder. -In main application you must add some XAML UI or UI helpers (for localization, as example) +The application has external add-ons and internal add-ons. Both types need some application code. -CDPIUI.AddOns referenced from CDPIUI.Shared and CDPIUI.Core. It means AddOn can access application core and shared utils directly (if requested class/method is public) +| Type | Location of the logic | Application code | +|---|---|---| +| External (for example GoodCheck) | An executable file from the Store | Code that sets the add-on, starts it through the tray and shows the results | +| Internal | A folder in `CDPIUI.AddOns` | Pages, dialogs and translations in the GUI | -### Apply changes to CDPIUI.TrayIcon +`CDPIUI.AddOns` has references to `CDPIUI.Core` and `CDPIUI.Shared`. An add-on can use their public types directly. Refer to [CDPIUI.AddOns/README.md](../CDPIUI.AddOns/README.md). -Are you really want it? Do it! -Just don't break everything. +### Add a component -Also, keep in mind – TrayIcon must be fast and responsible. This is the main rule. +Components, preset packs and subscriptions are Store items. -### Apply changes to CDPIUI.Shared +- To ask for a new item, use the developer channels. The application Telegram channel has the links. +- To add an item yourself, use the [Store repository](https://github.com/Storik4pro/cdpiui-store). -If you want fix something – make sure your fix won't break anything. -If you want add something – make sure you really want it. Shared – is a library for CDPIUI (GUI) and CDPIUI.TrayIcon (Back). If something used only in GUI, adding it to Shared isn't a good idea. +A new component also needs application code: -### Apply changes to CDPIUI.Core +1. Add the component ID to `CDPIUI.Core/Store/Data/HardcodedItemIds.cs`. +2. Add the component name to `ComponentNames` in `CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs`. Component names are only in English. -Make sure function you added stored in right directory (to keep structure understandable) +A new preset pack or a new subscription does not need code. -### Add new component +## Translations -You can request component addition to CDPIUI Store in official developer's social. All links can be found in application telegram channel. +The developer documents use the term "preset" for a ready settings file of a component. The English UI uses the word "Config" for a preset. The Russian UI uses the word "Пресет". -For add new component manually, create item in Store repo, add component Id to CDPIUI.Core.Store.Data.HardcodedItemIds. Then add locale key (view Localization paragraph) with id with localized name (EN only supported for component names) +To add a language, do these steps: -### Add new config kit/subscription +1. Make the `.resw` files in `CDPIUI/Strings/`. The code has the format `en-us` or `ru`. +2. Add the `.resw` files to `CDPIUI.csproj` as `Content` with `PreserveNewest`. +3. Translate the license agreement into `CDPIUI/ELUA//ELUA.md`. `` is the Store language code, for example `EN` or `RU`. +4. Translate the help articles into `CDPIUI/Help//`. +5. Translate the Store texts in the [Store repository](https://github.com/Storik4pro/cdpiui-store). +6. In the `Resources.resw` file of each language, add a string. The key is the language code. The value is the name of the language. +7. Add the language to `Languages` in `CDPIUI/Helper/UserExperience/LocalizationHelper.cs`. +8. In `InitializeLocalizer` in `CDPIUI/App.xaml.cs`, add the system culture of the new language. +9. For the tray, add a dictionary to `CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs`. +10. In `GetLocaleString` of the same file, add a case for the two-letter ISO code. -You can request config kit/subscription addition to CDPIUI Store in official developer's social. All links can be found in application telegram channel. +## Documents -For add new component manually, create item in Store repo. No code needed. +| Part | Document | +|---|---| +| GUI | [CDPIUI/README.md](../CDPIUI/README.md) | +| Core | [CDPIUI.Core/README.md](../CDPIUI.Core/README.md) | +| LScript language | [CDPIUI.Core/LScript/README.md](../CDPIUI.Core/LScript/README.md) | +| Shared | [CDPIUI.Shared/README.md](../CDPIUI.Shared/README.md) | +| Pipe protocol | [CDPIUI.Shared/Pipe/README.md](../CDPIUI.Shared/Pipe/README.md) | +| Tray | [CDPIUI.TrayIcon/README.md](../CDPIUI.TrayIcon/README.md) | +| Add-ons | [CDPIUI.AddOns/README.md](../CDPIUI.AddOns/README.md) | +| Tests | [CDPIUI.Tests/README.md](../CDPIUI.Tests/README.md) | +| Updater | [Update/README.md](../Update/README.md) | +| Hosts file editor | [EditHostFile/README.md](../EditHostFile/README.md) | +| Installer | [NewSetup/README.md](../NewSetup/README.md) | +| CI and releases | [.github/README.md](README.md) | diff --git a/.github/README.md b/.github/README.md new file mode 100644 index 00000000..329874b8 --- /dev/null +++ b/.github/README.md @@ -0,0 +1,232 @@ +# CI and releases + +The workflows are in `.github/workflows`. The scripts that they run are in `.github/scripts`. +All jobs run on `windows-latest`. [CONTRIBUTING.md](CONTRIBUTING.md) tells how to build locally. + +## CI (`workflows/ci.yml`) + +CI runs on pushes to `main`, `dev` and `fix/**`, and on pull requests. It has read-only +`contents` permission. A new run for the same ref cancels the current run. + +| Job | Steps | +|-----|-------| +| `libraries` | Installs the .NET 8 SDK. Runs `create_secret_stub.ps1`. Builds `CDPIUI.Shared`, `CDPIUI.Core` (x64), `CDPIUI.AddOns` and `CDPIUI.TrayIcon` (x64) in Release. Runs `dotnet test CDPIUI.Tests`. | +| `gui` | Runs `build_portable.ps1` and `check_setup_files.ps1`. Builds the MSI. Uploads `NewSetup/bin/**/*.msi` as the `setup-msi` artifact for 7 days. | +| `tools` | Builds `Update/Update.csproj` (with packages.config restore) and `EditHostFile/EditHostFile.csproj` in Release with `msbuild`. | + +The `gui` job has `continue-on-error`. The WinUI build needs the CommunityToolkit Labs feed and +the Windows App SDK tools. The job gives only information until the build is reproducible on +hosted runners. + +## Release files (`workflows/build_and_patch.yml`) + +A user starts this workflow manually (`workflow_dispatch`). + +### Inputs + +| Input | Default | Meaning | +|-------|---------|---------| +| `version` | empty | The version for `requirements.json`. If empty, the workflow uses the first `FileVersion` in `CDPIUI/CDPIUI.csproj`. The value must match `^\d+(\.\d+){1,3}$`. | +| `previous_repository` | `Storik4pro/cdpiui` | The repository whose release contains the previous `_portable.zip`. | +| `previous_tag` | empty | The release tag of the previous `_portable.zip`. If empty, the workflow uses the latest release. With `none`, the workflow makes a full patch. | +| `patch_urls` | empty | Patches that the application must install first, separated by spaces. The workflow writes them to `patch_urls`. | + +### Secrets + +| Secret | Use | +|--------|-----| +| `STORE_GITHUB_TOKEN`, `STORE_GITLAB_TOKEN` | The Store tokens in `Secret.cs` (`GitHubToken`, `GitLabToken`). | +| `STORE_PROXY_URL`, `STORE_PROXY_KEY` | The Store reverse proxy in `Secret.cs` (`ProxyURL`, `ProxyKey`). | +| `TRUSTED_CERTIFICATES` | The SHA-1 thumbprints for `Secret.TrustedCertificates`, separated by spaces, commas or semicolons. | +| `PIPE_AUTH_GUID` | The pipe secret `Secret.AuthGuid`. Keep the same value for all releases. | +| `CATALOG_CERTIFICATE_PFX_BASE64` | The PFX file of the catalog signature certificate, in Base64. The patch step writes it to `RUNNER_TEMP\catalog.pfx` and deletes it after the step. | +| `CATALOG_CERTIFICATE_PASSWORD` | The password of the PFX file. | + +All secrets are optional for the workflow. The "Create Secret.cs" step gives them to +`create_release_secret.ps1` as environment variables. The PFX goes in `CATALOG_CERTIFICATE`. + +WARNING: Set `CATALOG_CERTIFICATE_PFX_BASE64` and `CATALOG_CERTIFICATE_PASSWORD` before you make a +release. Without the certificate, the catalogs have no signature and the application rejects the +patch. + +WARNING: Make sure that `TrustedCertificates` is not empty in a release build. Set +`TRUSTED_CERTIFICATES`, or set the catalog certificate. With an empty list, the release build +rejects all signed catalogs, also its own patches. + +### Steps + +1. Write `CDPIUI.Shared/Secrets/Secret.cs` with `create_release_secret.ps1 -Force` (step "Create Secret.cs"). +2. Build the portable layout (`build_portable.ps1`). The script keeps the `Secret.cs` file of step 1. +3. Check the installer file list (`check_setup_files.ps1`). +4. Build the MSI. +5. Find the version. +6. Download the previous `_portable.zip` with `gh release download`. The workflow skips this step if `previous_tag` is `none`. +7. Make the patch with `make_patch.ps1`. +8. Make the portable archive: + 1. Copy the publish folder. + 2. Change the name of `Update.exe` to `_Update.exe`. + 3. Make a `catalog.cat` (version 2.0) of the full folder. + 4. Sign the catalog if the certificate is available. If the signature has no signer certificate, the step fails. + 5. Put the catalog in the folder and make the ZIP file. + +### Output + +The workflow makes the artifact `release-`. It contains these files: + +- `_patch.cdpipatch` +- `_portable.zip` +- `Setup.msi` + +The workflow does not make a GitHub release. + +## Scripts (`scripts/`) + +### `build_portable.ps1` + +Parameter: `-Configuration` (default `Release`). The script requires `msbuild` and the .NET 8 SDK. +The script does these steps: + +1. Run `create_secret_stub.ps1`. +2. Delete the old publish folder `CDPIUI/bin/x64//net8.0-windows10.0.22621.0/publish`. +3. Publish `CDPIUI/CDPIUI.csproj` to that folder (x64, `win-x64`, framework-dependent). +4. Remove the language folders that contain only `.mui` files, except `en-us` and `ru-ru`. The Windows App SDK copies approximately 90 languages. +5. Publish `CDPIUI.TrayIcon` as a single file to `artifacts/tools/tray`. +6. Build `Update` (x64) and `EditHostFile`. +7. Copy `CDPIUI_TrayIcon.exe`, `Update.exe` and `EditHostFile.exe` into the publish folder. + +### `check_setup_files.ps1` + +This script has no parameters. It compares two lists without case sensitivity: + +- the files in the Release publish folder, without `*.pdb` files; +- the `Source="$(var.SourceDir)\..."` entries in `NewSetup/NewSetupFiles.wxs`. + +The script fails if a file is missing from one of the lists. +[NewSetup/README.md](../NewSetup/README.md) gives the file list rule. + +### `create_secret_stub.ps1` + +This script has no parameters. Git ignores `CDPIUI.Shared/Secrets/Secret.cs`. The script does +these steps: + +- If the file does not exist, the script writes a `Secret` class with empty tokens, an empty + `TrustedCertificates` list and a random `AuthGuid`. With this class, the solution compiles. +- If the file exists and its `AuthGuid` is empty, the script writes a random `AuthGuid` into it. +- The script does not change other values of an existing file. + +The pipe rejects all clients when `AuthGuid` is empty. Thus the stub must have a value. + +### `create_release_secret.ps1` + +The release workflow runs this script. It writes `Secret.cs` from these environment variables: + +| Variable | Member | +|----------|--------| +| `STORE_GITHUB_TOKEN`, `STORE_GITLAB_TOKEN` | `GitHubToken`, `GitLabToken` | +| `STORE_PROXY_URL`, `STORE_PROXY_KEY` | `ProxyURL`, `ProxyKey` | +| `TRUSTED_CERTIFICATES` | `TrustedCertificates` | +| `CATALOG_CERTIFICATE`, `CATALOG_CERTIFICATE_PASSWORD` | `TrustedCertificates`, if `TRUSTED_CERTIFICATES` gives no thumbprint | +| `PIPE_AUTH_GUID` | `AuthGuid` | + +Rules: + +- The script keeps only thumbprints with 40 hexadecimal characters, in upper case. +- If `TRUSTED_CERTIFICATES` gives no thumbprint, the script uses the thumbprint of the PFX in `CATALOG_CERTIFICATE`. +- If there is no thumbprint, the script shows a warning. The build then rejects signed packages and patches. +- `AuthGuid` comes from `PIPE_AUTH_GUID`. Without it, the script derives the value from `CATALOG_CERTIFICATE` (SHA-256). Without both, the value is random and the script shows a warning. +- `AuthGuid` must stay the same from build to build. A new value changes `CDPIUI.Shared.dll` and the tray, and every patch then contains them. +- If `Secret.cs` exists, the script does not change it. With `-Force`, the script replaces it. + +### `make_patch.ps1` + +The script requires PowerShell 7. Windows PowerShell 5.1 can write `\` in ZIP entry names, and +the updater does not read these names as folders. + +| Parameter | Required | Meaning | +|-----------|----------|---------| +| `-CurrentDir` | Yes | The publish folder of the new build. | +| `-PreviousZip` | No | The previous `_portable.zip`. If empty, the patch contains all files. | +| `-Version` | Yes | The version for `requirements.json`, from `1.2` to `1.2.3.4`. | +| `-PatchUrls` | No | The `patch_urls` for `requirements.json`. | +| `-OutPatch` | No | The output file. The default is `patch.cdpipatch`. | +| `-CertificatePath`, `-CertificatePassword` | No | The PFX file that signs the catalog, and its password. | +| `-TimestampServer` | No | The timestamp server for `Set-AuthenticodeSignature`. The workflow does not use it. | + +Rules: + +- The patch contains a file if the previous build does not have it, or if its SHA-256 hash is different. +- The script ignores `catalog.cat` in the current folder. +- The script stores and compares `Update.exe` as `_Update.exe`. +- The script fails if no file changed. +- The script fails if the signature step fails. +- The script fails if `Test-FileCatalog` does not return `Valid` for the new catalog. + +## `.cdpipatch` format + +A `.cdpipatch` file is a ZIP archive with this structure: + +``` +requirements.json {"patch_urls": [...], "version": "x.y.z.w"} +CDPIUI/catalog.cat signed catalog (version 2.0) of all other files in CDPIUI/ +CDPIUI/... the changed files, at their installed paths +``` + +`make_patch.ps1` writes `requirements.json` in UTF-8 without BOM. The application reads it into +`PatchRequirementsModel` (`CDPIUI.Core/Store/Application/PatchRequirementsModel.cs`). + +## How the application applies a patch + +### GUI side + +`CDPIUI.Core/Store/Application/ApplicationUpdateService.cs` prepares the patch. For a portable +installation (`State.IsApplicationBuildAsSingleFile`), the Store puts the patch at +`//patch.cdpipatch`. `GetPatchReadyToInstall` then does these steps: + +1. Extract the patch to `Patches\` in the same folder. +2. Read `requirements.json`. An empty `version` causes an error. +3. Reject the patch if the installed version is the same or newer. +4. Verify `CDPIUI/catalog.cat` against the `CDPIUI/` folder. +5. Process each entry of `patch_urls`. The service skips empty entries. + 1. Download the required patch to `required/` in the patch folder. The index is 0 for the first entry, 1 for the next entry, and so on. + 2. Extract the full archive. + 3. Read the version from the `requirements.json` of the required patch. If the file or the version is not valid, the operation fails. + 4. If the installed version is the same or newer, skip the required patch. Also skip it if its version is the same as or newer than the version of the main patch, because the main patch is always the last layer. + 5. Verify the catalog of the `CDPIUI/` folder of the required patch. +6. Sort the required patches by version, the oldest first. The sequence in `patch_urls` is not important. +7. Write a new `patch.cdpipatch` with one layer for each patch (`layers/0/`, `layers/1/`, ...). + The main patch is the last layer. Each layer keeps its own `catalog.cat`. +8. Delete the `Patches` folder. + +When the Store operation completes without errors, the GUI sends the `BeginApplicationUpdate` +message to the tray (`CDPIUI.Core/Features/ApplicationUpdate.cs`). For an MSI installation, the +file is `patch.msi`. The tray installs it with `msiexec /i ... /qn+`. + +### Tray side + +`CDPIUI.TrayIcon/Helper/Utils.cs` and `CDPIUI.TrayIcon/Helper/PackageStaging.cs` do these steps: + +1. Check the path with `TrustedPaths.IsAllowed`. +2. Copy the file to `\.staging\\`. In a per-machine installation, only administrators can write to this folder. Thus nobody can replace the file between the check and the installation. +3. Extract the copy to `verify\`. +4. Verify the catalog of each `layers/*` folder. If there are no layers, verify the `CDPIUI/` folder. +5. If a check fails, stop and show the `UpdateFailure` notification. +6. Start `Update.exe --directory-to-zip "" --destination-directory ""`. + +[Update/README.md](../Update/README.md) describes `Update.exe`. The tray deletes the `.staging` +folder at each start. + +`Update.exe` does not copy a `catalog.cat` file from a patch into the installation folder. It also +does not remove files that are not in the patch, because a patch contains only the changed files. + +### Catalog check + +`CDPIUI.Shared/Security/CatalogVerifier.cs` checks a catalog with these rules: + +- The catalog must be a CMS signed message with one or more signers. +- The SHA-1 thumbprint of each signer certificate must be in `Secret.TrustedCertificates`. +- The signature of each signer must be valid. +- The verifier does not build the certificate chain, because the certificates are pinned and self-signed. +- `Test-FileCatalog ... -FilesToSkip catalog.cat` must return `Valid` in a maximum of 2 minutes. + +The verifier runs `Test-FileCatalog` in `%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe`. +It gives the paths through environment variables. diff --git a/.github/scripts/build_portable.ps1 b/.github/scripts/build_portable.ps1 new file mode 100644 index 00000000..6d38c28d --- /dev/null +++ b/.github/scripts/build_portable.ps1 @@ -0,0 +1,63 @@ +<# +.SYNOPSIS +Builds the x64 release layout of CDPI UI: the GUI publish folder with the tray, Update.exe and +EditHostFile.exe next to it. NewSetup/NewSetupFiles.wxs takes its files from the same folder. +Needs msbuild (Visual Studio or microsoft/setup-msbuild) and the .NET 8 SDK. +#> +param( + [string] $Configuration = "Release" +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +$root = (Resolve-Path (Join-Path $PSScriptRoot "../..")).Path +$publish = Join-Path $root "CDPIUI/bin/x64/$Configuration/net8.0-windows10.0.22621.0/publish" +$tools = Join-Path $root "artifacts/tools" + +function Invoke-Checked([string] $name, [scriptblock] $command) { + Write-Output "== $name" + & $command + if ($LASTEXITCODE -ne 0) { throw "$name failed with exit code $LASTEXITCODE" } +} + +& (Join-Path $PSScriptRoot "create_secret_stub.ps1") + +if (Test-Path -LiteralPath $publish) { Remove-Item -LiteralPath $publish -Recurse -Force } + +Invoke-Checked "Publish CDPIUI" { + msbuild (Join-Path $root "CDPIUI/CDPIUI.csproj") /restore /t:Publish /m /nologo /v:minimal ` + "/p:Configuration=$Configuration" /p:Platform=x64 /p:RuntimeIdentifier=win-x64 /p:SelfContained=false ` + "/p:PublishDir=$publish\" +} + +# The Windows App SDK copies its .mui files for about 90 languages regardless of +# SatelliteResourceLanguages; the application and the MSI have English and Russian only. +$keptLanguages = @("en-us", "ru-ru") +Get-ChildItem -LiteralPath $publish -Directory | + Where-Object { $keptLanguages -notcontains $_.Name.ToLowerInvariant() } | + Where-Object { + $files = @(Get-ChildItem -LiteralPath $_.FullName -File -Recurse) + $files.Count -gt 0 -and @($files | Where-Object { $_.Extension -ne ".mui" }).Count -eq 0 + } | + Remove-Item -Recurse -Force + +Invoke-Checked "Publish CDPIUI.TrayIcon" { + dotnet publish (Join-Path $root "CDPIUI.TrayIcon/CDPIUI.TrayIcon.csproj") -c $Configuration -p:Platform=x64 ` + -r win-x64 --self-contained false -p:PublishSingleFile=true -o (Join-Path $tools "tray") +} + +Invoke-Checked "Build Update" { + msbuild (Join-Path $root "Update/Update.csproj") /restore /p:RestorePackagesConfig=true /m /nologo /v:minimal ` + "/p:SolutionDir=$root\" "/p:Configuration=$Configuration" /p:Platform=x64 +} + +Invoke-Checked "Build EditHostFile" { + msbuild (Join-Path $root "EditHostFile/EditHostFile.csproj") /m /nologo /v:minimal "/p:Configuration=$Configuration" +} + +Copy-Item -LiteralPath (Join-Path $tools "tray/CDPIUI_TrayIcon.exe") -Destination $publish +Copy-Item -LiteralPath (Join-Path $root "Update/bin/x64/$Configuration/Update.exe") -Destination $publish +Copy-Item -LiteralPath (Join-Path $root "EditHostFile/bin/$Configuration/EditHostFile.exe") -Destination $publish + +Write-Output "Portable layout: $publish" diff --git a/.github/scripts/check_setup_files.ps1 b/.github/scripts/check_setup_files.ps1 new file mode 100644 index 00000000..04a71c52 --- /dev/null +++ b/.github/scripts/check_setup_files.ps1 @@ -0,0 +1,26 @@ +<# +.SYNOPSIS +Compares the publish folder with the file list of NewSetup/NewSetupFiles.wxs. WiX fails on +listed files that are missing, but a published file that is not listed would silently be left +out of the MSI. +#> +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +$root = (Resolve-Path (Join-Path $PSScriptRoot "../..")).Path +$wxs = Join-Path $root "NewSetup/NewSetupFiles.wxs" +$publish = Join-Path $root "CDPIUI/bin/x64/Release/net8.0-windows10.0.22621.0/publish" + +$listed = [regex]::Matches((Get-Content -Raw $wxs), 'Source="\$\(var\.SourceDir\)\\([^"]+)"') | + ForEach-Object { $_.Groups[1].Value.ToLowerInvariant() } +$published = Get-ChildItem -LiteralPath $publish -Recurse -File | + Where-Object { $_.Extension -ne ".pdb" } | + ForEach-Object { $_.FullName.Substring($publish.Length).TrimStart('\', '/').Replace('/', '\').ToLowerInvariant() } + +$notListed = @($published | Where-Object { $listed -notcontains $_ }) +$notPublished = @($listed | Where-Object { $published -notcontains $_ }) + +$notListed | ForEach-Object { Write-Output "Published but not in NewSetupFiles.wxs: $_" } +$notPublished | ForEach-Object { Write-Output "In NewSetupFiles.wxs but not published: $_" } +if ($notListed.Count + $notPublished.Count -gt 0) { throw "NewSetupFiles.wxs does not match the publish folder." } +Write-Output "NewSetupFiles.wxs matches the publish folder ($($listed.Count) files)." diff --git a/.github/scripts/create_release_secret.ps1 b/.github/scripts/create_release_secret.ps1 new file mode 100644 index 00000000..148beee9 --- /dev/null +++ b/.github/scripts/create_release_secret.ps1 @@ -0,0 +1,93 @@ +<# +.SYNOPSIS +Writes CDPIUI.Shared/Secrets/Secret.cs for a release build from environment variables. + +The stub of create_secret_stub.ps1 has an empty TrustedCertificates list, and a build with it +rejects every signed catalog, also its own patches. This script fills the list. + +Environment variables (all optional): + STORE_GITHUB_TOKEN, STORE_GITLAB_TOKEN Store repository tokens + STORE_PROXY_URL, STORE_PROXY_KEY Store reverse proxy + TRUSTED_CERTIFICATES SHA-1 thumbprints, separated by spaces, commas or semicolons + CATALOG_CERTIFICATE Base64 PFX that signs the catalogs. If TRUSTED_CERTIFICATES + CATALOG_CERTIFICATE_PASSWORD is empty, the thumbprint of this certificate is used. + PIPE_AUTH_GUID The pipe secret. It must stay the same from build to build: + a new value changes CDPIUI.Shared.dll and the tray, and every + patch then contains them. Without it, the value is derived + from the catalog certificate, or is random as a last choice. +#> +param( + [switch] $Force +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +$path = Join-Path $PSScriptRoot '../../CDPIUI.Shared/Secrets/Secret.cs' +if ((Test-Path -LiteralPath $path) -and -not $Force) { + Write-Output "Secret.cs already exists; use -Force to replace it." + return +} + +function Get-Env([string] $name) { + $value = [Environment]::GetEnvironmentVariable($name) + if ($null -eq $value) { return "" } + return $value +} + +# A C# string literal: the values come from secrets and must not break the source. +function ConvertTo-CSharpString([string] $value) { + return '"' + $value.Replace('\', '\\').Replace('"', '\"').Replace("`r", '').Replace("`n", '') + '"' +} + +$thumbprints = @((Get-Env 'TRUSTED_CERTIFICATES') -split '[\s,;]+' | + ForEach-Object { ($_ -replace '[^0-9A-Fa-f]', '').ToUpperInvariant() } | + Where-Object { $_.Length -eq 40 }) + +if ($thumbprints.Count -eq 0 -and (Get-Env 'CATALOG_CERTIFICATE')) { + $bytes = [Convert]::FromBase64String((Get-Env 'CATALOG_CERTIFICATE')) + $cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new( + $bytes, (Get-Env 'CATALOG_CERTIFICATE_PASSWORD'), + [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::EphemeralKeySet) + $thumbprints = @($cert.Thumbprint) + $cert.Dispose() +} + +if ($thumbprints.Count -eq 0) { + Write-Warning "No trusted certificate: the build will reject signed packages and patches." +} + +$list = ($thumbprints | ForEach-Object { ConvertTo-CSharpString $_ }) -join ', ' + +$authGuid = Get-Env 'PIPE_AUTH_GUID' +$parsedGuid = [Guid]::Empty +if ([Guid]::TryParse($authGuid, [ref] $parsedGuid)) { + $authGuid = $parsedGuid.ToString() +} elseif (Get-Env 'CATALOG_CERTIFICATE') { + # Stable for the same certificate and not readable from the public certificate data. + $hash = [System.Security.Cryptography.SHA256]::HashData( + [Text.Encoding]::UTF8.GetBytes('cdpiui-pipe:' + (Get-Env 'CATALOG_CERTIFICATE'))) + $authGuid = ([Guid]::new([byte[]]$hash[0..15])).ToString() +} else { + Write-Warning "No PIPE_AUTH_GUID: a random pipe secret makes every patch contain CDPIUI.Shared.dll and the tray." + $authGuid = [Guid]::NewGuid().ToString() +} + +@" +using System.Collections.Generic; + +namespace CDPIUI.Shared.Secrets +{ + public static class Secret + { + public static string GitHubToken = $(ConvertTo-CSharpString (Get-Env 'STORE_GITHUB_TOKEN')); + public static string GitLabToken = $(ConvertTo-CSharpString (Get-Env 'STORE_GITLAB_TOKEN')); + public const string ProxyURL = $(ConvertTo-CSharpString (Get-Env 'STORE_PROXY_URL')); + public const string ProxyKey = $(ConvertTo-CSharpString (Get-Env 'STORE_PROXY_KEY')); + public static string AuthGuid = "$authGuid"; + public static readonly List TrustedCertificates = [$list]; + } +} +"@ | Set-Content -LiteralPath $path -Encoding utf8 + +Write-Output ("Secret.cs written with {0} trusted certificate(s)." -f $thumbprints.Count) diff --git a/.github/scripts/create_secret_stub.ps1 b/.github/scripts/create_secret_stub.ps1 new file mode 100644 index 00000000..b04cbf39 --- /dev/null +++ b/.github/scripts/create_secret_stub.ps1 @@ -0,0 +1,30 @@ +# Creates CDPIUI.Shared/Secrets/Secret.cs with empty values for a local or CI build. +# The pipe rejects an empty AuthGuid, so the stub gets a random one. An older stub with an +# empty AuthGuid is updated in place. +$path = Join-Path $PSScriptRoot '../../CDPIUI.Shared/Secrets/Secret.cs' +$guid = [Guid]::NewGuid().ToString() + +if (Test-Path $path) { + $text = Get-Content -Raw -Path $path + if ($text -match 'AuthGuid\s*=\s*""') { + ($text -replace 'AuthGuid\s*=\s*""', "AuthGuid = `"$guid`"") | Set-Content -Path $path -Encoding utf8 -NoNewline + } + return +} + +@" +using System.Collections.Generic; + +namespace CDPIUI.Shared.Secrets +{ + public static class Secret + { + public static string GitHubToken = ""; + public static string GitLabToken = ""; + public const string ProxyURL = ""; + public const string ProxyKey = ""; + public static string AuthGuid = "$guid"; + public static readonly List TrustedCertificates = []; + } +} +"@ | Set-Content -Path $path -Encoding utf8 diff --git a/.github/scripts/make_patch.ps1 b/.github/scripts/make_patch.ps1 index 2071fe34..b723a9f7 100644 --- a/.github/scripts/make_patch.ps1 +++ b/.github/scripts/make_patch.ps1 @@ -1,112 +1,108 @@ +# On Windows PowerShell 5.1 (.NET Framework 4.x) ZipFile may store "\" in entry names, +# which the updater does not read as folders; PowerShell 7 always writes "/". +#Requires -Version 7.0 +<# +.SYNOPSIS +Creates a .cdpipatch from a portable build, in the format ApplicationUpdateService expects: + + requirements.json {"patch_urls": [...], "version": "x.y.z.w"} + CDPIUI/catalog.cat catalog of every other file in CDPIUI/ + CDPIUI/... files that differ from the previous portable build + +Update.exe cannot replace itself, so a new updater is shipped as _Update.exe and the tray +renames it on its next start. + +The application accepts only catalogs signed by a pinned certificate. Pass that certificate +with -CertificatePath, otherwise the patch is built with an unsigned catalog and is rejected. +#> param( - [Parameter(Mandatory=$true)] [string] $CurrentZip, - [string] $PrevZip = "", + [Parameter(Mandatory = $true)] [string] $CurrentDir, + [string] $PreviousZip = "", + [Parameter(Mandatory = $true)] [string] $Version, + [string[]] $PatchUrls = @(), [string] $OutPatch = "patch.cdpipatch", - [string] $Version = "", - [string] $DownloadUrl = "" + [string] $CertificatePath = "", + [string] $CertificatePassword = "", + [string] $TimestampServer = "" ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" -$work = Join-Path $env:TEMP ("cdpipatch_" + [Guid]::NewGuid().ToString()) -$curDir = Join-Path $work "current" +if (-not (Test-Path -LiteralPath $CurrentDir -PathType Container)) { throw "CurrentDir not found: $CurrentDir" } +if ($Version -notmatch '^\d+(\.\d+){1,3}$') { throw "Version must look like 1.2.3.4: $Version" } + +$CurrentDir = (Resolve-Path -LiteralPath $CurrentDir).Path +$work = Join-Path ([IO.Path]::GetTempPath()) ("cdpipatch_" + [Guid]::NewGuid().ToString("N")) $prevDir = Join-Path $work "prev" $payload = Join-Path $work "payload" - -New-Item -ItemType Directory -Path $curDir,$prevDir,$payload -Force | Out-Null - -Write-Output "Extracting current zip: $CurrentZip" -if (-not (Test-Path $CurrentZip)) { throw "CurrentZip not found: $CurrentZip" } -Expand-Archive -Path $CurrentZip -DestinationPath $curDir -Force - -if (-not [string]::IsNullOrWhiteSpace($PrevZip) -and (Test-Path $PrevZip)) { - Write-Output "Extracting previous zip: $PrevZip" - Expand-Archive -Path $PrevZip -DestinationPath $prevDir -Force -} else { - Write-Output "No previous zip provided or not found — payload will include all files." -} - -# helper to get relative path with forward slashes -function Get-RelPath([string]$base, [string]$full) { - $rel = $full.Substring($base.Length).TrimStart('\','/') - return $rel -replace '\\','/' -} - -$changedFiles = @() - -Get-ChildItem -Path $curDir -Recurse -File | ForEach-Object { - $cur = $_.FullName - $rel = Get-RelPath $curDir $cur - $prevPath = Join-Path $prevDir $rel - $copy = $true - if (Test-Path $prevPath) { - $h1 = (Get-FileHash -Path $cur -Algorithm SHA256).Hash - $h2 = (Get-FileHash -Path $prevPath -Algorithm SHA256).Hash - if ($h1 -eq $h2) { $copy = $false } - } - if ($copy) { - $dest = Join-Path $payload $rel - $destDir = Split-Path $dest - if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null } - Copy-Item -Path $cur -Destination $dest -Force - $changedFiles += $rel +$layer = Join-Path $payload "CDPIUI" +New-Item -ItemType Directory -Path $prevDir, $layer -Force | Out-Null + +try { + if (-not [string]::IsNullOrWhiteSpace($PreviousZip)) { + if (-not (Test-Path -LiteralPath $PreviousZip)) { throw "PreviousZip not found: $PreviousZip" } + Write-Output "Comparing with $PreviousZip" + Expand-Archive -LiteralPath $PreviousZip -DestinationPath $prevDir -Force + } else { + Write-Output "No previous build: the patch contains every file." } -} -Write-Output ("Changed files count: {0}" -f $changedFiles.Count) -if ($changedFiles.Count -eq 0) { - Write-Output "No changed files detected. Creating an empty patch (requirements only)." -} + $changed = New-Object System.Collections.Generic.List[string] + foreach ($file in Get-ChildItem -LiteralPath $CurrentDir -Recurse -File) { + $rel = $file.FullName.Substring($CurrentDir.Length).TrimStart('\', '/') + if ($rel -ieq "catalog.cat") { continue } + $target = if ($rel -ieq "Update.exe") { "_Update.exe" } else { $rel } + + $prevPath = Join-Path $prevDir $target + if ((Test-Path -LiteralPath $prevPath) -and + (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash -eq (Get-FileHash -LiteralPath $prevPath -Algorithm SHA256).Hash) { + continue + } + + $dest = Join-Path $layer $target + New-Item -ItemType Directory -Path (Split-Path $dest) -Force | Out-Null + Copy-Item -LiteralPath $file.FullName -Destination $dest + $changed.Add($target) + } -if ([string]::IsNullOrWhiteSpace($Version)) { - # try to read VERSION file from current dir root - $vfile = Join-Path $curDir "VERSION" - if (Test-Path $vfile) { - $Version = (Get-Content $vfile -Raw).Trim() + if ($changed.Count -eq 0) { throw "Nothing changed since the previous build." } + Write-Output ("Changed files: {0}" -f $changed.Count) + $changed | ForEach-Object { Write-Output " - $_" } + + # The catalog is created outside the layer so that it does not list itself. + $catalog = Join-Path $work "catalog.cat" + New-FileCatalog -Path $layer -CatalogFilePath $catalog -CatalogVersion 2.0 | Out-Null + + if (-not [string]::IsNullOrWhiteSpace($CertificatePath)) { + $password = ConvertTo-SecureString $CertificatePassword -AsPlainText -Force + $cert = Get-PfxCertificate -FilePath $CertificatePath -Password $password + $signArgs = @{ FilePath = $catalog; Certificate = $cert; HashAlgorithm = "SHA256" } + if (-not [string]::IsNullOrWhiteSpace($TimestampServer)) { $signArgs.TimestampServer = $TimestampServer } + $signature = Set-AuthenticodeSignature @signArgs + if ($null -eq $signature.SignerCertificate) { throw "Cannot sign the catalog: $($signature.StatusMessage)" } + Write-Output "Catalog signed by $($signature.SignerCertificate.Subject) ($($signature.SignerCertificate.Thumbprint))" } else { - # fallback to timestamp - $Version = (Get-Date).ToString("yyyyMMddHHmm") + Write-Warning "No certificate given: the catalog is unsigned and the application will refuse this patch." } -} - -if ([string]::IsNullOrWhiteSpace($DownloadUrl)) { - # by default leave blank or put placeholder - $DownloadUrl = "" -} - -$reqPath = Join-Path $payload "requirements.json" -$lines = @() -$lines += "{" -if ([string]::IsNullOrWhiteSpace($DownloadUrl)) { - # if no url provided, still write version with empty url - $lines += " { version = `"$Version`", url=`""`" }" -} else { - $lines += " { version = `"$Version`", url=`"$DownloadUrl`" }" -} -$lines += "}" -$lines -join "`r`n" | Out-File -FilePath $reqPath -Encoding utf8 + Move-Item -LiteralPath $catalog -Destination (Join-Path $layer "catalog.cat") -Write-Output "Wrote requirements in custom format to: $reqPath" + $check = Test-FileCatalog -CatalogFilePath (Join-Path $layer "catalog.cat") -Path $layer -FilesToSkip catalog.cat + if ($check -ne "Valid") { throw "The new catalog does not match the patch files: $check" } -$tmpZip = Join-Path $work "patch_payload.zip" -if (Test-Path $tmpZip) { Remove-Item $tmpZip -Force } - -if ((Get-ChildItem -Path $payload -Recurse -File | Measure-Object).Count -eq 0) { - New-Item -ItemType File -Path (Join-Path $payload "._no_changes.txt") -Force | Out-Null + $requirements = [ordered]@{ + patch_urls = @($PatchUrls | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }) + version = $Version + } + $json = ConvertTo-Json -InputObject $requirements -Depth 3 + [IO.File]::WriteAllText((Join-Path $payload "requirements.json"), $json, (New-Object System.Text.UTF8Encoding($false))) + + $outPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutPatch) + if (Test-Path -LiteralPath $outPath) { Remove-Item -LiteralPath $outPath -Force } + Add-Type -AssemblyName System.IO.Compression.FileSystem + [IO.Compression.ZipFile]::CreateFromDirectory($payload, $outPath) + Write-Output "Patch created: $OutPatch" } - -Add-Type -AssemblyName System.IO.Compression.FileSystem -[System.IO.Compression.ZipFile]::CreateFromDirectory($payload, $tmpZip) - -Copy-Item -Path $tmpZip -Destination $OutPatch -Force -Write-Output "Patch created: $OutPatch" - -if ($changedFiles.Count -gt 0) { - Write-Output "Files included in patch:" - $changedFiles | ForEach-Object { Write-Output " - $_" } -} else { - Write-Output "Patch contains only requirements / marker file (no changed binaries)." +finally { + Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue } - -# Remove-Item -Recurse -Force $work diff --git a/.github/workflows/build_and_patch.yml b/.github/workflows/build_and_patch.yml index d9ddc93e..443a6660 100644 --- a/.github/workflows/build_and_patch.yml +++ b/.github/workflows/build_and_patch.yml @@ -1,14 +1,22 @@ -name: Build portable and create patch (manual) +name: Build release files (manual) on: workflow_dispatch: inputs: - update_url: - description: '(optional) URL to update.exe (will be downloaded and included). If empty, uses Update/ from repo.' + version: + description: 'Version for requirements.json (empty: FileVersion of CDPIUI.csproj)' required: false default: '' - version: - description: '(optional) version string for requirements (if empty, workflow will try to infer)' + previous_repository: + description: 'Repository whose release holds the previous _portable.zip' + required: false + default: 'Storik4pro/cdpiui' + previous_tag: + description: 'Release tag of the previous _portable.zip (empty: latest release, "none": full patch)' + required: false + default: '' + patch_urls: + description: 'Patches that must be installed first, separated by spaces (requirements.json patch_urls)' required: false default: '' @@ -16,126 +24,117 @@ permissions: contents: read jobs: - build-and-patch: + build: runs-on: windows-latest - env: - DOTNET_ROOT: C:\hostedtoolcache\windows\dotnet steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 + - uses: actions/checkout@v4 - - name: Setup .NET 8 - uses: actions/setup-dotnet@v3 + - uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - - name: Restore solutions (if needed) - run: dotnet restore - - - name: Publish CDPIUI_TrayIcon + - uses: microsoft/setup-msbuild@v2 + + # Without this step build_portable.ps1 writes the stub, whose empty TrustedCertificates + # list makes the release reject every signed catalog, also its own patches. + - name: Create Secret.cs + shell: pwsh + env: + STORE_GITHUB_TOKEN: ${{ secrets.STORE_GITHUB_TOKEN }} + STORE_GITLAB_TOKEN: ${{ secrets.STORE_GITLAB_TOKEN }} + STORE_PROXY_URL: ${{ secrets.STORE_PROXY_URL }} + STORE_PROXY_KEY: ${{ secrets.STORE_PROXY_KEY }} + TRUSTED_CERTIFICATES: ${{ secrets.TRUSTED_CERTIFICATES }} + PIPE_AUTH_GUID: ${{ secrets.PIPE_AUTH_GUID }} + CATALOG_CERTIFICATE: ${{ secrets.CATALOG_CERTIFICATE_PFX_BASE64 }} + CATALOG_CERTIFICATE_PASSWORD: ${{ secrets.CATALOG_CERTIFICATE_PASSWORD }} + run: ./.github/scripts/create_release_secret.ps1 -Force + + - name: Build portable layout + shell: pwsh + run: ./.github/scripts/build_portable.ps1 + + - name: Check installer file list + shell: pwsh + run: ./.github/scripts/check_setup_files.ps1 + + - name: Build Setup.msi + run: dotnet build NewSetup/NewSetup.wixproj -c Release -p:Platform=x64 + + - name: Resolve version + id: version + shell: pwsh + env: + INPUT_VERSION: ${{ inputs.version }} run: | - dotnet publish ./CDPIUI_TrayIcon/CDPIUI_TrayIcon.csproj -c Release -r win-x64 -o ./artifacts/CDPIUI_TrayIcon/publish - - - name: Publish GoodbyeDPI_UI + $version = $env:INPUT_VERSION + if ([string]::IsNullOrWhiteSpace($version)) { + $version = ([xml](Get-Content CDPIUI/CDPIUI.csproj)).Project.PropertyGroup.FileVersion | Where-Object { $_ } | Select-Object -First 1 + } + if ($version -notmatch '^\d+(\.\d+){1,3}$') { throw "Invalid version: $version" } + "version=$version" >> $env:GITHUB_OUTPUT + + - name: Download previous _portable.zip + shell: pwsh + env: + GH_TOKEN: ${{ github.token }} + PREVIOUS_REPOSITORY: ${{ inputs.previous_repository }} + PREVIOUS_TAG: ${{ inputs.previous_tag }} run: | - dotnet publish ./GoodbyeDPI_UI/GoodbyeDPI_UI.csproj -c Release -r win-x64 -o ./artifacts/GoodbyeDPI_UI/publish - - # Handle Update: prefer explicit URL supplied by user, otherwise use Update/ from repo - - name: Prepare Update (from URL if provided) - shell: powershell + if ($env:PREVIOUS_TAG -eq 'none') { exit 0 } + $tagArgs = @() + if (-not [string]::IsNullOrWhiteSpace($env:PREVIOUS_TAG)) { $tagArgs = @($env:PREVIOUS_TAG) } + gh release download @tagArgs --repo $env:PREVIOUS_REPOSITORY --pattern _portable.zip --output prev_portable.zip + if ($LASTEXITCODE -ne 0) { throw "Cannot download _portable.zip from $env:PREVIOUS_REPOSITORY" } + + - name: Create portable zip and patch + shell: pwsh + env: + VERSION: ${{ steps.version.outputs.version }} + PATCH_URLS: ${{ inputs.patch_urls }} + CATALOG_CERTIFICATE: ${{ secrets.CATALOG_CERTIFICATE_PFX_BASE64 }} + CATALOG_CERTIFICATE_PASSWORD: ${{ secrets.CATALOG_CERTIFICATE_PASSWORD }} run: | - $updateUrl = "${{ github.event.inputs.update_url }}" - $outDir = "artifacts/Update" - Remove-Item -Recurse -Force $outDir -ErrorAction SilentlyContinue - if (-not [string]::IsNullOrWhiteSpace($updateUrl)) { - New-Item -ItemType Directory -Path $outDir | Out-Null - $fileName = Split-Path $updateUrl -Leaf - $dest = Join-Path $outDir $fileName - Write-Output "Downloading update from $updateUrl to $dest" - Invoke-WebRequest -Uri $updateUrl -OutFile $dest -UseBasicParsing -Headers @{ 'Accept' = '*/*' } - } elseif (Test-Path Update) { - robocopy Update artifacts\Update /MIR | Out-Null - } else { - Write-Output "No Update provided and Update/ not found in repo. Continuing without Update." - } + $publish = "CDPIUI/bin/x64/Release/net8.0-windows10.0.22621.0/publish" + New-Item -ItemType Directory -Force out | Out-Null - - name: Prepare portable structure - shell: powershell - run: | - Remove-Item -Recurse -Force ./portable_tmp -ErrorAction SilentlyContinue - New-Item -ItemType Directory -Path ./portable_tmp | Out-Null - robocopy artifacts\CDPIUI_TrayIcon\publish portable_tmp\CDPIUI_TrayIcon /MIR | Out-Null - robocopy artifacts\GoodbyeDPI_UI\publish portable_tmp\GoodbyeDPI_UI /MIR | Out-Null - if (Test-Path artifacts\Update) { - robocopy artifacts\Update portable_tmp\Update /MIR | Out-Null + $certPath = "" + if ($env:CATALOG_CERTIFICATE) { + $certPath = Join-Path $env:RUNNER_TEMP "catalog.pfx" + [IO.File]::WriteAllBytes($certPath, [Convert]::FromBase64String($env:CATALOG_CERTIFICATE)) } - - name: Create _portable.zip - shell: powershell - run: | - Remove-Item -Force _portable.zip -ErrorAction SilentlyContinue - Add-Type -AssemblyName System.IO.Compression.FileSystem - [System.IO.Compression.ZipFile]::CreateFromDirectory("portable_tmp", "_portable.zip") - Write-Output "Created _portable.zip" - - # Find previous _portable.zip in repo releases (if any) - - name: Find previous release portable asset (if exists) - id: find_prev - uses: actions/github-script@v6 - with: - script: | - const releases = await github.rest.repos.listReleases({ - owner: context.repo.owner, - repo: context.repo.repo, - per_page: 20 - }); - for (const r of releases.data) { - if (r.draft) continue; - // find first release (most recent) that has an asset name containing "_portable.zip" - const asset = r.assets?.find(a => a.name && a.name.includes('_portable.zip')); - if (asset) { - return { url: asset.browser_download_url, tag: r.tag_name }; - } + try { + $prev = if (Test-Path prev_portable.zip) { "prev_portable.zip" } else { "" } + $urls = @($env:PATCH_URLS -split '\s+' | Where-Object { $_ }) + ./.github/scripts/make_patch.ps1 -CurrentDir $publish -PreviousZip $prev -Version $env:VERSION ` + -PatchUrls $urls -OutPatch "out/$($env:VERSION)_patch.cdpipatch" ` + -CertificatePath $certPath -CertificatePassword $env:CATALOG_CERTIFICATE_PASSWORD + + # The portable build ships the updater as _Update.exe like the published one, + # with a catalog of the whole folder. + $portable = Join-Path $env:RUNNER_TEMP "portable" + Copy-Item -Recurse $publish $portable + Move-Item (Join-Path $portable "Update.exe") (Join-Path $portable "_Update.exe") + $catalog = Join-Path $env:RUNNER_TEMP "catalog.cat" + New-FileCatalog -Path $portable -CatalogFilePath $catalog -CatalogVersion 2.0 | Out-Null + if ($certPath) { + $cert = Get-PfxCertificate -FilePath $certPath -Password (ConvertTo-SecureString $env:CATALOG_CERTIFICATE_PASSWORD -AsPlainText -Force) + $signature = Set-AuthenticodeSignature -FilePath $catalog -Certificate $cert -HashAlgorithm SHA256 + if ($null -eq $signature.SignerCertificate) { throw "Cannot sign the portable catalog: $($signature.StatusMessage)" } } - return null; - - - name: Download previous _portable.zip (if found) - if: steps.find_prev.outputs.result != 'null' - shell: powershell - run: | - $obj = ConvertFrom-Json '${{ steps.find_prev.outputs.result }}' - $url = $obj.url - Write-Output "Downloading previous portable from $url" - # Use GITHUB_TOKEN for authenticated download, in case private assets - Invoke-WebRequest -Uri $url -OutFile prev_portable.zip -UseBasicParsing -Headers @{ Authorization = "token $env:GITHUB_TOKEN" } - - - name: Ensure scripts directory exists - run: mkdir -Force .\.github\scripts - - - name: Save make_patch.ps1 to scripts (if not committed) - run: | - echo "Assumes .github/scripts/make_patch.ps1 is present in repo. If not, add it." + Move-Item $catalog (Join-Path $portable "catalog.cat") + Compress-Archive -Path "$portable/*" -DestinationPath out/_portable.zip + } + finally { + if ($certPath) { Remove-Item $certPath -Force -ErrorAction SilentlyContinue } + } - - name: Run patch creation script - shell: powershell - run: | - $prev = "" - if (Test-Path prev_portable.zip) { $prev = "prev_portable.zip" } - $verInput = "${{ github.event.inputs.version }}" - $cmd = ".\.github\scripts\make_patch.ps1 -CurrentZip _portable.zip -PrevZip $prev -OutPatch patch.cdpipatch -Version `"$verInput`" -DownloadUrl `""`"" - Write-Output "Running: $cmd" - pwsh -NoProfile -ExecutionPolicy Bypass $cmd - - - name: Upload _portable.zip artifact - uses: actions/upload-artifact@v4 - with: - name: portable_zip - path: _portable.zip + $msi = Get-ChildItem NewSetup/bin -Recurse -Filter *.msi | Select-Object -First 1 + if (-not $msi) { throw "Setup.msi was not built" } + Copy-Item $msi.FullName out/Setup.msi - - name: Upload patch artifact - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@v4 with: - name: patch_cdpipatch - path: patch.cdpipatch + name: release-${{ steps.version.outputs.version }} + path: out/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..d098753a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,82 @@ +name: CI + +on: + push: + branches: [ main, dev, 'fix/**' ] + pull_request: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +jobs: + libraries: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Create build secrets stub + shell: pwsh + run: ./.github/scripts/create_secret_stub.ps1 + + - name: Build + run: | + dotnet build CDPIUI.Shared/CDPIUI.Shared.csproj -c Release + dotnet build CDPIUI.Core/CDPIUI.Core.csproj -c Release -p:Platform=x64 + dotnet build CDPIUI.AddOns/CDPIUI.AddOns.csproj -c Release + dotnet build CDPIUI.TrayIcon/CDPIUI.TrayIcon.csproj -c Release -p:Platform=x64 + + - name: Test + run: dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj -c Release + + gui: + runs-on: windows-latest + # The WinUI project needs the CommunityToolkit Labs feed and the Windows App SDK + # tooling; keep it informative until the build is reproducible on hosted runners. + continue-on-error: true + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - uses: microsoft/setup-msbuild@v2 + + - name: Build portable layout + shell: pwsh + run: ./.github/scripts/build_portable.ps1 + + - name: Check installer file list + shell: pwsh + run: ./.github/scripts/check_setup_files.ps1 + + - name: Build Setup.msi + run: dotnet build NewSetup/NewSetup.wixproj -c Release -p:Platform=x64 + + - uses: actions/upload-artifact@v4 + with: + name: setup-msi + path: NewSetup/bin/**/*.msi + retention-days: 7 + + tools: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + + - uses: microsoft/setup-msbuild@v2 + + - name: Build Update and EditHostFile + shell: pwsh + run: | + msbuild Update/Update.csproj /restore /p:RestorePackagesConfig=true "/p:SolutionDir=$pwd\" /p:Configuration=Release /m + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + msbuild EditHostFile/EditHostFile.csproj /p:Configuration=Release /m diff --git a/CDPI.sln b/CDPI.sln index 4a456105..dc19670e 100644 --- a/CDPI.sln +++ b/CDPI.sln @@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CDPIUI.Core", "CDPIUI.Core\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CDPIUI.AddOns", "CDPIUI.AddOns\CDPIUI.AddOns.csproj", "{B7D509E3-DB69-4DAF-A06D-608932C34316}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CDPIUI.Tests", "CDPIUI.Tests\CDPIUI.Tests.csproj", "{8E9C98F3-0483-4381-99FF-78E21C156F2C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -47,15 +49,15 @@ Global {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x64.ActiveCfg = Debug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x64.Build.0 = Debug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x64.Deploy.0 = Debug|x64 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.ActiveCfg = Debug|x86 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.Build.0 = Debug|x86 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.Deploy.0 = Debug|x86 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.ActiveCfg = Debug|x64 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.Build.0 = Debug|x64 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Debug|x86.Deploy.0 = Debug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|ARM64.ActiveCfg = MsiRelease|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|ARM64.Build.0 = MsiRelease|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x64.ActiveCfg = MsiRelease|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x64.Build.0 = MsiRelease|x64 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x86.ActiveCfg = MsiRelease|x86 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x86.Build.0 = MsiRelease|x86 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x86.ActiveCfg = MsiRelease|x64 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.MsiRelease|x86.Build.0 = MsiRelease|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Release|ARM64.ActiveCfg = Release|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Release|x64.ActiveCfg = Release|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.Release|x64.Build.0 = Release|x64 @@ -67,12 +69,12 @@ Global {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|ARM64.Build.0 = RemoteDebug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x64.ActiveCfg = RemoteDebug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x64.Build.0 = RemoteDebug|x64 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x86.ActiveCfg = RemoteDebug|x86 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x86.Build.0 = RemoteDebug|x86 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x86.ActiveCfg = RemoteDebug|x64 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.RemoteDebug|x86.Build.0 = RemoteDebug|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.SingleFileRelease|ARM64.ActiveCfg = Release|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.SingleFileRelease|x64.ActiveCfg = SingleFileRelease|x64 {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.SingleFileRelease|x64.Build.0 = SingleFileRelease|x64 - {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.SingleFileRelease|x86.ActiveCfg = Release|x86 + {3B8F23B1-28F5-4867-9D5D-1A77524F3A79}.SingleFileRelease|x86.ActiveCfg = Release|x64 {8872755A-41D3-41D4-801F-7F0E4D2801CE}.Debug|ARM64.ActiveCfg = Debug|x64 {8872755A-41D3-41D4-801F-7F0E4D2801CE}.Debug|ARM64.Build.0 = Debug|x64 {8872755A-41D3-41D4-801F-7F0E4D2801CE}.Debug|x64.ActiveCfg = Debug|x64 @@ -271,6 +273,27 @@ Global {B7D509E3-DB69-4DAF-A06D-608932C34316}.SingleFileRelease|x64.Build.0 = Release|Any CPU {B7D509E3-DB69-4DAF-A06D-608932C34316}.SingleFileRelease|x86.ActiveCfg = Release|Any CPU {B7D509E3-DB69-4DAF-A06D-608932C34316}.SingleFileRelease|x86.Build.0 = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|ARM64.Build.0 = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|x64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|x64.Build.0 = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|x86.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Debug|x86.Build.0 = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.MsiRelease|ARM64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.MsiRelease|x64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.MsiRelease|x86.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|ARM64.ActiveCfg = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|ARM64.Build.0 = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|x64.ActiveCfg = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|x64.Build.0 = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|x86.ActiveCfg = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.Release|x86.Build.0 = Release|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.RemoteDebug|ARM64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.RemoteDebug|x64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.RemoteDebug|x86.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.SingleFileRelease|ARM64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.SingleFileRelease|x64.ActiveCfg = Debug|Any CPU + {8E9C98F3-0483-4381-99FF-78E21C156F2C}.SingleFileRelease|x86.ActiveCfg = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CDPIUI.AddOns/BlockCheck2/Catalog/StrategyCatalogValidator.cs b/CDPIUI.AddOns/BlockCheck2/Catalog/StrategyCatalogValidator.cs index 50deb968..a2501a7b 100644 --- a/CDPIUI.AddOns/BlockCheck2/Catalog/StrategyCatalogValidator.cs +++ b/CDPIUI.AddOns/BlockCheck2/Catalog/StrategyCatalogValidator.cs @@ -323,15 +323,16 @@ private static bool IsValidRange(string range) private static BlockCheckIssue Error(string code, string message, string? subjectId = null) => new(BlockCheckIssueSeverity.Error, code, message, subjectId); - [GeneratedRegex("^[A-Za-z0-9_.-]+$", RegexOptions.CultureInvariant)] + // \z instead of $: "$" also matches before a final newline, which let "id\n" through. + [GeneratedRegex("^[A-Za-z0-9_.-]+\\z", RegexOptions.CultureInvariant)] private static partial Regex SafeIdentifierRegex(); - [GeneratedRegex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)] + [GeneratedRegex("^[A-Za-z_][A-Za-z0-9_]*\\z", RegexOptions.CultureInvariant)] private static partial Regex SafeArgumentNameRegex(); - [GeneratedRegex("^[A-Za-z0-9_@%#.+,\\-<>=:/\\\\]*$", RegexOptions.CultureInvariant)] + [GeneratedRegex("^[A-Za-z0-9_@%#.+,\\-<>=:/\\\\]*\\z", RegexOptions.CultureInvariant)] private static partial Regex SafeArgumentValueRegex(); - [GeneratedRegex("^(?[ndbsp]-?\\d+)?(?:-|<)(?[ndbsp]-?\\d+)?$", RegexOptions.CultureInvariant)] + [GeneratedRegex("^(?[ndbsp]-?\\d+)?(?:-|<)(?[ndbsp]-?\\d+)?\\z", RegexOptions.CultureInvariant)] private static partial Regex RangeRegex(); } diff --git a/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckManualStrategyTestService.cs b/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckManualStrategyTestService.cs index ddfbb61a..bb625dfd 100644 --- a/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckManualStrategyTestService.cs +++ b/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckManualStrategyTestService.cs @@ -1,7 +1,7 @@ using CDPIUI.AddOns.BlockCheck2.Models; using CDPIUI.AddOns.BlockCheck2.Synthesis; using CDPIUI.Shared.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; namespace CDPIUI.AddOns.BlockCheck2.Execution; diff --git a/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckScanService.cs b/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckScanService.cs index 5c7864c0..378d203b 100644 --- a/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckScanService.cs +++ b/CDPIUI.AddOns/BlockCheck2/Execution/BlockCheckScanService.cs @@ -173,7 +173,8 @@ private async Task ScanPreparedAsync( BlockCheckIssueSeverity.Warning, "DNS_TARGETS_IGNORED", $"Ignored {ignoredTargetIds.Count} target(s) for {ignoredHosts.Length} host(s) because " + - $"the selected DNS resolver returned no address: {preview}.")); + $"the selected DNS resolver returned no address: {preview}. This is often DNS blocking, " + + "which no DPI bypass strategy fixes: use encrypted DNS (DoH) for these hosts.")); } if (activeTargets.Length == 0) @@ -226,6 +227,11 @@ private async Task ScanPreparedAsync( List results = []; int completed = 0; bool wasCanceled = false; + // When winws2 or its driver cannot start, every strategy fails the same way; the scan + // went on through all of them and the synthesis ran with nothing. + const int MaxConsecutiveStartFailures = 5; + int consecutiveStartFailures = 0; + bool abortedOnStartFailures = false; foreach (RouteScanGroup route in routes) { HashSet successfulPlans = new(StringComparer.Ordinal); @@ -284,6 +290,7 @@ private async Task ScanPreparedAsync( startAttempted = true; await strategyRunner.StartAsync(configuration.CommandLine, cancellationToken) .ConfigureAwait(false); + consecutiveStartFailures = 0; foreach (BlockCheckTarget target in route.Targets) { @@ -374,6 +381,14 @@ await strategyRunner.StartAsync(configuration.CommandLine, cancellationToken) "SCAN_STRATEGY_FAILED", $"Strategy/route scan failed: {exception.Message}", $"{strategy.Id}:{route.Key}")); + if (attemptsByTarget.Values.All(attempts => attempts.All(a => a.FailureCode == "strategy_start_failed")) && + ++consecutiveStartFailures >= MaxConsecutiveStartFailures) + { + abortedOnStartFailures = true; + issues.Add(Error( + "SCAN_STRATEGIES_DO_NOT_START", + $"The last {MaxConsecutiveStartFailures} strategies could not be started, so the scan was stopped: {exception.Message}")); + } } finally { @@ -428,6 +443,11 @@ await strategyRunner.StartAsync(configuration.CommandLine, cancellationToken) failedChecks, successfulStrategies)); + if (abortedOnStartFailures) + { + break; + } + if (options.EnableRouteEarlyStop && successfulPlans.Count >= options.SuccessfulStrategiesPerRoute) { @@ -435,7 +455,7 @@ await strategyRunner.StartAsync(configuration.CommandLine, cancellationToken) } } - if (wasCanceled) + if (wasCanceled || abortedOnStartFailures) { break; } diff --git a/CDPIUI.AddOns/BlockCheck2/Execution/CurlBlockCheckProbeRunner.cs b/CDPIUI.AddOns/BlockCheck2/Execution/CurlBlockCheckProbeRunner.cs index ba2c3007..642bb32a 100644 --- a/CDPIUI.AddOns/BlockCheck2/Execution/CurlBlockCheckProbeRunner.cs +++ b/CDPIUI.AddOns/BlockCheck2/Execution/CurlBlockCheckProbeRunner.cs @@ -26,7 +26,7 @@ public sealed class CurlCapabilities public sealed class CurlBlockCheckProbeRunner : IBlockCheckProbeRunner, IBlockCheckProbePreflight { private const string WriteOutMarker = "__CDPIUI_BLOCKCHECK2__"; - private const string WriteOutFormat = WriteOutMarker + "%{http_code}|%{time_starttransfer}"; + private const string WriteOutFormat = WriteOutMarker + "%{http_code}|%{time_starttransfer}|%{redirect_url}"; private readonly CurlBlockCheckProbeRunnerOptions _options; private readonly ICommandExecutor _executor; @@ -183,10 +183,10 @@ public async Task ProbeAsync( }; } - (int statusCode, double timeToFirstByteMs) = ParseWriteOut(result.StandardOutput); + (int statusCode, double timeToFirstByteMs, string redirectUrl) = ParseWriteOut(result.StandardOutput); bool success = result.ExitCode == 0 && statusCode > 0 && - !(target.Protocol == BlockCheckProtocol.Http && statusCode == 400); + !(target.Protocol == BlockCheckProtocol.Http && IsProviderStub(statusCode, redirectUrl, target.Host)); string diagnostic = FirstUsefulLine(result.StandardError); if (!success && diagnostic.Length == 0 && !result.StandardOutput.Contains(WriteOutMarker, StringComparison.Ordinal)) @@ -290,24 +290,47 @@ private async Task RecognizesOptionsAsync( !error.Contains("unsupported option", StringComparison.OrdinalIgnoreCase); } - private static (int StatusCode, double TimeToFirstByteMs) ParseWriteOut(string output) + /// + /// Plain HTTP can be answered by the provider: 400 for a broken request, 451 for a blocked + /// site, or a redirect to its own block page. Any such status counted as a success, so the + /// route was dropped as needing no bypass. + /// + private static bool IsProviderStub(int statusCode, string redirectUrl, string targetHost) + { + if (statusCode is 400 or 451) + return true; + if (statusCode is >= 300 and < 400 && + Uri.TryCreate(redirectUrl, UriKind.Absolute, out Uri? redirect)) + { + string host = BlockCheckTarget.NormalizeHost(targetHost); + string redirectHost = BlockCheckTarget.NormalizeHost(redirect.Host); + // The site moving to HTTPS or to a subdomain of itself is normal. + bool sameSite = redirectHost.Equals(host, StringComparison.OrdinalIgnoreCase) || + redirectHost.EndsWith("." + host, StringComparison.OrdinalIgnoreCase) || + host.EndsWith("." + redirectHost, StringComparison.OrdinalIgnoreCase); + return !sameSite; + } + return false; + } + + private static (int StatusCode, double TimeToFirstByteMs, string RedirectUrl) ParseWriteOut(string output) { int marker = output.LastIndexOf(WriteOutMarker, StringComparison.Ordinal); if (marker < 0) { - return (0, -1); + return (0, -1, string.Empty); } string[] values = output[(marker + WriteOutMarker.Length)..] .Trim() - .Split('|', 2); + .Split('|', 3); int.TryParse(values.ElementAtOrDefault(0), NumberStyles.None, CultureInfo.InvariantCulture, out int status); bool hasTiming = double.TryParse( values.ElementAtOrDefault(1), NumberStyles.Float, CultureInfo.InvariantCulture, out double seconds); - return (status, hasTiming ? seconds * 1000d : -1); + return (status, hasTiming ? seconds * 1000d : -1, values.ElementAtOrDefault(2) ?? string.Empty); } private static string GetFailureCode( diff --git a/CDPIUI.AddOns/BlockCheck2/Presentation/BlockCheckSiteListLoader.cs b/CDPIUI.AddOns/BlockCheck2/Presentation/BlockCheckSiteListLoader.cs index 2a6a38f9..8feb4cc4 100644 --- a/CDPIUI.AddOns/BlockCheck2/Presentation/BlockCheckSiteListLoader.cs +++ b/CDPIUI.AddOns/BlockCheck2/Presentation/BlockCheckSiteListLoader.cs @@ -31,6 +31,18 @@ public BlockCheckSiteListLoader(BlockCheckTargetInputParser? targetParser = null _targetParser = targetParser ?? new BlockCheckTargetInputParser(); } + private static string NormalizePath(string? path) + { + try + { + return string.IsNullOrWhiteSpace(path) ? string.Empty : Path.GetFullPath(path); + } + catch (Exception exception) when (exception is ArgumentException or NotSupportedException or PathTooLongException) + { + return path ?? string.Empty; + } + } + public BlockCheckSiteListLoadResult Load( IEnumerable siteLists, BlockCheckTargetInputOptions options) @@ -40,8 +52,9 @@ public BlockCheckSiteListLoadResult Load( List targets = []; List issues = []; + // Compared by full path: "list.txt" and ".\\list.txt" are the same file. foreach (BlockCheckSiteListInput siteList in siteLists - .DistinctBy(item => item.FilePath, StringComparer.OrdinalIgnoreCase)) + .DistinctBy(item => NormalizePath(item.FilePath), StringComparer.OrdinalIgnoreCase)) { string subject = string.IsNullOrWhiteSpace(siteList.DisplayName) ? siteList.FilePath diff --git a/CDPIUI.AddOns/BlockCheck2/README.md b/CDPIUI.AddOns/BlockCheck2/README.md index 9a62e9a7..07b20985 100644 --- a/CDPIUI.AddOns/BlockCheck2/README.md +++ b/CDPIUI.AddOns/BlockCheck2/README.md @@ -1,241 +1,257 @@ -# BlockCheck2 для Zapret2: техническое описание +# BlockCheck2 for Zapret2 -## 1. Назначение и границы +## 1. Purpose and scope -BlockCheck2 — AddOn CDPIUI для подбора, объединения, проверки и последующего редактирования стратегий Zapret2. Его задача не ограничивается поиском отдельных успешных запусков. После сканирования AddOn строит совместимый многопрофильный пресет, проверяет его целиком и при необходимости пытается заменить конфликтующую стратегию. +BlockCheck2 is an internal add-on of CDPI UI. It finds Zapret2 strategies, combines them into one preset, checks the preset and lets the user edit it. The add-on does more than find single strategies that operate. After the scan, it makes one multi-profile preset, checks the complete preset and tries to replace a strategy that causes a conflict. -Основной сценарий рассчитан на HTTP и HTTPS-сайты. Модель данных и каталог поддерживают QUIC, однако выбор QUIC пока скрыт в штатном интерфейсе. Подбор для произвольного UDP-трафика приложений, в частности голосовых соединений Discord, не реализован: такой трафик нельзя достоверно проверить обычным HTTP-запросом `curl`. +The main scenario is for HTTP and HTTPS sites. The data model and the catalog support QUIC, but the standard UI hides the QUIC option. The add-on does not find strategies for random UDP traffic of applications, for example Discord voice. A usual `curl` HTTP request cannot check such traffic. -BlockCheck2 не является отдельным Store-пакетом. Алгоритм находится в проекте `CDPIUI.AddOns`, а UI и адаптеры инфраструктуры CDPIUI — в основном проекте `CDPIUI`. +BlockCheck2 is not a separate Store package. The algorithm is in the `CDPIUI.AddOns` project. The UI and the CDPI UI adapters are in the `CDPIUI` project. -## 2. Архитектура +## 2. Architecture -### 2.1. UI-независимое ядро +### 2.1. Core without UI -Каталог `CDPIUI.AddOns/BlockCheck2` разделён на следующие области: +The folder `CDPIUI.AddOns/BlockCheck2` has these areas: -| Область | Назначение | +| Area | Contents | |---|---| -| `Catalog` | Встроенный каталог стратегий, его загрузка, программное расширение и валидация | -| `Models` | Цели, стратегии, результаты запросов, профили, параметры сканирования и валидации | -| `Execution` | Baseline-проверка, запуск временных конфигураций Zapret2, запросы `curl`, тест отдельной стратегии и проверка готового пресета | -| `Analysis` | Нормализация эквивалентных стратегий, Pareto-фильтрация, группировка целей и оптимизация покрытия | -| `Synthesis` | Выбор профилей, circular-fallback, ручная сборка и генерация строки Zapret2 | -| `Reporting` | Формирование, сериализация, импорт и восстановление сессии из отчёта | -| `Presentation` | Режимы подбора, подготовка входных данных, списки сайтов и оценка времени | +| `Catalog` | The built-in strategy catalog, its loader, the expander and the validator | +| `Models` | Targets, strategies, probe results, profiles, scan options and validation options | +| `Execution` | The baseline check, temporary Zapret2 runs, `curl` requests, the test of one strategy and the check of a ready preset | +| `Analysis` | The normalization of equivalent strategies, the Pareto filter, the target groups and the coverage optimizers | +| `Synthesis` | The profile selection, the circular fallback, the manual preset and the Zapret2 command line | +| `Reporting` | The report builder, the serializer, the import and the session restore from a report | +| `Presentation` | The run modes, the input preparation, the site lists and the time estimates | -`BlockCheckAddOnService` является UI-независимой точкой входа. Он принимает каталог, цели, реализации `IBlockCheckStrategyRunner` и `IBlockCheckProbeRunner`, параметры запуска, объект прогресса и `CancellationToken`. +`BlockCheckAddOnService` is the entry point. It does not use the UI. It accepts the catalog, the targets, an `IBlockCheckStrategyRunner`, an `IBlockCheckProbeRunner`, the run options, a progress object and a `CancellationToken`. -### 2.2. Интеграционный слой CDPIUI +### 2.2. CDPI UI integration -Основные интеграционные классы: +| Type | Location | Function | +|---|---|---| +| `BlockCheck2ViewModel` | `CDPIUI/ViewModels` | Wizard state, settings, session start, ETA, intermediate counters and report save | +| `Views/BlockCheck2/MainPage` | `CDPIUI/Views/BlockCheck2` | Pages for preparation, parameter review, progress and completion | +| `Views/BlockCheck2/ReportHistoryPage` | `CDPIUI/Views/BlockCheck2` | The list of recent selections | +| `BlockCheck2ResultViewModel` | `CDPIUI/ViewModels` | Report view, evidence table and state of the manual builder | +| `Views/BlockCheck2/ResultPage` | `CDPIUI/Views/BlockCheck2` | Selection result, manual preset and diagnostics | +| `Views/BlockCheck2/PresetReviewPage` | `CDPIUI/Views/BlockCheck2` | Review, expert edit, test and save of the ready preset | +| `BlockCheck2PresetEditorControl` | `CDPIUI/Controls/BlockCheck2` | Adapter between the BlockCheck2 draft and the common `ConfigMakerUserControl` | +| `BlockCheck2PresetStorageService` | `CDPIUI/Helper/AddOns/BlockCheck2` | Saves the preset in the user configs and copies the dependencies | +| `BlockCheckReportHistoryService` | `CDPIUI/Helper/AddOns/BlockCheck2` | Recent reports | +| `BlockCheck2HistoryStoreItemService` | `CDPIUI/Helper/AddOns/BlockCheck2` | Registers the history as a Store item that the user can remove | -| Класс | Назначение | -|---|---| -| `BlockCheck2ViewModel` | Состояние мастера, настройки, запуск сессии, ETA, промежуточная статистика и сохранение отчёта | -| `Views/BlockCheck2/MainPage` | Страницы подготовки, проверки параметров, прогресса и завершения | -| `BlockCheck2ResultViewModel` | Представление отчёта, evidence-таблица и состояние ручного конструктора | -| `Views/BlockCheck2/ResultPage` | Результат подбора, ручная сборка и диагностика | -| `Views/BlockCheck2/PresetReviewPage` | Просмотр, экспертное редактирование, тест и сохранение готового пресета | -| `BlockCheck2PresetEditorControl` | Адаптер между черновиком BlockCheck2 и общим `ConfigMakerUserControl` | -| `BlockCheck2PresetStorageService` | Сохранение пресета в конфигурации пользователя и копирование зависимостей | -| `BlockCheckReportHistoryService` | Недавние отчёты | -| `BlockCheck2HistoryStoreItemService` | Регистрация истории как удаляемого Store-элемента | +## 3. Session sequence -## 3. Общий жизненный цикл сессии +1. The user selects one or more site lists, or makes a "custom site list". +2. For each list, the user selects one of two modes: the list as one unit, or each site separately. +3. The user selects the quick, medium or full mode. +4. On the page with more parameters, the user selects the protocols, the IP versions, the timeouts, the number of requests to each target and the option to test all strategies. +5. `BlockCheckSessionPreparationService` loads the built-in catalog, parses the input and makes one `BlockCheckTarget` for each combination of URL, protocol and IP version. +6. The parameter review page shows a conservative maximum duration. +7. `BlockCheckAddOnService` runs the preflight, the baseline, the strategy scan, the synthesis, the check of the ready preset and a possible repair. +8. `BlockCheckReportBuilder` makes a report. It also does this for a best-effort result and for a canceled session with partial data. +9. The view model saves the JSON report in the history automatically. +10. A separate result window lets the user accept the automatic preset or make a preset from all the evidence. -1. Пользователь выбирает один или несколько списков сайтов либо создаёт «Собственный список сайтов». -2. Для каждого списка выбирается обработка как единого списка или каждого сайта отдельно. -3. Пользователь выбирает быстрый, средний или полный режим. -4. На странице дополнительных параметров выбираются протоколы, версии IP, таймауты, число запросов к каждой цели и режим принудительной проверки всех стратегий. -5. `BlockCheckSessionPreparationService` загружает встроенный каталог, разбирает ввод и создаёт `BlockCheckTarget` для каждой комбинации URL, протокола и версии IP. -6. На странице проверки параметров строится консервативная оценка максимального времени. -7. `BlockCheckAddOnService` выполняет preflight, baseline, сканирование стратегий, синтез, проверку готовой конфигурации и возможный repair. -8. `BlockCheckReportBuilder` создаёт отчёт даже для best-effort результата и штатно отменённой сессии с частичными данными. -9. JSON-отчёт автоматически сохраняется в историю. -10. Отдельное окно результата позволяет принять автоматически предложенный пресет либо собрать свой на основе всей полученной evidence-информации. +## 4. Input data and targets -## 4. Входные данные и цели +### 4.1. Domains and URLs -### 4.1. Домены и URL +`BlockCheckTargetInputParser` accepts: -`BlockCheckTargetInputParser` принимает: +- a DNS name without a scheme; +- an absolute `http://` or `https://` URL; +- a path and a query string; +- a non-standard port. -- DNS-имя без схемы; -- абсолютный `http://` или `https://` URL; -- путь и query string; -- нестандартный порт. +The parser ignores empty lines and lines that start with `#`. It converts domains to lowercase IDN ASCII. Only DNS names are valid. IP literals and other schemes cannot be targets. -Комментарии, начинающиеся с `#`, и пустые строки игнорируются. Домены нормализуются в нижний регистр и IDN ASCII. Поддерживаются только DNS-имена; IP-литералы и произвольные схемы не используются как цели. +A line without a scheme expands into all selected protocols. An explicit scheme limits the line to the compatible protocols. Each target also expands into all selected IP versions. The parser then removes duplicates. -Если схема не указана, строка расширяется во все выбранные протоколы. Явная схема ограничивает набор совместимыми протоколами. Каждая цель также расширяется во все выбранные версии IP. После разбора дубликаты удаляются. +One parser call makes a maximum of 200 targets (`BlockCheckTargetInputOptions.MaximumTargets`). If the input makes more targets, the parser returns `TARGET_INPUT_LIMIT_EXCEEDED`. The limit applies separately to the direct input and to each list in the `WholeList` mode. In the `EachSite` mode, the parser reads each domain separately. -Идентификатор вида `target-005-tls12-ipv4` является внутренним. В UI URL и сведения о соединении выводятся отдельно: например, `https://example.org/path` и `TLS 1.2 · IPv4 · TCP:443`. +An ID such as `target-005-tls12-ipv4` is internal. The UI shows the URL and the connection data separately, for example `https://example.org/path` and `TLS 1.2 · IPv4 · TCP:443`. -### 4.2. Списки сайтов +### 4.2. Site lists -Доступны: +These sources are available: -- списки, уже зарегистрированные в CDPIUI; -- локальные TXT-файлы, выбранные через диалог; -- TXT-файлы, перетащенные на страницу; -- собственный редактируемый список, вводимый через отдельный `CustomSiteListContentDialog`. +- lists that CDPI UI already knows; +- local TXT files from the file dialog; +- TXT files that the user drops on the page; +- a custom list that the user types in `CustomSiteListContentDialog`. -Строка списка очищается от BOM и начального `^`. Пустые строки и комментарии `#` игнорируются. Списки исключений не предлагаются как обычные входные списки. +The loader removes a BOM and a leading `^` from each line. It ignores empty lines and `#` comments. The UI does not offer exclusion lists as usual input lists. -Для каждого списка существует два режима: +Each list has one of two modes: -- `WholeList` — файл остаётся атомарной областью. Итоговый профиль ссылается на него через `--hostlist`; -- `EachSite` — для каждого домена создаётся отдельный временный TXT-файл. Это разрешает выбрать разные стратегии для разных сайтов исходного списка. +- `WholeList`: the file stays one atomic unit. The final profile refers to the file with `--hostlist`. +- `EachSite`: the loader makes a separate temporary TXT file for each domain. Thus different sites of one list can get different strategies. -Временные файлы `site_0001_.txt` размещаются в `BlockCheck2/GeneratedSiteLists` внутри каталога данных CDPIUI. Они не показываются в общем меню выбора списков. Если подготовка или запуск закончились без отчёта, доступного для просмотра, временные файлы удаляются. При наличии результата они сохраняются, поскольку на них может ссылаться отчёт или ещё не сохранённый пресет. +The temporary files have names such as `site_0001_.txt`. They are in `BlockCheck2/GeneratedSiteLists` in the CDPI UI data folder. The general list selection menu does not show them. If preparation or the run ends without a report that the user can open, the view model deletes the temporary files. If a result exists, the files stay, because the report or an unsaved preset can refer to them. -Собственный список хранится в: +The custom list is in this folder: ```text Store/Items/LocalUserStorage/List/BlockCheck2/Custom ``` -### 4.3. Атомарность списка +### 4.3. Atomic lists + +One `--hostlist` option covers the complete file. Thus the algorithm cannot put one list into several conflicting profiles and expect each profile to process only "its" domains. -Один `--hostlist` охватывает весь файл. Поэтому алгоритм не может безопасно поместить один и тот же список в несколько конфликтующих профилей и надеяться, что каждый профиль обработает только «свои» домены. +`BlockCheckTargetGroupBuilder` puts these targets into one group: -`BlockCheckTargetGroupBuilder` объединяет: +- the checks of one runtime route; +- all targets of one list with the same connection shape; +- lists that share targets, transitively. -- проверки одного runtime-маршрута; -- все цели одного списка с одинаковой формой соединения; -- пересекающиеся списки транзитивно. +A strict strategy must operate for all targets of the group. The scan, the strict optimizer, the best-effort optimizer and `BlockCheckSynthesisService` use the same groups. Thus a site of a list that passes the baseline stays in its group, and the selected strategy must not break it. -Для такой группы строгая стратегия должна успешно работать для всех её целей. Если разные домены одного атомарного списка проходят только с разными стратегиями, создаётся предупреждение `NO_COMMON_SITE_LIST_STRATEGY`. Для раздельного подбора пользователь должен выбрать `EachSite`. +If different domains of one atomic list pass only with different strategies, the synthesis adds the warning `NO_COMMON_SITE_LIST_STRATEGY`. If the synthesis then cannot make a profile, the issue with the same code is an error. To get different strategies for the sites, the user must select `EachSite`. -## 5. Протоколы и runtime-маршрут +## 5. Protocols and runtime route -Модель различает: +The model has these protocols: -- `Http` — TCP, порт 80 по умолчанию; -- `Tls12` — HTTPS с принудительным TLS 1.2 и HTTP/1.1; -- `Tls13` — HTTPS с принудительным TLS 1.3 и HTTP/1.1; -- `TlsAuto` — HTTPS без принудительного TLS или ALPN; -- `Quic` — UDP и HTTP/3. +- `Http`: TCP, port 80 by default; +- `Tls12`: HTTPS with forced TLS 1.2 and HTTP/1.1; +- `Tls13`: HTTPS with forced TLS 1.3 and HTTP/1.1; +- `TlsAuto`: HTTPS without a forced TLS version or ALPN; +- `Quic`: UDP and HTTP/3. -Runtime-маршрут определяется кортежем: +This tuple identifies a runtime route: ```text host + IP version + transport + port + L7 protocol ``` -TLS 1.2, TLS 1.3 и automatic TLS одного домена могут давать отдельные строки evidence, но обычный профиль Zapret2 не умеет выбирать действие по согласованной версии TLS после ClientHello. Поэтому совместимые TLS-проверки одной формы должны учитываться как единая область при выборе стратегии. Успех только на одной TLS-версии помечается как частичный. +TLS 1.2, TLS 1.3 and automatic TLS of one domain can give separate evidence rows. A usual Zapret2 profile cannot select an action by the TLS version after the ClientHello. Thus the strategy selection treats compatible TLS checks of one shape as one unit. A success on only one TLS version is partial. -Версия IP является частью профиля, и генератор всегда пишет `--filter-l3=ipv4` либо `--filter-l3=ipv6`. Если браузер может использовать обе версии, обе версии следует включить в подбор. Успех `curl -4` не доказывает доступность браузера, который выбрал IPv6. +The IP version is a part of the profile. The writer always adds `--filter-l3=ipv4` or `--filter-l3=ipv6`. If a browser can use the two IP versions, include the two versions in the selection. A success of `curl -4` does not prove that a browser that uses IPv6 can open the site. -## 6. Режимы подбора +## 6. Run modes -`BlockCheckRunPresetFactory` синхронно задаёт параметры сканирования, синтеза и проверки готового пресета. +`BlockCheckRunPresetFactory` sets the scan, synthesis and preset check options together. -| Параметр | Быстрый | Средний | Полный | +| Parameter | Quick | Medium | Full | |---|---:|---:|---:| -| Рекомендуемое число запросов к цели | 2 | 3 | 5 | -| Максимальный tier каталога | Quick | Balanced | Exhaustive | -| Порог успеха | 100% | 80% | 80% | -| Успешных различных планов до ранней остановки | 2 | 2 | 4 | -| Ранняя остановка маршрута | Да | Да | Нет | -| Circular fallback на профиль | До 1 | До 1 | До 2 | -| Максимум repair-итераций | 1 | 3 | 5 | -| Предпочтение оптимизатора | Скорость | Баланс | Стабильность | +| Recommended requests to each target | 2 | 3 | 5 | +| Maximum catalog tier | Quick | Balanced | Exhaustive | +| Success threshold | 100% | 80% | 80% | +| Different successful plans before early stop | 2 | 2 | 4 | +| Early stop of a route | Yes | Yes | No | +| Circular fallbacks for each profile | Up to 1 | Up to 1 | Up to 2 | +| Circular failure threshold (`fails`) | 2 | 3 | 3 | +| Maximum repair iterations | 1 | 3 | 5 | +| Optimizer preference | Speed | Balanced | Stability | -Выбор режима устанавливает рекомендуемое число запросов. После этого пользователь может вручную задать целое значение от 1 до 20. Оно одинаково применяется к: +The mode sets the recommended number of requests. The user can then set an integer value from 1 to 20. The value applies to: -- baseline-проверкам; -- каждой паре стратегия/цель; -- минимальному числу evidence-попыток при синтезе; -- проверке каждого кандидата готового пресета; -- оценке максимального времени. +- the baseline checks; +- each strategy and target pair; +- the minimum number of evidence attempts in the synthesis; +- the check of each candidate of the ready preset; +- the maximum duration estimate. -Параметр «Не возвращать после нахождения лучшего» отключает раннюю остановку в быстром и среднем tier. Полный режим и без него проверяет весь применимый каталог. Флаг не расширяет выбранный tier: средний режим с этим параметром не начинает тестировать Exhaustive-стратегии. +The option "Do not stop after the best result" disables the early stop in the quick and medium tiers. The full mode tests the complete applicable catalog without this option. The option does not extend the selected tier. For example, the medium mode with this option does not test the Exhaustive strategies. -## 7. Настройки `BLOCKCHECK` +## 7. `BLOCKCHECK` settings -Настройки записываются через `SettingsManager` в `Settings.xml`, группа `BLOCKCHECK`. +`SettingsManager` writes the settings to `Settings.xml`, group `BLOCKCHECK`. -| Ключ | Тип | Значение по умолчанию | Назначение | +| Key | Type | Default | Function | |---|---|---:|---| -| `SelectedRunPreset` | `int` | `Balanced` | Выбранный режим | -| `IncludeHttp` | `bool` | `false` | Проверять HTTP | -| `IncludeTls12` | `bool` | `true` | Проверять TLS 1.2 | -| `IncludeTls13` | `bool` | `false` | Проверять TLS 1.3 | -| `IncludeTlsAuto` | `bool` | `true` | Проверять automatic HTTPS | -| `IncludeQuic` | `bool` | `false` | Проверять QUIC; штатный переключатель пока скрыт | -| `IncludeIpv4` | `bool` | `true` | Проверять IPv4 | -| `IncludeIpv6` | `bool` | `false` | Проверять IPv6 | -| `ConnectTimeoutSeconds` | `double` | `5` | Таймаут установления соединения, 1–60 с | -| `RequestTimeoutSeconds` | `double` | `10` | Полный таймаут запроса, 2–180 с и не меньше connect timeout | -| `StartupGraceSeconds` | `double` | `0.75` | Пауза после запуска Zapret2, 0.25–10 с | -| `TestAllStrategies` | `bool` | `false` | Отключить раннюю остановку | -| `RequestsPerDomain` | `int` | 2, 3 или 5 | Число запросов к каждой цели, 1–20 | -| `historyStoreItemRegistrationCompleted` | `bool` | `false` | Завершена одноразовая Store-регистрация истории | - -Ошибочное или выходящее за диапазон сохранённое значение заменяется безопасным значением по умолчанию при загрузке ViewModel. Ошибка чтения или записи настроек не останавливает подбор и записывается в журнал CDPIUI. - -## 8. Каталог стратегий - -Seed-каталог хранится как embedded resource `strategy-catalog.v1.json`. `BuiltInStrategyCatalogExpander` дополняет его стандартными семействами и публикует итоговую версию `2026.08.11-standard`. - -Каталог содержит HTTP-, TLS- и QUIC-варианты семейств: - -- `multisplit` и `multidisorder` с разными позициями; -- `fake` с `tcp_md5`, `badsum`, изменениями sequence/ack/timestamp и TCP flags; -- `fakedsplit` и `fakeddisorder`; +| `SelectedRunPreset` | `int` | `Balanced` | Selected mode | +| `IncludeHttp` | `bool` | `false` | Check HTTP | +| `IncludeTls12` | `bool` | `true` | Check TLS 1.2 | +| `IncludeTls13` | `bool` | `false` | Check TLS 1.3 | +| `IncludeTlsAuto` | `bool` | `true` | Check automatic HTTPS | +| `IncludeQuic` | `bool` | `false` | Check QUIC. The standard switch is hidden. | +| `IncludeIpv4` | `bool` | `true` | Check IPv4 | +| `IncludeIpv6` | `bool` | `false` | Check IPv6 | +| `ConnectTimeoutSeconds` | `double` | `5` | Connection timeout, 1–60 s | +| `RequestTimeoutSeconds` | `double` | `10` | Full request timeout, 2–180 s, not less than the connection timeout | +| `StartupGraceSeconds` | `double` | `0.75` | Pause after the Zapret2 start, 0.25–10 s | +| `TestAllStrategies` | `bool` | `false` | Disable the early stop | +| `RequestsPerDomain` | `int` | 2, 3 or 5 | Requests to each target, 1–20 | +| `historyStoreItemRegistrationCompleted` | `bool` | `false` | The one-time Store registration of the history is complete | + +When the view model loads, it replaces an incorrect or out-of-range value with a safe default. A settings read or write error does not stop the selection. The view model writes the error to the CDPI UI log. + +## 8. Strategy catalog + +The seed catalog is the embedded resource `strategy-catalog.v1.json`. `BuiltInStrategyCatalogExpander` adds the standard families and sets the catalog version `2026.08.11-standard`. + +The catalog has HTTP, TLS and QUIC variants of these families: + +- `multisplit` and `multidisorder` with different positions; +- `fake` with `tcp_md5`, `badsum`, sequence, ack and timestamp changes, and TCP flags; +- `fakedsplit` and `fakeddisorder`; - `hostfakesplit`; -- комбинированные fake + split/faked/hostfake; -- QUIC fake и IP fragmentation. +- combinations of fake with split, faked or hostfake; +- QUIC fake and IP fragmentation. + +Each `StrategyDefinition` sets: -Каждая `StrategyDefinition` задаёт: +- a stable `Id` and a display name; +- the family and the scan tier; +- the applicable protocols and IP versions; +- the base cost; +- circular support; +- the pre-host and inbound traffic requirements; +- the blobs; +- the ordered Lua actions, the payloads and the ranges. -- стабильный `Id` и отображаемое имя; -- семейство и scan tier; -- применимые протоколы и версии IP; -- базовую стоимость; -- поддержку circular; -- требования к pre-host и входящему трафику; -- blobs; -- упорядоченные Lua-действия, payload и диапазоны. +`StrategyCatalogValidator` rejects: -`StrategyCatalogValidator` запрещает неизвестные или небезопасные Lua-функции, дублирующиеся идентификаторы, некорректные диапазоны и несовместимые определения. Порядок `--lua-desync` семантически значим и сохраняется. +- unknown Lua functions; +- unknown payload filters; +- duplicate IDs; +- incorrect ranges; +- the reserved arguments `strategy` and `final`; +- argument values with unsafe characters; +- blob sources with an absolute path or `..`. -`CandidateNormalizer` строит сигнатуру фактического плана без presentation metadata. Два элемента каталога с одинаковыми действиями считаются одним техническим планом при подсчёте успешных альтернатив и выборе fallback. +The regular expressions for IDs, argument names, argument values and ranges end with `\z`, not with `$`. Thus a value with a final line break does not pass. The order of `--lua-desync` actions has a meaning, and the writer keeps it. -## 9. Подготовка окружения +`CandidateNormalizer` makes a signature of the actual plan without the display data. If two catalog items have the same actions, the add-on counts them as one technical plan. This applies to the count of successful alternatives and to the fallback selection. -`CdpiuiZapret2StrategyRunner` запускает временные аргументы через штатный путь `ComponentTasksManager` → CDPIUI Tray/ConPTY. +## 9. Environment preparation -Перед baseline он: +`CdpiuiZapret2StrategyRunner` starts temporary arguments through the usual path: `ComponentTasksManager`, then the CDPI UI tray and ConPTY. -1. проверяет установку компонента Zapret2 и наличие исполняемого файла; -2. получает эксклюзивный semaphore для компонента; -3. фиксирует запущенные Zapret2, Zapret Legacy, GoodbyeDPI, ByeDPI, SpoofDPI и NoDPI; -4. останавливает конфликтующие компоненты. +Before the baseline, the runner does these steps: -После сканирования или ошибки runner останавливает временный процесс и восстанавливает компоненты, работавшие до начала. Cleanup выполняется с `CancellationToken.None`, поэтому ручная отмена не должна прервать восстановление окружения. +1. It makes sure that the Zapret2 component and its executable file are installed. +2. It gets an exclusive semaphore for the component. +3. It records which of these components operate: Zapret2, Zapret Legacy, GoodbyeDPI, ByeDPI, SpoofDPI and NoDPI. +4. It stops the components that cause a conflict. -После каждого запуска стратегии выдерживается `StartupGraceSeconds`. Если процесс завершился, в предупреждение включается хвост вывода процесса длиной до 800 символов. Ошибка старта одной стратегии записывается как `SCAN_STRATEGY_FAILED`; она не уничтожает результаты предыдущих стратегий и не прерывает весь маршрут. +After the scan or an error, the runner stops the temporary process. It then starts again the components that operated before. The cleanup uses `CancellationToken.None`. Thus a manual cancel does not stop the restore of the environment. -## 10. Preflight и `curl` +After each strategy start, the runner waits `StartupGraceSeconds`. If the process stops, the warning contains the last 800 characters of the process output. A start error of one strategy gives `SCAN_STRATEGY_FAILED`. This error does not delete the results of earlier strategies and does not stop the route. If 5 strategies in sequence do not start, the scan stops with the error `SCAN_STRATEGIES_DO_NOT_START`. Usually this means that winws2 or its driver cannot start. -`CurlBlockCheckProbeRunner` сначала определяет: +## 10. Preflight and `curl` -- доступность `curl.exe`; -- поддержку `--tls-max`; -- поддержку TLS 1.3; -- поддержку HTTP/3. +`CurlBlockCheckProbeRunner` first finds out: -Если выбранная проверка невозможна текущей сборкой curl, AddOn возвращает preflight error до изменения состояния компонентов. +- if `curl.exe` is available; +- if curl supports `--tls-max`; +- if curl supports TLS 1.3; +- if curl supports HTTP/3. -Для запроса используются `--connect-timeout`, `--max-time`, `--output NUL`, машинный `--write-out`, отдельный User-Agent и жёсткий `-4`/`-6`. TLS-запросы по умолчанию используют полноценный GET, а не HEAD. `TlsAuto` не принуждает HTTP/1.1 или версию TLS и потому ближе к обычному HTTPS-клиенту, но всё равно не является браузером. +If the curl build cannot do a selected check, the add-on returns a preflight error before it changes the state of the components. The errors are `CURL_NOT_AVAILABLE`, `CURL_TLS_MAX_UNSUPPORTED`, `CURL_TLS13_UNSUPPORTED` and `CURL_HTTP3_UNSUPPORTED`. -Попытка считается успешной, если `curl` завершился с кодом 0 и вернул ненулевой HTTP status. Для HTTP status 400 отдельно используется `http-400-possible-fake-leak`. +Each request uses `--connect-timeout`, `--max-time`, `--output NUL`, a machine-readable `--write-out`, a separate User-Agent and a fixed `-4` or `-6`. HTTP, TLS 1.2 and TLS 1.3 requests use `--http1.1`. TLS requests use a full GET by default, not HEAD. QUIC requests use HEAD and `--http3-only` by default. `TlsAuto` does not force HTTP/1.1 or a TLS version. Thus it is nearer to a usual HTTPS client, but it is not a browser. -Стабильные failure codes включают: +An attempt is successful when `curl` exits with code 0 and returns an HTTP status that is not zero. For the `Http` protocol, the response also must not look like a provider stub. Status 400, status 451 and a redirect (3xx) to a different site are failures. Status 400 gives `http-400-possible-fake-leak`. The other stub responses give `http-`. +The stable failure codes include: + +- `curl-start-failed`; +- `proxy-resolution-failed`; - `dns-resolution-failed`; - `connection-failed`; - `timeout`; @@ -245,291 +261,304 @@ Seed-каталог хранится как embedded resource `strategy-catalog. - `http-status-missing`; - `curl-exit-N`. -В отчёт сохраняются число попыток, число успехов, success rate, median TTFB, p95 TTFB, HTTP statuses и failure codes. +The report keeps the number of attempts, the number of successes, the success rate, the median TTFB, the p95 TTFB, the HTTP statuses and the failure codes. ## 11. Baseline -До запуска Zapret2 каждый target проверяется с тем же числом запросов, протоколом, URL и версией IP. +Before Zapret2 starts, the add-on checks each target. It uses the same number of requests, protocol, URL and IP version. -Baseline используется для трёх задач: +The baseline has three functions: -1. не подбирать обход для маршрута, который уже проходит без Zapret2; -2. сравнивать best-effort кандидата с исходным состоянием; -3. отбрасывать цели, которые DNS-резолвер системы не разрешает. +1. It prevents a bypass selection for a route that operates without Zapret2. +2. It gives a reference for a best-effort candidate. +3. It removes targets that the system DNS resolver cannot resolve. -Если все попытки цели завершились `dns-resolution-failed`, цель добавляется в `IgnoredTargetIds`, исключается из синтеза и отчёта и сопровождается `DNS_TARGETS_IGNORED`. Если не разрешилась ни одна цель, возвращается `NO_RESOLVABLE_TARGETS`. +If all attempts of a target end with `dns-resolution-failed`, the add-on adds the target to `IgnoredTargetIds`. The synthesis and the report do not use this target, and the issues get `DNS_TARGETS_IGNORED`. If no target resolves, the add-on returns `NO_RESOLVABLE_TARGETS`. -Маршрут пропускается как baseline-доступный только тогда, когда все входящие в его атомарную группу проверки достигли порога режима. Сообщение `NO_BYPASS_REQUIRED` относится только к точным проверенным transport routes. Оно не утверждает, что браузер отрисовал страницу, что HTTPS после HTTP redirect доступен или что другая версия IP работает. +The add-on skips a route as accessible only when all checks of its atomic group reach the threshold of the mode. The message `NO_BYPASS_REQUIRED` applies only to the exact transport routes that the add-on checked. It does not prove that a browser shows the page, that HTTPS after an HTTP redirect is available or that the other IP version operates. -## 12. Сканирование стратегий +## 12. Strategy scan -Стратегии фильтруются по выбранному tier и применимости ко всей атомарной группе. Порядок прогрессивный: сначала более дешёвые и низкие tier, затем более сложные. +The scan uses only strategies of the selected tier that apply to all targets of the atomic group. The order is progressive: low tiers come first. In a tier, the scan takes one strategy from each family in turn, and cheaper families come first. -Один запуск Zapret2 обслуживает одну комбинацию «стратегия + атомарная группа». Пока процесс работает, последовательно проверяются все targets группы и все повторы. Это важно для TLS 1.2/TLS 1.3 и списков: стратегия признаётся успешной для группы только при достижении порога на каждой цели. +One Zapret2 start serves one combination of a strategy and an atomic group. While the process operates, the scan checks all targets of the group with all repeats. This is important for TLS 1.2, TLS 1.3 and for lists: a strategy is successful for a group only if it reaches the threshold on each target. -Промежуточный прогресс содержит: +The progress data contains: -- фазу; -- завершённые и общие strategy jobs; -- текущий URL; -- отдельное описание соединения; -- текущую стратегию; -- номер попытки; -- успешные и неуспешные запросы; -- число успешных технически различных стратегий. +- the phase; +- the completed and the total strategy jobs; +- the current URL; +- a separate connection description; +- the current strategy; +- the attempt number; +- the successful and the failed requests; +- the number of successful, technically different strategies. -Если включена ранняя остановка, маршрут прекращает перебор после нахождения требуемого числа различных успешных планов. При принудительном полном тесте перебираются все применимые стратегии выбранного tier. +With the early stop, a route stops after the scan finds the required number of different successful plans. With the full test, the scan tries all applicable strategies of the selected tier. -Ошибки отдельного запроса, генерации аргументов, старта или остановки процесса превращаются в evidence и issues. Уже собранные `ProbeResult` сохраняются. +Errors of one request, of argument generation, of a process start or of a process stop become evidence and issues. The scan keeps the `ProbeResult` items that it has collected. -## 13. Синтез автоматического пресета +## 13. Automatic preset synthesis ### 13.1. Evidence -`ProbeResultAnalyzer` агрегирует попытки по стратегии и цели в `CandidateEvaluation`. Кандидат проходит строгий порог только при достаточном числе попыток и требуемом success rate. +`ProbeResultAnalyzer` collects the attempts of each strategy and target into a `CandidateEvaluation`. A candidate passes the strict threshold only with enough attempts and the required success rate. -### 13.2. Pareto-фильтрация +### 13.2. Pareto filter -`ParetoReducer` удаляет кандидата, если другой кандидат: +`ParetoReducer` removes a candidate if another candidate: -- покрывает не меньше целей; -- имеет не меньшую минимальную стабильность; -- не медленнее по худшему p95; -- не дороже по базовой стоимости; -- строго лучше хотя бы по одному критерию. +- covers all targets that the first candidate covers (a candidate without strict coverage stays); +- has a minimum stability that is not lower; +- is not slower by the worst p95; +- does not have a higher base cost; +- is strictly better by at least one criterion. -Эквивалентные планы детерминированно сводятся к одному ID. +Equivalent plans become one ID in a deterministic way. -### 13.3. Строгое покрытие +### 13.3. Strict coverage -`CoverageOptimizer` решает жадную задачу покрытия атомарных групп. Score равен пользе нового покрытия, делённой на effective cost. Effective cost сочетает: +`CoverageOptimizer` solves a greedy coverage problem for the atomic groups. The score is the value of the new coverage divided by the effective cost. The effective cost uses: -- базовую сложность стратегии; -- p95 latency; -- штраф нестабильности; -- веса режима Speed/Balanced/Stability. +- the base complexity of the strategy; +- the p95 latency; +- a penalty for instability; +- the weights of the Speed, Balanced or Stability mode. -При равенстве учитываются количество маршрутов, минимальный success rate, p95 и стабильный ID. +If two scores are equal, the optimizer compares the number of routes, the minimum success rate, the p95 and the stable ID. ### 13.4. Best effort -Если строгий порог не достигнут, `BestEffortCoverageOptimizer` может выбрать «лучшего из худших», но только когда стратегия наблюдаемо улучшает baseline. Приоритет: +If no strategy reaches the strict threshold, `BestEffortCoverageOptimizer` can select the "best of the bad" strategy. It does this only if the strategy gives a better result than the baseline. The priority is: -1. число targets, прошедших строгий порог; -2. число targets хотя бы с одним успехом; -3. суммарное число успешных запросов; -4. общий success rate; -5. p95; -6. базовая стоимость и ID. +1. the number of targets that pass the strict threshold; +2. the number of targets with at least one success; +3. the total number of successful requests; +4. the general success rate; +5. the p95; +6. the base cost and the ID. -Best-effort не маскируется под валидированный результат. Отчёт и профиль получают `IsBestEffort`, а issues — `BEST_EFFORT_CONFIG`. Если ни одна стратегия не улучшает baseline, пресет не генерируется и возвращается `NO_EFFECTIVE_STRATEGY`. +A best-effort result does not look like a validated result. The report and the profile get `IsBestEffort`, and the issues get `BEST_EFFORT_CONFIG`. If no strategy improves the baseline, the add-on does not make a preset and returns `NO_EFFECTIVE_STRATEGY`. -### 13.5. Профили и fallback +### 13.5. Profiles and fallback -`ProfilePlanner` объединяет назначения только при совпадении: +`ProfilePlanner` merges assignments only if these items are the same: -- нормализованного плана стратегии; -- версии IP; -- транспорта; -- порта; -- L7; -- признака best-effort. +- the normalized strategy plan; +- the IP version; +- the transport; +- the port; +- the L7 protocol; +- the best-effort flag. -Домены прямого ввода попадают в `--hostlist-domains`. Цели из файлов сохраняют исходные пути и попадают в `--hostlist`. Более специфичные доменные профили записываются раньше общих. +Domains from the direct input go to `--hostlist-domains`. Targets from files keep their original paths and go to `--hostlist`. More specific domain profiles come before general profiles. -`CircularFallbackPlanner` добавляет только стратегии, которые: +`CircularFallbackPlanner` adds only strategies that: -- успешны на всех целях профиля; -- поддерживают circular; -- не требуют pre-host; -- имеют отличающуюся фактическую сигнатуру. +- are successful on all targets of the profile; +- support circular; +- do not require pre-host; +- have a different actual signature. -Наличие нескольких отдельно успешных стратегий само по себе не означает, что их комбинация рабочая. Поэтому после синтеза обязательно проверяется вся строка пресета. +The primary strategy of the profile must also support circular and must not require pre-host. Several strategies that operate separately do not always operate together. Thus the add-on always checks the complete preset command line after the synthesis. -## 14. Генерация аргументов Zapret2 +## 14. Zapret2 arguments -`Zapret2ConfigWriter` формирует одну строку запуска. +`Zapret2ConfigWriter` makes one command line. -Перед профилями выводятся общие WinDivert-фильтры: +Before the profiles, the writer adds the common WinDivert filters and libraries: -- `--wf-tcp-out` / `--wf-udp-out`; -- входящие порты только при необходимости circular или inbound-стратегии; -- общие blobs; +- `--wf-tcp-out` and `--wf-udp-out`; +- inbound ports only for a circular or inbound strategy; +- the common blobs; - `--lua-init=@lua/zapret-lib.lua`; - `--lua-init=@lua/zapret-antidpi.lua`; -- `--lua-init=@lua/zapret-auto.lua`, если используется circular. +- `--lua-init=@lua/zapret-auto.lua` if a profile uses circular. -Профили разделяются `--new` и получают явные: +`--new` separates the profiles. Each profile gets explicit values for: - `--filter-l3`; -- `--filter-tcp` либо `--filter-udp`; +- `--filter-tcp` or `--filter-udp`; - `--filter-l7`; -- `--hostlist` либо `--hostlist-domains`; +- `--hostlist` or `--hostlist-domains`; - `--name`. -Путь каждого `--hostlist` принудительно заключается в двойные кавычки, в том числе без пробелов. Кавычки внутри значения экранируются. +The writer always puts the path of each `--hostlist` in double quotes, also when the path has no spaces. It escapes quotes in the value. + +For each Lua action, the writer adds the sticky filters `--payload`, `--in-range` and `--out-range` again. The line is longer, but no action can get the state of an earlier action or profile by accident. -Для каждого Lua-действия заново выводятся sticky-фильтры `--payload`, `--in-range` и `--out-range`. Это делает строку длиннее, но не позволяет действию случайно унаследовать состояние предыдущего действия или профиля. +A circular profile gets one common `circular` instance, numbered actions `strategy=N` and `final` on the last fallback strategy, if the writer options permit it. -Circular-профиль получает общий `circular` instance, пронумерованные действия `strategy=N` и `final` у последней резервной стратегии, если это разрешено параметрами writer. +## 15. Check and repair of the ready preset -## 15. Проверка и repair готового пресета +`BlockCheckPresetValidationService` starts the complete synthesized command line and checks all active targets again. -`BlockCheckPresetValidationService` запускает синтезированную строку целиком и повторно проверяет все активные targets. +If the candidate fails, the service does these steps: -Если кандидат не прошёл: +1. It finds the profiles and the actual strategy signatures of the failed targets. +2. It removes these strategy and route pairs from the original evidence. +3. It runs the synthesis again. +4. It checks the new candidate completely. -1. неуспешные targets сопоставляются с профилями и фактическими сигнатурами стратегий; -2. соответствующие стратегия/маршрут исключаются из исходной evidence; -3. синтез запускается повторно; -4. новый кандидат снова проверяется целиком. +The mode limits the number of repair iterations. The possible final issues are `PRESET_VALIDATION_FAILED`, `PRESET_REPAIR_LIMIT_REACHED`, `PRESET_REPAIR_STALLED` and `PRESET_REPAIR_NO_CANDIDATE`. A successful repair adds `PRESET_REPAIRED`. -Цикл ограничен режимом. Возможные терминальные issues: `PRESET_VALIDATION_FAILED`, `PRESET_REPAIR_LIMIT_REACHED`, `PRESET_REPAIR_STALLED`, `PRESET_REPAIR_NO_CANDIDATE`. Успешный repair добавляет `PRESET_REPAIRED`. +An infrastructure failure does not prove that a strategy does not operate. The service records it separately. -Infrastructure failure не считается доказательством неработоспособности стратегии и отмечается отдельно. +## 16. Cancel and failure processing -## 16. Отмена и обработка отказов +A manual stop sets the session to `Canceling` and cancels the current request. The scan service then: -Ручная остановка переводит сессию в `Canceling` и отменяет текущий запрос. Scan service: +- stops the temporary Zapret2 process; +- keeps the completed baseline and strategy probes; +- adds `RUN_CANCELED` or `SCAN_CANCELED`; +- tries to run the synthesis with the available data; +- makes a partial report. -- останавливает временный Zapret2; -- сохраняет уже завершённые baseline и strategy probes; -- добавляет `RUN_CANCELED`/`SCAN_CANCELED`; -- пытается выполнить синтез по доступным данным; -- создаёт частичный отчёт. +If the cancel occurs during the preset check, the report contains the scan results and the first synthesized candidate. The report does not contain the preset check attempts or the repair candidates. `WasCanceled` prevents a full success. -Если отмена произошла во время валидации, отчёт всё равно содержит результаты сканирования и последний синтезированный кандидат. `WasCanceled` не позволяет считать его полностью успешным. +An unhandled exception outside the usual limits shows as an operation error. An error of the automatic history save does not delete the report in memory. The UI shows this error separately. -Необработанное исключение вне штатных границ отображается как ошибка операции. Ошибка автоматического сохранения истории не уничтожает находящийся в памяти отчёт и показывается отдельно. +## 17. Time estimates -## 17. Оценка времени +### 17.1. Before the run -### 17.1. До запуска +`BlockCheckMaximumDurationEstimator` calculates a conservative limit. It assumes that 80% of the requests fail. A failed request takes the full `RequestTimeoutSeconds`. -`BlockCheckMaximumDurationEstimator` рассчитывает консервативный предел, предполагая 80% неудачных запросов. Неудачный запрос занимает полный `RequestTimeoutSeconds`. +The estimate includes: -В расчёт входят: +- all baseline requests; +- the applicable strategy jobs; +- the number of repeats for each target; +- the startup grace of each process; +- the first preset check; +- the maximum number of repair checks. -- все baseline requests; -- применимые strategy jobs; -- число повторов на target; -- startup grace каждого процесса; -- первая проверка пресета; -- максимальное число repair-проверок. +With the early stop, the estimate assumes that one strategy of five is successful. Thus the estimate includes the number of jobs that it expects for the limit of successful strategies. With `TestAllStrategies`, the estimate includes the complete selected tier. -При ранней остановке предполагается, что успешна одна стратегия из пяти; поэтому оценка включает количество jobs, необходимое для ожидаемого достижения лимита успешных стратегий. При `TestAllStrategies` учитывается весь выбранный tier. +### 17.2. During the run -### 17.2. Во время работы +The ETA of the current stage uses an exponential moving average with `alpha = 0.2`. The UI also estimates the total remaining time. During the baseline, the add-on does not have enough data. Thus the UI shows "Calculating the remaining time…" and not an invented number. -ETA текущего этапа строится по exponential moving average с `alpha = 0.2`. Отдельно оценивается общий остаток. На baseline, когда ещё нет достаточного количества наблюдений, UI выводит «Расчёт оставшегося времени…», а не выдуманное число. +The counters of successes, errors, completed requests and found strategies change after each attempt. -Промежуточные счётчики успехов, ошибок, завершённых запросов и найденных стратегий обновляются по мере выполнения попыток. +## 18. Report -## 18. Отчёт +The JSON schema version is `2`. `BlockCheckReport` contains: -Текущая JSON-схема — `2`. `BlockCheckReport` содержит: +- the UTC time and the catalog version; +- the mode, `Success`, `WasCanceled` and `IsBestEffort`; +- the normalized targets and the runtime routes; +- the baseline, strategy and preset-validation probes; +- the final profiles and their scope; +- the primary and fallback IDs; +- the validation candidates; +- the issues; +- the ready line `PresetArguments`. -- UTC-время и версию каталога; -- режим, `Success`, `WasCanceled`, `IsBestEffort`; -- нормализованные targets и runtime routes; -- baseline, strategy и preset-validation probes; -- итоговые профили и их scope; -- primary/fallback ID; -- validation candidates; -- issues; -- готовую строку `PresetArguments`. +The report writes an undefined median or p95 as `null`. Thus the JSON does not contain `Infinity` or `NaN`. -Неопределённые median/p95 записываются как `null`, поэтому JSON не содержит `Infinity` или `NaN`. +These formats are available: -Поддерживаются: +- JSON export for later work in the application; +- a text diagnostic report; +- a TXT file with the preset arguments. -- JSON-экспорт для повторной работы в приложении; -- текстовый диагностический отчёт; -- TXT с аргументами пресета. +The TXT export with arguments requires a successful or explicitly best-effort report with a line that is not empty. -TXT с аргументами разрешён для успешного или явно best-effort отчёта с непустой строкой. +## 19. Report import -## 19. Импорт отчёта +`BlockCheckReportImportService` reads the file as strict UTF-8. It finds the format automatically: text that starts with `{` is JSON, other text is a text report. The service rejects an empty file and a file larger than 64 MiB. -`BlockCheckReportImportService` автоматически различает JSON и текст. Максимальный размер — 64 MiB. Для JSON проверяются schema version, коллекции, enum-значения, идентификаторы, ссылки на targets и разумные пределы количества элементов. +The service checks the schema version (only `2`), the collections, the enum values, the IDs, the target references and the item counts: -При импорте: +| Item | Limit | +|---|---:| +| `Targets` | 20 000 | +| `Probes` | 200 000 | +| `Profiles` | 20 000 | +| `AttemptCount` of one probe record | 0–1 000 | -- загружается текущий каталог; -- восстанавливаются targets и probes; -- профили связываются со стратегиями по ID; -- создаётся `BlockCheckResultSession` для ручной работы; -- отсутствующие файлы списков и неизвестные стратегии оформляются предупреждениями; -- несовпадение версии каталога не запрещает просмотр, но неизвестную стратегию нельзя редактировать или тестировать. +The service also checks `PresetArguments` with `ConfigShareService.EnsureNoOutsideWrites`. The tray runs the preset of a report with administrator rights. Thus the service rejects a report with options that write files: `--debug=@…`, `--wf-save`, `--hostlist-auto` and `--hostlist-auto-debug`. The synthesis never writes these options. -Текстовый отчёт содержит меньше структурных данных. Его parser восстанавливает возможный scope по существующим спискам сайтов и не назначает отсутствующий список всем targets, потому что это создало бы ложную точность. +During the import, the service: -## 20. Недавние подборы и Store +- loads the current catalog; +- restores the targets and the probes; +- connects the profiles to the strategies by ID. A profile with an unknown primary strategy is not restored; +- makes the attempts again from the stored counters. Each successful attempt gets the median time; +- makes a `BlockCheckResultSession` for manual work; +- adds warnings for missing list files and unknown strategies. -История хранится в: +A different catalog version does not prevent the view. But the user cannot edit or test an unknown strategy. + +A text report contains less structured data. Its parser finds the possible scope from the site lists that exist. It does not assign a missing list to all targets, because that would give a false precision. + +## 20. Recent selections and the Store + +The history is in this folder: ```text Store/Items/BlockCheck2 ``` -Старый тестовый каталог `Reports/BlockCheck2` не мигрируется и не читается. +The add-on does not migrate or read the old test folder `Reports/BlockCheck2`. -Имя файла включает UTC timestamp, режим, статус и короткий GUID. Сохраняется не более 50 JSON-отчётов; более старые удаляются после успешной записи нового. +The file name contains the UTC time, the mode, the status and a short GUID. The history keeps a maximum of 50 JSON reports. After a successful write of a new report, the service deletes the older reports. -Store-элемент: +The Store item: -| Поле | Значение | +| Field | Value | |---|---| | `Id` | `BlockCheck2` | -| `Name` / `ShortName` | `Недавние подборы для Zapret2` | +| `Name` / `ShortName` | The localized string `RecentBlockCheck2Selections` | | `Type` | `CDPIUIUpdateItem` | | `VersionControlType` | `local` | | `Developer` | `CDPIUI` | -`DatabaseInitializationService.QuickRestore` вызывает `RegisterOnFirstLaunch`. После первой успешной регистрации выставляется `BLOCKCHECK/historyStoreItemRegistrationCompleted=true`; поэтому удаление элемента пользователем через настройки памяти Store не отменяется на каждом следующем старте. +`DatabaseInitializationService.QuickRestore` calls `RegisterOnFirstLaunch`. After the first successful registration, the service sets `BLOCKCHECK/historyStoreItemRegistrationCompleted=true`. Thus the next start does not add the item again after the user removes it in the Store memory settings. -Перед сохранением нового отчёта `EnsureRegistered` создаёт каталог и Store-запись при необходимости. То есть удалённая пользователем история остаётся удалённой до тех пор, пока BlockCheck2 снова не создаст новые данные. +Before the service saves a new report, `EnsureRegistered` makes the folder and the Store record if necessary. Thus a history that the user removed stays removed until BlockCheck2 makes new data. -Удаление Store-элемента использует штатный `StoreHelper.RemoveItem`: удаляется каталог с отчётами и запись базы Store. +The removal of the Store item uses the usual `StoreHelper.RemoveItem`. It deletes the report folder and the Store database record. -Страница недавних подборов позволяет: +The page of recent selections lets the user: -- открыть запись в окне результата; -- открыть произвольный JSON/TXT-отчёт; -- показать файл в Explorer; -- удалить отдельный отчёт с подтверждением; -- открыть каталог; -- обновить список. +- open a record in the result window; +- open a JSON or TXT report from a file; +- show the file in Explorer; +- delete one report after a confirmation; +- open the folder; +- refresh the list. -## 21. Интеграция в «Средства Автоподбора» +## 21. Integration into the auto-selection tools -BlockCheck2 встроен в `CDPIUI.CreateConfigUtilWindow` и доступен только для установленного Zapret2. +BlockCheck2 is a part of `CDPIUI.CreateConfigUtilWindow`. It is available only when Zapret2 is installed. -Мастер использует `AnimatedHorizontalContentViewer`. Заголовок и описание принадлежат `AnimatedHorizontalContentItem`, а не дублируются внутри страницы. +The wizard uses `AnimatedHorizontalContentViewer`. The title and the description belong to `AnimatedHorizontalContentItem`. The page does not show them again. -Страницы мастера: +The wizard pages are: -1. списки сайтов и пользовательский ввод; -2. режим подбора; -3. дополнительные параметры; -4. проверка параметров и максимального времени; -5. прогресс; -6. завершение. +1. site lists and user input; +2. the selection mode; +3. more parameters; +4. the parameter review and the maximum duration; +5. progress; +6. completion. -Возврат в главное меню поддерживается как из мастера, так и со страницы недавних подборов. При закрытии окна во время работы используется общий диалог остановки средств подбора. +The user can go back to the main menu from the wizard and from the page of recent selections. If the user closes the window during a run, the common stop dialog of the selection tools opens. -Публичные protocol routes: +The public protocol routes are: ```text cdpiui://Tools/AutoConfig/Zapret2 cdpiui://Tools/AutoConfig/Zapret2/Reports ``` -Маршруты входят в явный allowlist `CommandUriMapper` и открывают `CreateConfigUtilWindow` с нужным action. +The routes are in the explicit allow list of `CommandUriMapper`. They open `CreateConfigUtilWindow` with the necessary action. -## 22. Окно результата +## 22. Result window -`BlockCheck2ResultWindow` — отдельное MikaAlt-окно с общей MenuBar, статусной строкой и центром компактных уведомлений. Структура строк окна: +`BlockCheck2ResultWindow` is a separate window with the Mica Alt backdrop. It has a common MenuBar, a status bar and a center for compact notifications. The rows of the window are: ```text TitleBar @@ -538,171 +567,140 @@ Content Frame Status bar ``` -Основные разделы `AnimatedSelectorBar`: +`ResultPage` has these sections in an `AnimatedSelectorBar`: -- «Результат подбора»; -- «Сборка пресета»; -- «Диагностические сведения». +- "Selection result"; +- "Preset builder"; +- "Diagnostic data". -### 22.1. Результат подбора +### 22.1. Selection result -Стратегии показываются по сайтам. `SemanticZoom` переключает полный список и обзор групп. Доступны поиск, фильтр по успешным/частичным/неуспешным результатам и сортировки по результату, сайту и времени ответа. +The page shows the strategies by site. `SemanticZoom` switches between the full list and the group overview. The user can search, filter by successful, partial and failed results, and sort by result, site and response time. -В строке отображаются фактическое имя стратегии, URL, отдельные сведения о соединении, число успехов, success rate, response time, HTTP status, failure code и признак выбора в пресет. +A row shows the actual strategy name, the URL, the separate connection data, the number of successes, the success rate, the response time, the HTTP status, the failure code and a flag if the preset uses the strategy. -Символ предупреждения означает, что стратегия прошла текущую строку, но не доказана для всей runtime-области, например для соседней TLS-версии. Его назначение поясняется ToolTip. +A warning symbol means that the strategy passed the current row, but the add-on did not prove it for the complete runtime area, for example for the other TLS version. A ToolTip explains the symbol. -Стратегию можно: +The user can: -- протестировать отдельно через временный профиль и `curl`; -- запустить как временный компонент для ручной проверки пользователем; -- добавить для текущего сайта; -- добавить для связанного списка сайтов; -- скопировать как действия либо как полную строку тестового профиля. +- test the strategy separately with a temporary profile and `curl`; +- start it as a temporary component for a manual check; +- add it for the current site; +- add it for the related site list; +- copy it as actions or as the full command line of the test profile. -### 22.2. Сборка пресета +### 22.2. Preset builder -Автоматически предложенные profiles заранее проецируются в список назначений. Пользователь может удалить, переупорядочить или дополнить их. Порядок назначений внутри одного scope определяет primary и circular fallbacks. +The builder shows the automatic profiles as assignments. The user can remove, move or add assignments. The order of assignments in one scope sets the primary strategy and the circular fallbacks. -Manual builder проверяет: +The manual builder checks: -- существование стратегии и цели; -- применимость ко всей области; -- поддержку circular; -- отсутствие дубликатов фактического плана; -- наличие evidence; -- прохождение порога на всех целях scope; -- пересечение scopes. +- that the strategy and the target exist; +- that the strategy applies to the complete scope; +- circular support; +- that no actual plan is a duplicate; +- that evidence exists; +- that all targets of the scope pass the threshold; +- that the scopes do not overlap. -Одинаковые предупреждения агрегируются по code/message/subjects и показываются один раз со счётчиком. Для `MANUAL_SCOPE_OVERLAP` доступно быстрое подтверждение предупреждения на уровне конструктора; это не скрывает другие категории ошибок и не изменяет назначение стратегии автоматически. +The builder shows identical warnings once, with a counter. It groups them by code, message and subjects. For `MANUAL_SCOPE_OVERLAP`, the user can quickly accept the warning in the builder. This does not hide other error types and does not change the assignment. -Предпросмотр строки использует `TextControlBox` через общий редактор и автоформатирование по флагам. Кнопка перехода к готовому пресету находится в области сборки и в меню действия, но ведёт на одну и ту же страницу review. +The preview of the command line uses `TextControlBox` in the common editor, with automatic format by flags. The button that opens the ready preset is in the builder area and in the action menu. Both open the same review page. -### 22.3. Диагностика +### 22.3. Diagnostics -Раздел содержит общие issues отчёта и полный список probes. Предупреждения ручного конструктора не смешиваются с диагностикой редактора строки. +The section shows the issues of the report and the full list of probes. The warnings of the manual builder are separate from the diagnostics of the command line editor. -## 23. Черновик и просмотр готового пресета +## 23. Draft and ready preset review -`BlockCheck2PresetDraft` хранит две формы: +`BlockCheck2PresetDraft` keeps two forms: -- структурированную строку, построенную автоматическим или ручным конструктором; -- временную expert-строку. +- the structured command line from the automatic or manual builder; +- a temporary expert command line. -Origin принимает `Automatic`, `Manual` или `ModifiedAutomatic`. Структурированные назначения остаются источником истины до включения expert editing. +The `Origin` value is `Automatic`, `Manual` or `ModifiedAutomatic`. The structured assignments are the source of truth until the user enables expert editing. -На `PresetReviewPage` строка по умолчанию read-only. Флаг «Разрешить редактирование» после подтверждения включает экспертный режим и панель команд ConfigMaker. Возврат к конструктору после изменения экспертной строки требует подтверждения и удаляет expert changes, потому что произвольный текст невозможно без потерь преобразовать обратно в назначения. +On `PresetReviewPage`, the command line is read-only by default. The "Allow editing" flag enables the expert mode and the ConfigMaker command bar after a confirmation. A return to the builder after a change of the expert line requires a confirmation and deletes the expert changes. The reason: the add-on cannot convert free text back to assignments without data loss. -Панели «Используемые файлы» и «Группы пресета» являются частью редактора: +The "Used files" and "Preset groups" panels are a part of the editor: -- файлы сгруппированы как TXT-списки, библиотеки и payloads; -- группы показывают общие фильтры, профильные фильтры и последовательность действий; -- для Zapret2 флаги получают контекстные объяснения фактических значений; -- двойной щелчок по элементу группы переводит к точному occurrence в редакторе, включая дублирующиеся флаги; -- панели динамически обновляются при изменении строки. +- the files are in groups: TXT lists, libraries and payloads; +- the groups show the common filters, the profile filters and the sequence of actions; +- for Zapret2, the flags get explanations of their actual values; +- a double-click on a group element moves the editor to the exact occurrence, also for duplicate flags; +- the panels change when the command line changes. -Для файла доступны «Открыть», «Показать в папке» и, если файл отсутствует, «Заменить». TXT открывается через настройку приложения; если приложение не выбрано, используется `EditSitelistAskApplicationContentDialog`. +For a file, the commands "Open", "Show in folder" and, for a missing file, "Replace" are available. The editor opens a TXT file with the application from the settings. If no application is set, `EditSitelistAskApplicationContentDialog` opens. -Замена отсутствующего файла оборачивает `ConfigImportAutoCorrector`: +The replacement of a missing file uses `ConfigImportAutoCorrector`: -- пользователь выбирает автокоррекцию либо файл вручную; -- допустима рекомендация создать пустой файл; -- заменяются все упоминания исходного пути в строке, а не первое; -- дерево файлов и группы обновляются после замены. +- the user selects the automatic correction or a file; +- an empty file can be the recommendation; +- the editor replaces all occurrences of the original path, not only the first one; +- the file tree and the groups change after the replacement. -`ConfigMakerUserControl` получает справку Zapret2 через `--help` в Tray/ConPTY, что позволяет работать и с компонентами, требующими UAC. Parser восстанавливает логические строки из управляющих последовательностей терминала, группы help, продолжения длинных описаний и флаги без описания. Неизвестный `--help` не считается фатальным, если процесс всё равно вывел перечень опций. +`ConfigMakerUserControl` gets the Zapret2 help through the tray and ConPTY. Thus it can also use components that require UAC. The parser restores the logical lines from the terminal control sequences, the help groups, the continued long descriptions and the flags without a description. An unknown `--help` is not fatal if the process shows the list of options. -Редактор предоставляет поиск, замену, масштаб, шрифт, фон, форматирование по флагам, warnings/errors без отвлекающей анимации и временный запуск конфигурации. +The editor has search, replace, zoom, font, background, format by flags, warnings and errors without distracting animation, and a temporary run of the config. -## 24. Сохранение пресета +## 24. Preset save ### 24.1. TXT -Текущую effective-строку можно сохранить как обычный TXT без изменения хранилища CDPIUI. +The user can save the current effective line as a usual TXT file. This does not change the CDPI UI storage. -### 24.2. В приложение +### 24.2. In the application -Перед сохранением пользователь вводит имя. `BlockCheck2PresetStorageService`: +Before the save, the user types a name. `BlockCheck2PresetStorageService` does these steps: -1. проверяет наличие непустого пресета и установленного Zapret2; -2. извлекает `--hostlist`, `--hostlist-exclude`, `--lua-init` и файловые `--blob`; -3. разрешает абсолютные, component-relative и `$GETCURRENTDIR()` пути; -4. оставляет библиотеки/payloads внутри каталога Zapret2 component-relative; -5. копирует внешние списки в `LocalUserStorage/List/BlockCheck2/`; -6. копирует внешние библиотеки и payloads в `LocalUserStorage/Bin/BlockCheck2/`; -7. переписывает все ссылки на `$GETCURRENTDIR()/List/...` или `$GETCURRENTDIR()/Bin/...`; -8. создаёт JSON `ConfigItem` пользователя, связанный с Zapret2; -9. обновляет список конфигураций компонента. +1. It makes sure that the preset is not empty and that Zapret2 is installed. +2. It finds `--hostlist`, `--hostlist-exclude`, `--lua-init` and file `--blob` values. +3. It resolves absolute, component-relative and `$GETCURRENTDIR()` paths. +4. It keeps libraries and payloads in the Zapret2 folder as component-relative paths. +5. It copies external lists to `LocalUserStorage/List/BlockCheck2/`. +6. It copies external libraries and payloads to `LocalUserStorage/Bin/BlockCheck2/`. +7. It changes all references to `$GETCURRENTDIR()/List/...` or `$GETCURRENTDIR()/Bin/...`. +8. It makes a user JSON `ConfigItem` for Zapret2. +9. It refreshes the config list of the component. -Русское и иное Unicode-имя пресета сохраняется как display name. Идентификатор каталога и имя JSON транслитерируются в ASCII, очищаются и дополняются Unix timestamp, например `Novyi_preset_1786437896.json`. Имена копируемых файлов также транслитерируются; коллизии получают суффикс `_2`, `_3` и т. д. +The service keeps a Russian or other Unicode preset name as the display name. It transliterates the folder ID and the JSON name to ASCII, removes unsafe characters and adds a Unix time, for example `Novyi_preset_1786437896.json`. It also transliterates the names of the copied files. A name collision gets the suffix `_2`, `_3` and so on. -При ошибке незавершённые каталоги зависимостей удаляются. Исходные файлы пользователя не изменяются. +If an error occurs, the service deletes the incomplete dependency folders. It does not change the original user files. -## 25. Локализация +## 25. Localization -Основные строки находятся в: +The main strings are in these files: ```text CDPIUI/Strings/ru/Resources.resw -CDPIUI/Strings/en-US/Resources.resw +CDPIUI/Strings/en-us/Resources.resw ``` -Русское «Пресеты» соответствует английскому «Configs». Новые UI-элементы должны получать значения через `l:Uids.Uid` или `ILocalizer`, а технические ID, issue codes, report keys и strategy IDs не локализуются. - -## 26. Регрессионный проект +The Russian word "Пресеты" is "Configs" in English. New UI elements must get their text through `l:Uids.Uid` or `ILocalizer`. Do not translate technical IDs, issue codes, report keys or strategy IDs. -`CDPIUI.AddOns.BlockCheck2.Regression` — консольный проект без UI. Обычный запуск выполняет набор детерминированных проверок с fake runners и probes. +## 26. Tests -Покрываются, в частности: - -- валидность каталога; -- runtime-группировка TLS; -- атомарность списков; -- baseline и DNS ignore; -- ранняя остановка и принудительный полный перебор; -- сохранение частичных результатов при ошибке/отмене; -- применение пользовательского числа запросов ко всем стадиям; -- strict и best-effort synthesis; -- circular fallback; -- combined validation и repair; -- отчёты и импорт; -- ручной конструктор и тест отдельной стратегии; -- quoting `--hostlist` и sticky filters. - -Вспомогательные режимы: - -```text ---catalog-summary ---curl-capabilities ---probe [path] [4|6] ---import-report -``` - -Пример проверки: - -```powershell -dotnet run --project .\CDPIUI.AddOns.BlockCheck2.Regression\CDPIUI.AddOns.BlockCheck2.Regression.csproj -``` +The solution has no separate test project for BlockCheck2. `CDPIUI.Tests` has no BlockCheck2 tests at this time. The core does not use WinUI or the tray. Thus tests can use fake `IBlockCheckStrategyRunner`, `IBlockCheckProbeRunner` and `ICommandExecutor` implementations. `CDPIUI.AddOns` gives `CDPIUI.Tests` access to internal types through `InternalsVisibleTo`. -## 27. Известные ограничения +## 27. Known limits -1. Успех curl доказывает только конкретный URL, протокол и версию IP. Он не моделирует ECH, размер ClientHello, браузерный HTTP/2/HTTP/3 fallback, загрузку subresources и JavaScript. -2. Browser-сервис может оставаться неработоспособным, если основная страница проходит, а API, CDN, video/media hosts или альтернативная версия IP не входят в список. -3. QUIC существует в модели и каталоге, но скрыт в стандартном UI до завершения пользовательского сценария и проверки доступности HTTP/3 curl. -4. Discord voice и произвольный UDP пока не поддерживаются. Для них нужен другой probe: захват адресов/портов, управляемый запуск клиента и критерий реального media handshake. -5. Автоматический circular fallback всё равно может зависеть от сетевого состояния; поэтому combined validation обязательна. -6. Текстовый отчёт менее точен, чем JSON. Для последующего редактирования предпочтителен JSON. -7. Imported report может ссылаться на отсутствующие локальные файлы или стратегии удалённой версии каталога; просмотр доступен, но пресет требует repair. -8. Временные per-site списки сохраняются, пока на них ссылается доступный результат. После сохранения пресета зависимости копируются в постоянное пользовательское хранилище. +1. A curl success proves only the exact URL, protocol and IP version. curl does not model ECH, the ClientHello size, the HTTP/2 or HTTP/3 fallback of a browser, subresources or JavaScript. +2. A service can stay unavailable in a browser when the main page passes but the list does not include the API, CDN, video or media hosts, or the other IP version. +3. QUIC is in the model and the catalog. The standard UI hides it until the user scenario is complete and the HTTP/3 support of curl is verified. +4. Discord voice and random UDP are not supported. They need a different probe: address and port capture, a controlled client start and a criterion for a real media handshake. +5. The automatic circular fallback can depend on the network state. Thus the combined check is mandatory. +6. A text report is less precise than JSON. Use JSON for later editing. +7. An imported report can refer to local files that are missing or to strategies of a removed catalog version. The user can view it, but the preset needs a repair. +8. The temporary per-site lists stay while an available result refers to them. When the user saves the preset, the service copies the dependencies to the permanent user storage. -## 28. Основные точки расширения +## 28. Extension points -Для добавления нового probe transport необходимо реализовать `IBlockCheckProbeRunner` и при необходимости `IBlockCheckProbePreflight`. +To add a new probe transport, implement `IBlockCheckProbeRunner` and, if necessary, `IBlockCheckProbePreflight`. -Для другого способа запуска компонента реализуется `IBlockCheckStrategyRunner`. Ядро AddOn не зависит от WinUI или Tray. +To start the component in a different way, implement `IBlockCheckStrategyRunner`. The add-on core does not depend on WinUI or the tray. -Новые стратегии добавляются в seed JSON либо генератор каталога. Для каждой стратегии необходимы корректные applicability, tier, cost, ordered actions и признаки circular/pre-host/inbound. +Add new strategies to the seed JSON or to the catalog expander. Each strategy must have correct applicability, tier, cost, ordered actions and circular, pre-host and inbound flags. -Новый формат отчёта требует увеличения `SchemaVersion`, отдельной миграции в `BlockCheckReportImportService` и регрессионных примеров. Молчаливое принятие неизвестной схемы запрещено. +A new report format requires a higher `SchemaVersion`, a migration in `BlockCheckReportImportService` and test examples. The import must not accept an unknown schema silently. -При изменении параметров режима требуется сохранять согласованность `Scan.AttemptsPerTarget`, `Synthesis.MinimumAttempts` и `Validation.AttemptsPerTarget`; иначе preflight возвращает `RUN_ATTEMPTS_MISMATCH` или `RUN_VALIDATION_ATTEMPTS_MISMATCH` до запуска процессов. +If you change the mode parameters, keep `Scan.AttemptsPerTarget` and `Validation.AttemptsPerTarget` not less than `Synthesis.MinimumAttempts`. Otherwise, the preflight returns `RUN_ATTEMPTS_MISMATCH` or `RUN_VALIDATION_ATTEMPTS_MISMATCH` before any process starts. diff --git a/CDPIUI.AddOns/BlockCheck2/Reporting/BlockCheckReportImportService.cs b/CDPIUI.AddOns/BlockCheck2/Reporting/BlockCheckReportImportService.cs index b58a9073..faeaf79e 100644 --- a/CDPIUI.AddOns/BlockCheck2/Reporting/BlockCheckReportImportService.cs +++ b/CDPIUI.AddOns/BlockCheck2/Reporting/BlockCheckReportImportService.cs @@ -113,6 +113,20 @@ public BlockCheckReportImportResult Import(BlockCheckReport sourceReport) catalog, importIssues); + // The preset of a report runs in the tray with administrator rights: a report from + // someone else must not make the component write files. The synthesizer + // never writes such options, so any of them means the report was edited. + try + { + CDPIUI.AddOns.ConfigShare.ConfigShareService.EnsureNoOutsideWrites( + sourceReport.PresetArguments, + Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); + } + catch (CDPIUI.AddOns.ConfigShare.ConfigShareException exception) + { + throw new InvalidDataException(exception.Message, exception); + } + BlockCheckReport report = CopyWithIssues(sourceReport, sourceReport.Issues .Concat(importIssues) .DistinctBy(issue => (issue.Severity, issue.Code, issue.Message, issue.SubjectId)) @@ -167,8 +181,8 @@ private static void ValidateReport(BlockCheckReport report) { throw new InvalidDataException("The BlockCheck2 report contains null collections."); } - if (report.Targets.Count > 100_000 || report.Probes.Count > 2_000_000 || - report.Profiles.Count > 100_000) + if (report.Targets.Count > 20_000 || report.Probes.Count > 200_000 || + report.Profiles.Count > 20_000) { throw new InvalidDataException("The BlockCheck2 report contains unreasonable item counts."); } @@ -192,7 +206,7 @@ target.Port is < 1 or > 65535 || probe is null || !targetIds.Contains(probe.TargetId) || string.IsNullOrWhiteSpace(probe.Kind) || - probe.AttemptCount is < 0 or > 10_000 || + probe.AttemptCount is < 0 or > 1_000 || probe.SuccessCount < 0 || probe.SuccessCount > probe.AttemptCount || probe.HttpStatusCodes is null || diff --git a/CDPIUI.AddOns/BlockCheck2/Synthesis/BlockCheckSynthesisService.cs b/CDPIUI.AddOns/BlockCheck2/Synthesis/BlockCheckSynthesisService.cs index 5ecddfc4..f1b5186c 100644 --- a/CDPIUI.AddOns/BlockCheck2/Synthesis/BlockCheckSynthesisService.cs +++ b/CDPIUI.AddOns/BlockCheck2/Synthesis/BlockCheckSynthesisService.cs @@ -41,10 +41,12 @@ public BlockCheckSynthesisResult Synthesize( baselineProbeResults, options, issues); - BlockCheckTarget[] targetsRequiringProfile = targetArray - .GroupBy(target => target.GetRuntimeRouteKey()) - .Where(group => group.Any(target => !baselineAccessibleTargetIds.Contains(target.Id))) - .SelectMany(group => group) + // The units are the same as in the optimizers: an emitted --hostlist covers the whole + // file, so a site from the list that passed the baseline stays in the unit and the chosen + // strategy must not break it. + BlockCheckTarget[] targetsRequiringProfile = BlockCheckTargetGroupBuilder.Build(targetArray) + .Where(group => group.Targets.Any(target => !baselineAccessibleTargetIds.Contains(target.Id))) + .SelectMany(group => group.Targets) .ToArray(); if (targetsRequiringProfile.Length == 0) diff --git a/CDPIUI.AddOns/CDPIUI.AddOns.csproj b/CDPIUI.AddOns/CDPIUI.AddOns.csproj index 6bf46f6f..859e1b01 100644 --- a/CDPIUI.AddOns/CDPIUI.AddOns.csproj +++ b/CDPIUI.AddOns/CDPIUI.AddOns.csproj @@ -27,4 +27,8 @@ + + + + diff --git a/CDPIUI.AddOns/ConfigImport/ConfigImportAutoCorrector.cs b/CDPIUI.AddOns/ConfigImport/ConfigImportAutoCorrector.cs index e3758bea..fe30d07a 100644 --- a/CDPIUI.AddOns/ConfigImport/ConfigImportAutoCorrector.cs +++ b/CDPIUI.AddOns/ConfigImport/ConfigImportAutoCorrector.cs @@ -56,8 +56,16 @@ public bool ShouldSuggestEmptyFile(string missingPath) { string sourceDirectory = Path.GetDirectoryName(sourcePath)!; string fileName = Path.GetFileName(missingPath); + // A .bat in Downloads made this walk the whole folder tree on the UI thread. + var options = new EnumerationOptions + { + RecurseSubdirectories = true, + MaxRecursionDepth = 3, + IgnoreInaccessible = true, + AttributesToSkip = FileAttributes.ReparsePoint, + }; return Directory - .EnumerateFiles(sourceDirectory, fileName, SearchOption.AllDirectories) + .EnumerateFiles(sourceDirectory, fileName, options) .Select(Path.GetFullPath) .FirstOrDefault(path => !path.Equals(missingPath, StringComparison.OrdinalIgnoreCase)); } diff --git a/CDPIUI.AddOns/ConfigImport/ConfigImportService.cs b/CDPIUI.AddOns/ConfigImport/ConfigImportService.cs index 2372ece6..2ce11781 100644 --- a/CDPIUI.AddOns/ConfigImport/ConfigImportService.cs +++ b/CDPIUI.AddOns/ConfigImport/ConfigImportService.cs @@ -195,7 +195,8 @@ public IReadOnlyList FindMatchingTargets( string text; try { - text = File.ReadAllText(filePath); + // The same encoding detection as the Zapret importer: a .bat in cp866 or cp1251 lost its Cyrillic text. + text = CDPIUI.Core.System.BatchFileEncoding.ReadAllText(filePath); } catch { @@ -840,7 +841,7 @@ private static string TranslateBatchValue(string value) => private static IReadOnlyList ReadLogicalLines(string filePath) { - string[] physicalLines = File.ReadAllLines(filePath, Encoding.UTF8); + string[] physicalLines = CDPIUI.Core.System.BatchFileEncoding.ReadAllLines(filePath); var result = new List(); var current = new StringBuilder(); int currentLine = 1; @@ -874,11 +875,17 @@ private static bool HasContinuationCaret(string line) return count % 2 == 1; } - private static bool TryParseSet(string line, out string name, out string value) + internal static bool TryParseSet(string line, out string name, out string value) { Match match = SetRegex().Match(line); name = match.Success ? match.Groups["name"].Value.Trim() : string.Empty; - value = match.Success ? match.Groups["value"].Value.Trim().TrimEnd('"') : string.Empty; + value = match.Success ? match.Groups["value"].Value.Trim() : string.Empty; + if (match.Success && match.Groups["quoted"].Success) + { + int closingQuote = value.LastIndexOf('"'); + if (closingQuote >= 0) + value = value[..closingQuote].TrimEnd(); + } return match.Success && name.Length > 0; } @@ -888,11 +895,15 @@ private static bool TryParseCommentedSet(string line, out string name, out strin return TryParseSet(match.Success ? match.Groups["set"].Value : string.Empty, out name, out value); } - private static bool IsComment(string line) => - line.StartsWith("::", StringComparison.Ordinal) || - line.StartsWith("rem ", StringComparison.OrdinalIgnoreCase) || - line.Equals("rem", StringComparison.OrdinalIgnoreCase) || - line.StartsWith('#'); + private static bool IsComment(string line) + { + // "@rem" and "rem" are comments too, as in the Zapret importer. + string command = line.TrimStart('@'); + return line.StartsWith("::", StringComparison.Ordinal) || + (command.StartsWith("rem", StringComparison.OrdinalIgnoreCase) && + (command.Length == 3 || char.IsWhiteSpace(command[3]))) || + line.StartsWith('#'); + } private static void SetVariable( IDictionary variables, diff --git a/CDPIUI.AddOns/ConfigImport/README.md b/CDPIUI.AddOns/ConfigImport/README.md index 2b988b9a..0e3953c6 100644 --- a/CDPIUI.AddOns/ConfigImport/README.md +++ b/CDPIUI.AddOns/ConfigImport/README.md @@ -1,94 +1,99 @@ -# Config Import AddOn +# Config Import add-on -## Назначение документа +## About this document -Этот документ описывает фактическую реализацию механизма импорта Config-файлов в CDPIUI: от статического анализа исходного файла до тестового запуска и сохранения готового `ConfigItem` в `LocalUserStorage`. +This document describes the import of config files in CDPI UI. The import starts with a static analysis of the source file. It ends with a test run and a saved `ConfigItem` in `LocalUserStorage`. -Основная логика находится в пространстве имен `CDPIUI.AddOns.ConfigImport`. Пользовательский мастер расположен в основном проекте CDPIUI и выступает клиентом AddOn. +The main logic is in the namespace `CDPIUI.AddOns.ConfigImport`. The import wizard is in the `CDPIUI` project. The wizard is a client of the add-on. -Документ предназначен для разработчиков и авторов будущей пользовательской справки. Он описывает текущее поведение, включая ограничения и случаи, в которых результат зависит от выбора компонента или ручного решения пользователя. +This document is for developers and for the authors of the user help. It describes the current behavior, the limits and the cases where the result depends on the selected component or on a decision of the user. -## Цели механизма +## Functions -AddOn решает следующие задачи: +The add-on does these tasks: -1. Импортирует `.bat`, `.cmd`, `.txt` и `.json` без выполнения исходного скрипта. -2. Определяет подходящий установленный компонент по его `executable` и содержимому файла. -3. Преобразует командную строку и переменные исходного файла в модель `ConfigItem` CDPIUI. -4. Использует специализированный статический анализатор для `winws` и `winws2`. -5. Сохраняет редактируемые значения BAT-переменных в `variables`, `commaVars`, `availableCommaVarsValues` и `jparams`. -6. Обнаруживает внешние BIN/TXT/LUA-ресурсы, сгенерированные файлы и отсутствующие зависимости. -7. Предлагает замену отсутствующего файла, либо позволяет явно создать пустой файл. -8. Поддерживает тестовый запуск выбранного компонента с импортированными аргументами. -9. Копирует зависимости в изолированные каталоги `List/Converted`, `Bin/Converted` и `Lua/Converted`. -10. Выполняет встроенное преобразование готовой строки `winws1 -> winws2` при ретаргетинге Config на Zapret 2. +1. It imports `.bat`, `.cmd`, `.txt` and `.json` files. It does not run the source script. +2. It finds the applicable installed component from its `executable` and from the file contents. +3. It converts the command line and the variables of the source file into a CDPI UI `ConfigItem`. +4. It uses a special static analyzer for `winws` and `winws2`. +5. It keeps editable BAT variable values in `variables`, `commaVars`, `availableCommaVarsValues` and `jparams`. +6. It finds external BIN, TXT and LUA resources, generated files and missing dependencies. +7. It suggests a replacement for a missing file. The user can also select an empty file. +8. It lets the user test the selected component with the imported arguments. +9. It copies the dependencies to separate folders: `List/Converted`, `Bin/Converted` and `Lua/Converted`. +10. It converts a ready `winws1` command line to `winws2` when the user moves the config to Zapret 2. +11. It sends `.cdpiconfig` packages to the ConfigShare add-on. Refer to [ConfigShare/README.md](../ConfigShare/README.md). -## Принцип безопасности +## Safety principle -Импорт является статическим. `ConfigImportService` и `ZapretConfigImportService` читают исходный текст, распознают известные конструкции и строят модель результата. Они не запускают `cmd.exe`, PowerShell, исходный BAT/CMD-файл или найденные в нем команды. +The import is static. `ConfigImportService` and `ZapretConfigImportService` read the source text, find known constructs and make a result model. They do not start `cmd.exe`, PowerShell, the source BAT or CMD file or the commands in it. -Это означает, что во время анализа не выполняются: +Thus the analysis does not: -- команды `if`, `for`, `goto`, `copy`, `del`, `reg`, `netsh` и другие команды оболочки; -- загружаемые BAT-файлы как реальные процессы; -- произвольные программы, указанные в исходном скрипте; -- сетевые запросы; -- модификации исходного каталога. +- run `if`, `for`, `goto`, `copy`, `del`, `reg`, `netsh` or other shell commands; +- start called BAT files as processes; +- start programs from the source script; +- send network requests; +- change the source folder. -Кнопка **Тестировать** является отдельной операцией. Она запускает выбранный установленный компонент через `ComponentTasksManager` с вычисленными аргументами. Это не песочница: безопасность статического импорта не распространяется на тестовый запуск исполняемого файла компонента. +The **Test** button is a separate operation. It starts the selected installed component through `ComponentTasksManager` with the calculated arguments. -## Поддерживаемые входные форматы +WARNING: The test run is not a sandbox. The safety of the static import does not apply to the test run of the component executable. -| Формат | Общий компонент | `winws` / Zapret Legacy | `winws2` / Zapret 2 | +## Supported input formats + +| Format | General component | `winws` / Zapret Legacy | `winws2` / Zapret 2 | |---|---:|---:|---:| -| `.bat` | Да | Да, специализированный анализ | Да, специализированный анализ | -| `.cmd` | Да | Да, специализированный анализ | Да, специализированный анализ | -| `.txt` | Да | Да | Да | -| `.json` | Да | Через общий импорт JSON | Через общий импорт JSON | +| `.bat` | Yes | Yes, special analyzer | Yes, special analyzer | +| `.cmd` | Yes | Yes, special analyzer | Yes, special analyzer | +| `.txt` | Yes | Yes, special analyzer | Yes, special analyzer | +| `.json` | Yes | General JSON import | General JSON import | +| `.cdpiconfig` | ConfigShare | ConfigShare | ConfigShare | -Для Zapret специализированный анализатор используется только для BAT/CMD/TXT. JSON загружается общим механизмом `ConfigItem`/`OldConfigItem`. +For Zapret, the special analyzer reads only BAT, CMD and TXT. The general `ConfigItem` and `OldConfigItem` loader reads JSON. `ConfigShareService` reads `.cdpiconfig` packages. -## Состав реализации +## Implementation -### AddOn +### Add-on -| Файл | Ответственность | +| File | Function | |---|---| -| `ConfigImportModels.cs` | Общие контракты результата, target, диагностик, отсутствующих и генерируемых файлов | -| `ConfigImportService.cs` | Фасад импорта, поиск target, общий парсер, вызов Zapret-парсера, ретаргетинг | -| `ZapretConfigImportModels.cs` | Специализированные модели Zapret и параметры анализатора | -| `ZapretConfigImportService.cs` | Статический анализ BAT/CMD/TXT для `winws` и `winws2` | -| `ConfigImportAutoCorrector.cs` | Поиск безопасной замены отсутствующего ресурса | -| `ConfigImportTestPlaceholder.cs` | Подготовка временных файлов и подмена путей перед тестом | -| `ConfigImportInstaller.cs` | Копирование ресурсов, переписывание путей и сохранение Config | +| `ConfigImportModels.cs` | Common contracts: result, target, issues, missing and generated files | +| `ConfigImportService.cs` | Import facade, target detection, general parser, call of the Zapret parser, retarget | +| `ZapretConfigImportModels.cs` | Zapret models and analyzer options | +| `ZapretConfigImportService.cs` | Static analysis of BAT, CMD and TXT for `winws` and `winws2` | +| `ConfigImportAutoCorrector.cs` | Search for a safe replacement of a missing resource | +| `ConfigImportTestPlaceholder.cs` | Temporary files and path replacement before a test | +| `ConfigImportInstaller.cs` | Copies the resources, changes the paths and saves the config | -### Интеграция в CDPIUI +### CDPI UI integration -| Файл | Ответственность | +| File | Function | |---|---| -| `CDPIUI/Views/ConfigImportUtil/MainPage.xaml` | Разметка мастера импорта | -| `CDPIUI/Views/ConfigImportUtil/MainPage.xaml.cs` | Оркестрация анализа, исправления, тестирования и сохранения | -| `CDPIUI/ViewModels/ConfigImportPresetViewModel.cs` | Состояние одного импортируемого Config | -| `CDPIUI/ViewModels/ConfigImportMissingFileViewModel.cs` | Состояние решения для одного отсутствующего файла | -| `CDPIUI/Windows/ConfigImportUtilWindow.*` | Отдельное окно утилиты | -| `CDPIUI/Views/UtilsPage.*` | Точка входа со страницы утилит | -| `CDPIUI/Commands/CommandUriMapper.cs` | Маршруты `cdpiui://Tools/ImportConfig` и `cdpiui://Tools/ImportConfig/{componentId}` | +| `CDPIUI/Views/ConfigImportUtil/MainPage.xaml` | Layout of the import wizard | +| `CDPIUI/Views/ConfigImportUtil/MainPage.xaml.cs` | Controls the analysis, the corrections, the test and the save | +| `CDPIUI/ViewModels/ConfigImportPresetViewModel.cs` | State of one imported config | +| `CDPIUI/ViewModels/ConfigImportMissingFileViewModel.cs` | State of the decision for one missing file | +| `CDPIUI/Helper/AddOns/ConfigImport/ConfigImportHelper.cs` | The file selection dialog | +| `CDPIUI/Windows/ConfigImportUtilWindow.*` | Separate window of the tool | +| `CDPIUI/Views/UtilsPage.*` | Entry point on the tools page | +| `CDPIUI/Commands/CommandUriMapper.cs` | Routes `cdpiui://Tools/ImportConfig` and `cdpiui://Tools/ImportConfig/{componentId}` | -## Общий конвейер +## General sequence ```text -Выбор одного или нескольких файлов +The user selects one or more files | v -Загрузка списка установленных компонентов из Store DB +Load the installed components from the Store DB | v -Поиск target по componentId / executable / сигнатурам TXT +Find the target by componentId / executable / TXT signatures | v -Статический импорт +Static import | | - | generic | winws / winws2 + | general | winws / winws2 v v ConfigImportService ZapretConfigImportService | | @@ -97,148 +102,153 @@ ConfigImportService ZapretConfigImportService ConfigImportResult | v -Обнаружение отсутствующих файлов и запрос решения +Find missing files and ask for a decision | v -Выбор пользователем компонента и имени +The user selects the component and the name | | v v - Тест Сохранение + Test Save | | - временные файлы List/Bin/Lua/Converted + temporary files List/Bin/Lua/Converted | | - запуск компонента Config JSON в - LocalUserStorage + start the component config JSON in + LocalUserStorage ``` -## Модель target +## Target model + +`ConfigImportTarget` contains: + +- `ComponentId`: the unique component ID in the Store DB; +- `DisplayName`: the display name; +- `Executable`: the executable name from the DB, with or without `.exe`; +- `Version`: the current component version; +- `Directory`: the folder of the installed component. -`ConfigImportTarget` содержит: +The wizard shows only Store items of the type `component` that meet these conditions: -- `ComponentId` — уникальный ID компонента в Store DB; -- `DisplayName` — отображаемое имя; -- `Executable` — имя исполняемого файла из БД, с `.exe` или без него; -- `Version` — текущая версия компонента; -- `Directory` — каталог установленного компонента. +1. `Id`, `Directory` and `Executable` are set. +2. The folder exists. +3. The executable exists in the folder. If necessary, the wizard adds the extension `.exe`. -В UI в список target попадают только Store-элементы типа `component`, для которых: +Thus a component that is not installed does not take part in the automatic detection. The wizard cannot test or save a config for it. -1. заданы `Id`, `Directory` и `Executable`; -2. каталог существует; -3. в каталоге существует указанный executable, при необходимости с добавленным расширением `.exe`. +## Automatic component detection -Следовательно, неустановленный компонент не участвует в автоматическом определении и не доступен для тестирования или сохранения через текущий мастер. +`FindMatchingTargets` operates as follows. -## Автоматическое определение компонента +### `.cdpiconfig` -Метод `FindMatchingTargets` работает следующим образом. +The method reads the package with `ConfigShareService.Read`. It selects the target with the component ID from `target[0]` of the packaged config. If the package is not valid, the method returns no target. The import then reports the detailed archive error. ### JSON -Из JSON десериализуется `ConfigItem`, после чего первый элемент `target` сопоставляется с `ComponentId` установленных компонентов. +The method loads a `ConfigItem` from the JSON. It compares the first `target` element with the `ComponentId` values of the installed components. -### BAT, CMD и TXT +### BAT, CMD and TXT -Текст проверяется на ссылку на executable каждого установленного компонента. Поддерживается имя с `.exe` и без него, в том числе форма `%~dp0`. +The method looks for a reference to the executable of each installed component. The name can have `.exe` or not. The form `%~dp0` is also valid. -Сопоставление выполняется по границам имени. Например, `winws` не должен ошибочно совпасть с произвольной частью более длинного идентификатора. +The match uses name boundaries. For example, `winws` does not match a part of a longer identifier. -### Дополнительные сигнатуры TXT +### More TXT signatures -Если явного executable в TXT нет, применяются сигнатуры: +If a TXT file has no explicit executable, the method uses these signatures: -- `# BuiltinVersion: ...` или параметр `--lua-desync` означают `winws2`; -- параметр `--dpi-desync` означает `winws`. +- `# BuiltinVersion: ...` or the option `--lua-desync` means `winws2`; +- the option `--dpi-desync` means `winws`. -### Приоритет target в мастере +### Target priority in the wizard -Мастер выбирает первый доступный вариант в следующем порядке: +The wizard selects the target for the analysis in this sequence: -1. первый target, найденный анализатором; -2. component ID, переданный маршрутом `cdpiui://Tools/ImportConfig/{componentId}`; -3. первый установленный компонент из списка; -4. пустой target, если подходящих компонентов нет. +1. the first target that the analyzer found; +2. the component ID from the route `cdpiui://Tools/ImportConfig/{componentId}`; +3. the first installed component in the list; +4. an empty target, if no component is applicable. -Переданный маршрутом компонент имеет приоритет для первоначального выбора в UI, даже если анализатор определил другой target. Факт успешного автоматического определения хранится отдельно и используется для предупреждения о несовместимом выборе. +For the initial selection in the UI, the component from the route has priority, also when the analyzer found a different target. The wizard records the automatic detection separately. It uses this record for the warning about an incompatible selection. -## Контракт результата анализа +## Analysis result -`ConfigImportResult` содержит: +`ConfigImportResult` contains: -- `Config` — сформированный `ConfigItem` либо `null`, если анализ завершился ошибкой; -- `Target` — target, для которого выполнялся анализ; -- `SourcePath` — основной импортируемый файл; -- `Issues` — предупреждения и ошибки; -- `SourceFiles` — основной и подключенные файлы конфигурации/скрипты; -- `ReferencedFiles` — существующие ресурсы, которые следует перенести; -- `MissingReferencedFiles` — отсутствующие ресурсы; -- `MissingFileResolutions` — явные решения пользователя; -- `GeneratedFiles` — файлы, которые исходный BAT должен был бы создать до запуска. +- `SharedPackage`: the extracted `.cdpiconfig` package, or `null`; +- `Config`: the `ConfigItem`, or `null` if the analysis failed; +- `Target`: the target of the analysis; +- `SourcePath`: the main imported file; +- `Issues`: the warnings and the errors; +- `SourceFiles`: the main file and the included config files and scripts; +- `ReferencedFiles`: the resources that exist and that the installer copies; +- `MissingReferencedFiles`: the missing resources; +- `MissingFileResolutions`: the explicit decisions of the user; +- `GeneratedFiles`: the files that the source BAT makes before the launch. -Результат считается успешным только при выполнении двух условий: +The result is successful only if these two conditions are true: ```text Config != null -и -Issues не содержит элементов Severity == Error +and +Issues contains no item with Severity == Error ``` -Предупреждения не блокируют тестирование и сохранение. Ошибки блокируют обе операции. +Warnings do not block the test and the save. Errors block the two operations. -## Общий импорт компонентов +## General component import -Общий анализатор используется для любого executable, кроме нормализованных имен `winws` и `winws2`. +The general analyzer applies to all executables except the normalized names `winws` and `winws2`. -Нормализация executable удаляет каталог, кавычки и окончание `.exe`. Сравнение регистронезависимое. +The normalization of the executable name removes the folder, the quotes and the end `.exe`. The comparison ignores the case. -### Импорт BAT/CMD +### BAT and CMD import -1. Файл читается как UTF-8. -2. Физические строки, оканчивающиеся нечетным числом `^`, объединяются в одну логическую строку. -3. Обрабатываются присваивания `set NAME=VALUE` и `set "NAME=VALUE"`. -4. Закомментированные `set` после `rem` или `::` рассматриваются как альтернативные значения переменной. -5. Комментарии и пустые строки пропускаются. -6. Каждая строка токенизируется с учетом двойных кавычек. -7. Ищется токен, который после статического раскрытия соответствует executable выбранного компонента. -8. Хвост после executable становится строкой аргументов. Перенаправления, pipe и `&` вне кавычек отбрасывают остаток строки. -9. Для каждого запуска сохраняется snapshot переменных на момент запуска. -10. Допускается ровно один подходящий запуск. Ноль или несколько запусков считаются неоднозначностью и завершают импорт ошибкой. +1. The analyzer reads the file with the encoding detection of the Zapret analyzer (`BatchFileEncoding`): UTF-8, OEM 866 or ANSI 1251. +2. It joins physical lines that end with an odd number of `^` into one logical line. +3. It processes the assignments `set NAME=VALUE` and `set "NAME=VALUE"`, with an optional `@` before `set`. +4. It uses a `set` after `rem ` or `::` as an alternative value of the variable. +5. It skips comments and empty lines. A comment is a line that starts with `::` or `#`, or a line that starts with `rem` or `@rem` followed by a space, a tab or the line end. +6. It splits each line into tokens. A double quote starts or ends a quoted part. +7. It looks for a token that is the executable of the selected component after the static expansion. +8. The text after the executable becomes the argument line. A redirection, a pipe or `&` outside quotes cuts the rest of the line. +9. For each launch, the analyzer keeps a copy of the variables at the time of the launch. +10. Exactly one applicable launch must exist. Zero or more launches are ambiguous, and the import fails. -Если аргументы запуска состоят ровно из одного токена, указывающего на существующий `.txt`, этот файл импортируется как содержимое Config. Допускается префикс `@` или `$`. Путь раскрывается относительно BAT/CMD-файла. +If the launch arguments are one token that points to an existing `.txt` file, the analyzer imports this file as the config contents. The token can have the prefix `@` or `$`. The analyzer resolves the path relative to the BAT or CMD file. -После выделения аргументов анализатор вычисляет транзитивно используемые переменные: если startup string использует `%A%`, а значение `A` использует `%B%`, в результат попадут обе переменные. +After it finds the arguments, the analyzer collects the used variables transitively. For example, if the startup string uses `%A%` and the value of `A` uses `%B%`, the result contains the two variables. -### Импорт общего TXT +### General TXT import -1. Файл читается тем же механизмом логических строк. -2. Пустые строки и комментарии пропускаются. -3. Если строка содержит запуск выбранного executable, executable удаляется, а в результат попадает только хвост аргументов. -4. Остальные строки объединяются через пробел. -5. Имя берется из комментария `# Config:`, `# Preset:` или `# ActivePreset:`. При отсутствии метаданных используется имя файла без расширения. +1. The analyzer reads the file with the same logical line reader. +2. It skips empty lines and comments. +3. If a line contains a launch of the selected executable, the analyzer removes the executable and keeps only the arguments. +4. It joins the other lines with a space. +5. It takes the name from a comment `# Config:`, `# Preset:` or `# ActivePreset:` (`=` is also valid). Without such a comment, it uses the file name without the extension. -Пустой итоговый набор аргументов считается ошибкой. +An empty argument set is an error. -### Импорт JSON +### JSON import -Сначала выполняется попытка загрузить текущий `ConfigItem`. Если JSON не содержит непустой `startup_string`, выполняется попытка загрузить старый `OldConfigItem`: +The analyzer first tries to load a current `ConfigItem`. If the JSON has no `startup_string`, the analyzer tries to load an old `OldConfigItem`: -- `custom_parameters` переносится в `startup_string`; -- создается `meta = "pUC:v1.0"`; -- имя берется из имени файла; -- `jparams` и `variables` создаются пустыми. +- `custom_parameters` goes to `startup_string`; +- `meta` becomes `pUC:v1.0`; +- the name comes from the file name; +- `jparams` and `variables` are empty. -После загрузки `target` всегда заменяется на выбранный component ID и его версию. +After the load, the analyzer always sets `target` to the selected component ID and its version. -### Переменные общего импортера +### Variables of the general importer -Все распознанные используемые BAT-переменные сохраняются как `commaVars`. Основное значение проходит преобразование: +All used BAT variables go to `commaVars`. The main value gets these changes: ```text %~dp0 -> $GETCURRENTDIR()/ \ -> / ``` -Если у переменной есть закомментированные альтернативы, создается элемент `availableCommaVarsValues`: +If a variable has commented alternatives, the analyzer makes an `availableCommaVarsValues` item: ```json { @@ -251,104 +261,107 @@ Issues не содержит элементов Severity == Error } ``` -Неизвестная `%VARIABLE%`, которая осталась в startup string или значениях, добавляется в `commaVars` со значением `$EMPTY`. Исключения: встроенные переменные `CD`, `ERRORLEVEL`, `DATE` и `TIME`. +An unknown `%VARIABLE%` in the startup string or in the values goes to `commaVars` with the value `$EMPTY`. The built-in variables `CD`, `ERRORLEVEL`, `DATE` and `TIME` are exceptions. + +The general analyzer does not make conditional `jparams`. The `jparams` and `variables` of such a config are empty. + +## Zapret import -Общий анализатор не строит условные `jparams`: `jparams` и обычный список `variables` у такого Config пусты. +`ZapretConfigImportService` does the static import for `winws` and `winws2`. In the code, the generations are `Winws1` and `Winws2`. -## Специализированный импорт Zapret +The Zapret import does not convert winws1 syntax to winws2. It makes a config of the generation that it finds in the source BAT, CMD or TXT. Only `ConfigImportService.Retarget` converts `winws1 -> winws2`, at a later step. -`ZapretConfigImportService` предназначен для статического импорта `winws` и `winws2`. Внутри него поколения называются `Winws1` и `Winws2`. +### Encoding of Zapret files -Специализированный импорт намеренно не преобразует синтаксис winws1 в winws2. Он создает Config того же поколения, которое обнаружено в исходном BAT/CMD/TXT. Конвертация `winws1 -> winws2` выполняется позже, только методом `ConfigImportService.Retarget`. +The analyzer reads BAT, CMD and TXT files through `CDPIUI.Core.System.BatchFileEncoding`. It finds the encoding in this sequence: -### Кодировка Zapret-файлов +1. UTF-8 with BOM; +2. UTF-16 LE with BOM; +3. UTF-16 BE with BOM; +4. strict UTF-8 without BOM; +5. a Cyrillic code page if the bytes are not valid UTF-8. If more bytes are in the range `0x80`–`0xAF` than in the range `0xC0`–`0xFF`, the code page is OEM 866 (the `cmd` code page). Otherwise, it is ANSI 1251. -Для BAT/CMD/TXT используется следующий порядок определения кодировки: +Thus the analyzer can import Russian BAT sets without a manual conversion. -1. UTF-8 с BOM; -2. UTF-16 LE с BOM; -3. UTF-16 BE с BOM; -4. строгая проверка UTF-8 без BOM; -5. Windows-1251, если байты не являются корректным UTF-8. +### Zapret BAT and CMD analysis -Это позволяет импортировать распространенные русскоязычные BAT-наборы без предварительной перекодировки. +The analyzer does these steps: -### Анализ BAT/CMD Zapret +1. It reads the logical lines. A line that ends with an odd number of `^` continues on the next line. +2. It loads `PredefinedVariables`, if the caller supplies them. +3. It collects the usual and the commented `set` commands in sequence. +4. It follows the known calls of external BAT and CMD files statically. +5. It finds the `winws` or `winws2` launches. The executable name must not continue a longer name: `mywinws.exe` is not a launch. +6. It keeps a copy of the variables for each launch. +7. It requires exactly one launch. +8. It expands a referenced TXT config such as `@config.txt`. +9. It makes sure that the executable and the option generation agree. +10. It keeps only the variables that the launch uses. +11. It makes switches for conditional values and for `ACTIVE_*.bin`. +12. It finds the existing, missing and generated resources. +13. It makes the `ConfigItem`. -Алгоритм верхнего уровня: +A comment is a line that starts with `rem`, `@rem` or `::`. After `rem`, a space, a tab or the line end must follow. A commented alternative value must start with `rem ` or `::`. The analyzer does not read `@rem set ...` as an alternative value. -1. Чтение логических строк с поддержкой продолжения через `^`. -2. Загрузка `PredefinedVariables`, если они переданы вызывающей стороной. -3. Последовательный сбор обычных и закомментированных `set`. -4. Статический обход распознанных вызовов внешних BAT/CMD. -5. Поиск запусков `winws` или `winws2`. -6. Snapshot переменных для каждого запуска. -7. Требование ровно одного запуска. -8. Раскрытие ссылочного TXT-конфига вида `@config.txt`. -9. Проверка соответствия executable и поколения параметров. -10. Выбор только реально используемых переменных. -11. Создание переключателей для условных значений и `ACTIVE_*.bin`. -12. Поиск существующих, отсутствующих и генерируемых ресурсов. -13. Формирование `ConfigItem`. +The analyzer finds a launch by the text `winws` or `winws2`, with an optional `.exe`, before a quote, a space or the line end. After the executable, one of these items must follow: options that start with `--`, a referenced TXT file or a BAT variable. This rule decreases false matches with service lines. -Запуск распознается по `winws`, `winws.exe`, `winws2` или `winws2.exe`. После executable должны присутствовать параметры `--...`, ссылочный TXT-файл или BAT-переменная. Это уменьшает количество ложных совпадений с обслуживающими строками. +The analyzer cuts the command line at the first `>`, `<`, `|` or `&` outside quotes. Outside quotes, it also removes caret escapes as `cmd` does: `^&` becomes `&`, and `^^` becomes `^`. -Часть командной строки после неэкранированных `>`, `<`, `|` или `&` вне кавычек не включается в startup string. +A backslash is not an escape character in the analyzer. Thus a quoted folder that ends with a backslash, for example `"C:\dir\"`, closes the quote correctly. -### Поддерживаемые BAT-переменные +### Supported BAT variables -Статическое раскрытие понимает: +The static expansion knows these forms: -- `%~dp0` — каталог текущего BAT-файла; -- `%~f0` — полный путь текущего BAT-файла; -- `%~n0` — имя BAT-файла без расширения; -- `%0` — полный путь BAT-файла; -- `%NAME%` — известная BAT-переменная или переменная окружения процесса CDPIUI; -- `!NAME!` — delayed expansion, нормализуемый в `%NAME%` в результирующем Config; -- `$GETCURRENTDIR()/` и `$GETCURRENTDIR()\` при повторной обработке уже нормализованных значений. +- `%~0`: the path of the current BAT file with the modifiers `f` (full path), `d` (drive), `p` (folder), `n` (name) and `x` (extension), combined as `cmd` does. For example, `%~dp0` is the folder of the BAT file, and `%~n0` is its name without the extension; +- `%0`: the full path of the BAT file; +- `%%`: a literal percent sign. The expansion does not read it as the start of a variable; +- `%NAME%`: a known BAT variable or an environment variable of the CDPI UI process; +- `!NAME!`: delayed expansion. The config gets `%NAME%` instead; +- `$GETCURRENTDIR()/` and `$GETCURRENTDIR()\` in values that are already normalized. -Рекурсивное раскрытие ограничено глубиной 16. Неизвестная переменная сохраняется в строке и сопровождается предупреждением. +The recursive expansion has a maximum depth of 16. An unknown variable stays in the line, and the analyzer adds a warning. -### Внешние BAT/CMD и `service.bat` +### External BAT and CMD files and `service.bat` -Анализатор распознает вызовы вида: +The analyzer knows calls such as: ```bat call "path\script.bat" argument call path\script.cmd argument ``` -Вызовы локальных меток `call :label` не считаются внешними файлами. Для существующего BAT/CMD: +A call of a local label, `call :label`, is not an external file. For an existing BAT or CMD file, the analyzer does these steps: -1. путь раскрывается относительно вызывающего скрипта; -2. при отсутствии расширения добавляется `.bat`; -3. файл добавляется в `SourceFiles`; -4. его переменные собираются статически; -5. результат объединяется с переменными вызывающего файла. +1. It resolves the path relative to the calling script. +2. If the path has no extension, it adds `.bat`. +3. It adds the file to `SourceFiles`. +4. It collects the variables of the file statically. +5. It merges the result with the variables of the calling file. -Если первый аргумент вызова сопоставляется с конструкцией вида `if "%~1"=="argument" (...)`, анализируются метки `call :label` внутри соответствующего блока. Если диспетчеризация не распознана, анализируются верхнеуровневые строки до первой BAT-метки. +If the first argument of the call matches a construct such as `if "%~1"=="argument" (...)`, the analyzer reads the `call :label` labels in this block. If it does not find such a dispatch, it reads the top-level lines before the first BAT label. -Рекурсивные вызовы ограничены `MaxIncludeDepth`, по умолчанию 8. Повтор одного и того же файла с тем же набором аргументов не анализируется второй раз. +`MaxIncludeDepth` limits the recursive calls. The default value is 8. The analyzer does not read the same file with the same arguments a second time. -Специально поддержан вызов команды `load_user_lists`. В одноименной метке внешнего скрипта анализируются операции: +The command `load_user_lists` has special support. In the label with this name in the external script, the analyzer reads these operations: ```bat echo content > path\file.txt echo content >> path\file.txt ``` -Если целевой файл отсутствует, его содержимое собирается в памяти и возвращается в `GeneratedFiles`. Исходный каталог не изменяется. Файл вне каталога источника игнорируется с предупреждением. +If the target file does not exist, the analyzer collects its contents in memory and returns them in `GeneratedFiles`. The source folder does not change. The analyzer ignores a file outside the source folder and adds a warning. -Это специализированная статическая модель, а не общий интерпретатор BAT. Другие способы генерации файлов автоматически не воспроизводятся. +This is a special static model, not a general BAT interpreter. The analyzer does not repeat other ways to make files. -### TXT-конфиги и response files +### TXT configs and response files -TXT-конфиг состоит из: +A TXT config contains: -- комментариев, начинающихся с `#`; -- непустых строк параметров, объединяемых через пробел. +- comments that start with `#`; +- lines with options. The analyzer joins them with a space. -Поддерживаются метаданные: +The analyzer knows this metadata: ```text # BuiltinVersion: 2.11 @@ -356,61 +369,63 @@ TXT-конфиг состоит из: # ActivePreset: Config name ``` -`BuiltinVersion` одновременно указывает на поколение winws2 и задает версию target. Если метаданные имени отсутствуют, используется имя файла. +The version line can also have the forms `Built-in version` or `Builtin Version`, with `:` or `=`. `BuiltinVersion` means the winws2 generation and also sets the target version. If the file has no name metadata, the analyzer uses the file name. -Ссылка `@file.txt` обрабатывается как response file только если: +The analyzer reads a reference `@file.txt` as a response file only if: -1. файл существует; -2. расширение равно `.txt`; -3. в первых 64 строках найдена `BuiltinVersion` либо строка, начинающаяся с `--`. +1. the file exists; +2. the extension is `.txt`; +3. one of the first 64 lines contains `BuiltinVersion` or starts with `--`. -Содержимое response file заменяет всю исходную строку параметров. Если рядом с `@file.txt` были другие аргументы, они игнорируются, а в результат добавляется предупреждение `CONFIG_INCLUDE_IGNORES_OTHER_ARGUMENTS`. +If the response file does not exist, the analyzer adds the error `INCLUDED_CONFIG_NOT_FOUND`. -Вложенность ограничена `MaxIncludeDepth`, по умолчанию 8. Активный стек файлов контролируется для обнаружения циклов. +The contents of the response file replace the complete option line. If the line had other arguments near `@file.txt`, the analyzer ignores them and adds the warning `CONFIG_INCLUDE_IGNORES_OTHER_ARGUMENTS`. -### Определение поколения Zapret +`MaxIncludeDepth` (default 8) limits the depth of includes. The analyzer keeps a stack of the active files to find cycles. -Поколение определяется в следующем порядке: +### Zapret generation -1. Наличие `BuiltinVersion` означает winws2. -2. Наличие winws2-специфичных параметров, включая `--lua-init`, `--lua-desync`, `--blob`, `--out-range`, `--ctrack-*` и расширенные варианты `--wf-*`, означает winws2. -3. Наличие `--dpi-desync` или старых `--wf-tcp`/`--wf-udp` означает winws1. -4. Если TXT не содержит однозначных признаков, используется `DefaultTextConfigGeneration`. +The analyzer finds the generation in this sequence: -При вызове через общий фасад значение по умолчанию равно поколению выбранного target. При прямом использовании `ZapretConfigImportService` значение по умолчанию — winws2. Можно передать `null`, чтобы запретить неявное определение. +1. `BuiltinVersion` means winws2. +2. winws2 options mean winws2. These options are `--lua-init`, `--lua-desync`, `--blob`, `--out-range`, `--ctrack-*` and `--wf-tcp-out`, `--wf-tcp-in`, `--wf-udp-out`, `--wf-udp-in`. +3. `--dpi-desync` or the old `--wf-tcp` and `--wf-udp` mean winws1. +4. If a TXT file has no clear signs, the analyzer uses `DefaultTextConfigGeneration`. -Если BAT запускает одно поколение, а параметры или подключенный TXT однозначно относятся к другому, импорт завершается ошибкой `GENERATION_MISMATCH`. +The facade sets the default to the generation of the selected target. For a direct call of `ZapretConfigImportService`, the default is winws2. A `null` value disables the default. -### Определение версии +If the BAT file starts one generation and the options or the included TXT clearly belong to the other generation, the import fails with `GENERATION_MISMATCH`. -Для winws2 версия выбирается так: +### Version -1. значение `# BuiltinVersion`, если оно присутствует; -2. `CurrentVersionResolver` из `ZapretConfigImportOptions`; -3. `CurrentVersion` установленного Zapret 2 из Store DB; +For winws2, the analyzer selects the version in this sequence: + +1. the value of `# BuiltinVersion`, if it exists; +2. `CurrentVersionResolver` from `ZapretConfigImportOptions`; +3. `CurrentVersion` of the installed Zapret 2 in the Store DB; 4. `%CURRENT%`. -Для winws1 шаг с `BuiltinVersion` не применяется. Ошибки обращения к резолверу или БД не блокируют импорт. +For winws1, the analyzer does not use `BuiltinVersion`. Errors of the resolver or of the DB do not block the import. -### Преобразование переменных Zapret в ConfigItem +### Zapret variables in the ConfigItem -#### Обычные `variables` +#### Usual `variables` -Используемая переменная без альтернатив сохраняется как строка: +A used variable without alternatives becomes a line in `variables`: ```text %LISTS%=$GETCURRENTDIR()/lists/list-general.txt ``` -#### `commaVars` и `availableCommaVarsValues` +#### `commaVars` and `availableCommaVarsValues` -Переменная с альтернативами сохраняется в `commaVars`. Закомментированные `set` и значения, наблюдавшиеся при статическом обходе, образуют список `availableCommaVarsValues`. +A variable with alternatives goes to `commaVars`. The commented `set` values and the values from the static walk become the list in `availableCommaVarsValues`. -В startup string остается ссылка `%NAME%`, поэтому значение можно менять через соответствующий UI-виджет без ручного редактирования всей строки. +The startup string keeps the reference `%NAME%`. Thus the user can change the value in the UI widget and does not have to edit the full line. -#### Неизвестные переменные +#### Unknown variables -Любая нераспознанная `%NAME%`, обнаруженная в startup string, основном значении или альтернативах, добавляется как: +The analyzer adds each unknown `%NAME%` from the startup string, the main values or the alternatives as: ```json "commaVars": { @@ -418,66 +433,66 @@ TXT-конфиг состоит из: } ``` -Переменные `%~...%` исключаются из этого правила. Одновременно анализатор сообщает `UNRESOLVED_BATCH_VARIABLE` для неизвестной переменной, реально требуемой startup string. +The `%~...%` forms are not included. For an unknown variable that the startup string or a used variable requires, the analyzer also adds the warning `UNRESOLVED_BATCH_VARIABLE`. -#### Условные переменные и `jparams` +#### Conditional variables and `jparams` -Переменная становится кандидатом на логический переключатель, если она была присвоена внутри статически анализируемого вызванного скрипта. Если для нее наблюдаются ровно два различных непустых значения, создаются: +A variable can become a boolean switch if a statically read called script assigns it. If the analyzer sees exactly two different non-empty values for the variable, it makes: -- boolean-параметр `use` в `jparams`; -- условная переменная в `variables`. +- a boolean parameter `use` in `jparams`; +- a conditional variable in `variables`. -Формат результата: +The result has this format: ```text %GameFilter%=$LOCALCONDITION(useGameFilter==true ? 1024-65535 $SEPARATOR 12) ``` -Текущее значение переменной определяет начальное состояние boolean-параметра. Имя параметра очищается от символов, кроме букв, цифр и `_`; при конфликте добавляется числовой суффикс. +The current value of the variable sets the initial state of the boolean parameter. The analyzer removes all characters except letters, digits and `_` from the parameter name. A name collision gets a number suffix. -Если наблюдается не ровно два значения, обычный boolean-переключатель не создается. +If the analyzer sees a different number of values, it does not make a boolean switch. -#### Переключатели `ACTIVE_*.bin` +#### `ACTIVE_*.bin` switches -Ссылка вида `%ROOT%ACTIVE_name.bin` может быть преобразована в отдельную выбираемую переменную. +The analyzer can convert a reference such as `%ROOT%ACTIVE_name.bin` into a separate variable that the user can select. -Алгоритм: +The analyzer does these steps: -1. раскрывается каталог из переменной `ROOT`; -2. в нем перечисляются все `*.bin`, имя которых не начинается с `ACTIVE_`; -3. SHA-256 активного BIN сравнивается с кандидатами; -4. совпадающий по содержимому файл становится текущим значением; -5. все найденные BIN становятся вариантами `availableCommaVarsValues`; -6. исходная ссылка заменяется на `%%`. +1. It expands the folder from the variable `ROOT`. +2. It finds all `*.bin` files in the folder whose names do not start with `ACTIVE_`. It sorts them by name. +3. It compares the SHA-256 of the active BIN file with each candidate. +4. The last candidate with the same contents becomes the current value. +5. All found BIN files become values in `availableCommaVarsValues`. +6. The reference becomes `%%`. -Если активный файл отсутствует, каталог не существует или вариантов нет, исходная ссылка остается без изменения. +The reference stays unchanged if the `ROOT` variable is unknown, if the folder does not exist or if the folder has no candidates. If the active file is missing or no candidate has the same contents, the analyzer still makes the switch. The current value is then the original `ACTIVE_` file path. -### Формирование Zapret ConfigItem +### Zapret ConfigItem -Результат имеет следующие базовые поля: +The result has these basic fields: ```json { "meta": "pUC:v1.0", - "name": "имя из метаданных или файла", + "name": "name from the metadata or the file", "target": ["component-id", "version-or-%CURRENT%"], "jparams": {}, "variables": [], "commaVars": {}, "availableCommaVarsValues": [], - "startup_string": "нормализованные аргументы" + "startup_string": "normalized arguments" } ``` -Последовательности пробельных символов вне двойных кавычек сворачиваются до одного пробела. `%~dp0` заменяется на `$GETCURRENTDIR()/`. +The analyzer changes each sequence of whitespace outside double quotes to one space. It replaces `%~dp0` with `$GETCURRENTDIR()/`. It does not change backslashes. -Специализированный анализатор сначала использует внутренние ID Zapret/Zapret 2 для поколения, затем `ConfigImportService` заменяет их на фактический ID выбранного Store-компонента. Поэтому другие установленные компоненты с executable `winws` или `winws2` также могут использовать этот парсер. +The Zapret analyzer first uses the internal Zapret or Zapret 2 ID of the generation. `ConfigImportService` then replaces it with the ID of the selected Store component. Thus other installed components with the executable `winws` or `winws2` can also use this parser. -## Обнаружение внешних ресурсов +## External resources -### Общий анализатор +### General analyzer -Ищет ссылки с расширениями: +The general analyzer looks for references with these extensions: - `.bin`; - `.txt`; @@ -487,147 +502,149 @@ TXT-конфиг состоит из: - `.cfg`; - `.json`. -### Zapret-анализатор +### Zapret analyzer -Ищет `.bin`, `.txt` и `.lua`. +The Zapret analyzer looks for `.bin`, `.txt` and `.lua`. -Пути могут находиться в кавычках или быть некавычными токенами. Префиксы `@` и `$` удаляются перед проверкой файла. Относительный путь разрешается относительно основного файла, из значения которого собирается ссылка. +A path can be in quotes or not. The analyzer removes the prefixes `@` and `$` before it checks the file. It resolves a relative path relative to the file that contains the reference. -Существующий файл добавляется в `ReferencedFiles`. Отсутствующий — в `MissingReferencedFiles` и сопровождается предупреждением `REFERENCED_FILE_NOT_FOUND`. +The analyzer adds a file that exists to `ReferencedFiles`. It adds a missing file to `MissingReferencedFiles` and adds the warning `REFERENCED_FILE_NOT_FOUND`. A missing file that the BAT script generates does not go to `MissingReferencedFiles`. -### Стандартные LUA-библиотеки компонента +### Standard LUA libraries of the component -Для Zapret отсутствующая `.lua` может быть исключена из списка отсутствующих файлов, если: +For Zapret, the facade can remove a missing `.lua` file from the missing file list if: -1. путь находится внутри относительной структуры импортируемого каталога; -2. файл с тем же относительным путем существует в каталоге выбранного установленного компонента; -3. Config явно не требует импортированную копию через `$GETCURRENTDIR()/relative/path.lua`. +1. the path is inside the folder structure of the imported file; +2. a file with the same relative path exists in the folder of the selected installed component; +3. the config does not require the imported copy through `$GETCURRENTDIR()/relative/path.lua`. -В таком случае предполагается, что LUA-файл поставляется самим компонентом, а соответствующее предупреждение удаляется. +In this case, the facade assumes that the component supplies the LUA file. It removes the related warning. -## Обработка отсутствующих файлов +## Missing files -Для каждого пути из `MissingReferencedFiles` мастер требует явное решение до сохранения. +The wizard requires an explicit decision for each path in `MissingReferencedFiles` before the save. -Возможны два типа решения: +Two types of decision are possible: -1. существующий файл-замена; -2. пустая заглушка — `ReplacementPath == null`. +1. a replacement file that exists; +2. an empty file: `ReplacementPath == null`. -`ApplyMissingFileResolutions` принимает только решения для файлов, которые действительно присутствуют в исходном списке отсутствующих. Пути нормализуются через `Path.GetFullPath`. Если выбранный файл-замена отсутствует, добавляется блокирующая ошибка `MISSING_FILE_REPLACEMENT_NOT_FOUND`. +`ApplyMissingFileResolutions` accepts only decisions for files that are in the original missing file list. It normalizes the paths with `Path.GetFullPath`. If the selected replacement file does not exist, it adds the blocking error `MISSING_FILE_REPLACEMENT_NOT_FOUND`. -### Автокорректор +### Auto-corrector -`ConfigImportAutoCorrector` ищет замену в два этапа: +`ConfigImportAutoCorrector` looks for a replacement in two steps: -1. рекурсивный поиск файла с тем же именем внутри каталога импортируемого Config; -2. вызов существующего `Autocorrector.FindAutoCorrectPath` с временно установленным `packId`, указывающим на исходный каталог. +1. It looks for a file with the same name in the folder of the imported config. The search goes down a maximum of 3 subfolder levels. It skips folders that it cannot open and reparse points. +2. It calls the existing `Autocorrector.FindAutoCorrectPath` with a temporary `packId` that points to the source folder. -Автокорректор ничего не копирует и не изменяет. Он только возвращает существующий путь. Исключения поиска подавляются, чтобы ошибка подсказки не блокировала мастер. +The auto-corrector does not copy or change files. It only returns a path that exists. It ignores search exceptions, so that a failed suggestion does not block the wizard. -Для файлов, имя которых содержит `user` или `debug` без учета регистра, автоматически предлагается пустой файл вместо поиска замены. +For a file whose name contains `user` or `debug` (any case), the wizard suggests an empty file and does not search for a replacement. -На странице исправления пользователь может: +On the correction page, the user can: -- принять найденную замену; -- выбрать другой файл; -- выбрать пустой файл; -- принять все доступные автоматические предложения кнопкой продолжения. +- accept the found replacement; +- select a different file; +- select an empty file; +- accept all available suggestions with the continue button. -## Ретаргетинг Config +## Retarget -Выбор компонента в карточке результата не изменяет исходный объект немедленно. Перед тестированием и сохранением мастер: +A component selection on the result card does not change the original object immediately. Before the test and the save, the wizard does these steps: -1. применяет решения отсутствующих файлов; -2. вызывает `ConfigImportService.Retarget`; -3. проверяет, что после ретаргетинга Config остается успешным. +1. It applies the decisions for missing files. +2. It calls `ConfigImportService.Retarget`. +3. It makes sure that the config is still successful after the retarget. -### Одинаковый executable +### Same executable -Если исходный и новый target используют одинаковый executable, сохраняется уже вычисленная версия из `config.target`, если она непустая. Component ID заменяется на выбранный. +If the old and the new target use the same executable, the retarget keeps the version from `config.target`, if it is not empty. It replaces the component ID with the selected ID. ### `winws1 -> winws2` -Это единственное специализированное межпоколенческое преобразование в `Retarget`. +This is the only conversion between generations in `Retarget`. -1. В clone исходного Config временно записывается `packId` исходного каталога. -2. `ConfigurationService.GetStartupParametersByConfigItem` раскрывает `variables`, `commaVars`, `jparams` и служебные функции в готовую строку запуска winws1. -3. Готовая строка передается существующему `Zapret1ToZapret2Converter`. -4. Диагностики конвертера добавляются в общий список `Issues`. -5. При успехе converted startup string записывается в `startup_string`. -6. `jparams`, `variables`, `commaVars` и `availableCommaVarsValues` очищаются, поскольку строка уже материализована. -7. Target меняется на выбранный winws2-компонент и его версию. -8. Временный `packId` сбрасывается. +1. The retarget writes the source folder as `packId` into a clone of the config. +2. `ConfigurationService.GetStartupParametersByConfigItem` expands `variables`, `commaVars`, `jparams` and the service functions into a ready winws1 command line. +3. The existing `Zapret1ToZapret2Converter` gets the ready line. +4. The converter issues go to `Issues`. +5. If the conversion is successful, the converted line goes to `startup_string`. +6. The retarget clears `jparams`, `variables`, `commaVars` and `availableCommaVarsValues`, because the line is already expanded. +7. The target becomes the selected winws2 component and its version. +8. The retarget clears the temporary `packId`. -Если конвертер сообщает ошибку, `Config` становится `null`, и тестирование/сохранение блокируется. +If the converter returns an error, `Config` becomes `null`, and the test and the save are blocked. -Встроенный `Zapret1ToZapret2Converter` не является частью этого AddOn; AddOn только вызывает его и передает диагностики без изменения. +`Zapret1ToZapret2Converter` is not a part of this add-on. The add-on only calls it and passes its issues without changes. -### Прочие сочетания executable +### Other executable combinations -Для прочих сочетаний специального преобразования нет: target и версия заменяются, а startup string сохраняется. UI показывает предупреждение, если автоматически определенный executable отличается от выбранного. Это предупреждение информативное и само по себе не является блокирующей ошибкой. +For other combinations, no special conversion occurs. The retarget replaces the target and the version and keeps the startup string. The UI shows a warning if the detected executable is different from the selected one. This warning is for information and does not block the operation. -В частности, обратная конвертация `winws2 -> winws1` не реализована. +The add-on cannot convert `winws2 -> winws1`. -## Тестирование Config +## Config test -Тестовый запуск выполняется из `MainPage`: +`MainPage` does the test run in these steps: -1. предыдущий активный тест останавливается; -2. применяются решения отсутствующих файлов; -3. выполняется ретаргетинг; -4. `packId` временно устанавливается в каталог исходного файла; -5. `ConfigurationService.GetStartupParametersByConfigItem` формирует готовые аргументы; -6. `ConfigImportTestPlaceholder.Create` подготавливает временные зависимости; -7. выбранный компонент останавливается, если он уже запущен; -8. компонент запускается через `ComponentTasksManager.CreateAndRunNewTask(componentId, arguments)`; -9. мастер подписывается на изменение состояния задачи; -10. при завершении процесса временные файлы удаляются. +1. It stops the earlier active test. +2. It applies the decisions for missing files. +3. It runs the retarget. +4. It sets `packId` to the folder of the source file temporarily. +5. `ConfigurationService.GetStartupParametersByConfigItem` makes the ready arguments. +6. `ConfigImportTestPlaceholder.Create` prepares the temporary dependencies. +7. It stops the selected component if it operates. +8. It starts the component through `ComponentTasksManager.CreateAndRunNewTask(componentId, arguments)`. +9. It subscribes to the state changes of the task. +10. When the process stops, it deletes the temporary files. -Одновременно поддерживается один активный тест. Повторное нажатие кнопки для той же карточки останавливает тест. Закрытие/выгрузка страницы также останавливает процесс и освобождает временные файлы. +Only one test can operate at a time. A second click on the button of the same card stops the test. When the page closes or unloads, it also stops the process and releases the temporary files. -Если процесс завершился сразу после запуска, операция считается ошибкой тестирования. +If the process stops immediately after the start, the test fails. -### Временные зависимости теста +### Temporary test dependencies -Временный каталог создается по шаблону: +The temporary folder has this pattern: ```text %TEMP%\CDPIUI\ConfigImportPlaceholders\\ ``` -Если хотя бы одно отсутствующее значение должно быть пустым, создается общий нулевой файл: +If at least one missing value must be empty, the class makes one common empty file: ```text empty.bin ``` -Если пользователь выбрал существующую замену, используется ее исходный путь. Генерируемые файлы записываются как UTF-8 без BOM в подкаталог `generated` с именами вида `0000_`. +If the user selected an existing replacement, the test uses its original path. The class writes generated files as UTF-8 without BOM into the subfolder `generated`, with names such as `0000_`. + +The class replaces these forms in the ready arguments: -В готовых аргументах заменяются: +- the expected absolute path; +- the form `$GETCURRENTDIR()/relative/path`. -- абсолютный ожидаемый путь; -- форма `$GETCURRENTDIR()/relative/path`. +Mixed `/` and `\` are valid in these forms. The class does not process the forms `%~dp0relative`, `./relative` and a plain `relative/path` if `ConfigurationService` did not expand them earlier. -Смешанные `/` и `\` в этих формах поддерживаются. На текущем этапе временный переписчик не обрабатывает отдельно оставшиеся формы `%~dp0relative`, `./relative` и простой `relative/path`, если они не были раскрыты ранее `ConfigurationService`. +`Dispose` deletes only a folder in the permitted root `ConfigImportPlaceholders`. The cleanup is best effort. -`Dispose` удаляет только каталог внутри разрешенного корня `ConfigImportPlaceholders`. Очистка выполняется best effort. +## Config and resource save -## Сохранение Config и ресурсов +`ConfigImportInstaller.InstallAsync` accepts only a successful result, a name that is not empty and an explicit decision for each missing file. -`ConfigImportInstaller.InstallAsync` принимает только успешный результат, непустое имя и явное решение для каждого отсутствующего файла. +For a `.cdpiconfig` result (`SharedPackage` is set), the installer calls `ConfigShareService.InstallAsync` with the edited config. The rest of this section applies to the other formats. -### Корень сохранения +### Save root -Config сохраняется в служебный локальный набор: +The installer saves the config in the local service kit: ```text Store\Items\LocalUserStorage\ ``` -Точный абсолютный путь получает `ConfigurationService.GetItemFolderFromPackId(SharedConstants.LocalUserItemsId)`. +`ConfigurationService.GetItemFolderFromPackId(SharedConstants.LocalUserItemsId)` gives the exact absolute path. -Ресурсы сохраняются по типу: +The installer saves the resources by type: ```text LocalUserStorage\List\Converted\\... @@ -635,51 +652,51 @@ LocalUserStorage\Bin\Converted\\... LocalUserStorage\Lua\Converted\\... ``` -Категория определяется расширением: +The extension sets the category: - `.txt` -> `List`; - `.lua` -> `Lua`; -- все остальные найденные ресурсы -> `Bin`. +- all other resources -> `Bin`. -`importId` состоит из UTC-времени `yyyyMMddHHmmss`, символа `_` и уникального фрагмента GUID; итоговая строка обрезается до 24 символов. +`importId` is the UTC time `yyyyMMddHHmmss`, the character `_` and a GUID part. The installer cuts the string to 24 characters. -### Организация относительных путей +### Relative paths -Сохраняется исходная вложенность ресурса. Один стандартный верхний каталог удаляется, чтобы не получить дублирование категорий: +The installer keeps the original subfolders of a resource. It removes one standard top folder, so that the category does not repeat: -- для `List`: `list` или `lists`; -- для `Lua`: `lua`; -- для `Bin`: `bin` или `fake`. +- for `List`: `list` or `lists`; +- for `Lua`: `lua`; +- for `Bin`: `bin` or `fake`. -Пример: +Example: ```text -исходный путь: lists\ipset-exclude.txt -сохраненный путь: List\Converted\\ipset-exclude.txt +original path: lists\ipset-exclude.txt +saved path: List\Converted\\ipset-exclude.txt ``` -Если внешний ресурс находится за пределами каталога исходного Config, для сохранения используется только имя файла. Это не разрешает запись вне целевого каталога: итоговый destination повторно проверяется. +If an external resource is outside the folder of the source config, the installer uses only the file name. This does not permit a write outside the target folder: the installer checks the final destination again. -При коллизии двух сохраненных относительных путей к имени второго файла добавляются первые восемь hex-символов SHA-256 от его ожидаемого абсолютного пути. +If two saved relative paths are the same, the installer adds the first eight hex characters of the SHA-256 of the expected absolute path to the name of the second file. -### Источники сохраняемого ресурса +### Resource sources -Ресурс может быть создан одним из трех способов: +The installer makes a resource in one of three ways: -1. существующий `ReferencedFile` копируется без перезаписи; -2. `GeneratedFile` записывается как UTF-8 без BOM; -3. отсутствующий файл с решением `null` создается нулевой длины, а с выбранной заменой — копируется из выбранного пути. +1. It copies an existing `ReferencedFile` and does not overwrite. +2. It writes a `GeneratedFile` as UTF-8 without BOM. +3. For a missing file with the decision `null`, it makes an empty file. For a missing file with a replacement, it copies the selected file. -### Переписывание путей +### Path changes -Перед сохранением Config глубоко клонируется через JSON. Переписываются: +Before the save, the installer makes a deep clone of the config through JSON. It changes the paths in: - `startup_string`; -- каждый элемент `variables`; -- каждое значение `commaVars`; -- каждое значение в `availableCommaVarsValues`. +- each item of `variables`; +- each value of `commaVars`; +- each value in `availableCommaVarsValues`. -Целевой формат: +The target format is: ```text $GETCURRENTDIR()/List/Converted//file.txt @@ -687,149 +704,153 @@ $GETCURRENTDIR()/Bin/Converted//file.bin $GETCURRENTDIR()/Lua/Converted//file.lua ``` -Переписчик поддерживает: +The installer knows these forms: -- абсолютный исходный путь; +- the absolute original path; - `$GETCURRENTDIR()/source/path`; -- `%~dp0source/path` для отображения каталогов; -- относительный `source/path`; -- `./source/path` и `.\source\path`; -- необязательный префикс `@` для отдельной ссылки на файл; -- смешанные разделители `/` и `\`. +- `%~dp0source/path` for folder mappings; +- the relative `source/path`; +- `./source/path` and `.\source\path`; +- an optional prefix `@` for a single file reference; +- mixed separators `/` and `\`. -Сначала строятся отображения каталогов. Отображение применяется только когда один исходный каталог однозначно соответствует одному сохраненному каталогу. Затем переписываются конкретные файлы. +The installer first makes the folder mappings. A mapping applies only when one source folder maps to exactly one saved folder. Then the installer changes the paths of the single files. -### Транзакционность сохранения +### Transactional save -Для каждой категории сначала создается staging-каталог: +For each category, the installer first makes a staging folder: ```text \Converted\..tmp ``` -Порядок операции: +The installer does these steps: + +1. It makes the staging folders. +2. It writes or copies all resources. +3. It prepares the config clone and changes the paths. +4. It moves the staging folders to ``. +5. It saves the JSON through `ConfigurationService.SaveConfigItem`. +6. If an error occurs, it deletes the folders that it made and the JSON, if it exists. -1. создать staging-каталоги; -2. записать или скопировать все ресурсы; -3. подготовить clone Config и переписать пути; -4. атомарно переместить staging-каталоги в ``; -5. сохранить JSON через `ConfigurationService.SaveConfigItem`; -6. при ошибке удалить созданные каталоги и возможный JSON. +The deletion is limited to the checked `Converted` root of the category. The rollback is best effort. The installer never deletes the original files. -Удаление ограничено проверенным корнем соответствующего `Converted`. Rollback best effort, исходные файлы никогда не удаляются. +### Config file name -### Имя Config-файла +The installer writes the display name to `name` and `not_converted_name`. -Отображаемое имя записывается в `name` и `not_converted_name`. +The JSON name has these rules: -Имя JSON строится так: +1. Unicode letters, digits, `_`, `.` and `-` stay. +2. Each other character, also each whitespace character, becomes `_`. +3. The installer removes `_` and `.` at the start and at the end. +4. An empty result becomes `Imported_Config`. +5. A name collision gets `_2`, `_3` and so on. -1. сохраняются буквы Unicode, цифры, `_`, `.`, `-`; -2. остальные символы заменяются на `_`; -3. пробелы сворачиваются в `_`; -4. крайние `_` и `.` удаляются; -5. пустой результат заменяется на `Imported_Config`; -6. при коллизии добавляется `_2`, `_3` и далее. +The installer saves the config with `packId = LocalUserStorage` through `SaveConfigItem`. The JSON does not have to contain `packId`. -Config сохраняется с `packId = LocalUserStorage` через API `SaveConfigItem`; значение `packId` не обязано присутствовать внутри самого JSON. +After a successful save, the UI calls `ReInitConfigs()` for the selected component. -После успешного сохранения UI вызывает `ReInitConfigs()` для выбранного компонента. +## Import wizard -## Работа мастера импорта +### Step 1. Selection -### Шаг 1. Выбор +The file dialog permits a selection of several `.txt`, `.bat`, `.cmd`, `.json` and `.cdpiconfig` files. The user can also drop files on the component settings page (`ViewComponentSettingsPage`). The page then opens the import window with these files and the component ID. A single dropped `.cdpiconfig` file can go to the ConfigShare dialog instead. The default config name is the file name without the extension. For a `.cdpiconfig` package, the default name is the name from the manifest. -Диалог поддерживает множественный выбор `.txt`, `.bat`, `.cmd` и `.json`. Имя будущего Config по умолчанию равно имени файла без расширения. +### Step 2. Analysis -### Шаг 2. Анализ +A background task processes the files one by one. For each file, the wizard does these steps: -Файлы обрабатываются последовательно в фоновой задаче. Для каждого файла: +1. It finds the applicable target. +2. It runs the import. +3. It makes a separate result card. +4. It shows the success, the warning or the error. -1. определяется подходящий target; -2. выполняется импорт; -3. создается отдельная карточка результата; -4. показываются успех, предупреждение или ошибка. +For a `.cdpiconfig` package with a missing component, the wizard offers the Store installer of the component. -Диагностики доступны через Flyout **Подробнее** в формате `[CODE] [line] message`. +The **More** flyout shows the issues in the format `[CODE] [line] message`. -### Шаг 3. Исправление отсутствующих файлов +### Step 3. Missing files -Шаг появляется только если успешные результаты содержат отсутствующие ресурсы. Продолжение возможно, когда каждый файл решен вручную либо для всех оставшихся файлов существует автоматическое предложение. +The wizard shows this step only if successful results have missing resources. The user can continue when the user resolved each file, or when each remaining file has a suggestion. -### Шаг 4. Результаты +### Step 4. Results -Для каждой карточки доступны: +Each card lets the user: -- изменение имени; -- выбор любого установленного компонента; -- предупреждение о несовпадении executable; -- просмотр диагностик; -- тестирование/остановка теста; -- отдельное сохранение. +- change the name; +- select an installed component; +- see a warning about a different executable; +- see the issues; +- start and stop the test; +- save the card. -Кнопка **Сохранить все** последовательно сохраняет все карточки, для которых `CanSave == true`. Успешные карточки удаляются из списка. Карточки с ошибкой остаются доступными для просмотра. +The **Save all** button saves all cards with `CanSave == true` one by one. The wizard removes the saved cards from the list. Cards with an error stay in the list. -## Диагностика +## Issues -Каждая диагностика имеет: +Each issue has: -- `Severity`: `Warning` или `Error`; -- стабильный строковый `Code`; -- техническое сообщение `Message`; -- необязательный `SourcePath`; -- необязательный `LineNumber`. +- `Severity`: `Warning` or `Error`; +- a stable string `Code`; +- a technical `Message`; +- an optional `SourcePath`; +- an optional `LineNumber`. -### Общие коды +### General codes -| Код | Уровень | Условие | +| Code | Level | Condition | |---|---|---| -| `FILE_NOT_FOUND` | Error | Исходный файл отсутствует | -| `TARGET_REQUIRED` | Error | Не выбран component ID | -| `EXECUTABLE_REQUIRED` | Error | В записи target отсутствует executable | -| `UNSUPPORTED_FORMAT` | Error | Расширение не поддерживается выбранным анализатором | -| `IMPORT_FAILED` | Error | Необработанное исключение во время анализа | -| `EXECUTABLE_LAUNCH_NOT_FOUND` | Error | Общий BAT/CMD не содержит запуска выбранного executable | -| `MULTIPLE_EXECUTABLE_LAUNCHES` | Error | Найдено более одного запуска выбранного executable | -| `TEXT_CONFIG_EMPTY` | Error | Общий TXT не содержит аргументов | -| `JSON_CONFIG_INVALID` | Error | JSON не является поддерживаемым `ConfigItem` или `OldConfigItem` | -| `EXECUTABLE_MISMATCH` | Error | Zapret-анализ определил поколение, не совпадающее с target первоначального анализа | -| `UNRESOLVED_VARIABLE` | Warning | Общая BAT-переменная не разрешена статически | -| `REFERENCED_FILE_NOT_FOUND` | Warning | Внешний ресурс отсутствует | -| `INVALID_RESOURCE_PATH` | Warning | Путь ресурса невозможно нормализовать | -| `MISSING_FILE_REPLACEMENT_NOT_FOUND` | Error | Выбранная пользователем замена отсутствует | - -### Коды Zapret-анализатора - -| Код | Уровень | Условие | +| `FILE_NOT_FOUND` | Error | The source file does not exist | +| `TARGET_REQUIRED` | Error | No component ID is selected | +| `EXECUTABLE_REQUIRED` | Error | The target record has no executable | +| `UNSUPPORTED_FORMAT` | Error | The selected analyzer does not support the extension | +| `IMPORT_FAILED` | Error | An unhandled exception occurred during the analysis | +| `EXECUTABLE_LAUNCH_NOT_FOUND` | Error | The general BAT or CMD file has no launch of the selected executable | +| `MULTIPLE_EXECUTABLE_LAUNCHES` | Error | The file has more than one launch of the selected executable | +| `TEXT_CONFIG_EMPTY` | Error | The general TXT file has no arguments | +| `JSON_CONFIG_INVALID` | Error | The JSON is not a supported `ConfigItem` or `OldConfigItem` | +| `EXECUTABLE_MISMATCH` | Error | The Zapret analysis found a generation that is different from the target | +| `UNRESOLVED_VARIABLE` | Warning | The general analyzer cannot resolve a BAT variable | +| `REFERENCED_FILE_NOT_FOUND` | Warning | An external resource is missing | +| `INVALID_RESOURCE_PATH` | Warning | The analyzer cannot normalize a resource path | +| `MISSING_FILE_REPLACEMENT_NOT_FOUND` | Error | The replacement that the user selected does not exist | + +For a `.cdpiconfig` file, the issue code is the code of the `ConfigShareException`, for example `SHARE_IMPORT_FAILED`. + +### Zapret analyzer codes + +| Code | Level | Condition | |---|---|---| -| `BATCH_READ_FAILED` | Error | Ошибка чтения BAT/CMD | -| `TEXT_CONFIG_READ_FAILED` | Error | Ошибка чтения TXT | -| `WINWS_LAUNCH_NOT_FOUND` | Error | BAT/CMD не содержит поддерживаемого запуска winws/winws2 | -| `MULTIPLE_WINWS_LAUNCHES` | Error | Найдено более одного запуска winws/winws2 | -| `TEXT_CONFIG_HAS_NO_OPTIONS` | Error | TXT не содержит параметров `--...` или response file | -| `CONFIG_GENERATION_UNKNOWN` | Error | Поколение TXT не определено, а default отключен | -| `GENERATION_MISMATCH` | Error | Executable и параметры относятся к разным поколениям | -| `INCLUDED_CONFIG_NOT_FOUND` | Error | Указанный response file отсутствует | -| `INCLUDE_CYCLE` | Error | Обнаружен цикл подключений TXT | -| `INCLUDE_DEPTH_EXCEEDED` | Error | Превышена глубина подключений | -| `CONFIG_INCLUDE_IGNORES_OTHER_ARGUMENTS` | Warning | Рядом с response file присутствовали игнорируемые аргументы | -| `CALLED_SCRIPT_READ_FAILED` | Warning | Вызванный BAT/CMD найден, но не прочитан | -| `UNRESOLVED_BATCH_VARIABLE` | Warning | Используемая BAT-переменная не разрешена | -| `GENERATED_FILE_OUTSIDE_SOURCE` | Warning | Генерируемый файл находится вне каталога источника | -| `REFERENCED_FILE_NOT_FOUND` | Warning | BIN/TXT/LUA-файл отсутствует | - -### Коды установщика - -| Код | Условие | +| `BATCH_READ_FAILED` | Error | The BAT or CMD file cannot be read | +| `TEXT_CONFIG_READ_FAILED` | Error | The TXT file cannot be read | +| `WINWS_LAUNCH_NOT_FOUND` | Error | The BAT or CMD file has no supported winws or winws2 launch | +| `MULTIPLE_WINWS_LAUNCHES` | Error | The file has more than one winws or winws2 launch | +| `TEXT_CONFIG_HAS_NO_OPTIONS` | Error | The TXT file has no `--...` options and no response file | +| `CONFIG_GENERATION_UNKNOWN` | Error | The TXT generation is unknown, and the default is disabled | +| `GENERATION_MISMATCH` | Error | The executable and the options belong to different generations | +| `INCLUDED_CONFIG_NOT_FOUND` | Error | The referenced response file does not exist | +| `INCLUDE_CYCLE` | Error | The TXT includes make a cycle | +| `INCLUDE_DEPTH_EXCEEDED` | Error | The include depth is more than the limit | +| `CONFIG_INCLUDE_IGNORES_OTHER_ARGUMENTS` | Warning | The analyzer ignored arguments near the response file | +| `CALLED_SCRIPT_READ_FAILED` | Warning | A called BAT or CMD file exists but cannot be read | +| `UNRESOLVED_BATCH_VARIABLE` | Warning | A used BAT variable cannot be resolved | +| `GENERATED_FILE_OUTSIDE_SOURCE` | Warning | A generated file is outside the source folder | +| `REFERENCED_FILE_NOT_FOUND` | Warning | A BIN, TXT or LUA file is missing | + +### Installer codes + +| Code | Condition | |---|---| -| `IMPORT_RESULT_INVALID` | Анализ или ретаргетинг неуспешен | -| `IMPORT_NAME_REQUIRED` | Имя Config пусто | -| `IMPORT_MISSING_FILE_RESOLUTION_REQUIRED` | Не для каждого отсутствующего файла задано решение | -| `IMPORT_INSTALL_FAILED: ...` | Исключение записи, копирования, переписывания или сохранения | +| `IMPORT_RESULT_INVALID` | The analysis or the retarget is not successful | +| `IMPORT_NAME_REQUIRED` | The config name is empty | +| `IMPORT_MISSING_FILE_RESOLUTION_REQUIRED` | A missing file has no decision | +| `IMPORT_INSTALL_FAILED: ...` | An exception occurred during a write, a copy, a path change or the save | +| `: ...` | `ConfigShareService.InstallAsync` failed for a `.cdpiconfig` package | -`ConfigurationService.SaveConfigItem` может вернуть собственный error code. Он передается вызывающей стороне без изменения. Коды встроенного `Zapret1ToZapret2Converter` также добавляются в `Issues` без переименования, поэтому их полный набор определяется реализацией конвертера. +`ConfigurationService.SaveConfigItem` can return its own error code. The installer passes it to the caller without changes. The installer also adds the codes of `Zapret1ToZapret2Converter` to `Issues` without changes. The converter implementation sets the full list of these codes. -## Пример прямого использования AddOn +## Example of a direct call ```csharp using CDPIUI.AddOns.ConfigImport; @@ -865,11 +886,11 @@ ConfigImportInstallResult installed = await installer.InstallAsync( "Imported Zapret Config"); ``` -Перед массовым использованием `ReplacementPath: null` вызывающая сторона должна явно подтвердить такое решение пользователю. Сам анализатор не считает отсутствие файла разрешенным автоматически. +CAUTION: Ask the user to confirm `ReplacementPath: null` before you use it for many files. The analyzer does not treat a missing file as resolved automatically. -## Пример преобразования BAT-переменных +## Example of a BAT variable conversion -Исходный фрагмент: +The source fragment: ```bat set "LISTS=%~dp0lists\list-general.txt" @@ -877,7 +898,7 @@ rem set "LISTS=%~dp0lists\list-alt.txt" winws2.exe --hostlist=%LISTS% --lua-desync=fake ``` -Логическая часть результата до установки: +The logical part of the result before the save: ```json { @@ -898,98 +919,107 @@ winws2.exe --hostlist=%LISTS% --lua-desync=fake } ``` -После установки пути обоих значений переписываются в `List/Converted//...`. +After the save, the paths of the two values point to `List/Converted//...`. + +## Limits of the static analysis + +The add-on is not a `cmd.exe` interpreter. It processes these constructs partially or not at all: -## Границы статического анализа +- `if` branches and `for` loops; +- `set /a` calculations and complex substitutions `%VAR:...%`; +- variable and command names that a script makes at run time; +- `goto` and the general control flow of BAT files; +- commands that make files in a way other than the supported `echo >` and `echo >>` in `load_user_lists`; +- an executable that a wrapper, a function or an unknown construct starts; +- several possible launches with a selection at run time. The add-on treats them as ambiguous on purpose; +- response files in a non-standard format; +- resources with an extension that is not in the known list. -Механизм не является интерпретатором `cmd.exe`. Следующие конструкции могут быть обработаны только частично или не обработаны: +Also note these limits: -- произвольные ветвления `if` и циклы `for`; -- вычисления `set /a` и сложные подстановки `%VAR:...%`; -- динамически сформированные имена переменных и команд; -- `goto` и общий control flow BAT; -- команды, создающие файлы иначе, чем поддержанный `echo >`/`echo >>` в `load_user_lists`; -- запуск executable через обертку, функцию или конструкцию, которую токенизатор не распознает; -- несколько возможных запусков с runtime-выбором — они намеренно считаются неоднозначностью; -- response files нестандартного формата; -- ресурсы с расширением, отсутствующим в списке распознаваемых; -- значения, зависящие от текущего окружения ОС, могут импортироваться по-разному на разных компьютерах; -- семантическая совместимость произвольно выбранного компонента не проверяется, кроме специальных проверок Zapret; -- обратное преобразование winws2 в winws1 отсутствует; -- тестирование не изолирует процесс компонента и не эмулирует весь исходный BAT; -- временный переписчик теста поддерживает меньше относительных форм пути, чем установщик; -- текущая реализация не содержит отдельного автоматизированного набора тестов для Config Import. +- Values that depend on the current OS environment can give different results on different computers. +- The add-on does not check the compatibility of a random component, except for the special Zapret checks. +- The add-on cannot convert winws2 to winws1. +- The test does not isolate the component process and does not repeat the full source BAT. +- The temporary path replacement of the test knows fewer relative forms than the installer. -Нераспознанная переменная обычно не удаляется: она становится редактируемой `commaVar` со значением `$EMPTY`. Это сохраняет возможность вручную довести Config до рабочего состояния. +An unknown variable usually stays: it becomes an editable `commaVar` with the value `$EMPTY`. Thus the user can complete the config manually. -## Требования к расширению механизма +## Extension rules -### Добавление нового общего компонента +### A new general component -Если его BAT/CMD запускает executable напрямую и параметры представлены обычной командной строкой, отдельный парсер не требуется. Достаточно: +If its BAT or CMD file starts the executable directly with a usual command line, no separate parser is necessary. Do these steps: -1. добавить/установить Store-компонент с корректными `Id`, `Directory`, `Executable`, `CurrentVersion`; -2. при необходимости расширить список ресурсных расширений; -3. проверить распознавание executable и сохранение путей. +1. Add or install the Store component with correct `Id`, `Directory`, `Executable` and `CurrentVersion` values. +2. If necessary, extend the list of resource extensions. +3. Make sure that the executable detection and the path save operate. -### Добавление специализированного синтаксиса +### A special syntax -Если компонент использует собственный include-формат, условные переменные или генерирует ресурсы, рекомендуется: +If the component uses its own include format, conditional variables or generated resources, do these steps: -1. оставить `ConfigImportService` фасадом; -2. добавить отдельный статический анализатор без UI-зависимостей; -3. преобразовать специализированный результат в `ConfigImportResult`; -4. не выполнять исходный скрипт; -5. возвращать стабильные issue codes; -6. явно описать поколение/версию и правила несовместимости; -7. добавить обработку зависимостей в `ReferencedFiles`, `MissingReferencedFiles` и `GeneratedFiles`; -8. обеспечить одинаковое переписывание путей при тестировании и установке; -9. добавить автоматизированные regression-тесты на реальные наборы. +1. Keep `ConfigImportService` as the facade. +2. Add a separate static analyzer without UI dependencies. +3. Convert the special result to `ConfigImportResult`. +4. Do not run the source script. +5. Return stable issue codes. +6. Describe the generation, the version and the incompatibility rules explicitly. +7. Put the dependencies into `ReferencedFiles`, `MissingReferencedFiles` and `GeneratedFiles`. +8. Make sure that the test and the save change the paths in the same way. +9. Add automated tests with real config sets to `CDPIUI.Tests`. -### Изменение структуры хранения +### A change of the storage structure -Структуру `List|Bin|Lua/Converted/` следует менять только совместно с: +Change the structure `List|Bin|Lua/Converted/` only together with: - `GetResourceCategory`; - `GetOrganizedRelativePath`; - `BuildResources`; - `RewriteResources`; -- тестами загрузки Config через `ConfigurationService`; -- логикой удаления/rollback, ограничивающей допустимые корни. - -## Инварианты реализации - -При дальнейшей разработке должны сохраняться следующие свойства: - -1. Анализ входного BAT/CMD не запускает его. -2. Исходные файлы не изменяются и не удаляются. -3. Отсутствующий файл не считается решенным без явного `ConfigImportMissingFileResolution`. -4. Каждый импорт получает отдельный каталог ресурсов. -5. Пути в Config после установки указывают на фактическую категорию `List`, `Bin` или `Lua`. -6. Запись не может выйти за пределы staging/destination-каталога. -7. Ошибка сохранения Config инициирует rollback созданных ресурсов. -8. `winws1 -> winws2` преобразует готовую раскрытую строку, а специализированный Zapret-парсер не смешивает поколения. -9. Нативный winws2 Config сохраняется без конвертации синтаксиса. -10. Тестовые временные файлы удаляются после остановки процесса или закрытия страницы. - -## Рекомендуемая проверка изменений - -Минимальный regression-набор должен включать: - -1. простой BAT для общего компонента; -2. BAT с caret-продолжениями; -3. BAT с одним и несколькими executable-запусками; -4. winws1 BAT с последующим ретаргетингом на winws2; -5. нативный winws2 BAT; -6. TXT с `# BuiltinVersion`; -7. BAT, который ссылается только на TXT response file; -8. `service.bat` с переменными, диспетчеризацией и `load_user_lists`; -9. `ACTIVE_*.bin` с совпадающим SHA-256; -10. известный, отсутствующий, замененный и пустой ресурс каждого типа; -11. пути `list/lists`, `bin/fake`, `lua`, абсолютные и `$GETCURRENTDIR()`; -12. тестовый запуск с отсутствующими и генерируемыми файлами; -13. сохранение двух Config с одинаковым именем; -14. искусственную ошибку `SaveConfigItem` для проверки rollback; -15. JSON текущего и старого формата. - -Успешная сборка подтверждает только компиляцию. Отдельно необходимо проверять фактические аргументы процесса, содержимое сохраненного JSON, расположение ресурсов и запуск Config из обычной страницы компонента. +- the tests that load the config through `ConfigurationService`; +- the deletion and rollback logic that limits the permitted roots. + +## Invariants + +Keep these properties in all changes: + +1. The analysis of an input BAT or CMD file does not run it. +2. The add-on does not change or delete the source files. +3. A missing file is not resolved without an explicit `ConfigImportMissingFileResolution`. +4. Each import gets a separate resource folder. +5. After the save, the config paths point to the actual category `List`, `Bin` or `Lua`. +6. A write cannot go outside the staging or destination folder. +7. A failed config save starts a rollback of the created resources. +8. `winws1 -> winws2` converts the ready expanded line. The Zapret parser does not mix generations. +9. A native winws2 config is saved without a syntax conversion. +10. The test removes the temporary files after the process stops or the page closes. + +## Tests + +`CDPIUI.Tests/AddOns` has tests for this add-on: + +- `ConfigImportServiceTests.cs` checks `ConfigImportService.TryParseSet`; +- `ZapretConfigImportServiceTests.cs` checks `TrimShellTail` and `ExpandFileModifiers`. + +`CDPIUI.Tests/Core/BatchFileEncodingTests.cs` checks the encoding detection. + +A minimum set of manual checks for a change includes: + +1. a simple BAT file for a general component; +2. a BAT file with caret continuations; +3. a BAT file with one launch and with several launches; +4. a winws1 BAT file and a retarget to winws2; +5. a native winws2 BAT file; +6. a TXT file with `# BuiltinVersion`; +7. a BAT file that refers only to a TXT response file; +8. `service.bat` with variables, a dispatch and `load_user_lists`; +9. `ACTIVE_*.bin` with the same SHA-256; +10. a known, a missing, a replaced and an empty resource of each type; +11. the paths `list/lists`, `bin/fake`, `lua`, absolute paths and `$GETCURRENTDIR()`; +12. a test run with missing and generated files; +13. a save of two configs with the same name; +14. an artificial `SaveConfigItem` error to check the rollback; +15. a current and an old JSON format. + +A successful build proves only the compilation. Also check the actual process arguments, the saved JSON, the resource locations and a start of the config from the usual component page. diff --git a/CDPIUI.AddOns/ConfigImport/ZapretConfigImportService.cs b/CDPIUI.AddOns/ConfigImport/ZapretConfigImportService.cs index 83e0cc05..95978720 100644 --- a/CDPIUI.AddOns/ConfigImport/ZapretConfigImportService.cs +++ b/CDPIUI.AddOns/ConfigImport/ZapretConfigImportService.cs @@ -1,4 +1,4 @@ -using CDPIUI.Core.ComponentServices.Helpers.Configuration; +using CDPIUI.Core.ComponentServices.Helpers.Configuration; using CDPIUI.Core.Store.Data; using CDPIUI.Core.Store.Database; using System.Security.Cryptography; @@ -643,24 +643,35 @@ private static bool TryExtractLaunch(string line, int lineNumber, out BatchLaunc return true; } - private static string TrimShellTail(string input) + /// + /// Cuts the command at the first unescaped redirection, pipe or command separator and removes + /// the caret escapes outside quotes, as cmd does ("^&" becomes "&", "^^" becomes "^"). + /// + internal static string TrimShellTail(string input) { + var result = new StringBuilder(input.Length); bool quoted = false; for (int index = 0; index < input.Length; index++) { char current = input[index]; if (current == '"') quoted = !quoted; - if (quoted) - continue; - if (current is '>' or '<' or '|') - return input[..index]; - if (current == '&' && (index == 0 || input[index - 1] != '^')) - return input[..index]; + if (!quoted) + { + if (current == '^' && index + 1 < input.Length) + { + result.Append(input[++index]); + continue; + } + if (current is '>' or '<' or '|' or '&') + break; + } + + result.Append(current); } - return input; + return result.ToString(); } private static bool TryParseSet(string line, out string name, out string value) @@ -691,10 +702,14 @@ private static bool TryParseCommentedSet(string line, out string name, out strin return false; } - private static bool IsComment(string line) => - line.StartsWith("rem ", StringComparison.OrdinalIgnoreCase) || - line.Equals("rem", StringComparison.OrdinalIgnoreCase) || - line.StartsWith("::", StringComparison.Ordinal); + private static bool IsComment(string line) + { + // "@rem" and "rem" are comments too. + string command = line.TrimStart('@'); + return (command.StartsWith("rem", StringComparison.OrdinalIgnoreCase) && + (command.Length == 3 || char.IsWhiteSpace(command[3]))) || + line.StartsWith("::", StringComparison.Ordinal); + } private static void SetVariable( IDictionary variables, @@ -952,13 +967,14 @@ private static string ExpandBatchValue( return value; string directory = Path.GetDirectoryName(batchFile)! + Path.DirectorySeparatorChar; + // "%%" is a literal percent sign; it is kept out of the variable expansion below. + const string PercentPlaceholder = "\u0000"; string result = value + .Replace("%%", PercentPlaceholder, StringComparison.Ordinal) .Replace("$GETCURRENTDIR()/", directory, StringComparison.OrdinalIgnoreCase) - .Replace("$GETCURRENTDIR()\\", directory, StringComparison.OrdinalIgnoreCase) - .Replace("%~dp0", directory, StringComparison.OrdinalIgnoreCase) - .Replace("%~f0", batchFile, StringComparison.OrdinalIgnoreCase) - .Replace("%~n0", Path.GetFileNameWithoutExtension(batchFile), StringComparison.OrdinalIgnoreCase) - .Replace("%0", batchFile, StringComparison.OrdinalIgnoreCase); + .Replace("$GETCURRENTDIR()\\", directory, StringComparison.OrdinalIgnoreCase); + result = BatchFileModifierRegex().Replace(result, match => ExpandFileModifiers(match.Groups["modifiers"].Value, batchFile)); + result = result.Replace("%0", batchFile, StringComparison.OrdinalIgnoreCase); string Replace(Match match) { @@ -971,7 +987,32 @@ string Replace(Match match) result = BatchVariableRegex().Replace(result, Replace); result = DelayedVariableRegex().Replace(result, Replace); - return result; + return depth == 0 ? result.Replace(PercentPlaceholder, "%", StringComparison.Ordinal) : result; + } + + /// + /// Expands the %~...0 modifiers of the batch file path: f (full path), d (drive), p (directory), + /// n (name) and x (extension), combined as cmd does + /// + internal static string ExpandFileModifiers(string modifiers, string batchFile) + { + string fullPath = Path.GetFullPath(batchFile); + modifiers = modifiers.ToLowerInvariant(); + if (modifiers.Contains('f') && !modifiers.Any(c => c is 'd' or 'p' or 'n' or 'x')) + return fullPath; + + string directory = Path.GetDirectoryName(fullPath) ?? string.Empty; + string root = Path.GetPathRoot(fullPath) ?? string.Empty; + string drive = root.TrimEnd('\\', '/'); + string path = directory[root.Length..]; + path = path.Length == 0 ? "\\" : "\\" + path.TrimStart('\\') + "\\"; + + var result = new StringBuilder(); + if (modifiers.Contains('d')) result.Append(drive); + if (modifiers.Contains('p')) result.Append(path); + if (modifiers.Contains('n')) result.Append(Path.GetFileNameWithoutExtension(fullPath)); + if (modifiers.Contains('x')) result.Append(Path.GetExtension(fullPath)); + return result.ToString(); } private static bool TryGetCalledScript( @@ -1414,34 +1455,8 @@ private static void MergeVariables( } } - private static string[] ReadAllLines(string filePath) - { - byte[] bytes = File.ReadAllBytes(filePath); - Encoding encoding = DetectEncoding(bytes); - string text = encoding.GetString(bytes).TrimStart('\uFEFF'); - return text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Split('\n'); - } - - private static Encoding DetectEncoding(byte[] bytes) - { - if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) - return new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); - if (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) - return Encoding.Unicode; - if (bytes.Length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) - return Encoding.BigEndianUnicode; - - try - { - _ = new UTF8Encoding(false, true).GetString(bytes); - return new UTF8Encoding(false); - } - catch (DecoderFallbackException) - { - Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - return Encoding.GetEncoding(1251); - } - } + private static string[] ReadAllLines(string filePath) => + CDPIUI.Core.System.BatchFileEncoding.ReadAllLines(filePath); private static string NormalizeWhitespace(string input) { @@ -1680,7 +1695,8 @@ private sealed record CalledScript(string Path, IReadOnlyList Arguments) private static partial Regex Winws1OptionRegex(); [GeneratedRegex( - """(?ix)(?winws2?)(?:\.exe)?(?=["\s]|$)""")] + // A left boundary: "mywinws.exe" is not a winws launch. + """(?ix)(?winws2?)(?:\.exe)?(?=["\s]|$)""")] private static partial Regex WinwsExecutableRegex(); [GeneratedRegex("""(?i)^\s*@?set\s+(?")?(?[^="]+)=(?.*)$""")] @@ -1692,6 +1708,9 @@ private sealed record CalledScript(string Path, IReadOnlyList Arguments) [GeneratedRegex(@"%(?[A-Za-z0-9_~]+)%", RegexOptions.IgnoreCase)] private static partial Regex BatchVariableRegex(); + [GeneratedRegex(@"%~(?[fdpnxs]+)0", RegexOptions.IgnoreCase)] + private static partial Regex BatchFileModifierRegex(); + [GeneratedRegex(@"!(?[A-Za-z0-9_]+)!", RegexOptions.IgnoreCase)] private static partial Regex DelayedVariableRegex(); diff --git a/CDPIUI.AddOns/ConfigShare/ConfigShareResources.cs b/CDPIUI.AddOns/ConfigShare/ConfigShareResources.cs index 33d2b319..536996be 100644 --- a/CDPIUI.AddOns/ConfigShare/ConfigShareResources.cs +++ b/CDPIUI.AddOns/ConfigShare/ConfigShareResources.cs @@ -9,6 +9,10 @@ internal sealed class ConfigShareResources(ConfigItem config, string packDirecto internal sealed record Resource(string ArchivePath); internal Dictionary Files { get; } = new(StringComparer.OrdinalIgnoreCase); private readonly Dictionary replacements = new(StringComparer.OrdinalIgnoreCase); + private readonly HashSet emptyResources = new(StringComparer.OrdinalIgnoreCase); + + // A list that the component creates while it runs and that does not exist yet. + internal bool IsEmptyResource(string source) => emptyResources.Contains(source); internal void Collect() { @@ -20,7 +24,12 @@ internal void Collect() { throw new ConfigShareException("SHARE_FILE_MISSING", file.Path, exception); } - if (!File.Exists(source)) throw new ConfigShareException("SHARE_FILE_MISSING", file.Path); + // winws creates the --hostlist-auto list while it runs, so before the first run there is + // no file and the preset could not be shared. It is shared as an empty list; + // the user's kit is not changed. + if (!File.Exists(source) && IsRuntimeCreated(file.OptionName)) + emptyResources.Add(source); + if (!File.Exists(source) && !emptyResources.Contains(source)) throw new ConfigShareException("SHARE_FILE_MISSING", file.Path); if (!Files.TryGetValue(source, out var resource)) { if (Files.Count >= 4095) throw new ConfigShareException("SHARE_TOO_LARGE", "Too many resource files."); @@ -44,6 +53,10 @@ internal void Collect() MapConfig(config, text => ConfigShareService.ReplaceConfigReferences(text, replacements)); } + private static bool IsRuntimeCreated(string? optionName) => + string.Equals(optionName, "--hostlist-auto", StringComparison.OrdinalIgnoreCase) || + string.Equals(optionName, "--hostlist-auto-debug", StringComparison.OrdinalIgnoreCase); + private void AddReplacement(string reference, string portable) { if (string.IsNullOrWhiteSpace(reference)) return; diff --git a/CDPIUI.AddOns/ConfigShare/ConfigShareService.cs b/CDPIUI.AddOns/ConfigShare/ConfigShareService.cs index 07c7eac0..a6947c1d 100644 --- a/CDPIUI.AddOns/ConfigShare/ConfigShareService.cs +++ b/CDPIUI.AddOns/ConfigShare/ConfigShareService.cs @@ -22,6 +22,7 @@ public sealed class ConfigShareService internal const string ReferencePrefix = "share://"; private const string ManifestName = "manifest.json"; private const long MaxTotalBytes = 512L * 1024 * 1024; + private const long MaxCompressionRatio = 200; private const int MaxEntries = 4096; private const int MaxManifestBytes = 4 * 1024 * 1024; private static readonly SemaphoreSlim InstallGate = new(1, 1); @@ -78,6 +79,15 @@ private ConfigSharePackage Export(ConfigItem source, string name, string develop componentDirectory ??= DatabaseHelper.Instance.GetItemById(config.target[0])?.Directory; var collector = new ConfigShareResources(config, packDirectory, componentDirectory); collector.Collect(); + // The same check as the import: a preset that writes outside its files exported fine, + // and every receiver then got an archive that cannot be opened. + ConfigShareResources.MapConfig(config, text => + { + EnsureNoOutsideWrites( + text?.Replace(ReferencePrefix, directory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase), + directory); + return text; + }); config.name = config.not_converted_name = name.Trim(); config.jparams ??= []; config.variables ??= []; @@ -91,7 +101,7 @@ private ConfigSharePackage Export(ConfigItem source, string name, string develop { foreach (var resource in collector.Files) { - byte[] bytes = ReadBoundedFile(resource.Key); + byte[] bytes = collector.IsEmptyResource(resource.Key) ? [] : ReadBoundedFile(resource.Key); total += bytes.LongLength; if (total > MaxTotalBytes || manifest.Resources.Count >= MaxEntries - 1) throw new ConfigShareException("SHARE_TOO_LARGE", "The preset exceeds the package size or file count limit."); @@ -119,6 +129,58 @@ private ConfigSharePackage Export(ConfigItem source, string name, string develop } } + // Options with which the component writes a file. The tray starts components with + // administrator rights, so a shared preset could write anywhere with them. + private static readonly HashSet FileWritingOptions = new(StringComparer.OrdinalIgnoreCase) + { + "--debug", "--wf-save", "--hostlist-auto", "--hostlist-auto-debug" + }; + + /// + /// A shared preset may only write into its own files. + /// + internal static void EnsureNoOutsideWrites(string? text, string packageDirectory) + { + if (string.IsNullOrWhiteSpace(text)) + return; + string root = Path.GetFullPath(packageDirectory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + IReadOnlyList tokens = ConfigCommandLine.Tokenize(text); + for (int index = 0; index < tokens.Count; index++) + { + if (!ConfigCommandLine.TryGetOptionName(tokens[index], out string name, out int equalsIndex) || + !FileWritingOptions.Contains(name)) + continue; + + string value = equalsIndex >= 0 + ? tokens[index][(equalsIndex + 1)..] + : index + 1 < tokens.Count && !ConfigCommandLine.IsOption(tokens[index + 1]) ? tokens[index + 1] : string.Empty; + value = ConfigCommandLine.Unquote(value).Trim(); + if (name.Equals("--debug", StringComparison.OrdinalIgnoreCase)) + { + // --debug=0|1|syslog|android writes no file; --debug=@file does. + if (!value.StartsWith('@')) + continue; + value = value[1..]; + } + if (value.Length == 0) + continue; + + bool inside; + try + { + inside = Path.IsPathRooted(value) && + Path.GetFullPath(value).StartsWith(root, StringComparison.OrdinalIgnoreCase); + } + catch (Exception exception) when (exception is ArgumentException or NotSupportedException or PathTooLongException) + { + inside = false; + } + if (!inside) + throw new ConfigShareException("SHARE_UNSAFE_OPTION", + $"The preset writes a file outside of its package with {name}: {value}"); + } + } + public Task ReadAsync(string path) => Task.Run(() => Read(path)); public ConfigSharePackage Read(string path) @@ -138,6 +200,10 @@ public ConfigSharePackage Read(string path) throw new ConfigShareException("SHARE_ARCHIVE_INVALID", "Duplicate entries or excessive archive size."); if (((entry.ExternalAttributes >> 16) & 0xF000) == 0xA000) throw new ConfigShareException("SHARE_ARCHIVE_INVALID", "Symbolic links are not supported."); + // Site lists compress well, but not a thousand times: a small file that unpacks to + // the size limit only fills memory and disk. + if (entry.Length > 1024 * 1024 && entry.Length > entry.CompressedLength * MaxCompressionRatio) + throw new ConfigShareException("SHARE_ARCHIVE_INVALID", $"The entry {entry.FullName} is compressed too strongly."); } if (!entries.TryGetValue(ManifestName, out var manifestEntry) || manifestEntry.Length > MaxManifestBytes) throw new ConfigShareException("SHARE_ARCHIVE_INVALID", "The archive has no valid manifest."); @@ -170,6 +236,11 @@ public ConfigSharePackage Read(string path) config.variables ??= []; AddParentReferences(references); ConfigShareResources.MapConfig(config, text => ReplaceConfigReferences(text, references)); + ConfigShareResources.MapConfig(config, text => + { + EnsureNoOutsideWrites(text, directory); + return text; + }); config.packId = directory; config.file_name = null; foreach (var resource in manifest.Resources.Where(item => item.RewriteReferences)) @@ -266,7 +337,7 @@ private ConfigShareInstallResult Install(ConfigSharePackage package, string name Id = packId, Type = "configlist", Directory = packDirectory, Name = newPackName!.Trim(), ShortName = newPackName.Trim(), Developer = package.Manifest.Developer, VersionControlType = "local", CurrentVersion = "1.0", IconPath = "$STATICIMAGE(Store/empty.png)", - BackgroudColor = "", + BackgroundColor = "", RequiredItemIds = [Tuple.Create(config.target![0], config.target.ElementAtOrDefault(1) ?? "0.0")], DependentItemIds = [] })) throw new ConfigShareException("SHARE_DATABASE_FAILED", "Cannot register the new kit."); Log($"Installed shared preset in {packId}/{filename}."); diff --git a/CDPIUI.AddOns/ConfigShare/README.md b/CDPIUI.AddOns/ConfigShare/README.md index a811ad54..03fc98fc 100644 --- a/CDPIUI.AddOns/ConfigShare/README.md +++ b/CDPIUI.AddOns/ConfigShare/README.md @@ -1,53 +1,96 @@ # ConfigShare -`.cdpiconfig` is a ZIP archive containing a single preset and the resources it uses. -`ConfigShareService` provides export, archive validation, extraction and installation; -`ConfigImportService` and `ConfigImportInstaller` delegate this format to it. +A `.cdpiconfig` file is a ZIP archive with one preset and the resources that the preset uses. `ConfigShareService` exports, checks, extracts and installs these archives. `ConfigImportService` and `ConfigImportInstaller` send this format to `ConfigShareService`. + +The archive does not contain scripts, and the service does not run code from it. ## Format v1 -`manifest.json` contains `Version`, `Name`, `Developer`, `Config` (a `ConfigItem`), -and `Resources`. Each resource has `Path`, `Length`, `Sha256` and `RewriteReferences`. -Resources live under `resources/`; portable references use `share://resources/...`. -The file list comes exclusively from `ConfigItem.UsedFiles`. Paths are resolved with -`ConfigItem.ResolveFilePath`, using the component directory and preset pack directory. -ConfigShare only packages that list and rewrites the corresponding preset references. -Files, including component libraries, are copied unchanged; ConfigShare does not parse -Lua or enumerate additional dependencies. Output files excluded by `UsedFiles` are not -packaged. Missing files reported by `UsedFiles` produce an export error. +`manifest.json` contains `Version`, `Name`, `Developer`, `Config` (a `ConfigItem`) and `Resources`. Each resource has `Path`, `Length`, `Sha256` and `RewriteReferences`. The resources are in the folder `resources/`. A portable reference has the form `share://resources/...`. + +The export puts resources into these folders: + +| Source of the file | Archive path | +|---|---| +| The preset kit folder | `resources/preset/...` | +| The component folder | `resources/component/...` | +| Other locations | `resources/external//...` | + +The file list comes only from `ConfigItem.UsedFiles`. The export resolves each path with `ConfigItem.ResolveFilePath`. It uses the component folder and the preset kit folder. ConfigShare packages only this list and changes the related references in the preset. It copies the files, also component libraries, without changes. ConfigShare does not parse Lua and does not look for more dependencies. + +`UsedFiles` does not contain the target of `--debug`, so the export does not package it. If a file from `UsedFiles` is missing, the export fails with `SHARE_FILE_MISSING`. Exception: if the list of `--hostlist-auto` or `--hostlist-auto-debug` is missing, the export packages an empty file for it. The export does not make a file in the preset kit. The component makes this list while it operates. + +## Limits and checks + +| Item | Limit | +|---|---:| +| Entries in the archive | 4096, including `manifest.json` | +| Resources in an export | 4095 | +| Uncompressed size, total and for one entry | 512 MiB | +| `manifest.json` | 4 MiB | +| Compression ratio of an entry larger than 1 MiB | 200 | + +Before the installation, the reader checks: + +- the format version (only `1`); +- that the name, the developer, `target[0]` and `startup_string` are not empty; +- that the archive has exactly the entries of the manifest and `manifest.json`; +- duplicate entry names (the comparison ignores the case); +- the declared size and the SHA-256 of each resource; +- the entry count, the total size and the compression ratio; +- safe relative paths. + +A safe path starts with `resources/` and uses only `/`. It has no `:`, no `.` or `..` part, no part that ends with a dot or a space, no invalid file name characters and no reserved device names such as `CON` or `LPT1`. + +The archive cannot contain filesystem links. The reader rejects symbolic link entries. `SafeDestination` also rejects a destination folder that contains a reparse point. + +## Options that write files + +The tray starts components with administrator rights. Thus a shared preset must not write files outside its package. After the reader replaces the `share://` references, `EnsureNoOutsideWrites` checks each text of the config. It looks for these options: + +- `--debug=@` (`--debug` with other values does not write a file); +- `--wf-save`; +- `--hostlist-auto`; +- `--hostlist-auto-debug`. -The reader checks format version, resource hashes, declared sizes, duplicate names, -entry count and safe relative paths before installation. Limits: 4096 archive entries, -512 MiB uncompressed total and 4 MiB manifest. Archives cannot contain filesystem links. +The value of such an option must be an absolute path in the extracted package folder. Otherwise, the reader fails with `SHARE_UNSAFE_OPTION`. A relative path is also rejected. + +The export does the same check after it replaces the references. Thus the export fails with `SHARE_UNSAFE_OPTION` for a preset that the reader would reject. + +`BlockCheckReportImportService` uses the same check for the preset line of an imported BlockCheck2 report. Refer to [BlockCheck2/README.md](../BlockCheck2/README.md). ## Lifecycle -`ExportAsync` and `ReadAsync` return an owning `ConfigSharePackage`. Dispose it after -copying the exported file or completing/canceling import. Before Windows sharing, -`RetainForSystemShare` marks its directory so disposing the package keeps the file. -Opening the next export dialog calls `CleanupPreviousSystemShares`, which removes -these marked directories even after an application restart. Other in-progress imports -and exports are not touched. Temporary directories are allocated under `TempFiles` with -`FileSystemService.GetNewTempFileName` and a GUID to prevent name collisions. - -`InstallAsync` creates resources in a unique `Shared/` directory. It can install -into `LocalUserData` or an existing GUID kit, or register a new GUID kit. Store IDs -are never valid destinations. Failure removes only the files created by that import. -Reimporting creates a new preset and never overwrites an existing preset or resource. - -Dependencies not reported by `UsedFiles` must be explicitly attached as preset resources. - -## UI and validation - -The common component `ConfigSelector` opens the export dialog. File activation opens -`ConfigShareImportDialog`; the ConfigImport wizard keeps its usual review/test/save UI. -Export and message layouts are defined in `Controls/Dialogs/ConfigShare/*.xaml`; -their code-behind handles actions and state changes. -Both entry points offer the existing Store component installer when the required -component is missing. Zapret Legacy is considered available when Zapret2 is installed; -the wizard uses the existing Legacy-to-Zapret2 conversion in that case. -Windows sharing uses [per-window desktop interop](https://learn.microsoft.com/windows/apps/develop/ui/display-ui-objects). - -Run `dotnet run --project CDPIUI.AddOns.ConfigShare.Regression` for isolated round-trip, -dependency, destination, malformed archive and cleanup checks. The regression program -uses its own data directory; it does not modify the installed application's presets. +`ExportAsync` and `ReadAsync` return a `ConfigSharePackage` that owns its temporary folder. Dispose the package after you copy the exported file, or after the import is complete or canceled. + +Before a Windows share, call `RetainForSystemShare`. This writes the marker file `.retain-for-system-share`, and `Dispose` then keeps the folder. When the next export dialog opens, `CleanupPreviousSystemShares` deletes the marked folders. This also occurs after an application restart. The cleanup does not touch other imports and exports that are in progress. + +The temporary folders are in `TempFiles`. Their names come from `FileSystemService.GetNewTempFileName` and contain `ConfigShare_` and a GUID. Thus names do not collide. + +`InstallAsync` puts the resources into a unique folder `Shared/` in the destination kit. Only one installation operates at a time. The destination can be: + +- `LocalUserStorage` (the default); +- an existing kit with a GUID ID; +- a new kit with a GUID ID. The service makes the folder with `init.json` and registers a `configlist` item in the Store DB. + +Store kit IDs are never valid destinations (`SHARE_DESTINATION_INVALID`). The config file name is `preset__<8 hex characters>.json`. If an error occurs, the service removes only the files and folders that this installation made. A second import of the same package makes a new preset. It never overwrites an existing preset or resource. + +Dependencies that `UsedFiles` does not report must be attached to the preset as explicit resources. + +## UI + +| Entry point | Function | +|---|---| +| `ConfigSelector` (`CDPIUI/Controls/ComponentSettings`) | Opens the export dialog through `ConfigShareUI.ShowExportAsync` | +| `ConfigShareExportContentDialog`, `ConfigShareMessageContentDialog` (`CDPIUI/Controls/Dialogs/ConfigShare/*.xaml`) | Export and message layouts. The code-behind files process the actions and the state changes. | +| `ConfigShareImportDialog` (`CDPIUI/Windows/Messages`) | Opens when the user opens a `.cdpiconfig` file | +| The ConfigImport wizard | Shows its usual review, test and save steps for `.cdpiconfig` files | +| `WindowsPresetShare` (`CDPIUI/Helper/AddOns/ConfigShare`) | Windows sharing through [per-window desktop interop](https://learn.microsoft.com/windows/apps/develop/ui/display-ui-objects) | + +The two import entry points offer the Store installer of the component when the required component is missing (`ConfigShareUI.OfferComponentInstallAsync`). Zapret Legacy is available when Zapret2 is installed. In this case, the wizard uses the existing conversion from Legacy to Zapret2. + +## Tests + +`CDPIUI.Tests/AddOns/ConfigShareWriteOptionsTests.cs` checks `EnsureNoOutsideWrites`. It makes sure that file writes outside the package are rejected and that writes inside the package and a plain `--debug` are accepted. + +The solution has no separate test project for the round trip, the dependencies, the destinations, damaged archives or the cleanup. diff --git a/CDPIUI.AddOns/GoodCheck/DomainValidationHelper.cs b/CDPIUI.AddOns/GoodCheck/DomainValidationHelper.cs index 664332bf..4d2cb3ce 100644 --- a/CDPIUI.AddOns/GoodCheck/DomainValidationHelper.cs +++ b/CDPIUI.AddOns/GoodCheck/DomainValidationHelper.cs @@ -122,7 +122,8 @@ private static async Task IsFileSupportedAsync(string filePath, CheckMode string line = raw.Trim(); - if (string.IsNullOrEmpty(line)) + // Comment lines are common in site lists and do not make a list invalid. + if (string.IsNullOrEmpty(line) || line.StartsWith('#')) { continue; } diff --git a/CDPIUI.AddOns/GoodCheck/GoodCheckProcessService.cs b/CDPIUI.AddOns/GoodCheck/GoodCheckProcessService.cs index c61ae438..b021b16e 100644 --- a/CDPIUI.AddOns/GoodCheck/GoodCheckProcessService.cs +++ b/CDPIUI.AddOns/GoodCheck/GoodCheckProcessService.cs @@ -2,7 +2,7 @@ using CDPIUI.Core.Basic; using CDPIUI.Core.Communication; using CDPIUI.Core.Data; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Core.Store.Data; using CDPIUI.Core.Store.Database; using System.Text; @@ -27,11 +27,24 @@ public class GoodCheckOperationModel public int OperationId { get; set; } public string? SiteListName { get; set; } public string? SiteListPath { get; set; } - public string Output { get; set; } = ""; + // Appended line by line during a long run: string concatenation copied the whole log + // on every line. + private readonly System.Text.StringBuilder _output = new(); + public string Output + { + get { lock (_output) return _output.ToString(); } + set { lock (_output) _output.Clear().Append(value); } + } + public void AppendOutputLine(string line) + { + lock (_output) _output.Append(line).Append('\n'); + } public GoodCheckOperationType OperationType { get; set; } = GoodCheckOperationType.Wait; public string ErrorCode { get; set; } = ""; public CancellationTokenSource? CancellationTokenSource { get; set; } public CancellationToken CancellationToken { get; set; } + /// The tray reported that the GoodCheck process ended. + public bool ProcessExited { get; set; } } public enum GoodCheckOperationType { @@ -43,18 +56,18 @@ public enum GoodCheckOperationType } public class GoodCheckProcessService { - public Action AllComplete; - public Action ErrorHappens; - public Action ProcessCompleted; - public Action CurrentSiteListChanged; - public Action> ProgressChanged; - public Action> StrategyResultAdded; - public Action CorrectCountChanged; - public Action IncorrectCountChanged; - - public Action> OperationOutputAdded; - public Action> OperationTypeChanged; - public Action OperationsListChanged; + public event Action? AllComplete; + public event Action? ErrorHappens; + public event Action? ProcessCompleted; + public event Action? CurrentSiteListChanged; + public event Action>? ProgressChanged; + public event Action>? StrategyResultAdded; + public event Action? CorrectCountChanged; + public event Action? IncorrectCountChanged; + + public event Action>? OperationOutputAdded; + public event Action>? OperationTypeChanged; + public event Action? OperationsListChanged; public int CorrectCount { get; private set; } public int IncorrectCount { get; private set; } @@ -103,6 +116,22 @@ private GoodCheckProcessService() } + /// + /// The folder GoodCheck is installed in, as recorded in the Store database. + /// + public static string GetAddOnDirectory() + { + if (DatabaseHelper.Instance.IsItemInstalled(AddonId)) + { + string? directory = DatabaseHelper.Instance.GetItemById(AddonId)?.Directory; + if (!string.IsNullOrWhiteSpace(directory) && Directory.Exists(directory)) + return directory; + } + return Path.Combine(Directories.StoreItemsDirectory, AddonId); + } + + public static bool IsAddOnInstalled() => DatabaseHelper.Instance.IsItemInstalled(AddonId); + private bool IsGoodCheckInstalled() { bool isInstalled = DatabaseHelper.Instance.IsItemInstalled(AddonId); @@ -112,8 +141,7 @@ private bool IsGoodCheckInstalled() DatabaseStoreItem item = DatabaseHelper.Instance.GetItemById(AddonId); - string localAppData = Core.Data.Directories.DataDirectory; - DirName = Path.Combine(Directories.StoreItemsDirectory, AddonId); + DirName = GetAddOnDirectory(); ExeFileName = Path.Combine( DirName, @@ -130,7 +158,8 @@ public static void MergeUniquePreserveOrder( bool ignoreEmpty = true, StringComparer? comparer = null) { - encoding ??= Encoding.UTF8; + // Without BOM: GoodCheck took it for a part of the first domain. + encoding ??= new UTF8Encoding(false); comparer ??= StringComparer.Ordinal; var seen = new HashSet(comparer); @@ -165,13 +194,18 @@ List siteListsToCheck CurrentOperationId = 0; + // GetKeyByValue returns default(Components), Zapret, for Zapret2 or NoDPI, and the + // check then ran with the wrong component. + if (!IsComponentSupported(componentId)) + throw new NotSupportedException($"GoodCheck does not support the component {componentId}."); ComponentName = GoodCheckResultViewHelper.ComponentNormalNames[HardcodedItemIds.ComponentIds.GetKeyByValue(componentId)]; ComponentId = componentId; _mode = checkMode; - string localAppData = Core.Data.Directories.DataDirectory; - string siteListFolder = Path.Combine(Directories.StoreItemsDirectory, AddonId, CheckLists); + string siteListFolder = Path.Combine(GetAddOnDirectory(), CheckLists); + + RemoveOldMergedSiteLists(siteListFolder); if (_mode == GoodCheckProcessMode.AllAsOne) { @@ -200,13 +234,21 @@ List siteListsToCheck } else { + var usedNames = new HashSet(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < siteListsToCheck.Count; i++) { var model = siteListsToCheck[i]; string path = model.SiteListPath; - File.Copy(model.SiteListPath, Path.Combine(siteListFolder, Path.GetFileName(model.SiteListPath)), true); + // Lists with the same file name from different packs overwrote each other. + string name = Path.GetFileName(model.SiteListPath); + if (!usedNames.Add(name)) + { + name = $"{i}_{name}"; + usedNames.Add(name); + } + File.Copy(model.SiteListPath, Path.Combine(siteListFolder, name), true); - model.SiteListPath = Path.GetFileName(model.SiteListPath); + model.SiteListPath = name; model.StrategyListPath = Path.GetFileName(model.StrategyListPath); CancellationTokenSource cancellationTokenSource = new(); @@ -227,6 +269,26 @@ List siteListsToCheck } + // The merged lists of earlier runs ({epoch}_sitelist.txt) were never removed. + private static void RemoveOldMergedSiteLists(string siteListFolder) + { + try + { + if (!Directory.Exists(siteListFolder)) + return; + foreach (string file in Directory.EnumerateFiles(siteListFolder, "*_sitelist.txt")) + { + string prefix = Path.GetFileName(file).Split('_')[0]; + if (prefix.All(char.IsDigit) && File.GetLastWriteTimeUtc(file) < DateTime.UtcNow.AddDays(-1)) + File.Delete(file); + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Logger.Instance.CreateWarningLog(nameof(GoodCheckProcessService), $"Cannot remove old site lists: {ex.Message}"); + } + } + public void Start() { CancellationTokenSource = new(); @@ -234,9 +296,14 @@ public void Start() _ = StartCheck(CancellationToken); } + public static bool IsComponentSupported(string componentId) => + GoodCheckResultViewHelper.ComponentNormalNames.Keys + .Any(component => HardcodedItemIds.ComponentIds.TryGetValue(component, out string? id) && id == componentId); + public void Stop() { - CancellationTokenSource.Cancel(); + // Stop before the first Start had no source to cancel. + CancellationTokenSource?.Cancel(); _ = PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Stop); } @@ -292,6 +359,15 @@ public async Task StartCheck(CancellationToken cancellationToken) ErrorHappens?.Invoke("ERR_INTERNAL"); AllComplete?.Invoke(string.Empty); } + finally + { + // A stopped or failed check left its operation shown as running. + foreach (var operation in Operations.Where(o => o.OperationType == GoodCheckOperationType.WorkInProgress).ToList()) + { + operation.OperationType = GoodCheckOperationType.UserInterrupt; + OperationTypeChanged?.Invoke(Tuple.Create(operation.OperationId, GoodCheckOperationType.UserInterrupt)); + } + } } private async Task StartProcessAsync(GoodCheckSiteListModel siteList, CancellationToken cancellationToken) @@ -305,9 +381,13 @@ private async Task StartProcessAsync(GoodCheckSiteListModel siteList, Canc return false; } string resolver = SettingsManager.Instance.GetValue(["ADDONS", AddonId], "useCurl") ? "curl" : "native"; + // A number from a text box: anything else would add arguments of its own. + string passes = SettingsManager.Instance.GetValue(["ADDONS", AddonId], "passesValue") ?? string.Empty; + if (!int.TryParse(passes.Trim(), out int passCount) || passCount < 1) + passCount = 1; string args = $"-q " + - $"-p {SettingsManager.Instance.GetValue(["ADDONS", AddonId], "passesValue")} " + + $"-p {passCount} " + $"-f {ComponentName} " + $"-m {resolver} " + $"-c \"{siteList.SiteListPath}\" " + @@ -330,6 +410,11 @@ private async Task StartProcessAsync(GoodCheckSiteListModel siteList, Canc GoodCheckOperationModel model = GetOperationById(CurrentOperationId); + if (model.ProcessExited && !cancellationToken.IsCancellationRequested && !CancellationToken.IsCancellationRequested) + { + // The process ended by itself: the result comes from its complete log. + return result; + } if (cancellationToken.IsCancellationRequested || model.CancellationToken.IsCancellationRequested || CancellationToken.IsCancellationRequested) { _ = PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Stop); @@ -391,7 +476,7 @@ private void AddOutput(string output) StrategyResultAdded?.Invoke(Tuple.Create(currentInt, allInt)); - if (allInt != 0 && currentInt / allInt * 100 >= 65) + if (allInt != 0 && currentInt * 100 / allInt >= 65) { CorrectCount++; CorrectCountChanged?.Invoke(CorrectCount); @@ -406,7 +491,7 @@ private void AddOutput(string output) } OperationOutputAdded?.Invoke(Tuple.Create(CurrentOperationId, $"{output}")); - Operations[CurrentOperationId].Output += $"{output}\n"; + Operations[CurrentOperationId].AppendOutputLine(output); } private async Task MonitorProcessAndLogsAsync(string logsDirectory, CancellationToken cancellationToken) @@ -421,12 +506,32 @@ private async Task MonitorProcessAndLogsAsync(string logsDirectory, Cancel FileStream fs = null; GoodCheckOperationModel model = GetOperationById(CurrentOperationId); + // Only the log of this run: an older log could end with "All Done". + DateTime runStartUtc = DateTime.UtcNow.AddSeconds(-2); + + // Returns true or false when the line decides the result, otherwise null. + bool? CheckLines(string text) + { + foreach (var line in text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)) + { + AddOutput(line); + + if (line.IndexOf(errorFlag, StringComparison.OrdinalIgnoreCase) >= 0) + { + HandleProcessException("ERR_GOODCHECK_EXCEPTION", CurrentOperationId); + return false; + } + if (line.IndexOf(successFlag, StringComparison.OrdinalIgnoreCase) >= 0) + return true; + } + return null; + } try { while (!cancellationToken.IsCancellationRequested && !model.CancellationToken.IsCancellationRequested && !CancellationToken.IsCancellationRequested) { - string newest = GetNewestLogFile(logsDirectory, logPattern); + string newest = GetNewestLogFile(logsDirectory, logPattern, runStartUtc); if (string.IsNullOrEmpty(newest)) { @@ -439,12 +544,11 @@ private async Task MonitorProcessAndLogsAsync(string logsDirectory, Cancel reader?.Dispose(); fs?.Dispose(); + // A log of this run is read from its start: reading from the end lost + // everything written before the file was found. currentLogFile = newest; fs = new FileStream(currentLogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); reader = new StreamReader(fs); - - currentPosition = fs.Length; - fs.Seek(currentPosition, SeekOrigin.Begin); } string newText = await reader.ReadToEndAsync().ConfigureAwait(false); @@ -452,27 +556,25 @@ private async Task MonitorProcessAndLogsAsync(string logsDirectory, Cancel if (!string.IsNullOrEmpty(newText)) { currentPosition = fs.Position; - string[] lines = newText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); - foreach (var line in lines) - { - AddOutput(line); - - if (line.IndexOf(errorFlag, StringComparison.OrdinalIgnoreCase) >= 0) - { - HandleProcessException("ERR_GOODCHECK_EXCEPTION", CurrentOperationId); - return false; - } - if (line.IndexOf(successFlag, StringComparison.OrdinalIgnoreCase) >= 0) - { - return true; - } - } + bool? decided = CheckLines(newText); + if (decided != null) + return decided.Value; } await Task.Delay(500, cancellationToken).ConfigureAwait(false); } - + if (model.ProcessExited && !cancellationToken.IsCancellationRequested && !CancellationToken.IsCancellationRequested) + { + // The process ended: read what it wrote last, and a run without "All Done", + // such as a failed connectivity check, is no success. + string rest = reader != null ? await reader.ReadToEndAsync().ConfigureAwait(false) : string.Empty; + bool? decided = string.IsNullOrEmpty(rest) ? null : CheckLines(rest); + if (decided != null) + return decided.Value; + HandleProcessException("ERR_GOODCHECK_EXITED_EARLY", CurrentOperationId); + return false; + } } catch (OperationCanceledException) { @@ -495,13 +597,15 @@ private async Task MonitorProcessAndLogsAsync(string logsDirectory, Cancel return false; } - private string? GetNewestLogFile(string directory, string pattern) + private string? GetNewestLogFile(string directory, string pattern, DateTime notBeforeUtc) { try { if (!Directory.Exists(directory)) return null; - var files = Directory.GetFiles(directory, pattern); - if (files == null || files.Length == 0) return null; + var files = Directory.GetFiles(directory, pattern) + .Where(f => File.GetCreationTimeUtc(f) >= notBeforeUtc) + .ToArray(); + if (files.Length == 0) return null; return files.OrderByDescending(f => File.GetLastWriteTimeUtc(f)).FirstOrDefault(); } @@ -519,14 +623,26 @@ public void HandleProcessException(string errorCode, int operationId) ErrorHappens?.Invoke(errorCode); } - public void OperationWithIdDied(int id) + /// + /// The tray could not start GoodCheck or it ended with an error. The log monitor waited for + /// a process that never ran until the user cancelled; it stops now. + /// + public void OperationWithIdFailed(int id, string errorCode) { + HandleProcessException(string.IsNullOrEmpty(errorCode) ? "ERR_UNKNOWN" : errorCode, id); GoodCheckOperationModel model = GetOperationById(id); - model.OperationType = GoodCheckOperationType.SuccessFinish; - model.CancellationTokenSource.Cancel(); - // model.CancellationTokenSource.Dispose(); + if (model == null) return; + model.ProcessExited = true; + model.CancellationTokenSource?.Cancel(); + } - OperationTypeChanged?.Invoke(Tuple.Create(id, GoodCheckOperationType.SuccessFinish)); + public void OperationWithIdDied(int id) + { + // Ended by itself: the log monitor decides whether it was a success. Marking it as one + // here turned an early failure into a success with an empty report. + GoodCheckOperationModel model = GetOperationById(id); + model.ProcessExited = true; + model.CancellationTokenSource?.Cancel(); } public void RemoveFromQueueOrStopOperation(int id) @@ -545,8 +661,7 @@ private void CreateAndSaveReport() int secondsSinceEpoch = (int)t.TotalSeconds; string filePath = Path.Combine( - Directories.StoreItemsDirectory, - AddonId, + GetAddOnDirectory(), "Reports", $"GoodCheckReport_{secondsSinceEpoch}.xml"); if (!Path.Exists(Path.GetDirectoryName(filePath))) @@ -603,7 +718,7 @@ public List GetCurrentOperations() catch { return null; } } - public bool IsRunned() + public bool IsRunning() { return CancellationTokenSource != null && !CancellationTokenSource.IsCancellationRequested; } diff --git a/CDPIUI.AddOns/GoodCheck/GoodCheckResultViewHelper.cs b/CDPIUI.AddOns/GoodCheck/GoodCheckResultViewHelper.cs index 2b950c07..e703c66d 100644 --- a/CDPIUI.AddOns/GoodCheck/GoodCheckResultViewHelper.cs +++ b/CDPIUI.AddOns/GoodCheck/GoodCheckResultViewHelper.cs @@ -1,7 +1,7 @@ using CDPIUI.Core.Basic; using CDPIUI.Core.Data; using CDPIUI.Core.Store.Data; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System.Xml.Linq; namespace CDPIUI.AddOns.GoodCheck @@ -45,9 +45,9 @@ public static List GetAvailableStrategiesLists(string c { List strategiesLists = []; + // The folder recorded in the Store database, as for the GoodCheck executable. string targetFolder = - Path.Combine(Directories.StoreItemsDirectory, - HardcodedItemIds.AddOnsIds[Core.Store.Data.AddOns.GoodCheck], + Path.Combine(GoodCheckProcessService.GetAddOnDirectory(), "StrategiesGoGo", ComponentNormalNames[HardcodedItemIds.ComponentIds.GetKeyByValue(componentId)] ); @@ -56,7 +56,9 @@ public static List GetAvailableStrategiesLists(string c try { - var files = Directory.EnumerateFiles(targetFolder, "*.txt", SearchOption.AllDirectories); + // GoodCheck gets only the file name (-s) and looks in this folder, so lists in + // subfolders could be selected but not found. + var files = Directory.EnumerateFiles(targetFolder, "*.txt", SearchOption.TopDirectoryOnly); foreach (var file in files) { diff --git a/CDPIUI.AddOns/GoodCheck/README.md b/CDPIUI.AddOns/GoodCheck/README.md new file mode 100644 index 00000000..9240f1c9 --- /dev/null +++ b/CDPIUI.AddOns/GoodCheck/README.md @@ -0,0 +1,221 @@ +# GoodCheck add-on + +## Purpose + +GoodCheck is an external add-on. It is a separate executable file that the user installs from the Store. The Store ID is `ASGKOI001` (`HardcodedItemIds.AddOnsIds[AddOns.GoodCheck]`). GoodCheck starts a component with each strategy from a strategy list and checks the sites from a site list. It writes the results to a log file. + +The folder `CDPIUI.AddOns/GoodCheck` contains the CDPI UI side of the add-on. This code prepares the site lists, sends the start request to the tray, reads the GoodCheck log, counts the results and writes an XML report. The CDPI UI code does not contain the GoodCheck algorithm. + +## Supported components + +GoodCheck supports only these components: + +| Component | `HardcodedItemIds` key | GoodCheck name (`-f`) | +|---|---|---| +| ByeDPI | `Components.ByeDPI` | `ciadpi` | +| GoodbyeDPI | `Components.GoodbyeDPI` | `gdpi` | +| Zapret Legacy | `Components.Zapret` | `zapret` | + +`GoodCheckResultViewHelper.ComponentNormalNames` contains this map. `GoodCheckProcessService.IsComponentSupported` uses it. `InitGoodCheck` throws `NotSupportedException` for other components. The GUI uses the same list from `HardcodedItemIds.GoodCheckSupportedComponents`. + +For Zapret2, the auto-selection window opens BlockCheck2 instead. Refer to [BlockCheck2/README.md](../BlockCheck2/README.md). + +## Main types + +| Type | File | Function | +|---|---|---| +| `GoodCheckProcessService` | `GoodCheckProcessService.cs` | Singleton. Prepares the lists, starts the checks one by one, reads the logs, raises events and writes the report. | +| `GoodCheckProcessMode` | `GoodCheckProcessService.cs` | `AllAsOne` or `AnyAsAny` | +| `GoodCheckSiteListModel` | `GoodCheckProcessService.cs` | A site list and its strategy list | +| `GoodCheckOperationModel` | `GoodCheckProcessService.cs` | One check: ID, list name, output, state, error code and cancel token | +| `GoodCheckOperationType` | `GoodCheckProcessService.cs` | `Wait`, `WorkInProgress`, `SuccessFinish`, `ErrorHappens` or `UserInterrupt` | +| `GoodCheckResultViewHelper` | `GoodCheckResultViewHelper.cs` | The component names, the available strategy lists and the report loader | +| `StrategyModel`, `GroupModel`, `GoodCheckStrategiesList` | `GoodCheckResultViewHelper.cs` | Report and strategy list models | +| `SiteListHelper` | `SiteListHelper.cs` | Finds the site lists of the installed config kits | +| `DomainValidationHelper` | `DomainValidationHelper.cs` | Checks that a TXT file is a domain list | + +## Modes + +| Mode | Function | +|---|---| +| `AllAsOne` | The service merges all selected site lists into one list. It checks this list with one strategy list. | +| `AnyAsAny` | The service checks each site list separately, each with its own strategy list. | + +In `AllAsOne` mode, the service uses the strategy list of the first site list. It writes the merged list as `_sitelist.txt`. The merge removes empty lines and duplicate lines and keeps the order. The merge writes UTF-8 without BOM, because GoodCheck reads a BOM as a part of the first domain. + +In `AnyAsAny` mode, the service copies each site list into the `CheckLists` folder. If two lists have the same file name, the second copy gets the prefix `_`. + +The GUI disables the mode switch for GoodbyeDPI. Thus GoodbyeDPI uses only `AllAsOne`. + +## Files of the add-on + +All paths are relative to the add-on folder. `GoodCheckProcessService.GetAddOnDirectory()` returns the folder from the Store DB. If the DB has no valid folder, it returns `Store/Items/ASGKOI001`. + +| Path | Contents | +|---|---| +| `.exe` | The GoodCheck executable. The name comes from the `Executable` field of the Store item. | +| `config.ini` | The GoodCheck settings | +| `CheckLists/` | The site lists of the current run | +| `StrategiesGoGo//*.txt` | The strategy lists of each component | +| `Logs/logfile_GoodCheckGoGo_*.log` | The logs that GoodCheck writes | +| `Reports/GoodCheckReport_.xml` | The reports that CDPI UI writes | + +Before each run, the service deletes merged lists (`_sitelist.txt`) that are older than one day. + +## Sequence of a run + +1. The user opens the auto-selection window and selects a supported component. +2. If GoodCheck is not installed, the window opens the Store download dialog for `ASGKOI001`. +3. On the `CreateViaGoodCheck` page, the user selects the site lists, the mode and the strategy lists. +4. The user sets the additional settings. The page saves them (refer to "Settings"). +5. The page calls `InitGoodCheck` with the component ID, the mode and the list models. The service prepares the lists and makes one `GoodCheckOperationModel` for each check. +6. The page opens `GoodCheckWorkPage` and calls `Start`. +7. For each check, the service sends the pipe message `GoodCheckMessageIds.Start` with the executable path and the arguments. +8. The tray starts GoodCheck. Refer to "Tray process". +9. The service reads the newest GoodCheck log and updates the progress. +10. After the last check, or after a cancel, the service writes the XML report and raises `AllComplete`. +11. `GoodCheckWorkPage` shows the end state. The forward button opens the report in `CreateConfigHelperWindow`. + +The service uses these arguments: + +```text +-q -p -f -m -c "" -s "" +``` + +The service passes only the file names of the site list and the strategy list. + +## Tray process + +`CDPIUI.TrayIcon/Helper/GoodCheckProcessHelper.cs` starts the process. It operates with administrator rights. It does these steps: + +1. It makes sure that the executable is in a trusted path (`TrustedPaths.IsAllowed`). Otherwise, it reports `ERR_UNABLE_START_PROCESS`. +2. It stops all component tasks. +3. It sends `Running` to the GUI and blocks the component buttons. +4. It starts GoodCheck without a window. The working folder is the folder of the executable. +5. It puts the process into a job object that kills the process when the job closes. +6. When the process stops, it sends `Died` with the operation ID. +7. It unblocks the component buttons. + +A start error gives `DiedViaError` with `ERR_UNABLE_START_PROCESS` or `ERR_WIN32_EXCEPTION`. The message `Stop` kills the process. When the pipe disconnects, the tray also stops GoodCheck. + +In the GUI, `CDPIUI/Commands/CommandsHandler.cs` sends `Died` to `OperationWithIdDied` and `DiedViaError` to `OperationWithIdFailed`. `OperationWithIdFailed` marks the operation as failed and stops the log monitor of the operation. + +## Log monitor + +`MonitorProcessAndLogsAsync` reads only the logs that GoodCheck made after the start of the current check (2 seconds earlier at most). It selects the newest file, reads it from the start and reads it again every 500 ms. + +For each line, the service does these checks: + +| Text in the line | Result | +|---|---| +| `Launching '', strategy N/M: []` | Raises `ProgressChanged` | +| `This strategy has no successes` | Increases the incorrect count | +| `worst result for this strategy: S/A` followed by `Terminating program...` | Raises `StrategyResultAdded`. If `S * 100 / A >= 65`, the correct count increases. Otherwise, if `S` is not 0, the incorrect count increases. | +| `Exiting with an error...` | The check fails with `ERR_GOODCHECK_EXCEPTION` | +| `All Done` | The check is successful | + +When the tray reports `Died`, the service reads the rest of the log. If the rest does not contain `All Done` or the error text, the check fails with `ERR_GOODCHECK_EXITED_EARLY`. + +The service keeps the full output of each check in `GoodCheckOperationModel`. It also raises `OperationOutputAdded` for each line. + +## Report + +`CreateAndSaveReport` writes the XML report: + +```xml + + + + false + ... + S + A + + + +``` + +The service makes one `Group` for each operation. It writes the report also after a cancel. `GoodCheckResultViewHelper.LoadGroupsFromFile` reads the report. A group without `ComponentId` makes the file invalid. + +The user can open reports in these places: + +- `GoodCheckWorkPage`, after the run; +- `RecentGoodCheckSelectionsContentDialog`, which lists the `Reports` folder; +- the "open report" button of `CreateConfigHelperWindow`, with a file dialog. + +`ViewGoodCheckReportPage` and `ViewGoodCheckSiteListReportPage` (`CDPIUI/Views/CreateConfigHelper`) show the report. + +## Cancel + +| Action | Method | Result | +|---|---|---| +| Stop all checks | `Stop` | Cancels the run token and sends `Stop` to the tray | +| Stop the current check | `RemoveFromQueueOrStopOperation(CurrentOperationId)` | Sets the check to `UserInterrupt` and cancels its token | +| End the service state after the report | `Kill` | Cancels and disposes the run token | + +When the run ends, the service sets each check that is still `WorkInProgress` to `UserInterrupt`. If the user closes `CreateConfigUtilWindow` during a run, the window asks to stop the run and calls `Stop`. + +## Errors + +| Code | Source | Condition | +|---|---|---| +| `ERR_ADDON_NOT_INSTALLED` | Service | GoodCheck is not installed, or its executable is missing | +| `ERR_EXE_NOT_FOUND` | Service | The executable is missing when a check starts | +| `ERR_GOODCHECK_EXCEPTION` | Service | The log contains `Exiting with an error...` | +| `ERR_GOODCHECK_EXITED_EARLY` | Service | The process stopped before `All Done` | +| `ERR_MONITOR_EXCEPTION` | Service | The log monitor failed | +| `ERR_INTERNAL` | Service | An unhandled exception in the run | +| `ERR_UNABLE_START_PROCESS` | Tray | The path is not trusted, or the process did not start | +| `ERR_WIN32_EXCEPTION` | Tray | The job object or the process start failed | + +## Settings + +The page `CreateViaGoodCheck` (`CDPIUI/Views/CreateConfigUtil/CreateViaGoodCheckPage.xaml.cs`) reads and writes the settings. + +CDPI UI settings, group `ADDONS/ASGKOI001`: + +| Key | Type | Function | +|---|---|---| +| `passesValue` | `string` | The number of passes (`-p`). A value that is not an integer of 1 or more becomes 1. | +| `useCurl` | `bool` | `true` gives `-m curl`, `false` gives `-m native` | + +For ByeDPI, the page offers only the curl resolver. + +GoodCheck `config.ini` (UTF-8): + +| Section | Keys | +|---|---| +| `General` | `ConnectionTimeout`, `AutomaticGoogleCacheTest` | +| `Advanced` | `ResolverNativeTimeout`, `ResolverNativeRetries`, `InternalTimeoutMs`, `AutomaticConnectivityTest`, `ConnectivityTestURL`, `SkipCertVerify`, `GoogleCacheMappingURLs` | +| `Resolvers` | `UseDoH`, `DoHResolvers` | +| `Fakes` | `FakeSNI`, `FakeHexStreamTCP`, `FakeHexStreamUDP`, `FakeHexBytesTCP`, `FakeHexBytesUDP`, `PayloadTCP`, `PayloadUDP` | +| `Zapret`, `GoodbyeDPI`, `ByeDPI` | The folder and the executable name of each installed component. The folder comes from the Store database. | + +If `config.ini` is missing, the page tells the user to install the add-on again. + +## Site lists + +`SiteListHelper.GetAllAvailableSiteListTemplatesAsync` finds the TXT files in the folders of all `configlist` Store items. If one kit has several files with the same name and different contents, all of them stay in the list. Files with the same name and the same SHA-256 appear once. + +`DomainValidationHelper` checks a list. In `Quick` mode, it reads the first 10 lines that are not empty and not comments. A list is not valid if a line contains whitespace or `/`, or if a line is an IP address. A file that the user selects must pass the `Quick` check. The page then copies it to `Store/Items/LocalUserStorage/List` with a unique name. + +## GUI entry points + +| Entry point | Location | +|---|---| +| Auto-selection window, component selection | `CDPIUI/Views/CreateConfigUtil/MainPage.xaml.cs` (`BeginNewSelectionButton_Click`) | +| Route | `cdpiui://Tools/AutoConfig/{componentId}` | +| Setup pages | `CDPIUI/Views/CreateConfigUtil/CreateViaGoodCheckPage.xaml.cs` (class `CreateViaGoodCheck`) | +| Progress page | `CDPIUI/Views/CreateConfigUtil/GoodCheckWorkPage.xaml.cs` | +| Site list controls | `CDPIUI/Controls/GoodCheck` | +| Report pages | `CDPIUI/Views/CreateConfigHelper/ViewGoodCheckReportPage.xaml.cs`, `ViewGoodCheckSiteListReportPage.xaml.cs` | +| Recent reports | `CDPIUI/Controls/Dialogs/CreateConfigHelper/RecentGoodCheckSelectionsContentDialog.xaml.cs` | + +## Limits + +- Only one GoodCheck run can operate, because `GoodCheckProcessService` is a singleton and the tray has one process slot. +- The tray stops all components before GoodCheck starts. +- The service passes only file names with `-c` and `-s`. GoodCheck must find the files from its own folder. For this reason, the page shows only the strategy lists in the top level of `StrategiesGoGo/`, not in its subfolders. +- In `AllAsOne` mode, only the strategy list of the first site list is used. +- The correct and incorrect counters use a fixed threshold of 65%. +- The add-on has no tests in `CDPIUI.Tests`. diff --git a/CDPIUI.AddOns/HostsFileEditor/HostsFileEditorService.cs b/CDPIUI.AddOns/HostsFileEditor/HostsFileEditorService.cs index 69e8394a..3c46d6d6 100644 --- a/CDPIUI.AddOns/HostsFileEditor/HostsFileEditorService.cs +++ b/CDPIUI.AddOns/HostsFileEditor/HostsFileEditorService.cs @@ -11,7 +11,7 @@ private enum Flags remove, recover, } - private static Process RunEditorWithFlag(Flags flag) + private static Process? RunEditorWithFlag(Flags flag) { string startupString = $"/{flag}"; string path = Path.Combine(Core.Data.Directories.CurrentDirectory, @@ -28,27 +28,19 @@ private static Process RunEditorWithFlag(Flags flag) return process; } - public static async Task AddDomains() - { - Process process = RunEditorWithFlag(Flags.add); - await process.WaitForExitAsync(); - - return process.ExitCode; - } + public static Task AddDomains() => RunAndWait(Flags.add); - public static async Task RemoveDomains() - { - Process process = RunEditorWithFlag(Flags.remove); - await process.WaitForExitAsync(); + public static Task RemoveDomains() => RunAndWait(Flags.remove); - return process.ExitCode; - } + public static Task RestoreDomains() => RunAndWait(Flags.recover); - public static async Task RestoreDomains() + // Process.Start can return null; the callers then failed with NullReferenceException. + private static async Task RunAndWait(Flags flag) { - Process process = RunEditorWithFlag(Flags.recover); + using Process? process = RunEditorWithFlag(flag); + if (process == null) + return -1; await process.WaitForExitAsync(); - return process.ExitCode; } } diff --git a/CDPIUI.AddOns/HostsFileEditor/README.md b/CDPIUI.AddOns/HostsFileEditor/README.md new file mode 100644 index 00000000..ab6bcf29 --- /dev/null +++ b/CDPIUI.AddOns/HostsFileEditor/README.md @@ -0,0 +1,77 @@ +# HostsFileEditor add-on + +## Purpose + +The HostsFileEditor add-on adds the CDPI UI domain block to the Windows hosts file and removes it. The GUI operates without administrator rights. Thus the add-on does not change the hosts file itself. It starts the separate tool `EditHostFile.exe` with administrator rights and waits for its exit code. + +`EditHostFile.exe` contains all the logic of the hosts file changes: the managed block, the backup, the encoding and the safe write. Refer to [EditHostFile/README.md](../../EditHostFile/README.md). + +## Main type + +`HostsFileEditService` (`HostsFileEditorService.cs`, namespace `CDPIUI.AddOns.HostsFileEditor`) is a static class. + +| Method | Tool argument | Function | +|---|---|---| +| `AddDomains()` | `/add` | Adds the CDPI UI block and writes `hosts.bak` | +| `RemoveDomains()` | `/remove` | Removes the CDPI UI block | +| `RestoreDomains()` | `/recover` | Removes the CDPI UI block and deletes `hosts.bak` | + +Each method returns `Task`: the exit code of the tool. + +## Sequence + +1. The method finds `EditHostFile.exe` in the application folder (`Directories.CurrentDirectory`). +2. If the file does not exist, the method throws `ApplicationFilesDamagedException`. +3. The method starts the tool with `UseShellExecute = true` and the verb `runas`. Windows shows the UAC prompt. +4. The method waits for the process to exit. +5. The method returns the exit code. If `Process.Start` returns `null`, the method returns `-1`. + +If the user cancels the UAC prompt, `Process.Start` throws an exception. The method does not catch it. The caller must process it. + +The exit codes of the tool are: + +| Code | Meaning | +|---|---| +| `0` | Success | +| `0x80070057` (`E_INVALIDARG`) | No known argument | +| Other | The `HResult` of the exception that stopped the tool, or `1` | + +## GUI + +| Item | Location | Function | +|---|---|---| +| `EditHostFileWindow` | `CDPIUI/Windows/EditHostFileWindow.xaml(.cs)` | The window of the tool | +| `Views.EditHostsFile.MainPage` | `CDPIUI/Views/EditHostsFile/MainPage.xaml(.cs)` | Buttons for add, remove, recover and open | +| `HostsFileEditHelper` | `CDPIUI/Helper/AddOns/HostsFileEditHelper.cs` | Opens the hosts file in a text editor | + +These items open the window: + +- the hosts file card on the tools page (`CDPIUI/Views/UtilsPage.xaml.cs`); +- the route `cdpiui://Tools/Hosts` (`CDPIUI/Commands/CommandUriMapper.cs`). + +The page converts the exit code or the exception to an error text with `ErrorsHelper.Convertor.GetPrettyErrorCode("HOSTEDIT", ...)`. The result `HOSTEDIT_SUCCESS (0x00000000)` is a success. The page does not show an error for a result that starts with `HOSTEDIT_OPERATION_CANCELLED_BY_USER`. For other results, the page shows an error dialog. + +The "Open" card calls `HostsFileEditHelper.EditHostsFile`. The helper opens `%WINDIR%\System32\drivers\etc\hosts` with a UAC request. Notepad is the default application. The settings group `FILEOPENACTIONS` controls the application selection. If `isDialogShown` is `false`, or if `doNotRemindAgain` is `false`, `EditSitelistAskApplicationContentDialog` asks which application to use first. + +The help button opens `cdpiui://Help/Utils/EditHostsFileUtility/`. + +## Settings + +The page keeps the state in the CDPI UI settings, group `HOSTS`: + +| Key | Type | Set to `true` | Set to `false` | +|---|---|---|---| +| `isFileEdited` | `bool` | After a successful `/add` | After a successful `/remove` or `/recover` | +| `isBackupAvailable` | `bool` | After a successful `/add` | After a successful `/recover` | + +The page uses these values to enable the cards: + +- "Add" is enabled when `isFileEdited` is `false`. +- "Remove" is enabled when `isFileEdited` is `true`. +- "Recover" is enabled when `isBackupAvailable` is `true`. + +## Limits + +- The page does not read the hosts file to find its state. It uses only the `HOSTS` settings. A change of the hosts file outside CDPI UI does not change these settings. For example, the MSI uninstall cleanup runs `EditHostFile.exe /remove` but does not change the settings. +- Each operation shows a UAC prompt. +- The add-on has no tests in `CDPIUI.Tests`. diff --git a/CDPIUI.AddOns/README.md b/CDPIUI.AddOns/README.md new file mode 100644 index 00000000..d4f721a6 --- /dev/null +++ b/CDPIUI.AddOns/README.md @@ -0,0 +1,86 @@ +# CDPIUI.AddOns + +`CDPIUI.AddOns` is the library of the internal add-ons of CDPI UI. Each add-on has one folder. The GUI project `CDPIUI` contains the pages, the dialogs and the translations of the add-ons. + +## Project + +| Item | Value | +|---|---| +| Project file | `CDPIUI.AddOns/CDPIUI.AddOns.csproj` | +| Target framework | `net8.0` (not a Windows target, thus no WinUI types) | +| Project references | `CDPIUI.Core`, `CDPIUI.Shared` | +| Package references | `System.ServiceProcess.ServiceController` | +| Embedded resources | `BlockCheck2/Catalog/strategy-catalog.v1.json` | +| Internal types visible to | `CDPIUI.Tests` (`InternalsVisibleTo`) | +| Projects that refer to it | `CDPIUI`, `CDPIUI.Tests` | + +The namespace of an add-on is `CDPIUI.AddOns.`, for example `CDPIUI.AddOns.ConfigImport`. + +## Add-ons + +### BlockCheck2 + +BlockCheck2 finds Zapret2 strategies for a set of sites with `curl` probes. It combines the successful strategies into one multi-profile preset, checks the complete preset, and repairs it if necessary. It also keeps a history of reports and lets the user build and save a preset manually. The auto-selection window (`cdpiui://Tools/AutoConfig/Zapret2`) opens it for Zapret2. Refer to [BlockCheck2/README.md](BlockCheck2/README.md). + +### ConfigImport + +ConfigImport converts `.bat`, `.cmd`, `.txt` and `.json` files into a CDPI UI config with a static analysis. It never runs the source script. It has a special analyzer for `winws` and `winws2`, finds the missing resources, lets the user test the result and copies the resources into `LocalUserStorage`. It sends `.cdpiconfig` packages to ConfigShare. The import window (`cdpiui://Tools/ImportConfig`) uses it. Refer to [ConfigImport/README.md](ConfigImport/README.md). + +### ConfigShare + +ConfigShare exports one preset with its resources into a `.cdpiconfig` ZIP archive. It checks, extracts and installs such archives. It limits the archive size, the entry count and the compression ratio, and it rejects options that write files outside the package. The export dialog of `ConfigSelector` and the file activation use it. Refer to [ConfigShare/README.md](ConfigShare/README.md). + +### GoodCheck + +GoodCheck is the CDPI UI side of the external GoodCheck add-on from the Store. It prepares the site lists, sends a start request to the tray, reads the GoodCheck logs and writes an XML report. It supports only ByeDPI (`ciadpi`), GoodbyeDPI (`gdpi`) and Zapret Legacy (`zapret`), in the modes `AllAsOne` and `AnyAsAny`. The auto-selection window opens it for these components. Refer to [GoodCheck/README.md](GoodCheck/README.md). + +### HostsFileEditor + +HostsFileEditor starts `EditHostFile.exe` with administrator rights to add, remove or recover the CDPI UI block in the Windows hosts file. It returns the exit code of the tool. The hosts file window (`cdpiui://Tools/Hosts`) uses it. Refer to [HostsFileEditor/README.md](HostsFileEditor/README.md). + +### Troubleshooting + +Troubleshooting checks Windows services, network settings and programs that can stop a DPI bypass. It also checks the access to the Store repositories and the Store folders. It can fix the TCP timestamps, the WinDivert service and the Store folder access. The troubleshooting window (`cdpiui://Tools/Troubleshooting`) uses it. Refer to [Troubleshooting/README.md](Troubleshooting/README.md). + +## Add a new internal add-on + +### Logic in `CDPIUI.AddOns` + +1. Make a folder `CDPIUI.AddOns/`. +2. Use the namespace `CDPIUI.AddOns.` and its sub-namespaces. +3. Put only logic without UI into the folder. The project targets `net8.0` and cannot use WinUI or XAML types. +4. Use the public types of `CDPIUI.Core` and `CDPIUI.Shared` directly. Do not add a reference to the `CDPIUI` project. +5. Add an `EmbeddedResource` item to `CDPIUI.AddOns.csproj` for each file that the add-on embeds. The SDK includes `.cs` files automatically. +6. Write a `README.md` in the folder. Add a paragraph and a link to this file. + +Use these rules: + +- Give the UI an entry point that accepts its dependencies as parameters, for example `BlockCheckAddOnService`. +- Put the code that starts processes behind an interface, for example `IBlockCheckStrategyRunner`. Then tests can use a fake implementation. +- Return stable issue or error codes. Do not return translated texts. +- Start components and external add-on executables through the tray, for example with `ComponentTasksManager` or a pipe message. The tray starts only files that pass its trusted path checks. The GUI process can start system tools itself, for example `curl.exe` in BlockCheck2. + +WARNING: The tray operates as administrator. Do not send a path or arguments from an untrusted file to the tray without a check. For example, ConfigShare and BlockCheck2 reject options that write files. + +### GUI pieces in `CDPIUI` + +| Piece | Folder | +|---|---| +| Window | `CDPIUI/Windows` | +| Pages of the window | `CDPIUI/Views/` | +| User controls | `CDPIUI/Controls/` | +| Content dialogs | `CDPIUI/Controls/Dialogs/` | +| View models | `CDPIUI/ViewModels` | +| GUI helpers that use translations or XAML types | `CDPIUI/Helper/AddOns/` | +| UI strings | `CDPIUI/Strings/en-us/Resources.resw` and `CDPIUI/Strings/ru/Resources.resw` | +| Help articles | `CDPIUI/Help/EN` and `CDPIUI/Help/RU` | +| `cdpiui://` routes | `CDPIUI/Commands/CommandUriMapper.cs` | +| Entry card on the tools page | `CDPIUI/Views/UtilsPage.xaml(.cs)` | + +### Tests + +Add unit tests to `CDPIUI.Tests/AddOns`. `CDPIUI.Tests` already refers to `CDPIUI.AddOns` and can see its internal types. + +### External add-ons + +An external add-on, for example GoodCheck, is an executable from the Store. Its ID goes to `HardcodedItemIds.AddOnsIds` in `CDPIUI.Core/Store/Data/HardcodedItemIds.cs`. The code in `CDPIUI.AddOns` sets it, starts it through the tray and reads its results. diff --git a/CDPIUI.AddOns/Troubleshooting/README.md b/CDPIUI.AddOns/Troubleshooting/README.md new file mode 100644 index 00000000..9f9b9994 --- /dev/null +++ b/CDPIUI.AddOns/Troubleshooting/README.md @@ -0,0 +1,105 @@ +# Troubleshooting add-on + +## Purpose + +The Troubleshooting add-on finds common causes of problems on the computer and in the Store. It can fix some of them. `TroubleshootingService` (`TroubleshootingService.cs`) contains all the logic. The add-on has two checks: + +- the basic check: Windows services, network settings and programs that can stop a DPI bypass; +- the Store check: the access to the Store repositories and the write access to the Store folders. + +## Main types + +| Type | Function | +|---|---| +| `TroubleshootingService` | Singleton (`Instance`). Runs the checks and the fixes. | +| `RunBasicDialogStates` | The steps of the basic check | +| `StoreCheckStates` | The steps of the Store check | +| `DiagnosticStateModel` | The correct value of a step and the keys of its display texts | +| `FixResultModel` | The result of a fix: `IsFixed` and `ErrorCode` | +| `BasicDiagnosticStateModelPairs`, `StoreDiagnosticStateModelPairs` | Static maps from a step to its `DiagnosticStateModel` | + +The service raises `CurrentStateChanged` with the current step before each check. + +## Basic check + +`RunBasicDiagnostic` first stops all component tasks. Then it sends the `StopService` request to the tray (`ProcessService.StopService`) and does not wait for the result. The tray stops the GoodbyeDPI, Zapret and Zapret2 tasks and runs `sc stop WinDivert` and `sc delete WinDivert`. The service then waits 1 second and does the checks below. It returns a map from each step to a `bool` value. + +| Step | Check | Correct value | +|---|---|---| +| `CheckBFE` | The service `BFE` operates | `true` | +| `CheckProxy` | The Windows proxy is enabled (`WindowsNativeProxySettingsService.IsProxyEnabled`) | `false` | +| `CheckNetsh` | `netsh.exe` is in the current folder or in `PATH` | `true` | +| `CheckTimestamps` | PowerShell `(Get-NetTCPSetting -SettingName Internet).Timestamps` returns `Enabled` | `true` | +| `CheckAdGuard` | A process `adguardvpnsvc`, `adguardvpn` or `adguardsvc` operates | `false` | +| `CheckKiller` | A service with `Killer` in its name operates | `false` | +| `CheckIntelConnectivityNetwork` | A service with `Intel` and `Connectivity Network` in its name or display name operates | `false` | +| `CheckCheckPointServices` | A service with `TracSrvWrapper` or `EPWD` in its name operates | `false` | +| `CheckSmartByte` | A service with `SmartByte` in its name operates | `false` | +| `CheckVPNs` | A service with `vpn` in its name operates | `false` | +| `CheckDNS` | At least one network interface has DoH flags (`DohFlags > 0` under `HKLM:System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters`) | `true` | +| `CheckWinDivert` | A service with `windivert` in its name operates | `false` | +| `CheckAnotherComponentsServices` | A service with `GoodbyeDPI`, `discordfix_zapret`, `winws1` or `winws2` in its name exists | `false` | +| `CheckAnotherComponents` | A process `winws`, `winws2` or `goodbyedpi` operates | `false` | + +The service name checks ignore the case. A service is "operating" if its state is `Running`, `StartPending` or `StopPending`. The service reads the service list one time for each check run. + +The PowerShell calls use `powershell.exe -NoProfile -NonInteractive -Command`. The service stops a tool after 30 seconds. + +## Store check + +`RunStoreDiagnostic` also stops all component tasks and sends the `StopService` request first. It then does these checks: + +| Step | Check | Correct value | +|---|---|---| +| `CheckGitHubRepo` | `StoreHelper.TryLoadDatabaseForVersionControl(GitHub)` | `true` | +| `CheckGitLabRepo` | `StoreHelper.TryLoadDatabaseForVersionControl(GitLab)` | `true` | +| `CheckWriteAccess` | Write access to `Directories.StoreDirectory` | `true` | +| `CheckWriteAccessIntoItemsFolder` | Write access to `Directories.StoreItemsDirectory` | `true` | +| `CheckWriteAccessIntoRepoFolder` | Write access to `Directories.StoreRepoCacheDirectory` | `true` | +| `CheckWriteAccessIntoDatabaseFolder` | Write access to `Directories.StoreLocalCacheDirectory` | `true` | + +The write check makes a temporary file with a random name and the `DeleteOnClose` option. + +## Fixes + +`FixAllBasicErrors` and `FixAllStoreErrors` try to fix each step whose value is different from the correct value. All other steps return `IsFixed = false` and `INFO_CANNOT_FIX`. + +| Step | Fix | +|---|---| +| `CheckTimestamps` | Runs `netsh interface tcp set global timestamps=enabled`. The fix is successful if the exit code is 0. | +| `CheckWinDivert` | Sends the `StopService` request through `ProcessService.StopService`. It then checks every 500 ms, a maximum of 20 times, until the service does not operate. | +| `CheckWriteAccess`, `CheckWriteAccessIntoItemsFolder`, `CheckWriteAccessIntoRepoFolder`, `CheckWriteAccessIntoDatabaseFolder` | Sends a `GrantAccessRequest` pipe message for the folder to the tray (`PipeHelper.SendGrantAccessPacket`) | + +A failed fix returns `ERR_WIN32_UNKNOWN`. + +The tray changes the access only for paths in the Store (`TrustedPaths.IsInStore`). It sends the answer `GrantAccessResponse`. `CDPIUI/Commands/CommandsHandler.cs` gives the answer to `TroubleshootingService.OnGrantAccessCompleted`. If no answer comes in 30 seconds, the fix returns `false`. + +## GUI entry points + +| Entry point | Location | +|---|---| +| Window | `CDPIUI/Windows/TroubleshootingWindow.xaml(.cs)` | +| Start page with the two checks | `CDPIUI/Views/Troubleshooting/MainPage.xaml.cs` | +| Progress, results and fixes | `CDPIUI/Views/Troubleshooting/WorkPage.xaml.cs` | +| Tools page | `CDPIUI/Views/UtilsPage.xaml.cs` | +| Component error window | `CDPIUI/Windows/Messages/ComponentErrorWindow.xaml` (link `cdpiui://Tools/Troubleshooting`) | +| Store download error dialog | `CDPIUI/Controls/Dialogs/Store/DownloadErrorActionsContentDialog.xaml.cs` | + +The routes in `CDPIUI/Commands/CommandUriMapper.cs` are: + +```text +cdpiui://Tools/Troubleshooting +cdpiui://Tools/Troubleshooting/BasicCheck +cdpiui://Tools/Troubleshooting/StoreCheck +``` + +`BasicCheck` opens `WorkPage` with the action `BeginBasicCheck`. `StoreCheck` opens it with `BeginStoreRepoCheck`. `WorkPage` shows each result with the display text keys from `DiagnosticStateModel`. + +## Limits + +- The two checks stop all components. The components do not start again automatically after the check. +- Most name checks use a substring of the service name. For example, `CheckVPNs` finds any operating service with `vpn` in its name. +- `CheckAnotherComponentsServices` checks that a service exists, not that it operates. +- Only the timestamps, WinDivert and Store folder access steps have a fix. +- On an operating system other than Windows, the service checks return `true`. +- The add-on has no settings and no tests in `CDPIUI.Tests`. diff --git a/CDPIUI.AddOns/Troubleshooting/TroubleshootingService.cs b/CDPIUI.AddOns/Troubleshooting/TroubleshootingService.cs index 9bdd0575..9e88ffcf 100644 --- a/CDPIUI.AddOns/Troubleshooting/TroubleshootingService.cs +++ b/CDPIUI.AddOns/Troubleshooting/TroubleshootingService.cs @@ -150,20 +150,20 @@ public enum RunBasicDialogStates public static Dictionary BasicDiagnosticStateModelPairs = new() { - { RunBasicDialogStates.CheckBFE, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "ServiceRunned", InCorrectValueDisplayText="ServiceStopped" } }, + { RunBasicDialogStates.CheckBFE, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "ServiceRunning", InCorrectValueDisplayText="ServiceStopped" } }, { RunBasicDialogStates.CheckProxy, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "Disabled", InCorrectValueDisplayText="Enabled" } }, { RunBasicDialogStates.CheckNetsh, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "Exist", InCorrectValueDisplayText="NotExist" } }, { RunBasicDialogStates.CheckTimestamps, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "Exist", InCorrectValueDisplayText="NotExist" } }, - { RunBasicDialogStates.CheckAdGuard, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckKiller, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckIntelConnectivityNetwork, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckCheckPointServices, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckSmartByte, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckVPNs, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckDNS, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "ServiceRunned", InCorrectValueDisplayText="ServiceStopped" } }, - { RunBasicDialogStates.CheckWinDivert, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckAnotherComponents, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, - { RunBasicDialogStates.CheckAnotherComponentsServices, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunned" } }, + { RunBasicDialogStates.CheckAdGuard, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckKiller, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckIntelConnectivityNetwork, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckCheckPointServices, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckSmartByte, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckVPNs, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckDNS, new DiagnosticStateModel() { CorrectState = true, CorrectValueDisplayText = "ServiceRunning", InCorrectValueDisplayText="ServiceStopped" } }, + { RunBasicDialogStates.CheckWinDivert, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckAnotherComponents, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, + { RunBasicDialogStates.CheckAnotherComponentsServices, new DiagnosticStateModel() { CorrectState = false, CorrectValueDisplayText = "ServiceStopped", InCorrectValueDisplayText="ServiceRunning" } }, }; public async Task> RunBasicDiagnostic() @@ -186,7 +186,7 @@ public async Task> RunBasicDiagnostic() bool isNetshExistInPATH = ExistsOnPath("netsh.exe"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckTimestamps); - bool isTimestampsChecked = CheckTimestamps(); + bool isTimestampsChecked = await CheckTimestamps(); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckAdGuard); bool isAdGuardRunning = IsProcessRunning("adguardvpnsvc") || IsProcessRunning("adguardvpn") || IsProcessRunning("adguardsvc"); @@ -196,7 +196,10 @@ public async Task> RunBasicDiagnostic() bool isRunningKiller = IsServiceRunning("Killer"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckIntelConnectivityNetwork); - bool isRunningIntelConnectivityNetwork = IsServiceRunning("Intel") || IsServiceRunning("Connectivity") || IsServiceRunning("Network"); + // The Intel(R) Connectivity Network Service only: "Network" alone matched many + // Windows services, so the check failed on almost every computer. + bool isRunningIntelConnectivityNetwork = IsServiceRunning(service => + Matches(service, "Intel") && Matches(service, "Connectivity Network")); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckCheckPointServices); bool isCheckPointServiceRunning = IsServiceRunning("TracSrvWrapper") || IsServiceRunning("EPWD"); @@ -206,18 +209,19 @@ public async Task> RunBasicDiagnostic() bool isSmartByteRunning = IsServiceRunning("SmartByte"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckVPNs); - bool isRunningVPN = IsServiceExist("vpn"); + // Running, not merely installed: the dialog reports it as running. + bool isRunningVPN = IsServiceRunning("vpn"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckDNS); - bool isUsedDNS = GetDnsAdress(); + bool isUsedDNS = await GetDnsAddress(); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckWinDivert); bool isWinDivertRunning = IsServiceRunning("windivert"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckAnotherComponentsServices); - bool isAnotherServiceComponentRunned = IsServiceExist("GoodbyeDPI") || IsServiceExist("discordfix_zapret") || IsServiceExist("winws1") || IsServiceExist("winws2"); + bool isAnotherServiceComponentRunning = IsServiceExist("GoodbyeDPI") || IsServiceExist("discordfix_zapret") || IsServiceExist("winws1") || IsServiceExist("winws2"); CurrentStateChanged?.Invoke(RunBasicDialogStates.CheckAnotherComponents); - bool isAnotherComponentRunned = IsProcessRunning("winws") || IsProcessRunning("goodbyedpi"); + bool isAnotherComponentRunning = IsProcessRunning("winws") || IsProcessRunning("winws2") || IsProcessRunning("goodbyedpi"); await Task.Delay(1000); completedStates = new() @@ -234,8 +238,8 @@ public async Task> RunBasicDiagnostic() { RunBasicDialogStates.CheckVPNs, isRunningVPN }, { RunBasicDialogStates.CheckDNS, isUsedDNS }, { RunBasicDialogStates.CheckWinDivert, isWinDivertRunning }, - { RunBasicDialogStates.CheckAnotherComponents, isAnotherServiceComponentRunned }, - { RunBasicDialogStates.CheckAnotherComponentsServices, isAnotherServiceComponentRunned }, + { RunBasicDialogStates.CheckAnotherComponents, isAnotherComponentRunning }, + { RunBasicDialogStates.CheckAnotherComponentsServices, isAnotherServiceComponentRunning }, }; await Task.CompletedTask; @@ -272,12 +276,21 @@ public async Task TryToFixBasicError(RunBasicDialogStates state) switch (state) { case RunBasicDialogStates.CheckTimestamps: - fixResultModel.IsFixed = EnableTimestamps(); + fixResultModel.IsFixed = await EnableTimestamps(); fixResultModel.ErrorCode = fixResultModel.IsFixed ? string.Empty : "ERR_WIN32_UNKNOWN"; break; case RunBasicDialogStates.CheckWinDivert: - _ = ProcessService.StopService(); - fixResultModel.IsFixed = true; + // The tray stops the service on its own time: success is reported only when + // the service is really gone. + await ProcessService.StopService(); + fixResultModel.IsFixed = false; + for (int i = 0; i < 20 && !fixResultModel.IsFixed; i++) + { + await Task.Delay(500); + Services = null; + fixResultModel.IsFixed = !IsServiceRunning("windivert"); + } + fixResultModel.ErrorCode = fixResultModel.IsFixed ? string.Empty : "ERR_WIN32_UNKNOWN"; break; default: fixResultModel.IsFixed = false; @@ -342,16 +355,20 @@ private bool IsServiceExist(string name) } } - private bool IsServiceRunning(string name) + private bool IsServiceRunning(string name) => + IsServiceRunning(service => service.ServiceName.Contains(name, StringComparison.OrdinalIgnoreCase)); + + // Any matching service that runs: the first match alone could be a stopped one. + private bool IsServiceRunning(Func match) { try { if (!OperatingSystem.IsWindows()) return true; Services ??= ServiceController.GetServices(); #pragma warning disable CA1416 // Validate platform compatibility - ServiceControllerStatus serviceControllerStatus = Services.FirstOrDefault(s => s.ServiceName.Contains(name, StringComparison.OrdinalIgnoreCase))?.Status ?? ServiceControllerStatus.Stopped; + return Services.Where(match).Any(service => + service.Status is ServiceControllerStatus.Running or ServiceControllerStatus.StopPending or ServiceControllerStatus.StartPending); #pragma warning restore CA1416 // Validate platform compatibility - return serviceControllerStatus == ServiceControllerStatus.Running || serviceControllerStatus == ServiceControllerStatus.StopPending || serviceControllerStatus == ServiceControllerStatus.StartPending; } catch { @@ -359,6 +376,12 @@ private bool IsServiceRunning(string name) } } +#pragma warning disable CA1416 // Validate platform compatibility + private static bool Matches(ServiceController service, string text) => + service.ServiceName.Contains(text, StringComparison.OrdinalIgnoreCase) || + service.DisplayName.Contains(text, StringComparison.OrdinalIgnoreCase); +#pragma warning restore CA1416 // Validate platform compatibility + private static bool ExistsOnPath(string fileName) { try @@ -386,17 +409,48 @@ private static bool ExistsOnPath(string fileName) return null; } - private bool EnableTimestamps() + private static readonly string PowerShellPath = + Path.Combine(Environment.SystemDirectory, "WindowsPowerShell", "v1.0", "powershell.exe"); + + /// + /// Runs a system tool without a console window and without blocking the calling thread + /// + private static async Task<(int ExitCode, string Output)> RunToolAsync(string fileName, params string[] arguments) + { + var startInfo = new ProcessStartInfo(fileName) + { + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + foreach (string argument in arguments) + startInfo.ArgumentList.Add(argument); + + using var process = Process.Start(startInfo) ?? throw new InvalidOperationException($"Cannot start {fileName}"); + Task output = process.StandardOutput.ReadToEndAsync(); + Task errors = process.StandardError.ReadToEndAsync(); + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + try + { + await process.WaitForExitAsync(timeout.Token); + } + catch (OperationCanceledException) + { + try { process.Kill(); } catch { } + throw new System.TimeoutException($"{fileName} did not finish in time"); + } + await errors; + return (process.ExitCode, await output); + } + + private async Task EnableTimestamps() { try { - Process p = new Process(); - ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface tcp set global timestamps=enabled"); - p.StartInfo = psi; - p.Start(); - p.WaitForExit(); - // Debug.WriteLine(p.StandardOutput); - return p.ExitCode == 0; + var result = await RunToolAsync(Path.Combine(Environment.SystemDirectory, "netsh.exe"), + "interface", "tcp", "set", "global", "timestamps=enabled"); + return result.ExitCode == 0; } catch (Exception ex) { @@ -405,17 +459,16 @@ private bool EnableTimestamps() } } - private bool CheckTimestamps() + private async Task CheckTimestamps() { try { - Process p = new Process(); - ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface tcp show global | findstr /i \"timestamps\" | findstr /i \"enabled\""); - p.StartInfo = psi; - p.Start(); - p.WaitForExit(); - // Debug.WriteLine(p.StandardOutput); - return p.ExitCode == 0; + // netsh prints localized text and cannot pipe into findstr without a shell; + // Get-NetTCPSetting returns the state as Enabled or Disabled in any language. + var result = await RunToolAsync(PowerShellPath, "-NoProfile", "-NonInteractive", "-Command", + "(Get-NetTCPSetting -SettingName Internet).Timestamps"); + return result.ExitCode == 0 && + string.Equals(result.Output.Trim(), "Enabled", StringComparison.OrdinalIgnoreCase); } catch (Exception ex) { @@ -436,18 +489,14 @@ private static bool IsProcessRunning(string procName) return false; } - private static bool GetDnsAdress() + private static async Task GetDnsAddress() { try { - Process p = new Process(); - ProcessStartInfo psi = new ProcessStartInfo("powershell", "Get-ChildItem -Recurse -Path 'HKLM:System\\CurrentControlSet\\Services\\Dnscache\\InterfaceSpecificParameters\\' | Get-ItemProperty | Where-Object { $_.DohFlags -gt 0 } | Measure-Object | Select-Object -ExpandProperty Count"); - p.StartInfo = psi; - p.StartInfo.RedirectStandardOutput = true; - p.Start(); - p.WaitForExit(); - - return int.TryParse(p.StandardOutput.ReadToEnd(), out int result) && result != 0; + var result = await RunToolAsync(PowerShellPath, "-NoProfile", "-NonInteractive", "-Command", + "Get-ChildItem -Recurse -Path 'HKLM:System\\CurrentControlSet\\Services\\Dnscache\\InterfaceSpecificParameters\\' | Get-ItemProperty | Where-Object { $_.DohFlags -gt 0 } | Measure-Object | Select-Object -ExpandProperty Count"); + + return int.TryParse(result.Output.Trim(), out int count) && count != 0; } catch (Exception ex) { @@ -461,23 +510,28 @@ private static bool GetDnsAdress() #region StoreFixServices private TaskCompletionSource? _tcs; private bool opResult; + // The answer of the tray can be lost; without a time limit the fix waited forever. + private static readonly TimeSpan GrantAccessTimeout = TimeSpan.FromSeconds(30); + private async Task GrantAccess(string file, CancellationToken ct = default) { - _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _tcs = tcs; using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); - using (cts.Token.Register(() => _tcs.TrySetCanceled())) + cts.CancelAfter(GrantAccessTimeout); + using (cts.Token.Register(() => tcs.TrySetResult(false))) { await PipeHelper.SendGrantAccessPacket(file); - await _tcs.Task.ConfigureAwait(false); - return _tcs.Task.Result; + return await tcs.Task.ConfigureAwait(false); } } public void OnGrantAccessCompleted(bool state) { opResult = state; - _tcs.TrySetResult(state); + // An answer without a request is ignored. + _tcs?.TrySetResult(state); } private static bool IsDirectoryWritable(string dirPath, bool throwIfFails = false) diff --git a/CDPIUI.Core/Application.cs b/CDPIUI.Core/Application.cs index 7e5e0f74..40ae75af 100644 --- a/CDPIUI.Core/Application.cs +++ b/CDPIUI.Core/Application.cs @@ -33,7 +33,10 @@ public void SetLocalization(string key) - public static string Version => GetVersion(); + // Read once: it was read from disk for every HTTP request. + private static readonly Lazy _version = new(GetVersion); + + public static string Version => _version.Value; private static string GetVersion() { diff --git a/CDPIUI.Core/Basic/ErrorsHelper.cs b/CDPIUI.Core/Basic/ErrorsHelper.cs index a4c012ec..94e23221 100644 --- a/CDPIUI.Core/Basic/ErrorsHelper.cs +++ b/CDPIUI.Core/Basic/ErrorsHelper.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Logger; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.ComponentModel; @@ -58,14 +58,14 @@ public override PrettyErrorCode MapExceptionToCode(Exception ex, out uint? rawHR return base.MapExceptionToCode(ex, out rawHResult, out statusCode); } - public override string GetPrettyErrorCode(string preffix, int hcode, ILogger? _ = null) + public override string GetPrettyErrorCode(string prefix, int hcode, ILogger? _ = null) { - return base.GetPrettyErrorCode(preffix, hcode, Logger.Instance); + return base.GetPrettyErrorCode(prefix, hcode, Logger.Instance); } - public override string GetPrettyErrorCode(string preffix, Exception ex, ILogger? _ = null) + public override string GetPrettyErrorCode(string prefix, Exception ex, ILogger? _ = null) { - return base.GetPrettyErrorCode(preffix, ex, Logger.Instance); + return base.GetPrettyErrorCode(prefix, ex, Logger.Instance); } public override ErrorModel GetErrorModel(string @object, Exception ex, ILogger? _ = null) diff --git a/CDPIUI.Core/Basic/Logger.cs b/CDPIUI.Core/Basic/Logger.cs index 221ff255..5bb0809d 100644 --- a/CDPIUI.Core/Basic/Logger.cs +++ b/CDPIUI.Core/Basic/Logger.cs @@ -1,5 +1,5 @@ using CDPIUI.Core.Data; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Shared.Logger; namespace CDPIUI.Core.Basic @@ -30,7 +30,7 @@ private static LogSelevirity GetSelevirity() #if DEBUG return LogSelevirity.DEBG; #else - return SettingsManager.Instance.GetValue("DEBUG", "logLevel").ToEnum(LogSelevirity.DEBG); + return SettingsManager.Instance.GetValue("DEBUG", "logLevel").ToEnum(LogSelevirity.WARN); #endif } } diff --git a/CDPIUI.Core/CDPIUI.Core.csproj b/CDPIUI.Core/CDPIUI.Core.csproj index 89011d2f..426b154c 100644 --- a/CDPIUI.Core/CDPIUI.Core.csproj +++ b/CDPIUI.Core/CDPIUI.Core.csproj @@ -55,4 +55,8 @@ none + + + + diff --git a/CDPIUI.Core/Communication/CoreCommandsHandler.cs b/CDPIUI.Core/Communication/CoreCommandsHandler.cs index 44937764..c7be2b67 100644 --- a/CDPIUI.Core/Communication/CoreCommandsHandler.cs +++ b/CDPIUI.Core/Communication/CoreCommandsHandler.cs @@ -1,7 +1,7 @@ using CDPIUI.Core.Basic; using CDPIUI.Core.Store.MSI; using CDPIUI.Shared.Pipe.Models; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System.Diagnostics; using static CDPIUI.Core.Store.MSI.MsiInstallerService; using CDPIUI.Core.ComponentServices; @@ -102,9 +102,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) var id = model.MessageData?["componentId"]; if (id == null) return; - ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager? .ClearOutput(); @@ -116,9 +114,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) var id = model.MessageData?["componentId"]; if (id == null) return; - ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager? .MarkAsStarted(); @@ -133,9 +129,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) var title = model.MessageData?["errorMessage"]; var text = model.MessageData?["errorObject"]; - var pm = ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + var pm = (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager; if (title != null && text != null) @@ -159,9 +153,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) if (id == null || processName == null) return; - ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager? .ChangeProcName(processName); @@ -176,9 +168,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) if (id == null || output == null) return; - ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager? .AddOutput(output); @@ -193,9 +183,7 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) if (id == null || output == null) return; - var pm = ComponentTasksManager.Instance - .GetTaskFromId(id) - .Result? + var pm = (await ComponentTasksManager.Instance.GetTaskFromId(id))? .ProcessManager; pm?.SetOutput(output); @@ -282,7 +270,7 @@ private static async Task HandleConditionalLaunchMessage(ConditionalLaunchMessag case ConditionalActionType.StartComponent: { var componentId = RequireParameter(model, "componentId"); - if (!await ComponentTasksManager.Instance.IsTaskRunned(componentId)) + if (!await ComponentTasksManager.Instance.IsTaskRunning(componentId)) { await ComponentTasksManager.Instance.CreateAndRunNewTask(componentId); var task = await ComponentTasksManager.Instance.GetTaskFromId(componentId); @@ -299,7 +287,7 @@ private static async Task HandleConditionalLaunchMessage(ConditionalLaunchMessag case ConditionalActionType.ToggleComponent: { var componentId = RequireParameter(model, "componentId"); - if (!await ComponentTasksManager.Instance.IsTaskRunned(componentId)) + if (!await ComponentTasksManager.Instance.IsTaskRunning(componentId)) { await ComponentTasksManager.Instance.CreateAndRunNewTask(componentId); var task = await ComponentTasksManager.Instance.GetTaskFromId(componentId); diff --git a/CDPIUI.Core/Communication/PipeClientService.cs b/CDPIUI.Core/Communication/PipeClientService.cs index 5890ee95..53314900 100644 --- a/CDPIUI.Core/Communication/PipeClientService.cs +++ b/CDPIUI.Core/Communication/PipeClientService.cs @@ -2,7 +2,7 @@ using CDPIUI.Shared; using CDPIUI.Shared.Pipe; using CDPIUI.Shared.Pipe.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.Shared.Migration; using System.Diagnostics; using System.IO.Pipes; @@ -53,7 +53,7 @@ public void Init() { PipeStream = new NamedPipeClientStream(".", SharedConstants.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous, - TokenImpersonationLevel.Impersonation); + TokenImpersonationLevel.Identification); } public async void Start() @@ -71,6 +71,18 @@ public async void Start() } catch { } + if (!PipeStream.IsConnected && IsTrayProcessRunning()) + { + try + { + await ((NamedPipeClientStream)PipeStream).ConnectAsync(TrayStartupWaitMs); + } + catch (Exception ex) when (ex is TimeoutException or IOException) + { + Logger?.CreateErrorLog(nameof(PipeClientService), $"Tray process is running but did not accept the connection: {ex.Message}"); + } + } + if (!PipeStream.IsConnected) { try @@ -81,6 +93,12 @@ public async void Start() { startupString = $"--show-ui {migrationRequest!.RawArgument}"; } + else if (Shared.Basic.CommandLine.GuiActivationArguments.Encode( + Environment.GetCommandLineArgs().Skip(1)) is string guiArgs) + { + // The tray starts the GUI again with these, so the opened file or link is not lost. + startupString = $"--show-ui {guiArgs}"; + } var psi = new ProcessStartInfo(Path.Combine(Data.Directories.CurrentDirectory, "CDPIUI_TrayIcon.exe"), startupString) { UseShellExecute = true, @@ -96,6 +114,14 @@ public async void Start() return; } + if (!PipePeerVerifier.IsServerExpected((NamedPipeClientStream)PipeStream, Path.Combine(Data.Directories.CurrentDirectory, "CDPIUI_TrayIcon.exe"))) + { + Logger?.CreateErrorLog(nameof(PipeClientService), "Pipe server is not the application tray process. Connection closed."); + Dispose(); + Process.GetCurrentProcess().Kill(); + return; + } + CancellationTokenSource.Dispose(); CancellationTokenSource = null; CreateCancellationToken(); @@ -119,6 +145,24 @@ public async void Start() } + private const int TrayStartupWaitMs = 15000; + + private static bool IsTrayProcessRunning() + { + try + { + var processes = Process.GetProcessesByName("CDPIUI_TrayIcon"); + bool running = processes.Length > 0; + foreach (var process in processes) + process.Dispose(); + return running; + } + catch (Exception) + { + return false; + } + } + protected override async Task SendConnectionPacket() { await SendMessageAsync(ServiceMessageModel.RequestAuth(Shared.Secrets.Secret.AuthGuid).ToString()); diff --git a/CDPIUI.Core/ComponentServices/ComponentTasksManager.cs b/CDPIUI.Core/ComponentServices/ComponentTasksManager.cs index aa5c7fa9..8c710d0b 100644 --- a/CDPIUI.Core/ComponentServices/ComponentTasksManager.cs +++ b/CDPIUI.Core/ComponentServices/ComponentTasksManager.cs @@ -1,4 +1,5 @@ -using CDPIUI.Core.Communication; +using CDPIUI.Core.Basic; +using CDPIUI.Core.Communication; using CDPIUI.Core.ComponentServices.Helpers; using CDPIUI.Core.Store; using CDPIUI.Core.Store.Database; @@ -26,22 +27,40 @@ public static ComponentTasksManager Instance private ComponentTasksManager() { - UpdateTaskList(); + _ = UpdateTaskList(); ComponentItemsLoaderHelper.Instance.InitRequested += SendTasksData; StoreHelper.Instance.ItemActionsStopped += (id) => RequestComponentItemsInit(); StoreHelper.Instance.ItemRemoved += (id) => RequestComponentItemsInit(); } - public void UpdateTaskList() + /// + /// Rebuilds the task list from the installed components, for example after a component + /// was installed or updated, and starts again the components that were running. + /// + public async Task UpdateTaskList() { - var _tasks = Tasks; - _ = StopAllTasks(); - foreach (var item in DatabaseHelper.Instance.GetItemsByType("component")) + try { - AddNewTask(item.Id!); - if (_tasks.FirstOrDefault(x => x.Id == item.Id)?.ProcessManager?.IsProcessRunning ?? false) - _ = CreateAndRunNewTask(item.Id!); + // Taken before stopping: StopAllTasks clears the running state. + var runningIds = (await GetTasksSnapshot()) + .Where(x => x.ProcessManager?.IsProcessRunning ?? false) + .Select(x => x.Id) + .ToHashSet(); + + await StopAllTasks(); + + foreach (var item in DatabaseHelper.Instance.GetItemsByType("component")) + { + if (runningIds.Contains(item.Id)) + await CreateAndRunNewTask(item.Id!); + else + await AddNewTaskAsync(item.Id!); + } + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(ComponentTasksManager), $"Cannot update the task list: {ex}"); } } @@ -84,15 +103,16 @@ await PipeHelper.SendSettingsPacket( } else { + // The task can be missing here, so the requested id is reported. await PipeHelper.SendSettingsPacket( Shared.Pipe.Models.SettingsMessageIds.ComponentNotInstalled, - new() { { "componentId", task.Id } }); + new() { { "componentId", id } }); } } public async void SendTasksData() { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { await SendTaskData(task.Id); } @@ -118,9 +138,11 @@ public async void StopService() public async Task RunAllPreferredActions() { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { - await task.ProcessManager?.RunActionsIfAutorunSelected(); + // "await x?.M()" awaits null and throws when ProcessManager is null. + if (task.ProcessManager != null) + await task.ProcessManager.RunActionsIfAutorunSelected(); } } diff --git a/CDPIUI.Core/ComponentServices/Helpers/ComponentHelper.cs b/CDPIUI.Core/ComponentServices/Helpers/ComponentHelper.cs index 882f00be..db2d1703 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/ComponentHelper.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/ComponentHelper.cs @@ -86,7 +86,8 @@ public ConfigurationService GetConfigHelper() public string? GetDirectory() { - if (File.Exists(Directory)) + // A folder: File.Exists is always false for it. + if (global::System.IO.Directory.Exists(Directory)) return Directory; return TryGetNewDirectory(); diff --git a/CDPIUI.Core/ComponentServices/Helpers/ComponentItemsLoaderHelper.cs b/CDPIUI.Core/ComponentServices/Helpers/ComponentItemsLoaderHelper.cs index 814d4d4e..c95a8a97 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/ComponentItemsLoaderHelper.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/ComponentItemsLoaderHelper.cs @@ -5,7 +5,9 @@ namespace CDPIUI.Core.ComponentServices.Helpers public class ComponentItemsLoaderHelper { - private List Components = []; + // Replaced as a whole, never changed in place: Init runs on the startup path and after every + // Store change while other threads look components up. + private volatile List Components = []; public Action? InitRequested; @@ -30,36 +32,34 @@ private ComponentItemsLoaderHelper() public void Init(bool forse = true) { - if (!forse && Components.Count != 0) return; - Components.Clear(); + List current = Components; + if (!forse && current.Count != 0) return; + List configItems = DatabaseHelper.Instance.GetItemsByType("component"); + List components = []; foreach (DatabaseStoreItem item in configItems) { if (!Path.Exists(item.Directory) || - !File.Exists(Path.Combine(item.Directory, item.Executable + ".exe"))) + !File.Exists(Path.Combine(item.Directory!, item.Executable + ".exe"))) continue; - ComponentHelper componentHelper = new(item.Id!); - Components.Add(componentHelper); + // New helpers: an installed or updated pack changes the preset lists they cache. + components.Add(new ComponentHelper(item.Id!)); } + Components = components; InitRequested?.Invoke(); } public List GetComponentHelpers() { - return Components; + return [.. Components]; } public ComponentHelper? GetComponentHelperFromId(string id) { - foreach (ComponentHelper componentHelper in Components) - { - if (componentHelper.Id == id) - return componentHelper; - } - return null; + return Components.FirstOrDefault(componentHelper => componentHelper.Id == id); } } } diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigCommandLine.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigCommandLine.cs index 8818383d..83969df6 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigCommandLine.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigCommandLine.cs @@ -21,10 +21,18 @@ public static IReadOnlyList Tokenize(string? commandLine) StringBuilder current = new(); char quote = '\0'; int backslashes = 0; - foreach (char character in commandLine ?? string.Empty) + string text = commandLine ?? string.Empty; + for (int index = 0; index < text.Length; index++) { - if ((character is '"' or '\'') && - (quote == '\0' || quote == character) && backslashes % 2 == 0) + char character = text[index]; + // A quoted folder path ends with a backslash ("C:\dir\"): before whitespace or the + // end of the line that quote closes the argument instead of being escaped. + bool closesAfterBackslash = quote == '"' && + (index + 1 == text.Length || char.IsWhiteSpace(text[index + 1])); + // Only double quotes group on the Windows command line; an apostrophe inside a value + // (a Lua argument, a file name) must not join the following arguments. + if (character == '"' && + (quote == '\0' || quote == character) && (backslashes % 2 == 0 || closesAfterBackslash)) { quote = quote == character ? '\0' : character; } @@ -104,7 +112,8 @@ public static bool TryGetOptionName(string token, out string name, out int equal return IsOption(name); } - public static bool IsOption(string token) => token.Length > 1 && token[0] == '-'; + // A negative number such as -10000 is a value, not an option. + public static bool IsOption(string token) => token.Length > 1 && token[0] == '-' && !char.IsDigit(token[1]); public static string ToSingleLine(string? commandLine) => string.Join( diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigFileReferences.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigFileReferences.cs index d934afec..7452d4c6 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigFileReferences.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigFileReferences.cs @@ -27,7 +27,7 @@ public sealed record ConfigUsedFile( } /// Shared, UI-independent parsing and rewriting of config file references. -public static class ConfigFileReferences +public static partial class ConfigFileReferences { public static IReadOnlyList ExtractFromText( string? text, @@ -76,8 +76,10 @@ public static IReadOnlyList ExtractFromText( { return null; } - // --lua-init also accepts inline Lua, and --blob accepts inline hexadecimal data. + // --lua-init also accepts inline Lua, and --blob accepts inline hexadecimal data. A value + // with the @ file marker is a file even when its name has ';' or '='. if (option.Name.Equals("--lua-init", StringComparison.OrdinalIgnoreCase) && + !ConfigCommandLine.Unquote(option.Value).StartsWith('@') && expanded.IndexOfAny([';', '=', '\n', '\r']) >= 0) { return null; @@ -131,10 +133,20 @@ private static string GetOptionFileReference(string name, string value) { int separator = path.IndexOf(':'); path = separator >= 0 ? path[(separator + 1)..] : path; + path = StripBlobOffset(path); } return NormalizePath(path); } + /// + /// --blob=name:+4@file.bin reads the file from offset 4: the "+4" is not part of the path. + /// + private static string StripBlobOffset(string source) => + BlobOffsetRegex().Replace(source, string.Empty, 1); + + [GeneratedRegex(@"^\+\d+(?=[@$])")] + private static partial Regex BlobOffsetRegex(); + /// Replaces all matching file arguments, retaining blob names and Lua markers. public static string ReplaceFileInText(string? text, string filePath, ConfigFileKind kind, string replacementPath) { @@ -175,8 +187,10 @@ public static string ReplaceFileInText(string? text, string filePath, ConfigFile int separator = unquoted.IndexOf(':'); string prefix = separator >= 0 ? unquoted[..(separator + 1)] : string.Empty; string source = separator >= 0 ? unquoted[(separator + 1)..] : unquoted; + string offset = source[..^StripBlobOffset(source).Length]; + source = source[offset.Length..]; char marker = source.StartsWith('$') && !source.StartsWith("$GETCURRENTDIR()", StringComparison.OrdinalIgnoreCase) ? '$' : '@'; - updatedValue = ConfigCommandLine.Quote($"{prefix}{marker}{replacementPath}"); + updatedValue = ConfigCommandLine.Quote($"{prefix}{offset}{marker}{replacementPath}"); } else { diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigItem.Content.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigItem.Content.cs index edf763ef..f0e4f3a8 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigItem.Content.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigItem.Content.cs @@ -115,12 +115,13 @@ public string ExpandVariables(string? text) } string result = text ?? string.Empty; HashSet visited = new(StringComparer.Ordinal); - for (int depth = 0; depth < 16 && visited.Add(result); depth++) + for (int depth = 0; depth < global::CDPIUI.Core.ComponentServices.Configuration.ConfigurationService.MaxVariablePasses && visited.Add(result); depth++) { - string expanded = Regex.Replace(result, "%([A-Za-z0-9_]+)%", + string expanded = Regex.Replace(result, "%([A-Za-z0-9_.\\-]+)%", match => values.TryGetValue(match.Groups[1].Value, out string? value) ? value : match.Value, RegexOptions.CultureInvariant); - if (expanded == result) + // %A%=%A%%A% grows four times per pass: stop before it takes all memory. + if (expanded == result || expanded.Length > global::CDPIUI.Core.ComponentServices.Configuration.ConfigurationService.MaxExpandedLength) { break; } diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigPackMakeHelper.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigPackMakeHelper.cs index 9f8cac80..f29ed32e 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigPackMakeHelper.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigPackMakeHelper.cs @@ -6,8 +6,8 @@ using CDPIUI.Shared; using CDPIUI.Shared.Basic.Filesystem; using CDPIUI.Shared.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.PrettyErrorConversionService; +using CDPIUI.Shared.Extensions; using System.IO.Compression; using CDPIUI.Core.ComponentServices.Configuration; using CDPIUI.Core.ComponentServices.Helpers.Configuration.Helpers; @@ -51,7 +51,7 @@ private static async Task> CreateConfigPack_Wo Directory.CreateDirectory(tempDir); - var copyFilesResult = await CopyUsedFilesToDirecrory(configItems, tempDir); + var copyFilesResult = await CopyUsedFilesToDirectory(configItems, tempDir); if (!(bool)copyFilesResult?.Success!) return copyFilesResult; var copyConfigsResult = await CopyConfigsToDirectory(configItems, tempDir); @@ -135,7 +135,7 @@ private static void ImportToDatabase(ConfigPackInitModel modelTemplate, string f Name = modelTemplate.Name, ShortName = modelTemplate.ShortName, Developer = modelTemplate.Developer, - BackgroudColor = "" + BackgroundColor = "" }); } @@ -188,7 +188,8 @@ private static ConfigPackInitModel FillInitModel( modelTemplate.Name ??= modelTemplate.ShortName; modelTemplate.BeforeInstallActions ??= string.Empty; - modelTemplate.AfterInstallActions ??= $"$RUNSCRIPT(apply_config, {modelTemplate.StoreId})"; + // Nothing to run: the preset lists are reloaded after every installation. + modelTemplate.AfterInstallActions ??= string.Empty; modelTemplate.Type = "configlist"; @@ -252,7 +253,8 @@ private static async Task> CopyLocFile Path.Combine(item.Item2, locFile.Value)); if (oldLocalizationDict == null || newLocalizationDict == null) continue; - oldLocalizationDict.AddRange(newLocalizationDict); + foreach (var (key, value) in newLocalizationDict) + oldLocalizationDict.TryAdd(key, value); string jsonString = JSONConvertor.SerializeObject(oldLocalizationDict); File.WriteAllText(targetDir, jsonString); @@ -290,7 +292,8 @@ private static async Task>>> Cop string jsonString = JSONConvertor.SerializeObject(item); File.WriteAllText(targetFile, jsonString); - if (requirements.FirstOrDefault(x => x.Item1 == item.target[0]) == null) + if (item.target is { Count: >= 2 } && + requirements.FirstOrDefault(x => x.Item1 == item.target[0]) == null) { requirements.Add(Tuple.Create(item.target[0], item.target[1])); } @@ -310,7 +313,7 @@ private static async Task>>> Cop return OperationResultModel>>.SuccessResult(requirements); } - private static async Task> CopyUsedFilesToDirecrory( + private static async Task> CopyUsedFilesToDirectory( List itemsToAdd, string targetDirectory) { @@ -326,7 +329,9 @@ private static async Task> CopyUsedFilesToDire Autocorrector.FindAutoCorrectPath( file, item, - LScriptCore.ExecuteScript("$GETCURRENTDIR()", callItemId: item.packId))); + // A file path is expected: the search uses its folder, and the pack + // folder itself gave the parent Store\Items folder. + Path.Combine(LScriptCore.ExecuteScript("$GETCURRENTDIR()", callItemId: item.packId), "init.json"))); if (!usedFiles.Contains(pair)) usedFiles.Add(pair); } @@ -341,6 +346,16 @@ private static async Task> CopyUsedFilesToDire string dir = Path.GetRelativePath( ConfigurationService.GetItemFolderFromPackId(idFilePair.Item1), idFilePair.Item2); + // A file of another item gives "..\..\" here, and the copy would land + // outside the pack being built. + if (Path.IsPathRooted(dir) || dir == ".." || + dir.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal)) + { + Logger.Instance.CreateWarningLog(nameof(ConfigPackMakeHelper), + $"\"{idFilePair.Item2}\" is outside the pack {idFilePair.Item1} and is not copied."); + continue; + } + Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(targetDirectory, dir))!); await FileSystemService.CopyFileAsync(idFilePair.Item2, Path.Combine(targetDirectory, dir)); } @@ -348,7 +363,8 @@ private static async Task> CopyUsedFilesToDire } catch (Exception ex) { - OperationResultModel.FailureResult( + // The failure was created and dropped, so the export reported success without files. + return OperationResultModel.FailureResult( new ErrorModel() { ErrorCode = ErrorsHelper.Convertor.GetPrettyErrorCode("CONFIG_PACK_COPY_F", ex), diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigurationService.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigurationService.cs index d21b2d0f..df2628bc 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigurationService.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/ConfigurationService.cs @@ -8,7 +8,7 @@ using CDPIUI.Core.Store.Data; using CDPIUI.Core.Store.Database; using CDPIUI.Core.Store.Repository.Localization; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; @@ -17,7 +17,9 @@ namespace CDPIUI.Core.ComponentServices.Configuration { public partial class ConfigurationService { - private const string BatStartRegex = @"start\s+(?:"".*?"")?"".*?""\s+(((?:/min){0,1}\s+"".*?"")|(\S*))?(.*)\^?$"; + // start ["title"] [/min /b ...] program args: the program may be quoted or not, so that + // `start "t" /min %BIN%winws.exe ...` does not leave the program in the arguments. + private const string BatStartRegex = @"^start\s+(?:""[^""]*""\s+)?(?:/\w+\s+)*(?:""[^""]*""|\S+)(?.*?)\^?\s*$"; private readonly List Items = []; @@ -47,7 +49,19 @@ private List InitConfigDirectory(DatabaseStoreItem item) if (!File.Exists(initFile)) return configItems; - ConfigInitItem configInitItem = JSONConvertor.LoadJson(initFile); + // A damaged pack must not hide the presets of the other packs (issue #61). + ConfigInitItem? configInitItem; + try + { + configInitItem = JSONConvertor.LoadJson(initFile); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(ConfigurationService), $"Cannot read \"{initFile}\": {ex.Message}"); + return configItems; + } + if (configInitItem == null) + return configItems; string[] jsonFiles = Directory.GetFiles(directory, "*.json"); @@ -61,7 +75,8 @@ private List InitConfigDirectory(DatabaseStoreItem item) foreach (string jsonFile in jsonFiles) { - if (Path.GetFileName(jsonFile).StartsWith("init")) + // Only the pack description: presets such as "initial-*.json" were skipped too. + if (Path.GetFileName(jsonFile).Equals("init.json", StringComparison.OrdinalIgnoreCase)) continue; try @@ -76,7 +91,8 @@ private List InitConfigDirectory(DatabaseStoreItem item) var result = Regex.Replace( configItem.name!, - @"\$.*?\((.*?)\)", + // Only $NAME(key) is a script: a preset named "Fast $5 (beta)" stays as it is. + @"\$[A-Za-z_]+\((.*?)\)", match => { var key = match.Groups[1].Value; @@ -281,13 +297,24 @@ public static async Task SaveConfigItem(string filename, string packId, string folder = GetItemFolderFromPackId(packId); string fileName = Path.Combine(folder, filename); - item.name = item.not_converted_name; + // The file keeps the name as written by its author ($LOADDYNAMIC... keys), while the cached + // item keeps showing the localized name. + string? displayName = item.name; + string jsonString; + try + { + item.name = item.not_converted_name ?? item.name; + jsonString = JSONConvertor.SerializeObject(item); + } + finally + { + item.name = displayName; + } - string jsonString = JSONConvertor.SerializeObject(item); Logger.Instance.CreateDebugLog(nameof(ConfigurationService), jsonString); try { - File.WriteAllText(fileName, jsonString); + CDPIUI.Shared.Basic.Filesystem.FileSystemService.WriteAllTextAtomic(fileName, jsonString); } catch (Exception ex) { @@ -307,8 +334,17 @@ public static string GetDefaultLocalePath(string packId) if (!File.Exists(initFile)) return string.Empty; - ConfigInitItem configInitItem = JSONConvertor.LoadJson(initFile); - string _preFilepath = configInitItem.localized_strings_directory?.GetValueOrDefault("EN", null) ?? string.Empty; + ConfigInitItem? configInitItem; + try + { + configInitItem = JSONConvertor.LoadJson(initFile); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(ConfigurationService), $"Cannot read \"{initFile}\": {ex.Message}"); + return string.Empty; + } + string _preFilepath = configInitItem?.localized_strings_directory?.GetValueOrDefault("EN", null) ?? string.Empty; return string.IsNullOrEmpty(_preFilepath) ? string.Empty : Path.Combine(folder, _preFilepath); } @@ -336,7 +372,9 @@ public string GetLocalizedConfigVarName(string name, string packId) List variables, Dictionary jparams) { - Dictionary result = []; + // Same rules as ConfigItem.ExpandVariables in the UI: names ignore case, and a name + // declared twice takes the last value instead of failing the start. + Dictionary result = new(StringComparer.OrdinalIgnoreCase); string regexString = @"%(.*?)%=(.*?)$"; if (variables == null || jparams == null) @@ -351,14 +389,26 @@ public string GetLocalizedConfigVarName(string name, string packId) if (!match.Success) continue; - result.Add( - match.Groups[1].Value, - LScriptCore.ExecuteScript(match.Groups[2].Value, callItemId: id, jparams: jparams)); + // A folder script is left for the expansion of the whole startup string, which + // quotes a path with spaces; expanded here, the path stayed unquoted (issue #2). + string value = match.Groups[2].Value; + string trimmed = value.TrimStart(); + result[match.Groups[1].Value] = + trimmed.StartsWith("$GETCURRENTDIR", StringComparison.OrdinalIgnoreCase) || + trimmed.StartsWith("$GETSRDIR", StringComparison.OrdinalIgnoreCase) + ? value + : LScriptCore.ExecuteScript(value, callItemId: id, jparams: jparams); } return result; } + internal const int MaxVariablePasses = 16; + internal const int MaxExpandedLength = 64 * 1024; + + [GeneratedRegex("%(?[A-Za-z0-9_.\\-]+)%", RegexOptions.CultureInvariant)] + private static partial Regex VariableReferenceRegex(); + public static string ReplaceVariables(string input, IDictionary readyToUseVars) { if (string.IsNullOrEmpty(input) || readyToUseVars == null || readyToUseVars.Count == 0) @@ -366,32 +416,22 @@ public static string ReplaceVariables(string input, IDictionary return input; } - var pattern = new Regex("%(?[A-Za-z0-9_]+)%", RegexOptions.Compiled); + var vars = new Dictionary(readyToUseVars, StringComparer.OrdinalIgnoreCase); - foreach (var variable in readyToUseVars) - { - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"{variable.Key}, {variable.Value}"); - } - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"Check {input}"); + string evaluator(Match match) => + vars.TryGetValue(match.Groups["name"].Value, out string? value) ? value : match.Value; - string evaluator(Match match) + // Variables may refer to other variables, as in the editor preview, which expands + // them the same way; the length limit stops definitions such as %A%=%A%%A%. + string result = input; + for (int pass = 0; pass < MaxVariablePasses; pass++) { - string value = ""; - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"Check {value}"); - - var varName = match.Groups["name"].Value; - if (readyToUseVars.TryGetValue(varName, out value)) - { - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"Check {varName} >>> {value}"); - return value; - } - - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"Check {varName} >>> {value}"); - - return match.Value; + string expanded = VariableReferenceRegex().Replace(result, evaluator); + if (expanded == result || expanded.Length > MaxExpandedLength) + break; + result = expanded; } - - return pattern.Replace(input, new MatchEvaluator(evaluator)); + return result; } /// @@ -400,10 +440,10 @@ string evaluator(Match match) /// /// Target item /// - /// Force rebuild config if component required hidden convertion.

+ /// Force rebuild config if component required hidden conversion.

/// If , rebuild called only if user sets /// "ZAPRET2/validateLegacyCacheHashes" as

- /// If target not required convertion this property + /// If target not required conversion this property /// ignores. /// /// Component startup string for item @@ -423,7 +463,7 @@ public static string GetStartupParametersByConfigItem(ConfigItem item, bool forc startupString = ReplaceVariables(startupString, readyToUseVars); startupString = ReplaceCommaVariables(startupString, commaVars); - startupString = LScript.LScriptCore.ExecuteScriptUnsafe(startupString, callItemId: packId); + startupString = LScript.LScriptCore.ExecuteScriptUnsafe(startupString, callItemId: packId, quotePaths: true); if (!item.IsLegacy) { @@ -479,7 +519,7 @@ public string GetStartupParameters(string filename, string packId) startupString = ReplaceVariables(startupString, readyToUseVars); startupString = ReplaceCommaVariables(startupString, commaVars); - startupString = LScript.LScriptCore.ExecuteScriptUnsafe(startupString, callItemId: packId); + startupString = LScript.LScriptCore.ExecuteScriptUnsafe(startupString, callItemId: packId, quotePaths: true); return startupString; } @@ -809,8 +849,8 @@ private void GetAllWindowsInConfig( string startupString = GetStartupParameters(filename, packId); Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"{startupString}"); - windows = startupString - .Split(new[] { "--new" }, StringSplitOptions.RemoveEmptyEntries) + // Only the --new option separates profiles, not options such as --newfoo. + windows = Regex.Split(startupString, @"(? w.Trim()) .Where(w => w.Length > 0) .ToList(); @@ -825,7 +865,7 @@ public static string GetItemFolderFromPackId(string packId) private static Tuple ConvertConfigFromBAT(string filepath) { // TODO: recursevily call other .BAT files via GOTO check - string[] lines = File.ReadAllLines(filepath, Encoding.UTF8); + string[] lines = CDPIUI.Core.System.BatchFileEncoding.ReadAllLines(filepath); Dictionary vars = []; List availableVarValues = []; @@ -841,16 +881,18 @@ private static Tuple ConvertConfigFromBAT(string filepath) if (string.IsNullOrEmpty(line)) continue; - if (line.StartsWith("set")) + if (line.StartsWith("set", StringComparison.OrdinalIgnoreCase)) { string[] setLine = line[3..].Split("="); if (setLine.Length >= 2) { - string key = setLine[0].Replace("\"", ""); + // set "WF=..." keeps a space before the name: trimmed here, or the + // variable was not found by the alternatives below. + string key = setLine[0].Replace("\"", "").Trim(); string value = line[3..].Replace($"{setLine[0]}=", "").Replace("\"", "").Trim(); - Match match = VarInVarRegex().Match(setLine[1]); - if (match.Success) + // Every !VAR! of the value, not only the first one. + foreach (Match match in VarInVarRegex().Matches(value)) { string varInVar = match.Groups[1].Value; if (vars.ContainsKey(varInVar)) @@ -889,7 +931,9 @@ private static Tuple ConvertConfigFromBAT(string filepath) comment = line[2..^2].Trim(); continue; } - if (line.StartsWith("rem") && line.Contains("set")) + // "rem set NAME=value" lists another value of a variable; a comment such as + // "rem settings = ..." is no such line. + if (RemSetRegex().IsMatch(line)) { string _l = line[3..]; var rx = new Regex(@"\s+set\b", RegexOptions.None); @@ -936,7 +980,7 @@ private static Tuple ConvertConfigFromBAT(string filepath) } continue; } - if (line.StartsWith("start")) + if (line.StartsWith("start", StringComparison.OrdinalIgnoreCase)) { if (line.Contains("winws.exe")) { @@ -953,7 +997,7 @@ private static Tuple ConvertConfigFromBAT(string filepath) Match match = BatFileStartRegex().Match(line); if (match.Success) { - comma = match.Groups[4].Value.Trim().Replace('^', ' '); + comma = match.Groups["args"].Value.Trim().Replace('^', ' '); Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"Found start line {comma}"); } continue; @@ -1003,7 +1047,7 @@ private static Tuple ConvertConfigFromBAT(string filepath) return null; - if (Path.GetExtension(filepath).Equals(".json", StringComparison.CurrentCultureIgnoreCase)) + if (Path.GetExtension(filepath).Equals(".json", StringComparison.OrdinalIgnoreCase)) { OldConfigItem oldConfig = JSONConvertor.LoadJson(filepath); if (oldConfig != null && oldConfig.custom_parameters != null) @@ -1021,8 +1065,8 @@ private static Tuple ConvertConfigFromBAT(string filepath) } return Tuple.Create(JSONConvertor.LoadJson(filepath), true); } - else if (Path.GetExtension(filepath).Equals(".bat", StringComparison.CurrentCultureIgnoreCase) || - Path.GetExtension(filepath).Equals(".cmd", StringComparison.CurrentCultureIgnoreCase)) + else if (Path.GetExtension(filepath).Equals(".bat", StringComparison.OrdinalIgnoreCase) || + Path.GetExtension(filepath).Equals(".cmd", StringComparison.OrdinalIgnoreCase)) { try { @@ -1049,25 +1093,31 @@ public static List GetUsedFilesFromConfigItem(ConfigItem configItem) => public static ConfigItem ReplaceFilesPath(ConfigItem configItem, Dictionary files) { + // Replaces whole paths only, with either separator and with the quotes around them: + // a substring replacement also changed "mylist.txt" when "list.txt" was replaced, every + // apostrophe in the line became a quote, and "" (an empty value) became ". static string ReplaceInString(string input, Dictionary files) { if (string.IsNullOrEmpty(input) || files == null || files.Count == 0) return input; - input = input.Replace("%~dp0", "").Replace("\'", "\""); + input = input.Replace("%~dp0", ""); foreach (var kvp in files) { - string oldPath = kvp.Key; - string newPath = kvp.Value; - - if (input.Contains(oldPath)) - { - input = input.Replace(oldPath, $"\"{newPath}\""); - Logger.Instance.CreateDebugLog(nameof(ConfigurationService), $"{input}"); - } + string oldPath = kvp.Key?.Trim().Trim('"', '\'') ?? string.Empty; + if (oldPath.Length == 0) + continue; + string newPath = $"\"{kvp.Value.Trim().Trim('"')}\""; + + string pathPattern = string.Join("[\\\\/]", + oldPath.Split('\\', '/').Select(Regex.Escape)); + input = Regex.Replace( + input, + $@"(?[""']?){pathPattern}\k(?![\w.\\/-])", + _ => newPath, + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); } - input = input.Replace("\"\"", "\""); return input; } @@ -1158,9 +1208,11 @@ public static string GetLastEditTimeFromConfigFile(string filename, string packI [GeneratedRegex(@"("")(/.*?\\)(.*?"")")] private static partial Regex WindowConverterRegex(); - [GeneratedRegex(BatStartRegex)] + [GeneratedRegex(BatStartRegex, RegexOptions.IgnoreCase)] private static partial Regex BatFileStartRegex(); [GeneratedRegex(@"!(.*?)!")] private static partial Regex VarInVarRegex(); + [GeneratedRegex(@"^rem\s+set\s", RegexOptions.IgnoreCase)] + private static partial Regex RemSetRegex(); } } diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret1ToZapret2Converter.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret1ToZapret2Converter.cs index ebb0fe8a..a256d0bf 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret1ToZapret2Converter.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret1ToZapret2Converter.cs @@ -6,7 +6,7 @@ namespace CDPIUI.Core.ComponentServices.Helpers.Configuration.Converters; /// /// Converts an already expanded winws/nfqws1 command line to a winws2 command line. -/// Converts only command line - config files convertion isn't supported +/// Converts only command line - config files conversion isn't supported /// public sealed class Zapret1ToZapret2Converter { @@ -80,6 +80,23 @@ public sealed class Zapret1ToZapret2Converter "dpi-desync-udplen-pattern", "dpi-desync-start", "dpi-desync-cutoff" }; + // Names of older zapret1 versions, renamed in zapret 67 to the names Zapret2 also uses. + private static readonly Dictionary LegacyModeAliases = new(StringComparer.OrdinalIgnoreCase) + { + ["split2"] = "multisplit", + ["disorder2"] = "multidisorder", + ["split"] = "fakedsplit", + ["disorder"] = "fakeddisorder", + }; + + // Options that only tune details of the packets and have no Zapret2 counterpart here. They are + // dropped with a warning: failing the whole preset for them left the user with nothing. + private static readonly HashSet DroppedLegacyOptions = new(StringComparer.OrdinalIgnoreCase) + { + "wssize", "wssize-cutoff", "hostcase", "hostspell", "hostnospace", "domcase", + "methodspace", "methodeol", "unixeol" + }; + private static readonly HashSet ZeroPhaseModes = new(StringComparer.OrdinalIgnoreCase) { "synack", "syndata" @@ -240,6 +257,16 @@ private string ConvertProfile(LegacyProfile profile) continue; } + if (DroppedLegacyOptions.Contains(option.Name)) + { + AddWarning( + "DROPPED_OPTION", + $"Zapret1 option '--{option.Name}' has no Zapret2 counterpart and was left out.", + option.Name, + profile.Index); + continue; + } + if (!SupportedLegacyOptions.Contains(option.Name)) { AddError( @@ -267,7 +294,9 @@ private string ConvertProfile(LegacyProfile profile) } string[] modes = strategyValue - .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(mode => LegacyModeAliases.TryGetValue(mode, out string? current) ? current : mode) + .ToArray(); if (modes.Length == 0) { @@ -897,8 +926,9 @@ private static void ParseFileExpression(string value, out string path, out long offset = 0; path = value; + // "+N@path" or "@path": an '@' later in the path is part of the file name. int atIndex = value.IndexOf('@'); - if (atIndex >= 0) + if (atIndex == 0 || (atIndex > 0 && value[0] == '+' && value[1..atIndex].All(char.IsDigit))) { string offsetPart = value[..atIndex]; path = value[(atIndex + 1)..]; diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret2LegacyConfigService.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret2LegacyConfigService.cs index a8f5749f..225f7aa7 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret2LegacyConfigService.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Converters/Zapret2LegacyConfigService.cs @@ -19,6 +19,7 @@ public static class Zapret2LegacyConfigService public const bool DefaultHashValidationValue = true; private const string CacheFormatVersion = "1"; + private static readonly Lazy ConverterVersion = new(() => global::CDPIUI.Core.ApplicationInfo.Version); private static readonly object SyncRoot = new(); public static string CacheRoot => @@ -64,7 +65,7 @@ public static void EnsureStorageRegistered(string? applicationVersion = null) Name = "Zapret1 to Zapret2 converted config storage", ShortName = "Converted Zapret configs", Developer = "Autogenerated", - BackgroudColor = string.Empty + BackgroundColor = string.Empty }; if (!DatabaseHelper.Instance.AddOrUpdateItem(cacheItem)) @@ -104,9 +105,14 @@ public static string GetStartupString( string manifestPath = Path.Combine(manifestDirectory, GetManifestFileName(config)); ConvertedConfigManifest? existingManifest = LoadManifest(manifestPath); - if (!forceRebuild && !validateHashes && IsCachePresent(existingManifest)) + // Without file hashing the cache is still bound to the converter version and to the + // command line, so a new converter or a changed toggle or variable converts again. + string commandLineHash = ComputeCommandLineHash(expandedLegacyStartupString); + if (!forceRebuild && !validateHashes && IsCachePresent(existingManifest) && + existingManifest!.ConverterVersion == ConverterVersion.Value && + existingManifest.CommandLineHash == commandLineHash) { - return existingManifest!.StartupString; + return existingManifest.StartupString; } Dictionary mappings = @@ -166,6 +172,8 @@ string MapFile(string sourcePath) PackId = config.packId, ConfigFileName = config.file_name, InputHash = inputHash, + ConverterVersion = ConverterVersion.Value, + CommandLineHash = commandLineHash, StartupString = conversion.StartupString, RequiredCachedFiles = GetRequiredCachedFiles(conversion.ReferencedFiles, mappings), ConvertedAtUtc = DateTime.UtcNow @@ -286,6 +294,13 @@ private static void MaterializeFiles( } } + private static string ComputeCommandLineHash(string expandedLegacyStartupString) + { + using IncrementalHash hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + AppendString(hash, expandedLegacyStartupString); + return Convert.ToHexString(hash.GetHashAndReset()).ToLowerInvariant(); + } + private static string ComputeInputHash( ConfigItem config, string expandedLegacyStartupString, @@ -295,6 +310,7 @@ private static string ComputeInputHash( { using IncrementalHash hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); AppendString(hash, CacheFormatVersion); + AppendString(hash, ConverterVersion.Value); AppendString(hash, config.packId ?? string.Empty); AppendString(hash, config.file_name ?? string.Empty); AppendString(hash, expandedLegacyStartupString); @@ -439,6 +455,8 @@ private sealed class ConvertedConfigManifest public string PackId { get; set; } = string.Empty; public string ConfigFileName { get; set; } = string.Empty; public string InputHash { get; set; } = string.Empty; + public string ConverterVersion { get; set; } = string.Empty; + public string CommandLineHash { get; set; } = string.Empty; public string StartupString { get; set; } = string.Empty; public List? RequiredCachedFiles { get; set; } public DateTime ConvertedAtUtc { get; set; } diff --git a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Helpers/Autocorrector.cs b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Helpers/Autocorrector.cs index c273908d..7cc89247 100644 --- a/CDPIUI.Core/ComponentServices/Helpers/Configuration/Helpers/Autocorrector.cs +++ b/CDPIUI.Core/ComponentServices/Helpers/Configuration/Helpers/Autocorrector.cs @@ -44,47 +44,14 @@ public static string FindAutoCorrectPath(string filePath, ConfigItem configItem, return Path.Combine(Path.GetDirectoryName(configPath)!, filePath); } - var items = DatabaseHelper.Instance.GetItemsByType("configlist"); - foreach (var item in items) + // One item whose folder is gone must not end the search for all others. + foreach (string type in new[] { "configlist", "component", "addon" }) { - string itemPath = item.Directory; - var files = Directory.EnumerateFiles( - itemPath!, - $"*{Path.GetExtension(filePath)}", - SearchOption.AllDirectories); - foreach (var file in files) + foreach (var item in DatabaseHelper.Instance.GetItemsByType(type)) { - if (Path.GetFileName(filePath) == Path.GetFileName(file)) - return file; - } - } - var components = DatabaseHelper.Instance.GetItemsByType("component"); - foreach (var item in components) - { - string itemPath = item.Directory; - var files = Directory.EnumerateFiles( - itemPath!, - $"*{Path.GetExtension(filePath)}", - SearchOption.AllDirectories); - foreach (var file in files) - { - if (Path.GetFileName(filePath) == Path.GetFileName(file)) - return file; - } - } - var addOns = DatabaseHelper.Instance.GetItemsByType("addon"); - foreach (var item in addOns) - { - string itemPath = item.Directory; - var files = Directory.EnumerateFiles( - itemPath!, - $"*{Path.GetExtension(filePath)}", - SearchOption.AllDirectories); - foreach (var file in files) - { - - if (Path.GetFileName(filePath) == Path.GetFileName(file)) - return file; + string? found = FindInItem(item.Directory, filePath); + if (found != null) + return found; } } } @@ -94,5 +61,21 @@ public static string FindAutoCorrectPath(string filePath, ConfigItem configItem, } return string.Empty; } + + private static string? FindInItem(string? itemPath, string filePath) + { + if (string.IsNullOrEmpty(itemPath) || !Directory.Exists(itemPath)) + return null; + try + { + string fileName = Path.GetFileName(filePath); + return Directory.EnumerateFiles(itemPath, $"*{Path.GetExtension(filePath)}", SearchOption.AllDirectories) + .FirstOrDefault(file => string.Equals(Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase)); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + return null; + } + } } } diff --git a/CDPIUI.Core/ComponentServices/ProcessService.cs b/CDPIUI.Core/ComponentServices/ProcessService.cs index 7ce630bd..d8214ba2 100644 --- a/CDPIUI.Core/ComponentServices/ProcessService.cs +++ b/CDPIUI.Core/ComponentServices/ProcessService.cs @@ -1,10 +1,10 @@ using CDPIUI.Core.Basic; using CDPIUI.Core.Communication; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Core.Store.Database; using CDPIUI.Shared.ComponentsTask; using CDPIUI.Shared.Exceptions.Database; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; @@ -79,12 +79,22 @@ public async Task StartProcess() ProcessName = DatabaseHelper.Instance.GetItemById(Id)?.Executable?.FirstCharToUpper() ?? string.Empty; + if (componentHelper == null) + throw new InvalidOperationException($"Component {Id} is not installed or its executable is missing."); + var exePath = componentHelper.GetExecutablePath(); var workingDirectory = componentHelper.GetDirectory(); - string args = SetupProxy(componentHelper.GetStartupParams(), Id); + string startupParams = componentHelper.GetStartupParams(); + // GoodbyeDPI started with its defaults and winws exited at once when no preset was + // selected or the preset gave an empty line. + if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath)) + throw new FileNotFoundException($"The executable of {Id} was not found.", exePath); + if (string.IsNullOrWhiteSpace(startupParams)) + throw new InvalidOperationException("No preset is selected for this component, or the preset has no startup parameters."); + string args = SetupProxy(startupParams, Id); var component = HardcodedItemIds.ComponentIds.GetKeyByValue(Id); - if (component == Components.NoDPI) args = args.Replace("--quiet", "") + "--quiet"; + if (component == Components.NoDPI) args = AppendQuietFlag(args); Logger.Instance.CreateDebugLog(nameof(ProcessService), $"Args is {args}"); @@ -113,14 +123,14 @@ public async Task StartProcess(string args, string? _reserved = null) // TODO: r ComponentHelper componentHelper = ComponentItemsLoaderHelper.Instance.GetComponentHelperFromId(Id); - ProcessName = DatabaseHelper.Instance.GetItemById(Id).Executable?.FirstCharToUpper() ?? Id; + ProcessName = DatabaseHelper.Instance.GetItemById(Id)?.Executable?.FirstCharToUpper() ?? Id; var exePath = componentHelper.GetExecutablePath(); var workingDirectory = componentHelper.GetDirectory(); args = SetupProxy(args, Id); var component = HardcodedItemIds.ComponentIds.GetKeyByValue(Id); - if (component == Components.NoDPI) args = args.Replace("--quiet", "") + "--quiet"; + if (component == Components.NoDPI) args = AppendQuietFlag(args); Logger.Instance.CreateDebugLog(nameof(ProcessService), $"Args is {args}"); @@ -295,18 +305,35 @@ public void MarkAsFinished() ErrorHappens?.Invoke(LastError); } } + private const int MaxOutputLength = 2 * 1024 * 1024; + + private static void TrimToLimit(StringBuilder buffer) + { + if (buffer.Length > MaxOutputLength) + buffer.Remove(0, buffer.Length - MaxOutputLength / 2); + } + public void ChangeProcName(string name) { ProcessName = name; ProcessNameChanged?.Invoke(name); } + // An escape sequence split between two chunks is cleaned once the rest arrives. + private string _pendingEscape = string.Empty; + public void AddOutput(string output) { - string prettyOutput = OutputCleanupHelper.ReplaceSymbols(output); + string prettyOutput; lock (outputLock) { + string chunk = _pendingEscape + output; + _pendingEscape = OutputCleanupHelper.TakeIncompleteTail(ref chunk); + prettyOutput = OutputCleanupHelper.ReplaceSymbols(chunk); _outputDefaultBuffer.Append(output); _outputBuffer.Append(prettyOutput); + // A component running with --debug for hours filled memory: only the end is kept. + TrimToLimit(_outputDefaultBuffer); + TrimToLimit(_outputBuffer); } OutputReceived?.Invoke(prettyOutput); @@ -321,11 +348,21 @@ public void SetOutput(string output) lock (outputLock) { _outputDefaultBuffer.Clear().Append(output); + _pendingEscape = string.Empty; _outputBuffer.Clear().Append(OutputCleanupHelper.ReplaceSymbols(output)); } OutputReset?.Invoke(); } + public static string AppendQuietFlag(string args) + { + string withoutQuiet = QuietFlagRegex().Replace(args, string.Empty).Trim(); + return withoutQuiet.Length == 0 ? "--quiet" : $"{withoutQuiet} --quiet"; + } + + [GeneratedRegex(@"(? + /// Set when an installed package (a driver or runtime MSI) asked for a system restart + /// and the user has not been told yet. + /// + public bool IsRestartPending { get; private set; } + + public event Action? RestartRequired; + + public void InvokeRestartRequired(string itemId) + { + Logger.Instance.CreateInfoLog(nameof(CoreEvents), $"{itemId} requested a system restart"); + IsRestartPending = true; + RestartRequired?.Invoke(itemId); + } + + public void AcknowledgeRestart() => IsRestartPending = false; } } diff --git a/CDPIUI.Core/Data/Directories.cs b/CDPIUI.Core/Data/Directories.cs index 677730f5..36d7f86d 100644 --- a/CDPIUI.Core/Data/Directories.cs +++ b/CDPIUI.Core/Data/Directories.cs @@ -96,9 +96,11 @@ private string TryGetDataDirectory() } else { - DataDirectoryProperty = string.Empty; + // An empty path made every file go to the current working directory. + DataDirectoryProperty = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CDPIUI"); CoreEvents.Instance.InvokeCriticalCoreExceptionHappens(result.Error ?? new UnknownException()); - return string.Empty; + return DataDirectoryProperty; } } diff --git a/CDPIUI.Core/Features/ApplicationUpdate.cs b/CDPIUI.Core/Features/ApplicationUpdate.cs index 52cdd48f..47a754e4 100644 --- a/CDPIUI.Core/Features/ApplicationUpdate.cs +++ b/CDPIUI.Core/Features/ApplicationUpdate.cs @@ -3,7 +3,7 @@ using CDPIUI.Core.Store; using CDPIUI.Core.Store.Repository; using CDPIUI.Shared; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System.Diagnostics; namespace CDPIUI.Core.Features @@ -106,13 +106,13 @@ public async Task CheckForUpdates(bool notify = false) ErrorHappened = true; ErrorInfo = _data.Error.ErrorCode; ErrorHappens?.Invoke(); + // The UI waits for Completed to leave the "checking" state. + CheckForUpdatesCompleted?.Invoke(); return false; } - Version serverVersion = new(_data.Result.ReleaseTag!); - Version currentVersion = new(ApplicationInfo.Version); - - if (serverVersion > currentVersion) + // Release tags such as v1.2 or 1.3-preview do not parse as System.Version. + if (Store.VersionHelper.CompareVersionStrings(ApplicationInfo.Version, _data.Result.ReleaseTag) < 0) { ServerVersion = _data.Result.ReleaseTag!; IsUpdateAvailable = true; diff --git a/CDPIUI.Core/Features/FileSystemLinksManager.cs b/CDPIUI.Core/Features/FileSystemLinksManager.cs index aed9e9c5..0f4f2b8e 100644 --- a/CDPIUI.Core/Features/FileSystemLinksManager.cs +++ b/CDPIUI.Core/Features/FileSystemLinksManager.cs @@ -28,16 +28,7 @@ public static async Task> CreateSymbolicLinkFo { try { - if (File.Exists(linkTo)) - { - string backupFilePath = BackupFile(linkTo); - Debug.WriteLine($"{itemId}, {linkFrom}"); - SettingsManager.Instance.SetValue( - ["HARDLINKS", itemId, linkTo], "backupFile", backupFilePath); - File.Delete(linkTo); - } - - File.CreateSymbolicLink(linkTo, linkFrom); + ReplaceWithLink(itemId, linkTo, temporaryPath => File.CreateSymbolicLink(temporaryPath, linkFrom)); SettingsManager.Instance.SetValue( ["HARDLINKS", itemId, linkTo], "targetFile", linkFrom); @@ -67,13 +58,16 @@ public static async Task> RemoveLinkForItemId( Debug.WriteLine($">>> {linkTo} {itemId}"); string backupFile = SettingsManager.Instance.GetValue( ["HARDLINKS", itemId, linkTo], "backupFile"); - if (!File.Exists(backupFile)) + bool hadOriginal = !string.IsNullOrEmpty(backupFile) && backupFile != "NaN"; + if (hadOriginal && !File.Exists(backupFile)) { throw new FileNotFoundException( $"Backup file \"{backupFile}\" not found. Hardlink remove cannot be complete."); } File.Delete(linkTo); - File.Move(backupFile, linkTo); + // Without an original file the link is only removed. + if (hadOriginal) + File.Move(backupFile, linkTo); SettingsManager.Instance.SetValue(["HARDLINKS", itemId, linkTo], "backupFile", "NaN"); SettingsManager.Instance.SetValue(["HARDLINKS", itemId, linkTo], "targetFile", "NaN"); @@ -101,16 +95,12 @@ public static async Task> CreateHardLinkForIte { try { - if (File.Exists(linkTo)) + ReplaceWithLink(itemId, linkTo, temporaryPath => { - string backupFilePath = BackupFile(linkTo); - Debug.WriteLine($"{itemId}, {linkFrom}"); - SettingsManager.Instance.SetValue( - ["HARDLINKS", itemId, linkTo], "backupFile", backupFilePath); - File.Delete(linkTo); - } - - CreateHardLink(linkTo, linkFrom, nint.Zero); + // Fails, for example, between two volumes; that was reported as success. + if (!CreateHardLink(temporaryPath, linkFrom, nint.Zero)) + throw new global::System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); + }); SettingsManager.Instance.SetValue( ["HARDLINKS", itemId, linkTo], "targetFile", linkFrom); @@ -130,11 +120,38 @@ public static async Task> CreateHardLinkForIte } } - private static string BackupFile(string filePath) + /// + /// Creates the link under a temporary name first and replaces the file only when that + /// worked: the original was deleted before the link was created, so a failed + /// CreateSymbolicLink (no privilege without developer mode) left nothing in its place. + /// + private static void ReplaceWithLink(string itemId, string linkTo, Action createLink) { - string bakFilePath = $"{filePath}.bak"; - File.Copy(filePath, bakFilePath, true); - return bakFilePath; + string temporaryPath = $"{linkTo}.{Guid.NewGuid():N}.link"; + createLink(temporaryPath); + try + { + bool isExistingLink = IsFileLinked(itemId, linkTo) != null || + (File.Exists(linkTo) && (File.GetAttributes(linkTo) & FileAttributes.ReparsePoint) != 0); + if (isExistingLink && File.Exists(linkTo)) + { + // Linking again: the backup of the original file is already there. + File.Delete(linkTo); + } + else if (File.Exists(linkTo)) + { + string backupFilePath = $"{linkTo}.bak"; + File.Move(linkTo, backupFilePath, overwrite: true); + SettingsManager.Instance.SetValue( + ["HARDLINKS", itemId, linkTo], "backupFile", backupFilePath); + } + File.Move(temporaryPath, linkTo); + } + finally + { + if (File.Exists(temporaryPath)) + File.Delete(temporaryPath); + } } #region WINAPI diff --git a/CDPIUI.Core/LScript/LScriptCore.cs b/CDPIUI.Core/LScript/LScriptCore.cs index e4de9a1c..84803d44 100644 --- a/CDPIUI.Core/LScript/LScriptCore.cs +++ b/CDPIUI.Core/LScript/LScriptCore.cs @@ -2,7 +2,7 @@ using CDPIUI.Core.Communication; using CDPIUI.Core.ComponentServices; using CDPIUI.Core.Data; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System.Diagnostics; using System.Text.RegularExpressions; @@ -11,8 +11,11 @@ namespace CDPIUI.Core.LScript public class LScriptCore { - public const string ScriptGetArgsRegex = @"\$.*?\((.*?)\)"; - public const string Pattern = @"\$(STATICIMAGE|DYNAMICIMAGE|LOADDYNAMIC|GETCURRENTDIR|LOCALCONDITION|GETSRDIR)(?:\((.*?)\))?"; + // The argument takes balanced parentheses: "$LOCALCONDITION(a==true ? $GETCURRENTDIR()/x $SEPARATOR y)" + // ended at the first ")" before, and the condition was cut. + private const string BalancedArgument = @"((?>[^()]+|\((?)|\)(?<-depth>))*(?(depth)(?!)))"; + public const string ScriptGetArgsRegex = @"\$[A-Za-z_0-9]*\(" + BalancedArgument + @"\)"; + public const string Pattern = @"\$(STATICIMAGE|DYNAMICIMAGE|LOADDYNAMIC|GETCURRENTDIR|LOCALCONDITION|GETSRDIR)(?:\(" + BalancedArgument + @"\))?"; public const string ConditionPattern = "{0}==true ? {1} : {2}"; @@ -44,7 +47,7 @@ public static string GetScriptArgs(string scriptString, string scriptArgs = null } if (scriptArgs != null) - scriptData = Regex.Replace(scriptData, @"{.*?}", scriptArgs); + scriptData = Regex.Replace(scriptData, @"{.*?}", _ => scriptArgs); } return scriptData; } @@ -111,7 +114,7 @@ public virtual string ExecuteScriptWork( } catch (Exception ex) { - // pass + Logger.Instance.CreateWarningLog(nameof(LScriptCore), $"Script {scriptString} failed: {ex.Message}"); } return executeResult; @@ -121,7 +124,8 @@ public static string ExecuteScriptUnsafe( string scriptString, string? scriptArgs = null, string? callItemId = null, - Dictionary? jparams = null + Dictionary? jparams = null, + bool quotePaths = false ) { if (string.IsNullOrEmpty(scriptString)) @@ -140,6 +144,7 @@ public static string ExecuteScriptUnsafe( } + string source = result; result = Regex.Replace(result, Pattern, (Match m) => { string command = m.Groups[1].Value.ToUpperInvariant(); @@ -148,7 +153,7 @@ public static string ExecuteScriptUnsafe( if (!string.IsNullOrEmpty(scriptArgs)) { - scriptData = Regex.Replace(scriptData, @"{.*?}", scriptArgs); + scriptData = Regex.Replace(scriptData, @"{.*?}", _ => scriptArgs); } string replacement = m.Value; @@ -171,15 +176,19 @@ public static string ExecuteScriptUnsafe( break; case "GETCURRENTDIR": - replacement = localItemsFolder; + replacement = quotePaths ? QuoteIfNeeded(localItemsFolder, source, m.Index) : localItemsFolder; break; case "LOCALCONDITION": - replacement = LocalCondition(scriptData, jparams); + // The chosen branch can hold scripts of its own, such as $GETCURRENTDIR(). + replacement = ExecuteScriptUnsafe( + LocalCondition(scriptData, jparams), scriptArgs, callItemId, jparams, quotePaths); break; case "GETSRDIR": - replacement = Directories.StoreRepoCacheDirectory; + replacement = quotePaths + ? QuoteIfNeeded(Directories.StoreRepoCacheDirectory, source, m.Index) + : Directories.StoreRepoCacheDirectory; break; } } @@ -204,6 +213,37 @@ public static string ExecuteScriptUnsafe( + /// + /// A folder with a space (a user profile such as "C:\Users\Ivan Petrov") split the argument + /// it was put into (issue #2). It is quoted unless it already stands inside quotes; a quote in + /// the middle of an argument is valid for the command-line parser of the components. + /// + private static string QuoteIfNeeded(string path, string text, int position) + { + if (!path.Any(char.IsWhiteSpace)) + return path; + int quotesBefore = text.AsSpan(0, position).Count('"'); + return quotesBefore % 2 == 0 ? $"\"{path}\"" : path; + } + + /// + /// The ':' of "condition ? a : b", skipping the colon of a drive letter such as "C:\". + /// + private static int FindTernaryColon(string condition, int questionMark) + { + for (int i = questionMark + 1; i < condition.Length; i++) + { + if (condition[i] != ':') + continue; + bool isDriveColon = i > 0 && char.IsLetter(condition[i - 1]) && + (i == 1 || !char.IsLetterOrDigit(condition[i - 2])) && + i + 1 < condition.Length && (condition[i + 1] == '\\' || condition[i + 1] == '/'); + if (!isDriveColon) + return i; + } + return -1; + } + public static string LocalCondition(string condition, Dictionary? jparams) { if (string.IsNullOrWhiteSpace(condition)) @@ -217,7 +257,7 @@ public static string LocalCondition(string condition, Dictionary? condition = condition.Replace("$SEPARATOR", "$"); } else - cPos = condition.IndexOf(':'); + cPos = FindTernaryColon(condition, qPos); if (qPos < 0 || cPos < 0 || cPos < qPos) { Logger.Instance.CreateWarningLog(nameof(LScriptCore), $"0x0 Not correct condition"); @@ -239,7 +279,7 @@ public static string LocalCondition(string condition, Dictionary? var varName = parts[0].Trim(); var literal = parts[1].Trim(); - if (!jparams.TryGetValue(varName, out var varBool)) + if (jparams == null || !jparams.TryGetValue(varName, out var varBool)) { Logger.Instance.CreateWarningLog(nameof(LScriptCore), $"Param {varName} not exist"); return string.Empty; @@ -276,7 +316,7 @@ public static string LocalCondition(string condition, Dictionary? conditionString = conditionString.Replace("$SEPARATOR", "$"); } else - cPos = conditionString.IndexOf(':'); + cPos = FindTernaryColon(conditionString, qPos); if (qPos < 0 || cPos < 0 || cPos < qPos) { @@ -334,13 +374,20 @@ public static string LocalCondition(string condition, Dictionary? } string[] parts = scriptData.Split(", ", StringSplitOptions.RemoveEmptyEntries); + // An empty script, for example after a trailing ';'. + if (parts.Length == 0) + continue; string scriptName = parts[0]; string[] scriptArgs = parts.Skip(1).ToArray(); switch (scriptName) { case "finish_component_setup": - FinishComponentSetup(scriptArgs); + await FinishComponentSetup(scriptArgs); + break; + case "apply_config": + // Written by older preset pack builders. The preset lists are reloaded + // after every installation, so there is nothing left to do. break; case "download_easy_designer_annotation_file": if (scriptArgs.Length < 1) @@ -350,15 +397,14 @@ public static string LocalCondition(string condition, Dictionary? case "install_msi": if (scriptArgs.Length < 2) break; - LScriptMsiHandler.InstallMsi(scriptArgs, extraArgs, cancellationToken ?? default); + // Awaited, so a failed installation fails the item installation instead of the process. + await LScriptMsiHandler.InstallMsi(scriptArgs, extraArgs, cancellationToken ?? default); break; default: Logger.Instance.CreateWarningLog(nameof(LScriptCore), $"Unknown script command: {scriptName}"); break; } } - await Task.CompletedTask; - return result; } @@ -367,13 +413,13 @@ public static string CreateCondition(string varName, string onValue, string offV return string.Format(ConditionPattern, varName, onValue, offValue); } - private static void FinishComponentSetup(string[] args) + private static async Task FinishComponentSetup(string[] args) { if (args.Length < 1) return; string componentName = args[0]; - ComponentTasksManager.Instance.UpdateTaskList(); + await ComponentTasksManager.Instance.UpdateTaskList(); if (componentName == "byedpi" || componentName == "spoofdpi" || componentName == "nodpi") { diff --git a/CDPIUI.Core/LScript/LScriptMsiHandler.cs b/CDPIUI.Core/LScript/LScriptMsiHandler.cs index b54af4c1..d1fef755 100644 --- a/CDPIUI.Core/LScript/LScriptMsiHandler.cs +++ b/CDPIUI.Core/LScript/LScriptMsiHandler.cs @@ -8,12 +8,18 @@ namespace CDPIUI.Core.LScript { public class LScriptMsiHandler() { - public static async void InstallMsi( + public static async Task InstallMsi( string[] scriptArgs, Dictionary? extraArgs, CancellationToken cancellationToken) { - string path = Path.Combine( - extraArgs?.GetValueOrDefault("CurrentDirectory", string.Empty) ?? string.Empty, - scriptArgs[0]); + string itemDirectory = extraArgs?.GetValueOrDefault("CurrentDirectory", string.Empty) ?? string.Empty; + string path = Path.Combine(itemDirectory, scriptArgs[0]); + + // A script may only install packages that its own item brought. + string checkedPath = path.EndsWith("$ALL") ? path.Substring(0, path.Length - "$ALL".Length) : path; + if (string.IsNullOrEmpty(itemDirectory) || !IsInsideDirectory(checkedPath, itemDirectory)) + { + throw new MsiInstallException("The package is outside the item directory"); + } if (bool.TryParse(scriptArgs[1], out bool removeAfterAction)) { Debug.WriteLine(path); @@ -22,18 +28,14 @@ public static async void InstallMsi( var _result = await InstallAllMsiFromPath(path.Replace("$ALL", ""), removeAfterAction, cancellationToken); if (!_result.Item1) throw new MsiInstallException("Install failure"); if (_result.Item2) - { - // TODO: ask restart - } + CoreEvents.Instance.InvokeRestartRequired(Path.GetFileName(itemDirectory.TrimEnd('\\', '/'))); } else { var _result = await InstallMsi(path, removeAfterAction, cancellationToken); if (!_result.Item1) throw new MsiInstallException("Install failure"); if (_result.Item2) - { - // TODO: ask restart - } + CoreEvents.Instance.InvokeRestartRequired(Path.GetFileName(itemDirectory.TrimEnd('\\', '/'))); } } else @@ -42,10 +44,18 @@ public static async void InstallMsi( } } + private static bool IsInsideDirectory(string path, string directory) + { + string root = Path.GetFullPath(directory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + string fullPath = Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + return fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase); + } + private static async Task> InstallAllMsiFromPath(string directory, bool removeAfterAction, CancellationToken cancellationToken) { bool requestRestart = false; - foreach (string filepath in Directory.EnumerateFiles(directory)) + // Only installers: a readme or a .cab next to them failed the whole pack. + foreach (string filepath in Directory.EnumerateFiles(directory, "*.msi")) { var result = await InstallMsi(filepath, removeAfterAction, cancellationToken); if (!result.Item1) return Tuple.Create(false, false); diff --git a/CDPIUI.Core/LScript/README.md b/CDPIUI.Core/LScript/README.md new file mode 100644 index 00000000..ddee4d94 --- /dev/null +++ b/CDPIUI.Core/LScript/README.md @@ -0,0 +1,259 @@ +# LScript reference + +LScript is a small expression language. Store metadata, preset variables and preset startup strings use it. +The implementation is in `CDPIUI.Core/LScript`: + +| File | Content | +|---|---| +| `LScriptCore.cs` | Value functions (`ExecuteScript`, `ExecuteScriptUnsafe`, `LocalCondition`) and install actions (`RunScript`). | +| `DefaultMessageHandler.cs` | Path and file helpers for `$STATICIMAGE`, `$DYNAMICIMAGE` and `$LOADDYNAMIC`. | +| `LScriptMsiHandler.cs` | The install action `install_msi`. | + +The GUI adds one function in `CDPIUI/Helper/LScript/LScriptLangHelper.cs` (see "GUI extension"). + +LScript has two parts: + +- Value functions. They replace a `$FUNCTION(argument)` text with a value. +- Install actions. They run commands after the Store downloads an item. + +## Value functions + +| Function | Result | +|---|---| +| `$STATICIMAGE(path)` | `ms-appx:///Assets/`, an image in the GUI package. | +| `$DYNAMICIMAGE(path)` | `\`. `` is the script argument when it is given, otherwise `Store\Cache\Repo`. The function does not read the file. | +| `$LOADDYNAMIC(path)` | The text of the file `Store\Cache\Repo\`. | +| `$GETCURRENTDIR()` | The folder of the calling item, `Store\Items\`. Without `callItemId`, the folder `Store\Items`. | +| `$GETSRDIR` | The Store repository cache folder, `Store\Cache\Repo`. Only `ExecuteScriptUnsafe` knows this function. | +| `$LOCALCONDITION(condition)` | One of two values. The value of a boolean in `jparams` selects it. See "Conditions". | +| `$EMPTY` | An empty string. The library removes it before it evaluates the other functions. | + +`Store\...` folders are in the data directory. See `CDPIUI.Core/README.md`, section "Data directories". + +### Placeholders in arguments + +If the caller gives `scriptArgs`, each `{...}` text in the argument is replaced with `scriptArgs`. +The GUI uses this to select a language file. Examples: + +```text +$LOADDYNAMIC(items/zapret/description_{lang}.md) scriptArgs = "EN" -> text of Store\Cache\Repo\items\zapret\description_EN.md +$DYNAMICIMAGE(icon.png) scriptArgs = "C:\Temp\ap123" -> C:\Temp\ap123\icon.png +``` + +The file names in these examples are examples only. `LocalItemsInstallerHelper` uses the second form. +It gives the temporary pack folder as `scriptArgs` and reads the icon of a local pack. +When the Store registers a local pack, `StoreHelper.GetIconPath` keeps the icon as a full path in the item folder. +It does this only when the file exists in the item folder. Otherwise it keeps the `$DYNAMICIMAGE(...)` text. + +## Evaluation modes + +The library has two evaluation methods. They do not give the same results. + +| Property | `ExecuteScript` | `ExecuteScriptUnsafe` | +|---|---|---| +| Where the function can be | Only at the start of the string. The full value must start with `$`. | Anywhere in the string. All occurrences are replaced. | +| Number of functions | One. Text after the call is ignored, except for `$GETCURRENTDIR()`. | All matches. | +| Function names | Case-sensitive. | Not case-sensitive. | +| `$GETSRDIR` | Not supported. | Supported. | +| `$GETCURRENTDIR()` | The item folder plus all text after `$GETCURRENTDIR()`. | Only the item folder. The text after it stays. | +| `$LOCALCONDITION` result | The selected value, not expanded. | The selected value, expanded again with the same arguments. | +| Error | The input string is returned (without `$EMPTY`). | The text of the failed match stays. | +| Path quotes | No. | Optional (`quotePaths`). | + +A string that does not start with `$` goes through `ExecuteScript` unchanged, but without `$EMPTY`. +An unknown `$NAME(...)` also stays unchanged. + +### Where each mode is used + +| Caller | Mode | Arguments | +|---|---|---| +| Preset variable values (`ConfigurationService.GetReadyToUseVariables`) | `ExecuteScript` | `callItemId` = pack id, `jparams` of the preset | +| Preset startup string after variable replacement (`GetStartupParametersByConfigItem`) | `ExecuteScriptUnsafe` | `callItemId` = pack id, `quotePaths: true` | +| File search for presets (`Autocorrector`) | `ExecuteScriptUnsafe` | `callItemId` = pack id | +| Store icons and texts in the GUI | `ExecuteScript` through `LScriptLangHelper` | the language code as `scriptArgs` for texts | + +### Sequence for a startup string + +The library expands preset variables before it expands LScript in the startup string: + +1. Each `%NAME%=value` gives `NAME` = `ExecuteScript(value)`. A value that starts with `$GETCURRENTDIR` or `$GETSRDIR` (not case-sensitive, after white space) stays unchanged. +2. The library replaces `%NAME%` in the startup string (16 passes maximum). +3. The library replaces `commaVars`. +4. `ExecuteScriptUnsafe` expands the remaining functions, for example `$GETCURRENTDIR()`. + +Step 1 evaluates the other values that start with `$`. A value that does not start with `$` stays as text. +Step 4 then expands the remaining functions, with path quotes. Thus a folder path with spaces gets quotes also when a variable value starts with `$GETCURRENTDIR()`. + +Example of a preset in the pack `MyPack`: + +```json +{ + "target": ["CSZTBN012"], + "jparams": { "useGameFilter": true }, + "variables": [ + "%GAMEFILTER%=$LOCALCONDITION(useGameFilter==true ? 1024-65535 $SEPARATOR 12)", + "%LIST%=--hostlist=$GETCURRENTDIR()/lists/list-general.txt" + ], + "startup_string": "--wf-udp=443,%GAMEFILTER% %LIST%" +} +``` + +Result, when the data directory has no spaces: +`--wf-udp=443,1024-65535 --hostlist=\Store\Items\MyPack/lists/list-general.txt`. + +## Path quotes + +`ExecuteScriptUnsafe` with `quotePaths: true` adds quotes to the result of `$GETCURRENTDIR()` and `$GETSRDIR`. These conditions apply: + +- The folder path contains a white-space character, for example `C:\Users\Ivan Petrov\...`. +- The number of `"` characters before the function is even. Thus the function is not already in quotes. + +Only the folder is in quotes. The rest of the argument stays after the closing quote: + +```text +--hostlist=$GETCURRENTDIR()/lists/a.txt -> --hostlist="C:\Users\Ivan Petrov\CDPIUI\Store\Items\MyPack"/lists/a.txt +--hostlist="$GETCURRENTDIR()/lists/a.txt" -> --hostlist="C:\Users\Ivan Petrov\CDPIUI\Store\Items\MyPack/lists/a.txt" +``` + +The components accept a quote in the middle of an argument (see the comment on `QuoteIfNeeded`). +`ExecuteScript` never adds quotes. + +## Conditions + +Syntax: + +```text +$LOCALCONDITION(== ? $SEPARATOR ) +$LOCALCONDITION(== ? : ) +``` + +Rules (`LScriptCore.LocalCondition`): + +- `` is a key of `jparams`. The condition is true when `jparams[name]` equals the literal. +- The literal must parse as a .NET `bool` (`true`, `false`, not case-sensitive). +- The first `?` ends the condition. The condition cannot contain `?`. +- When the text contains `$SEPARATOR`, the first `$SEPARATOR` separates the two values. +- Without `$SEPARATOR`, the first `:` after `?` separates the values. A drive colon (a letter, then `:`, then `\` or `/`) is not a separator. +- The library removes white space at the start and end of each value. It removes `$EMPTY` from the selected value. + +Results for problems: + +| Problem | Result | +|---|---| +| No `?`, no separator, or the separator is before `?` | An empty string and a warning in the log. | +| No `==`, or more than one `==` | An empty string and a warning. | +| `` is not in `jparams` | An empty string and a warning. | +| The literal is not `true` or `false` | An exception. The caller returns the original text. | +| `jparams` is `null` | An empty string and a warning. | + +Use `$SEPARATOR` when a value contains `:`. The ConfigMaker and the ConfigImport add-on always write `$SEPARATOR`. + +Examples from the code and tests: + +```text +$LOCALCONDITION(useGameFilter==true ? 1024-65535 $SEPARATOR 12) +useA==true?C:\lists\a.txt:D:\lists\b.txt (test of LocalCondition: drive colons are skipped) +$LOCALCONDITION(useFake==false ? $EMPTY $SEPARATOR --dpi-desync=fake) +``` + +The test is `CDPIUI.Tests/Core/LScriptCoreTests.cs`. The third line is an example only. + +### Condition helpers + +- `CreateCondition(name, on, off)` returns `"==true ? : "`. It does not add `$LOCALCONDITION(...)`. +- `GetNameOnOffValuesFromConditionString(expression)` reads `%VAR%=$LOCALCONDITION(param==true ? on $SEPARATOR off)`. + It returns the tuple (`param`, `VAR`, `on`, `off`). The editors use it to show switches. + Without `$SEPARATOR`, it uses the first `:` after `?`. It skips drive colons, as `LocalCondition` does. + +## Install actions + +The Store runs install actions with `LScriptCore.RunScript`. They come from two item fields: + +- `before_install_actions`: run after the download and before the database registration. +- `after_install_actions`: run after the database registration. + +The Store gives the item folder as `CurrentDirectory` to the two lists. + +Only Store items and signed local packs (`.cdpisignedpack`) have install actions. +The library ignores the actions of a pack that is not signed. + +### Syntax + +```text +$(, , );$(, ...) +``` + +- `;` separates the actions. An empty action (for example after the last `;`) is skipped. +- The text inside the outer parentheses of the action holds the command and the arguments. +- `, ` (a comma and one space) separates the command and the arguments. A comma without a space does not separate them. +- The name between `$` and `(` is not checked. Only the command name is important. +- An unknown command gives a warning in the log. The next action then runs. +- A cancellation stops the sequence before the next action. + +### Commands + +| Command | Arguments | Effect | +|---|---|---| +| `finish_component_setup` | component name | Calls `ComponentTasksManager.UpdateTaskList`. For `byedpi`, `spoofdpi` or `nodpi`, it sends the `ProxySetupRequired` notification when `PROXY/proxyType` is `None`. | +| `install_msi` | path, `true`/`false` | Installs an MSI through the tray. The second argument tells if the library deletes the MSI after the installation. | +| `download_easy_designer_annotation_file` | HTTPS URL | Adds `DOWNLOAD=$SEPARATORedannotationfile;` to the result of `RunScript`. | +| `apply_config` | any | No operation. Older pack builders write it. The preset lists load again after each installation. | + +The names in the first column are case-sensitive and lower case. +The component names of `finish_component_setup` are also case-sensitive. + +#### `install_msi` + +- The path is relative to the item folder (`CurrentDirectory`). +- The full path must stay inside the item folder. Otherwise the action throws `MsiInstallException`. +- A path that ends with `$ALL` installs each `*.msi` file in that folder. Subfolders are not included. +- The second argument must parse as a `bool`. Otherwise the action throws `MsiInstallException`. +- A failed installation throws an exception. Then the item installation fails. +- An installation can take a maximum of 30 minutes (`MsiInstallerService`). +- If the MSI requests a system restart, `CoreEvents.RestartRequired` gets the name of the item folder. + +```text +$RUN(install_msi, drivers/WinDivert.msi, true) +$RUN(install_msi, redist/$ALL, false);$RUN(finish_component_setup, byedpi) +``` + +`RUN` and the file names are examples only. Any name is accepted between `$` and `(`. + +#### `download_easy_designer_annotation_file` + +The command does not download the file. It returns a `DOWNLOAD=` entry. +`OnlineItemsInstallationService.LaunchBeforeInstallActions` reads the result of the two action lists: + +- The URL must be an absolute HTTPS URL. Otherwise the download fails. +- The file goes into the item folder with the name `edannotationfile` and the extension of the URL file. +- A URL that ends with `.zip` is extracted into the item folder. + +If a download from `before_install_actions` fails, the item installation fails. +If a download from `after_install_actions` fails, the library writes a warning to the log. The item stays installed. + +## GUI extension + +`LScriptLangHelper.ExecuteScript` (in the `CDPIUI` project) adds one function: + +| Function | Result | +|---|---| +| `$Q_LINK(key)` | The localized GUI string `` from the WinUI3Localizer resources. | + +All other strings go to `LScriptCore.ExecuteScriptWork`. + +The GUI also uses `GetScriptArgs` and `GetArgumentsFromScript` to read the argument of a `$NAME(...)` text, for example a URL. + +## Preset name keys + +Preset names are not LScript. `ConfigurationService` replaces each `$WORD(key)` in the `name` of a preset. +The replacement is the value of `key` in the localization file of the pack (`localized_strings_directory` in `init.json`). +When a preset is saved, the file keeps the original name with the keys. + +## Limits + +- The parentheses in an argument must be balanced. An argument can contain a call, for example `$LOCALCONDITION(useA==true ? $GETCURRENTDIR()/a.txt $SEPARATOR b.txt)`. +- Only `ExecuteScriptUnsafe` expands more than one function in a string. It also expands the functions in the selected value of `$LOCALCONDITION`. + `ExecuteScript` returns the selected value without expansion. In a startup string, step 4 then expands it. +- A condition can test only one boolean with `==`. It has no `!=`, `&&` or `||`. +- `$LOADDYNAMIC` reads only from the Store repository cache. It does not use `scriptArgs` as the base folder. +- An action argument cannot contain `, ` or `;`, because these characters are separators. diff --git a/CDPIUI.Core/Proxy/ProxyHelper.cs b/CDPIUI.Core/Proxy/ProxyHelper.cs index f388a983..84223cf2 100644 --- a/CDPIUI.Core/Proxy/ProxyHelper.cs +++ b/CDPIUI.Core/Proxy/ProxyHelper.cs @@ -13,42 +13,83 @@ public enum ProxySetupTypes public static class ProxyHelper { - public static List Tokens = new() { "-p", "--port", "-i", "--ip", "-addr", "--host" }; + public static List Tokens = new() { "-p", "--port", "-i", "--ip", "-addr", "-port", "--host" }; - public static string ReplaseIp(string args) + /// + /// Removes the address and port arguments, which the application sets itself + /// + public static string ReplaceIp(string args) { - string[] splittedArgs = args.Split(' '); + List tokens = SplitArguments(args); string finalArgs = string.Empty; - for (int i = 0; i < splittedArgs.Length; i++) + for (int i = 0; i < tokens.Count; i++) { - var spA = splittedArgs[i].Split("="); - string token = spA[0]; - string value = spA.Length > 1 ? spA[1] + " " : string.Empty; - if (Tokens.Contains(token)) + string token = tokens[i]; + int separator = token.IndexOf('='); + string name = separator >= 0 ? token[..separator] : token; + if (Tokens.Contains(name)) { - if (splittedArgs[i].Contains('=')) - { - continue; - } - else - { + // "--port 8080" takes the next token as its value, "--port=8080" does not. + if (separator < 0) i++; - continue; - } - } - if (!string.IsNullOrEmpty(value)) - { - finalArgs += $"{token}={value}"; - } - else - { - finalArgs += $"{token} "; + continue; } + // The token is kept whole: "--x=a=b" and quoted paths with spaces stay intact. + finalArgs += $"{token} "; } return finalArgs; } + // Whitespace separates arguments except inside double quotes, as on the command line. + private static List SplitArguments(string args) + { + var tokens = new List(); + var current = new global::System.Text.StringBuilder(); + bool quoted = false; + foreach (char c in args ?? string.Empty) + { + if (c == '"') + quoted = !quoted; + + if (char.IsWhiteSpace(c) && !quoted) + { + if (current.Length > 0) + { + tokens.Add(current.ToString()); + current.Clear(); + } + continue; + } + current.Append(c); + } + if (current.Length > 0) + tokens.Add(current.ToString()); + return tokens; + } + + /// + /// Whether a proxy component listening on is reachable only from this computer. + /// Any other address, including 0.0.0.0, opens the proxy to the network without authentication. + /// + public static bool IsLoopbackAddress(string? ip) + { + string value = (ip ?? "").Trim().Trim('[', ']'); + if (string.Equals(value, "localhost", StringComparison.OrdinalIgnoreCase)) + return true; + return global::System.Net.IPAddress.TryParse(value, out var address) + && global::System.Net.IPAddress.IsLoopback(address); + } + + /// + /// Whether is a TCP port number. Any text was accepted and + /// written into the system proxy settings and app-config.json. + /// + public static bool IsValidPort(string? port) => + int.TryParse((port ?? string.Empty).Trim(), global::System.Globalization.NumberStyles.None, + global::System.Globalization.CultureInfo.InvariantCulture, out int value) && + value is >= 1 and <= 65535; + public static List ProxyLikeComponents = [ HardcodedItemIds.ComponentIds[Components.ByeDPI], HardcodedItemIds.ComponentIds[Components.NoDPI], @@ -61,7 +102,7 @@ internal static string ReplaceArgsForProxy( string port, string componentId) { - string finalArgs = ReplaseIp(args); + string finalArgs = ReplaceIp(args); if (componentId == "CSSIXC048") finalArgs = $"-addr={ip} -port={port} " + finalArgs; else if (componentId == "CSNIG9025") diff --git a/CDPIUI.Core/Proxy/WindowsNativeProxySettingsService.cs b/CDPIUI.Core/Proxy/WindowsNativeProxySettingsService.cs index 8d4a12b0..f5cc7363 100644 --- a/CDPIUI.Core/Proxy/WindowsNativeProxySettingsService.cs +++ b/CDPIUI.Core/Proxy/WindowsNativeProxySettingsService.cs @@ -45,6 +45,38 @@ public static Dictionary ReadProxySettings() return result; } + public static bool TryParseProxyServer(string? proxyServer, out string host, out string port) + { + host = string.Empty; + port = string.Empty; + if (string.IsNullOrWhiteSpace(proxyServer)) + return false; + + string entry = proxyServer.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .FirstOrDefault() ?? string.Empty; + int schemeSeparator = entry.IndexOf('='); + if (schemeSeparator >= 0) + entry = entry[(schemeSeparator + 1)..]; + + int portSeparator = entry.LastIndexOf(':'); + if (entry.StartsWith('[')) + { + int closingBracket = entry.IndexOf(']'); + if (closingBracket < 0 || portSeparator < closingBracket) + return false; + host = entry[1..closingBracket]; + } + else + { + if (portSeparator <= 0 || entry.IndexOf(':') != portSeparator) + return false; + host = entry[..portSeparator]; + } + + port = entry[(portSeparator + 1)..]; + return host.Length > 0 && port.Length > 0; + } + public static bool IsProxyEnabled() { try diff --git a/CDPIUI.Core/README.md b/CDPIUI.Core/README.md new file mode 100644 index 00000000..de9faff1 --- /dev/null +++ b/CDPIUI.Core/README.md @@ -0,0 +1,268 @@ +# CDPIUI.Core + +`CDPIUI.Core` is a .NET 8 class library. It contains the logic of the GUI process +that does not use WinUI: components, presets, the Store, the pipe client and settings. +The library does not start component processes. The elevated tray process +(`CDPIUI.TrayIcon`) starts them when the library sends a request through the pipe. + +## Projects that use this library + +| Project | Reference | +|---|---| +| `CDPIUI` (GUI) | `CDPIUI/CDPIUI.csproj` | +| `CDPIUI.AddOns` | `CDPIUI.AddOns/CDPIUI.AddOns.csproj` | +| `CDPIUI.Tests` | `CDPIUI.Tests/CDPIUI.Tests.csproj`; internal types are visible to it (`InternalsVisibleTo`) | + +`CDPIUI.TrayIcon` does not use `CDPIUI.Core`. Both processes use `CDPIUI.Shared`. +The library uses the packages `Microsoft.Data.Sqlite`, `Newtonsoft.Json` and `Semver`. + +## General rules + +- Most services are lazy singletons with a static `Instance` property and a lock. + Examples are `SettingsManager`, `StoreHelper`, `DatabaseHelper` and `ComponentTasksManager`. +- The namespace `CDPIUI.Core.System` hides the namespace `System` in code inside `CDPIUI.Core`. + Use `global::System...` when the short name is not clear, for example + `global::System.IO.Directory.Exists` in `ComponentServices/Helpers/ComponentHelper.cs`. +- `ConfigurationService` is in the namespace `CDPIUI.Core.ComponentServices.Configuration`. + Its file is in the folder `ComponentServices/Helpers/Configuration`. +- Operations that can fail usually return `OperationResultModel` (from `CDPIUI.Shared`). + Do not throw exceptions to the UI when a result model is available. + +## Root files + +| File | Responsibility | +|---|---| +| `Application.cs` | `ApplicationInfo`: the UI language (`Localization`, default `en-US`) and `Version`. `Version` is the file version of the entry assembly, read one time. | +| `CoreEvents.cs` | `CoreEvents`: the event `CriticalCoreExceptionHappens` (the last error is in `LastCriticalError`) and the event `RestartRequired`. `IsRestartPending` stays `true` until the UI calls `AcknowledgeRestart`. | +| `SettingsManager.cs` | `SettingsManager`: XML settings in `Settings/Settings.xml`. A missing key gets its default value from `Template/Settings/Settings.xml` in the application folder. | +| `State.cs` | `State`: the installation kind, `IsApplicationBuildAsSingleFile` (portable) and `IsApplicationBuildAsMsi`. See "Installation kind". | + +Settings use a group and a key, for example `GetValue("PROXY", "proxyType")`. +Component settings use a group path, for example `["CONFIGS", componentId]`. + +### Installation kind + +`State` gives the installation kind: + +- The `SingleFileRelease` configuration defines `SINGLEFILE`. The installation is then always portable. +- The `MsiRelease` configuration defines `MSIFILE`. The installation is then always an MSI installation. +- Without these symbols (for example `Release` or `Debug`), `State` finds the kind at run time, one time. + The MSI writes its installation folder to the registry value `HKLM\Software\Storik4\CDPI UI\InstallLocation` (64-bit view, component `InstallLocationRegistration` in `NewSetup/Product.wxs`). + If this value is equal to the application folder (not case-sensitive), the installation is an MSI installation. Otherwise it is portable. + +The release workflow packs the same `Release` build as the MSI and as the portable ZIP. Thus the run-time check is necessary. +The tray uses the same registry check (`CDPIUI.TrayIcon/Helper/Utils.cs`). +The installation kind selects the data directory, the update file (`patch.cdpipatch` or `patch.msi`) and the Store file type of the application update. + +## Data directories + +`Data/Directories.cs` gives all paths. Use these properties. Do not make paths by hand. + +| Property | Path | +|---|---| +| `CurrentDirectory` | The folder of the application executable. | +| `DataDirectory` | `CurrentDirectory` when the process can write to it and the installation is not an MSI installation (`State.IsApplicationBuildAsMsi`). Otherwise `%LOCALAPPDATA%\CDPIUI`. | +| `SettingsFilePath` | `\Settings\Settings.xml` | +| `TemplateSettingsFilePath` | `\Template\Settings\Settings.xml` | +| `StoreItemsDirectory` | `\Store\Items`. Each installed item has the folder `Items\`. | +| `StoreLocalUserItemDirectory` | `Store\Items\LocalUserStorage` (user presets, with the subfolders `List`, `Bin` and `Loc`). | +| `StoreLocalCacheDirectory` | `Store\Cache\Local`. It contains the item database `storedata.db`. | +| `StoreRepoCacheDirectory` | `Store\Cache\Repo`. It contains the extracted Store repository. | +| `TempFilesDirectory` | `\TempFiles` | +| `DownloadManagerDirectory` | `TempFiles\Downloads` | +| `ELUADirectory` | `\ELUA` | + +If `DataDirectory` cannot be found, the library uses `%LOCALAPPDATA%\CDPIUI` and raises +`CriticalCoreExceptionHappens`. + +## Basic + +- `Logger`: the log singleton (`LoggerBase` from `CDPIUI.Shared`) in `DataDirectory`. + Debug builds write all levels. Other builds use the setting `DEBUG/logLevel` (default `WARN`). +- `ErrorsHelper.Convertor` (`CustomErrorConvertor`): converts exceptions to error codes and `ErrorModel`. + An `HttpRequestException` with a status code gives `UNEXPECTED_STATUS_CODE`. + +## Communication + +The GUI is a pipe client. The tray process is the pipe server. + +- `PipeClientService`: connects to the pipe `SharedConstants.PipeName`. + - It waits 2 seconds. If the tray process runs, it waits 15 more seconds. + - If there is no connection, it starts `CDPIUI_TrayIcon.exe` as administrator (`runas`) and stops the GUI process. + The tray gets `--autorun` or `--show-ui`. It also gets the GUI activation arguments when they are present. + - It closes the connection and stops the GUI when the server is not the expected `CDPIUI_TrayIcon.exe` (`PipePeerVerifier`). + - It sends the auth packet with `Secret.AuthGuid`. + - A change in a settings group `CONFIGS/` sends `ProcessIdStartupArgsChanged` and the setup state of the component. +- `PipeHelper`: static methods that send typed packets (settings, update, notification, ConPTY, MSI, proxy, conditional launch). +- `CoreCommandsHandler`: handles the packets from the tray (service, ConPTY, settings, MSI, application, conditional launch). + Conditional launch actions send `SendConditionalLaunchResult` with success or the error text. +- `ConPtyHelpCaptureClient`: asks the tray to run an executable and returns its help output (`CaptureHelpAsync`). + +## ComponentServices + +### Main types + +- `ComponentTasksManager`: a `TasksManageService` with one task for each installed component. + `UpdateTaskList` stops all tasks, makes the list again from the database, and starts again the tasks that ran before. +- `ProcessService`: the state of one component: running state, output buffers, last error and events. + It keeps the output of a component up to 2 Mi characters. When the output is longer, it keeps the last half. +- `ComponentItemsLoaderHelper`: a list of `ComponentHelper` objects for installed components with an existing executable. + `Init` replaces the full list. It does not change the list in place. +- `ComponentHelper`: the executable path, the folder and the startup parameters of one component. + The executable is `\.exe` from the database. If it is missing, the helper tries `Store\Items\`. + +### How a component task starts + +1. The tray sends `CONPTYMessageIds.GetStartupString` with `componentId`. + For autorun, it sends `GetAllStartupStrings`. A conditional launch can send `StartComponent` or `ToggleComponent`. +2. `CoreCommandsHandler` calls `ComponentTasksManager.CreateAndRunNewTask(id)`. + If the task exists, the manager stops it first (`StopProcessId`). +3. `ProcessService.StartProcess` reads the selected preset and makes the startup string (see "Presets"). +4. An exception occurs when the executable is missing or the startup string is empty. The task then shows an error. +5. For ByeDPI, NoDPI and SpoofDPI, `SetupProxy` adds the address and port and sends proxy packets (see "Proxy"). +6. For NoDPI, the service adds `--quiet` one time. +7. The service sends `CONPTYMessageIds.StartProcessId` with `componentId`, `exePath` and `args`. +8. The tray starts the process and sends `MarkProcessIdAsStarted`, `ProcessIdNewOutput` and `MarkProcessIdAsStopped`. + +`RunAllPreferredActions` starts each component that has `CONFIGS//usedForAutorun` set to `true`. + +### Presets (`ConfigItem`) + +A preset pack is a Store item of the type `configlist`. Its folder contains: + +- `init.json` (`ConfigInitItem`): `toggleListAvailable` and `localized_strings_directory` (language code to file, for example `"EN"`). +- One `.json` file for each preset (`ConfigItem`). All `.json` files except `init.json` are presets. + +Important `ConfigItem` fields: + +| Field | Content | +|---|---| +| `target` | The first value is the component id. `$ANY` as a target loads all presets. | +| `name` | The display name. Each `$WORD(key)` in the name is replaced with `key` from the localization file of the pack. | +| `startup_string` | The command line with `%NAME%` variables and LScript functions. | +| `variables` | A list of `"%NAME%=value"`. A value can be an LScript expression, for example `$LOCALCONDITION(...)`. | +| `jparams` | Boolean switches that `$LOCALCONDITION` reads. | +| `commaVars`, `availableCommaVarsValues` | Variables with a list of possible values. | +| `configMaker` | Editor data only. The runtime does not read it. | + +The selected preset is in the settings `CONFIGS//configId` (the pack id) and `configFile` (the file name). +The Zapret2 component also loads Zapret (`CSZTBN012`) presets. These presets have `IsLegacy = true`. + +`ConfigurationService.GetStartupParametersByConfigItem` makes the startup string in this sequence: + +1. If `jparams`, `variables` or `startup_string` is `null`, it returns an empty string. +2. `GetReadyToUseVariables` runs `LScriptCore.ExecuteScript` on each variable value (`callItemId` is the pack id). + Names are not case-sensitive. When a name occurs two times, the last value is used. +3. `ReplaceVariables` replaces `%NAME%` (letters, digits, `_`, `.`, `-`). Values can refer to other variables. + It does a maximum of 16 passes and stops at 64 KiB. Unknown names stay. +4. `ReplaceCommaVariables` replaces `commaVars` with the same rules. +5. `LScriptCore.ExecuteScriptUnsafe` expands LScript functions in the full string (`quotePaths: true`). +6. For a legacy preset, `Zapret2LegacyConfigService` converts the result for winws2. + +The legacy conversion keeps a cache in `Store\Items\__ConvertedZPRT`. The setting +`ZAPRET2/validateLegacyCacheHashes` (default `true`) makes it compare file hashes before it uses the cache. + +Other configuration types: + +- `ConfigItem.ExpandVariables`, `ExpandFileReference`, `ResolveFilePath` and `UsedFiles`: expand variables and `preset://` aliases without LScript. AddOns (for example ConfigShare) use them. +- `ConfigCommandLine`: tokenizes and quotes command lines. It does not run them. +- `ConfigFileReferences`: finds and rewrites file arguments in a command line. +- `ConfigPackMakeHelper.CreateConfigPack`: copies presets, used files and localization files into a pack. It writes a ZIP file, or registers the pack in the database (`autoImport`). +- `ConfigurationService.LoadConfigFromFile`: reads a `.json` preset, an old preset with `custom_parameters`, or a `.bat`/`.cmd` file. +- `Autocorrector.FindAutoCorrectPath`: finds a file of a preset in the pack, the component and other installed items. + +## Features + +- `ApplicationAutorunManager`: sends `AddToAutorun` or `RemoveFromAutorun` to the tray and sets `SYSTEM/autorun`. +- `ApplicationUpdate`: checks the application release on GitHub or GitLab. After the Store installs the application item, it sends `BeginApplicationUpdate` to the tray with `patch.cdpipatch` (portable) or `patch.msi` (MSI). +- `FileSystemLinksManager`: creates symbolic links or hard links for item files. It creates the link with a temporary name first. + +## JSON + +`JSONConvertor` loads, serializes and deserializes JSON with Newtonsoft.Json. + +## LScript + +LScript is a small expression language in Store metadata, preset variables and startup strings. +See [LScript/README.md](LScript/README.md). + +## Proxy + +- `ProxyHelper`: `ProxySetupTypes` (`None`, `AllSystem`, `ProxiFyre`, `NoActions`, `AsInConfig`) and `ProxyLikeComponents` (ByeDPI, NoDPI, SpoofDPI). + `ReplaceArgsForProxy` removes the address and port options and adds the values of `PROXY/IPAddress` and `PROXY/port`. + `IsLoopbackAddress` and `IsValidPort` validate user input. +- `WindowsNativeProxySettingsService`: reads and writes the Windows Internet Settings proxy values in the registry. + +`ProcessService.SetupProxy` uses `PROXY/proxyType`. `ProxiFyre` needs the add-on `ASPEWK002`. + +## Security + +`CertificateCheck.CheckCatalog` verifies a signed `catalog.cat` against a folder. +It uses `CatalogVerifier` from `CDPIUI.Shared` and `Secret.TrustedCertificates`. + +## Store + +`StoreHelper` is the facade for the Store. The GUI uses this class. The other Store services are internal. + +| Type | Responsibility | +|---|---| +| `StoreHelper` | Install, update, remove and update checks. Events for progress and errors. | +| `Queue/QueueManagerService` | A FIFO queue. It installs one item at a time. It also keeps the list of failed items. | +| `Repository/RepositoryLoaderService` | Downloads the Store repository (GitHub or GitLab) and reads categories, items and ready kits. | +| `Database/DatabaseHelper` | The SQLite table `Items` in `storedata.db`. One row for each installed item (`DatabaseStoreItem`). All queries, also `RestorePaths`, read the table under one lock. At start, the GUI calls `RestorePaths`. If the GUI cannot restore an item, it registers the user item or the application item again. For other items, `UnregisterMissingItem` deletes the row when the item folder is missing (`CDPIUI/Helper/Database/DatabaseInitializationService.cs`). | +| `Network/APIWorker`, `RequestWorker`, `DownloadWorker` | Release API requests, HTTPS downloads, archive extraction and MSI start. | +| `MSI/MsiInstallerService`, `MsiInstallerQueueManager` | Send an MSI to the tray for installation and wait for the result (maximum 30 minutes). | +| `Application/ApplicationUpdateService` | Prepares an application patch (`patch.cdpipatch`) and its required patches. It reads the version of each required patch from its own `requirements.json` and skips versions that are not newer than the installed version or not older than the main patch. It puts the layers in version sequence, the oldest first. See [.github/README.md](../.github/README.md#gui-side). | +| `LocalItemsInstallerHelper` | Reads local pack files and adds them to the queue. | +| `StoreItemIds` | Validates item ids (`^[A-Za-z0-9_-]{1,64}$`) and item folders. | +| `VersionHelper` (`VersionComparier.cs`) | Compares version strings. | +| `Data/HardcodedItemIds` | Fixed ids of components and add-ons. | + +### Store repository cache + +`LoadAllStoreDatabase` downloads the repository archive when the cache is missing. +It also downloads the archive when a sync is forced and the last sync (`STORE/lastSyncTime`) is one day old or more. +The archive goes into a new folder `Repo.new-`. The library replaces `Store\Cache\Repo` only after a full extraction. +If GitHub fails, the library tries the GitLab mirror. + +### How an item is installed or updated + +1. `AddItemToQueue` adds the item with a new operation id. An item can be in the queue one time only. +2. `InstallItemWorker` validates the item id. +3. If the item is installed, the library stops the component and its driver service. It keeps the running state. +4. The library creates `Store\Items\`. It deletes the old content only when `cleanDirectoryBeforeInstalling` is `true`. +5. An online item is downloaded through HTTPS to `TempFiles\Downloads`. The library then extracts or copies it into the item folder. +6. A local pack is moved into place (see "Staging and rollback"). +7. `AddItemToDatabase` runs `before_install_actions`, writes the database row, and runs `after_install_actions`. + The two action lists get the item folder as `CurrentDirectory`. The library downloads the `DOWNLOAD=` requests of the two lists (see [LScript/README.md](LScript/README.md)). + For a local pack, the library keeps a `$DYNAMICIMAGE(...)` icon as a full path in the item folder (`GetIconPath`). +8. If the component ran before the update, the library starts it again. +9. `ItemActionsStopped` makes `ComponentTasksManager` load the component list again. + +If a first installation fails, the library removes the item. If an update fails, the database row of the installed version stays. + +### Staging and rollback + +| Content | Behaviour | +|---|---| +| Store repository | Extracted to `Repo.new-`, then moved into place. | +| Signed archive (`signedZip`, `.cdpisignedpack`) | Extracted to `.verify-`. The library moves the files into place only when the catalog is valid. | +| Local pack | The old folder moves to `.previous-`. If the move of the new pack fails, the old folder comes back. | +| Online, not signed archive | Extracted directly into the item folder. There is no rollback. | + +CAUTION: Do not expect a rollback for online updates. A failed online update can leave new files next to old files. + +An archive that is not signed merges `.txt` files: the library adds new lines to an existing file and does not replace it. +A pack that is not signed (not `.cdpisignedpack`) cannot bring `.exe`, `.dll`, `.sys`, `.msi`, scripts or links. +For such a pack, the library ignores `ExecutableFile`, `BeforeInstallActions` and `AfterInstallActions`. + +## System + +- `ZipService` (internal): ZIP extraction with a root folder filter (`$ANY` matches one segment), skip patterns and the catalog check. +- `ArchivePaths`: rejects archive entries that point outside the target folder. +- `BatchFileEncoding`: reads `.bat` files in UTF-8, UTF-16 or a Cyrillic code page. +- `ShellHelper`: opens folders and files and starts applications. + +## Tests + +Run `dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj`. The tests of this library are in `CDPIUI.Tests/Core`. diff --git a/CDPIUI.Core/Security/CertificateCheck.cs b/CDPIUI.Core/Security/CertificateCheck.cs index 4fab1c44..2f511de6 100644 --- a/CDPIUI.Core/Security/CertificateCheck.cs +++ b/CDPIUI.Core/Security/CertificateCheck.cs @@ -1,8 +1,5 @@ -using CDPIUI.Shared.Exceptions.Catalog; -using CDPIUI.Shared.Secrets; -using System.Diagnostics; -using System.Security.Cryptography; -using System.Security.Cryptography.X509Certificates; +using CDPIUI.Shared.Secrets; +using CDPIUI.Shared.Security; namespace CDPIUI.Core.Security { @@ -19,57 +16,24 @@ public class CertificateCheck { public static async Task CheckCatalog(string catalogFile, string compareDirectory) { - CatalogCheckResult catalogCheckResult; - bool result = false; try { - result = await OpenAndCompareCatalogFile(catalogFile, compareDirectory); - if (result) catalogCheckResult = CatalogCheckResult.Success; - else catalogCheckResult = CatalogCheckResult.FailureNotValid; - } - catch (Exception ex) - { - if (ex is CertificateNotTrusted) catalogCheckResult = CatalogCheckResult.FailureNotTrustedSignature; - else if (ex is CryptographicException) catalogCheckResult = CatalogCheckResult.FailureNoSignature; - else catalogCheckResult = CatalogCheckResult.FailureUnknown; - } - - await Task.CompletedTask; - return catalogCheckResult; - } + CatalogVerification verification = await CatalogVerifier.VerifyAsync( + catalogFile, compareDirectory, Secret.TrustedCertificates); - private static async Task OpenAndCompareCatalogFile(string catalogFile, string compareDirectory) - { - X509Certificate catalogFileSign = X509Certificate.CreateFromSignedFile(catalogFile); - if (!Secret.TrustedCertificates.Contains(catalogFileSign.GetCertHashString())) + return verification switch + { + CatalogVerification.Success => CatalogCheckResult.Success, + CatalogVerification.NoSignature => CatalogCheckResult.FailureNoSignature, + CatalogVerification.NotTrustedSignature => CatalogCheckResult.FailureNotTrustedSignature, + CatalogVerification.FilesDoNotMatch => CatalogCheckResult.FailureNotValid, + _ => CatalogCheckResult.FailureUnknown, + }; + } + catch { - throw new CertificateNotTrusted("Certificate not trusted"); + return CatalogCheckResult.FailureUnknown; } - - ProcessStartInfo startInfo = new ProcessStartInfo(); - - startInfo.FileName = @"powershell.exe"; - startInfo.Arguments = - $"Test-FileCatalog " + - $"-CatalogFilePath \'{catalogFile}\' " + - $"-Path \'{compareDirectory}\' " + - $"-FilesToSkip catalog.cat"; - startInfo.RedirectStandardOutput = true; - startInfo.RedirectStandardError = true; - startInfo.UseShellExecute = false; - startInfo.CreateNoWindow = true; - - Process process = new Process(); - - process.StartInfo = startInfo; - process.Start(); - await process.WaitForExitAsync(); - - string output = process.StandardOutput.ReadToEnd().ReplaceLineEndings(""); - if (string.Equals("Valid", output, StringComparison.OrdinalIgnoreCase)) - return true; - else - return false; } } } diff --git a/CDPIUI.Core/State.cs b/CDPIUI.Core/State.cs index dc3f826a..42721655 100644 --- a/CDPIUI.Core/State.cs +++ b/CDPIUI.Core/State.cs @@ -1,25 +1,44 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.Win32; +using System; +using System.IO; namespace CDPIUI.Core { public static class State { #if SINGLEFILE - public static bool IsApplicationBuildAsSingleFile = true; - public static bool IsApplicationBuildAsMsi = false; + public static bool IsApplicationBuildAsSingleFile => true; + public static bool IsApplicationBuildAsMsi => false; #elif MSIFILE - public static bool IsApplicationBuildAsSingleFile = false; - public static bool IsApplicationBuildAsMsi = true; -#elif Release - public static bool IsApplicationBuildAsSingleFile = true; - public static bool IsApplicationBuildAsMsi = false; + public static bool IsApplicationBuildAsSingleFile => false; + public static bool IsApplicationBuildAsMsi => true; #else - public static bool IsApplicationBuildAsSingleFile = false; - public static bool IsApplicationBuildAsMsi = false; + // A Release build is packed both as the MSI and as the portable ZIP, so the kind of + // installation is found at run time: the MSI writes its install folder to the registry. + // "#elif Release" never matched (the symbol is RELEASE), and such builds looked for MSI updates. + public static bool IsApplicationBuildAsSingleFile => !InstalledByMsi.Value; + public static bool IsApplicationBuildAsMsi => InstalledByMsi.Value; + + private static readonly Lazy InstalledByMsi = new(DetectMsiInstallation); + + private static bool DetectMsiInstallation() + { + try + { + using RegistryKey? key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) + .OpenSubKey(@"Software\Storik4\CDPI UI"); + if (key?.GetValue("InstallLocation") is not string location || string.IsNullOrWhiteSpace(location)) + return false; + return string.Equals( + Path.GetFullPath(location).TrimEnd(Path.DirectorySeparatorChar), + Path.GetFullPath(AppContext.BaseDirectory).TrimEnd(Path.DirectorySeparatorChar), + StringComparison.OrdinalIgnoreCase); + } + catch (Exception) + { + return false; + } + } #endif } } diff --git a/CDPIUI.Core/Store/Application/ApplicationUpdateService.cs b/CDPIUI.Core/Store/Application/ApplicationUpdateService.cs index 1a7b5f4a..0c23fddd 100644 --- a/CDPIUI.Core/Store/Application/ApplicationUpdateService.cs +++ b/CDPIUI.Core/Store/Application/ApplicationUpdateService.cs @@ -8,7 +8,7 @@ using CDPIUI.Shared.Exceptions; using CDPIUI.Shared.Exceptions.Catalog; using CDPIUI.Shared.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System.IO.Compression; namespace CDPIUI.Core.Store.Application @@ -23,7 +23,6 @@ public async Task> GetPatchReadyToInstall( string appItemDirectory = Path.Combine(Directories.StoreItemsDirectory, SharedConstants.ApplicationStoreId); string patchesDirectory = Path.Combine(appItemDirectory, "Patches"); - string finalDirectory = Path.Combine(appItemDirectory, "CDPIUI"); string patchDirectory = Path.Combine( patchesDirectory, @@ -61,7 +60,6 @@ public async Task> GetPatchReadyToInstall( FinalizePatch( downloadedPatches, patchDirectory, - finalDirectory, appItemDirectory); return OperationResultModel.SuccessResult(); @@ -74,7 +72,7 @@ public async Task> GetPatchReadyToInstall( { try { - Directory.Delete(patchesDirectory); + Directory.Delete(patchesDirectory, recursive: true); } catch { } } @@ -133,43 +131,48 @@ private async Task>> DownloadRequiredPatches( return OperationResultModel> .FailureResult(HandleException(PrettyErrorCode.SERVER_UNREACHABLE)); - var patches = new List(); + // The version of a required patch comes from its own requirements.json, not from the + // shape of its URL (GitLab links were skipped), and the layers are applied by version, + // oldest first, whatever the order of patch_urls. Every folder has its own index: a + // name from the current second was shared by patches downloaded in the same second. + var patches = new List<(Version Version, string Layer)>(); try { - foreach (string requirement in requirements.patch_urls) + int index = 0; + foreach (string requirement in requirements.patch_urls ?? []) { - string[] parts = requirement.TrimStart('/').Split('/'); - if (parts.Length > 2) - { - string[] reversedParts = parts.Reverse().ToArray(); - Logger.Instance.CreateInfoLog(nameof(ApplicationUpdateService), $"Version {reversedParts[1]}"); - Version patchVer = new(reversedParts[1]); - if (currentVersion >= patchVer) - continue; - } - else - { - continue; // TODO: Fix requiriments search for GitLab - } - - TimeSpan epochTime = DateTime.UtcNow - new DateTime(1970, 1, 1); - int secondsSinceEpoch = (int)epochTime.TotalSeconds; - string downloadDir = Path.Combine(patchDirectory, secondsSinceEpoch.ToString()); - patches.Add(downloadDir); + if (string.IsNullOrWhiteSpace(requirement)) + continue; + + string downloadDir = Path.Combine(patchDirectory, "required", (index++).ToString()); bool ok = await downloadWorker.DownloadAndExtractAsync( requirement, downloadDir, extractArchive: true, extractSkipFiletypes: [], - extractRootFolder: "CDPIUI/", + extractRootFolder: "/", executableFileName: "", - filetype: Shared.Basic.Filesystem.FileExtentionTypes.archive); + filetype: Shared.Basic.Filesystem.FileExtensionTypes.archive); if (!ok) return OperationResultModel>.UnSuccessResult(); - await ValidateCatalog(downloadDir); + var requiredResult = LoadPatchRequirements(downloadDir); + if (!requiredResult.Success || !Version.TryParse(requiredResult.Result!.version, out Version? requiredVersion)) + throw new UnknownFileFormatException(); + + Logger.Instance.CreateInfoLog(nameof(ApplicationUpdateService), $"Required patch {requiredVersion}"); + if (currentVersion >= requiredVersion) + continue; + // The main patch is always the last layer; a required patch that is not older + // than it would be applied first and then partly overwritten. + if (Version.TryParse(requirements.version, out Version? mainVersion) && requiredVersion >= mainVersion) + continue; + + string layer = Path.Combine(downloadDir, "CDPIUI"); + await ValidateCatalog(layer); + patches.Add((requiredVersion, layer)); } } catch (Exception ex) @@ -177,8 +180,8 @@ private async Task>> DownloadRequiredPatches( return OperationResultModel>.FailureResult(HandleException(ex)); } - patches.Reverse(); - return OperationResultModel>.SuccessResult(patches); + return OperationResultModel>.SuccessResult( + patches.OrderBy(patch => patch.Version).Select(patch => patch.Layer).ToList()); } private static async Task ValidateCatalog(string dir) @@ -192,18 +195,28 @@ private static async Task ValidateCatalog(string dir) throw new CatalogInvalid(); } - private void FinalizePatch(List patches, string patchDirectory, string finalDirectory, string appItemDirectory) + /// + /// Packs the required patches, oldest first, and the main patch as separate layers + /// (layers/0/, layers/1/, ...). Each layer keeps its own signed catalog, so the elevated + /// tray can check every signature again before Update.exe applies the layers in order. + /// + private void FinalizePatch(List patches, string patchDirectory, string appItemDirectory) { - foreach (string dir in patches) - { - Directory.Move(dir, finalDirectory); - } - Directory.Move(Path.Combine(patchDirectory, "CDPIUI"), finalDirectory); + var layers = new List(patches) { Path.Combine(patchDirectory, "CDPIUI") }; string finalPatchFile = Path.Combine(appItemDirectory, "patch.cdpipatch"); if (File.Exists(finalPatchFile)) File.Delete(finalPatchFile); - ZipFile.CreateFromDirectory(patchDirectory, finalPatchFile); + + using ZipArchive archive = ZipFile.Open(finalPatchFile, ZipArchiveMode.Create); + for (int i = 0; i < layers.Count; i++) + { + foreach (string file in Directory.EnumerateFiles(layers[i], "*", SearchOption.AllDirectories)) + { + string relativePath = Path.GetRelativePath(layers[i], file).Replace('\\', '/'); + archive.CreateEntryFromFile(file, $"layers/{i}/{relativePath}"); + } + } } private static ErrorModel HandleException(Exception ex) diff --git a/CDPIUI.Core/Store/Database/DatabaseHelper.cs b/CDPIUI.Core/Store/Database/DatabaseHelper.cs index a15eed8b..3c4574bc 100644 --- a/CDPIUI.Core/Store/Database/DatabaseHelper.cs +++ b/CDPIUI.Core/Store/Database/DatabaseHelper.cs @@ -1,6 +1,6 @@ using CDPIUI.Core.Basic; using CDPIUI.Core.Data; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Shared; using Microsoft.Data.Sqlite; using System.Data; @@ -93,24 +93,26 @@ INSERT OR REPLACE INTO Items (Id, Type, Directory, Executable, UpdateCheckUrl, D @RequiredItemIds, @DependentItemIds, @Icon, @Name, @ShortName, @Developer, @BackgroundColor)"; cmd.CommandType = CommandType.Text; + // Microsoft.Data.Sqlite rejects a null parameter value: an item without developer, + // icon or version was not saved although its installation was reported as done. cmd.Parameters.AddWithValue("@Id", item.Id); - cmd.Parameters.AddWithValue("@Type", item.Type); - cmd.Parameters.AddWithValue("@Directory", item.Directory); + cmd.Parameters.AddWithValue("@Type", item.Type ?? string.Empty); + cmd.Parameters.AddWithValue("@Directory", item.Directory ?? string.Empty); cmd.Parameters.AddWithValue("@Executable", item.Executable == null? DBNull.Value : item.Executable); cmd.Parameters.AddWithValue("@UpdateCheckUrl", item.UpdateCheckUrl == null ? DBNull.Value : item.UpdateCheckUrl); cmd.Parameters.AddWithValue("@DownloadUrl", item.DownloadUrl == null ? DBNull.Value : item.DownloadUrl); cmd.Parameters.AddWithValue("@DownloadFileType", item.DownloadFileType == null ? DBNull.Value : item.DownloadFileType); - cmd.Parameters.AddWithValue("@VersionControlType", item.VersionControlType); - cmd.Parameters.AddWithValue("@CurrentVersion", item.CurrentVersion); + cmd.Parameters.AddWithValue("@VersionControlType", item.VersionControlType ?? string.Empty); + cmd.Parameters.AddWithValue("@CurrentVersion", item.CurrentVersion ?? string.Empty); cmd.Parameters.AddWithValue("@RequiredItemIds", (object)item.RequiredItemIds?.SerializeTuples() ?? DBNull.Value); cmd.Parameters.AddWithValue("@DependentItemIds", (object)item.DependentItemIds?.SerializeTuples() ?? DBNull.Value); - cmd.Parameters.AddWithValue("@Icon", item.IconPath); - cmd.Parameters.AddWithValue("@Name", item.Name); - cmd.Parameters.AddWithValue("@ShortName", item.ShortName); - cmd.Parameters.AddWithValue("@Developer", item.Developer); - cmd.Parameters.AddWithValue("@BackgroundColor", item.BackgroudColor); + cmd.Parameters.AddWithValue("@Icon", item.IconPath ?? string.Empty); + cmd.Parameters.AddWithValue("@Name", item.Name ?? string.Empty); + cmd.Parameters.AddWithValue("@ShortName", item.ShortName ?? string.Empty); + cmd.Parameters.AddWithValue("@Developer", item.Developer ?? string.Empty); + cmd.Parameters.AddWithValue("@BackgroundColor", item.BackgroundColor ?? string.Empty); lock (databaseRequestLock) { @@ -193,6 +195,19 @@ public List GetAllInstalledItems() return items; } + /// + /// Removes the registration of an item whose folder no longer exists. Such entries made + /// the item look installed, and Autocorrector and the component pages used missing files. + /// + public void UnregisterMissingItem(DatabaseStoreItem item) + { + if (item == null || string.IsNullOrEmpty(item.Id) || + (!string.IsNullOrEmpty(item.Directory) && Directory.Exists(item.Directory))) + return; + Logger.Instance.CreateWarningLog(nameof(DatabaseHelper), $"The folder of {item.Id} is missing; its registration is removed."); + DeleteItemById(item.Id); + } + internal void DeleteItemById(string id) { using var connection = new SqliteConnection(LocalDatabaseConnectionString); @@ -220,35 +235,34 @@ public bool IsItemInstalled(string id) if (GetItemById(id) == null) return false; return true; } + // A NULL in an optional column of one row made GetString throw and broke the list of all + // installed items. + private static string? GetStringOrNull(SqliteDataReader reader, string column) + { + int ordinal = reader.GetOrdinal(column); + return reader.IsDBNull(ordinal) ? null : reader.GetString(ordinal); + } + private static DatabaseStoreItem CreateItemFromReader(SqliteDataReader reader) { return new DatabaseStoreItem { Id = reader.GetString(reader.GetOrdinal("Id")), - Type = reader.GetString(reader.GetOrdinal("Type")), - Directory = reader.GetString(reader.GetOrdinal("Directory")), - Executable = reader.IsDBNull(reader.GetOrdinal("Executable")) - ? null : reader.GetString(reader.GetOrdinal("Executable")), - UpdateCheckUrl = reader.IsDBNull(reader.GetOrdinal("UpdateCheckUrl")) - ? null : reader.GetString(reader.GetOrdinal("UpdateCheckUrl")), - DownloadUrl = reader.IsDBNull(reader.GetOrdinal("DownloadUrl")) - ? null : reader.GetString(reader.GetOrdinal("DownloadUrl")), - DownloadFileType = reader.IsDBNull(reader.GetOrdinal("DownloadFileType")) - ? null : reader.GetString(reader.GetOrdinal("DownloadFileType")), - VersionControlType = reader.GetString(reader.GetOrdinal("VersionControlType")), - CurrentVersion = reader.GetString(reader.GetOrdinal("CurrentVersion")), - RequiredItemIds = reader.IsDBNull(reader.GetOrdinal("RequiredItemIds")) - ? null : reader.GetString(reader.GetOrdinal("RequiredItemIds")).DeserializeTuples(), - DependentItemIds = reader.IsDBNull(reader.GetOrdinal("DependentItemIds")) - ? null : reader.GetString(reader.GetOrdinal("DependentItemIds")).DeserializeTuples(), - IconPath = reader.GetString(reader.GetOrdinal("Icon")), - Name = reader.GetString(reader.GetOrdinal("Name")), - ShortName = reader.IsDBNull(reader.GetOrdinal("ShortName")) - ? null : reader.GetString(reader.GetOrdinal("ShortName")), - Developer = reader.IsDBNull(reader.GetOrdinal("Developer")) - ? string.Empty : reader.GetString(reader.GetOrdinal("Developer")), - BackgroudColor = reader.IsDBNull(reader.GetOrdinal("BackgroundColor")) - ? string.Empty : reader.GetString(reader.GetOrdinal("BackgroundColor")), + Type = GetStringOrNull(reader, "Type"), + Directory = GetStringOrNull(reader, "Directory"), + Executable = GetStringOrNull(reader, "Executable"), + UpdateCheckUrl = GetStringOrNull(reader, "UpdateCheckUrl"), + DownloadUrl = GetStringOrNull(reader, "DownloadUrl"), + DownloadFileType = GetStringOrNull(reader, "DownloadFileType"), + VersionControlType = GetStringOrNull(reader, "VersionControlType"), + CurrentVersion = GetStringOrNull(reader, "CurrentVersion"), + RequiredItemIds = GetStringOrNull(reader, "RequiredItemIds")?.DeserializeTuples(), + DependentItemIds = GetStringOrNull(reader, "DependentItemIds")?.DeserializeTuples(), + IconPath = GetStringOrNull(reader, "Icon"), + Name = GetStringOrNull(reader, "Name"), + ShortName = GetStringOrNull(reader, "ShortName"), + Developer = GetStringOrNull(reader, "Developer") ?? string.Empty, + BackgroundColor = GetStringOrNull(reader, "BackgroundColor") ?? string.Empty, }; } @@ -298,7 +312,7 @@ public UnprocessedOperationResultModel RegisterUserCustomItem( Name = "Storage for custom items of current user", ShortName = "Local data storage", Developer = "Storik4", - BackgroudColor = "" + BackgroundColor = "" }; if (!AddOrUpdateItem(userItem)) @@ -324,7 +338,7 @@ public UnprocessedOperationResultModel RegisterUserCustomItem( Name = "CDPI UI updates helper item", ShortName = "CDPI UI updates helper item", Developer = "Storik4", - BackgroudColor = "" + BackgroundColor = "" }; if (!AddOrUpdateItem(applicationItem)) @@ -344,19 +358,33 @@ public OperationResultModel> RestorePaths() connection.Open(); using var cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Items"; - using var reader = cmd.ExecuteReader(); + + // Read under the lock like the other queries; the items are checked after it. + List allItems = []; + lock (databaseRequestLock) + { + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + allItems.Add(CreateItemFromReader(reader)); + } var itemsToUpdate = new List(); List RestoreFailureItems = []; - while (reader.Read()) + foreach (var item in allItems) { - var item = CreateItemFromReader(reader); - if (!item.Directory.StartsWith(baseDir) || !Directory.Exists(item.Directory)) + // With a separator and ignoring case: "C:\CDPIUI2" is not inside "C:\CDPIUI". + string baseWithSeparator = baseDir.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + bool isInsideBase = !string.IsNullOrEmpty(item.Directory) && + (string.Equals(item.Directory.TrimEnd(Path.DirectorySeparatorChar), baseDir.TrimEnd(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase) || + item.Directory.StartsWith(baseWithSeparator, StringComparison.OrdinalIgnoreCase)); + if (!isInsideBase || !Directory.Exists(item.Directory)) { - string folderName = Path.GetFileName(item.Directory.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); + string folderName = string.IsNullOrEmpty(item.Directory) + ? item.Id + : Path.GetFileName(item.Directory.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); string newPath = Path.Combine(Directories.StoreItemsDirectory, folderName); if (Directory.Exists(newPath)) diff --git a/CDPIUI.Core/Store/Database/DatabaseStoreItem.cs b/CDPIUI.Core/Store/Database/DatabaseStoreItem.cs index 1bd330ae..6ecdcfe6 100644 --- a/CDPIUI.Core/Store/Database/DatabaseStoreItem.cs +++ b/CDPIUI.Core/Store/Database/DatabaseStoreItem.cs @@ -17,6 +17,6 @@ public class DatabaseStoreItem public string? Name { get; set; } public string? ShortName { get; set; } public string? Developer { get; set; } - public string? BackgroudColor { get; set; } + public string? BackgroundColor { get; set; } } } diff --git a/CDPIUI.Core/Store/LocalItemsInstallerHelper.cs b/CDPIUI.Core/Store/LocalItemsInstallerHelper.cs index c7bc8c16..b608fd49 100644 --- a/CDPIUI.Core/Store/LocalItemsInstallerHelper.cs +++ b/CDPIUI.Core/Store/LocalItemsInstallerHelper.cs @@ -48,6 +48,13 @@ public static LocalItemsInstallerHelper Instance } } + // Files an unsigned pack may not bring: programs, libraries, drivers, installers and scripts. + private static readonly List UnsignedPackSkippedFiles = + [ + "*.exe", "*.dll", "*.sys", "*.msi", "*.com", "*.scr", "*.cpl", "*.bat", "*.cmd", + "*.ps1", "*.vbs", "*.vbe", "*.js", "*.jse", "*.wsf", "*.hta", "*.lnk", "*.url", + ]; + private LocalItemsInstallerHelper() { string localAppData = CDPIUI.Core.Data.Directories.DataDirectory; @@ -60,7 +67,7 @@ private LocalItemsInstallerHelper() string tempDestination = Path.Combine(TempDirectory, tempFolderName); bool isCatalogCheckRequired = Path.GetExtension(itemPackFile) == ".cdpisignedpack" ? true : false; - List filesToSkip = isCatalogCheckRequired ? [] : [".exe"]; + List filesToSkip = isCatalogCheckRequired ? [] : UnsignedPackSkippedFiles; try { @@ -75,16 +82,18 @@ private LocalItemsInstallerHelper() } LocalItemInitModel localItemInitModel = JSONConvertor.LoadJson(initFilePath); - if (localItemInitModel.Icon.StartsWith("$DYNAMICIMAGE")) + if (localItemInitModel.Icon?.StartsWith("$DYNAMICIMAGE") == true) { string iconUrl = LScriptCore.ExecuteScript(localItemInitModel.Icon, scriptArgs: tempDestination); - if (File.Exists(iconUrl)) + // Only an image of the pack itself: an absolute path in Icon copied any local file + // before the user had confirmed anything. + if (File.Exists(iconUrl) && IsPackImage(tempDestination, iconUrl)) { string newIconUrl = Path.Combine(TempDirectory, $"{tempFolderName}_ICONDATA", Path.GetFileName(iconUrl)); - if (!Directory.Exists(newIconUrl)) Directory.CreateDirectory(Path.GetDirectoryName(newIconUrl)); + Directory.CreateDirectory(Path.GetDirectoryName(newIconUrl)!); - File.Copy(iconUrl, newIconUrl); + File.Copy(iconUrl, newIconUrl, overwrite: true); localItemInitModel.ReadyToUseIcon = newIconUrl; } } @@ -93,6 +102,10 @@ private LocalItemsInstallerHelper() { throw new AccessViolationException("StoreId is protected by security policy"); } + if (!StoreItemIds.IsValid(localItemInitModel.StoreId)) + { + throw new InvalidDataException("StoreId is not a valid identifier"); + } Directory.Delete(tempDestination, recursive: true); @@ -113,7 +126,7 @@ public async Task BeginLocalItemInstalling(string itemPackFile, Action? string tempDestination = Path.Combine(TempDirectory, tempFolderName); bool isCatalogCheckRequired = Path.GetExtension(itemPackFile) == ".cdpisignedpack" ? true : false; - List filesToSkip = isCatalogCheckRequired ? [] : [".exe"]; + List filesToSkip = isCatalogCheckRequired ? [] : UnsignedPackSkippedFiles; try { @@ -127,9 +140,15 @@ public async Task BeginLocalItemInstalling(string itemPackFile, Action? return; } LocalItemInitModel localItemInitModel = JSONConvertor.LoadJson(initFilePath); + if (!StoreItemIds.IsValid(localItemInitModel.StoreId) || + localItemInitModel.StoreId == SharedConstants.ApplicationStoreId || + localItemInitModel.StoreId == SharedConstants.LocalUserItemsId) + { + throw new InvalidDataException("StoreId is not allowed for a local pack"); + } if (cancellationToken.IsCancellationRequested) return; - StoreHelper.Instance.AddItemToQueue(localItemInitModel.StoreId!, packFile: tempDestination); + StoreHelper.Instance.AddItemToQueue(localItemInitModel.StoreId!, packFile: tempDestination, isSignedPack: isCatalogCheckRequired); return; } catch (Exception ex) @@ -146,8 +165,8 @@ public async void ImportApplicationUpdatePatchFromFile(string path) string tempFolderName = FileSystemService.GetNewTempFileName("ap"); string tempDestination = Path.Combine(TempDirectory, tempFolderName); - bool isCatalogCheckRequired = Path.GetExtension(path) == "cdpisignedpack" ? true : false; - List filesToSkip = isCatalogCheckRequired ? [] : [".exe"]; + bool isCatalogCheckRequired = Path.GetExtension(path) == ".cdpisignedpack" ? true : false; + List filesToSkip = isCatalogCheckRequired ? [] : UnsignedPackSkippedFiles; try { @@ -164,5 +183,15 @@ private string HandleError(Exception ex) { return ErrorsHelper.Convertor.GetPrettyErrorCode("ERR_LOCAL_ITEM_INSTALLING", ex); } + + private static readonly string[] PackImageExtensions = [".png", ".jpg", ".jpeg", ".ico", ".bmp", ".gif"]; + + private static bool IsPackImage(string packDirectory, string file) + { + string root = Path.GetFullPath(packDirectory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + string fullPath = Path.GetFullPath(file); + return fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase) && + PackImageExtensions.Contains(Path.GetExtension(fullPath), StringComparer.OrdinalIgnoreCase); + } } } diff --git a/CDPIUI.Core/Store/MSI/MsiInstallerQueueManager.cs b/CDPIUI.Core/Store/MSI/MsiInstallerQueueManager.cs index fdece8a5..b2b87d45 100644 --- a/CDPIUI.Core/Store/MSI/MsiInstallerQueueManager.cs +++ b/CDPIUI.Core/Store/MSI/MsiInstallerQueueManager.cs @@ -27,35 +27,41 @@ private class MsiInstallerModel public MsiInstallerService? MsiInstallerHelper { get; set; } } - private List installerModels = []; + // Responses come from the pipe thread while the store adds and removes operations. + private readonly List installerModels = []; + private readonly object modelsLock = new(); public void SendMsiInstallMessage(string operationId, string filename, MsiInstallerService installerHelper) { - installerModels.Add(new MsiInstallerModel + lock (modelsLock) { - OperationId = operationId, - MsiInstallerHelper = installerHelper - }); + installerModels.Add(new MsiInstallerModel + { + OperationId = operationId, + MsiInstallerHelper = installerHelper + }); + } _ = PipeHelper.SendMSIPacket(MSIInstallationMessageIds.Begin, operationId, filename); } public void RemoveMsiInstallerModel(string operationId, bool notify = true) { - MsiInstallerModel msiInstallerModel = installerModels.FirstOrDefault(i => i.OperationId == operationId); - if (msiInstallerModel != null) + bool removed; + lock (modelsLock) { - installerModels.Remove(msiInstallerModel); - if (notify) _ = PipeHelper.SendMSIPacket(MSIInstallationMessageIds.Kill, operationId); + removed = installerModels.RemoveAll(i => i.OperationId == operationId) > 0; } + if (removed && notify) _ = PipeHelper.SendMSIPacket(MSIInstallationMessageIds.Kill, operationId); } public void GetMsiInstallerMessage(string operationId, MsiState message) { - MsiInstallerModel msiInstallerModel = installerModels.FirstOrDefault(i => i.OperationId == operationId); - if (msiInstallerModel != null) + MsiInstallerModel? msiInstallerModel; + lock (modelsLock) { - msiInstallerModel.MsiInstallerHelper.OnResponse(message); + msiInstallerModel = installerModels.FirstOrDefault(i => i.OperationId == operationId); } + msiInstallerModel?.MsiInstallerHelper?.OnResponse(message); } } } diff --git a/CDPIUI.Core/Store/MSI/MsiInstallerService.cs b/CDPIUI.Core/Store/MSI/MsiInstallerService.cs index 2db080fe..7a848432 100644 --- a/CDPIUI.Core/Store/MSI/MsiInstallerService.cs +++ b/CDPIUI.Core/Store/MSI/MsiInstallerService.cs @@ -24,6 +24,8 @@ public class MsiCallback private readonly string _filename; private MsiState _state; + private static readonly TimeSpan InstallTimeout = TimeSpan.FromMinutes(30); + public MsiInstallerService(string operationId, string filename) { _operationId = operationId; @@ -34,11 +36,25 @@ public async Task Run(CancellationToken ct = default) { _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + // The task finished only with an answer of the tray: a lost answer or a hanging + // msiexec kept the Store busy forever, and a cancellation left msiexec running. using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); + cts.CancelAfter(InstallTimeout); using (cts.Token.Register(() => _tcs.TrySetCanceled())) { MsiInstallerQueueManager.Instance.SendMsiInstallMessage(_operationId, _filename, this); - await _tcs.Task.ConfigureAwait(false); + try + { + await _tcs.Task.ConfigureAwait(false); + } + catch (OperationCanceledException) + { + MsiInstallerQueueManager.Instance.RemoveMsiInstallerModel(_operationId, notify: true); + if (ct.IsCancellationRequested) + throw; + Logger.Instance.CreateErrorLog(nameof(MsiInstallerService), $"The installation of {_filename} did not finish in {InstallTimeout.TotalMinutes} minutes."); + _state = MsiState.ExceptionHappens; + } return new MsiCallback() { operationId = _operationId, diff --git a/CDPIUI.Core/Store/Network/APIWorker.cs b/CDPIUI.Core/Store/Network/APIWorker.cs index 17f5e51e..b9943373 100644 --- a/CDPIUI.Core/Store/Network/APIWorker.cs +++ b/CDPIUI.Core/Store/Network/APIWorker.cs @@ -3,9 +3,9 @@ using CDPIUI.Core.Store.Repository; using CDPIUI.Core.Store.ViewModels; using CDPIUI.Shared.Basic.Filesystem; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Shared.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.Shared.Secrets; using System.Diagnostics; using System.Text.Json; @@ -31,7 +31,7 @@ public async Task> GetDownloadLinkForVersion( string? repoUrl, string? targetFileOrFileType, string? version = null, - string? prefferedFile = null + string? preferredFile = null ) { APILinkModel linkModel = new(); @@ -68,12 +68,12 @@ public async Task> GetDownloadLinkForVersion( .Where(a => string.Equals(a.Name, targetFileOrFileType, StringComparison.OrdinalIgnoreCase) || a.Name.EndsWith( - FileSystemService.GetFileExtention(targetFileOrFileType.ToEnum()), + FileSystemService.GetFileExtension(targetFileOrFileType.ToEnum()), StringComparison.OrdinalIgnoreCase) ) .Where(a => - prefferedFile is null || - a.Name.Contains(prefferedFile, StringComparison.Ordinal) + preferredFile is null || + a.Name.Contains(preferredFile, StringComparison.Ordinal) ) .ToList(); @@ -189,11 +189,24 @@ private async Task GetGithubResponse( { if (string.IsNullOrEmpty(Token)) throw new ArgumentNullException(nameof(Token)); var uri = new Uri(repoUrl); - var parts = uri.AbsolutePath.Trim('/').Split('/'); + var parts = uri.AbsolutePath.Trim('/').Split('/', StringSplitOptions.RemoveEmptyEntries); + // GitLab projects may sit in nested groups ("group/subgroup/project"), and its pages + // continue after "/-/". + if (VersionControl == SupportedVersionControls.GitLab) + { + int dash = Array.IndexOf(parts, "-"); + if (dash >= 0) parts = parts[..dash]; + } + else if (parts.Length > 2) + { + parts = parts[..2]; + } if (parts.Length < 2) - throw new ArgumentException("Invalid GitHub repository URL.", nameof(repoUrl)); - var owner = parts[0]; - var repo = parts[1]; + throw new ArgumentException("Invalid repository URL.", nameof(repoUrl)); + var owner = string.Join("/", parts[..^1]); + var repo = parts[^1]; + if (repo.EndsWith(".git", StringComparison.OrdinalIgnoreCase)) + repo = repo[..^4]; string apiUrl = GetApiUrlForVersion(owner, repo, version, VersionControl); @@ -210,18 +223,25 @@ private async Task GetGithubResponse( private static string GetApiUrlForVersion(string owner, string repo, string? version, SupportedVersionControls versionControl) { string url = string.Empty; + // The version comes from item metadata; escaped, "../" cannot move the request to another API endpoint. + if (!string.IsNullOrEmpty(version)) + version = Uri.EscapeDataString(version); + if (versionControl == SupportedVersionControls.GitLab) + { + // The project id is the whole path with every '/' encoded. + string project = Uri.EscapeDataString($"{owner}/{repo}"); + return string.IsNullOrEmpty(version) + ? $"https://gitlab.com/api/v4/projects/{project}/releases/permalink/latest" + : $"https://gitlab.com/api/v4/projects/{project}/releases/{version}"; + } + owner = Uri.EscapeDataString(owner); + repo = Uri.EscapeDataString(repo); if (versionControl == SupportedVersionControls.GitHub) { url = string.IsNullOrEmpty(version) ? $"https://api.github.com/repos/{owner}/{repo}/releases/latest" : $"https://api.github.com/repos/{owner}/{repo}/releases/tags/{version}"; } - else if (versionControl == SupportedVersionControls.GitLab) - { - url = string.IsNullOrEmpty(version) - ? $"https://gitlab.com/api/v4/projects/{owner}%2F{repo}/releases/permalink/latest" - : $"https://gitlab.com/api/v4/projects/{owner}%2F{repo}/releases/{version}"; - } return url; } diff --git a/CDPIUI.Core/Store/Network/DownloadWorker.cs b/CDPIUI.Core/Store/Network/DownloadWorker.cs index 2a5022bf..81cf8a3c 100644 --- a/CDPIUI.Core/Store/Network/DownloadWorker.cs +++ b/CDPIUI.Core/Store/Network/DownloadWorker.cs @@ -3,8 +3,8 @@ using CDPIUI.Core.System; using CDPIUI.Shared.Basic.Filesystem; using CDPIUI.Shared.Exceptions; -using CDPIUI.Shared.PrettyErrorConvertionService; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.PrettyErrorConversionService; +using CDPIUI.Shared.Extensions; using System; using System.Collections.Generic; using System.IO; @@ -65,11 +65,11 @@ public async Task DownloadAndExtractAsync( bool success = false; List _extractedFiles = []; - var enFiletype = filetype.ToEnum(FileExtentionTypes.none); - bool isSupportedFileExtention = enFiletype != FileExtentionTypes.none; + var enFiletype = filetype.ToEnum(FileExtensionTypes.none); + bool isSupportedFileExtension = enFiletype != FileExtensionTypes.none; - string tempFileName = FileSystemService.GetNewTempFileName(nameof(DownloadWorker), FileSystemService.GetFileExtention(FileExtentionTypes.temp)); + string tempFileName = FileSystemService.GetNewTempFileName(nameof(DownloadWorker), FileSystemService.GetFileExtension(FileExtensionTypes.temp)); string tempDestination = Path.Combine(TempDirectory, tempFileName); try @@ -88,8 +88,10 @@ await requestWorker.DownloadFileAsync(url, tempDestination, ReportDownloadProgre } catch (HttpRequestException ex) when (ex.StatusCode.HasValue) { + string errorCode = $"ERR_DOWNLOAD_{PrettyErrorCode.UNEXPECTED_STATUS_CODE}_{(int)ex.StatusCode.Value}"; StageChanged?.Invoke(Tuple.Create(OperationId, "ErrorHappens")); - ErrorHappens?.Invoke(Tuple.Create(OperationId, $"ERR_DOWNLOAD_{PrettyErrorCode.UNEXPECTED_STATUS_CODE}_{(int)ex.StatusCode.Value}", "Server Error")); + ErrorHappens?.Invoke(Tuple.Create(OperationId, errorCode, "Server Error")); + LastError = ErrorModel.OnlyErrorCode(errorCode); return false; } @@ -99,31 +101,37 @@ await requestWorker.DownloadFileAsync(url, tempDestination, ReportDownloadProgre if (extractArchive) { StageChanged?.Invoke(Tuple.Create(OperationId, "Extracting")); - await ZipService.ExtractZip(tempDestination, extractRootFolder, destinationPath, extractSkipFiletypes, isCatalogCheckRequired: filetype == FileExtentionTypes.signedZip.ToString()); + await ZipService.ExtractZip(tempDestination, extractRootFolder, destinationPath, extractSkipFiletypes, isCatalogCheckRequired: filetype == FileExtensionTypes.signedZip.ToString()); } else { if (!string.IsNullOrEmpty(executableFileName)) - File.Copy(tempDestination, Path.Combine(destinationPath, executableFileName + (!isSupportedFileExtention ? filetype : FileSystemService.GetFileExtention(enFiletype))), true); + { + string copiedPath = Path.Combine(destinationPath, executableFileName + (!isSupportedFileExtension ? filetype : FileSystemService.GetFileExtension(enFiletype))); + File.Copy(tempDestination, copiedPath, true); + _extractedFiles.Add(copiedPath); + } else { string exeName = string.IsNullOrEmpty(filename) ? GetFileNameFromUri(url) : filename; if (string.IsNullOrEmpty(exeName)) throw new IOException(); - string extention = !isSupportedFileExtention? filetype : FileSystemService.GetFileExtention(enFiletype); + string extension = !isSupportedFileExtension? filetype : FileSystemService.GetFileExtension(enFiletype); - File.Copy(tempDestination, Path.Combine(destinationPath, exeName + extention), true); + string copiedPath = Path.Combine(destinationPath, exeName + extension); + File.Copy(tempDestination, copiedPath, true); + _extractedFiles.Add(copiedPath); } } - if (filetype == FileExtentionTypes.msi.ToString() || filetype == FileExtentionTypes.elmsi.ToString()) + if (filetype == FileExtensionTypes.msi.ToString() || filetype == FileExtensionTypes.elmsi.ToString()) { StageChanged?.Invoke(Tuple.Create(OperationId, "ConnectingToService")); string installerExeName = GetFileNameFromUri(url); installerExeName = string.IsNullOrEmpty(installerExeName) ? "installer" : installerExeName; - string msiPath = Path.Combine(destinationPath, installerExeName + (!isSupportedFileExtention ? filetype : FileSystemService.GetFileExtention(enFiletype))); + string msiPath = Path.Combine(destinationPath, installerExeName + (!isSupportedFileExtension ? filetype : FileSystemService.GetFileExtension(enFiletype))); msiGUID = Guid.NewGuid().ToString(); MsiInstallerService msiInstallerHelper = new(msiGUID, msiPath); msiInstallerHelper.callbackAction += HandleMsiInstallerMessage; @@ -165,11 +173,14 @@ await requestWorker.DownloadFileAsync(url, tempDestination, ReportDownloadProgre { try { if (File.Exists(tempDestination)) File.Delete(tempDestination); } catch { } - if (!success || removeAfterAction) + // An installer copied from this download is removed again when it failed to install + // only; other files may have replaced the files of the installed version and + // stay. Archives of signed packs are checked in a separate folder by ZipService and + // never reach the destination unchecked. + bool isInstaller = filetype == FileExtensionTypes.msi.ToString() || filetype == FileExtensionTypes.elmsi.ToString(); + if (removeAfterAction || (!success && isInstaller)) { - try { if (File.Exists(destinationPath)) File.Delete(destinationPath); } catch { } - - foreach (var file in _extractedFiles ?? Enumerable.Empty()) + foreach (var file in _extractedFiles) { try { if (File.Exists(file)) File.Delete(file); } catch { } } @@ -185,7 +196,7 @@ public async Task DownloadAndExtractAsync( IEnumerable? extractSkipFiletypes = null, string? extractRootFolder = null, string? executableFileName = "executableFile", - FileExtentionTypes filetype = FileExtentionTypes.temp, + FileExtensionTypes filetype = FileExtensionTypes.temp, bool removeAfterAction = false, string filename = "" ) diff --git a/CDPIUI.Core/Store/Network/RequestWorker.cs b/CDPIUI.Core/Store/Network/RequestWorker.cs index 4454d02b..16c6b6ac 100644 --- a/CDPIUI.Core/Store/Network/RequestWorker.cs +++ b/CDPIUI.Core/Store/Network/RequestWorker.cs @@ -12,14 +12,17 @@ internal sealed class RequestWorker : IDisposable { private readonly Lazy directClient; private readonly Lazy proxyClient; - private readonly bool ownsClient; private int disposed; + // A client per request leaves a connection pool and sockets behind every time; the workers + // created for single requests share these instead. + private static readonly Lazy SharedDirectClient = new(() => new HttpClient(CreateHandler(proxyEnable: false))); + private static readonly Lazy SharedProxyClient = new(() => new HttpClient(CreateHandler(proxyEnable: true))); + public RequestWorker(HttpClient? client = null) { - directClient = new(() => client ?? new HttpClient(CreateHandler(proxyEnable: false))); - proxyClient = new(() => client ?? new HttpClient(CreateHandler(proxyEnable: true))); - ownsClient = client == null; + directClient = client != null ? new(() => client) : SharedDirectClient; + proxyClient = client != null ? new(() => client) : SharedProxyClient; } private static HttpClientHandler CreateHandler(bool proxyEnable) @@ -74,6 +77,7 @@ public async Task DownloadFileAsync( response.EnsureSuccessStatusCode(); long totalBytes = response.Content.Headers.ContentLength ?? -1L; + EnsureFreeSpace(destinationPath, totalBytes); using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); cancellationToken.ThrowIfCancellationRequested(); using var fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None, 81920, true); @@ -81,10 +85,25 @@ public async Task DownloadFileAsync( long totalRead = 0; var stopwatch = Stopwatch.StartNew(); var lastUpdate = stopwatch.Elapsed; - int read; - while ((read = await contentStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0) + // HttpClient.Timeout ends with the response headers: a connection that stops sending + // the body would wait forever, so every read gets its own deadline. + using var stallCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + while (true) { + stallCts.CancelAfter(ReadStallTimeout); + int read; + try + { + read = await contentStream.ReadAsync(buffer.AsMemory(0, buffer.Length), stallCts.Token); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + throw new TimeoutException($"The download stopped: no data for {ReadStallTimeout.TotalSeconds} seconds."); + } + if (read == 0) + break; + await fileStream.WriteAsync(buffer, 0, read, cancellationToken); totalRead += read; @@ -97,12 +116,34 @@ public async Task DownloadFileAsync( } } + private static readonly TimeSpan ReadStallTimeout = TimeSpan.FromSeconds(60); + + private static void EnsureFreeSpace(string destinationPath, long requiredBytes) + { + if (requiredBytes <= 0) + return; + try + { + string? root = Path.GetPathRoot(Path.GetFullPath(destinationPath)); + if (string.IsNullOrEmpty(root)) + return; + var drive = new DriveInfo(root); + if (drive.IsReady && drive.AvailableFreeSpace < requiredBytes) + throw new IOException($"Not enough free space on {root}: {requiredBytes} bytes are needed, {drive.AvailableFreeSpace} are free."); + } + catch (ArgumentException) + { + // A network path or another root DriveInfo does not support: no check. + } + } + private static HttpRequestMessage CreateRequest( string url, string? token, string authenticationScheme, bool useStoreUserAgent, bool proxyEnable) { - if (!Uri.TryCreate(url, UriKind.Absolute, out var sourceUri) || - (sourceUri.Scheme != Uri.UriSchemeHttp && sourceUri.Scheme != Uri.UriSchemeHttps)) - throw new ArgumentException("The source URL must be an absolute HTTP or HTTPS URL.", nameof(url)); + // Downloads carry components and installers without signatures of their own, so plain HTTP + // would let anyone on the way replace them. + if (!Uri.TryCreate(url, UriKind.Absolute, out var sourceUri) || sourceUri.Scheme != Uri.UriSchemeHttps) + throw new ArgumentException("The source URL must be an absolute HTTPS URL.", nameof(url)); var request = new HttpRequestMessage(HttpMethod.Get, proxyEnable ? CreateProxyUri(url) : sourceUri); try @@ -142,15 +183,7 @@ private static Uri CreateProxyUri(string url) return builder.Uri; } - public void Dispose() - { - if (Interlocked.Exchange(ref disposed, 1) == 0 && ownsClient) - { - if (directClient.IsValueCreated) - directClient.Value.Dispose(); - if (proxyClient.IsValueCreated) - proxyClient.Value.Dispose(); - } - } + // The clients are shared or owned by the caller, so only this worker is marked as disposed. + public void Dispose() => Interlocked.Exchange(ref disposed, 1); } } diff --git a/CDPIUI.Core/Store/OnlineItemsInstallationService.cs b/CDPIUI.Core/Store/OnlineItemsInstallationService.cs index 0c6c484f..581fc91f 100644 --- a/CDPIUI.Core/Store/OnlineItemsInstallationService.cs +++ b/CDPIUI.Core/Store/OnlineItemsInstallationService.cs @@ -5,9 +5,9 @@ using CDPIUI.Core.Store.Queue; using CDPIUI.Core.Store.ViewModels; using CDPIUI.Shared.Basic.Filesystem; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Shared.Models; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System.Collections.Generic; using System.Diagnostics; @@ -64,12 +64,19 @@ public async Task LaunchBeforeInstallActions(string? actions, string destD string url = _p[0]; string filename = _p[1]; + // Both come from item metadata: download only over HTTPS and only into the item folder. + if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri) || uri.Scheme != Uri.UriSchemeHttps) + throw new InvalidDataException($"Only HTTPS downloads are allowed: {url}"); + if (string.IsNullOrWhiteSpace(filename) || filename != Path.GetFileName(filename) || + filename == "." || filename == ".." || filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) + throw new InvalidDataException($"Invalid download file name: {filename}"); + if (DownloadWorker == null) { throw new NullReferenceException(nameof(DownloadWorker)); } - string extention = Path.GetExtension(FileSystemService.GetFileNameFromUrl(url)); + string extension = Path.GetExtension(FileSystemService.GetFileNameFromUrl(url)); bool result = await DownloadWorker.DownloadAndExtractAsync( url, @@ -78,7 +85,7 @@ await DownloadWorker.DownloadAndExtractAsync( null, null, string.Empty, - extention, + extension, false, filename); if (!result) return false; @@ -144,7 +151,7 @@ RepoItemModel item .FailureResult(ErrorModel.OnlyErrorCode(PrettyErrorCode.UNKNOWN)); } - FileExtentionTypes filetype = downloadLinkModel.type.ToEnum(FileExtentionTypes.temp); + FileExtensionTypes filetype = downloadLinkModel.type.ToEnum(FileExtensionTypes.temp); bool result = await DownloadWorker.DownloadAndExtractAsync( downloadLinkModel.link!, @@ -164,10 +171,14 @@ RepoItemModel item if (!result) return OperationResultModel .FailureResult(DownloadWorker.LastError); + + downloadUrl = downloadLinkModel.link ?? downloadUrl; + if (!string.IsNullOrEmpty(downloadLinkModel.version)) + tag = downloadLinkModel.version; } if (restartFlag) - Logger.Instance.CreateDebugLog(nameof(StoreHelper), "Restart requested"); + CoreEvents.Instance.InvokeRestartRequired(id); return OperationResultModel.SuccessResult(new APILinkModel() { @@ -193,7 +204,7 @@ private async Task>> GetDownloadLinksForIt case ItemsVersionControlTypes.git_only_last: var onlyLastVesionData = await APIWorker.GetDownloadLinkForVersion( - item.version_control_link, item.filetype, version: version, prefferedFile: item.preffered_to_download_file_name); + item.version_control_link, item.filetype, version: version, preferredFile: item.preffered_to_download_file_name); if (onlyLastVesionData.Success && onlyLastVesionData.Result != null) { @@ -209,7 +220,7 @@ await APIWorker.GetDownloadLinkForVersion( case ItemsVersionControlTypes.git: var data = await APIWorker.GetDownloadLinkForVersion( - item.version_control_link, item.filetype, version: version, prefferedFile: item.preffered_to_download_file_name); + item.version_control_link, item.filetype, version: version, preferredFile: item.preffered_to_download_file_name); if (data.Success && data.Result != null) { diff --git a/CDPIUI.Core/Store/Queue/IQueueManagerService.cs b/CDPIUI.Core/Store/Queue/IQueueManagerService.cs index e210126d..17a57ee0 100644 --- a/CDPIUI.Core/Store/Queue/IQueueManagerService.cs +++ b/CDPIUI.Core/Store/Queue/IQueueManagerService.cs @@ -15,7 +15,7 @@ internal interface IQueueManagerService /// Item version (null if latest) /// Clean files after instaliing (if msi-like) /// Pack file (null if online download - void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null); + void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null, bool isSignedPack = false); /// /// Cancel operation for item id @@ -27,7 +27,7 @@ internal interface IQueueManagerService /// /// Get current operation id /// - /// Operation id. null if no operation runned + /// Operation id. null if no operation running string GetCurrentQueueOperationId(); /// diff --git a/CDPIUI.Core/Store/Queue/QueueItemModel.cs b/CDPIUI.Core/Store/Queue/QueueItemModel.cs index cc6168db..9144a5e9 100644 --- a/CDPIUI.Core/Store/Queue/QueueItemModel.cs +++ b/CDPIUI.Core/Store/Queue/QueueItemModel.cs @@ -1,12 +1,14 @@ namespace CDPIUI.Core.Store.Queue { - public class QueueItemModel(string itemId, string operationId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFilePath = null) + public class QueueItemModel(string itemId, string operationId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFilePath = null, bool isSignedPack = false) { public string OperationId { get; } = operationId; public string ItemId { get; } = itemId; public string Version { get; } = version ?? string.Empty; public bool CleanDirectoryBeforeInstalling { get; } = cleanDirectoryBeforeInstalling; public string? PackFilePath { get; } = packFilePath ?? string.Empty; + // A local pack whose catalog signature was verified; only such packs may bring executables and install actions. + public bool IsSignedPack { get; } = isSignedPack; public string Status { get; set; } = "WAIT"; public string DownloadStage { get; set; } = string.Empty; public double DownloadProgress { get; set; } diff --git a/CDPIUI.Core/Store/Queue/QueueManagerService.cs b/CDPIUI.Core/Store/Queue/QueueManagerService.cs index ae90a0ca..7760e0e0 100644 --- a/CDPIUI.Core/Store/Queue/QueueManagerService.cs +++ b/CDPIUI.Core/Store/Queue/QueueManagerService.cs @@ -11,32 +11,38 @@ internal class QueueManagerService : IQueueManagerService public QueueItemModel? CurrentDownloadingItem { get; set; } - public void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null) // PUBLIC + // Every access to Queue and CurrentDownloadingItem changes goes through _queueLock: the + // queue is used from the UI thread, download callbacks and the install worker. + // Events are raised outside the lock. + public void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null, bool isSignedPack = false) // PUBLIC { RemoveItemFromDownloadFailureList(itemId); - if (GetOperationIdFromItemId(itemId) != null) return; - - var opId = Guid.NewGuid().ToString(); - var qi = new QueueItemModel(itemId, opId, version, cleanDirectoryBeforeInstalling, packFilePath: packFile); lock (_queueLock) { - Queue.Enqueue(qi); - TryProcessNext(); + if (GetOperationIdFromItemId(itemId) != null) return; + + var opId = Guid.NewGuid().ToString(); + Queue.Enqueue(new QueueItemModel(itemId, opId, version, cleanDirectoryBeforeInstalling, packFilePath: packFile, isSignedPack: isSignedPack)); } - QueueUpdated?.Invoke(); + TryProcessNext(); } public bool RemoveItemFromQueue(string itemId) { - var items = Queue.ToList(); - var removed = items.RemoveAll(i => i.ItemId == itemId) > 0; - if (removed) + bool removed; + lock (_queueLock) { - Queue.Clear(); - foreach (var i in items) Queue.Enqueue(i); - QueueUpdated?.Invoke(); + var items = Queue.ToList(); + removed = items.RemoveAll(i => i.ItemId == itemId) > 0; + if (removed) + { + Queue.Clear(); + foreach (var i in items) Queue.Enqueue(i); + } } + if (removed) + QueueUpdated?.Invoke(); var currentItem = CurrentDownloadingItem; if (currentItem != null && currentItem.ItemId == itemId) @@ -54,70 +60,63 @@ public bool RemoveItemFromQueue(string itemId) return removed; } + /// + /// A copy of the waiting items. + /// public Queue GetQueue() { - return Queue; + lock (_queueLock) + return new Queue(Queue); } public string GetCurrentQueueOperationId() { - return CurrentDownloadingItem != null ? CurrentDownloadingItem.OperationId : string.Empty; + var current = CurrentDownloadingItem; + return current != null ? current.OperationId : string.Empty; } public string? GetOperationIdFromItemId(string storeId) { - if (CurrentDownloadingItem != null && CurrentDownloadingItem.ItemId == storeId) - { - return CurrentDownloadingItem.OperationId; - } - foreach (var item in Queue) + lock (_queueLock) { - if (item.ItemId == storeId) - return item.OperationId; + var current = CurrentDownloadingItem; + if (current != null && current.ItemId == storeId) + return current.OperationId; + return Queue.FirstOrDefault(item => item.ItemId == storeId)?.OperationId; } - return null; } public string? GetItemIdFromOperationId(string operationId) { - if (CurrentDownloadingItem != null && CurrentDownloadingItem.OperationId == operationId) - return CurrentDownloadingItem.ItemId; - - foreach (var item in Queue) - { - if (item.OperationId == operationId) - return item.ItemId; - } - return null; + return GetQueueItemFromOperationId(operationId)?.ItemId; } public QueueItemModel? GetQueueItemFromOperationId(string operationId) { - if (CurrentDownloadingItem != null && CurrentDownloadingItem.OperationId == operationId) - return CurrentDownloadingItem; - - foreach (var item in Queue) + lock (_queueLock) { - if (item.OperationId == operationId) - return item; + var current = CurrentDownloadingItem; + if (current != null && current.OperationId == operationId) + return current; + return Queue.FirstOrDefault(item => item.OperationId == operationId); } - return null; } public void TryProcessNext() { - QueueUpdated?.Invoke(); - if (Queue.Count == 0) + QueueItemModel? next = null; + lock (_queueLock) { - return; + if (CurrentDownloadingItem == null && Queue.Count > 0) + { + next = Queue.Dequeue(); + CurrentDownloadingItem = next; + } } - if (CurrentDownloadingItem == null) + QueueUpdated?.Invoke(); + if (next != null) { - - var next = Queue.Dequeue(); - CurrentDownloadingItem = next; - ProcessItem?.Invoke(next); QueueUpdated?.Invoke(); } diff --git a/CDPIUI.Core/Store/Repository/Localization/LocalizationService.cs b/CDPIUI.Core/Store/Repository/Localization/LocalizationService.cs index 221afbfc..9c9fd10a 100644 --- a/CDPIUI.Core/Store/Repository/Localization/LocalizationService.cs +++ b/CDPIUI.Core/Store/Repository/Localization/LocalizationService.cs @@ -15,7 +15,7 @@ public static string GetStoreLikeLocale() return ApplicationInfo.Instance.Localization switch { "ru" => "RU", - "en-US" => "EN", + "en-us" or "en-US" => "EN", _ => "EN", }; } @@ -26,7 +26,23 @@ internal class LocalizationService : ILocalizationService public string? LocalizationName; public Dictionary? LocalizationDict; - public Dictionary StoreLocalizationPaths = []; + private Dictionary storeLocalizationPaths = []; + private readonly object cacheLock = new(); + + // Set after every repository sync: the cached strings were kept from the previous one. + public Dictionary StoreLocalizationPaths + { + get => storeLocalizationPaths; + set + { + lock (cacheLock) + { + storeLocalizationPaths = value ?? []; + LocalizationName = null; + LocalizationDict = null; + } + } + } public string GetLocalizedStoreItemName(string name, string langCode) { @@ -39,30 +55,33 @@ public string GetLocalizedStoreItemName(string name, string langCode) try { - if (LocalizationName != langCode) + lock (cacheLock) { - string locFilePath; - if (!StoreLocalizationPaths.TryGetValue(langCode, out string value)) + if (LocalizationName != langCode || LocalizationDict == null) { - locFilePath = Path.Combine(localRepoFolder, StoreLocalizationPaths["EN"]); - } - else - { - locFilePath = Path.Combine(localRepoFolder, value); - } + string locFilePath; + if (!StoreLocalizationPaths.TryGetValue(langCode, out string value)) + { + locFilePath = Path.Combine(localRepoFolder, StoreLocalizationPaths["EN"]); + } + else + { + locFilePath = Path.Combine(localRepoFolder, value); + } - using (StreamReader r = new StreamReader(locFilePath)) - { - string json = r.ReadToEnd(); - Dictionary localizationDict = JSONConvertor.DeserializeObject>(json); + using (StreamReader r = new StreamReader(locFilePath)) + { + string json = r.ReadToEnd(); + Dictionary localizationDict = JSONConvertor.DeserializeObject>(json); - LocalizationName = langCode; - LocalizationDict = localizationDict; - } + LocalizationName = langCode; + LocalizationDict = localizationDict; + } + } + if (LocalizationDict != null && LocalizationDict.TryGetValue(name, out string? localized)) + localizedString = localized; } - localizedString = LocalizationDict[name]; - } catch (Exception ex) { diff --git a/CDPIUI.Core/Store/Repository/RepositoryLoaderService.cs b/CDPIUI.Core/Store/Repository/RepositoryLoaderService.cs index 865a1c44..e110487a 100644 --- a/CDPIUI.Core/Store/Repository/RepositoryLoaderService.cs +++ b/CDPIUI.Core/Store/Repository/RepositoryLoaderService.cs @@ -5,11 +5,12 @@ using CDPIUI.Core.Store.Network; using CDPIUI.Core.Store.ViewModels; using CDPIUI.Shared; -using CDPIUI.Shared.Extentions; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.Extensions; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.Shared.Secrets; using System.Diagnostics; using System.IO.Compression; +using CDPIUI.Core.System; namespace CDPIUI.Core.Store.Repository { @@ -58,7 +59,7 @@ public static async Task TryLoadDatabaseForVersionControl(SupportedVersion { string zipUrl = GetStoreUrl(versionControl); - string tempZipPath = Path.Combine(Path.GetTempPath(), "store_repo.tmp"); + string tempZipPath = Path.Combine(Path.GetTempPath(), $"cdpiui_store_{Guid.NewGuid():N}.tmp"); using var requestWorker = new RequestWorker(); await requestWorker.DownloadFileAsync(zipUrl, tempZipPath, token: versionControl == SupportedVersionControls.GitHub ? GitHubApiToken : GitLabApiToken, @@ -93,45 +94,16 @@ public async Task LoadAllStoreDatabase(bool forseSync, SupportedVersionCon if ((forseSync && t.TotalDays >= 1) || !Path.Exists(targetFolder)) { - string zipUrl = GetStoreUrl(usedVersionControl); - - Debug.WriteLine(zipUrl); - string tempZipPath = Path.Combine(Path.GetTempPath(), "store_repo.tmp"); - using var requestWorker = new RequestWorker(); - await requestWorker.DownloadFileAsync(zipUrl, tempZipPath, - token: usedVersionControl == SupportedVersionControls.GitHub ? GitHubApiToken : GitLabApiToken, - useStoreUserAgent: true, - completionOption: HttpCompletionOption.ResponseContentRead); - - if (Directory.Exists(targetFolder)) - Directory.Delete(targetFolder, recursive: true); - Directory.CreateDirectory(targetFolder); - - using (var archive = ZipFile.OpenRead(tempZipPath)) + try { - foreach (var entry in archive.Entries) - { - if (string.IsNullOrEmpty(entry.Name) && entry.FullName.EndsWith("/")) - continue; - - var parts = entry.FullName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length <= 1) - continue; - - string[] subParts = parts.Skip(1).ToArray(); - string relativePath = Path.Combine(subParts); - - string destinationPath = Path.Combine(targetFolder, relativePath); - - string destDir = Path.GetDirectoryName(destinationPath); - if (!string.IsNullOrEmpty(destDir)) - Directory.CreateDirectory(destDir); - - entry.ExtractToFile(destinationPath, overwrite: true); - } + await DownloadAndExtractStore(usedVersionControl, targetFolder); + } + catch (Exception ex) when (usedVersionControl == SupportedVersionControls.GitHub) + { + // GitHub is blocked for some users: the GitLab mirror has the same content (issue #26). + Logger.Instance.CreateWarningLog(nameof(RepositoryLoaderService), $"GitHub is not available, using GitLab: {ex.Message}"); + await DownloadAndExtractStore(SupportedVersionControls.GitLab, targetFolder); } - - File.Delete(tempZipPath); SettingsManager.Instance.SetValue("STORE", "lastSyncTime", DateTime.UtcNow); } @@ -153,6 +125,82 @@ await requestWorker.DownloadFileAsync(zipUrl, tempZipPath, return false; } + /// + /// Downloads the Store repository and replaces the cache only after the archive was + /// extracted completely, so a failed download or a damaged archive keeps the old cache. + /// + private async Task DownloadAndExtractStore(SupportedVersionControls versionControl, string targetFolder) + { + string zipUrl = GetStoreUrl(versionControl); + Debug.WriteLine(zipUrl); + + // A fixed name in the shared temp folder could be prepared by another process. + string tempZipPath = Path.Combine(Path.GetTempPath(), $"cdpiui_store_{Guid.NewGuid():N}.tmp"); + string newFolder = $"{targetFolder.TrimEnd(Path.DirectorySeparatorChar)}.new-{Guid.NewGuid():N}"; + try + { + using (var requestWorker = new RequestWorker()) + { + await requestWorker.DownloadFileAsync(zipUrl, tempZipPath, + token: versionControl == SupportedVersionControls.GitHub ? GitHubApiToken : GitLabApiToken, + useStoreUserAgent: true, + completionOption: HttpCompletionOption.ResponseContentRead); + } + + Directory.CreateDirectory(newFolder); + using (var archive = ZipFile.OpenRead(tempZipPath)) + { + foreach (var entry in archive.Entries) + { + if (string.IsNullOrEmpty(entry.Name) && entry.FullName.EndsWith("/")) + continue; + + var parts = entry.FullName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length <= 1) + continue; + + string[] subParts = parts.Skip(1).ToArray(); + string relativePath = Path.Combine(subParts); + + string destinationPath = ArchivePaths.GetDestinationOrThrow(newFolder, relativePath); + + string? destDir = Path.GetDirectoryName(destinationPath); + if (!string.IsNullOrEmpty(destDir)) + Directory.CreateDirectory(destDir); + + entry.ExtractToFile(destinationPath, overwrite: true); + } + } + + if (Directory.Exists(targetFolder)) + Directory.Delete(targetFolder, recursive: true); + Directory.Move(newFolder, targetFolder); + } + finally + { + try { File.Delete(tempZipPath); } catch (IOException) { } + try + { + if (Directory.Exists(newFolder)) + Directory.Delete(newFolder, recursive: true); + } + catch (IOException) { } + } + } + + private static T? TryLoad(string path) where T : class + { + try + { + return JSONConvertor.LoadJson(path); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(RepositoryLoaderService), $"Skipping \"{path}\": {ex.Message}"); + return null; + } + } + public RepoItemModel? GetItemInfoFromStoreId(string? storeId) { if (storeId == SharedConstants.LocalUserItemsId) @@ -269,9 +317,11 @@ private List GetFormattedStoreDatabase() continue; } - RepoCategoryInitializationModel repoCategoryInit = JSONConvertor.LoadJson(categoryInitPath); + // One damaged file skips its category or item instead of the whole catalog. + RepoCategoryInitializationModel? repoCategoryInit = TryLoad(categoryInitPath); - if (!SupportedCategoryTypes.Contains(repoCategoryInit.type)) + if (repoCategoryInit?.items == null || repoCategoryInit.items_directories == null || + !SupportedCategoryTypes.Contains(repoCategoryInit.type)) { Logger.Instance.CreateDebugLog(nameof(RepositoryLoaderService), $"Skip the category: {categoryName}, {categoryPath}"); continue; @@ -305,7 +355,9 @@ private List GetFormattedStoreDatabase() continue; } - RepoItemModel repoCategoryItemInit = JSONConvertor.LoadJson(categoryItemInitPath); + RepoItemModel? repoCategoryItemInit = TryLoad(categoryItemInitPath); + if (repoCategoryItemInit == null) + continue; repoCategoryItemInit.category_id = repoCategoryInit.store_id; diff --git a/CDPIUI.Core/Store/StoreHelper.cs b/CDPIUI.Core/Store/StoreHelper.cs index 3544b9f0..68dd05fa 100644 --- a/CDPIUI.Core/Store/StoreHelper.cs +++ b/CDPIUI.Core/Store/StoreHelper.cs @@ -8,7 +8,7 @@ using CDPIUI.Core.Data; using CDPIUI.Core.Store.Repository.Localization; using CDPIUI.Core.Store.Queue; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.Shared; using CDPIUI.Core.LScript; using CDPIUI.Core.JSON; @@ -99,7 +99,7 @@ private StoreHelper() APIWorker.Token = RepositoryLoaderService.GetToken(); } - private void RepositoryLoaderService_InternalErrorHappens(Shared.PrettyErrorConvertionService.ErrorModel obj) + private void RepositoryLoaderService_InternalErrorHappens(Shared.PrettyErrorConversionService.ErrorModel obj) { StoreInternalErrorHappens?.Invoke(obj.FriendlyDescription ?? obj.ErrorCode); } @@ -185,8 +185,8 @@ private void AddItemToDownloadFailureList(string itemId, string operationId, str #endregion #region Queue - public void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null) => - QueueManagerService.AddItemToQueue(itemId, version, cleanDirectoryBeforeInstalling, packFile); + public void AddItemToQueue(string itemId, string? version = null, bool cleanDirectoryBeforeInstalling = false, string? packFile = null, bool isSignedPack = false) => + QueueManagerService.AddItemToQueue(itemId, version, cleanDirectoryBeforeInstalling, packFile, isSignedPack); public bool RemoveItemFromQueue(string itemId) => QueueManagerService.RemoveItemFromQueue(itemId); @@ -226,19 +226,25 @@ private async Task ProcessAsync(QueueItemModel qi) cancellationTokenSource = operationSource; cancellationToken = operationSource.Token; + bool installed = false; try { qi.Status = "GETR"; qi.DownloadStage =qi.Status; ItemDownloadStageChanged?.Invoke(Tuple.Create(qi.OperationId, qi.Status)); - await InstallItem(qi); + installed = await InstallItem(qi); qi.Status = operationSource.IsCancellationRequested ? "CANC" : "END"; qi.DownloadStage = qi.Status; ItemDownloadStageChanged?.Invoke(Tuple.Create(qi.OperationId, qi.Status)); } - catch + catch (Exception ex) { - // pass + // The exception was swallowed and the item stayed in GETR/WORK with no error shown. + Logger.Instance.CreateErrorLog(nameof(StoreHelper), $"Installation of {qi.ItemId} failed: {ex}"); + qi.Status = "END"; + qi.DownloadStage = qi.Status; + ItemInstallingErrorHappens?.Invoke(Tuple.Create(qi.OperationId, ErrorModel.OnlyErrorCode(HandleException(ex)))); + ItemDownloadStageChanged?.Invoke(Tuple.Create(qi.OperationId, qi.Status)); } finally { @@ -248,7 +254,8 @@ private async Task ProcessAsync(QueueItemModel qi) } ItemActionsStopped?.Invoke(qi.ItemId); - var _item = UpdatesAvailableList.FirstOrDefault(x => x.StoreId == qi.ItemId); + // Only an installed update leaves the list; a failed one is still available. + var _item = installed ? UpdatesAvailableList.FirstOrDefault(x => x.StoreId == qi.ItemId) : null; if (_item != null) { UpdatesAvailableList.Remove(_item); @@ -265,8 +272,11 @@ private async Task ProcessAsync(QueueItemModel qi) public void RemoveItem(string itemId) { - _ = ProcessService.StopService(); - _ = ComponentTasksManager.Instance.StopTask(itemId); + _ = RemoveItemAsync(itemId); + } + + public async Task RemoveItemAsync(string itemId) + { var item = DatabaseHelper.Instance.GetItemById(itemId); if (item == null || @@ -274,92 +284,146 @@ public void RemoveItem(string itemId) item.Id == SharedConstants.LocalUserItemsId) return; - try + // The component has to exit before its files can be deleted (issue #37). + await StopComponentForFileChanges(item); + + // The directory comes from the item database; never delete anything outside the item folders. + if (Path.Exists(item.Directory) && StoreItemIds.IsItemFolder(item.Directory)) { - if (Path.Exists(item.Directory)) + for (int attempt = 1; ; attempt++) { - Directory.Delete(item.Directory, recursive: true); + try + { + Directory.Delete(item.Directory, recursive: true); + break; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + if (attempt == 5) + { + Logger.Instance.CreateErrorLog(nameof(StoreHelper), $"Cannot delete \"{item.Directory}\": {ex.Message}"); + break; + } + await Task.Delay(500 * attempt); + } } - } - catch { } DatabaseHelper.Instance.DeleteItemById(itemId); ItemRemoved?.Invoke(itemId); } + /// + /// Stops the component before its files are replaced or deleted, together with the + /// driver service it may hold open. Returns whether the component was running. + /// + private static async Task StopComponentForFileChanges(DatabaseStoreItem item) + { + if (item.Type != "component") + return false; + + bool wasRunning = false; + try + { + var task = await ComponentTasksManager.Instance.GetTaskFromId(item.Id!); + wasRunning = task?.ProcessManager?.IsProcessRunning ?? false; + await ComponentTasksManager.Instance.StopTask(item.Id!); + await ProcessService.StopService(); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(StoreHelper), $"Cannot stop {item.Id}: {ex.Message}"); + } + return wasRunning; + } + public async Task CheckUpdates() { if (IsNowUpdatesChecked) return; IsNowUpdatesChecked = true; - UpdatesAvailableList.Clear(); - UpdateCheckStarted?.Invoke(); - - List storeItems = DatabaseHelper.Instance.GetAllInstalledItems(); bool exceptionHappens = false; - - foreach (var item in storeItems) + // The flag must be reset whatever happens, otherwise the Store shows "checking" forever. + try { - if (item.Id == SharedConstants.LocalUserItemsId) continue; + UpdatesAvailableList.Clear(); + UpdateCheckStarted?.Invoke(); - string downloadUrl = item.DownloadUrl; - string versionControlType = item.VersionControlType; - string directory = item.Directory; + List storeItems = DatabaseHelper.Instance.GetAllInstalledItems(); - string repoUrl = GetReadyToUseRepoUrl(item.UpdateCheckUrl, item.Id); + foreach (var item in storeItems) + { + if (item.Id == SharedConstants.LocalUserItemsId || item.VersionControlType == "local") continue; - var versionDataCheckResult = await APIWorker.GetLastVersionAndVersionNotes(repoUrl); + string downloadUrl = item.DownloadUrl; + string versionControlType = item.VersionControlType; + string directory = item.Directory; - if (versionDataCheckResult.ErrorHappens || !versionDataCheckResult.Success) - { - Logger.Instance.CreateWarningLog( - $"{nameof(StoreHelper)}/{nameof(CheckUpdates)}", - $"Cannot check updates for {item.Id}, with version control type {item.VersionControlType}. Uri used to check {repoUrl} " + - $"Exception information: {versionDataCheckResult.Error.ErrorCode}"); - exceptionHappens = true; - continue; - } + string? repoUrl = GetReadyToUseRepoUrl(item.UpdateCheckUrl, item.Id); + // Local packs have no update source; that is not an error. + if (string.IsNullOrEmpty(repoUrl)) + continue; - var versionData = versionDataCheckResult.Result; + var versionDataCheckResult = await APIWorker.GetLastVersionAndVersionNotes(repoUrl); - try - { - string curV = item.CurrentVersion; - string serV = versionData.ReleaseTag; + if (versionDataCheckResult.ErrorHappens || !versionDataCheckResult.Success) + { + Logger.Instance.CreateWarningLog( + $"{nameof(StoreHelper)}/{nameof(CheckUpdates)}", + $"Cannot check updates for {item.Id}, with version control type {item.VersionControlType}. Uri used to check {repoUrl} " + + $"Exception information: {versionDataCheckResult.Error.ErrorCode}"); + exceptionHappens = true; + continue; + } - if (VersionHelper.CompareVersionStrings(curV, serV) == -1) + var versionData = versionDataCheckResult.Result; + + try { - UpdatesAvailableList.Add(new() + string curV = item.CurrentVersion; + string serV = versionData.ReleaseTag; + + if (VersionHelper.CompareVersionStrings(curV, serV) == -1) { - StoreId = item.Id, - CurrentVersion = item.CurrentVersion, - ServerVersion = versionData.ReleaseTag, - VersionInfo = versionData.ReleaseNotes, - }); + UpdatesAvailableList.Add(new() + { + StoreId = item.Id, + CurrentVersion = item.CurrentVersion, + ServerVersion = versionData.ReleaseTag, + VersionInfo = versionData.ReleaseNotes, + }); + } + } + catch + { + Logger.Instance.CreateWarningLog( + $"{nameof(StoreHelper)}/{nameof(CheckUpdates)}", + $"Cannot compare versions {item.CurrentVersion}&&{versionData.ReleaseNotes} for {item.Id}"); + exceptionHappens = true; + continue; } - } - catch - { - Logger.Instance.CreateWarningLog( - $"{nameof(StoreHelper)}/{nameof(CheckUpdates)}", - $"Cannot compare versions {item.CurrentVersion}&&{versionData.ReleaseNotes} for {item.Id}"); - exceptionHappens = true; - continue; } } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog($"{nameof(StoreHelper)}/{nameof(CheckUpdates)}", ex.ToString()); + exceptionHappens = true; + } + finally + { + IsNowUpdatesChecked = false; + IsExceptonHappensWhileCheckingUpdates = exceptionHappens; - IsNowUpdatesChecked = false; - IsExceptonHappensWhileCheckingUpdates = exceptionHappens; - - UpdateCheckStopped?.Invoke(); + UpdateCheckStopped?.Invoke(); + } } - private async Task InstallItem(QueueItemModel qi) + private async Task InstallItem(QueueItemModel qi) { + bool wasInstalled = DatabaseHelper.Instance.IsItemInstalled(qi.ItemId); var result = await InstallItemWorker(qi); if (cancellationToken?.IsCancellationRequested == true) - return; + return false; if (!result.Success) { @@ -378,10 +442,18 @@ private async Task InstallItem(QueueItemModel qi) try { - RemoveItem(qi.ItemId); + // A failed update keeps the installed version; only a failed first + // installation is cleaned up. + if (!wasInstalled) + await RemoveItemAsync(qi.ItemId); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(StoreHelper), $"Cannot clean up {qi.ItemId} after a failed installation: {ex.Message}"); } - catch { } + return false; } + return true; } private async Task> InstallItemWorker(QueueItemModel qi) @@ -392,10 +464,18 @@ private async Task> InstallItemWorker(QueueIte NowProcessItemActions?.Invoke(id); + if (!StoreItemIds.IsValid(id)) + return OperationResultModel + .FailureResult(ErrorModel.OnlyErrorCode(HandleException(new InvalidDataException($"Invalid store item id: {id}")))); + List> requiredItems = []; + bool restartAfterUpdate = false; if (DatabaseHelper.Instance.IsItemInstalled(id)) { - requiredItems = DatabaseHelper.Instance.GetItemById(id).RequiredItemIds; + DatabaseStoreItem installedItem = DatabaseHelper.Instance.GetItemById(id); + requiredItems = installedItem.RequiredItemIds; + // Running executables cannot be overwritten (issue #37). + restartAfterUpdate = await StopComponentForFileChanges(installedItem); } string itemFolder = Path.Combine(Directories.StoreItemsDirectory, qi.ItemId); @@ -453,7 +533,10 @@ private async Task> InstallItemWorker(QueueIte return gettingPatchReadyResult; } - return await AddItemToDatabase(tag, downloadUrl, itemFolder, item, requiredItems); + var addResult = await AddItemToDatabase(tag, downloadUrl, itemFolder, item, requiredItems); + if (restartAfterUpdate) + await ComponentTasksManager.Instance.CreateAndRunNewTask(id); + return addResult; } private async Task>> DownloadItemOnline(string itemId, QueueItemModel qi) @@ -522,7 +605,19 @@ private async Task>> D return OperationResultModel> .FailureResult(HandleException(PrettyErrorCode.PACK_NOT_SUPPORTED_TYPE)); - LocalItemInitModel localItemInitModel = JSONConvertor.LoadJson(initFilePath); + LocalItemInitModel? localItemInitModel; + try + { + localItemInitModel = JSONConvertor.LoadJson(initFilePath); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(StoreHelper), $"Cannot read \"{initFilePath}\": {ex.Message}"); + localItemInitModel = null; + } + if (localItemInitModel == null) + return OperationResultModel> + .FailureResult(HandleException(PrettyErrorCode.PACK_NOT_SUPPORTED_TYPE)); item = new() { @@ -530,15 +625,17 @@ private async Task>> D type = localItemInitModel.Type ?? "configlist", name = localItemInitModel.Name ?? localItemInitModel.ShortName ?? qi.ItemId, short_name = localItemInitModel.ShortName ?? qi.ItemId, - target_executable_file = localItemInitModel.ExecutableFile, + // An unsigned pack is only a set of presets and lists: it cannot name the executable + // the tray starts elevated, and it cannot run install actions. + target_executable_file = qi.IsSignedPack ? localItemInitModel.ExecutableFile : null, filetype = "localPack", version_control = "local", icon = localItemInitModel.Icon, developer = localItemInitModel.Developer, background = localItemInitModel.Color, dependencies = localItemInitModel.Requirements, - before_install_actions = localItemInitModel.BeforeInstallActions, - after_install_actions = localItemInitModel.AfterInstallActions, + before_install_actions = qi.IsSignedPack ? localItemInitModel.BeforeInstallActions : null, + after_install_actions = qi.IsSignedPack ? localItemInitModel.AfterInstallActions : null, }; tag = localItemInitModel.Version; downloadUrl = null; @@ -552,6 +649,29 @@ private async Task>> D .SuccessResult(Tuple.Create(tag, downloadUrl, item)); } + // A local pack names its icon as $DYNAMICIMAGE(icon.png) relative to the pack. Evaluated later + // without the pack folder, the path pointed into the repository cache, so the icon of a local + // pack is stored as a full path, and only when the file is inside the item folder. + private static string? GetIconPath(RepoItemModel item, string itemFolder) + { + const string prefix = "$DYNAMICIMAGE("; + string? icon = item.icon; + if (item.filetype != "localPack" || icon == null || + !icon.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) || !icon.EndsWith(')')) + return icon; + + try + { + string root = Path.GetFullPath(itemFolder).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + string path = Path.GetFullPath(Path.Combine(itemFolder, icon[prefix.Length..^1])); + return path.StartsWith(root, StringComparison.OrdinalIgnoreCase) && File.Exists(path) ? path : icon; + } + catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException) + { + return icon; + } + } + private static OperationResultModel CreateItemDirectory(string itemId, string itemFolder, bool clean) { if (itemId == SharedConstants.ApplicationStoreId || itemId == SharedConstants.LocalUserItemsId) @@ -582,8 +702,35 @@ private static OperationResultModel CopyLocalPack(string itemId, st { if (itemId != SharedConstants.ApplicationStoreId) { - if (Directory.Exists(itemFolder)) Directory.Delete(itemFolder, recursive: true); - Directory.Move(packFilePath, itemFolder); + // The installed version is replaced only by a pack that can be installed. + if (!File.Exists(Path.Combine(packFilePath, "init.json"))) + return OperationResultModel + .FailureResult(HandleException(PrettyErrorCode.PACK_NOT_SUPPORTED_TYPE)); + + string? previous = null; + if (Directory.Exists(itemFolder)) + { + previous = $"{itemFolder}.previous-{Guid.NewGuid():N}"; + Directory.Move(itemFolder, previous); + } + try + { + Directory.Move(packFilePath, itemFolder); + } + catch + { + if (previous != null && !Directory.Exists(itemFolder)) + Directory.Move(previous, itemFolder); + throw; + } + if (previous != null) + { + try { Directory.Delete(previous, recursive: true); } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Logger.Instance.CreateWarningLog(nameof(StoreHelper), $"Cannot delete the previous version \"{previous}\": {ex.Message}"); + } + } } else { @@ -609,9 +756,11 @@ private async Task> AddItemToDatabase( { List> _dependencies = []; + // A one-element entry in init.json threw here and failed the installation silently. foreach (string[] dependency in item.dependencies ?? []) { - _dependencies.Add(Tuple.Create(dependency[0], dependency[1])); + if (dependency is { Length: >= 2 }) + _dependencies.Add(Tuple.Create(dependency[0], dependency[1])); } DatabaseStoreItem databaseStoreItem = new() @@ -625,20 +774,27 @@ private async Task> AddItemToDatabase( Executable = item.target_executable_file, DownloadUrl = downloadUrl, DownloadFileType = item.filetype, - IconPath = item.icon, + IconPath = GetIconPath(item, itemFolder), UpdateCheckUrl = item.version_control_link, VersionControlType = item.version_control, DependentItemIds = _dependencies, RequiredItemIds = requiredItems, Developer = item.developer, - BackgroudColor = item.background, + BackgroundColor = item.background, }; + // Both action lists run with the item folder: install_msi of a "before" action had none + // and always failed. + Dictionary extraArgs = new() + { + { "CurrentDirectory", itemFolder } + }; + try { if (!await OnlineItemsInstallationService.LaunchBeforeInstallActions( - await LScriptCore.RunScript(item.before_install_actions), + await LScriptCore.RunScript(item.before_install_actions, extraArgs: extraArgs, cancellationToken: cancellationToken), itemFolder)) return OperationResultModel .UnSuccessResult(); @@ -649,14 +805,14 @@ await LScriptCore.RunScript(item.before_install_actions), // TODO: add RequiredItemIds to new installed item from store (foreach) Logger.Instance.CreateDebugLog(nameof(StoreHelper), $"Adding item {item.name} to database..."); - DatabaseHelper.Instance.AddOrUpdateItem(databaseStoreItem); - - Dictionary extraArgs = new() - { - { "CurrentDirectory", itemFolder } - }; + if (!DatabaseHelper.Instance.AddOrUpdateItem(databaseStoreItem)) + return OperationResultModel + .FailureResult(ErrorModel.OnlyErrorCode(HandleException(new IOException($"Cannot save {item.store_id} to the item database.")))); - await LScriptCore.RunScript(item.after_install_actions, extraArgs: extraArgs, cancellationToken: cancellationToken); + // download_easy_designer_annotation_file returns a download request; it was dropped. + string? afterDownloads = await LScriptCore.RunScript(item.after_install_actions, extraArgs: extraArgs, cancellationToken: cancellationToken); + if (!await OnlineItemsInstallationService.LaunchBeforeInstallActions(afterDownloads, itemFolder)) + Logger.Instance.CreateWarningLog(nameof(StoreHelper), $"Downloads after the installation of {item.store_id} failed."); Logger.Instance.CreateDebugLog(nameof(StoreHelper), $"Adding item {item.name}... COMPLETE"); @@ -669,48 +825,48 @@ await LScriptCore.RunScript(item.before_install_actions), } } - private static void AddDependencies(List dependencies, string id) + private static void AddDependencies(List? dependencies, string id) { - foreach (var dependency in dependencies) + foreach (var dependency in dependencies ?? []) { - if (!DatabaseHelper.Instance.IsItemInstalled(dependency[0])) + if (dependency.Length < 2 || !DatabaseHelper.Instance.IsItemInstalled(dependency[0])) continue; DatabaseStoreItem dependencyItem = DatabaseHelper.Instance.GetItemById(dependency[0]); - dependencyItem?.RequiredItemIds?.Add(Tuple.Create(id, dependency[1])); - if (dependencyItem != null) DatabaseHelper.Instance.AddOrUpdateItem(dependencyItem); + if (dependencyItem == null) + continue; + // A reinstall added the same entry again every time. + dependencyItem.RequiredItemIds ??= []; + if (dependencyItem.RequiredItemIds.Any(r => r.Item1 == id)) + continue; + dependencyItem.RequiredItemIds.Add(Tuple.Create(id, dependency[1])); + DatabaseHelper.Instance.AddOrUpdateItem(dependencyItem); } } + /// + /// The URL to check for updates of an item: the stored one when it is on the host of the + /// Store in use, otherwise the link from the Store catalog (the item was installed from the + /// other mirror or has no stored URL). Null when the item has nothing to check, such as a + /// local pack. + /// private string? GetReadyToUseRepoUrl(string? repoUrl, string? storeId) { - if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri)) + string? expectedHost = VersionControl switch { - return repoUrl; - } - string siteName = repoUri.GetLeftPart(UriPartial.Authority); - if (!string.IsNullOrEmpty(siteName)) + SupportedVersionControls.GitHub => "github.com", + SupportedVersionControls.GitLab => "gitlab.com", + _ => null + }; + + if (Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri) && + (expectedHost == null || repoUri.Host.Equals(expectedHost, StringComparison.OrdinalIgnoreCase))) { - if (VersionControl == SupportedVersionControls.GitHub) - { - if (!siteName.Equals("github.com", StringComparison.CurrentCultureIgnoreCase)) - { - var item = GetItemInfoFromStoreId(storeId); - return item.version_control_link; - } - } - else if (VersionControl == SupportedVersionControls.GitLab) - { - if (!siteName.Equals("gitlab.com", StringComparison.CurrentCultureIgnoreCase)) - { - var item = GetItemInfoFromStoreId(storeId); - return item.version_control_link; - } - } + return repoUrl; } - return siteName; + return GetItemInfoFromStoreId(storeId)?.version_control_link ?? repoUrl; } diff --git a/CDPIUI.Core/Store/StoreItemIds.cs b/CDPIUI.Core/Store/StoreItemIds.cs new file mode 100644 index 00000000..530ad843 --- /dev/null +++ b/CDPIUI.Core/Store/StoreItemIds.cs @@ -0,0 +1,28 @@ +using System.Text.RegularExpressions; + +namespace CDPIUI.Core.Store +{ + /// + /// Store item identifiers come from item metadata and local packs; they become folder names + /// that are created, replaced and deleted recursively + /// + public static class StoreItemIds + { + private static readonly Regex ValidId = new(@"^[A-Za-z0-9_-]{1,64}$", RegexOptions.CultureInvariant); + + public static bool IsValid(string? id) => id != null && ValidId.IsMatch(id); + + /// + /// Checks that a directory is a single item folder inside the store items directory + /// + public static bool IsItemFolder(string? directory) + { + if (string.IsNullOrWhiteSpace(directory)) + return false; + + string root = Path.GetFullPath(CDPIUI.Core.Data.Directories.StoreItemsDirectory).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + string fullPath = Path.GetFullPath(directory).TrimEnd(Path.DirectorySeparatorChar); + return fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase) && fullPath.Length > root.Length; + } + } +} diff --git a/CDPIUI.Core/Store/UserExperienceService.cs b/CDPIUI.Core/Store/UserExperienceService.cs index 5d1fd645..7cc05c54 100644 --- a/CDPIUI.Core/Store/UserExperienceService.cs +++ b/CDPIUI.Core/Store/UserExperienceService.cs @@ -2,7 +2,7 @@ using CDPIUI.Core.Store.Data; using CDPIUI.Core.Store.Database; using CDPIUI.Core.Store.ViewModels; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System.Linq; namespace CDPIUI.Core @@ -62,6 +62,9 @@ public List GetSimilarItemsForStoreId(string storeId) } if (!DatabaseHelper.Instance.IsItemInstalled(HardcodedItemIds.AddOnsIds.FirstOrDefault(x => x.Key == AddOns.GoodCheck).Value) && + // GetKeyByValue gives default(Components), which is Zapret, for an id that is no + // component, so GoodCheck was suggested on the page of every item. + HardcodedItemIds.ComponentIds.ContainsValue(storeId) && HardcodedItemIds.GoodCheckSupportedComponents.Contains(HardcodedItemIds.ComponentIds.GetKeyByValue(storeId))) { items.Add(Store.GetItemInfoFromStoreId(HardcodedItemIds.AddOnsIds.FirstOrDefault(x => x.Key == AddOns.GoodCheck).Value)); diff --git a/CDPIUI.Core/Store/VersionComparier.cs b/CDPIUI.Core/Store/VersionComparier.cs index b3fc6fcd..954e98a0 100644 --- a/CDPIUI.Core/Store/VersionComparier.cs +++ b/CDPIUI.Core/Store/VersionComparier.cs @@ -28,8 +28,9 @@ public static int CompareVersionStrings(string? oldVersion, string? newVersion) if (oldVersion.StartsWith('v')) oldVersion = oldVersion[1..]; if (newVersion.StartsWith('v')) newVersion = newVersion[1..]; - if (oldVersion.Contains("rc")) oldVersion = oldVersion.Replace("rc", "-rc"); - if (newVersion.Contains("rc")) newVersion = newVersion.Replace("rc", "-rc"); + // "1.0.0rc1" -> "1.0.0-rc1"; "1.0.0-rc1" stays (it became "1.0.0--rc1"). + oldVersion = ReleaseCandidateRegex().Replace(oldVersion, "-rc"); + newVersion = ReleaseCandidateRegex().Replace(newVersion, "-rc"); if (Semver.SemVersion.TryParse(oldVersion, out var oldSemVersion) && Semver.SemVersion.TryParse(newVersion, out var newSemVersion)) { @@ -37,9 +38,7 @@ public static int CompareVersionStrings(string? oldVersion, string? newVersion) } else if (Version.TryParse(oldVersion, out var oldVerVersion) && Version.TryParse(newVersion, out var newVerVersion)) { - if (oldVerVersion < newVerVersion) return -1; - else if (oldVerVersion > newVerVersion) return 1; - else return 0; + return Normalize(oldVerVersion).CompareTo(Normalize(newVerVersion)); } if (DateTime.TryParseExact(oldVersion, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime oldData) && @@ -48,10 +47,46 @@ public static int CompareVersionStrings(string? oldVersion, string? newVersion) return DateTime.Compare(oldData, newData); } + // Tags such as "release-70.6" or "zapret 70.6": compare the version numbers they contain. + Match oldNumber = VersionNumberRegex().Match(oldVersion); + Match newNumber = VersionNumberRegex().Match(newVersion); + if (oldNumber.Success && newNumber.Success && + Version.TryParse(PadSingleNumber(oldNumber.Value), out var oldFallback) && + Version.TryParse(PadSingleNumber(newNumber.Value), out var newFallback)) + { + return Normalize(oldFallback).CompareTo(Normalize(newFallback)); + } + Logger.Instance.CreateErrorLog(nameof(VersionHelper), $"Cannot compare {oldVersion} and {newVersion}."); return 0; } + // Version treats missing parts as -1, so 1.0 was less than 1.0.0. + private static Version Normalize(Version version) => + new(version.Major, version.Minor, Math.Max(version.Build, 0), Math.Max(version.Revision, 0)); + + private static string PadSingleNumber(string number) => number.Contains('.') ? number : number + ".0"; + + [GeneratedRegex(@"(?<=\d)\.?rc", RegexOptions.IgnoreCase)] + private static partial Regex ReleaseCandidateRegex(); + + [GeneratedRegex(@"\d+(?:\.\d+){0,3}")] + private static partial Regex VersionNumberRegex(); + + public static bool IsVersionInRange(string? version, string? minVersion, string? maxVersion) + { + if (!Version.TryParse(version, out Version? current) || !Version.TryParse(minVersion, out Version? min)) + return false; + + if (current < min) + return false; + + if (string.IsNullOrEmpty(maxVersion) || maxVersion == "NaN") + return true; + + return Version.TryParse(maxVersion, out Version? max) && current <= max; + } + /// /// Is version string are correct /// diff --git a/CDPIUI.Core/System/ArchivePaths.cs b/CDPIUI.Core/System/ArchivePaths.cs new file mode 100644 index 00000000..9e0014cc --- /dev/null +++ b/CDPIUI.Core/System/ArchivePaths.cs @@ -0,0 +1,33 @@ +using System; +using System.IO; + +namespace CDPIUI.Core.System +{ + public static class ArchivePaths + { + public static bool TryGetDestination(string root, string relativePath, out string destination) + { + destination = string.Empty; + if (string.IsNullOrEmpty(relativePath) || Path.IsPathRooted(relativePath) || relativePath.Contains(':')) + return false; + + string fullRoot = Path.GetFullPath(root); + if (!fullRoot.EndsWith(Path.DirectorySeparatorChar)) + fullRoot += Path.DirectorySeparatorChar; + + string fullPath = Path.GetFullPath(Path.Combine(fullRoot, relativePath)); + if (!fullPath.StartsWith(fullRoot, StringComparison.OrdinalIgnoreCase)) + return false; + + destination = fullPath; + return true; + } + + public static string GetDestinationOrThrow(string root, string relativePath) + { + if (!TryGetDestination(root, relativePath, out string destination)) + throw new InvalidDataException($"Archive entry \"{relativePath}\" points outside of the target directory."); + return destination; + } + } +} diff --git a/CDPIUI.Core/System/BatchFileEncoding.cs b/CDPIUI.Core/System/BatchFileEncoding.cs new file mode 100644 index 00000000..8866ecf9 --- /dev/null +++ b/CDPIUI.Core/System/BatchFileEncoding.cs @@ -0,0 +1,49 @@ +using System.Text; + +namespace CDPIUI.Core.System +{ + /// + /// Reads batch files that can be saved as UTF-8, UTF-16 or in a Cyrillic code page + /// + public static class BatchFileEncoding + { + public static string ReadAllText(string path) => Decode(File.ReadAllBytes(path)); + + public static string[] ReadAllLines(string path) => + ReadAllText(path).Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Split('\n'); + + public static string Decode(byte[] bytes) => Detect(bytes).GetString(bytes).TrimStart('\uFEFF'); + + public static Encoding Detect(byte[] bytes) + { + if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) + return new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); + if (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) + return Encoding.Unicode; + if (bytes.Length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) + return Encoding.BigEndianUnicode; + + try + { + _ = new UTF8Encoding(false, true).GetString(bytes); + return new UTF8Encoding(false); + } + catch (DecoderFallbackException) + { + } + + // Without a BOM a Cyrillic file is either OEM 866 (what cmd uses) or ANSI 1251 (Notepad). + // Lowercase letters fall into 0xA0-0xAF and 0xE0-0xEF in 866 and into 0xE0-0xFF in 1251, + // and 0x80-0xBF holds the 866 letters that 1251 uses only for rare symbols. + int oemScore = 0, ansiScore = 0; + foreach (byte value in bytes) + { + if (value >= 0x80 && value <= 0xAF) oemScore++; + else if (value >= 0xC0) ansiScore++; + } + + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + return Encoding.GetEncoding(oemScore > ansiScore ? 866 : 1251); + } + } +} diff --git a/CDPIUI.Core/System/ZipService.cs b/CDPIUI.Core/System/ZipService.cs index 4b970fb7..0317da2b 100644 --- a/CDPIUI.Core/System/ZipService.cs +++ b/CDPIUI.Core/System/ZipService.cs @@ -18,6 +18,54 @@ public static async Task ExtractZip( IEnumerable? filesToSkip = null, bool isCatalogCheckRequired = false ) + { + if (!isCatalogCheckRequired) + { + await ExtractEntries(zipFilePath, zipFolderToUnpack, extractTo, filesToSkip, mergeTextFiles: true); + return; + } + + // A signed archive is checked in a folder of its own and moved into place only when its + // catalog is valid, so files of a rejected archive never reach the destination. + string verifyDirectory = extractTo.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + + ".verify-" + Guid.NewGuid().ToString("N"); + try + { + await ExtractEntries(zipFilePath, zipFolderToUnpack, verifyDirectory, filesToSkip, mergeTextFiles: false); + await VerifyCatalog(verifyDirectory); + MoveContents(verifyDirectory, extractTo); + } + finally + { + try + { + if (Directory.Exists(verifyDirectory)) + Directory.Delete(verifyDirectory, recursive: true); + } + catch { } + } + } + + private static void MoveContents(string sourceDirectory, string destinationDirectory) + { + Directory.CreateDirectory(destinationDirectory); + foreach (string file in Directory.EnumerateFiles(sourceDirectory, "*", SearchOption.AllDirectories)) + { + string destination = Path.Combine(destinationDirectory, Path.GetRelativePath(sourceDirectory, file)); + Directory.CreateDirectory(Path.GetDirectoryName(destination)!); + File.Move(file, destination, overwrite: true); + } + foreach (string directory in Directory.EnumerateDirectories(sourceDirectory, "*", SearchOption.AllDirectories)) + Directory.CreateDirectory(Path.Combine(destinationDirectory, Path.GetRelativePath(sourceDirectory, directory))); + } + + private static async Task ExtractEntries( + string zipFilePath, + string? zipFolderToUnpack, + string extractTo, + IEnumerable? filesToSkip, + bool mergeTextFiles + ) { filesToSkip = filesToSkip ?? Enumerable.Empty(); @@ -30,7 +78,7 @@ public static async Task ExtractZip( int totalFiles = entries.Count; int extractedFiles = 0; - if (zipFolderToUnpack == "/") + if (zipFolderToUnpack == null || zipFolderToUnpack == "/") zipFolderToUnpack = string.Empty; else if (zipFolderToUnpack.EndsWith("/")) zipFolderToUnpack = zipFolderToUnpack.TrimEnd('/'); @@ -82,15 +130,18 @@ public static async Task ExtractZip( if (string.IsNullOrEmpty(relativePath)) continue; - if (filesToSkip.Any(skip => relativePath.Contains(skip))) + // "*.ext" skips by extension; any other value skips paths that contain it. + if (filesToSkip.Any(skip => skip.StartsWith("*.") + ? relativePath.EndsWith(skip.Substring(1), StringComparison.OrdinalIgnoreCase) + : relativePath.Contains(skip))) continue; - var destinationPath = Path.Combine(extractTo, relativePath); + var destinationPath = ArchivePaths.GetDestinationOrThrow(extractTo, relativePath); var destinationDir = Path.GetDirectoryName(destinationPath); if (!Directory.Exists(destinationDir) && !string.IsNullOrEmpty(destinationDir)) Directory.CreateDirectory(destinationDir); - if (!isCatalogCheckRequired) + if (mergeTextFiles) { if (relativePath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) && File.Exists(destinationPath)) @@ -101,23 +152,40 @@ public static async Task ExtractZip( destinationPath = $"{destinationPath}.bak"; } // End of piece of shit - string destLines = File.ReadAllText(destinationPath); - string tmpFile = Path.Combine(destinationDir!, $"__TEMPFILE.txt"); - entry.ExtractToFile(tmpFile, overwrite: true); - - var stream = File.AppendText(destinationPath); + // Whole lines are compared: as a substring, "t.co" was dropped because + // the list already had "reddit.com". + var destLines = new HashSet( + File.ReadLines(destinationPath).Select(line => line.Trim()), StringComparer.Ordinal); + bool endsWithNewLine; + using (var existing = File.OpenRead(destinationPath)) + { + endsWithNewLine = existing.Length == 0; + if (!endsWithNewLine) + { + existing.Seek(-1, SeekOrigin.End); + endsWithNewLine = existing.ReadByte() == '\n'; + } + } - using (stream) + var newLines = new List(); + using (var reader = new StreamReader(entry.Open())) { - foreach (var line in File.ReadLines(tmpFile)) + string? line; + while ((line = await reader.ReadLineAsync()) != null) { - if (!destLines.Contains(line)) - { - await stream.WriteLineAsync(line); - } + if (line.Trim().Length > 0 && destLines.Add(line.Trim())) + newLines.Add(line); } } - File.Delete(tmpFile); + + if (newLines.Count > 0) + { + using var stream = File.AppendText(destinationPath); + if (!endsWithNewLine) + await stream.WriteLineAsync(); + foreach (string line in newLines) + await stream.WriteLineAsync(line); + } continue; } @@ -136,24 +204,23 @@ public static async Task ExtractZip( extractedFiles++; } } + } - if (isCatalogCheckRequired) + private static async Task VerifyCatalog(string directory) + { + CatalogCheckResult catalogCheckResult = await CertificateCheck.CheckCatalog(Path.Combine(directory, "catalog.cat"), directory); + switch (catalogCheckResult) { - CatalogCheckResult catalogCheckResult = await CertificateCheck.CheckCatalog(Path.Combine(extractTo, "catalog.cat"), extractTo); - switch (catalogCheckResult) - { - case CatalogCheckResult.Success: - return; - case CatalogCheckResult.FailureNoSignature: - throw new CatalogNoSignature("Catalog file isn't signed"); - case CatalogCheckResult.FailureNotTrustedSignature: - throw new CatalogNoSignature("Signature not trusted"); - case CatalogCheckResult.FailureNotValid: - throw new CatalogInvalid(); - case CatalogCheckResult.FailureUnknown: - throw new CatalogInvalid("Unknown"); - - } + case CatalogCheckResult.Success: + return; + case CatalogCheckResult.FailureNoSignature: + throw new CatalogNoSignature("Catalog file isn't signed"); + case CatalogCheckResult.FailureNotTrustedSignature: + throw new CatalogNoSignature("Signature not trusted"); + case CatalogCheckResult.FailureNotValid: + throw new CatalogInvalid(); + default: + throw new CatalogInvalid("Unknown"); } } } diff --git a/CDPIUI.Shared/Basic/CommandLine/GuiActivationArguments.cs b/CDPIUI.Shared/Basic/CommandLine/GuiActivationArguments.cs new file mode 100644 index 00000000..f0ca39c3 --- /dev/null +++ b/CDPIUI.Shared/Basic/CommandLine/GuiActivationArguments.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CDPIUI.Shared.Basic.CommandLine +{ + /// + /// Carries the command-line arguments of a GUI start through the tray. When the GUI finds no + /// tray, it starts the tray and exits; the tray then starts the GUI again. Without these + /// arguments the opened file, cdpiui:// link or --direct: command was lost. + /// + public static class GuiActivationArguments + { + public const string Prefix = "--gui-args:"; + + // Arguments longer than this are dropped rather than passed half. + private const int MaxEncodedLength = 16 * 1024; + + public static string? Encode(IEnumerable arguments) + { + var list = arguments.Where(a => !string.IsNullOrEmpty(a)).ToList(); + if (list.Count == 0) + return null; + + string encoded = Prefix + string.Join(".", list.Select(a => Convert.ToBase64String(Encoding.UTF8.GetBytes(a)))); + return encoded.Length <= MaxEncodedLength ? encoded : null; + } + + public static bool TryDecode(IEnumerable arguments, out string[] decoded) + { + decoded = Array.Empty(); + string? value = arguments.FirstOrDefault(a => a.StartsWith(Prefix, StringComparison.Ordinal)); + if (value == null || value.Length > MaxEncodedLength) + return false; + + try + { + decoded = value.Substring(Prefix.Length) + .Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries) + .Select(part => Encoding.UTF8.GetString(Convert.FromBase64String(part))) + .ToArray(); + return decoded.Length > 0; + } + catch (FormatException) + { + decoded = Array.Empty(); + return false; + } + } + + /// + /// Builds a command line that CommandLineToArgvW and the .NET runtime split back into + /// exactly . + /// + public static string JoinCommandLine(IEnumerable arguments) + { + return string.Join(" ", arguments.Select(Quote)); + } + + private static string Quote(string argument) + { + if (argument.Length > 0 && argument.IndexOfAny(new[] { ' ', '\t', '\n', '\v', '"' }) < 0) + return argument; + + var builder = new StringBuilder("\""); + int backslashes = 0; + foreach (char c in argument) + { + if (c == '\\') + { + backslashes++; + continue; + } + if (c == '"') + { + // Backslashes before a quote are escaped, and so is the quote. + builder.Append('\\', backslashes * 2 + 1); + } + else + { + builder.Append('\\', backslashes); + } + backslashes = 0; + builder.Append(c); + } + // Backslashes before the closing quote are escaped too. + builder.Append('\\', backslashes * 2); + builder.Append('"'); + return builder.ToString(); + } + } +} diff --git a/CDPIUI.Shared/Basic/Filesystem/ApplicationPathMatcher.cs b/CDPIUI.Shared/Basic/Filesystem/ApplicationPathMatcher.cs new file mode 100644 index 00000000..ee8f8e24 --- /dev/null +++ b/CDPIUI.Shared/Basic/Filesystem/ApplicationPathMatcher.cs @@ -0,0 +1,41 @@ +using System; + +namespace CDPIUI.Shared.Basic.Filesystem +{ + /// + /// Decides which processes and drivers belong to the application when it is uninstalled. + /// + public static class ApplicationPathMatcher + { + // Data directory of MSI builds for every user profile. + private const string UserDataSegment = @"\AppData\Local\CDPIUI\"; + + /// + /// Converts a process path or a service ImagePath to a plain Windows path + /// + public static string Normalize(string path) + { + string result = path.Trim().Trim('"').Replace('/', '\\'); + if (result.StartsWith(@"\??\", StringComparison.Ordinal)) + result = result.Substring(4); + return result; + } + + /// + /// Checks whether a file is inside the installation directory or a user data directory of the application + /// + /// Process path or service ImagePath + /// Installation directory + public static bool BelongsToApplication(string? path, string installDirectory) + { + if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(installDirectory)) + return false; + + string normalized = Normalize(path!); + string root = Normalize(installDirectory).TrimEnd('\\') + "\\"; + + return normalized.StartsWith(root, StringComparison.OrdinalIgnoreCase) || + normalized.IndexOf(UserDataSegment, StringComparison.OrdinalIgnoreCase) >= 0; + } + } +} diff --git a/CDPIUI.Shared/Basic/Filesystem/FileSystemService.cs b/CDPIUI.Shared/Basic/Filesystem/FileSystemService.cs index 76403f0b..526b48f7 100644 --- a/CDPIUI.Shared/Basic/Filesystem/FileSystemService.cs +++ b/CDPIUI.Shared/Basic/Filesystem/FileSystemService.cs @@ -12,7 +12,7 @@ namespace CDPIUI.Shared.Basic.Filesystem { - public enum FileExtentionTypes + public enum FileExtensionTypes { none, archive, @@ -31,39 +31,39 @@ public enum FileExtentionTypes public class FileSystemService { /// - /// File extentions by without dots + /// File extensions by without dots /// - public static Dictionary FileExtentions = new() + public static Dictionary FileExtensions = new() { - { FileExtentionTypes.none, "" }, - { FileExtentionTypes.archive, "zip" }, - { FileExtentionTypes.configPack, "cdpiconfigpack" }, - { FileExtentionTypes.signedZip, "cdpisignedpack" }, - { FileExtentionTypes.WIN32application, "exe" }, - { FileExtentionTypes.CDPIUIUpdateItem, "cdpipatch" }, - { FileExtentionTypes.msi, "msi" }, - { FileExtentionTypes.UPDmsi, "msi" }, - { FileExtentionTypes.elmsi, "exe" }, - { FileExtentionTypes.lst, "lst" }, - { FileExtentionTypes.raw, "txt" }, - { FileExtentionTypes.temp, "cdpitempfile" }, + { FileExtensionTypes.none, "" }, + { FileExtensionTypes.archive, "zip" }, + { FileExtensionTypes.configPack, "cdpiconfigpack" }, + { FileExtensionTypes.signedZip, "cdpisignedpack" }, + { FileExtensionTypes.WIN32application, "exe" }, + { FileExtensionTypes.CDPIUIUpdateItem, "cdpipatch" }, + { FileExtensionTypes.msi, "msi" }, + { FileExtensionTypes.UPDmsi, "msi" }, + { FileExtensionTypes.elmsi, "exe" }, + { FileExtensionTypes.lst, "lst" }, + { FileExtensionTypes.raw, "txt" }, + { FileExtensionTypes.temp, "cdpitempfile" }, }; /// - /// Get file extention with dot + /// Get file extension with dot /// /// Type - /// .extention - public static string GetFileExtention(FileExtentionTypes type) + /// .extension + public static string GetFileExtension(FileExtensionTypes type) { - return $".{FileExtentions.FirstOrDefault(x => x.Key == type).Value}"; + return $".{FileExtensions.FirstOrDefault(x => x.Key == type).Value}"; } /// /// Archive-like compressed file types /// - public static List CompressedFileTypes = - [FileExtentionTypes.signedZip, FileExtentionTypes.archive, FileExtentionTypes.configPack]; + public static List CompressedFileTypes = + [FileExtensionTypes.signedZip, FileExtensionTypes.archive, FileExtensionTypes.configPack]; private readonly static Uri SomeBaseUri = new("https://canbeanything"); @@ -72,7 +72,7 @@ public static string GetFileExtention(FileExtentionTypes type) /// https://stackoverflow.com/a/40361205 /// /// URL - /// File name with extention if url correct, otherwise + /// File name with extension if url correct, otherwise public static string GetFileNameFromUrl(string url) { Uri uri; @@ -94,19 +94,45 @@ public static string GetNewTempFileName() /// /// Get unique temp file/folder name. Format /// - /// secondsSinceEpoch_suffix(.extention) + /// secondsSinceEpoch_suffix_random(.extension) /// /// /// Suffix of file - /// Extention. Leave emty for no extention + /// Extension. Leave emty for no extension /// Temp file/folder name - public static string GetNewTempFileName(string suffix, string? extention = null) + public static string GetNewTempFileName(string suffix, string? extension = null) { TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); int secondsSinceEpoch = (int)t.TotalSeconds; + // Seconds alone repeat when two names are requested within the same second. + string unique = Guid.NewGuid().ToString("N").Substring(0, 8); - if (string.IsNullOrEmpty(extention)) return $"{secondsSinceEpoch}_{suffix}"; - return $"{secondsSinceEpoch}_{suffix}.{extention}"; + if (string.IsNullOrEmpty(extension)) return $"{secondsSinceEpoch}_{suffix}_{unique}"; + return $"{secondsSinceEpoch}_{suffix}_{unique}.{extension!.TrimStart('.')}"; + } + + /// + /// Writes text to a temporary file next to the target and swaps it in, so readers never + /// see a half-written file and a crash keeps the previous content + /// + /// Target file + /// Text to write + public static void WriteAllTextAtomic(string path, string contents) + { + string fullPath = Path.GetFullPath(path); + string tempPath = fullPath + ".tmp-" + Guid.NewGuid().ToString("N"); + try + { + File.WriteAllText(tempPath, contents); + if (File.Exists(fullPath)) + File.Replace(tempPath, fullPath, null); + else + File.Move(tempPath, fullPath); + } + finally + { + if (File.Exists(tempPath)) File.Delete(tempPath); + } } /// @@ -116,6 +142,10 @@ public static string GetNewTempFileName(string suffix, string? extention = null) /// Copy destination public static async Task CopyFileAsync(string sourcePath, string destinationPath) { + // Creating the destination first would truncate the source when both are the same file. + if (string.Equals(Path.GetFullPath(sourcePath), Path.GetFullPath(destinationPath), StringComparison.OrdinalIgnoreCase)) + return; + using Stream source = File.OpenRead(sourcePath); using Stream destination = File.Create(destinationPath); await source.CopyToAsync(destination); @@ -139,6 +169,8 @@ public static long GetFileSize(string filePath, ILogger? logger = null) uint clusterSize = sectorsPerCluster * bytesPerSector; uint hosize; uint losize = GetCompressedFileSizeW(filePath, out hosize); + // INVALID_FILE_SIZE with an error set is a failure, not a 4 GB file. + if (losize == 0xFFFFFFFF && Marshal.GetLastWin32Error() != 0) throw new Win32Exception(); long size; size = (long)hosize << 32 | losize; return ((size + clusterSize - 1) / clusterSize) * clusterSize; @@ -146,6 +178,12 @@ public static long GetFileSize(string filePath, ILogger? logger = null) catch (Exception ex) { logger?.CreateWarningLog(nameof(FileSystemService), $"Unable to calculate size for \"{filePath}\". {ex.Message}"); + try + { + var info = new FileInfo(filePath); + if (info.Exists) fileSize = info.Length; + } + catch (Exception) { } } return fileSize; } @@ -322,7 +360,7 @@ private static string TransliterateToAscii(string input) } #region WINAPI - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", SetLastError = true)] static extern uint GetCompressedFileSizeW([In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh); diff --git a/CDPIUI.Shared/Basic/Filesystem/TrustedPathValidator.cs b/CDPIUI.Shared/Basic/Filesystem/TrustedPathValidator.cs new file mode 100644 index 00000000..a77d7e1a --- /dev/null +++ b/CDPIUI.Shared/Basic/Filesystem/TrustedPathValidator.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace CDPIUI.Shared.Basic.Filesystem +{ + /// + /// Checks paths that an elevated process receives from a less trusted one + /// + public static class TrustedPathValidator + { + /// + /// Checks that a path is inside one of the roots and that no directory between the root + /// and the path, and not the path itself, is a symbolic link or a junction + /// + /// Path to check + /// Allowed root directories + public static bool IsInside(string? path, IEnumerable roots) + { + if (string.IsNullOrWhiteSpace(path)) + return false; + + string fullPath; + try { fullPath = Path.GetFullPath(path); } + catch { return false; } + + foreach (string? root in roots) + { + if (string.IsNullOrWhiteSpace(root)) + continue; + + string fullRoot; + try { fullRoot = WithSeparator(Path.GetFullPath(root)); } + catch { continue; } + + if (!fullPath.StartsWith(fullRoot, StringComparison.OrdinalIgnoreCase)) + continue; + + return !HasLinkBelowRoot(fullRoot, fullPath); + } + + return false; + } + + private static bool HasLinkBelowRoot(string fullRoot, string fullPath) + { + string current = fullRoot.TrimEnd(Path.DirectorySeparatorChar); + string[] parts = fullPath.Substring(fullRoot.Length) + .Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + + foreach (string part in parts) + { + current = Path.Combine(current, part); + FileAttributes attributes; + try { attributes = File.GetAttributes(current); } + catch (FileNotFoundException) { return false; } + catch (DirectoryNotFoundException) { return false; } + catch { return true; } + + if ((attributes & FileAttributes.ReparsePoint) != 0) + return true; + } + return false; + } + + private static string WithSeparator(string path) => + path.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) + ? path + : path + Path.DirectorySeparatorChar; + } +} diff --git a/CDPIUI.Shared/Basic/XMLSettingsService.cs b/CDPIUI.Shared/Basic/XMLSettingsService.cs index 52117d1e..331c4045 100644 --- a/CDPIUI.Shared/Basic/XMLSettingsService.cs +++ b/CDPIUI.Shared/Basic/XMLSettingsService.cs @@ -1,9 +1,11 @@ -using CDPIUI.Shared.Exceptions.Settings; +using CDPIUI.Shared.Exceptions.Settings; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; +using System.Threading; using System.Xml.Linq; using System.Xml; @@ -92,12 +94,124 @@ private static bool IsReadFailure(Exception exception) => private static XDocument LoadDocument(string path) { - var document = XDocument.Load(path); + // Settings are read by the elevated tray from a file the user can change: no DTD processing, + // so internal entities cannot blow up the memory of the elevated process. + var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null }; + XDocument document; + using (var reader = XmlReader.Create(path, readerSettings)) + document = XDocument.Load(reader); if (document.Root?.Name != "Settings") throw new XmlException("The settings document must have a Settings root element."); return document; } + // Items are separated by ';'. A ';' or '%' inside an item is stored as %3B or %25, so file + // names with ';' survive; values written before contain neither sequence and read the same. + private static string JoinList(IEnumerable items) => + string.Join(";", items.Select(item => (item ?? string.Empty).Replace("%", "%25").Replace(";", "%3B"))); + + private static List SplitList(string value) => + value.Split(';') + .Select(item => System.Text.RegularExpressions.Regex.Replace(item, "%(3B|25)", match => + string.Equals(match.Groups[1].Value, "3B", StringComparison.OrdinalIgnoreCase) ? ";" : "%", + System.Text.RegularExpressions.RegexOptions.IgnoreCase)) + .ToList(); + + private static void ApplySetting(XElement root, IEnumerable groupPath, string key, string type, string valueString) + { + XElement current = root; + foreach (var grp in groupPath) + { + var next = current + .Elements("Group") + .FirstOrDefault(g => (string?)g.Attribute("Name") == grp); + + if (next == null) + { + next = new XElement("Group", new XAttribute("Name", grp)); + current.Add(next); + } + + current = next; + } + + var setting = current + .Elements("Setting") + .FirstOrDefault(s => (string?)s.Attribute("Key") == key); + + if (setting != null) + { + setting.SetAttributeValue("Type", type); + setting.SetAttributeValue("Value", valueString); + } + else + { + current.Add(new XElement("Setting", + new XAttribute("Key", key), + new XAttribute("Type", type), + new XAttribute("Value", valueString))); + } + } + + /// + /// The UI and the tray share the settings file. Each change is applied to the file as it is on disk + /// now, so a process with an older copy in memory does not overwrite what the other one saved. + /// + private void SaveChange(IEnumerable groupPath, string key, string type, string valueString) + { + lock (_reloadLock) + { + using (AcquireFileLock()) + { + if (!_persistenceUnavailable && File.Exists(SettingsFilePath)) + { + try + { + _xDocument = LoadDocument(SettingsFilePath); + } + catch (Exception ex) when (IsReadFailure(ex)) + { + // Keep the copy in memory; saving it replaces the unreadable file. + } + } + + ApplySetting(_xDocument!.Root!, groupPath, key, type, valueString); + SaveSettings(); + } + } + } + + private const int LockAttempts = 40; + private const int LockRetryDelayMs = 50; + + // A lock file works for both processes, while a named mutex of the elevated tray would not be + // accessible to the UI. + private IDisposable? AcquireFileLock() + { + string lockPath = SettingsFilePath + ".lock"; + for (int attempt = 1; attempt <= LockAttempts; attempt++) + { + try + { + Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(SettingsFilePath))!); + return new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, + 1, FileOptions.DeleteOnClose); + } + catch (IOException) + { + Thread.Sleep(LockRetryDelayMs); + } + catch (UnauthorizedAccessException) + { + return null; + } + } + return null; + } + + private const int SaveAttempts = 5; + private const int SaveRetryDelayMs = 50; + private void SaveSettings(bool keepBackup = true) { if (_persistenceUnavailable) return; @@ -106,16 +220,33 @@ private void SaveSettings(bool keepBackup = true) { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(SettingsFilePath))!); _xDocument!.Save(temporaryPath); - if (File.Exists(SettingsFilePath)) - File.Replace(temporaryPath, SettingsFilePath, keepBackup ? SettingsFilePath + ".bak" : null); - else - File.Move(temporaryPath, SettingsFilePath); + + for (int attempt = 1; ; attempt++) + { + try + { + if (File.Exists(SettingsFilePath)) + File.Replace(temporaryPath, SettingsFilePath, keepBackup ? SettingsFilePath + ".bak" : null); + else + File.Move(temporaryPath, SettingsFilePath); + break; + } + catch (IOException) when (attempt < SaveAttempts) + { + // The other process (GUI or tray) may be reading the file at this moment. + Thread.Sleep(SaveRetryDelayMs * attempt); + } + } } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + catch (UnauthorizedAccessException ex) { _persistenceUnavailable = true; Debug.WriteLine(ex); } + catch (IOException ex) + { + Debug.WriteLine(ex); + } finally { try { File.Delete(temporaryPath); } @@ -134,20 +265,23 @@ private void SaveSettings(bool keepBackup = true) /// Value if key exsits, otherwise default public T? GetValueOrDefault(string group, string key, XElement? xElement = null, T? defaultValue = default) { - if (xElement == null) xElement = _xDocument.Root; - var settingElement = xElement - .Elements("Group") - .FirstOrDefault(g => g.Attribute("Name")?.Value == group)? - .Elements("Setting") - .FirstOrDefault(s => s.Attribute("Key")?.Value == key); - - if (settingElement == null) + lock (_reloadLock) { - SetValue(group, key, defaultValue); - Debug.WriteLine($"Setting '{key}' in group '{group}' not found."); - return defaultValue; + if (xElement == null) xElement = _xDocument.Root; + var settingElement = xElement + .Elements("Group") + .FirstOrDefault(g => g.Attribute("Name")?.Value == group)? + .Elements("Setting") + .FirstOrDefault(s => s.Attribute("Key")?.Value == key); + + if (settingElement == null) + { + SetValue(group, key, defaultValue); + Debug.WriteLine($"Setting '{key}' in group '{group}' not found."); + return defaultValue; + } + else return GetValue(group, key, xElement); } - else return GetValue(group, key, xElement); } /// /// Get value or hardcoded defailt. Sets default value to settings file. @@ -159,37 +293,42 @@ private void SaveSettings(bool keepBackup = true) /// Custom if settings must be loaded from another file /// Throw if not found in file /// Value if key exsits, otherwise default - /// /// public T? GetValue(string group, string key, XElement? xElement = null, bool raiseExceptionIfNotExits = false) { - xElement ??= _xDocument.Root; - var settingElement = xElement - .Elements("Group") - .FirstOrDefault(g => g.Attribute("Name")?.Value == group)? - .Elements("Setting") - .FirstOrDefault(s => s.Attribute("Key")?.Value == key); - - if (settingElement == null) + lock (_reloadLock) { - if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); + xElement ??= _xDocument.Root; + var settingElement = xElement + .Elements("Group") + .FirstOrDefault(g => g.Attribute("Name")?.Value == group)? + .Elements("Setting") + .FirstOrDefault(s => s.Attribute("Key")?.Value == key); - var defaultValue = GetDefaultValueForKey(group, key); + if (settingElement == null) + { + if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); - SetValue(group, key, defaultValue); - Debug.WriteLine($"Setting '{key}' in group '{group}' not found."); - return defaultValue; - } - string value = settingElement.Attribute("Value")?.Value; - string type = settingElement.Attribute("Type")?.Value; + var defaultValue = GetDefaultValueForKey(group, key); - try - { - return GetValueFromString(value, type); - } - catch - { - throw new SettingTypeNotSupported($"Type mismatch or unsupported type for setting '{key}' in group '{group}'."); + SetValue(group, key, defaultValue); + Debug.WriteLine($"Setting '{key}' in group '{group}' not found."); + return defaultValue; + } + string value = settingElement.Attribute("Value")?.Value; + string type = settingElement.Attribute("Type")?.Value; + + try + { + return GetValueFromString(value, type); + } + catch + { + // A value of another type, for example after a manual edit, must not crash + // the caller: hardly any of them expects an exception here (issue #61). + Debug.WriteLine($"Type mismatch or unsupported type for setting '{key}' in group '{group}'."); + return GetDefaultValueForKey(group, key); + } } } /// @@ -202,51 +341,54 @@ private void SaveSettings(bool keepBackup = true) /// Custom if settings must be loaded from another file /// Throw if not found in file /// Value if key exsits, otherwise default - /// /// public T? GetValue(IEnumerable groupPath, string key, XElement? xElement = null, bool raiseExceptionIfNotExits = false) { - if (xElement == null) xElement = _xDocument.Root; - XElement current = xElement; - - foreach (var grp in groupPath) + lock (_reloadLock) { - current = current - .Elements("Group") - .FirstOrDefault(g => (string)g.Attribute("Name") == grp); - if (current == null) + if (xElement == null) xElement = _xDocument.Root; + XElement current = xElement; + + foreach (var grp in groupPath) + { + current = current + .Elements("Group") + .FirstOrDefault(g => (string)g.Attribute("Name") == grp); + if (current == null) + { + if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); + var defaultValue = GetDefaultValueForKey(groupPath, key); + SetValue(groupPath, key, defaultValue); + Debug.WriteLine($"Group path '{string.Join("/", groupPath)}' not found."); + return defaultValue; + } + } + + var settingElement = current + .Elements("Setting") + .FirstOrDefault(s => (string)s.Attribute("Key") == key); + + if (settingElement == null) { if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); var defaultValue = GetDefaultValueForKey(groupPath, key); SetValue(groupPath, key, defaultValue); - Debug.WriteLine($"Group path '{string.Join("/", groupPath)}' not found."); + Debug.WriteLine($"Setting '{key}' in group path '{string.Join("/", groupPath)}' not found."); return defaultValue; } - } - - var settingElement = current - .Elements("Setting") - .FirstOrDefault(s => (string)s.Attribute("Key") == key); - - if (settingElement == null) - { - if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); - var defaultValue = GetDefaultValue(); - SetValue(groupPath, key, defaultValue); - Debug.WriteLine($"Setting '{key}' in group path '{string.Join("/", groupPath)}' not found."); - return defaultValue; - } - string value = (string)settingElement.Attribute("Value"); - string type = (string)settingElement.Attribute("Type"); + string value = (string)settingElement.Attribute("Value"); + string type = (string)settingElement.Attribute("Type"); - try - { - return GetValueFromString(value, type); - } - catch - { - throw new SettingTypeNotSupported($"Type mismatch or unsupported type for setting '{key}' in group path '{string.Join("/", groupPath)}'."); + try + { + return GetValueFromString(value, type); + } + catch + { + Debug.WriteLine($"Type mismatch or unsupported type for setting '{key}' in group path '{string.Join("/", groupPath)}'."); + return GetDefaultValueForKey(groupPath, key); + } } } @@ -261,35 +403,48 @@ private void SaveSettings(bool keepBackup = true) /// public Dictionary GetKeyPair(IEnumerable groupPath, XElement? xElement = null, bool raiseExceptionIfNotExits = false) { - if (xElement == null) xElement = _xDocument.Root; - XElement current = xElement; + lock (_reloadLock) + { + if (xElement == null) xElement = _xDocument.Root; + XElement current = xElement; - Dictionary dict = []; + Dictionary dict = []; - foreach (var grp in groupPath) - { - current = current - .Elements("Group") - .FirstOrDefault(g => (string)g.Attribute("Name") == grp); - if (current == null) + foreach (var grp in groupPath) { - if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); - var defaultValue = GetDefaultValueForKey(groupPath, "$$~"); - SetValue(groupPath, "$$~", defaultValue); - Debug.WriteLine($"Group path '{string.Join("/", groupPath)}' not found."); - return dict; + current = current + .Elements("Group") + .FirstOrDefault(g => (string)g.Attribute("Name") == grp); + if (current == null) + { + if (raiseExceptionIfNotExits) throw new SettingNotFoundException("Value not exist"); + var defaultValue = GetDefaultValueForKey(groupPath, "$$~"); + SetValue(groupPath, "$$~", defaultValue); + Debug.WriteLine($"Group path '{string.Join("/", groupPath)}' not found."); + return dict; + } } - } - if (current != null) - { - foreach (var setting in current.Elements("Setting")) + if (current != null) { - var result = GetValueFromString((string)setting.Attribute("Value"), (string)setting.Attribute("Type")); - dict.Add((string)setting.Attribute("Key"), result); + foreach (var setting in current.Elements("Setting")) + { + string key = (string)setting.Attribute("Key"); + // "$$~" only marks a group created empty; it is no setting. + if (key == null || key == "$$~") + continue; + try + { + dict[key] = GetValueFromString((string)setting.Attribute("Value"), (string)setting.Attribute("Type")); + } + catch + { + Debug.WriteLine($"Skipping setting '{key}' of another type in group path '{string.Join("/", groupPath)}'."); + } + } } + return dict; } - return dict; } @@ -310,67 +465,40 @@ public void SetValue(string group, string key, T value) type = "int"; valueString = value.ToString(); } - else if (value is double) + else if (value is double d) { type = "double"; - valueString = value.ToString(); + valueString = FormatDouble(d); } else if (value is bool) { type = "bool"; - valueString = value.ToString().ToLower(); + valueString = value.ToString().ToLowerInvariant(); } else if (value is string) { type = "string"; valueString = value as string; } - else if (value is DateTime) + else if (value is DateTime dateTime) { type = nameof(DateTime); - valueString = value.ToString(); + valueString = FormatDateTime(dateTime); } else if (typeof(T) == typeof(List)) { type = nameof(List); - valueString = string.Join(";", (List)(object)value); + valueString = JoinList((List)(object)value); Debug.WriteLine(valueString); } else { - type = nameof(T); - valueString = value.ToString(); + type = typeof(T).Name; + valueString = value?.ToString() ?? string.Empty; } - var groupElement = _xDocument.Root - .Elements("Group") - .FirstOrDefault(g => g.Attribute("Name")?.Value == group); - - if (groupElement == null) - { - groupElement = new XElement("Group", new XAttribute("Name", group)); - _xDocument.Root.Add(groupElement); - } - - var settingElement = groupElement - .Elements("Setting") - .FirstOrDefault(s => s.Attribute("Key")?.Value == key); - - if (settingElement != null) - { - settingElement.SetAttributeValue("Value", valueString); - settingElement.SetAttributeValue("Type", type); - } - else - { - groupElement.Add(new XElement("Setting", - new XAttribute("Key", key), - new XAttribute("Type", type), - new XAttribute("Value", valueString))); - } - - SaveSettings(); + SaveChange([group], key, type, valueString); PropertyChanged?.Invoke(group); } @@ -394,11 +522,11 @@ public void SetValue(IEnumerable groupPath, string key, T value) break; case double d: type = "double"; - valueString = d.ToString(); + valueString = FormatDouble(d); break; case bool b: type = "bool"; - valueString = b.ToString().ToLower(); + valueString = b.ToString().ToLowerInvariant(); break; case string s: type = "string"; @@ -406,66 +534,53 @@ public void SetValue(IEnumerable groupPath, string key, T value) break; case List lst: type = nameof(List); - valueString = string.Join(";", lst); + valueString = JoinList(lst); + break; + case DateTime dateTime: + type = nameof(DateTime); + valueString = FormatDateTime(dateTime); break; default: - type = nameof(T); - valueString = value.ToString(); + type = typeof(T).Name; + valueString = value?.ToString() ?? string.Empty; break; } - XElement current = _xDocument.Root; - foreach (var grp in groupPath) - { - var next = current - .Elements("Group") - .FirstOrDefault(g => (string)g.Attribute("Name") == grp); - - if (next == null) - { - next = new XElement("Group", new XAttribute("Name", grp)); - current.Add(next); - } - - current = next; - } - - var setting = current - .Elements("Setting") - .FirstOrDefault(s => (string)s.Attribute("Key") == key); - - if (setting != null) - { - setting.SetAttributeValue("Type", type); - setting.SetAttributeValue("Value", valueString); - } - else - { - current.Add(new XElement("Setting", - new XAttribute("Key", key), - new XAttribute("Type", type), - new XAttribute("Value", valueString))); - } - - SaveSettings(); + SaveChange(groupPath, key, type, valueString); EnumPropertyChanged?.Invoke(groupPath); } private static T? GetValueFromString(string? value, string? type) { if (typeof(T) == typeof(int) && type == "int" && int.TryParse(value, out var iv)) return (T)(object)iv; - if (typeof(T) == typeof(double) && type == "double" && double.TryParse(value, out var dv)) return (T)(object)dv; + if (typeof(T) == typeof(double) && type == "double" && TryParseDouble(value, out var dv)) return (T)(object)dv; if (typeof(T) == typeof(bool) && type == "bool" && bool.TryParse(value, out var bv)) return (T)(object)bv; if (typeof(T) == typeof(string) && type == "string") return (T)(object)value; - if (typeof(T) == typeof(List) && type == nameof(List)) return string.IsNullOrEmpty(value) ? (T)(object)new List() : (T)(object)value.Split(';').ToList(); + if (typeof(T) == typeof(List) && type == nameof(List)) return string.IsNullOrEmpty(value) ? (T)(object)new List() : (T)(object)SplitList(value); - if (typeof(T) == typeof(DateTime) && type == nameof(DateTime)) - return (T)(object)DateTime.Parse((string)value); + if (typeof(T) == typeof(DateTime) && type == nameof(DateTime) && TryParseDateTime(value, out var dtv)) + return (T)(object)dtv; throw new Exception(); } + public static string FormatDouble(double value) => + value.ToString("R", CultureInfo.InvariantCulture); + + public static string FormatDateTime(DateTime value) => + value.ToString("o", CultureInfo.InvariantCulture); + + // Values written by older versions used the current culture, so it is tried after the invariant form. + public static bool TryParseDouble(string? value, out double result) => + double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result) + || double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out result); + + public static bool TryParseDateTime(string? value, out DateTime result) => + DateTime.TryParseExact(value, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out result) + || DateTime.TryParse(value, CultureInfo.CurrentCulture, DateTimeStyles.None, out result) + || DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out result); + protected virtual T? GetDefaultValueForKey(string group, string key) { return GetDefaultValue(); diff --git a/CDPIUI.Shared/CDPIUI.Shared.csproj b/CDPIUI.Shared/CDPIUI.Shared.csproj index fefa1059..522d3e50 100644 --- a/CDPIUI.Shared/CDPIUI.Shared.csproj +++ b/CDPIUI.Shared/CDPIUI.Shared.csproj @@ -16,6 +16,7 @@ + diff --git a/CDPIUI.Shared/ComponentsTask/OutputCleanupHelper.cs b/CDPIUI.Shared/ComponentsTask/OutputCleanupHelper.cs index 913f617f..4e9f128b 100644 --- a/CDPIUI.Shared/ComponentsTask/OutputCleanupHelper.cs +++ b/CDPIUI.Shared/ComponentsTask/OutputCleanupHelper.cs @@ -1,31 +1,62 @@ using System; -using System.Collections.Generic; -using System.Text; using System.Text.RegularExpressions; namespace CDPIUI.Shared.ComponentsTask { + /// + /// Turns pseudo-console output into plain text. Every pattern starts with ESC: patterns + /// without it also cut ordinary text, "[HOSTLIST]" became "OSTLIST]". + /// public static class OutputCleanupHelper { + private const string Esc = "\u001b"; + + // Control sequence (CSI), operating system command (OSC) or a two-character sequence. + private static readonly Regex CompleteSequenceRegex = new( + @"^\u001b(?:\[[0-?]*[ -/]*[@-~]|\][^\u0007\u001b]*(?:\u0007|\u001b\\)|[@-Z\\-_])", + RegexOptions.Compiled); + + private static readonly Regex CursorForwardRegex = new(@"\u001b\[(\d{0,3})C", RegexOptions.Compiled); + private static readonly Regex OscRegex = new(@"\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)", RegexOptions.Compiled); + private static readonly Regex CsiRegex = new(@"\u001b\[[0-?]*[ -/]*[@-~]", RegexOptions.Compiled); + private static readonly Regex OtherEscapeRegex = new(@"\u001b[@-Z\\-_]?", RegexOptions.Compiled); + public static string ReplaceSymbols(string str) { - str = str.Replace("[?25l\u001b[2J\u001b[m\u001b[H", ""); - str = str.Replace("[K", ""); - str = str.Replace("[4;1H", "\n"); - str = str.Replace("[12;1H", "\n"); - str = str.Replace("[32m", "\n"); - str = str.Replace("[90m", "\n"); - str = str.Replace("[23;1H", "\n\t"); - str = Regex.Replace(str, @"\u001b\]0;.*?\[\?25h", ""); - str = Regex.Replace(str, @"\[\?25l|\[1C|", ""); - str = Regex.Replace(str, @"\[\?\d{4}\w", ""); - str = Regex.Replace(str, @"\[\d[A-Z]", ""); - str = Regex.Replace(str, @"\[\d{1,2};\d{1,2}[A-Z]", ""); - str = Regex.Replace(str, @"\[\?\d{1,2}[a-z]", ""); - str = Regex.Replace(str, @"(\[\d{0,2}m)?(\[H)?", ""); - str = Regex.Replace(str, @"(\[\d{0,2}C);", "\t"); - str = str.Replace("]0;", ""); + if (string.IsNullOrEmpty(str) || str.IndexOf('\u001b') < 0) + return str; + + str = str.Replace(Esc + "[?25l" + Esc + "[2J" + Esc + "[m" + Esc + "[H", ""); + // Rows of the winws status screen start a new line. + str = str.Replace(Esc + "[4;1H", "\n"); + str = str.Replace(Esc + "[12;1H", "\n"); + str = str.Replace(Esc + "[23;1H", "\n\t"); + str = str.Replace(Esc + "[32m", "\n"); + str = str.Replace(Esc + "[90m", "\n"); + str = CursorForwardRegex.Replace(str, match => + new string(' ', int.TryParse(match.Groups[1].Value, out int count) ? Math.Clamp(count, 1, 200) : 1)); + str = OscRegex.Replace(str, ""); + str = CsiRegex.Replace(str, ""); + str = OtherEscapeRegex.Replace(str, ""); return str; } + + /// + /// Splits off an escape sequence cut at the end of a chunk, so that it is cleaned together + /// with the rest of it in the next chunk. + /// + public static string TakeIncompleteTail(ref string chunk) + { + int index = chunk.LastIndexOf('\u001b'); + if (index < 0 || chunk.Length - index > 64) + return string.Empty; + + string tail = chunk.Substring(index); + if (CompleteSequenceRegex.IsMatch(tail)) + return string.Empty; + + chunk = chunk.Substring(0, index); + return tail; + } } } diff --git a/CDPIUI.Shared/ComponentsTask/ProcessManagerInterface.cs b/CDPIUI.Shared/ComponentsTask/ProcessManagerInterface.cs index 0d94fd92..5512ecec 100644 --- a/CDPIUI.Shared/ComponentsTask/ProcessManagerInterface.cs +++ b/CDPIUI.Shared/ComponentsTask/ProcessManagerInterface.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Threading.Tasks; diff --git a/CDPIUI.Shared/ComponentsTask/TasksManageService.cs b/CDPIUI.Shared/ComponentsTask/TasksManageService.cs index 15a80627..0996d66a 100644 --- a/CDPIUI.Shared/ComponentsTask/TasksManageService.cs +++ b/CDPIUI.Shared/ComponentsTask/TasksManageService.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Linq; @@ -67,32 +67,47 @@ public TasksManageService() { } return null; } - /// - /// Add new task to task list - /// - /// Task Id - public async void AddNewTask(string id) + private async Task<(TaskModel Task, bool Created)> GetOrAddTask(string id, bool? isSetupComplete = null) { - var t = await GetTaskFromId(id); - if (t != null) return; - await _taskOperationLock.WaitAsync(); - - var processManager = new T() { Id = id }; - processManager.ProcessStateChanged += HandleProcessStateUpdate; - processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; - try { - Tasks.Add(new() { Id = id, ProcessManager = processManager }); + var existTask = Tasks.FirstOrDefault(t => t.Id == id); + if (existTask != null) + return (existTask, false); + + T processManager = new() { Id = id }; + processManager.ProcessStateChanged += HandleProcessStateUpdate; + processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; + + var task = new TaskModel { Id = id, ProcessManager = processManager, IsSetupComplete = isSetupComplete }; + Tasks.Add(task); + return (task, true); } - catch { } finally { _taskOperationLock.Release(); } + } - TaskListUpdated?.Invoke(); + /// + /// Add new task to task list + /// + /// Task Id + public async void AddNewTask(string id) + { + await AddNewTaskAsync(id); + } + + /// + /// Add new task to task list + /// + /// Task Id + public async Task AddNewTaskAsync(string id) + { + var (_, created) = await GetOrAddTask(id); + if (created) + TaskListUpdated?.Invoke(); } /// @@ -101,31 +116,13 @@ public async void AddNewTask(string id) /// Task Id public async Task CreateAndRunNewTask(string id) { - var t = await GetTaskFromId(id); - if (t != null) - { - await StopTask(id); - await t.ProcessManager.StartProcess(); - } + var (task, created) = await GetOrAddTask(id); + if (created) + TaskListUpdated?.Invoke(); else - { - T processManager = new() { Id = id }; - processManager.ProcessStateChanged += HandleProcessStateUpdate; - processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; - await processManager.StartProcess(); + await task.ProcessManager.StopProcess(); - await _taskOperationLock.WaitAsync(); - try - { - Tasks.Add(new() { Id = id, ProcessManager = processManager }); - } - catch { } - finally - { - _taskOperationLock.Release(); - } - TaskListUpdated?.Invoke(); - } + await task.ProcessManager.StartProcess(); } /// /// Add new task to task list, run associated with custom executable @@ -135,32 +132,13 @@ public async Task CreateAndRunNewTask(string id) /// Command-line flags public virtual async Task CreateAndRunNewTask(string id, string executable, string args) { - var t = await GetTaskFromId(id); - if (t != null) - { - await StopTask(id); - await t.ProcessManager.StartProcess(executable, args); - } + var (task, created) = await GetOrAddTask(id); + if (created) + TaskListUpdated?.Invoke(); else - { - await _taskOperationLock.WaitAsync(); + await task.ProcessManager.StopProcess(); - T processManager = new() { Id = id }; - processManager.ProcessStateChanged += HandleProcessStateUpdate; - processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; - await processManager.StartProcess(executable, args); - - try - { - Tasks.Add(new() { Id = id, ProcessManager = processManager }); - } - catch { } - finally - { - _taskOperationLock.Release(); - } - TaskListUpdated?.Invoke(); - } + await task.ProcessManager.StartProcess(executable, args); } /// @@ -170,32 +148,13 @@ public virtual async Task CreateAndRunNewTask(string id, string executable, stri /// Command-line flags public async Task CreateAndRunNewTask(string id, string args) { - var t = await GetTaskFromId(id); - if (t != null) - { - await StopTask(id); - await t.ProcessManager.StartProcess(args, null); - } + var (task, created) = await GetOrAddTask(id); + if (created) + TaskListUpdated?.Invoke(); else - { - await _taskOperationLock.WaitAsync(); + await task.ProcessManager.StopProcess(); - T processManager = new() { Id = id }; - processManager.ProcessStateChanged += HandleProcessStateUpdate; - processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; - await processManager.StartProcess(args, null); - - try - { - Tasks.Add(new() { Id = id, ProcessManager = processManager }); - } - catch { } - finally - { - _taskOperationLock.Release(); - } - TaskListUpdated?.Invoke(); - } + await task.ProcessManager.StartProcess(args, null); } /// @@ -205,31 +164,11 @@ public async Task CreateAndRunNewTask(string id, string args) /// Is task with Id ready to run public async void SetTaskStatus(string id, bool isSetupComplete) { - var targetTask = await GetTaskFromId(id); - if (targetTask != null) - { - targetTask.IsSetupComplete = isSetupComplete; - } - else - { - await _taskOperationLock.WaitAsync(); - - T processManager = new() { Id = id }; - processManager.ProcessStateChanged += HandleProcessStateUpdate; - processManager.ShowErrorMessageWindow += HandleShowErrorMessageWindow; - - try - { - Tasks.Add(new() { Id = id, ProcessManager = processManager, IsSetupComplete = isSetupComplete }); - } - catch { } - finally - { - _taskOperationLock.Release(); - } - + var (targetTask, created) = await GetOrAddTask(id, isSetupComplete); + if (created) TaskListUpdated?.Invoke(); - } + else + targetTask.IsSetupComplete = isSetupComplete; TaskSetupStateUpdated?.Invoke(Tuple.Create(id, isSetupComplete)); } @@ -240,13 +179,11 @@ public async void SetTaskStatus(string id, bool isSetupComplete) /// Target status public async void ApplyStatusToAllTasks(bool status) { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { task.IsSetupComplete = status; TaskSetupStateUpdated?.Invoke(Tuple.Create(task.Id, status)); } - - await Task.CompletedTask; } /// @@ -309,15 +246,26 @@ public async Task RestartTask(string id) /// public async Task StopAllTasks() { - await _taskOperationLock.WaitAsync(); - try + foreach (var task in await GetTasksSnapshot()) { - foreach (var task in Tasks) + try { await task.ProcessManager.StopProcess(); } + catch { } + } + } + + /// + /// Get a copy of the tasks list that is safe to enumerate while tasks are added or removed + /// + public async Task>> GetTasksSnapshot() + { + await _taskOperationLock.WaitAsync(); + try + { + return [.. Tasks]; } - catch { } finally { _taskOperationLock.Release(); @@ -325,11 +273,11 @@ public async Task StopAllTasks() } /// - /// Is task wuth Id runned + /// Is task wuth Id running /// /// Task Id - /// true if runned, otherwise false - public async Task IsTaskRunned(string id) + /// true if running, otherwise false + public async Task IsTaskRunning(string id) { var task = await GetTaskFromId(id); if (task == null) return false; diff --git a/CDPIUI.Shared/ConditionalLaunch/ConditionalTaskModels.cs b/CDPIUI.Shared/ConditionalLaunch/ConditionalTaskModels.cs index 83f294f9..f54bd9de 100644 --- a/CDPIUI.Shared/ConditionalLaunch/ConditionalTaskModels.cs +++ b/CDPIUI.Shared/ConditionalLaunch/ConditionalTaskModels.cs @@ -181,21 +181,29 @@ public static string GetTasksDirectoryFromSettingsFile(string settingsFilePath) return Path.Combine(dataDirectory, DirectoryName); } - public static IReadOnlyList LoadDirectory(string directoryPath) + /// Receives the path and the error of every file that is skipped + public static IReadOnlyList LoadDirectory(string directoryPath, Action? onError = null) { Directory.CreateDirectory(directoryPath); var tasks = new List(); - foreach (var filePath in Directory.EnumerateFiles(directoryPath, $"*{FileExtension}")) + var ids = new HashSet(StringComparer.OrdinalIgnoreCase); + // Sorted, so the same copy of a duplicated task wins on every load. + foreach (var filePath in Directory.EnumerateFiles(directoryPath, $"*{FileExtension}") + .OrderBy(path => path, StringComparer.OrdinalIgnoreCase)) { try { - tasks.Add(Load(filePath)); + ConditionalTask task = Load(filePath); + // A copied task file would otherwise run the same task twice. + if (!ids.Add(task.Id)) + throw new InvalidDataException($"Another task file already has the identifier {task.Id}."); + tasks.Add(task); } - catch + catch (Exception ex) { - // Invalid task files are ignored by the runtime. The editor - // reports import/load errors to the user separately. + // Invalid task files are skipped. The editor reports import errors to the user separately. + onError?.Invoke(filePath, ex); } } @@ -234,13 +242,35 @@ public static string Save(ConditionalTask task, string directoryPath, string? fi if (!filePath.StartsWith(targetDirectory, StringComparison.OrdinalIgnoreCase)) filePath = Path.Combine(directoryPath, $"{task.Id}{FileExtension}"); - using (var stream = File.Create(filePath)) - Serializer.Serialize(stream, task); + WriteTask(task, filePath); task.FilePath = filePath; return filePath; } + // Writes next to the target and swaps the file in, so a crash never leaves a truncated task. + private static void WriteTask(ConditionalTask task, string filePath) + { + string tempPath = filePath + ".tmp"; + try + { + using (var stream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + Serializer.Serialize(stream, task); + stream.Flush(true); + } + + if (File.Exists(filePath)) + File.Replace(tempPath, filePath, null); + else + File.Move(tempPath, filePath); + } + finally + { + if (File.Exists(tempPath)) File.Delete(tempPath); + } + } + public static void Export(ConditionalTask task, string filePath) { Validate(task); @@ -248,8 +278,7 @@ public static void Export(ConditionalTask task, string filePath) if (!string.Equals(Path.GetExtension(filePath), FileExtension, StringComparison.OrdinalIgnoreCase)) filePath += FileExtension; - using var stream = File.Create(filePath); - Serializer.Serialize(stream, task); + WriteTask(task, Path.GetFullPath(filePath)); } public static void Validate(ConditionalTask task) diff --git a/CDPIUI.Shared/Exceptions/Database/ApplicationItemRegistrationException.cs b/CDPIUI.Shared/Exceptions/Database/ApplicationItemRegistrationException.cs index b4740a1f..120fe821 100644 --- a/CDPIUI.Shared/Exceptions/Database/ApplicationItemRegistrationException.cs +++ b/CDPIUI.Shared/Exceptions/Database/ApplicationItemRegistrationException.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Exceptions.Interface; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Exceptions/Database/DatabaseUserItemRegistrationException.cs b/CDPIUI.Shared/Exceptions/Database/DatabaseUserItemRegistrationException.cs index ac95e76c..4699296e 100644 --- a/CDPIUI.Shared/Exceptions/Database/DatabaseUserItemRegistrationException.cs +++ b/CDPIUI.Shared/Exceptions/Database/DatabaseUserItemRegistrationException.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Exceptions.Interface; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Exceptions/Interface/CustomExceptionInterface.cs b/CDPIUI.Shared/Exceptions/Interface/CustomExceptionInterface.cs index e283228b..f1e711c1 100644 --- a/CDPIUI.Shared/Exceptions/Interface/CustomExceptionInterface.cs +++ b/CDPIUI.Shared/Exceptions/Interface/CustomExceptionInterface.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Exceptions/Settings/SettingNotFoundException.cs b/CDPIUI.Shared/Exceptions/Settings/SettingNotFoundException.cs index e641a51e..17050efe 100644 --- a/CDPIUI.Shared/Exceptions/Settings/SettingNotFoundException.cs +++ b/CDPIUI.Shared/Exceptions/Settings/SettingNotFoundException.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Exceptions.Interface; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Exceptions/Settings/SettingTypeNotSupported.cs b/CDPIUI.Shared/Exceptions/Settings/SettingTypeNotSupported.cs index 2a1d5266..8fd9b05a 100644 --- a/CDPIUI.Shared/Exceptions/Settings/SettingTypeNotSupported.cs +++ b/CDPIUI.Shared/Exceptions/Settings/SettingTypeNotSupported.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Exceptions.Interface; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Extentions/DictionaryExtention.cs b/CDPIUI.Shared/Extensions/DictionaryExtension.cs similarity index 66% rename from CDPIUI.Shared/Extentions/DictionaryExtention.cs rename to CDPIUI.Shared/Extensions/DictionaryExtension.cs index 85fc76ea..53034c4e 100644 --- a/CDPIUI.Shared/Extentions/DictionaryExtention.cs +++ b/CDPIUI.Shared/Extensions/DictionaryExtension.cs @@ -3,9 +3,9 @@ using System.Linq; using System.Text; -namespace CDPIUI.Shared.Extentions +namespace CDPIUI.Shared.Extensions { - public static class DictionaryExtention + public static class DictionaryExtension { /// /// Get dictionary key by value.

@@ -13,14 +13,14 @@ public static class DictionaryExtention /// uses .

/// Otherwise uses default object comparier. ///
- public static KeyType GetKeyByValue(this Dictionary dicitionary, ValueType value) where ValueType : class + public static KeyType GetKeyByValue(this Dictionary dictionary, ValueType value) where ValueType : class { if (value is string stval) { - return dicitionary.FirstOrDefault(kvp => string.Equals(kvp.Value as string, value as string, StringComparison.Ordinal)).Key; + return dictionary.FirstOrDefault(kvp => string.Equals(kvp.Value as string, value as string, StringComparison.Ordinal)).Key; } - return dicitionary.FirstOrDefault(kvp => kvp.Value == value).Key; + return dictionary.FirstOrDefault(kvp => kvp.Value == value).Key; } public static void AddRange(this ICollection target, IEnumerable source) diff --git a/CDPIUI.Shared/Extentions/EnumExtention.cs b/CDPIUI.Shared/Extensions/EnumExtension.cs similarity index 87% rename from CDPIUI.Shared/Extentions/EnumExtention.cs rename to CDPIUI.Shared/Extensions/EnumExtension.cs index bd06afb8..53f5b953 100644 --- a/CDPIUI.Shared/Extentions/EnumExtention.cs +++ b/CDPIUI.Shared/Extensions/EnumExtension.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Text; -namespace CDPIUI.Shared.Extentions +namespace CDPIUI.Shared.Extensions { - public static class EnumExtention + public static class EnumExtension { public static T ToEnum(this string value) { diff --git a/CDPIUI.Shared/Extentions/LogicalComparer.cs b/CDPIUI.Shared/Extensions/LogicalComparer.cs similarity index 93% rename from CDPIUI.Shared/Extentions/LogicalComparer.cs rename to CDPIUI.Shared/Extensions/LogicalComparer.cs index 133ff654..ff254708 100644 --- a/CDPIUI.Shared/Extentions/LogicalComparer.cs +++ b/CDPIUI.Shared/Extensions/LogicalComparer.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; -namespace CDPIUI.Shared.Extentions +namespace CDPIUI.Shared.Extensions { public class LogicalComparer : IComparer { diff --git a/CDPIUI.Shared/Extentions/StringExtention.cs b/CDPIUI.Shared/Extensions/StringExtension.cs similarity index 85% rename from CDPIUI.Shared/Extentions/StringExtention.cs rename to CDPIUI.Shared/Extensions/StringExtension.cs index c969b1f6..65d43552 100644 --- a/CDPIUI.Shared/Extentions/StringExtention.cs +++ b/CDPIUI.Shared/Extensions/StringExtension.cs @@ -3,9 +3,9 @@ using System.Linq; using System.Text; -namespace CDPIUI.Shared.Extentions +namespace CDPIUI.Shared.Extensions { - public static class StringExtention + public static class StringExtension { public static string? SerializeTuples(this List> list) { @@ -27,7 +27,7 @@ public static string FirstCharToUpper(this string input) => { null => throw new ArgumentNullException(nameof(input)), "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)), - _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1).ToString()) + _ => string.Concat(input[0].ToString().ToUpperInvariant(), input.AsSpan(1).ToString()) }; } } diff --git a/CDPIUI.Shared/Logger/LoggerBase.cs b/CDPIUI.Shared/Logger/LoggerBase.cs index 298455bb..18ab6733 100644 --- a/CDPIUI.Shared/Logger/LoggerBase.cs +++ b/CDPIUI.Shared/Logger/LoggerBase.cs @@ -31,21 +31,35 @@ public class LoggerBase(string? actualPath, LogSelevirity logLevel) public readonly LogSelevirity? LogLevel = logLevel; private static readonly object _logLock = new object(); + // A log grows without limit otherwise; the previous file is kept as *.old.log. + private const long MaxLogFileSize = 5 * 1024 * 1024; + private void LogWrite(string fileName, string logMessage) { if (string.IsNullOrEmpty(ActualPath)) return; try { string logFileDir = Path.Combine(ActualPath, LoggerResources.LogsFolder); - string logFilePath = Path.Combine(logFileDir, $"{fileName}{LoggerResources.LogsFileExtention}"); + // A sender such as "StoreHelper/CheckUpdates" named a missing folder, and the log was lost. + foreach (char invalid in Path.GetInvalidFileNameChars()) + fileName = fileName.Replace(invalid, '_'); + string logFilePath = Path.Combine(logFileDir, $"{fileName}{LoggerResources.LogsFileExtension}"); if (!Directory.Exists(logFileDir)) Directory.CreateDirectory(logFileDir); - if (!File.Exists(logFilePath)) + + var info = new FileInfo(logFilePath); + if (info.Exists && info.Length > MaxLogFileSize) { - File.WriteAllText(logFilePath, LoggerResources.LoggerReadyMessage + $" {LogLevel} " + Environment.NewLine); + File.Copy(logFilePath, Path.ChangeExtension(logFilePath, ".old" + LoggerResources.LogsFileExtension), true); + File.WriteAllText(logFilePath, string.Empty); + info.Refresh(); } - using StreamWriter w = File.AppendText(logFilePath); + // Shared write access: the UI and the tray may log to the same file at the same time. + using var stream = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); + using var w = new StreamWriter(stream); + if (!info.Exists || info.Length == 0) + w.WriteLine(LoggerResources.LoggerReadyMessage + $" {LogLevel} "); WriteMessageToTextWriter(logMessage, w); } catch { } diff --git a/CDPIUI.Shared/Logger/LoggerResources.cs b/CDPIUI.Shared/Logger/LoggerResources.cs index 2a279175..f2073b98 100644 --- a/CDPIUI.Shared/Logger/LoggerResources.cs +++ b/CDPIUI.Shared/Logger/LoggerResources.cs @@ -12,9 +12,9 @@ public static class LoggerResources public static string LogsFolder = "Logs"; /// - /// WriteMessageToTextWriter file extention with dot (.log) + /// WriteMessageToTextWriter file extension with dot (.log) /// - public static string LogsFileExtention = ".log"; + public static string LogsFileExtension = ".log"; /// /// LoggerBase first initialization message diff --git a/CDPIUI.Shared/Models/Models.cs b/CDPIUI.Shared/Models/Models.cs index 7b269697..8e15d0b7 100644 --- a/CDPIUI.Shared/Models/Models.cs +++ b/CDPIUI.Shared/Models/Models.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Text; diff --git a/CDPIUI.Shared/Pipe/Models/PipeMessageIds.cs b/CDPIUI.Shared/Pipe/Models/PipeMessageIds.cs index 276dab1a..5dbaa7fd 100644 --- a/CDPIUI.Shared/Pipe/Models/PipeMessageIds.cs +++ b/CDPIUI.Shared/Pipe/Models/PipeMessageIds.cs @@ -62,7 +62,7 @@ public enum CONPTYMessageIds public enum GoodCheckMessageIds { // ToClient - Runned, + Running, Died, DiedViaError, // ToServer diff --git a/CDPIUI.Shared/Pipe/Models/PipeMessageModels.cs b/CDPIUI.Shared/Pipe/Models/PipeMessageModels.cs index a9879d1b..6bb8ef31 100644 --- a/CDPIUI.Shared/Pipe/Models/PipeMessageModels.cs +++ b/CDPIUI.Shared/Pipe/Models/PipeMessageModels.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System; using System.Collections.Generic; using System.Collections.Specialized; @@ -24,7 +24,10 @@ public override string ToString() foreach (var name in MessageData.AllKeys) { - @params += $"{name}={MessageData[name]}&"; + if (name == null) + continue; + + @params += $"{Uri.EscapeDataString(name)}={Uri.EscapeDataString(MessageData[name] ?? string.Empty)}&"; } return $"{SharedConstants.Schema}://{Target}/{MessageType}?{@params}"; diff --git a/CDPIUI.Shared/Pipe/Models/PipeModelConvertor.cs b/CDPIUI.Shared/Pipe/Models/PipeModelConvertor.cs index db2d7736..3e5573ca 100644 --- a/CDPIUI.Shared/Pipe/Models/PipeModelConvertor.cs +++ b/CDPIUI.Shared/Pipe/Models/PipeModelConvertor.cs @@ -1,4 +1,4 @@ -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System; using System.Web; diff --git a/CDPIUI.Shared/Pipe/PipePeerVerifier.cs b/CDPIUI.Shared/Pipe/PipePeerVerifier.cs new file mode 100644 index 00000000..8142d49c --- /dev/null +++ b/CDPIUI.Shared/Pipe/PipePeerVerifier.cs @@ -0,0 +1,68 @@ +using System; +using System.IO; +using System.IO.Pipes; +using System.Runtime.InteropServices; +using System.Text; + +namespace CDPIUI.Shared.Pipe +{ + public static class PipePeerVerifier + { + private const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; + + public static bool IsClientExpected(NamedPipeServerStream pipe, string expectedImagePath) => + GetNamedPipeClientProcessId(pipe.SafePipeHandle, out uint processId) && + IsProcessImage(processId, expectedImagePath); + + public static bool IsServerExpected(NamedPipeClientStream pipe, string expectedImagePath) => + GetNamedPipeServerProcessId(pipe.SafePipeHandle, out uint processId) && + IsProcessImage(processId, expectedImagePath); + + public static bool IsProcessImage(uint processId, string expectedImagePath) + { + string? imagePath = GetProcessImagePath(processId); + if (imagePath == null) + return false; + +#if DEBUG + // Debug builds of the UI and the tray are produced in different output folders. + return string.Equals(Path.GetFileName(imagePath), Path.GetFileName(expectedImagePath), StringComparison.OrdinalIgnoreCase); +#else + return string.Equals(Path.GetFullPath(imagePath), Path.GetFullPath(expectedImagePath), StringComparison.OrdinalIgnoreCase); +#endif + } + + public static string? GetProcessImagePath(uint processId) + { + IntPtr process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId); + if (process == IntPtr.Zero) + return null; + + try + { + var buffer = new StringBuilder(32768); + int size = buffer.Capacity; + return QueryFullProcessImageName(process, 0, buffer, ref size) ? buffer.ToString(0, size) : null; + } + finally + { + CloseHandle(process); + } + } + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GetNamedPipeClientProcessId(SafeHandle pipe, out uint clientProcessId); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GetNamedPipeServerProcessId(SafeHandle pipe, out uint serverProcessId); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr OpenProcess(uint desiredAccess, bool inheritHandle, uint processId); + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern bool QueryFullProcessImageName(IntPtr process, int flags, StringBuilder exeName, ref int size); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool CloseHandle(IntPtr handle); + } +} diff --git a/CDPIUI.Shared/Pipe/PipeServiceBase.cs b/CDPIUI.Shared/Pipe/PipeServiceBase.cs index 3ce1413d..48468a79 100644 --- a/CDPIUI.Shared/Pipe/PipeServiceBase.cs +++ b/CDPIUI.Shared/Pipe/PipeServiceBase.cs @@ -69,20 +69,32 @@ public async Task HandleConnectionAsync() continue; } - Logger?.CreateInfoLog(nameof(PipeServiceBase), $"Received message: {message}"); + Logger?.CreateDebugLog(nameof(PipeServiceBase), $"Received message: {RedactSecrets(message)}"); } Logger?.CreateInfoLog(nameof(PipeServiceBase), $"{CancellationToken?.IsCancellationRequested}, {PipeStream.IsConnected.ToString()}"); } + // The authentication GUID must not end up in log files. + private static string RedactSecrets(string message) => + System.Text.RegularExpressions.Regex.Replace(message, "(GUID=)[^&]*", "$1***", + System.Text.RegularExpressions.RegexOptions.IgnoreCase); + public async void SendMessage(string message) { await SendMessageAsync(message); } public async Task SendMessageAsync(string message) { + if (!CanSend(message)) + return false; return await SendStringMessageToPipe(message); } + /// + /// Lets a server hold messages back, for example until the client is authorized. + /// + protected virtual bool CanSend(string message) => true; + protected virtual async Task SendConnectionPacket() { if (PipeStream == null) return; @@ -116,7 +128,7 @@ protected virtual async Task RunMessageActions(IPipeMessage model) private async Task SendStringMessageToPipe(string message) { - Logger?.CreateDebugLog(nameof(PipeServiceBase), $"Message \"{message}\" added to send queue"); + Logger?.CreateDebugLog(nameof(PipeServiceBase), $"Message \"{RedactSecrets(message)}\" added to send queue"); if (PipeStream == null || !PipeStream.IsConnected || StreamString == null) { @@ -133,7 +145,7 @@ private async Task SendStringMessageToPipe(string message) } catch (Exception ex) { - Logger?.CreateWarningLog(nameof(PipeServiceBase), $"Message \"{message}\" cannot be sended during exception {ex.Message}"); + Logger?.CreateWarningLog(nameof(PipeServiceBase), $"Message \"{RedactSecrets(message)}\" cannot be sent: {ex.Message}"); return false; } finally @@ -148,7 +160,12 @@ private async Task SendStringMessageToPipe(string message) public virtual void Dispose() { Disconnected?.Invoke(); - PipeStream?.WaitForPipeDrain(); + try + { + if (PipeStream?.IsConnected == true) + PipeStream.WaitForPipeDrain(); + } + catch (Exception ex) when (ex is IOException or ObjectDisposedException or InvalidOperationException) { } PipeStream?.Dispose(); StreamString = null; diff --git a/CDPIUI.Shared/Pipe/README.md b/CDPIUI.Shared/Pipe/README.md new file mode 100644 index 00000000..7e5f959e --- /dev/null +++ b/CDPIUI.Shared/Pipe/README.md @@ -0,0 +1,268 @@ +# Pipe protocol + +The GUI (`CDPIUI.exe`) and the tray (`CDPIUI_TrayIcon.exe`) use one named pipe to send messages. +The tray is the pipe server. The GUI is the pipe client. + +| Part | File | +|---|---| +| Shared base class, framing, message models | `CDPIUI.Shared/Pipe/` | +| Server (tray) | `CDPIUI.TrayIcon/Helper/Basic/ConnectionHelper.cs` (`PipeServer`), `PipeAccessPolicy.cs` | +| Tray handlers and senders | `CDPIUI.TrayIcon/Helper/Basic/CommandsHandler.cs`, `CDPIUI.TrayIcon/Helper/PipeHelper.cs` | +| Client (GUI) | `CDPIUI.Core/Communication/PipeClientService.cs` | +| GUI handlers and senders | `CDPIUI.Core/Communication/CoreCommandsHandler.cs`, `CDPIUI.Core/Communication/PipeHelper.cs`, `CDPIUI/Commands/CommandsHandler.cs` | + +## Pipe and connection + +- The pipe name is `SharedConstants.PipeName`: `{203ABE1D-01A2-47C0-B33D-8C8F7934CF4F}_CDPIUI`. +- The server accepts one client at a time (`_maxServerInstances = 1`). +- The client connects with `TokenImpersonationLevel.Identification`. +- After a disconnect, the server creates a new pipe instance and waits for the next client. + +The GUI connects in `PipeClientService.Start`: + +1. The GUI tries to connect for 2 seconds. +2. If a `CDPIUI_TrayIcon` process runs, the GUI tries again for 15 seconds. +3. If there is no connection, the GUI starts `CDPIUI_TrayIcon.exe` with `runas` and stops its own process. +4. The tray then starts the GUI again (see [CDPIUI.TrayIcon/README.md](../../CDPIUI.TrayIcon/README.md)). + +## Security + +### Pipe access + +`PipeAccessPolicy.CreatePipeSecurity` sets this access list: + +| Account | Access | +|---|---| +| The tray user (owner) | `FullControl` | +| `LocalSystem` | `FullControl` | +| The desktop user, if it is not the tray user | `ReadWrite` | +| `Network` | `FullControl`, deny | + +The desktop user is the owner of the shell window process (`GetShellWindow`). +This rule is necessary when the tray runs elevated with a different administrator account. + +### Peer executable check + +Each side checks the executable of the other process (`PipePeerVerifier`): + +- The server calls `IsClientExpected`. The expected client is `CDPIUI.exe` in the tray directory. +- The client calls `IsServerExpected`. The expected server is `CDPIUI_TrayIcon.exe` in the GUI directory. +- Release builds compare the full image paths. +- `DEBUG` builds compare only the file names, because the GUI and the tray have different output directories. + +The server disconnects a client that fails the check. +The client stops its own process if the server fails the check. + +### Authorization + +1. The server sends `Service/ConnectOK` after the connection. +2. The client sends `Service/RequestAuth` with the `GUID` key. The value is `Secret.AuthGuid`. +3. If the tray `Secret.AuthGuid` is not empty and the value is equal to it, the server sends `Service/AuthOK`. +4. In all other conditions, the server sends `Service/AuthFAIL` and disconnects the client. + +Rules: + +- An empty `Secret.AuthGuid` in the tray authorizes no client. +- The server disconnects the client if the client is not authorized 15 seconds after the connection. +- A failed `RequestAuth` always disconnects the client, also after an earlier `AuthOK`. The client cannot try again on the same connection. +- Before authorization, the server sends only `Service` messages (`PipeServer.CanSend`). +- Before authorization, the server handles only `RequestAuth`. It ignores all other messages and writes a warning to the log. +- The GUI and the tray must be built with the same `Secret.AuthGuid`. +- The log entries for received, queued and failed messages replace the `GUID` value with `***` (`PipeServiceBase.RedactSecrets`). + +CAUTION: Do not build with an empty `AuthGuid`. With an empty value, the GUI cannot connect to the tray. `create_secret_stub.ps1` and `create_release_secret.ps1` write a random value. + +## Framing + +`StreamString` writes each message as one frame: + +- The text is UTF-16 LE (`Encoding.Unicode`). +- A message shorter than 0xFFFF bytes has a 2-byte big-endian length before the text. +- A longer message has the marker `0xFF 0xFF`, then a 4-byte big-endian length. +- The maximum message length is 16 MiB. A longer message causes `InvalidDataException`. + +`PipeServiceBase.SendMessageAsync` uses a lock, so only one frame is written at a time. + +## Message model + +A message is a URI: + +```text +cdpiui:///?=&=& +``` + +- `` is a `PipeMessageTargetIds` name. +- `` is a name from the message enum of the target, for example `CONPTYMessageIds`. +- `MessageBaseModel.ToString` writes the enum name, not the integer. `IntMessageType` is not used in the URI. +- The data is a `NameValueCollection`. Keys and values are escaped with `Uri.EscapeDataString`. +- A message without data has no `?` part. + +`PipeModelConvertor.ConvertBack` reads a message: + +- The host and the path are parsed with `Enum.Parse(..., ignoreCase: true)`. `Uri` changes the host to lower case. +- The query is parsed with `HttpUtility.ParseQueryString`. +- An unknown target or type causes an exception. `PipeServiceBase` writes it to the log and ignores the message. +- A `null` value is sent as an empty string. The receiver gets `""`, not `null`. + +Use `PipePayloadCodec` (URL-safe Base64 of UTF-8) for binary data and for paths that must not change on the way. +`CaptureHelpOutput` and the help output messages use it. + +## Message families + +Direction: "GUI → tray" or "tray → GUI". In the GUI, `CoreCommandsHandler` and `CDPIUI/Commands/CommandsHandler.cs` handle the messages. + +### Service + +| Type | Direction | Data | +|---|---|---| +| `ConnectOK` | tray → GUI | none | +| `RequestAuth` | GUI → tray | `GUID` | +| `AuthOK` | tray → GUI | none. The GUI raises `Connected`. | +| `AuthFAIL` | tray → GUI | none. The GUI writes an error to the log. | + +### Presentation + +| Type | Direction | Data | +|---|---|---| +| `ShowWindow` | tray → GUI | `windowName` and optional window keys, for example `page`, `id`, `isUpdateRequested`, `action`, `helpUrl` | + +### CONPTY + +Component processes. `componentId` is the component id, for example `CSZTBN012`. + +| Type | Direction | Data | Effect | +|---|---|---|---| +| `StartProcessId` | GUI → tray | `componentId`, `exePath`, `args` | Starts the component through ConPTY. | +| `StopProcessId` | GUI → tray | `componentId` | Stops the component. | +| `RestartProcessId` | GUI → tray | `componentId` | Restarts the component. | +| `StopService` | GUI → tray | none | Stops the WinDivert components, then stops and deletes the `WinDivert` service. | +| `GetProcessIdFullOutput` | GUI → tray | `componentId` | The tray sends `ProcessIdFullOutput` for the task of `componentId`. If `componentId` is empty, the tray sends it for all tasks. | +| `GetAllProcessStates` | GUI → tray | none | The tray sends the state and the process name of all tasks. | +| `ProcessIdStartupArgsChanged` | GUI → tray | `componentId` | The next start asks the GUI for a new startup string. | +| `CaptureHelpOutput` | GUI → tray | `componentId`, `requestId`, `exePath` (`PipePayloadCodec`) | Runs ` --help` for 10 seconds. | +| `GetProcessIdState` | GUI → tray | `componentId` | The tray sends the state and the process name of the task of `componentId`. If `componentId` is empty, the tray sends them for all tasks. | +| `GetStartupString` | tray → GUI | `componentId` | The GUI starts the task. | +| `GetAllStartupStrings` | tray → GUI | `componentId` (empty) | The GUI starts all autorun components. | +| `CleanOutputForId` | tray → GUI | `componentId` | The GUI clears the output. | +| `MarkProcessIdAsStarted` | tray → GUI | `componentId` | | +| `MarkProcessIdAsStopped` | tray → GUI | `componentId`; on error also `errorHappens`, `errorMessage`, `errorObject`, `stateSnapshot` | With `stateSnapshot=true`, the GUI does not open the error window. | +| `ChangeProcessIdExecutable` | tray → GUI | `componentId`, `processName` | | +| `ProcessIdNewOutput` | tray → GUI | `componentId`, `output` | | +| `ProcessIdFullOutput` | tray → GUI | `componentId`, `output` | | +| `HelpOutputChunk` | tray → GUI | `requestId`, `chunkIndex`, `output` (`PipePayloadCodec`) | Chunks are 12 KiB of UTF-8. | +| `HelpOutputCompleted` | tray → GUI | `requestId`, `totalChunks`, `exitCode`, `timedOut`, `error` (`PipePayloadCodec`) | The GUI checks that all chunks arrived. | + +### GoodCheck + +| Type | Direction | Data | +|---|---|---| +| `Start` | GUI → tray | `operationId`, `exeFileName`, `args` | +| `Stop` | GUI → tray | none | +| `Running` | tray → GUI | `operationId`, `error` | +| `Died` | tray → GUI | `operationId`, `error` | +| `DiedViaError` | tray → GUI | `operationId`, `error` | + +The GUI reads `operationId` as an integer. + +### Utils + +| Type | Direction | Data | +|---|---|---| +| `GrantAccessRequest` | GUI → tray | `file`: a directory below `Store` | +| `GrantAccessResponse` | tray → GUI | `result`: `True` or `False` | + +### Settings + +| Type | Direction | Data | +|---|---|---| +| `AddToAutorun` | GUI → tray | none | +| `RemoveFromAutorun` | GUI → tray | none | +| `ReloadSettings` | GUI → tray | none | +| `ComponentSetupFinished` | GUI → tray | `componentId` | +| `ComponentSetupNotFinished` | GUI → tray | `componentId` | +| `ComponentNotInstalled` | GUI → tray | `componentId`. The tray stops and removes the task. | +| `AutorunFalse` | tray → GUI | none. The GUI sets `SYSTEM/autorun` to `false`. | + +### Update + +| Type | Direction | Data | +|---|---|---| +| `BeginApplicationUpdate` | GUI → tray | `filePath`: the update package (`.msi` or ZIP) | +| `UpdatesAreAvailable` | GUI → tray | none. The tray shows a notification. | +| `CheckForUpdates` | tray → GUI | none | + +### MSIInstallation + +| Type | Direction | Data | +|---|---|---| +| `Begin` | GUI → tray | `operationId`, `fileName` | +| `Kill` | GUI → tray | `operationId` | +| `SetOperationStatus` | tray → GUI | `operationId`, `state`: a `MsiState` name | +| `RemoveOperationId` | tray → GUI | `operationId`, `state` (empty) | + +`MsiState` values: `GettingReady`, `Installing`, `Complete`, `CompleteRestartRequest`, `ExceptionHappens`. +The enum exists in `CDPIUI.TrayIcon/Helper/MsiInstallerHelper.cs` and in `CDPIUI.Core/Store/MSI/MsiInstallerService.cs`. The names must stay equal. + +### Proxy + +All messages go from the GUI to the tray. + +| Type | Data | +|---|---| +| `Init` | `componentId`, `proxyFirePath` | +| `Setup` | `componentId`, `proxyType` (`AllSystem` or `ProxiFyre`), `ip`, `port` | +| `Clean` | `componentId` | + +### CompatibilityCheck + +| Type | Direction | Data | +|---|---|---| +| `Begin` | tray → GUI | none | + +### Notifications + +All messages go from the GUI to the tray. The tray shows a toast notification. + +| Type | Data | +|---|---| +| `ProxySetupRequired` | `componentName` | +| `CompatibilityCheckAssistant` | `componentName` | + +### Application + +| Type | Direction | Data | +|---|---|---| +| `HardRestart` | GUI → tray | none. The tray closes the GUI, starts it again and exits. | +| `CloseApplicationUI` | tray → GUI | none. The GUI stops its process. | + +### ConditionalLaunch + +| Type | Direction | Data | +|---|---|---| +| `ExecuteAction` | tray → GUI | `operationId`, `actionType`, and the action parameters | +| `ActionCompleted` | GUI → tray | `operationId`, `success`, `error` | +| `ReloadTasks` | GUI → tray | `tasksDirectory` | +| `ExecuteTask` | GUI → tray | `taskId` | + +The tray waits 45 seconds for `ActionCompleted`. + +## Messages when the GUI is not connected + +`PipeHelper.TrySendMessage` in the tray starts the GUI as the desktop user when the send fails. +The message goes in the argument `--direct:` for all targets. + +- For background messages to the `Service`, `CONPTY`, `Settings`, `Application`, `ConditionalLaunch`, `CompatibilityCheck` and `Update` targets, the tray also adds `--create-no-window`. +- For the other targets, for example `Presentation`, the tray does not add `--create-no-window`. + +The GUI handles `--direct:` with `CoreCommandsHandler`, then with `CommandsHandler.HandleCommand`. +The tray does not use `----ms-protocol:`. The GUI handles `----ms-protocol:` with `CommandUriMapper`, which accepts only public routes. Thus a pipe message in `----ms-protocol:`, for example `ShowWindow`, has no effect. + +## Add a message + +1. Add the value at the end of the enum in `Pipe/Models/PipeMessageIds.cs`. +2. For a new target, add a model in `PipeMessageModels.cs` and a case in `PipeModelConvertor.ConvertBack`. +3. Add a sender in the `PipeHelper` of the sending side. +4. Add a handler in `CommandsHandler` (tray) or in `CoreCommandsHandler` / `CDPIUI/Commands/CommandsHandler.cs` (GUI). +5. In the tray, check each path from the GUI with `TrustedPaths` before you use it. + +Tests: `CDPIUI.Tests/Shared/PipeMessageModelTests.cs`, `StreamStringTests.cs`, `PipePayloadCodecTests.cs`. diff --git a/CDPIUI.Shared/Pipe/StreamString.cs b/CDPIUI.Shared/Pipe/StreamString.cs index fc5c685e..30e63717 100644 --- a/CDPIUI.Shared/Pipe/StreamString.cs +++ b/CDPIUI.Shared/Pipe/StreamString.cs @@ -13,12 +13,25 @@ public class StreamString(Stream ioStream) ioStream ?? throw new ArgumentNullException(nameof(ioStream)); private readonly Encoding streamEncoding = Encoding.Unicode; + // Messages shorter than 0xFFFF bytes keep the original two-byte length. Longer ones, which were + // cut off in the middle of a character before, send 0xFFFF followed by a four-byte length. + private const int ExtendedLengthMarker = ushort.MaxValue; + private const int MaxMessageLength = 16 * 1024 * 1024; + public async Task ReadStringAsync(CancellationToken token = default) { byte[] lenBuffer = new byte[2]; await ReadExactlyAsync(lenBuffer, token).ConfigureAwait(false); int len = lenBuffer[0] * 256 + lenBuffer[1]; + if (len == ExtendedLengthMarker) + { + byte[] extendedBuffer = new byte[4]; + await ReadExactlyAsync(extendedBuffer, token).ConfigureAwait(false); + len = (extendedBuffer[0] << 24) | (extendedBuffer[1] << 16) | (extendedBuffer[2] << 8) | extendedBuffer[3]; + if (len < 0 || len > MaxMessageLength) + throw new InvalidDataException("The pipe message is too long."); + } byte[] inBuffer = new byte[len]; await ReadExactlyAsync(inBuffer, token).ConfigureAwait(false); @@ -46,15 +59,19 @@ private async Task ReadExactlyAsync(byte[] buffer, CancellationToken token) public async Task WriteStringAsync(string outString, CancellationToken token = default) { byte[] outBuffer = streamEncoding.GetBytes(outString ?? string.Empty); - int len = Math.Min(outBuffer.Length, ushort.MaxValue); + int len = outBuffer.Length; + if (len > MaxMessageLength) + throw new InvalidDataException("The pipe message is too long."); - byte[] header = new byte[2] { (byte)(len / 256), (byte)(len & 255) }; + byte[] header = len < ExtendedLengthMarker + ? new byte[] { (byte)(len / 256), (byte)(len & 255) } + : new byte[] { 0xFF, 0xFF, (byte)(len >> 24), (byte)(len >> 16), (byte)(len >> 8), (byte)len }; - await ioStream.WriteAsync(header, 0, 2, token).ConfigureAwait(false); + await ioStream.WriteAsync(header, 0, header.Length, token).ConfigureAwait(false); await ioStream.WriteAsync(outBuffer, 0, len, token).ConfigureAwait(false); await ioStream.FlushAsync(token).ConfigureAwait(false); - return len + 2; + return len + header.Length; } } } diff --git a/CDPIUI.Shared/PrettyErrorConvertionService/ErrorConvertor.cs b/CDPIUI.Shared/PrettyErrorConversionService/ErrorConvertor.cs similarity index 69% rename from CDPIUI.Shared/PrettyErrorConvertionService/ErrorConvertor.cs rename to CDPIUI.Shared/PrettyErrorConversionService/ErrorConvertor.cs index 45c37f38..766dcece 100644 --- a/CDPIUI.Shared/PrettyErrorConvertionService/ErrorConvertor.cs +++ b/CDPIUI.Shared/PrettyErrorConversionService/ErrorConvertor.cs @@ -13,7 +13,7 @@ using System.Net.WebSockets; using System.Text; -namespace CDPIUI.Shared.PrettyErrorConvertionService +namespace CDPIUI.Shared.PrettyErrorConversionService { /// /// Gets pretty errors from exceptions to show in userspace @@ -65,6 +65,8 @@ public virtual PrettyErrorCode MapExceptionToCode(Exception ex, out uint? rawHRe return PrettyErrorCode.CATALOG_SIGNATURE_CHECK_FAILURE; case CatalogInvalid: return PrettyErrorCode.CATALOG_INVALID; + case CertificateNotTrusted: + return PrettyErrorCode.CERTIFICATE_INVALID; case NewestVersionAlreadyInstalledException: return PrettyErrorCode.NEWEST_VERSION_INSTALLED; case UnknownFileFormatException: @@ -76,8 +78,19 @@ public virtual PrettyErrorCode MapExceptionToCode(Exception ex, out uint? rawHRe case UnknownException: return PrettyErrorCode.UNKNOWN; + case TimeoutException: + return PrettyErrorCode.TIMEOUT; + case SocketException socketException: + { + PrettyErrorCode socketCode = MapSocketError(socketException.SocketErrorCode); + if (socketCode != PrettyErrorCode.UNKNOWN) return socketCode; + break; + } } - if (current is IOException) + + statusCode ??= GetHttpStatusCode(current); + // A network IOException wraps the socket error, which says more. + if (current is IOException && current.InnerException is not SocketException) { var hrGeneric = unchecked((uint)current.HResult); @@ -92,7 +105,7 @@ public virtual PrettyErrorCode MapExceptionToCode(Exception ex, out uint? rawHRe return PrettyErrorCode.EXTRACT_INVALID_ARCHIVE; int _hr = current.HResult; - if (ex is Win32Exception w32ex) + if (current is Win32Exception w32ex) { _hr = w32ex.NativeErrorCode; } @@ -106,17 +119,65 @@ public virtual PrettyErrorCode MapExceptionToCode(Exception ex, out uint? rawHRe if (code != PrettyErrorCode.UNKNOWN) return code; } } - if (ex is UriFormatException) - return PrettyErrorCode.INVALID_URI; - if (ex is TimeoutException) - return PrettyErrorCode.TIMEOUT; + if (statusCode != null) + return PrettyErrorCode.UNEXPECTED_STATUS_CODE; + if (ex is HttpRequestException) + return PrettyErrorCode.HTTP_REQUEST_EXCEPTION; return PrettyErrorCode.UNKNOWN; } + private static PrettyErrorCode MapSocketError(SocketError error) + { + switch (error) + { + case SocketError.HostNotFound: + case SocketError.NoData: + case SocketError.TryAgain: + return PrettyErrorCode.HOST_NAME_NOT_RESOLVED; + case SocketError.ConnectionRefused: + case SocketError.NetworkUnreachable: + case SocketError.HostUnreachable: + case SocketError.NetworkDown: + return PrettyErrorCode.CANNOT_CONNECT; + case SocketError.ConnectionReset: + return PrettyErrorCode.CONNECTION_RESET; + case SocketError.ConnectionAborted: + return PrettyErrorCode.CONNECTION_ABORTED; + case SocketError.TimedOut: + return PrettyErrorCode.TIMEOUT; + default: + return PrettyErrorCode.UNKNOWN; + } + } + + // HttpRequestException.StatusCode exists only on .NET 5 and later. + private static int? GetHttpStatusCode(Exception exception) + { + if (exception is not HttpRequestException) + return null; + object? value = exception.GetType().GetProperty("StatusCode")?.GetValue(exception); + return value == null ? null : Convert.ToInt32(value); + } + private static PrettyErrorCode ConvertHresultToCode(int HResult, out uint? rawHResult) { uint hr = unchecked((uint)HResult); rawHResult = hr; + + // Win32 error codes arrive both as such (Win32Exception.NativeErrorCode) and wrapped + // into an HRESULT; the table below knows some codes in only one of these forms. + PrettyErrorCode code = ConvertExactHresultToCode(hr); + if (code != PrettyErrorCode.UNKNOWN || hr == 0) + return code; + if (hr < 0x10000) + return ConvertExactHresultToCode(0x80070000u | hr); + if ((hr & 0xFFFF0000u) == 0x80070000u) + return ConvertExactHresultToCode(hr & 0xFFFFu); + return code; + } + + private static PrettyErrorCode ConvertExactHresultToCode(uint hr) + { switch (hr) { case 0: @@ -167,11 +228,11 @@ private static PrettyErrorCode ConvertHresultToCode(int HResult, out uint? rawHR /// /// Get pretty error code string /// - /// Preffix (must be "SAMPLE_PREFFIX" format) + /// Prefix (must be "SAMPLE_PREFFIX" format) /// Exception /// Logger /// "SAMPLE_PREFFIX_ERROR_400 (0x0)" format string - public virtual string GetPrettyErrorCode(string preffix, Exception ex, ILogger? logger) + public virtual string GetPrettyErrorCode(string prefix, Exception ex, ILogger? logger) { string prettyErrorCode; @@ -183,11 +244,11 @@ public virtual string GetPrettyErrorCode(string preffix, Exception ex, ILogger? if (hr != null) { string hrHex = $"0x{hr.Value:X8}"; - prettyErrorCode = $"{preffix}_{code}{_statusCode} ({hrHex})"; + prettyErrorCode = $"{prefix}_{code}{_statusCode} ({hrHex})"; } else { - prettyErrorCode = $"{preffix}_{code}{_statusCode}"; + prettyErrorCode = $"{prefix}_{code}{_statusCode}"; } return prettyErrorCode; } @@ -195,11 +256,11 @@ public virtual string GetPrettyErrorCode(string preffix, Exception ex, ILogger? /// /// Get pretty error code string from windows hcode /// - /// Preffix (must be "SAMPLE_PREFFIX" format) + /// Prefix (must be "SAMPLE_PREFFIX" format) /// HCode /// Logger /// "SAMPLE_PREFFIX_ERROR_400 (0x0)" format string - public virtual string GetPrettyErrorCode(string preffix, int hcode, ILogger? logger) + public virtual string GetPrettyErrorCode(string prefix, int hcode, ILogger? logger) { string prettyErrorCode; @@ -211,11 +272,11 @@ public virtual string GetPrettyErrorCode(string preffix, int hcode, ILogger? log if (hr != null) { string hrHex = $"0x{hr.Value:X8}"; - prettyErrorCode = $"{preffix}_{code} ({hrHex})"; + prettyErrorCode = $"{prefix}_{code} ({hrHex})"; } else { - prettyErrorCode = $"{preffix}_{code}"; + prettyErrorCode = $"{prefix}_{code}"; } return prettyErrorCode; } @@ -234,7 +295,7 @@ public virtual ErrorModel GetErrorModel(string @object, Exception ex, ILogger? l logger?.CreateErrorLog(nameof(ErrorConvertor), $"{code} - {ex}"); - string hrHex = null; + string? hrHex = hr != null ? $"0x{hr.Value:X8}" : null; ErrorModel errorModel = new() { diff --git a/CDPIUI.Shared/PrettyErrorConvertionService/ErrorModel.cs b/CDPIUI.Shared/PrettyErrorConversionService/ErrorModel.cs similarity index 92% rename from CDPIUI.Shared/PrettyErrorConvertionService/ErrorModel.cs rename to CDPIUI.Shared/PrettyErrorConversionService/ErrorModel.cs index efd70759..05a62c1c 100644 --- a/CDPIUI.Shared/PrettyErrorConvertionService/ErrorModel.cs +++ b/CDPIUI.Shared/PrettyErrorConversionService/ErrorModel.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace CDPIUI.Shared.PrettyErrorConvertionService +namespace CDPIUI.Shared.PrettyErrorConversionService { public class ErrorModel { diff --git a/CDPIUI.Shared/PrettyErrorConvertionService/PrettyErrorCode.cs b/CDPIUI.Shared/PrettyErrorConversionService/PrettyErrorCode.cs similarity index 97% rename from CDPIUI.Shared/PrettyErrorConvertionService/PrettyErrorCode.cs rename to CDPIUI.Shared/PrettyErrorConversionService/PrettyErrorCode.cs index af725436..6d3e5a10 100644 --- a/CDPIUI.Shared/PrettyErrorConvertionService/PrettyErrorCode.cs +++ b/CDPIUI.Shared/PrettyErrorConversionService/PrettyErrorCode.cs @@ -1,4 +1,4 @@ -namespace CDPIUI.Shared.PrettyErrorConvertionService +namespace CDPIUI.Shared.PrettyErrorConversionService { public enum PrettyErrorCode { diff --git a/CDPIUI.Shared/README.md b/CDPIUI.Shared/README.md new file mode 100644 index 00000000..0d75d1cd --- /dev/null +++ b/CDPIUI.Shared/README.md @@ -0,0 +1,227 @@ +# CDPIUI.Shared + +`CDPIUI.Shared` is a `netstandard2.1` library (C# 12, nullable enabled). +It contains the code that the GUI, the core and the tray use together. + +These projects reference it: + +- `CDPIUI` (GUI) +- `CDPIUI.Core` +- `CDPIUI.AddOns` +- `CDPIUI.TrayIcon` +- `CDPIUI.Tests` + +Rules for all code in this library: + +- The code must compile for `netstandard2.1`. The `System/` folder adds the attributes that C# 11 `required` members need. +- The elevated tray uses this code. Treat each path and file that comes from the GUI as data from a less trusted process. +- Package references: `Microsoft.CSharp` and `System.Security.Cryptography.Pkcs`. + +Tests for this library are in `CDPIUI.Tests/Shared/`. +The pipe protocol is in [Pipe/README.md](Pipe/README.md). + +## Root files + +| File | Content | +|---|---| +| `SharedConstants.cs` | Store repository names, store item ids, update URLs, `DatabaseFileName`, `PipeName`, the URI schema `cdpiui`, `IsPreview` | +| `Utils.cs` (`SharedUtils`) | `GenerateNewId`, `IsOsSupportedNewGlyph`, `GetRandomHexNumber` | + +`GetRandomHexNumber` uses `RandomNumberGenerator`. Use it for secret values, not `System.Random`. + +## Basic + +| Type | Purpose | +|---|---| +| `XMLSettingsService` | Reads and writes `Settings.xml`. The GUI and the tray use it. | +| `CommandLine/GuiActivationArguments` | Encodes the GUI arguments as `--gui-args:`, so the tray can start the GUI with the same arguments. | +| `Filesystem/TrustedPathValidator` | Checks that a path is in an allowed root directory and that no part below the root is a reparse point. | +| `Filesystem/ApplicationPathMatcher` | Finds the processes and drivers of the application during uninstall. | +| `Filesystem/DirectoriesManager` | Returns the executable directory if it is writable. Otherwise, or if the caller sets `forceAppData` (MSI installation), it returns `%LOCALAPPDATA%\CDPIUI`. | +| `Filesystem/FileSystemService` | File extensions, temporary names, `WriteAllTextAtomic`, file and directory sizes. | + +### XMLSettingsService + +The file has a `Settings` root element, `Group` elements with a `Name` and `Setting` elements with `Key`, `Type` and `Value`. + +Load: + +- DTD processing is prohibited, because the elevated tray reads a file that the user can change. +- A missing file causes a new empty document, and the service saves it. +- An unreadable file causes a load from `Settings.xml.bak`. The service keeps a copy of the bad file as `Settings.xml.corrupt-`. +- If the backup is also unreadable, `HasUnrecoverableLoadError` is `true` and the service writes `Settings.xml.recovery-notice`. + +Read: + +- `GetValue` and `GetValueOrDefault` write the default value to the file when the key does not exist. +- A value of a different type returns the default value. It does not cause an exception. +- Default values: `false`, `"NaN"`, `0`, `10.0`, an empty list. Subclasses can override `GetDefaultValueForKey`. + +Write: + +- Each `SetValue` reads the file from the disk again, applies one change and saves the file. Thus a process with an old copy does not overwrite a change from the other process. +- A `Settings.xml.lock` file with `FileShare.None` locks the write. +- The service writes a temporary file and replaces `Settings.xml` with it. The previous file becomes `Settings.xml.bak`. +- Supported types: `int`, `double`, `bool`, `string`, `DateTime`, `List`. `double` and `DateTime` use the invariant culture. +- `List` items are separated by `;`. The service writes `;` as `%3B` and `%` as `%25`. +- `SetValue(group, ...)` raises `PropertyChanged`. `SetValue(groupPath, ...)` raises `EnumPropertyChanged`. +- `Reload` does not raise events. + +## ComponentsTask + +| Type | Purpose | +|---|---| +| `IProcessService` | The interface of a component process manager. The tray implements it in `ProcessManager`. | +| `TaskModel` | A task: `Id`, `ProcessManager`, `IsSetupComplete`. | +| `TasksManageService` | The task list. It adds, starts, stops and removes tasks and raises `TaskListUpdated`, `TaskStateUpdated`, `TaskSetupStateUpdated`. | +| `OutputCleanupHelper` | Removes escape sequences from pseudo console output. | + +Rules: + +- `TasksManageService` uses a `SemaphoreSlim` for the list. Use `GetTasksSnapshot` to read the list from more than one thread. +- `CreateAndRunNewTask` stops a task that exists before it starts it again. +- `OutputCleanupHelper` removes only sequences that start with ESC. Use `TakeIncompleteTail` to keep a sequence that is cut at the end of a chunk. + +## ConditionalLaunch + +`ConditionalTaskModels.cs` contains the task file format of the Conditional Launch feature. + +- `ConditionalTask` is XML (`.cdpitask`), version 3. Version 1 stored one `Trigger` element. `Normalize` moves it to `Triggers`. +- Trigger types: `HotKey`, `ProcessStarted`, `ProcessStopped`. +- Action types: see `ConditionalActionType`. +- `ConditionalTaskFileService.GetTasksDirectoryFromSettingsFile` returns `\ConditionalTasks`. The data directory is the parent of the `Settings` directory. +- `LoadDirectory` skips files that are not valid and files with a duplicate `Id`. The files are sorted by name. +- `Validate` requires a GUID `Id`, a name and a priority from -1 to 1. It also requires one or more triggers, one or more actions and a delay from 0 to 86400 seconds. +- `Save` and `Export` write a temporary file and then replace the target file. + +## Exceptions + +Custom exceptions that `ErrorConvertor` changes to a `PrettyErrorCode`. + +| Folder | Types | +|---|---| +| root | `ApplicationFilesDamagedException`, `AsyncOperationException`, `MsiInstallException`, `NewestVersionAlreadyInstalledException`, `UnknownException`, `UnknownFileFormatException` | +| `Catalog/` | `CatalogInvalid`, `CatalogNoSignature`, `CertificateNotTrusted` | +| `Database/` | `AddonNotInstalledException`, `ApplicationItemRegistrationException`, `DatabaseUserItemRegistrationException` | +| `Settings/` | `SettingNotFoundException`, `SettingTypeNotSupported` | +| `Interface/` | `ICustomException` | + +An exception that implements `ICustomException` gives its own `PrettyErrorCode`. + +## Extensions + +| Type | Purpose | +|---|---| +| `EnumExtension` | `ToEnum(string)` (case-insensitive, throws on an unknown value) and `ToEnum(string?, T defaultValue)` | +| `DictionaryExtension` | `GetKeyByValue`, `AddRange` (adds only new items) | +| `StringExtension` | `SerializeTuples`, `DeserializeTuples`, `FirstCharToUpper` | +| `LogicalComparer` | Sorts `INamedModel` items with `StrCmpLogicalW` | + +## Logger + +`LoggerBase` writes log files. Each process has its own subclass with an `Instance`: + +- The tray: `CDPIUI.TrayIcon/Helper/Basic/Logger.cs`. The root directory is the tray directory. +- The GUI and the core: `CDPIUI.Core/Basic/Logger.cs`. The root directory is `Directories.DataDirectory`. + +Files and rotation: + +- The file is `\Logs\.log`. The service replaces characters that are not valid in a file name with `_`. +- When a file is larger than 5 MiB, the service copies it to `.old.log` and clears it. +- The file is opened with `FileShare.ReadWrite | FileShare.Delete`, because the GUI and the tray can write to the same file. +- The first line of a new file is `LoggerBase is ready-to-work `. + +Levels (`LogSelevirity`): `DEBG`, `INFO`, `WARN`, `CRIT`. `CreateErrorLog` writes `CRIT`. +The logger writes only messages at the minimum level or higher. Each message also goes to `Debug.WriteLine`. + +WARNING: Do not write secrets (tokens, `AuthGuid`) to the log. + +## Migration + +`GoodbyeDpiMigrationActivation` reads and creates the `--gdpiui-migration=` argument. GDPIUI-Updater uses it to start a migration from GoodbyeDPI UI. + +- The value has six parts, separated by `.`: + 1. The version (`1`). + 2. The archive path (Base64 URL). + 3. The SHA-256 hash (lower case hex). + 4. The migration id (GUID, `N` format). + 5. The response pipe name (Base64 URL). + 6. The session token (32 to 64 bytes, Base64 URL). +- The archive must be a `.zip` file. The pipe name must start with `GDPIUI-Updater-`. +- The tray passes `RawArgument` to the GUI without a change. + +## Models + +| Type | Purpose | +|---|---| +| `OperationResultModel` | A result with an `ErrorModel` | +| `UnprocessedOperationResultModel` | A result with an `Exception` | +| `EmptyResult` | A result without a value | +| `IOperationResultModel`, `INamedModel` | Interfaces | + +Each result has `Success`, `Result`, `ErrorHappens` and `Error`. +Use `SuccessResult`, `UnSuccessResult` (failed, no error) and `FailureResult` (failed with an error). + +## Pipe + +See [Pipe/README.md](Pipe/README.md). + +## PrettyErrorConversionService + +`ErrorConvertor` changes an exception to a `PrettyErrorCode` and to a text for the user. + +- `MapExceptionToCode` examines the exception and each inner exception. It uses the exception type, socket errors, HTTP status codes and HRESULT values. +- Win32 codes and the related `0x8007xxxx` HRESULT give the same code. +- `GetPrettyErrorCode(prefix, ex, logger)` returns `_[_] (0x)`. The HRESULT part is present only when it is known. Example: `SAMPLE_PREFIX_IO_FILE_NOT_FOUND`. +- `GetErrorModel` returns an `ErrorModel` with `ErrorCode`, `Object`, `StatusCode`, `Exception` and `HResult`. +- Each method writes the error to the logger that you give it. + +The prefix must be in upper case with `_` between words, for example `SAMPLE_PREFIX`. +To add a code, add it to `PrettyErrorCode.cs` and to the mapping in `ErrorConvertor.cs`. +The tray uses a subclass, `CustomErrorConvertor`, that always writes to the tray log. + +## Secrets + +`Secrets/Secret.cs` contains the build secrets. Git ignores this file. +You must create it before you build the solution. + +1. For a local build, run `pwsh .github/scripts/create_secret_stub.ps1`. The script creates `Secret.cs` with empty tokens, an empty `TrustedCertificates` list and a random `AuthGuid`. In a file that exists, the script changes only an empty `AuthGuid`. +2. Alternatively, create a `public static class Secret` in the `CDPIUI.Shared.Secrets` namespace. Copy only the members of `SecretExample` in `SecretExample.cs`. + +The release workflow writes `Secret.cs` with `.github/scripts/create_release_secret.ps1` from repository secrets. See [.github/README.md](../.github/README.md). + +`SecretExample.cs` is excluded from the build in `CDPIUI.Shared.csproj`. The `ISecret` interface in it describes the members: + +| Member | Effect of an empty value | +|---|---| +| `GitHubToken`, `GitLabToken` | The Store is not available. | +| `ProxyURL`, `ProxyKey` | No Store reverse proxy. | +| `AuthGuid` | The tray authorizes no pipe client. The GUI cannot connect. | +| `TrustedCertificates` | Signed packages, signed patches and application updates cannot be verified. | + +`TrustedCertificates` contains the SHA-1 thumbprints of the certificates that can sign catalogs. + +CAUTION: Do not commit `Secret.cs`. Do not use the stub values in a release build. + +The GUI and the tray must come from the same build, because they must have the same `AuthGuid`. + +## Security + +`CatalogVerifier` verifies a signed `catalog.cat` and the files of its directory. + +1. The signature is decoded as CMS. Each signer certificate must be in the trusted thumbprint list. The certificates are self-signed, so the chain is not built. +2. Each signature must be valid (`CheckSignature(verifySignatureOnly: true)`). +3. `powershell.exe` from the system directory runs `Test-FileCatalog` with `-FilesToSkip catalog.cat`. The paths go through environment variables. +4. The result must be `Valid` in 2 minutes. + +The result is a `CatalogVerification` value: `Success`, `NoSignature`, `NotTrustedSignature`, `FilesDoNotMatch` or `Failure`. + +## System + +Polyfills for `netstandard2.1`: + +- `System/Runtime/CompilerServices/RequiredMemberAttribute.cs` +- `System/Runtime/CompilerServices/CompilerFeatureRequiredAttribute.cs` +- `System/Diagnostic/CodeAnalysis/SetsRequiredMembersAttribute.cs` + +Do not remove them. The `required` members in this library need them. diff --git a/CDPIUI.Shared/Security/CatalogVerifier.cs b/CDPIUI.Shared/Security/CatalogVerifier.cs new file mode 100644 index 00000000..59ce8de8 --- /dev/null +++ b/CDPIUI.Shared/Security/CatalogVerifier.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Security.Cryptography.Pkcs; +using System.Threading.Tasks; + +namespace CDPIUI.Shared.Security +{ + public enum CatalogVerification + { + Success, + NoSignature, + NotTrustedSignature, + FilesDoNotMatch, + Failure + } + + /// + /// Verifies signed file catalogs (catalog.cat) of packages and application updates + /// + public static class CatalogVerifier + { + private static readonly TimeSpan FileCheckTimeout = TimeSpan.FromMinutes(2); + + /// + /// Checks the catalog signature and that the files of the directory match the catalog + /// + /// Catalog file + /// Directory the catalog describes + /// SHA-1 thumbprints of the certificates allowed to sign catalogs + public static async Task VerifyAsync(string catalogFile, string directory, IEnumerable trustedThumbprints) + { + if (!File.Exists(catalogFile)) + return CatalogVerification.NoSignature; + + CatalogVerification signature = VerifySignature(File.ReadAllBytes(catalogFile), trustedThumbprints); + if (signature != CatalogVerification.Success) + return signature; + + return await VerifyFilesAsync(catalogFile, directory) ? CatalogVerification.Success : CatalogVerification.FilesDoNotMatch; + } + + /// + /// Checks that the catalog is signed and that every signature is valid and made by a trusted certificate. + /// The certificates are pinned and self-signed, so the chain is not built. + /// + public static CatalogVerification VerifySignature(byte[] catalog, IEnumerable trustedThumbprints) + { + var trusted = new HashSet( + trustedThumbprints.Select(NormalizeThumbprint), StringComparer.OrdinalIgnoreCase); + + var cms = new SignedCms(); + try + { + cms.Decode(catalog); + } + catch (CryptographicException) + { + return CatalogVerification.NoSignature; + } + + if (cms.SignerInfos.Count == 0) + return CatalogVerification.NoSignature; + + foreach (SignerInfo signer in cms.SignerInfos) + { + // Only the signer certificate is compared: a certificate merely embedded in the + // catalog proves nothing about who signed it. + if (signer.Certificate == null || !trusted.Contains(signer.Certificate.Thumbprint)) + return CatalogVerification.NotTrustedSignature; + + try + { + signer.CheckSignature(verifySignatureOnly: true); + } + catch (CryptographicException) + { + return CatalogVerification.NotTrustedSignature; + } + } + + return CatalogVerification.Success; + } + + private static string NormalizeThumbprint(string thumbprint) => + new string(thumbprint.Where(Uri.IsHexDigit).ToArray()); + + private static async Task VerifyFilesAsync(string catalogFile, string directory) + { + // A full path: the elevated tray would otherwise also look in its own, possibly writable, directory. + string powershell = Path.Combine(Environment.SystemDirectory, "WindowsPowerShell", "v1.0", "powershell.exe"); + var startInfo = new ProcessStartInfo(powershell) + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + // Paths go through the environment, so quotes in them cannot change the command. + startInfo.ArgumentList.Add("-NoProfile"); + startInfo.ArgumentList.Add("-NonInteractive"); + startInfo.ArgumentList.Add("-Command"); + startInfo.ArgumentList.Add( + "(Test-FileCatalog -CatalogFilePath $env:CDPIUI_CATALOG -Path $env:CDPIUI_CATALOG_DIRECTORY -FilesToSkip catalog.cat).ToString()"); + startInfo.Environment["CDPIUI_CATALOG"] = Path.GetFullPath(catalogFile); + startInfo.Environment["CDPIUI_CATALOG_DIRECTORY"] = Path.GetFullPath(directory); + + using var process = Process.Start(startInfo); + if (process == null) + return false; + + Task output = process.StandardOutput.ReadToEndAsync(); + Task errors = process.StandardError.ReadToEndAsync(); + Task exited = Task.Run(() => process.WaitForExit()); + + if (await Task.WhenAny(exited, Task.Delay(FileCheckTimeout)) != exited) + { + try { process.Kill(); } catch { } + return false; + } + + await Task.WhenAll(output, errors); + return string.Equals(output.Result.Trim(), "Valid", StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/CDPIUI.Shared/Utils.cs b/CDPIUI.Shared/Utils.cs index 8d28cb01..9aa18598 100644 --- a/CDPIUI.Shared/Utils.cs +++ b/CDPIUI.Shared/Utils.cs @@ -22,15 +22,15 @@ public static bool IsOsSupportedNewGlyph() return false; } - static Random random = new(); + // Used for secrets such as the TgWsProxy --secret: a shared System.Random is predictable + // and not thread safe. public static string GetRandomHexNumber(int digits) { - byte[] buffer = new byte[digits / 2]; - random.NextBytes(buffer); + byte[] buffer = new byte[(digits + 1) / 2]; + using (var generator = System.Security.Cryptography.RandomNumberGenerator.Create()) + generator.GetBytes(buffer); string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray()); - if (digits % 2 == 0) - return result; - return result + random.Next(16).ToString("X"); + return result.Substring(0, digits); } } } diff --git a/CDPIUI.Tests/AddOns/ConfigImportServiceTests.cs b/CDPIUI.Tests/AddOns/ConfigImportServiceTests.cs new file mode 100644 index 00000000..b3190b85 --- /dev/null +++ b/CDPIUI.Tests/AddOns/ConfigImportServiceTests.cs @@ -0,0 +1,18 @@ +using CDPIUI.AddOns.ConfigImport; + +namespace CDPIUI.Tests.AddOns; + +public class ConfigImportServiceTests +{ + [Theory] + [InlineData("set LIST=--hostlist=\"%~dp0lists\\list.txt\"", "LIST", "--hostlist=\"%~dp0lists\\list.txt\"")] + [InlineData("set \"BIN=%~dp0bin\\\"", "BIN", "%~dp0bin\\")] + [InlineData("set \"ARGS=--hostlist=\"a b.txt\"\"", "ARGS", "--hostlist=\"a b.txt\"")] + [InlineData("@SET PORT=443", "PORT", "443")] + public void TryParseSetKeepsQuotesInsideValue(string line, string expectedName, string expectedValue) + { + Assert.True(ConfigImportService.TryParseSet(line, out string name, out string value)); + Assert.Equal(expectedName, name); + Assert.Equal(expectedValue, value); + } +} diff --git a/CDPIUI.Tests/AddOns/ConfigShareWriteOptionsTests.cs b/CDPIUI.Tests/AddOns/ConfigShareWriteOptionsTests.cs new file mode 100644 index 00000000..e83f1fdf --- /dev/null +++ b/CDPIUI.Tests/AddOns/ConfigShareWriteOptionsTests.cs @@ -0,0 +1,28 @@ +using CDPIUI.AddOns.ConfigShare; + +namespace CDPIUI.Tests.AddOns; + +public class ConfigShareWriteOptionsTests +{ + private static readonly string Package = Path.Combine(Path.GetTempPath(), "cdpiui-share-test"); + + [Theory] + [InlineData("--debug=@C:\\Windows\\System32\\drivers\\etc\\hosts")] + [InlineData("--wf-save=C:\\Windows\\evil.sys")] + [InlineData("--hostlist-auto ..\\..\\autohostlist.txt")] + [InlineData("--hostlist-auto=autohostlist.txt")] + public void WritesOutsideThePackageAreRejected(string args) + { + var exception = Assert.Throws(() => + ConfigShareService.EnsureNoOutsideWrites($"--wf-tcp=443 {args} --dpi-desync=fake", Package)); + Assert.Equal("SHARE_UNSAFE_OPTION", exception.Code); + } + + [Fact] + public void WritesInsideThePackageAndPlainDebugAreAccepted() + { + string inside = Path.Combine(Package, "resources", "autohostlist.txt"); + + ConfigShareService.EnsureNoOutsideWrites($"--debug=1 --hostlist-auto=\"{inside}\" --dpi-desync=fake", Package); + } +} diff --git a/CDPIUI.Tests/AddOns/ZapretConfigImportServiceTests.cs b/CDPIUI.Tests/AddOns/ZapretConfigImportServiceTests.cs new file mode 100644 index 00000000..202694e2 --- /dev/null +++ b/CDPIUI.Tests/AddOns/ZapretConfigImportServiceTests.cs @@ -0,0 +1,38 @@ +using CDPIUI.AddOns.ConfigImport; + +namespace CDPIUI.Tests.AddOns; + +public class ZapretConfigImportServiceTests +{ + [Theory] + [InlineData("--a=1 --b=x^&y & echo done", "--a=1 --b=x&y ")] + [InlineData("--a=1 ^| --b=2 > log.txt", "--a=1 | --b=2 ")] + [InlineData("--hostlist=\"a^b & c.txt\" --c=^^", "--hostlist=\"a^b & c.txt\" --c=^")] + public void ShellTailIsCutAndCaretsAreRemoved(string input, string expected) + { + Assert.Equal(expected, ZapretConfigImportService.TrimShellTail(input)); + } + + [Theory] + [InlineData("dp")] + [InlineData("nx")] + [InlineData("f")] + [InlineData("n")] + [InlineData("x")] + public void FileModifiersMatchCmd(string modifiers) + { + string file = Path.Combine(Path.GetTempPath(), "zapret", "general.bat"); + string expected = modifiers switch + { + "dp" => Path.GetDirectoryName(file) + Path.DirectorySeparatorChar, + "nx" => "general.bat", + "f" => file, + "n" => "general", + _ => ".bat", + }; + + string actual = ZapretConfigImportService.ExpandFileModifiers(modifiers, file); + + Assert.Equal(expected.Replace('\\', '/'), actual.Replace('\\', '/')); + } +} diff --git a/CDPIUI.Tests/CDPIUI.Tests.csproj b/CDPIUI.Tests/CDPIUI.Tests.csproj new file mode 100644 index 00000000..ebaee70d --- /dev/null +++ b/CDPIUI.Tests/CDPIUI.Tests.csproj @@ -0,0 +1,31 @@ + + + + net8.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CDPIUI.Tests/Core/ArchivePathsTests.cs b/CDPIUI.Tests/Core/ArchivePathsTests.cs new file mode 100644 index 00000000..b983a381 --- /dev/null +++ b/CDPIUI.Tests/Core/ArchivePathsTests.cs @@ -0,0 +1,51 @@ +using CDPIUI.Core.System; + +namespace CDPIUI.Tests.Core; + +public class ArchivePathsTests +{ + private static readonly string Root = Path.Combine(Path.GetTempPath(), "cdpiui-archive-root"); + + [Theory] + [InlineData("file.txt")] + [InlineData("lists/hosts.txt")] + [InlineData("a/./b/c.bin")] + [InlineData("a/../b.txt")] + public void AcceptsPathsInsideRoot(string relative) + { + Assert.True(ArchivePaths.TryGetDestination(Root, relative, out string destination)); + Assert.StartsWith(Path.GetFullPath(Root), destination); + } + + [Theory] + [InlineData("../evil.txt")] + [InlineData("a/../../evil.txt")] + [InlineData("/etc/evil")] + [InlineData("C:/evil.txt")] + [InlineData("C:evil.txt")] + [InlineData("")] + public void RejectsPathsOutsideRoot(string relative) + { + Assert.False(ArchivePaths.TryGetDestination(Root, relative, out _)); + } + + [Fact] + public void RejectsSiblingDirectoryWithSamePrefix() + { + Assert.False(ArchivePaths.TryGetDestination(Root, "../cdpiui-archive-root2/x.txt", out _)); + } + + [Fact] + public void RootWithTrailingSeparatorBehavesTheSame() + { + Assert.True(ArchivePaths.TryGetDestination(Root + Path.DirectorySeparatorChar, "x.txt", out string a)); + Assert.True(ArchivePaths.TryGetDestination(Root, "x.txt", out string b)); + Assert.Equal(a, b); + } + + [Fact] + public void GetDestinationOrThrowThrowsInvalidData() + { + Assert.Throws(() => ArchivePaths.GetDestinationOrThrow(Root, "../x")); + } +} diff --git a/CDPIUI.Tests/Core/BatchFileEncodingTests.cs b/CDPIUI.Tests/Core/BatchFileEncodingTests.cs new file mode 100644 index 00000000..c78cbab9 --- /dev/null +++ b/CDPIUI.Tests/Core/BatchFileEncodingTests.cs @@ -0,0 +1,28 @@ +using System.Text; +using CDPIUI.Core.System; + +namespace CDPIUI.Tests.Core; + +public class BatchFileEncodingTests +{ + private const string Text = "rem Обход блокировок YouTube и Discord\r\nset BIN=%~dp0bin\\"; + + static BatchFileEncodingTests() => Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + [Theory] + [InlineData(866)] + [InlineData(1251)] + [InlineData(65001)] + public void CyrillicBatchFilesAreDecoded(int codePage) + { + byte[] bytes = Encoding.GetEncoding(codePage).GetBytes(Text); + Assert.Equal(Text, BatchFileEncoding.Decode(bytes)); + } + + [Fact] + public void Utf8BomIsRemoved() + { + byte[] bytes = [.. new UTF8Encoding(true).GetPreamble(), .. Encoding.UTF8.GetBytes(Text)]; + Assert.Equal(Text, BatchFileEncoding.Decode(bytes)); + } +} diff --git a/CDPIUI.Tests/Core/ConfigCommandLineTests.cs b/CDPIUI.Tests/Core/ConfigCommandLineTests.cs new file mode 100644 index 00000000..68674788 --- /dev/null +++ b/CDPIUI.Tests/Core/ConfigCommandLineTests.cs @@ -0,0 +1,38 @@ +using CDPIUI.Core.ComponentServices.Helpers.Configuration; + +namespace CDPIUI.Tests.Core; + +public class ConfigCommandLineTests +{ + [Fact] + public void TokenizeKeepsQuotedPathTogether() + { + var tokens = ConfigCommandLine.Tokenize("--wf-tcp=80 --hostlist=\"C:\\My Lists\\a.txt\" --new"); + + Assert.Equal(3, tokens.Count); + Assert.Equal("--wf-tcp=80", tokens[0]); + Assert.Equal("--new", tokens[2]); + } + + [Fact] + public void QuotedFolderEndingWithBackslashIsClosed() + { + var tokens = ConfigCommandLine.Tokenize("--lua-init=@\"C:\\dir\\\" --new --wf-udp=443"); + + Assert.Equal(["--lua-init=@\"C:\\dir\\\"", "--new", "--wf-udp=443"], tokens); + } + + [Fact] + public void TokenizeEmptyReturnsEmpty() + { + Assert.Empty(ConfigCommandLine.Tokenize(null)); + Assert.Empty(ConfigCommandLine.Tokenize(" ")); + } + + [Fact] + public void ApostropheDoesNotGroupArguments() + { + Assert.Equal(["--hostlist=O'Brien.txt", "--wf-tcp=443"], + CDPIUI.Core.ComponentServices.Helpers.Configuration.ConfigCommandLine.Tokenize("--hostlist=O'Brien.txt --wf-tcp=443")); + } +} diff --git a/CDPIUI.Tests/Core/ConfigFileReferencesTests.cs b/CDPIUI.Tests/Core/ConfigFileReferencesTests.cs new file mode 100644 index 00000000..89092cca --- /dev/null +++ b/CDPIUI.Tests/Core/ConfigFileReferencesTests.cs @@ -0,0 +1,39 @@ +using CDPIUI.Core.ComponentServices.Helpers.Configuration; + +namespace CDPIUI.Tests.Core; + +public class ConfigFileReferencesTests +{ + [Fact] + public void BlobOffsetIsNotPartOfThePath() + { + var files = ConfigFileReferences.ExtractFromText("--blob=tls:+4@files/tls.bin"); + + var file = Assert.Single(files); + Assert.Equal("files/tls.bin", file.Path); + } + + [Fact] + public void BlobReplacementKeepsNameAndOffset() + { + string result = ConfigFileReferences.ReplaceFileInText( + "--blob=tls:+4@files/tls.bin", "files/tls.bin", ConfigFileKind.Payload, "C:/new/tls.bin"); + + Assert.Equal("--blob=tls:+4@C:/new/tls.bin", result); + } + + [Fact] + public void LuaFileWithSemicolonInNameIsAFile() + { + var file = Assert.Single(ConfigFileReferences.ExtractFromText("--lua-init=@lua/a;b.lua")); + Assert.Equal("lua/a;b.lua", file.Path); + Assert.Empty(ConfigFileReferences.ExtractFromText("--lua-init=x=1;y=2")); + } + + [Fact] + public void NegativeNumberIsAValue() + { + Assert.False(ConfigCommandLine.IsOption("-10000")); + Assert.True(ConfigCommandLine.IsOption("-p")); + } +} diff --git a/CDPIUI.Tests/Core/LScriptCoreTests.cs b/CDPIUI.Tests/Core/LScriptCoreTests.cs new file mode 100644 index 00000000..e8872f33 --- /dev/null +++ b/CDPIUI.Tests/Core/LScriptCoreTests.cs @@ -0,0 +1,40 @@ +using CDPIUI.Core.LScript; + +namespace CDPIUI.Tests.Core; + +public class LScriptCoreTests +{ + [Theory] + [InlineData(true, "C:\\lists\\a.txt")] + [InlineData(false, "D:\\lists\\b.txt")] + public void ConditionWithDrivePathsPicksTheRightBranch(bool flag, string expected) + { + var jparams = new Dictionary { ["useA"] = flag }; + + Assert.Equal(expected, LScriptCore.LocalCondition("useA==true?C:\\lists\\a.txt:D:\\lists\\b.txt", jparams)); + } + + [Fact] + public void ArgumentKeepsNestedParentheses() + { + Assert.Equal( + "useA==true ? $GETCURRENTDIR()/a.txt $SEPARATOR b.txt", + LScriptCore.GetArgumentsFromScript("$LOCALCONDITION(useA==true ? $GETCURRENTDIR()/a.txt $SEPARATOR b.txt)")); + } + + [Fact] + public void ConditionWithoutParametersIsEmpty() + { + Assert.Equal(string.Empty, LScriptCore.LocalCondition("useA==true ? a $SEPARATOR b", null)); + } + + [Fact] + public void ConditionStringWithDrivePathIsSplitAtTheTernaryColon() + { + var result = LScriptCore.GetNameOnOffValuesFromConditionString("useA==true?C:\\a.txt:D:\\b.txt"); + + Assert.NotNull(result); + Assert.Equal("C:\\a.txt", result!.Item3); + Assert.Equal("D:\\b.txt", result.Item4); + } +} diff --git a/CDPIUI.Tests/Core/ProcessServiceTests.cs b/CDPIUI.Tests/Core/ProcessServiceTests.cs new file mode 100644 index 00000000..514c5f5a --- /dev/null +++ b/CDPIUI.Tests/Core/ProcessServiceTests.cs @@ -0,0 +1,17 @@ +using CDPIUI.Core.ComponentServices; + +namespace CDPIUI.Tests.Core; + +public class ProcessServiceTests +{ + [Theory] + [InlineData("-p 1080", "-p 1080 --quiet")] + [InlineData("-p 1080 --quiet", "-p 1080 --quiet")] + [InlineData("--quiet -p 1080", "-p 1080 --quiet")] + [InlineData("", "--quiet")] + [InlineData("--quieter", "--quieter --quiet")] + public void AppendQuietFlagKeepsSingleSeparatedFlag(string args, string expected) + { + Assert.Equal(expected, ProcessService.AppendQuietFlag(args)); + } +} diff --git a/CDPIUI.Tests/Core/ProxyHelperTests.cs b/CDPIUI.Tests/Core/ProxyHelperTests.cs new file mode 100644 index 00000000..a3b2fd56 --- /dev/null +++ b/CDPIUI.Tests/Core/ProxyHelperTests.cs @@ -0,0 +1,46 @@ +using CDPIUI.Core.Proxy; + +namespace CDPIUI.Tests.Core; + +public class ProxyHelperTests +{ + [Theory] + [InlineData("--port 1080 --mode=split", "--mode=split ")] + [InlineData("--ip=127.0.0.1 --port=1080 --auto=a=b", "--auto=a=b ")] + [InlineData("-addr 0.0.0.0 -f \"C:\\Program Files\\list.txt\"", "-f \"C:\\Program Files\\list.txt\" ")] + [InlineData(" --disorder 1 --host example.com ", "--disorder 1 ")] + [InlineData("-addr=127.0.0.1 -port=8080 -dns-addr 8.8.8.8", "-dns-addr 8.8.8.8 ")] + public void AddressAndPortArgumentsAreRemoved(string args, string expected) + { + Assert.Equal(expected, ProxyHelper.ReplaceIp(args)); + } + + [Theory] + [InlineData("127.0.0.1", true)] + [InlineData("127.0.0.2", true)] + [InlineData("::1", true)] + [InlineData("[::1]", true)] + [InlineData("localhost", true)] + [InlineData("0.0.0.0", false)] + [InlineData("192.168.1.10", false)] + [InlineData("::", false)] + [InlineData("", false)] + [InlineData(null, false)] + public void OnlyLoopbackAddressesAreLocal(string? ip, bool expected) + { + Assert.Equal(expected, ProxyHelper.IsLoopbackAddress(ip)); + } + + [Theory] + [InlineData("1080", true)] + [InlineData(" 65535 ", true)] + [InlineData("0", false)] + [InlineData("65536", false)] + [InlineData("-1", false)] + [InlineData("80;evil", false)] + [InlineData(null, false)] + public void PortMustBeANumberInRange(string? port, bool expected) + { + Assert.Equal(expected, ProxyHelper.IsValidPort(port)); + } +} diff --git a/CDPIUI.Tests/Core/ProxySettingsTests.cs b/CDPIUI.Tests/Core/ProxySettingsTests.cs new file mode 100644 index 00000000..2acd58f6 --- /dev/null +++ b/CDPIUI.Tests/Core/ProxySettingsTests.cs @@ -0,0 +1,29 @@ +using CDPIUI.Core.Proxy; + +namespace CDPIUI.Tests.Core; + +public class ProxySettingsTests +{ + [Theory] + [InlineData("socks=127.0.0.1:1080", "127.0.0.1", "1080")] + [InlineData("127.0.0.1:8080", "127.0.0.1", "8080")] + [InlineData("socks=[::1]:1080", "::1", "1080")] + [InlineData("http=proxy.local:3128;https=proxy.local:3129", "proxy.local", "3128")] + public void ParsesKnownFormats(string value, string host, string port) + { + Assert.True(WindowsNativeProxySettingsService.TryParseProxyServer(value, out string parsedHost, out string parsedPort)); + Assert.Equal(host, parsedHost); + Assert.Equal(port, parsedPort); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + [InlineData("proxy.local")] + [InlineData("[::1]")] + [InlineData("::1")] + public void RejectsValuesWithoutPort(string? value) + { + Assert.False(WindowsNativeProxySettingsService.TryParseProxyServer(value, out _, out _)); + } +} diff --git a/CDPIUI.Tests/Core/ReplaceVariablesTests.cs b/CDPIUI.Tests/Core/ReplaceVariablesTests.cs new file mode 100644 index 00000000..7de52718 --- /dev/null +++ b/CDPIUI.Tests/Core/ReplaceVariablesTests.cs @@ -0,0 +1,55 @@ +using CDPIUI.Core.ComponentServices.Configuration; + +namespace CDPIUI.Tests.Core; + +public class ReplaceVariablesTests +{ + [Fact] + public void NestedVariablesAndCaseAreResolved() + { + var vars = new Dictionary + { + ["BIN"] = "C:\\bin\\", + ["fake-tls"] = "%bin%tls.bin", + }; + + Assert.Equal("--dpi-desync-fake-tls=C:\\bin\\tls.bin", + ConfigurationService.ReplaceVariables("--dpi-desync-fake-tls=%FAKE-TLS%", vars)); + } + + [Fact] + public void UnknownVariablesStay() + { + Assert.Equal("%~dp0list.txt %X%", + ConfigurationService.ReplaceVariables("%~dp0list.txt %X%", new Dictionary { ["Y"] = "1" })); + } + + [Fact] + public void SelfGrowingVariableStopsAtLimit() + { + var vars = new Dictionary { ["A"] = "%A%%A%%A%%A%" }; + + string result = ConfigurationService.ReplaceVariables("%A%", vars); + + Assert.True(result.Length <= ConfigurationService.MaxExpandedLength); + } +} + +public class ReplaceFilesPathTests +{ + private static string Replace(string startup, Dictionary files) + { + var item = new CDPIUI.Core.ComponentServices.Helpers.Configuration.ConfigItem { startup_string = startup }; + return ConfigurationService.ReplaceFilesPath(item, files).startup_string; + } + + [Fact] + public void OnlyWholePathsAreReplaced() + { + var files = new Dictionary { ["lists\\list.txt"] = "C:\\Data\\My lists\\list.txt" }; + + Assert.Equal( + "--hostlist=\"C:\\Data\\My lists\\list.txt\" --hostlist=lists\\mylist.txt --x=\"\" --name='a'", + Replace("--hostlist=\"%~dp0lists/list.txt\" --hostlist=lists\\mylist.txt --x=\"\" --name='a'", files)); + } +} diff --git a/CDPIUI.Tests/Core/VersionHelperTests.cs b/CDPIUI.Tests/Core/VersionHelperTests.cs new file mode 100644 index 00000000..27b6351e --- /dev/null +++ b/CDPIUI.Tests/Core/VersionHelperTests.cs @@ -0,0 +1,48 @@ +using CDPIUI.Core.Store; + +namespace CDPIUI.Tests.Core; + +public class VersionHelperTests +{ + [Theory] + [InlineData("1.0.0", "1.0.1")] + [InlineData("1.9.0", "1.10.0")] + [InlineData("v1.2.0", "v1.3.0")] + [InlineData("1.0.0.1", "1.0.0.2")] + public void OlderVersionPrecedesNewer(string oldVersion, string newVersion) + { + Assert.True(VersionHelper.CompareVersionStrings(oldVersion, newVersion) < 0); + } + + [Fact] + public void EqualVersionsCompareAsZero() + { + Assert.Equal(0, VersionHelper.CompareVersionStrings("1.2.3", "1.2.3")); + Assert.Equal(0, VersionHelper.CompareVersionStrings("1.0", "1.0.0")); + Assert.Equal(0, VersionHelper.CompareVersionStrings("1.0.0.0", "1.0")); + } + + [Theory] + [InlineData("1.0.0-rc1", "1.0.0")] + [InlineData("1.0.0rc1", "1.0.0")] + [InlineData("1.0.0-rc1", "1.0.0-rc2")] + [InlineData("release-70.5", "release-70.6")] + [InlineData("zapret 69", "zapret 70")] + public void PrereleaseAndTaggedVersionsAreOrdered(string oldVersion, string newVersion) + { + Assert.True(VersionHelper.CompareVersionStrings(oldVersion, newVersion) < 0); + Assert.True(VersionHelper.CompareVersionStrings(newVersion, oldVersion) > 0); + } + + [Theory] + [InlineData("1.0.4.0", "1.0.0", "NaN", true)] + [InlineData("1.0.4.0", "1.0.0", null, true)] + [InlineData("1.0.4.0", "1.0.0", "1.0.3", false)] + [InlineData("1.0.4.0", "2.0", "NaN", false)] + [InlineData("1.0.4.0", null, "NaN", false)] + [InlineData("1.0.4.0", "1.0.0", "garbage", false)] + public void IsVersionInRangeHandlesMissingBounds(string version, string? min, string? max, bool expected) + { + Assert.Equal(expected, VersionHelper.IsVersionInRange(version, min, max)); + } +} diff --git a/CDPIUI.Tests/Core/Zapret1ToZapret2ConverterTests.cs b/CDPIUI.Tests/Core/Zapret1ToZapret2ConverterTests.cs new file mode 100644 index 00000000..6944970b --- /dev/null +++ b/CDPIUI.Tests/Core/Zapret1ToZapret2ConverterTests.cs @@ -0,0 +1,31 @@ +using CDPIUI.Core.ComponentServices.Helpers.Configuration.Converters; + +namespace CDPIUI.Tests.Core; + +public class Zapret1ToZapret2ConverterTests +{ + [Theory] + [InlineData("split2", "multisplit")] + [InlineData("disorder2", "multidisorder")] + [InlineData("split", "fakedsplit")] + [InlineData("disorder", "fakeddisorder")] + public void OldStrategyNamesAreConverted(string oldName, string newName) + { + var result = new Zapret1ToZapret2Converter().Convert( + $"--wf-tcp=443 --filter-tcp=443 --dpi-desync=fake,{oldName} --dpi-desync-split-pos=1"); + + Assert.True(result.IsSuccessful, string.Join("; ", result.Issues.Select(i => i.Message))); + Assert.Contains(newName, result.StartupString); + } + + [Fact] + public void WindowSizeIsDroppedWithWarning() + { + var result = new Zapret1ToZapret2Converter().Convert( + "--wf-tcp=443 --filter-tcp=443 --wssize=1:6 --dpi-desync=fake"); + + Assert.True(result.IsSuccessful, string.Join("; ", result.Issues.Select(i => i.Message))); + Assert.Contains(result.Issues, i => i.Code == "DROPPED_OPTION"); + Assert.DoesNotContain("wssize", result.StartupString); + } +} diff --git a/CDPIUI.Tests/Core/ZipServiceTests.cs b/CDPIUI.Tests/Core/ZipServiceTests.cs new file mode 100644 index 00000000..dcf0693b --- /dev/null +++ b/CDPIUI.Tests/Core/ZipServiceTests.cs @@ -0,0 +1,28 @@ +using System.IO.Compression; +using CDPIUI.Core.System; + +namespace CDPIUI.Tests.Core; + +public sealed class ZipServiceTests : IDisposable +{ + private readonly string directory = Path.Combine(Path.GetTempPath(), "cdpiui-zip-" + Guid.NewGuid().ToString("N")); + + public void Dispose() => Directory.Delete(directory, true); + + [Fact] + public async Task DomainListsAreMergedByWholeLines() + { + string target = Path.Combine(directory, "item"); + Directory.CreateDirectory(target); + File.WriteAllText(Path.Combine(target, "list.txt"), "reddit.com"); + + string archivePath = Path.Combine(directory, "pack.zip"); + using (var archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) + using (var writer = new StreamWriter(archive.CreateEntry("list.txt").Open())) + writer.Write("t.co\nreddit.com\nyoutube.com\n"); + + await ZipService.ExtractZip(archivePath, "/", target); + + Assert.Equal(["reddit.com", "t.co", "youtube.com"], File.ReadAllLines(Path.Combine(target, "list.txt"))); + } +} diff --git a/CDPIUI.Tests/README.md b/CDPIUI.Tests/README.md new file mode 100644 index 00000000..b1ec4531 --- /dev/null +++ b/CDPIUI.Tests/README.md @@ -0,0 +1,77 @@ +# CDPIUI.Tests + +`CDPIUI.Tests` contains the xUnit tests for `CDPIUI.Shared`, `CDPIUI.Core` and `CDPIUI.AddOns`. The target framework is `net8.0`. + +## Run the tests + +On Windows: + +``` +dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj +``` + +On Linux or macOS: + +``` +dotnet test CDPIUI.Tests/CDPIUI.Tests.csproj -p:EnableWindowsTargeting=true +``` + +Run the command from the repository root. The referenced projects use Windows packages. Thus a build on a different OS needs `EnableWindowsTargeting`. + +The tests do not need the tray, the GUI, administrator rights or a network connection. The tests that write files use directories in `Path.GetTempPath()`. + +## Covered areas + +| Folder | Test class | Subject | +| ------ | ---------- | ------- | +| `AddOns` | `ConfigImportServiceTests` | Parse of `set` commands in imported scripts | +| `AddOns` | `ConfigShareWriteOptionsTests` | Rejection of options that write outside a `.cdpiconfig` package | +| `AddOns` | `ZapretConfigImportServiceTests` | Batch file syntax: shell tail, carets, file modifiers | +| `Core` | `ArchivePathsTests` | Safe archive paths inside the target root | +| `Core` | `BatchFileEncodingTests` | Encoding of batch files (Cyrillic, UTF-8 BOM) | +| `Core` | `ConfigCommandLineTests` | Tokenization of preset command lines | +| `Core` | `ConfigFileReferencesTests` | File references in preset arguments (blob offsets, LUA files) | +| `Core` | `LScriptCoreTests` | LScript conditions, nested arguments and the parse of condition strings | +| `Core` | `ProcessServiceTests` | Quiet flag in process arguments | +| `Core` | `ProxyHelperTests`, `ProxySettingsTests` | Proxy address, port and format parse | +| `Core` | `ReplaceVariablesTests` | Variable replacement | +| `Core` | `VersionHelperTests` | Version comparison | +| `Core` | `Zapret1ToZapret2ConverterTests` | Conversion of Zapret Legacy arguments to Zapret2 | +| `Core` | `ZipServiceTests` | Merge of domain lists during ZIP extraction | +| `Shared` | `ApplicationPathMatcherTests` | Match of application file paths | +| `Shared` | `CatalogVerifierTests` | Signature check of update catalogs | +| `Shared` | `ConditionalTaskFileServiceTests` | Save and load of `.cdpitask` files | +| `Shared` | `ErrorConvertorTests` | Error code conversion | +| `Shared` | `GuiActivationArgumentsTests` | `--gui-args:` encode, decode and command-line quotes | +| `Shared` | `OutputCleanupHelperTests` | Removal of escape sequences from console output | +| `Shared` | `PipeMessageModelTests`, `PipePayloadCodecTests` | Pipe message round trip | +| `Shared` | `StreamStringTests` | Pipe stream strings | +| `Shared` | `TasksManageServiceTests` | Task creation and stop with concurrent calls | +| `Shared` | `TrustedPathValidatorTests` | Trusted path checks | +| `Shared` | `XmlSettingsServiceTests` | XML settings, values in different cultures | + +## Test data + +`TestData` contains the files for the tests. The project copies all files in `TestData` to the output directory (``). + +- `release-catalog.cat`: a catalog signed by the release certificate. `CatalogVerifierTests` uses it. + +To read a test file, use `Path.Combine(AppContext.BaseDirectory, "TestData", "")`. + +## Add a test + +1. Put the test class in the folder of the tested project: `AddOns`, `Core` or `Shared`. +2. Use the namespace `CDPIUI.Tests.`. +3. Mark each test with `[Fact]` or `[Theory]`. The project imports `Xunit` globally. +4. If the test writes files, use a new directory in `Path.GetTempPath()`. +5. Delete this directory in `Dispose`. +6. If the test needs a data file, put it in `TestData`. +7. Run the tests. + +Do not use the real application data directory or the installed settings in a test. + +## Not covered + +The tests do not cover the `CDPIUI` GUI project (WinUI 3). The test project does not reference it. Windows, pages, `CommandUriMapper`, `WindowOpenHelper`, the help parser and the localization need a manual check. + +The tests also do not cover `CDPIUI.TrayIcon`, `EditHostFile`, `Update` and `NewSetup`. diff --git a/CDPIUI.Tests/Shared/ApplicationPathMatcherTests.cs b/CDPIUI.Tests/Shared/ApplicationPathMatcherTests.cs new file mode 100644 index 00000000..67dae374 --- /dev/null +++ b/CDPIUI.Tests/Shared/ApplicationPathMatcherTests.cs @@ -0,0 +1,39 @@ +using CDPIUI.Shared.Basic.Filesystem; + +namespace CDPIUI.Tests.Shared; + +public class ApplicationPathMatcherTests +{ + private const string InstallDirectory = @"C:\Program Files\Storik4\CDPI UI"; + + [Theory] + [InlineData(@"C:\Program Files\Storik4\CDPI UI\CDPIUI_TrayIcon.exe")] + [InlineData(@"c:\program files\storik4\cdpi ui\Store\Items\CSZTBN012\bin\winws.exe")] + [InlineData(@"\??\C:\Users\user\AppData\Local\CDPIUI\Store\Items\CSZTBN012\bin\WinDivert64.sys")] + [InlineData("\"C:\\Users\\user\\AppData\\Local\\CDPIUI\\Store\\Items\\CSGIVS036\\x86_64\\goodbyedpi.exe\"")] + [InlineData(@"C:/Users/user/AppData/Local/CDPIUI/Store/Items/CSBIHA024/ciadpi.exe")] + public void ApplicationFilesMatch(string path) + { + Assert.True(ApplicationPathMatcher.BelongsToApplication(path, InstallDirectory)); + } + + [Theory] + [InlineData(@"C:\Program Files\Storik4\CDPI UI Other\winws.exe")] + [InlineData(@"C:\zapret\bin\winws.exe")] + [InlineData(@"\??\C:\zapret\bin\WinDivert64.sys")] + [InlineData(@"C:\Users\user\AppData\Local\CDPIUI2\winws.exe")] + [InlineData(@"\SystemRoot\System32\drivers\WinDivert.sys")] + [InlineData("")] + [InlineData(null)] + public void OtherFilesDoNotMatch(string? path) + { + Assert.False(ApplicationPathMatcher.BelongsToApplication(path, InstallDirectory)); + } + + [Fact] + public void InstallDirectoryWithTrailingSeparatorMatches() + { + Assert.True(ApplicationPathMatcher.BelongsToApplication( + @"C:\Program Files\Storik4\CDPI UI\CDPIUI.exe", InstallDirectory + "\\")); + } +} diff --git a/CDPIUI.Tests/Shared/CatalogVerifierTests.cs b/CDPIUI.Tests/Shared/CatalogVerifierTests.cs new file mode 100644 index 00000000..34638c00 --- /dev/null +++ b/CDPIUI.Tests/Shared/CatalogVerifierTests.cs @@ -0,0 +1,87 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.Pkcs; +using System.Security.Cryptography.X509Certificates; +using CDPIUI.Shared.Security; + +namespace CDPIUI.Tests.Shared; + +public class CatalogVerifierTests +{ + // Signer of the catalogs in the published application patches (CN=Storik4). + private const string ReleaseThumbprint = "03AB9EBCE1C2A66BE3974FD17AB90E0ECCDB2909"; + + private static byte[] ReleaseCatalog() => + File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "TestData", "release-catalog.cat")); + + [Fact] + public void ReleaseCatalogSignedByTrustedCertificatePasses() + { + Assert.Equal(CatalogVerification.Success, CatalogVerifier.VerifySignature(ReleaseCatalog(), [ReleaseThumbprint])); + } + + [Fact] + public void ThumbprintFormattingIsIgnored() + { + Assert.Equal(CatalogVerification.Success, + CatalogVerifier.VerifySignature(ReleaseCatalog(), ["03 ab 9e bc e1 c2 a6 6b e3 97 4f d1 7a b9 0e 0e cc db 29 09"])); + } + + [Fact] + public void ReleaseCatalogWithUnknownThumbprintFails() + { + Assert.Equal(CatalogVerification.NotTrustedSignature, + CatalogVerifier.VerifySignature(ReleaseCatalog(), ["0000000000000000000000000000000000000000"])); + } + + [Fact] + public void ModifiedCatalogContentFails() + { + var cms = new SignedCms(); + cms.Decode(ReleaseCatalog()); + byte[] content = (byte[])cms.ContentInfo.Content.Clone(); + + // Change one byte of the signed content and keep the original signature and certificate. + byte[] catalog = ReleaseCatalog(); + int index = IndexOf(catalog, content); + Assert.True(index > 0); + catalog[index + content.Length / 2] ^= 0xFF; + + Assert.Equal(CatalogVerification.NotTrustedSignature, CatalogVerifier.VerifySignature(catalog, [ReleaseThumbprint])); + } + + [Fact] + public void CatalogSignedByAnotherKeyWithEmbeddedTrustedCertificateFails() + { + var release = new SignedCms(); + release.Decode(ReleaseCatalog()); + X509Certificate2 trusted = release.Certificates[0]; + + using RSA key = RSA.Create(2048); + var request = new CertificateRequest("CN=Storik4", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + using X509Certificate2 attacker = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1)); + + var forged = new SignedCms(release.ContentInfo, false); + forged.ComputeSignature(new CmsSigner(attacker)); + forged.Certificates.Add(trusted); + + Assert.Equal(CatalogVerification.NotTrustedSignature, + CatalogVerifier.VerifySignature(forged.Encode(), [ReleaseThumbprint])); + } + + [Fact] + public void UnsignedDataFails() + { + Assert.Equal(CatalogVerification.NoSignature, CatalogVerifier.VerifySignature([1, 2, 3], [ReleaseThumbprint])); + } + + private static int IndexOf(byte[] haystack, byte[] needle) + { + for (int i = 0; i <= haystack.Length - needle.Length; i++) + { + int j = 0; + while (j < needle.Length && haystack[i + j] == needle[j]) j++; + if (j == needle.Length) return i; + } + return -1; + } +} diff --git a/CDPIUI.Tests/Shared/ConditionalTaskFileServiceTests.cs b/CDPIUI.Tests/Shared/ConditionalTaskFileServiceTests.cs new file mode 100644 index 00000000..28241838 --- /dev/null +++ b/CDPIUI.Tests/Shared/ConditionalTaskFileServiceTests.cs @@ -0,0 +1,58 @@ +using CDPIUI.Shared.ConditionalLaunch; + +namespace CDPIUI.Tests.Shared; + +public sealed class ConditionalTaskFileServiceTests : IDisposable +{ + private readonly string directory = Path.Combine(Path.GetTempPath(), "cdpiui-tasks-" + Guid.NewGuid().ToString("N")); + + public void Dispose() => Directory.Delete(directory, true); + + private static ConditionalTask CreateTask(string name) => new() + { + Name = name, + Triggers = [new ConditionalTrigger()], + Actions = [new ConditionalAction()], + }; + + [Fact] + public void SavedTaskLoadsBackWithoutTemporaryFiles() + { + ConditionalTask task = CreateTask("First"); + string path = ConditionalTaskFileService.Save(task, directory); + task.Name = "Renamed"; + ConditionalTaskFileService.Save(task, directory); + + ConditionalTask loaded = ConditionalTaskFileService.Load(path); + + Assert.Equal("Renamed", loaded.Name); + Assert.Single(Directory.GetFiles(directory)); + } + + [Fact] + public void CopiedTaskFileIsSkippedAndReported() + { + ConditionalTask task = CreateTask("Task"); + string path = ConditionalTaskFileService.Save(task, directory); + File.Copy(path, Path.Combine(directory, "copy" + ConditionalTaskFileService.FileExtension)); + var errors = new List(); + + IReadOnlyList tasks = ConditionalTaskFileService.LoadDirectory(directory, (file, _) => errors.Add(file)); + + Assert.Single(tasks); + Assert.Single(errors); + } + + [Fact] + public void BrokenTaskFileIsReported() + { + Directory.CreateDirectory(directory); + File.WriteAllText(Path.Combine(directory, "broken" + ConditionalTaskFileService.FileExtension), "(); + + IReadOnlyList tasks = ConditionalTaskFileService.LoadDirectory(directory, (file, _) => errors.Add(file)); + + Assert.Empty(tasks); + Assert.Single(errors); + } +} diff --git a/CDPIUI.Tests/Shared/ErrorConvertorTests.cs b/CDPIUI.Tests/Shared/ErrorConvertorTests.cs new file mode 100644 index 00000000..eec15a7e --- /dev/null +++ b/CDPIUI.Tests/Shared/ErrorConvertorTests.cs @@ -0,0 +1,76 @@ +using System.ComponentModel; +using System.Net; +using System.Net.Sockets; +using CDPIUI.Shared.PrettyErrorConversionService; + +namespace CDPIUI.Tests.Shared; + +public class ErrorConvertorTests +{ + private readonly ErrorConvertor convertor = new(); + + [Theory] + [InlineData(5, PrettyErrorCode.ACCESS_DENIED)] + [InlineData(2, PrettyErrorCode.IO_FILE_NOT_FOUND)] + [InlineData(32, PrettyErrorCode.IO_FILE_USED_FOR_ANOTHER_PROCESS)] + [InlineData(1223, PrettyErrorCode.OPERATION_CANCELLED_BY_USER)] + public void Win32ErrorsAreMapped(int error, PrettyErrorCode expected) + { + Assert.Equal(expected, convertor.MapExceptionToCode(new Win32Exception(error), out _)); + } + + [Fact] + public void Win32ErrorInsideAnotherExceptionIsMapped() + { + var exception = new InvalidOperationException("Start failed", new Win32Exception(5)); + Assert.Equal(PrettyErrorCode.ACCESS_DENIED, convertor.MapExceptionToCode(exception, out _)); + } + + [Theory] + [InlineData(SocketError.HostNotFound, PrettyErrorCode.HOST_NAME_NOT_RESOLVED)] + [InlineData(SocketError.ConnectionRefused, PrettyErrorCode.CANNOT_CONNECT)] + [InlineData(SocketError.ConnectionReset, PrettyErrorCode.CONNECTION_RESET)] + [InlineData(SocketError.TimedOut, PrettyErrorCode.TIMEOUT)] + public void NetworkErrorsAreMapped(SocketError error, PrettyErrorCode expected) + { + var exception = new HttpRequestException("Request failed", + new IOException("Transport failed", new SocketException((int)error))); + Assert.Equal(expected, convertor.MapExceptionToCode(exception, out _)); + } + + [Fact] + public void HttpStatusCodeIsKept() + { + var exception = new HttpRequestException("Not found", null, HttpStatusCode.NotFound); + + PrettyErrorCode code = convertor.MapExceptionToCode(exception, out _, out int? statusCode); + + Assert.Equal(PrettyErrorCode.UNEXPECTED_STATUS_CODE, code); + Assert.Equal(404, statusCode); + Assert.StartsWith("TEST_UNEXPECTED_STATUS_CODE_404", convertor.GetPrettyErrorCode("TEST", exception, null)); + } + + [Fact] + public void TimeoutInsideCancellationIsMapped() + { + var exception = new TaskCanceledException("Timed out", new TimeoutException()); + Assert.Equal(PrettyErrorCode.TIMEOUT, convertor.MapExceptionToCode(exception, out _)); + } + + [Fact] + public void ErrorModelKeepsHresult() + { + var exception = new IOException("Disk full", unchecked((int)0x80070070)); + + ErrorModel model = convertor.GetErrorModel("test", exception, null); + + Assert.Equal(nameof(PrettyErrorCode.IO_DISK_FULL), model.ErrorCode); + Assert.Equal("0x80070070", model.HResult); + } + + [Fact] + public void SuccessCodeIsUnchanged() + { + Assert.Equal("HOSTEDIT_SUCCESS (0x00000000)", convertor.GetPrettyErrorCode("HOSTEDIT", 0, null)); + } +} diff --git a/CDPIUI.Tests/Shared/GuiActivationArgumentsTests.cs b/CDPIUI.Tests/Shared/GuiActivationArgumentsTests.cs new file mode 100644 index 00000000..632560bf --- /dev/null +++ b/CDPIUI.Tests/Shared/GuiActivationArgumentsTests.cs @@ -0,0 +1,38 @@ +using CDPIUI.Shared.Basic.CommandLine; + +namespace CDPIUI.Tests.Shared; + +public class GuiActivationArgumentsTests +{ + [Fact] + public void ArgumentsSurviveEncoding() + { + string[] args = ["C:\\Users\\Иван\\Мои пресеты\\a b.cdpiconfig", "--direct:cdpiui://x?y=1&z=2", "\"quoted\""]; + + string? encoded = GuiActivationArguments.Encode(args); + + Assert.NotNull(encoded); + Assert.DoesNotContain(" ", encoded); + Assert.True(GuiActivationArguments.TryDecode(["--show-ui", encoded!], out var decoded)); + Assert.Equal(args, decoded); + } + + [Fact] + public void MissingOrDamagedValueIsIgnored() + { + Assert.False(GuiActivationArguments.TryDecode(["--show-ui"], out _)); + Assert.False(GuiActivationArguments.TryDecode(["--gui-args:%%%"], out _)); + Assert.Null(GuiActivationArguments.Encode([])); + } + + [Theory] + [InlineData("simple", "simple")] + [InlineData("with space", "\"with space\"")] + [InlineData("C:\\dir with space\\", "\"C:\\dir with space\\\\\"")] + [InlineData("say \"hi\"", "\"say \\\"hi\\\"\"")] + [InlineData("", "\"\"")] + public void ArgumentsAreQuotedForCommandLineToArgv(string argument, string expected) + { + Assert.Equal(expected, GuiActivationArguments.JoinCommandLine([argument])); + } +} diff --git a/CDPIUI.Tests/Shared/OutputCleanupHelperTests.cs b/CDPIUI.Tests/Shared/OutputCleanupHelperTests.cs new file mode 100644 index 00000000..eefc9f98 --- /dev/null +++ b/CDPIUI.Tests/Shared/OutputCleanupHelperTests.cs @@ -0,0 +1,32 @@ +using CDPIUI.Shared.ComponentsTask; + +namespace CDPIUI.Tests.Shared; + +public class OutputCleanupHelperTests +{ + [Fact] + public void PlainTextWithBracketsIsKept() + { + Assert.Equal("[HOSTLIST] loaded [K] 1C;", OutputCleanupHelper.ReplaceSymbols("[HOSTLIST] loaded [K] 1C;")); + } + + [Fact] + public void EscapeSequencesAreRemoved() + { + string raw = "\u001b]0;C:\\winws.exe\u0007\u001b[?25lready\u001b[K\u001b[4;1Hnext\u001b[3Cword\u001b[0m"; + Assert.Equal("ready\nnext word", OutputCleanupHelper.ReplaceSymbols(raw)); + } + + [Fact] + public void IncompleteTailIsHeldBack() + { + string chunk = "line\r\n\u001b[1"; + string tail = OutputCleanupHelper.TakeIncompleteTail(ref chunk); + Assert.Equal("line\r\n", chunk); + Assert.Equal("\u001b[1", tail); + + string next = tail + "2;1H[HOSTLIST]"; + Assert.Equal(string.Empty, OutputCleanupHelper.TakeIncompleteTail(ref next)); + Assert.Equal("\n[HOSTLIST]", OutputCleanupHelper.ReplaceSymbols(next)); + } +} diff --git a/CDPIUI.Tests/Shared/PipeMessageModelTests.cs b/CDPIUI.Tests/Shared/PipeMessageModelTests.cs new file mode 100644 index 00000000..bf931ed1 --- /dev/null +++ b/CDPIUI.Tests/Shared/PipeMessageModelTests.cs @@ -0,0 +1,41 @@ +using System.Collections.Specialized; +using CDPIUI.Shared.Pipe.Models; + +namespace CDPIUI.Tests.Shared; + +public class PipeMessageModelTests +{ + [Theory] + [InlineData("--dpi-desync-repeats=6 --dpi-desync-fooling=badseq --dup=+1:3-64")] + [InlineData("C:\\Tom & Jerry\\list #1.txt")] + [InlineData("100% = a+b")] + [InlineData("Список сайтов.txt")] + [InlineData("")] + public void ValuesSurviveRoundTrip(string value) + { + var model = new CONPTYMessageModel + { + MessageType = CONPTYMessageIds.StartProcessId, + MessageData = new NameValueCollection + { + { "componentId", "CSZTBN012" }, + { "args", value }, + }, + }; + + var parsed = PipeModelConvertor.ConvertBack(model.ToString()) as CONPTYMessageModel; + + Assert.NotNull(parsed); + Assert.Equal(CONPTYMessageIds.StartProcessId, parsed!.MessageType); + Assert.Equal("CSZTBN012", parsed.MessageData?["componentId"]); + Assert.Equal(value, parsed.MessageData?["args"]); + } + + [Fact] + public void MessageWithoutDataHasNoQuery() + { + var model = new CONPTYMessageModel { MessageType = CONPTYMessageIds.StartProcessId }; + + Assert.DoesNotContain("?", model.ToString()); + } +} diff --git a/CDPIUI.Tests/Shared/PipePayloadCodecTests.cs b/CDPIUI.Tests/Shared/PipePayloadCodecTests.cs new file mode 100644 index 00000000..6440313e --- /dev/null +++ b/CDPIUI.Tests/Shared/PipePayloadCodecTests.cs @@ -0,0 +1,16 @@ +using CDPIUI.Shared.Pipe; + +namespace CDPIUI.Tests.Shared; + +public class PipePayloadCodecTests +{ + [Theory] + [InlineData("")] + [InlineData("plain")] + [InlineData("C:\\Tom & Jerry\\list #1+2.txt")] + [InlineData("Юникод ✓")] + public void StringRoundTrip(string value) + { + Assert.Equal(value, PipePayloadCodec.DecodeString(PipePayloadCodec.Encode(value))); + } +} diff --git a/CDPIUI.Tests/Shared/StreamStringTests.cs b/CDPIUI.Tests/Shared/StreamStringTests.cs new file mode 100644 index 00000000..1cc5ebfd --- /dev/null +++ b/CDPIUI.Tests/Shared/StreamStringTests.cs @@ -0,0 +1,32 @@ +using CDPIUI.Shared.Pipe; + +namespace CDPIUI.Tests.Shared; + +public class StreamStringTests +{ + [Theory] + [InlineData(0)] + [InlineData(10)] + [InlineData(32767)] + [InlineData(32768)] + [InlineData(200_000)] + public async Task MessagesOfAnyLengthRoundTrip(int length) + { + string message = string.Concat(Enumerable.Range(0, length).Select(i => (char)('а' + i % 32))); + using var stream = new MemoryStream(); + + await new StreamString(stream).WriteStringAsync(message); + stream.Position = 0; + + Assert.Equal(message, await new StreamString(stream).ReadStringAsync()); + } + + [Fact] + public async Task ShortMessageKeepsTwoByteHeader() + { + using var stream = new MemoryStream(); + await new StreamString(stream).WriteStringAsync("ab"); + + Assert.Equal(new byte[] { 0, 4, (byte)'a', 0, (byte)'b', 0 }, stream.ToArray()); + } +} diff --git a/CDPIUI.Tests/Shared/TasksManageServiceTests.cs b/CDPIUI.Tests/Shared/TasksManageServiceTests.cs new file mode 100644 index 00000000..97543da0 --- /dev/null +++ b/CDPIUI.Tests/Shared/TasksManageServiceTests.cs @@ -0,0 +1,78 @@ +using CDPIUI.Shared.ComponentsTask; +using CDPIUI.Shared.PrettyErrorConversionService; + +namespace CDPIUI.Tests.Shared; + +public class TasksManageServiceTests +{ + private sealed class FakeProcess : IProcessService + { + public string Id { get; set; } = string.Empty; + public string ProcessName => Id; + public bool IsProcessRunning { get; private set; } + public bool IsErrorHappens => false; + public ErrorModel? LastError => null; + public int StartCount { get; private set; } + +#pragma warning disable CS0067 + public event Action>? ProcessStateChanged; + public event Action? ErrorHappens; + public event Action? OutputReceived; + public event Action? ProcessNameChanged; + public event Action? ShowErrorMessageWindow; +#pragma warning restore CS0067 + + public async Task StartProcess() + { + StartCount++; + await Task.Delay(5); + IsProcessRunning = true; + } + + public Task StartProcess(string stringParameter1, string? stringParameter2) => StartProcess(); + + public Task StopProcess(bool boolParameter = true) + { + IsProcessRunning = false; + return Task.CompletedTask; + } + + public Task RestartProcess() => StartProcess(); + } + + [Fact] + public async Task ConcurrentCreationKeepsSingleTaskPerId() + { + var service = new TasksManageService(); + + await Task.WhenAll(Enumerable.Range(0, 20).Select(_ => service.CreateAndRunNewTask("component"))); + + var tasks = await service.GetTasksSnapshot(); + Assert.Single(tasks); + Assert.True(tasks[0].ProcessManager.IsProcessRunning); + } + + [Fact] + public async Task StopAllTasksStopsEveryTask() + { + var service = new TasksManageService(); + await service.CreateAndRunNewTask("a"); + await service.CreateAndRunNewTask("b"); + + await service.StopAllTasks(); + + Assert.All(await service.GetTasksSnapshot(), task => Assert.False(task.ProcessManager.IsProcessRunning)); + } + + [Fact] + public async Task ExistingTaskIsReusedOnRestart() + { + var service = new TasksManageService(); + await service.CreateAndRunNewTask("a"); + await service.CreateAndRunNewTask("a"); + + var tasks = await service.GetTasksSnapshot(); + Assert.Single(tasks); + Assert.Equal(2, tasks[0].ProcessManager.StartCount); + } +} diff --git a/CDPIUI.Tests/Shared/TrustedPathValidatorTests.cs b/CDPIUI.Tests/Shared/TrustedPathValidatorTests.cs new file mode 100644 index 00000000..ca7538d8 --- /dev/null +++ b/CDPIUI.Tests/Shared/TrustedPathValidatorTests.cs @@ -0,0 +1,81 @@ +using CDPIUI.Shared.Basic.Filesystem; + +namespace CDPIUI.Tests.Shared; + +public sealed class TrustedPathValidatorTests : IDisposable +{ + private readonly string root = Path.Combine(Path.GetTempPath(), "cdpiui-trusted-" + Guid.NewGuid().ToString("N")); + private readonly string outside = Path.Combine(Path.GetTempPath(), "cdpiui-outside-" + Guid.NewGuid().ToString("N")); + + public TrustedPathValidatorTests() + { + Directory.CreateDirectory(Path.Combine(root, "Store", "Items", "CSZTBN012")); + File.WriteAllText(Path.Combine(root, "Store", "Items", "CSZTBN012", "winws.exe"), ""); + Directory.CreateDirectory(outside); + File.WriteAllText(Path.Combine(outside, "tool.exe"), ""); + } + + public void Dispose() + { + Directory.Delete(root, true); + Directory.Delete(outside, true); + } + + [Fact] + public void FileInsideRootIsAllowed() + { + Assert.True(TrustedPathValidator.IsInside(Path.Combine(root, "Store", "Items", "CSZTBN012", "winws.exe"), [root])); + } + + [Fact] + public void MissingFileInsideRootIsAllowed() + { + Assert.True(TrustedPathValidator.IsInside(Path.Combine(root, "Store", "Items", "new", "patch.msi"), [root])); + } + + [Fact] + public void FileOutsideRootsIsRejected() + { + Assert.False(TrustedPathValidator.IsInside(Path.Combine(outside, "tool.exe"), [root, null, ""])); + } + + [Fact] + public void ParentTraversalIsRejected() + { + string path = Path.Combine(root, "Store", "..", "..", Path.GetFileName(outside), "tool.exe"); + Assert.False(TrustedPathValidator.IsInside(path, [root])); + } + + [Fact] + public void SiblingWithSamePrefixIsRejected() + { + Assert.False(TrustedPathValidator.IsInside(root + "-other" + Path.DirectorySeparatorChar + "tool.exe", [root])); + } + + [Fact] + public void LinkedDirectoryInsideRootIsRejected() + { + string link = Path.Combine(root, "Store", "link"); + Directory.CreateSymbolicLink(link, outside); + + Assert.False(TrustedPathValidator.IsInside(Path.Combine(link, "tool.exe"), [root])); + } + + [Fact] + public void LinkedFileInsideRootIsRejected() + { + string link = Path.Combine(root, "Store", "tool.exe"); + File.CreateSymbolicLink(link, Path.Combine(outside, "tool.exe")); + + Assert.False(TrustedPathValidator.IsInside(link, [root])); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void EmptyPathIsRejected(string? path) + { + Assert.False(TrustedPathValidator.IsInside(path, [root])); + } +} diff --git a/CDPIUI.Tests/Shared/XmlSettingsServiceTests.cs b/CDPIUI.Tests/Shared/XmlSettingsServiceTests.cs new file mode 100644 index 00000000..566422ab --- /dev/null +++ b/CDPIUI.Tests/Shared/XmlSettingsServiceTests.cs @@ -0,0 +1,122 @@ +using System.Globalization; +using CDPIUI.Shared.Basic; + +namespace CDPIUI.Tests.Shared; + +public class XmlSettingsServiceTests : IDisposable +{ + private readonly string _directory = Path.Combine(Path.GetTempPath(), "cdpiui-settings-" + Guid.NewGuid().ToString("N")); + private readonly CultureInfo _culture = CultureInfo.CurrentCulture; + + private string SettingsPath => Path.Combine(_directory, "Settings.xml"); + + public void Dispose() + { + CultureInfo.CurrentCulture = _culture; + if (Directory.Exists(_directory)) + Directory.Delete(_directory, true); + } + + [Fact] + public void DoubleRoundTripsAcrossCultures() + { + CultureInfo.CurrentCulture = new CultureInfo("ru-RU"); + new XMLSettingsService(SettingsPath).SetValue("PSEUDOCONSOLE", "fontSize", 12.5); + + CultureInfo.CurrentCulture = new CultureInfo("en-US"); + Assert.Equal(12.5, new XMLSettingsService(SettingsPath).GetValue("PSEUDOCONSOLE", "fontSize")); + } + + [Fact] + public void DateTimeRoundTripsAcrossCultures() + { + var value = new DateTime(2026, 5, 10, 13, 45, 0, DateTimeKind.Utc); + + CultureInfo.CurrentCulture = new CultureInfo("ru-RU"); + new XMLSettingsService(SettingsPath).SetValue("STORE", "lastSyncTime", value); + + CultureInfo.CurrentCulture = new CultureInfo("en-US"); + Assert.Equal(value, new XMLSettingsService(SettingsPath).GetValue("STORE", "lastSyncTime")); + } + + [Fact] + public void ReadsDoubleWrittenInCurrentCultureByOlderVersions() + { + CultureInfo.CurrentCulture = new CultureInfo("ru-RU"); + Assert.True(XMLSettingsService.TryParseDouble("12,5", out double value)); + Assert.Equal(12.5, value); + } + + [Fact] + public void ValueOfAnotherTypeReturnsDefault() + { + new XMLSettingsService(SettingsPath).SetValue("SYSTEM", "someNumber", "not a number"); + + var settings = new XMLSettingsService(SettingsPath); + Assert.Equal(0, settings.GetValue("SYSTEM", "someNumber")); + Assert.Equal(0, settings.GetValue(["SYSTEM"], "someNumber")); + } + + [Fact] + public void DateTimeWithGroupPathIsReadable() + { + var value = new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc); + var settings = new XMLSettingsService(SettingsPath); + settings.SetValue(["ADDONS", "ASGKOI001"], "lastRun", value); + + Assert.Equal(value, new XMLSettingsService(SettingsPath).GetValue(["ADDONS", "ASGKOI001"], "lastRun")); + } + + [Fact] + public async Task ConcurrentReadsAndWritesDoNotThrow() + { + var settings = new XMLSettingsService(SettingsPath); + + await Task.WhenAll(Enumerable.Range(0, 8).Select(worker => Task.Run(() => + { + for (int i = 0; i < 50; i++) + { + settings.SetValue("GROUP" + worker, "key" + i, i); + settings.GetValue("GROUP" + ((worker + 1) % 8), "key" + i); + } + }))); + + Assert.Equal(49, new XMLSettingsService(SettingsPath).GetValue("GROUP3", "key49")); + } + + [Fact] + public void ProcessWithOlderCopyDoesNotOverwriteOtherChanges() + { + var ui = new XMLSettingsService(SettingsPath); + var tray = new XMLSettingsService(SettingsPath); + + ui.SetValue("APPEARANCE", "Theme", "Dark"); + tray.SetValue("TRAY", "iconGUID", "guid"); + ui.SetValue(["CONFIGS", "CSZTBN012"], "configFile", "preset.json"); + + var reloaded = new XMLSettingsService(SettingsPath); + Assert.Equal("Dark", reloaded.GetValue("APPEARANCE", "Theme")); + Assert.Equal("guid", reloaded.GetValue("TRAY", "iconGUID")); + Assert.Equal("preset.json", reloaded.GetValue(["CONFIGS", "CSZTBN012"], "configFile")); + Assert.False(File.Exists(SettingsPath + ".lock")); + } + + [Fact] + public void ListItemsWithSeparatorsRoundTrip() + { + List items = [@"C:\Lists\a;b.txt", "100%.txt", "%3B literal", ""]; + new XMLSettingsService(SettingsPath).SetValue>("USERCHOISES", "siteLists", items); + + Assert.Equal(items, new XMLSettingsService(SettingsPath).GetValue>("USERCHOISES", "siteLists")); + } + + [Fact] + public void ListsWrittenByOlderVersionsAreRead() + { + Directory.CreateDirectory(_directory); + File.WriteAllText(SettingsPath, + ""); + + Assert.Equal([@"C:\x\100%\a.txt", "b.txt"], new XMLSettingsService(SettingsPath).GetValue>("USERCHOISES", "siteLists")); + } +} diff --git a/CDPIUI.Tests/TestData/release-catalog.cat b/CDPIUI.Tests/TestData/release-catalog.cat new file mode 100644 index 00000000..0823d2c0 Binary files /dev/null and b/CDPIUI.Tests/TestData/release-catalog.cat differ diff --git a/CDPIUI.TrayIcon/ConditionalLaunch/ConditionalLaunchEngine.cs b/CDPIUI.TrayIcon/ConditionalLaunch/ConditionalLaunchEngine.cs index f761cedd..e640a9b4 100644 --- a/CDPIUI.TrayIcon/ConditionalLaunch/ConditionalLaunchEngine.cs +++ b/CDPIUI.TrayIcon/ConditionalLaunch/ConditionalLaunchEngine.cs @@ -30,7 +30,8 @@ internal sealed class ConditionalLaunchEngine : IDisposable private System.Threading.Timer? _processTimer; private System.Threading.Timer? _reloadTimer; private int _isPolling; - private int _nextHotKeyId = 0x4300; + private const int FirstHotKeyId = 0x4300; + private int _nextHotKeyId = FirstHotKeyId; private bool _disposed; public static ConditionalLaunchEngine Instance => LazyInstance.Value; @@ -86,7 +87,10 @@ public void Reload(string? tasksDirectory = null) string? directory; lock (_sync) { - if (!string.IsNullOrWhiteSpace(tasksDirectory)) + // The directory comes from the UI; the elevated tray only creates and watches one in the + // application directories. + if (!string.IsNullOrWhiteSpace(tasksDirectory) && + TrustedPaths.IsAllowed(tasksDirectory, "conditional tasks directory")) ConfigureTasksDirectory(tasksDirectory); directory = _tasksDirectory; } @@ -97,7 +101,10 @@ public void Reload(string? tasksDirectory = null) IReadOnlyList loadedTasks; try { - loadedTasks = ConditionalTaskFileService.LoadDirectory(directory); + loadedTasks = ConditionalTaskFileService.LoadDirectory(directory, (path, error) => + Logger.Instance.CreateWarningLog( + nameof(ConditionalLaunchEngine), + $"Skipped conditional task file \"{path}\": {error.Message}")); } catch (Exception ex) { @@ -110,9 +117,21 @@ public void Reload(string? tasksDirectory = null) lock (_sync) { UnregisterHotKeys(); - foreach (var state in _processStates.Values) - state.CancelPending(); - _processStates.Clear(); + + // Any write to the folder reloads all tasks: only the states of tasks that were + // removed or changed are reset, so a pending delayed action of another task stays. + var oldSignatures = _loadedTasks.ToDictionary(task => task.Id, GetSignature, StringComparer.OrdinalIgnoreCase); + var unchangedIds = loadedTasks + .Where(task => oldSignatures.TryGetValue(task.Id, out var signature) && signature == GetSignature(task)) + .Select(task => task.Id) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + foreach (var key in _processStates.Keys.ToList()) + { + if (unchangedIds.Contains(key.Split('|')[0])) + continue; + _processStates[key].CancelPending(); + _processStates.Remove(key); + } _loadedTasks = loadedTasks.ToList(); _tasks = _loadedTasks.Where(task => task.IsEnabled).ToList(); @@ -187,6 +206,11 @@ public bool RunTask(string? taskId) private void RegisterHotKeys() { + // Hot keys need the message window of Start; registered before it, they had no + // window to send WM_HOTKEY to. + if (_windowHandle == IntPtr.Zero) + return; + var groups = _tasks .SelectMany(task => task.Triggers .Where(trigger => trigger.Type == ConditionalTriggerType.HotKey) @@ -228,6 +252,8 @@ private void UnregisterHotKeys() foreach (var id in _hotKeyTasks.Keys) UnregisterHotKey(_windowHandle, id); _hotKeyTasks.Clear(); + // Ids above 0xBFFF are rejected; every reload used new ones. + _nextHotKeyId = FirstHotKeyId; } private void PollProcesses() @@ -416,10 +442,11 @@ private async Task RunTaskOnceAsync(ConditionalTask task) } } - private void ConfigureTasksDirectory(string directory) + private void ConfigureTasksDirectory(string directory, bool recreateWatcher = false) { directory = Path.GetFullPath(directory); - if (string.Equals(_tasksDirectory, directory, StringComparison.OrdinalIgnoreCase) && + if (!recreateWatcher && + string.Equals(_tasksDirectory, directory, StringComparison.OrdinalIgnoreCase) && _watcher != null) { return; @@ -432,6 +459,7 @@ private void ConfigureTasksDirectory(string directory) _watcher.Created -= TaskFilesChanged; _watcher.Deleted -= TaskFilesChanged; _watcher.Renamed -= TaskFilesChanged; + _watcher.Error -= TaskWatcherError; _watcher.Dispose(); } @@ -453,8 +481,28 @@ private void ConfigureTasksDirectory(string directory) _watcher.Created += TaskFilesChanged; _watcher.Deleted += TaskFilesChanged; _watcher.Renamed += TaskFilesChanged; + _watcher.Error += TaskWatcherError; + } + + // After a buffer overflow the watcher stops raising events for good: it is created + // again and the tasks are reloaded, so later changes are not missed. + private void TaskWatcherError(object sender, ErrorEventArgs e) + { + Logger.Instance.CreateWarningLog(nameof(ConditionalLaunchEngine), $"Task folder watcher failed: {e.GetException()?.Message}"); + lock (_sync) + { + if (!string.IsNullOrWhiteSpace(_tasksDirectory)) + { + try { ConfigureTasksDirectory(_tasksDirectory, recreateWatcher: true); } + catch (Exception ex) { Logger.Instance.CreateErrorLog(nameof(ConditionalLaunchEngine), $"Cannot watch the task folder: {ex.Message}"); } + } + } + TaskFilesChanged(sender, new FileSystemEventArgs(WatcherChangeTypes.Changed, _tasksDirectory ?? string.Empty, null)); } + private static string GetSignature(ConditionalTask task) => + System.Text.Json.JsonSerializer.Serialize(task); + private void TaskFilesChanged(object sender, FileSystemEventArgs e) { lock (_sync) @@ -493,9 +541,13 @@ private static bool TryGetHotKey( return key != Keys.None; } + // By value: "Control, Alt" and "Alt, Control" are the same hot key and were registered + // twice, the second time failing. private static string GetHotKeyIdentity(ConditionalTrigger trigger) { - return $"{trigger.GetParameter("modifiers")}|{trigger.GetParameter("key")}"; + return TryGetHotKey(trigger, out var modifiers, out var virtualKey) + ? $"{modifiers}|{virtualKey}" + : $"{trigger.GetParameter("modifiers")}|{trigger.GetParameter("key")}"; } private static bool IsProcessTrigger(ConditionalTriggerType type) diff --git a/CDPIUI.TrayIcon/Controls/ComponentUserControl.cs b/CDPIUI.TrayIcon/Controls/ComponentUserControl.cs index 21598704..cb15da50 100644 --- a/CDPIUI.TrayIcon/Controls/ComponentUserControl.cs +++ b/CDPIUI.TrayIcon/Controls/ComponentUserControl.cs @@ -38,13 +38,13 @@ public string ComponentId set => _componentId = value; } - private bool _isRunned = false; - public bool IsRunned + private bool _isRunning = false; + public bool IsRunning { - get { return _isRunned; } + get { return _isRunning; } set { - _isRunned = value; + _isRunning = value; StateChangedActions(); } } @@ -110,8 +110,8 @@ private void InitToolTips() private void StateChangedActions() { - ComponentWorkButton.IsHighlighted = IsRunned; - ComponentWorkButton.Glyph = IsRunned ? "\uF8AE" : "\uF5B0"; + ComponentWorkButton.IsHighlighted = IsRunning; + ComponentWorkButton.Glyph = IsRunning ? "\uF8AE" : "\uF5B0"; } private void SetupStateChanged() @@ -144,7 +144,7 @@ private async void ComponentWorkButton_Clicked(object? sender, EventArgs e) { EventHappens?.Invoke(); - if (!await TasksHelper.Instance.IsTaskRunned(ComponentId)) + if (!await TasksHelper.Instance.IsTaskRunning(ComponentId)) { _ = TasksHelper.Instance.CreateAndRunNewTask(ComponentId); } diff --git a/CDPIUI.TrayIcon/Controls/IconButtonUserControl.cs b/CDPIUI.TrayIcon/Controls/IconButtonUserControl.cs index 71ba85fc..011f7aae 100644 --- a/CDPIUI.TrayIcon/Controls/IconButtonUserControl.cs +++ b/CDPIUI.TrayIcon/Controls/IconButtonUserControl.cs @@ -145,6 +145,9 @@ private void SetFont() private void IconButtonUserControl_MouseClick(object? sender, MouseEventArgs e) { + // Enabled only changed the colors: a disabled button, such as start for a component + // without a preset, still started it. + if (!Enabled) return; Clicked?.Invoke(this, e); } diff --git a/CDPIUI.TrayIcon/Forms/EmptyForm.cs b/CDPIUI.TrayIcon/Forms/EmptyForm.cs index 4f341d3a..b0e90df5 100644 --- a/CDPIUI.TrayIcon/Forms/EmptyForm.cs +++ b/CDPIUI.TrayIcon/Forms/EmptyForm.cs @@ -32,7 +32,7 @@ public EmptyForm() InitializeComponent(); HideWindow(); - _iconRetryTimer.Tick += (_, _) => AddIcon(iconName: GetCurrentIcon(), toolTip: GetNowRunnedComponentsString()); + _iconRetryTimer.Tick += (_, _) => AddIcon(iconName: GetCurrentIcon(), toolTip: GetNowRunningComponentsString()); Application.ApplicationExit += Application_ApplicationExit; this.Disposed += EmptyForm_Disposed; @@ -43,7 +43,7 @@ public EmptyForm() private static Guid GetGuid() { string savedGUID = SettingsManager.Instance.GetValue("TRAY", "iconGUID"); - if (savedGUID == "NaN") + if (savedGUID == "NaN" || !Guid.TryParse(savedGUID, out Guid parsedGuid)) { Guid guid = Guid.NewGuid(); SettingsManager.Instance.SetValue("TRAY", "iconGUID", guid.ToString()); @@ -51,22 +51,42 @@ private static Guid GetGuid() } else { - return new(savedGUID); + return parsedGuid; } } private void Application_ApplicationExit(object? sender, EventArgs e) { Application.ApplicationExit -= Application_ApplicationExit; + StopComponentsBeforeExit(); this.Close(); this.Dispose(); } + private void OnSessionEnding(object? sender, SessionEndingEventArgs e) + { + StopComponentsBeforeExit(); + } + + private static void StopComponentsBeforeExit() + { + try + { + // Runs off the UI thread so continuations inside StopAllTasks cannot wait for the blocked message loop. + Task.Run(() => TasksHelper.Instance.StopAllTasks()).Wait(TimeSpan.FromSeconds(5)); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(EmptyForm), $"Cannot stop components before exit: {ex.Message}"); + } + } + private void ConnectHandlers() { TasksHelper.Instance.TaskStateUpdated += HandleTaskStateUpdate; TasksHelper.Instance.TaskListUpdated += HandleTasksListUpdate; SystemEvents.PowerModeChanged += OnPowerChange; + SystemEvents.SessionEnding += OnSessionEnding; } private void DisconnectHandlers() @@ -74,6 +94,7 @@ private void DisconnectHandlers() TasksHelper.Instance.TaskStateUpdated -= HandleTaskStateUpdate; TasksHelper.Instance.TaskListUpdated -= HandleTasksListUpdate; SystemEvents.PowerModeChanged -= OnPowerChange; + SystemEvents.SessionEnding -= OnSessionEnding; } private async void OnPowerChange(object s, PowerModeChangedEventArgs e) @@ -81,12 +102,22 @@ private async void OnPowerChange(object s, PowerModeChangedEventArgs e) switch (e.Mode) { case PowerModes.Resume: - foreach (var task in TasksHelper.Instance.Tasks) + // Network adapters come back some seconds after resume; components started + // before that bind to no interface and the connection stays broken (issue #7). + await WaitForNetworkAsync(TimeSpan.FromSeconds(30)); + foreach (var task in await TasksHelper.Instance.GetTasksSnapshot()) { - if (task.ProcessManager.IsProcessRunning) + if (!task.ProcessManager.IsProcessRunning) + continue; + + try { await TasksHelper.Instance.RestartTask(task.Id); } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(EmptyForm), $"Cannot restart {task.Id} after resume: {ex.Message}"); + } } break; case PowerModes.Suspend: @@ -94,6 +125,21 @@ private async void OnPowerChange(object s, PowerModeChangedEventArgs e) } } + private static async Task WaitForNetworkAsync(TimeSpan timeout) + { + var deadline = DateTime.UtcNow + timeout; + while (DateTime.UtcNow < deadline) + { + try + { + if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) + return; + } + catch (System.Net.NetworkInformation.NetworkInformationException) { return; } + await Task.Delay(1000); + } + } + private string GetCurrentIcon() { int rt = 0; @@ -130,7 +176,7 @@ private void HandleTaskStateUpdate(Tuple taskStateUpdate) catch (InvalidOperationException) { } return; } - UpdateIcon(GetCurrentIcon(), GetNowRunnedComponentsString()); + UpdateIcon(GetCurrentIcon(), GetNowRunningComponentsString()); } private void HandleTasksListUpdate() @@ -138,7 +184,7 @@ private void HandleTasksListUpdate() HandleTaskStateUpdate(Tuple.Create(string.Empty, false)); } - private static string GetNowRunnedComponentsString() + private static string GetNowRunningComponentsString() { string result = string.Empty; int cnt = 0; @@ -168,16 +214,19 @@ private void HideWindow() private void ShowContextMenuAt(Point location, int vertOffset, int horOffset) { + // A second right click opened another menu and the first one, with its task + // subscriptions, was never closed. + HideContextMenu(); TrayMenuForm = new(); TrayMenuForm.ShowWindow(location, vertOffset, horOffset); - TrayMenuForm.Hided += HideContextMenu; + TrayMenuForm.Hidden += HideContextMenu; } private void HideContextMenu() { if (TrayMenuForm != null) { - TrayMenuForm.Hided -= HideContextMenu; + TrayMenuForm.Hidden -= HideContextMenu; TrayMenuForm.Close(); TrayMenuForm.Dispose(); @@ -237,7 +286,7 @@ public void AddIcon(bool notify=false, string? iconName=null, string? toolTip = { if (IsDisposed || Disposing || _iconAdded) return; if (string.IsNullOrEmpty(iconName)) iconName = GetCurrentIcon(); - toolTip ??= GetNowRunnedComponentsString(); + toolTip ??= GetNowRunningComponentsString(); NOTIFYICONDATA data = new(); @@ -368,6 +417,10 @@ protected override void WndProc(ref Message m) break; case NIN_SELECT: + break; + case NIN_KEYSELECT: + // Enter or Space on the icon focused with the keyboard. + MaximizeApp(); break; case WM_CONTEXTMENU: var rect = GetRectIcon(); @@ -389,7 +442,7 @@ protected override void WndProc(ref Message m) { Logger.Instance.CreateDebugLog(nameof(EmptyForm), "TaskbarCreated received; restoring tray icon."); _iconAdded = false; - AddIcon(iconName: GetCurrentIcon(), toolTip: GetNowRunnedComponentsString()); + AddIcon(iconName: GetCurrentIcon(), toolTip: GetNowRunningComponentsString()); } } @@ -409,7 +462,8 @@ protected override void WndProc(ref Message m) public const Int32 NIN_BALLOONSHOW = 0x402; public const Int32 NIN_BALLOONTIMEOUT = 0x404; public const Int32 NIN_BALLOONUSERCLICK = 0x405; - public const Int32 NIN_KEYSELECT = 0x403; + // NIN_SELECT | NINF_KEY; it was 0x403, so the icon did not react to Enter or Space. + public const Int32 NIN_KEYSELECT = 0x401; public const Int32 NIN_SELECT = 0x400; public const Int32 NIN_POPUPOPEN = 0x406; public const Int32 NIN_POPUPCLOSE = 0x407; diff --git a/CDPIUI.TrayIcon/Forms/TrayMenuForm.cs b/CDPIUI.TrayIcon/Forms/TrayMenuForm.cs index 05bfc6f7..b9b6050d 100644 --- a/CDPIUI.TrayIcon/Forms/TrayMenuForm.cs +++ b/CDPIUI.TrayIcon/Forms/TrayMenuForm.cs @@ -1,4 +1,5 @@ -using CDPIUI.Shared.Pipe.Models; +using CDPIUI.Shared.ComponentsTask; +using CDPIUI.Shared.Pipe.Models; using CDPIUI.TrayIcon.Controls; using CDPIUI.TrayIcon.Helper; using CDPIUI.TrayIcon.Helper.Basic; @@ -12,7 +13,7 @@ public partial class TrayMenuForm : Form { private readonly int DefaultWidth; - public Action? Hided; + public Action? Hidden; public TrayMenuForm() { @@ -77,15 +78,15 @@ private void DisconnectHandlers() MaximizeButton.Clicked -= MaximizeButton_MouseClick; ExitButton.Clicked -= ExitButton_MouseClick; - DisconnectComponentPanelChildrensHandlers(); + DisconnectComponentPanelChildrenHandlers(); this.Disposed -= TrayMenuForm_Disposed; this.KeyDown -= TrayMenuForm_KeyDown; } - private void DisconnectComponentPanelChildrensHandlers() + private void DisconnectComponentPanelChildrenHandlers() { - foreach (Control control in ComponentPanel.Controls) + foreach (Control control in ComponentPanel.Controls.Cast().ToList()) { if (control is ComponentUserControl componentUserControl) { @@ -134,18 +135,29 @@ private void HandleEventHappens() private void HandleTasksListUpdate() { - this.Invoke(() => + if (IsDisposed || !IsHandleCreated) + return; + + List> tasks; + try + { + tasks = TasksHelper.Instance.Tasks.ToList().DistinctBy(task => task.Id).ToList(); + } + catch (InvalidOperationException) { - DisconnectComponentPanelChildrensHandlers(); + return; + } + + SafeInvoke(() => + { + ComponentPanel.SuspendLayout(); + DisconnectComponentPanelChildrenHandlers(); ComponentPanel.Controls.Clear(); - }); - int targetWidth = ComponentPanel.Width; - int offset = 0; + int targetWidth = ComponentPanel.Width; + int offset = 0; - if (TasksHelper.Instance.Tasks.Count == 0) - { - this.Invoke(() => + if (tasks.Count == 0) { var storeButton = new ModernButtonUserControl() { @@ -158,21 +170,17 @@ private void HandleTasksListUpdate() storeButton.Clicked += HandleButtonClick; ComponentPanel.Controls.Add(storeButton); storeButton.Invalidate(); - }); - - } - else - { - foreach (var task in TasksHelper.Instance.Tasks) + } + else { - this.Invoke(() => + foreach (var task in tasks) { var control = new ComponentUserControl() { Name = task.Id, DisplayText = LocaleHelper.GetLocalizedComponentName(task.Id), ComponentId = task.Id, - IsRunned = task.ProcessManager.IsProcessRunning, + IsRunning = task.ProcessManager.IsProcessRunning, IsSetupComplete = task.IsSetupComplete ?? true, Width = targetWidth, Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right, @@ -180,33 +188,46 @@ private void HandleTasksListUpdate() Top = offset }; - - control.EventHappens += HandleEventHappens; ComponentPanel.Controls.Add(control); control.Invalidate(); offset += control.Height + control.Margin.Top + control.Margin.Bottom; - }); + } } - } - this.Invoke(() => - { + + ComponentPanel.ResumeLayout(); CheckWindowHeight(); ComponentPanel.Invalidate(); }); } + // Task events come from other threads; the form can be destroyed between the + // IsHandleCreated check and Invoke, and the exception ended the tray. + private void SafeInvoke(Action action) + { + try + { + if (!IsDisposed && IsHandleCreated) + Invoke(action); + } + catch (ObjectDisposedException) { } + catch (InvalidOperationException) { } + } + private async void HandleTaskStateUpdate(Tuple taskStateUpdate) { - this.Invoke(() => + if (IsDisposed || !IsHandleCreated) + return; + + SafeInvoke(() => { Control[] controls = ComponentPanel.Controls.Find(taskStateUpdate.Item1, false); if (controls.Length > 0) { if (controls[0] is ComponentUserControl componentUserControl) { - componentUserControl.IsRunned = taskStateUpdate.Item2; + componentUserControl.IsRunning = taskStateUpdate.Item2; } } else @@ -220,7 +241,10 @@ private async void HandleTaskStateUpdate(Tuple taskStateUpdate) private async void HandleTaskSetupStateUpdate(Tuple taskStateUpdate) { - this.Invoke(() => + if (IsDisposed || !IsHandleCreated) + return; + + SafeInvoke(() => { Control[] controls = ComponentPanel.Controls.Find(taskStateUpdate.Item1, false); if (controls.Length > 0) @@ -292,8 +316,7 @@ public void HideWindow() Location = new Point(-2000, -2000); Size = new Size(1, 1); - GC.Collect(); - Hided?.Invoke(); + Hidden?.Invoke(); } public void ShowWindow(Point location, int vertOffset, int horOffset) { diff --git a/CDPIUI.TrayIcon/Helper/Basic/ChildProcessJob.cs b/CDPIUI.TrayIcon/Helper/Basic/ChildProcessJob.cs new file mode 100644 index 00000000..19fc8fa7 --- /dev/null +++ b/CDPIUI.TrayIcon/Helper/Basic/ChildProcessJob.cs @@ -0,0 +1,112 @@ +using System.ComponentModel; +using System.Runtime.InteropServices; + +namespace CDPIUI.TrayIcon.Helper.Basic +{ + /// + /// A job object that ends the components started by the tray when the tray process ends, + /// also when it is killed or crashes: winws, GoodbyeDPI or ProxiFyre kept running without a + /// tray that could stop them. + /// + internal static class ChildProcessJob + { + private static readonly Lazy Job = new(CreateJob); + + public static void Assign(IntPtr processHandle) + { + IntPtr job = Job.Value; + if (job == IntPtr.Zero) + return; + if (!AssignProcessToJobObject(job, processHandle)) + Logger.Instance.CreateWarningLog(nameof(ChildProcessJob), + $"Cannot assign the process to the job: {new Win32Exception(Marshal.GetLastWin32Error()).Message}"); + } + + private static IntPtr CreateJob() + { + IntPtr job = CreateJobObject(IntPtr.Zero, null); + if (job == IntPtr.Zero) + { + Logger.Instance.CreateWarningLog(nameof(ChildProcessJob), $"Cannot create the job: {Marshal.GetLastWin32Error()}"); + return IntPtr.Zero; + } + + var info = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION + { + BasicLimitInformation = new JOBOBJECT_BASIC_LIMIT_INFORMATION + { + LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE + } + }; + int length = Marshal.SizeOf(); + IntPtr buffer = Marshal.AllocHGlobal(length); + try + { + Marshal.StructureToPtr(info, buffer, false); + if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, buffer, (uint)length)) + { + Logger.Instance.CreateWarningLog(nameof(ChildProcessJob), $"Cannot configure the job: {Marshal.GetLastWin32Error()}"); + CloseHandle(job); + return IntPtr.Zero; + } + } + finally + { + Marshal.FreeHGlobal(buffer); + } + // The handle stays open for the life of the tray; the system closes it on exit. + return job; + } + + private const uint JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000; + private const int JobObjectExtendedLimitInformation = 9; + + [StructLayout(LayoutKind.Sequential)] + private struct JOBOBJECT_BASIC_LIMIT_INFORMATION + { + public long PerProcessUserTimeLimit; + public long PerJobUserTimeLimit; + public uint LimitFlags; + public UIntPtr MinimumWorkingSetSize; + public UIntPtr MaximumWorkingSetSize; + public uint ActiveProcessLimit; + public UIntPtr Affinity; + public uint PriorityClass; + public uint SchedulingClass; + } + + [StructLayout(LayoutKind.Sequential)] + private struct IO_COUNTERS + { + public ulong ReadOperationCount; + public ulong WriteOperationCount; + public ulong OtherOperationCount; + public ulong ReadTransferCount; + public ulong WriteTransferCount; + public ulong OtherTransferCount; + } + + [StructLayout(LayoutKind.Sequential)] + private struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION + { + public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; + public IO_COUNTERS IoInfo; + public UIntPtr ProcessMemoryLimit; + public UIntPtr JobMemoryLimit; + public UIntPtr PeakProcessMemoryUsed; + public UIntPtr PeakJobMemoryUsed; + } + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string? lpName); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool SetInformationJobObject(IntPtr hJob, int infoClass, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool CloseHandle(IntPtr hObject); + } +} diff --git a/CDPIUI.TrayIcon/Helper/Basic/CommandsHandler.cs b/CDPIUI.TrayIcon/Helper/Basic/CommandsHandler.cs index 29791856..6791b7a4 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/CommandsHandler.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/CommandsHandler.cs @@ -91,12 +91,17 @@ private static async Task HandleCONPTYMessage(CONPTYMessageModel model) return; } + // Both requests name one component; without an id they apply to all tasks. case CONPTYMessageIds.GetProcessIdFullOutput: - TasksHelper.Instance.SendAllTasksOutput(); + await TasksHelper.Instance.SendTasksOutput(model.MessageData?["componentId"]); + return; + + case CONPTYMessageIds.GetProcessIdState: + await TasksHelper.Instance.SendTasksState(model.MessageData?["componentId"]); return; case CONPTYMessageIds.GetAllProcessStates: - TasksHelper.Instance.SendAllTasksState(); + await TasksHelper.Instance.SendTasksState(); return; case CONPTYMessageIds.ProcessIdStartupArgsChanged: @@ -139,6 +144,10 @@ private static async Task CaptureHelpOutputAsync(CONPTYMessageModel model) "The component executable was not found.", executablePath); } + if (!TrustedPaths.IsAllowed(executablePath, "help output capture")) + { + throw new UnauthorizedAccessException("The executable is outside the application directories."); + } ConPTYCaptureResult result = await ConPTYHelper.CaptureProcessOutputAsync( executablePath, @@ -191,7 +200,6 @@ private static async Task HandleGoodCheckMessage(GoodCheckMessageModel model) return; await GoodCheckProcessHelper.Instance.StartAsync(executable, arguments, taskId); - TasksHelper.Instance.ApplyStatusToAllTasks(false); return; } diff --git a/CDPIUI.TrayIcon/Helper/Basic/ConPTYHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/ConPTYHelper.cs index e00a696d..53593c20 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/ConPTYHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/ConPTYHelper.cs @@ -1,10 +1,10 @@ -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.TrayIcon.Helper.Basic; using Microsoft.Win32.SafeHandles; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; namespace CDPIUI.TrayIcon.Helper { @@ -35,51 +35,68 @@ public class ConPTYHelper public Action? ProcessExited; public Action? OutputAdded; - private readonly string Preffix = string.Empty; + private readonly string Prefix = string.Empty; private StopActionCallers StopActionCaller = StopActionCallers.Unknown; - static readonly Dictionary errorMappings = new() + // Checked in this order and the first match of a line wins: with the dictionary every + // matching key overwrote the previous one, so "Error opening filter" became UNKNOWN + // through the generic "error" key. + static readonly (string Text, PrettyErrorCode Code)[] errorMappings = { - { "Error opening filter", PrettyErrorCode.FILTER_OPEN_ERROR }, - { "unknown option", PrettyErrorCode.PARAMETER_ERROR }, - { "hostlists load failed", PrettyErrorCode.HOSTLIST_LOAD_ERROR }, - { "must specify port filter", PrettyErrorCode.PORT_FILTER_ERROR }, - { "must specify port or/and partial raw filter", PrettyErrorCode.PORT_FILTER_WRONG_VALUE_ERROR }, - { "ERROR:", PrettyErrorCode.UNKNOWN }, - { "Component not installed correctly", PrettyErrorCode.COMPONENT_INSTALL_ERROR }, - { "error", PrettyErrorCode.UNKNOWN }, - { "invalid value", PrettyErrorCode.INVALID_VALUE_ERROR }, - { "nvalid value", PrettyErrorCode.INVALID_VALUE_ERROR }, - { "option requires an argument", PrettyErrorCode.INVALID_VALUE_ERROR }, - { "--debug=0|1|syslog|@", PrettyErrorCode.PARAMETER_ERROR }, - { "already running", PrettyErrorCode.ALREADY_RUNNING_WARN }, - { "could not read", PrettyErrorCode.FILE_READ_ERROR }, - { "flag provided but not defined:", PrettyErrorCode.PARAMETER_ERROR }, - { "cannot create", PrettyErrorCode.ACCESS_DENIED }, - { "cannot access", PrettyErrorCode.ACCESS_DENIED } + ("Error opening filter", PrettyErrorCode.FILTER_OPEN_ERROR), + ("unknown option", PrettyErrorCode.PARAMETER_ERROR), + ("hostlists load failed", PrettyErrorCode.HOSTLIST_LOAD_ERROR), + ("must specify port filter", PrettyErrorCode.PORT_FILTER_ERROR), + ("must specify port or/and partial raw filter", PrettyErrorCode.PORT_FILTER_WRONG_VALUE_ERROR), + ("ERROR:", PrettyErrorCode.UNKNOWN), + ("Component not installed correctly", PrettyErrorCode.COMPONENT_INSTALL_ERROR), + ("error", PrettyErrorCode.UNKNOWN), + ("invalid value", PrettyErrorCode.INVALID_VALUE_ERROR), + ("nvalid value", PrettyErrorCode.INVALID_VALUE_ERROR), + ("option requires an argument", PrettyErrorCode.INVALID_VALUE_ERROR), + ("--debug=0|1|syslog|@", PrettyErrorCode.PARAMETER_ERROR), + ("already running", PrettyErrorCode.ALREADY_RUNNING_WARN), + ("could not read", PrettyErrorCode.FILE_READ_ERROR), + ("flag provided but not defined:", PrettyErrorCode.PARAMETER_ERROR), + ("cannot create", PrettyErrorCode.ACCESS_DENIED), + ("cannot access", PrettyErrorCode.ACCESS_DENIED) }; + private static PrettyErrorCode? FindError(string output) + { + PrettyErrorCode? generic = null; + foreach (var (text, code) in errorMappings) + { + if (!output.Contains(text, StringComparison.OrdinalIgnoreCase)) + continue; + if (code != PrettyErrorCode.UNKNOWN) + return code; + generic ??= code; + } + return generic; + } + public bool processState { get; private set; } = false; private bool CurrentState = false; private object _setStateLock = new(); - private void ChangeProcessState(bool isRunned) + private void ChangeProcessState(bool isRunning) { lock (_setStateLock) { - if (CurrentState != isRunned) + if (CurrentState != isRunning) { - CurrentState = isRunned; + CurrentState = isRunning; processState = CurrentState; - ProcessStateChanged?.Invoke(isRunned); + ProcessStateChanged?.Invoke(isRunning); Debug.WriteLine(processState); } } } - public ConPTYHelper(string preffix = "") + public ConPTYHelper(string prefix = "") { - Preffix = preffix; + Prefix = prefix; _outputDefaultBuffer = new StringBuilder(); } @@ -175,11 +192,11 @@ public static async Task CaptureProcessOutputAsync( : workingDirectory; bool started = CreateProcess( null, - $"\"{exePath}\" {args}", + new StringBuilder($"\"{exePath}\" {args}"), IntPtr.Zero, IntPtr.Zero, false, - EXTENDED_STARTUPINFO_PRESENT, + EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, IntPtr.Zero, resolvedWorkingDirectory, ref startupInfo, @@ -190,6 +207,7 @@ public static async Task CaptureProcessOutputAsync( $"Cannot start process, error: {Marshal.GetLastWin32Error()}"); } + ChildProcessJob.Assign(processInfo.hProcess); CloseHandle(processInfo.hThread); processInfo.hThread = IntPtr.Zero; @@ -340,7 +358,7 @@ public async void RunProcess(string exePath, string args, string workingDirector } catch (Exception ex) { - ShowErrorMessage(ErrorHelper.Convertor.GetPrettyErrorCode(nameof(ConPTYHelper), ex).ToEnum(), _object: "console"); + ShowErrorMessage(ErrorHelper.Convertor.GetLoggedErrorCode(nameof(ConPTYHelper), ex), _object: "console"); SendStopMessage("Unexpected error happens while trying to stop process"); processState = false; @@ -358,11 +376,16 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki IntPtr hInputWrite = IntPtr.Zero; IntPtr hOutputRead = IntPtr.Zero; IntPtr hOutputWrite = IntPtr.Zero; + IntPtr lpAttrList = IntPtr.Zero; + var pi = new PROCESS_INFORMATION(); PrettyErrorCode lastError = PrettyErrorCode.SUCCESS; try { + if (!TrustedPaths.IsAllowed(exePath, "process start")) + throw new UnauthorizedAccessException("The executable is outside the application directories."); + CreatePipe(out hInputRead, out hInputWrite, false); CreatePipe(out hOutputRead, out hOutputWrite, false); @@ -396,26 +419,30 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki } }; - IntPtr lpAttrList = IntPtr.Zero; var lpSize = IntPtr.Zero; InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref lpSize); lpAttrList = Marshal.AllocHGlobal(lpSize); - InitializeProcThreadAttributeList(lpAttrList, 1, 0, ref lpSize); + if (!InitializeProcThreadAttributeList(lpAttrList, 1, 0, ref lpSize)) + { + Marshal.FreeHGlobal(lpAttrList); + lpAttrList = IntPtr.Zero; + throw new Exception($"Cannot initialize process attributes, error: {Marshal.GetLastWin32Error()}"); + } UpdateProcThreadAttribute(lpAttrList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, _pseudoConsoleHandle, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero); si.lpAttributeList = lpAttrList; - var pi = new PROCESS_INFORMATION(); - + // CreateProcessW: CreateProcessA turned characters outside the ANSI code page of + // the system into '?' in paths and arguments. var success = CreateProcess( null, - $"\"{exePath}\" {args}", + new StringBuilder($"\"{exePath}\" {args}"), IntPtr.Zero, IntPtr.Zero, false, - EXTENDED_STARTUPINFO_PRESENT, + EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, IntPtr.Zero, workingDirectory, ref si, @@ -428,6 +455,7 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki } _processInfo = pi; + ChildProcessJob.Assign(pi.hProcess); CloseHandle(pi.hThread); @@ -439,29 +467,32 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki using (var reader = new FileStream(safeOutputReadHandle, FileAccess.Read)) { byte[] buffer = new byte[4096]; + char[] chars = new char[Encoding.UTF8.GetMaxCharCount(buffer.Length)]; + // Keeps a multi-byte character that is split between two reads. + Decoder decoder = Encoding.UTF8.GetDecoder(); int bytesRead; while (!token.IsCancellationRequested) { bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length, cancellationToken: token); + // The pipe was closed: reading again returns 0 at once and spun the loop. + if (bytesRead == 0) + break; - string _output = Encoding.UTF8.GetString(buffer, 0, bytesRead); + string _output = new string(chars, 0, decoder.GetChars(buffer, 0, bytesRead, chars, 0)); - foreach (var errorMapping in errorMappings) - { - if (_output.Contains(errorMapping.Key, StringComparison.OrdinalIgnoreCase)) - { - lastError = errorMapping.Value; - } - } + // A specific error stays; a later generic match does not overwrite it. + PrettyErrorCode? found = FindError(_output); + if (found != null && (lastError == PrettyErrorCode.SUCCESS || found != PrettyErrorCode.UNKNOWN)) + lastError = found.Value; - if (!string.IsNullOrEmpty(Preffix) && !string.IsNullOrEmpty(_output)) + if (!string.IsNullOrEmpty(Prefix) && !string.IsNullOrEmpty(_output)) { string newOutput = string.Empty; foreach (string str in _output.Split("\n")) { - newOutput += $"[{Preffix}] {str}\n"; + newOutput += $"[{Prefix}] {str}\n"; } _output = newOutput; } @@ -477,11 +508,6 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki } } - TerminateProcess(pi.hProcess, 0); - - WaitForSingleObject(pi.hProcess, INFINITE); - - CloseHandle(pi.hProcess); } catch (OperationCanceledException ex) { @@ -492,11 +518,26 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki catch (Exception ex) { if (ex.Message != "External component has thrown an exception.") - ShowErrorMessage(ErrorHelper.Convertor.GetPrettyErrorCode(nameof(ConPTYHelper), ex).ToEnum(), _object: "console"); + ShowErrorMessage(ErrorHelper.Convertor.GetLoggedErrorCode(nameof(ConPTYHelper), ex), _object: "console"); } finally { + // The process handle was left open on the cancel and error paths. + if (pi.hProcess != IntPtr.Zero) + { + if (WaitForSingleObject(pi.hProcess, 0) == WAIT_TIMEOUT) + { + TerminateProcess(pi.hProcess, 0); + WaitForSingleObject(pi.hProcess, ProcessExitWaitMs); + } + CloseHandle(pi.hProcess); + } + if (lpAttrList != IntPtr.Zero) + { + DeleteProcThreadAttributeList(lpAttrList); + Marshal.FreeHGlobal(lpAttrList); + } if (_pseudoConsoleHandle != IntPtr.Zero) { ClosePseudoConsole(_pseudoConsoleHandle); @@ -507,6 +548,16 @@ public async void RunProcessWithConPTY(string exePath, string args, string worki CloseHandle(_hInputWrite); _hInputWrite = IntPtr.Zero; } + // Pipes of a failed CreatePseudoConsole. + if (hInputRead != IntPtr.Zero) + CloseHandle(hInputRead); + if (hOutputWrite != IntPtr.Zero) + CloseHandle(hOutputWrite); + if (_hOutputRead != IntPtr.Zero) + { + CloseHandle(_hOutputRead); + _hOutputRead = IntPtr.Zero; + } SendStopMessage("Process will be stopped"); @@ -556,14 +607,12 @@ public async Task StopProcess(bool output = true) try { + // The handle belongs to RunProcessWithConPTY, which closes it; here the process is + // only ended, and without an endless wait that could block the calling UI thread. if (_processInfo.hProcess != IntPtr.Zero) { TerminateProcess(_processInfo.hProcess, 0); - - WaitForSingleObject(_processInfo.hProcess, INFINITE); - - CloseHandle(_processInfo.hProcess); - + WaitForSingleObject(_processInfo.hProcess, ProcessExitWaitMs); _processInfo = default; } @@ -590,10 +639,10 @@ public async Task StopProcess(bool output = true) private void SendStopMessage(string output = "Process will be stopped by user") { - string preffix = string.IsNullOrEmpty(Preffix) ? "" : $" [{Preffix}]"; - _outputDefaultBuffer.Append($"\n[PSEUDOCONSOLE]{preffix} {output}"); + string prefix = string.IsNullOrEmpty(Prefix) ? "" : $" [{Prefix}]"; + _outputDefaultBuffer.Append($"\n[PSEUDOCONSOLE]{prefix} {output}"); - ProcessExited?.Invoke($"\n[PSEUDOCONSOLE]{preffix} {output}"); + ProcessExited?.Invoke($"\n[PSEUDOCONSOLE]{prefix} {output}"); } private void ShowErrorMessage(PrettyErrorCode errorCode, string _object = "process") @@ -616,6 +665,8 @@ public string GetDefaultOutput() private const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000; private const ushort SW_HIDE = 0; private const uint INFINITE = 0xFFFFFFFF; + private const uint ProcessExitWaitMs = 5000; + private const uint CREATE_UNICODE_ENVIRONMENT = 0x00000400; private const uint WAIT_OBJECT_0 = 0x000000000; private const uint WAIT_TIMEOUT = 0x00000102; @@ -640,10 +691,10 @@ static extern bool GetExitCodeProcess( IntPtr hProcess, out System.UInt32 lpExitCode); - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateProcessW")] static extern bool CreateProcess( string? lpApplicationName, - string lpCommandLine, + StringBuilder lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, diff --git a/CDPIUI.TrayIcon/Helper/Basic/ConnectionHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/ConnectionHelper.cs index 4a4d2050..c79cfa44 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/ConnectionHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/ConnectionHelper.cs @@ -56,17 +56,7 @@ private async Task ListenLoopAsync() IsAuthorized = false; try { - var ps = new PipeSecurity(); - - var currentUserSid = WindowsIdentity.GetCurrent().User; - if (currentUserSid == null) - { - continue; - } - ps.AddAccessRule(new PipeAccessRule(currentUserSid, PipeAccessRights.FullControl, AccessControlType.Allow)); - - var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); - ps.AddAccessRule(new PipeAccessRule(everyone, PipeAccessRights.ReadWrite, AccessControlType.Allow)); + var ps = PipeAccessPolicy.CreatePipeSecurity(); PipeStream = NamedPipeServerStreamAcl.Create( SharedConstants.PipeName, @@ -80,20 +70,47 @@ private async Task ListenLoopAsync() } catch (Exception ex) { - Logger?.CreateDebugLog(nameof(PipeServer), - $"Terminating PID={Environment.ProcessId}: pipe creation failed. " + - $"Another tray instance may already own the pipe. Exception: {ex}"); - Process.GetCurrentProcess().Kill(); - return; + if (IsAnotherTrayRunning()) + { + Logger?.CreateDebugLog(nameof(PipeServer), + $"Terminating PID={Environment.ProcessId}: pipe creation failed and another tray instance is running. " + + $"Exception: {ex}"); + Process.GetCurrentProcess().Kill(); + return; + } + + // Another process took the name while the pipe was being recreated. Clients check + // that the server is this tray, so waiting for the name to be released is enough. + Logger?.CreateWarningLog(nameof(PipeServer), $"Cannot create the pipe, retrying: {ex.Message}"); + try { await Task.Delay(TimeSpan.FromSeconds(2), CancellationToken ?? default); } + catch (OperationCanceledException) { break; } + continue; } try { - Console.WriteLine("WAIT"); await ((NamedPipeServerStream)PipeStream).WaitForConnectionAsync(CancellationToken ?? default); int threadId = Thread.CurrentThread.ManagedThreadId; + if (!PipePeerVerifier.IsClientExpected((NamedPipeServerStream)PipeStream, PipeAccessPolicy.ExpectedClientPath)) + { + Logger?.CreateWarningLog(nameof(PipeServer), "Rejected a pipe client that is not the application UI."); + continue; + } + Logger?.CreateDebugLog(nameof(PipeServer), $"Client connected on thread[{threadId}]."); + // The pipe takes one client at a time: a client that never authorizes would + // keep the real GUI out. + var connectedStream = (NamedPipeServerStream)PipeStream; + _ = Task.Run(async () => + { + await Task.Delay(AuthorizationTimeout); + if (!IsAuthorized && ReferenceEquals(PipeStream, connectedStream) && connectedStream.IsConnected) + { + Logger?.CreateWarningLog(nameof(PipeServer), "The client did not authorize in time; disconnecting."); + try { connectedStream.Disconnect(); } catch (Exception) { } + } + }); await HandleConnectionAsync(); } catch (OperationCanceledException) @@ -125,22 +142,64 @@ private async Task ListenLoopAsync() } } + private static readonly TimeSpan AuthorizationTimeout = TimeSpan.FromSeconds(15); + + // Before authorization only service messages (connection, authorization result) go out: + // component output and paths are for the authorized GUI only. + protected override bool CanSend(string message) + { + if (IsAuthorized) + return true; + try + { + return PipeModelConvertor.ConvertBack(message) is ServiceMessageModel; + } + catch (Exception) + { + return false; + } + } + + private static bool IsAnotherTrayRunning() + { + using var current = Process.GetCurrentProcess(); + Process[] trays = Process.GetProcessesByName(current.ProcessName); + try + { + return trays.Any(process => process.Id != current.Id); + } + finally + { + foreach (var process in trays) process.Dispose(); + } + } + + // Compared in constant time, so the time of a wrong answer tells nothing about the secret. + private static bool SecretEquals(string? value, string secret) => + value != null && System.Security.Cryptography.CryptographicOperations.FixedTimeEquals( + System.Text.Encoding.UTF8.GetBytes(value), System.Text.Encoding.UTF8.GetBytes(secret)); + protected override async Task RunMessageActions(IPipeMessage message) { if (message is ServiceMessageModel messageModel) { if (messageModel.MessageType == ServiceMessageIds.RequestAuth) { - Debug.WriteLine(messageModel.MessageData["GUID"]); - if (messageModel.MessageData != null && messageModel.MessageData["GUID"] == Secret.AuthGuid) + // An empty secret (a build without Secret.cs values) must not let any client in. + if (!string.IsNullOrEmpty(Secret.AuthGuid) && + messageModel.MessageData != null && SecretEquals(messageModel.MessageData["GUID"], Secret.AuthGuid)) { IsAuthorized = true; _ = SendMessageAsync(ServiceMessageModel.AuthSuccessful().ToString()); } else { + // A client with a wrong GUID could retry until the authorization timeout. IsAuthorized = false; - _ = SendMessageAsync(ServiceMessageModel.AuthFailure().ToString()); + await SendMessageAsync(ServiceMessageModel.AuthFailure().ToString()); + Logger?.CreateWarningLog(nameof(PipeServer), "Authorization failed; disconnecting the client."); + try { (PipeStream as NamedPipeServerStream)?.Disconnect(); } catch (Exception) { } + return; } } } diff --git a/CDPIUI.TrayIcon/Helper/Basic/ErrorHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/ErrorHelper.cs index bd088462..1cac4ee8 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/ErrorHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/ErrorHelper.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Logger; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using System; using System.Collections.Generic; using System.Linq; @@ -29,14 +29,20 @@ public static CustomErrorConvertor Convertor public class CustomErrorConvertor : ErrorConvertor { - public override string GetPrettyErrorCode(string preffix, int hcode, ILogger? _ = null) + public override string GetPrettyErrorCode(string prefix, int hcode, ILogger? _ = null) { - return base.GetPrettyErrorCode(preffix, hcode, Logger.Instance); + return base.GetPrettyErrorCode(prefix, hcode, Logger.Instance); } - public override string GetPrettyErrorCode(string preffix, Exception ex, ILogger? _ = null) + public override string GetPrettyErrorCode(string prefix, Exception ex, ILogger? _ = null) { - return base.GetPrettyErrorCode(preffix, ex, Logger.Instance); + return base.GetPrettyErrorCode(prefix, ex, Logger.Instance); + } + + public PrettyErrorCode GetLoggedErrorCode(string prefix, Exception ex) + { + GetPrettyErrorCode(prefix, ex); + return MapExceptionToCode(ex, out _); } } } diff --git a/CDPIUI.TrayIcon/Helper/Basic/Logger.cs b/CDPIUI.TrayIcon/Helper/Basic/Logger.cs index 16107bce..30d92f3b 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/Logger.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/Logger.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.Logger; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using System; using System.Collections.Generic; using System.Diagnostics; @@ -36,7 +36,7 @@ private static LogSelevirity GetSelevirity() #if DEBUG return LogSelevirity.DEBG; #else - return SettingsManager.Instance.GetValue("DEBUG", "logLevel").ToEnum(LogSelevirity.DEBG); + return SettingsManager.Instance.GetValue("DEBUG", "logLevel").ToEnum(LogSelevirity.WARN); #endif } } diff --git a/CDPIUI.TrayIcon/Helper/Basic/NotifyHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/NotifyHelper.cs index be2ef74b..48139001 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/NotifyHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/NotifyHelper.cs @@ -129,12 +129,19 @@ public static void OpenFileInDefaultApp(string filePath) { try { - var psi = new ProcessStartInfo + // The tray is elevated, and the handler of a file type comes from HKCU: opening the file + // through the shell here would run a program the user chose with administrator rights. + string fullPath = Path.GetFullPath(filePath); + string explorer = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe"); + if (RunHelper.RunAsDesktopUser(explorer, $"\"{fullPath}\"")) + return; + + Process.Start(new ProcessStartInfo { - FileName = Path.GetFullPath(filePath), - UseShellExecute = true - }; - Process.Start(psi); + FileName = Path.Combine(Environment.SystemDirectory, "notepad.exe"), + Arguments = $"\"{fullPath}\"", + UseShellExecute = false, + }); } catch (Exception ex) { diff --git a/CDPIUI.TrayIcon/Helper/Basic/PipeAccessPolicy.cs b/CDPIUI.TrayIcon/Helper/Basic/PipeAccessPolicy.cs new file mode 100644 index 00000000..26bd8a09 --- /dev/null +++ b/CDPIUI.TrayIcon/Helper/Basic/PipeAccessPolicy.cs @@ -0,0 +1,78 @@ +using System.IO.Pipes; +using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Principal; + +namespace CDPIUI.TrayIcon.Helper.Basic +{ + public static class PipeAccessPolicy + { + private const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; + private const uint TOKEN_QUERY = 0x0008; + + public static string ExpectedClientPath => Path.Combine(Utils.GetDataDirectory(), "CDPIUI.exe"); + + public static PipeSecurity CreatePipeSecurity() + { + var security = new PipeSecurity(); + var trayUser = WindowsIdentity.GetCurrent().User + ?? throw new InvalidOperationException("Cannot determine the tray process user."); + + security.SetOwner(trayUser); + security.AddAccessRule(new PipeAccessRule(trayUser, PipeAccessRights.FullControl, AccessControlType.Allow)); + security.AddAccessRule(new PipeAccessRule( + new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), PipeAccessRights.FullControl, AccessControlType.Allow)); + + // With over-the-shoulder elevation the tray runs as an administrator account while the UI runs as the desktop user. + var desktopUser = GetDesktopUserSid(); + if (desktopUser != null && desktopUser != trayUser) + security.AddAccessRule(new PipeAccessRule(desktopUser, PipeAccessRights.ReadWrite, AccessControlType.Allow)); + + security.AddAccessRule(new PipeAccessRule( + new SecurityIdentifier(WellKnownSidType.NetworkSid, null), PipeAccessRights.FullControl, AccessControlType.Deny)); + + return security; + } + + public static SecurityIdentifier? GetDesktopUserSid() + { + IntPtr shellWindow = GetShellWindow(); + if (shellWindow == IntPtr.Zero || GetWindowThreadProcessId(shellWindow, out uint shellProcessId) == 0) + return null; + + IntPtr process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, shellProcessId); + if (process == IntPtr.Zero) + return null; + + IntPtr token = IntPtr.Zero; + try + { + if (!OpenProcessToken(process, TOKEN_QUERY, out token)) + return null; + + using var identity = new WindowsIdentity(token); + return identity.User; + } + finally + { + if (token != IntPtr.Zero) CloseHandle(token); + CloseHandle(process); + } + } + + [DllImport("user32.dll")] + private static extern IntPtr GetShellWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr OpenProcess(uint desiredAccess, bool inheritHandle, uint processId); + + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool CloseHandle(IntPtr handle); + } +} diff --git a/CDPIUI.TrayIcon/Helper/Basic/RegeditHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/RegeditHelper.cs index d4dec91b..ec5fccf5 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/RegeditHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/RegeditHelper.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using CDPIUI.TrayIcon.Helper.Basic; +using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; @@ -18,6 +19,26 @@ public static class RegeditHelper [DllImport("wininet.dll", SetLastError = true)] private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); + /// + /// The Internet settings of the user at the desktop. The tray may run elevated under + /// another administrator account, whose HKCU is not the hive the browser reads. + /// + private static RegistryKey? OpenInternetSettings(bool writable) + { + var desktopUser = PipeAccessPolicy.GetDesktopUserSid(); + var trayUser = System.Security.Principal.WindowsIdentity.GetCurrent().User; + if (desktopUser != null && desktopUser != trayUser) + { + string path = $@"{desktopUser.Value}\{InternetSettingsKeyPath}"; + RegistryKey? userKey = writable ? Registry.Users.CreateSubKey(path) : Registry.Users.OpenSubKey(path, writable: false); + if (userKey != null) + return userKey; + } + return writable + ? Registry.CurrentUser.CreateSubKey(InternetSettingsKeyPath) + : Registry.CurrentUser.OpenSubKey(InternetSettingsKeyPath, writable: false); + } + public static Dictionary ReadProxySettings() { var result = new Dictionary(StringComparer.OrdinalIgnoreCase) @@ -26,7 +47,7 @@ public static Dictionary ReadProxySettings() ["ProxyOverride"] = string.Empty }; - using (var key = Registry.CurrentUser.OpenSubKey(InternetSettingsKeyPath, writable: false)) + using (var key = OpenInternetSettings(writable: false)) { if (key != null) { @@ -41,12 +62,90 @@ public static Dictionary ReadProxySettings() return result; } + public sealed record ProxySnapshot(int? ProxyEnable, string? ProxyServer, string? ProxyOverride); + + public static ProxySnapshot ReadProxySnapshot() + { + using var key = OpenInternetSettings(writable: false); + return new ProxySnapshot( + key?.GetValue("ProxyEnable") as int?, + key?.GetValue("ProxyServer") as string, + key?.GetValue("ProxyOverride") as string); + } + + public static void RestoreProxySnapshot(ProxySnapshot snapshot) + { + using (var key = OpenInternetSettings(writable: true)) + { + if (key == null) + throw new InvalidOperationException("ERR_REGISTRY_WRITE"); + + SetOrDelete(key, "ProxyServer", snapshot.ProxyServer); + SetOrDelete(key, "ProxyOverride", snapshot.ProxyOverride); + key.SetValue("ProxyEnable", snapshot.ProxyEnable ?? 0, RegistryValueKind.DWord); + } + + NotifySettingsChanged(); + } + + // The snapshot is also kept on disk while CDPI UI owns the system proxy, so the proxy of + // the user comes back after a crash, a kill or a shutdown of the tray. + private static string BackupFilePath => + Path.Combine(Utils.GetDataDirectory(), "proxy-backup.json"); + + public static void SaveSnapshotBackup(ProxySnapshot snapshot) + { + File.WriteAllText(BackupFilePath, System.Text.Json.JsonSerializer.Serialize(snapshot)); + } + + public static void DeleteSnapshotBackup() + { + try { File.Delete(BackupFilePath); } catch (IOException) { } + } + + /// + /// Restores the proxy settings left by a tray that did not stop its components. + /// + public static void RestoreLeftoverSnapshot() + { + try + { + if (!File.Exists(BackupFilePath)) + return; + var snapshot = System.Text.Json.JsonSerializer.Deserialize(File.ReadAllText(BackupFilePath)); + if (snapshot != null) + RestoreProxySnapshot(snapshot); + DeleteSnapshotBackup(); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(RegeditHelper), $"Cannot restore the proxy settings: {ex.Message}"); + } + } + + private static void SetOrDelete(RegistryKey key, string name, string? value) + { + if (value == null) + key.DeleteValue(name, throwOnMissingValue: false); + else + key.SetValue(name, value, RegistryValueKind.String); + } + + private static void NotifySettingsChanged() + { + if (!InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)) + throw new Win32Exception(Marshal.GetLastWin32Error(), "ERR_WININET_CALL"); + + if (!InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)) + throw new Win32Exception(Marshal.GetLastWin32Error(), "ERR_WININET_CALL"); + } + public static void SaveProxySettings(string proxyServer, string proxyOverride, int proxyEnable) { if (proxyEnable != 0 && proxyEnable != 1) throw new ArgumentOutOfRangeException(nameof(proxyEnable), "ERR_REGEDIT_HELPER_INTERNAL"); - using (var key = Registry.CurrentUser.CreateSubKey(InternetSettingsKeyPath)) + using (var key = OpenInternetSettings(writable: true)) { if (key == null) throw new InvalidOperationException("ERR_REGISTRY_WRITE"); @@ -60,15 +159,7 @@ public static void SaveProxySettings(string proxyServer, string proxyOverride, i key.SetValue("ProxyEnable", proxyEnable, RegistryValueKind.DWord); } - if (!InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)) - { - throw new Win32Exception(Marshal.GetLastWin32Error(), "ERR_WININET_CALL"); - } - - if (!InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)) - { - throw new Win32Exception(Marshal.GetLastWin32Error(), "ERR_WININET_CALL"); - } + NotifySettingsChanged(); } } } diff --git a/CDPIUI.TrayIcon/Helper/Basic/RunHelper.cs b/CDPIUI.TrayIcon/Helper/Basic/RunHelper.cs index 82de1dd0..4af0e637 100644 --- a/CDPIUI.TrayIcon/Helper/Basic/RunHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Basic/RunHelper.cs @@ -10,7 +10,7 @@ namespace CDPIUI.TrayIcon.Helper { public class RunHelper { - public static bool IsApplicationRunned(string exe) + public static bool IsApplicationRunning(string exe) { string target = exe; diff --git a/CDPIUI.TrayIcon/Helper/GoodCheckProcessHelper.cs b/CDPIUI.TrayIcon/Helper/GoodCheckProcessHelper.cs index 3473cd94..368c31a5 100644 --- a/CDPIUI.TrayIcon/Helper/GoodCheckProcessHelper.cs +++ b/CDPIUI.TrayIcon/Helper/GoodCheckProcessHelper.cs @@ -45,59 +45,82 @@ public GoodCheckProcessHelper() public async Task StartAsync(string executable, string args, string operationId) { + if (!TrustedPaths.IsAllowed(executable, "GoodCheck start")) + { + HandleProcessException("ERR_UNABLE_START_PROCESS", operationId); + return false; + } + await TasksHelper.Instance.StopAllTasks(); _cancellationTokenSource = new(); _cancellationToken = _cancellationTokenSource.Token; - await PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Runned, operationId); - TasksHelper.Instance.ApplyStatusToAllTasks(true); - - - var startInfo = new ProcessStartInfo + await PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Running, operationId); + TasksHelper.Instance.ApplyStatusToAllTasks(false); + // The component buttons are blocked while GoodCheck runs; they stayed blocked when + // the job could not be created, and the job handle was never closed. + try { - FileName = executable, - Arguments = args, - UseShellExecute = false, - CreateNoWindow = true, - WorkingDirectory = Path.GetDirectoryName(executable), - }; + var startInfo = new ProcessStartInfo + { + FileName = executable, + Arguments = args, + UseShellExecute = false, + CreateNoWindow = true, + WorkingDirectory = Path.GetDirectoryName(executable), + }; - Job = CreateKillOnCloseJob(); - + Job = CreateKillOnCloseJob(); - Process = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; + Process = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; - try - { - if (!Process.Start()) + try { + if (!Process.Start()) + { + HandleProcessException("ERR_UNABLE_START_PROCESS", operationId); + return false; + } + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(GoodCheckProcessHelper), $"ERR_UNABLE_START_PROCESS details => {ex.Message}"); HandleProcessException("ERR_UNABLE_START_PROCESS", operationId); return false; } - } - catch (Exception ex) - { - Logger.Instance.CreateErrorLog(nameof(GoodCheckProcessHelper), $"ERR_UNABLE_START_PROCESS details => {ex.Message}"); - HandleProcessException("ERR_UNABLE_START_PROCESS", operationId); - return false; - } - if (!AssignProcessToJobObject(Job, Process.Handle)) + if (!AssignProcessToJobObject(Job, Process.Handle)) + { + TryKillProcess(Process); + HandleProcessException("ERR_WIN32_EXCEPTION", operationId); + Logger.Instance.CreateErrorLog(nameof(GoodCheckProcessHelper), $"ERR_WIN32_EXCEPTION details => {Marshal.GetLastWin32Error()}"); + return false; + } + + var completedTask = await Task.WhenAny(Task.Run(() => + { + Process.WaitForExit(); + }, _cancellationToken)).ConfigureAwait(false); + + TryKillProcess(Process); + await PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Died, operationId); + return true; + } + catch (System.ComponentModel.Win32Exception ex) { + Logger.Instance.CreateErrorLog(nameof(GoodCheckProcessHelper), $"ERR_WIN32_EXCEPTION details => {ex.Message}"); HandleProcessException("ERR_WIN32_EXCEPTION", operationId); - Logger.Instance.CreateErrorLog(nameof(GoodCheckProcessHelper), $"ERR_WIN32_EXCEPTION details => {Marshal.GetLastWin32Error()}"); return false; } - - var completedTask = await Task.WhenAny(Task.Run(() => + finally { - Process.WaitForExit(); - }, _cancellationToken)).ConfigureAwait(false); - - TryKillProcess(Process); - await PipeHelper.SendGoodCheckPacket(Shared.Pipe.Models.GoodCheckMessageIds.Died, operationId); - TasksHelper.Instance.ApplyStatusToAllTasks(true); - return true; + if (Job != IntPtr.Zero) + { + CloseHandle(Job); + Job = IntPtr.Zero; + } + TasksHelper.Instance.ApplyStatusToAllTasks(true); + } } public void Stop() diff --git a/CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs b/CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs index f7e3ee7a..a7ca6f5c 100644 --- a/CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs +++ b/CDPIUI.TrayIcon/Helper/Locale/LocaleHelper.cs @@ -29,11 +29,11 @@ public static string GetLocalizedComponentName(string id) { { "SettingsRecoveryWarning", "Settings.xml could not be read or recovered. Settings were reset to defaults. Open the app to review them." }, { "UpdateFailure", "Application update failed.\nClick or tap here to open log" }, - { "TrayHide", "Application is runned and minimized to tray now.\nClick or tap here to open main window" }, + { "TrayHide", "Application is running and minimized to tray now.\nClick or tap here to open main window" }, { "Autorun", "Autorun" }, { "AutorunERR", "Cannot add this application to autorun." }, - { "ProcRun", "Process is runned now.\nClick or tap here to open open pseudoconsole and view process output" }, - { "ProcStop", "Process is stopped now.\nClick or tap here to open open pseudoconsole and view process output" }, + { "ProcRun", "Process is running now.\nClick or tap here to open pseudoconsole and view process output" }, + { "ProcStop", "Process is stopped now.\nClick or tap here to open pseudoconsole and view process output" }, { "ProcERR", "Cannot start process." }, { "Exit", "Exit" }, { "ShowMainWindow", "Maximize app" }, @@ -49,7 +49,7 @@ public static string GetLocalizedComponentName(string id) { "ConfigRequiredNewestVersionOfComponent", "One or several installed items required newest version of {0}.\nClick or tap here to check updates for {0}" }, { "Store", "Store" }, { "Setup", "Edit component's settings" }, - { "ToggleState", "Toggle component state (Runned/Stopped)" }, + { "ToggleState", "Toggle component state (Running/Stopped)" }, { "AllStopped", "All components are stopped." }, { "StartedAll", "All components are running now." }, { "StartedNow", "Now started: {0}." }, diff --git a/CDPIUI.TrayIcon/Helper/MsiInstallerHelper.cs b/CDPIUI.TrayIcon/Helper/MsiInstallerHelper.cs index f6dd8e59..8bd05345 100644 --- a/CDPIUI.TrayIcon/Helper/MsiInstallerHelper.cs +++ b/CDPIUI.TrayIcon/Helper/MsiInstallerHelper.cs @@ -158,17 +158,27 @@ private void TryStartNext() private async Task ProcessMsi(string operationId, string filePath) { + string? stagedPath = null; try { + if (!TrustedPaths.IsAllowed(filePath, "package installation")) + throw new UnauthorizedAccessException("The package is outside the application directories."); + + // Installers from the store (driver and runtime packages) are signed by their vendors. + // The copy is checked and run, so the user's copy cannot be swapped after the check. + stagedPath = PackageStaging.Stage(filePath); + if (!PackageStaging.HasValidSignature(stagedPath)) + throw new UnauthorizedAccessException("The package does not have a valid signature."); + _ = PipeHelper .SendMsiInstallationPacket(MSIInstallationMessageIds.SetOperationStatus, operationId, MsiState.Installing); lock (_lock) { - if (Path.GetExtension(filePath) == ".msi") - _currentProcess = RunHelper.Run("msiexec.exe", $"/i \"{filePath}\" /passive /norestart"); + if (string.Equals(Path.GetExtension(stagedPath), ".msi", StringComparison.OrdinalIgnoreCase)) + _currentProcess = RunHelper.Run(Path.Combine(Environment.SystemDirectory, "msiexec.exe"), $"/i \"{stagedPath}\" /passive /norestart"); else - _currentProcess = RunHelper.Run(filePath, $"/install /passive /norestart"); + _currentProcess = RunHelper.Run(stagedPath, $"/install /passive /norestart"); } if (_currentProcess == null) @@ -181,11 +191,11 @@ private async Task ProcessMsi(string operationId, string filePath) await _currentProcess.WaitForExitAsync().ConfigureAwait(false); int exitCode = _currentProcess.ExitCode; - if (exitCode == 3010) + if (exitCode == 3010 || exitCode == 1641) { _ = PipeHelper.SendMsiInstallationPacket(MSIInstallationMessageIds.SetOperationStatus, operationId, MsiState.CompleteRestartRequest); } - else if (exitCode == 0 || exitCode == 1603 || exitCode == 1638) + else if (exitCode == 0 || exitCode == 1638) { _ = PipeHelper.SendMsiInstallationPacket(MSIInstallationMessageIds.SetOperationStatus, operationId, MsiState.Complete); } @@ -220,6 +230,8 @@ private async Task ProcessMsi(string operationId, string filePath) _currentProcess = null; _currentOperationId = null; } + if (stagedPath != null) + PackageStaging.Release(stagedPath); TryStartNext(); } diff --git a/CDPIUI.TrayIcon/Helper/PackageStaging.cs b/CDPIUI.TrayIcon/Helper/PackageStaging.cs new file mode 100644 index 00000000..38aba091 --- /dev/null +++ b/CDPIUI.TrayIcon/Helper/PackageStaging.cs @@ -0,0 +1,171 @@ +using CDPIUI.Shared.Secrets; +using CDPIUI.Shared.Security; +using CDPIUI.TrayIcon.Helper.Basic; +using System.IO.Compression; +using System.Runtime.InteropServices; + +namespace CDPIUI.TrayIcon.Helper +{ + /// + /// Copies packages that the tray installs elevated out of the user's data directory and checks + /// them there, so the file cannot be replaced between the check and the installation + /// + public static class PackageStaging + { + // Under the installation directory, which only administrators can change in a per-machine installation. + private static string Root => Path.Combine(Utils.GetDataDirectory(), ".staging"); + + public static string Stage(string sourcePath) + { + string directory = Path.Combine(Root, Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(directory); + string stagedPath = Path.Combine(directory, Path.GetFileName(sourcePath)); + File.Copy(sourcePath, stagedPath); + return stagedPath; + } + + public static void Release(string stagedPath) + { + try + { + string? directory = Path.GetDirectoryName(stagedPath); + if (directory != null && Directory.Exists(directory)) + Directory.Delete(directory, true); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(PackageStaging), $"Cannot delete {stagedPath}: {ex.Message}"); + } + } + + /// + /// Removes packages left from earlier runs, for example by an installer that was started without waiting. + /// + public static void CleanUp() + { + try + { + if (Directory.Exists(Root)) + Directory.Delete(Root, true); + } + catch { } + } + + /// + /// Checks the signed catalog of every layer of an application update package + /// + public static async Task VerifyUpdatePackageAsync(string stagedPackage) + { + string verifyDirectory = Path.Combine(Path.GetDirectoryName(stagedPackage)!, "verify"); + try + { + using (ZipArchive archive = ZipFile.OpenRead(stagedPackage)) + { + archive.ExtractToDirectory(verifyDirectory); + } + + string layersDirectory = Path.Combine(verifyDirectory, "layers"); + List layers = Directory.Exists(layersDirectory) + ? Directory.GetDirectories(layersDirectory).ToList() + : [Path.Combine(verifyDirectory, "CDPIUI")]; + + if (layers.Count == 0 || !layers.All(Directory.Exists)) + return false; + + foreach (string layer in layers) + { + CatalogVerification result = await CatalogVerifier.VerifyAsync( + Path.Combine(layer, "catalog.cat"), layer, Secret.TrustedCertificates); + if (result != CatalogVerification.Success) + { + Logger.Instance.CreateWarningLog(nameof(PackageStaging), $"Update layer {Path.GetFileName(layer)} failed verification: {result}"); + return false; + } + } + return true; + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(PackageStaging), $"Cannot verify the update package: {ex.Message}"); + return false; + } + finally + { + try { if (Directory.Exists(verifyDirectory)) Directory.Delete(verifyDirectory, true); } catch { } + } + } + + /// + /// Checks that the file has a valid Authenticode signature that chains to a trusted root + /// + public static bool HasValidSignature(string path) + { + var fileInfo = new WINTRUST_FILE_INFO + { + cbStruct = (uint)Marshal.SizeOf(), + pcwszFilePath = path, + }; + IntPtr fileInfoPointer = Marshal.AllocHGlobal(Marshal.SizeOf()); + try + { + Marshal.StructureToPtr(fileInfo, fileInfoPointer, false); + var data = new WINTRUST_DATA + { + cbStruct = (uint)Marshal.SizeOf(), + dwUIChoice = WTD_UI_NONE, + fdwRevocationChecks = WTD_REVOKE_NONE, + dwUnionChoice = WTD_CHOICE_FILE, + pFile = fileInfoPointer, + dwStateAction = WTD_STATEACTION_IGNORE, + }; + + Guid action = WINTRUST_ACTION_GENERIC_VERIFY_V2; + int result = WinVerifyTrust(IntPtr.Zero, ref action, ref data); + if (result != 0) + Logger.Instance.CreateWarningLog(nameof(PackageStaging), $"Signature check of {path} failed: 0x{result:X8}"); + return result == 0; + } + finally + { + Marshal.DestroyStructure(fileInfoPointer); + Marshal.FreeHGlobal(fileInfoPointer); + } + } + + private static readonly Guid WINTRUST_ACTION_GENERIC_VERIFY_V2 = new("00AAC56B-CD44-11d0-8CC2-00C04FC295EE"); + private const uint WTD_UI_NONE = 2; + private const uint WTD_REVOKE_NONE = 0; + private const uint WTD_CHOICE_FILE = 1; + private const uint WTD_STATEACTION_IGNORE = 0; + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + private struct WINTRUST_FILE_INFO + { + public uint cbStruct; + [MarshalAs(UnmanagedType.LPWStr)] public string pcwszFilePath; + public IntPtr hFile; + public IntPtr pgKnownSubject; + } + + [StructLayout(LayoutKind.Sequential)] + private struct WINTRUST_DATA + { + public uint cbStruct; + public IntPtr pPolicyCallbackData; + public IntPtr pSIPClientData; + public uint dwUIChoice; + public uint fdwRevocationChecks; + public uint dwUnionChoice; + public IntPtr pFile; + public uint dwStateAction; + public IntPtr hWVTStateData; + public IntPtr pwszURLReference; + public uint dwProvFlags; + public uint dwUIContext; + public IntPtr pSignatureSettings; + } + + [DllImport("wintrust.dll", ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)] + private static extern int WinVerifyTrust(IntPtr hwnd, ref Guid pgActionID, ref WINTRUST_DATA pWVTData); + } +} diff --git a/CDPIUI.TrayIcon/Helper/PipeHelper.cs b/CDPIUI.TrayIcon/Helper/PipeHelper.cs index 408b432d..bb3b2855 100644 --- a/CDPIUI.TrayIcon/Helper/PipeHelper.cs +++ b/CDPIUI.TrayIcon/Helper/PipeHelper.cs @@ -138,7 +138,7 @@ public static async Task SendGoodCheckPacket(GoodCheckMessageIds messageId return await PipeServer.Instance.SendMessageAsync(model.ToString()); } - public static async Task SendGrantAcessPacket(bool result) + public static async Task SendGrantAccessPacket(bool result) { UtilsMessageModel model = new() { @@ -224,9 +224,14 @@ private static async Task TrySendMessage(MessageBaseModel message, b Path.Combine(Utils.GetDataDirectory(), "CDPIUI.exe"), $"{backgroundArgument}--direct:{message}"); } - else + else { - return RunHelper.RunAsDesktopUser(Path.Combine(Utils.GetDataDirectory(), "CDPIUI.exe"), $"----ms-protocol:{message.ToString()}"); + // ----ms-protocol: is the cdpiui:// link activation, which accepts public routes only, + // so window and notification messages passed that way were dropped. --direct: is the + // argument for messages from the tray. + return RunHelper.RunAsDesktopUser( + Path.Combine(Utils.GetDataDirectory(), "CDPIUI.exe"), + $"{(createNoWindow ? "--create-no-window " : string.Empty)}--direct:{message}"); } } diff --git a/CDPIUI.TrayIcon/Helper/ProcessManager.cs b/CDPIUI.TrayIcon/Helper/ProcessManager.cs index 43525b63..7a690a9d 100644 --- a/CDPIUI.TrayIcon/Helper/ProcessManager.cs +++ b/CDPIUI.TrayIcon/Helper/ProcessManager.cs @@ -1,5 +1,5 @@ using CDPIUI.Shared.ComponentsTask; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.TrayIcon.Helper.Basic; using Microsoft.Win32.SafeHandles; using System.Diagnostics; @@ -8,7 +8,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Shared.Pipe.Models; namespace CDPIUI.TrayIcon.Helper @@ -87,6 +87,7 @@ public async void StartProxy(string _proxyType, string ip, string port) _ip = ip; _port = port; proxyType = _proxyType; + useProxy = !string.IsNullOrEmpty(proxyType); Debug.WriteLine($"Proxy setup for {proxyType}, {ip}, {port}"); if (!string.IsNullOrEmpty(proxyType)) { @@ -113,11 +114,11 @@ public async Task StopProxy() public async Task StartProcess() { - if (useProxy) - StartProxy(proxyType, _ip, _port); - if (Executable != null && Args != null && !IsProcessInfoChanged) { + if (useProxy) + StartProxy(proxyType, _ip, _port); + await StartProcess(Executable, Args); } else @@ -176,32 +177,25 @@ public async Task RestartProcess() public static async Task StopService() { - string error = "NaN"; - try - { - await ExecuteCommand("sc", "stop WinDivert"); - } - catch (Exception ex) - { - error = ex.Message; - } - try + string sc = Path.Combine(Environment.SystemDirectory, "sc.exe"); + foreach (string command in new[] { "stop WinDivert", "delete WinDivert" }) { - await ExecuteCommand("sc", "delete WinDivert"); - } - catch (Exception ex) - { - - if (error != "NaN") Logger.Instance.CreateErrorLog(nameof(ProcessManager), $"Two or more errors occurred\n" + - $"[1/2]\n" + - $"{error}" + - $"[2/2]\n" + - $"{ex.Message}"); + try + { + var (exitCode, output) = await ExecuteCommand(sc, command); + // 1060: the service does not exist, 1062: it is not running. Other codes were + // printed to a console the tray does not have. + if (exitCode != 0 && exitCode != 1060 && exitCode != 1062) + Logger.Instance.CreateWarningLog(nameof(ProcessManager), $"sc {command} failed with {exitCode}: {output.Trim()}"); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(ProcessManager), $"Cannot run sc {command}: {ex.Message}"); + } } - } - private static async Task ExecuteCommand(string fileName, string arguments) + private static async Task<(int ExitCode, string Output)> ExecuteCommand(string fileName, string arguments) { using (Process process = new Process()) { @@ -220,18 +214,14 @@ private static async Task ExecuteCommand(string fileName, string arguments) process.StartInfo.CreateNoWindow = true; process.Start(); - string output = process.StandardOutput.ReadToEnd(); - string error = process.StandardError.ReadToEnd(); + // Both streams are read at once, so a full error pipe cannot block the process. + Task outputTask = process.StandardOutput.ReadToEndAsync(); + Task errorTask = process.StandardError.ReadToEndAsync(); await process.WaitForExitAsync(); + string output = await outputTask; + string error = await errorTask; - if (process.ExitCode != 0) - { - Console.WriteLine($"Unexpected error (Execute Command):\n{output}"); - } - else - { - Console.WriteLine("Success (Execute Command)"); - } + return (process.ExitCode, string.IsNullOrWhiteSpace(error) ? output : $"{output}\n{error}"); } } @@ -264,6 +254,10 @@ public void SendState() } } + // The system proxy is global, so the user's previous settings are kept once for all components. + private static readonly object SystemProxyLock = new(); + private static RegeditHelper.ProxySnapshot? _systemProxyBackup; + private void StartSystemProxy(string ip, string port) { string proxyServer = string.Empty; @@ -285,11 +279,19 @@ private void StartSystemProxy(string ip, string port) } try { - RegeditHelper.SaveProxySettings(proxyServer, string.Empty, 1); + lock (SystemProxyLock) + { + if (_systemProxyBackup == null) + { + _systemProxyBackup = RegeditHelper.ReadProxySnapshot(); + RegeditHelper.SaveSnapshotBackup(_systemProxyBackup); + } + RegeditHelper.SaveProxySettings(proxyServer, string.Empty, 1); + } } catch (Exception ex) { - ShowErrorMessage(Tuple.Create(ErrorHelper.Convertor.GetPrettyErrorCode(nameof(ConPTYHelper), ex).ToEnum(), nameof(RegeditHelper))); + ShowErrorMessage(Tuple.Create(ErrorHelper.Convertor.GetLoggedErrorCode(nameof(RegeditHelper), ex), nameof(RegeditHelper))); Logger.Instance.CreateErrorLog(nameof(ProcessManager), $"{ex.Message}"); } } @@ -298,11 +300,19 @@ private void StopSystemProxy() { try { - RegeditHelper.SaveProxySettings(string.Empty, string.Empty, 0); + lock (SystemProxyLock) + { + if (_systemProxyBackup != null) + RegeditHelper.RestoreProxySnapshot(_systemProxyBackup); + else + RegeditHelper.SaveProxySettings(string.Empty, string.Empty, 0); + _systemProxyBackup = null; + RegeditHelper.DeleteSnapshotBackup(); + } } catch (Exception ex) { - ShowErrorMessage(Tuple.Create(ErrorHelper.Convertor.GetPrettyErrorCode(nameof(RegeditHelper), ex).ToEnum(), nameof(RegeditHelper))); + ShowErrorMessage(Tuple.Create(ErrorHelper.Convertor.GetLoggedErrorCode(nameof(RegeditHelper), ex), nameof(RegeditHelper))); Logger.Instance.CreateErrorLog(nameof(ProcessManager), $"{ex.Message}"); } } @@ -324,12 +334,12 @@ private void SendOutput(string output) { "output", $"{output}" } }); } - private void ChangeProcessState(bool isRunned) + private void ChangeProcessState(bool isRunning) { - ProcessStateChanged?.Invoke(Tuple.Create(Id,isRunned)); + ProcessStateChanged?.Invoke(Tuple.Create(Id,isRunning)); - Debug.WriteLine($"Process state is {isRunned}"); - if (isRunned == false) + Debug.WriteLine($"Process state is {isRunning}"); + if (isRunning == false) { _ = PipeHelper.SendConPTYPacket(CONPTYMessageIds.MarkProcessIdAsStopped, Id); _ = StopProxy(); @@ -364,10 +374,10 @@ private void PFSendOutput(string output) { "output", $"{output}" } }); } - private void PFChangeProcessState(bool isRunned) + private void PFChangeProcessState(bool isRunning) { - ProcessStateChanged?.Invoke(Tuple.Create(Id, isRunned)); - if (isRunned == false) _ = conPTYHelper.StopProcess(); + ProcessStateChanged?.Invoke(Tuple.Create(Id, isRunning)); + if (isRunning == false) _ = conPTYHelper.StopProcess(); } private void PFShowErrorMessage(Tuple tuple) { diff --git a/CDPIUI.TrayIcon/Helper/Settings/AutoStartManager.cs b/CDPIUI.TrayIcon/Helper/Settings/AutoStartManager.cs index c3293c5e..415cd0d0 100644 --- a/CDPIUI.TrayIcon/Helper/Settings/AutoStartManager.cs +++ b/CDPIUI.TrayIcon/Helper/Settings/AutoStartManager.cs @@ -11,7 +11,7 @@ namespace CDPIUI.TrayIcon.Helper { public static class AutoStartManager { - private const string TaskName = "CDPIUI_Autostart"; + internal const string TaskName = "CDPIUI_Autostart"; public static bool AddToAutorun() { diff --git a/CDPIUI.TrayIcon/Helper/TasksHelper.cs b/CDPIUI.TrayIcon/Helper/TasksHelper.cs index 26ce0ae6..22d19a79 100644 --- a/CDPIUI.TrayIcon/Helper/TasksHelper.cs +++ b/CDPIUI.TrayIcon/Helper/TasksHelper.cs @@ -21,18 +21,22 @@ public static TasksHelper Instance public TasksHelper() { } - public void SendAllTasksOutput() + // The snapshot: tasks may be added or removed while the messages are sent. + public async Task SendTasksOutput(string? id = null) { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { - task.ProcessManager.SendDefaultProcessOutput(); + if (string.IsNullOrEmpty(id) || task.Id == id) + task.ProcessManager.SendDefaultProcessOutput(); } } - public void SendAllTasksState() + public async Task SendTasksState(string? id = null) { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { + if (!string.IsNullOrEmpty(id) && task.Id != id) + continue; task.ProcessManager.SendState(); task.ProcessManager.SendNowSelectedComponentName(); } @@ -74,11 +78,13 @@ public async void CleanProxyOnTask(string id) } - List serviceUsedComponentsIds = ["CSGIVS036", "CSZTBN012"]; + // GoodbyeDPI, Zapret and Zapret2 use the WinDivert driver; Zapret2 was missing, so the + // service could be deleted under a running winws2. + List serviceUsedComponentsIds = ["CSGIVS036", "CSZTBN012", "CSZTBN062"]; public async Task StopService() { - foreach (var task in Tasks) + foreach (var task in await GetTasksSnapshot()) { if (serviceUsedComponentsIds.Contains(task.Id)) { diff --git a/CDPIUI.TrayIcon/Helper/TrustedPaths.cs b/CDPIUI.TrayIcon/Helper/TrustedPaths.cs new file mode 100644 index 00000000..2803910f --- /dev/null +++ b/CDPIUI.TrayIcon/Helper/TrustedPaths.cs @@ -0,0 +1,60 @@ +using CDPIUI.Shared.Basic.Filesystem; +using CDPIUI.TrayIcon.Helper.Basic; +using Microsoft.Win32; + +namespace CDPIUI.TrayIcon.Helper +{ + /// + /// Limits the files that the UI may ask the elevated tray to run, install or change access to + /// + public static class TrustedPaths + { + // The installation directory and the data directory of MSI builds for the tray user and, + // when the tray was elevated with another account, for the desktop user that runs the UI. + private static IEnumerable Roots => + [ + Utils.GetDataDirectory(), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CDPIUI"), + GetDesktopUserDataDirectory(), + ]; + + public static string? GetDesktopUserDataDirectory() + { + try + { + var sid = PipeAccessPolicy.GetDesktopUserSid(); + if (sid == null) + return null; + + using RegistryKey? profile = Registry.LocalMachine.OpenSubKey( + $@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{sid.Value}"); + if (profile?.GetValue("ProfileImagePath") is not string profilePath) + return null; + + return Path.Combine(Environment.ExpandEnvironmentVariables(profilePath), "AppData", "Local", "CDPIUI"); + } + catch + { + return null; + } + } + + public static bool IsAllowed(string? path, string operation) + { + if (TrustedPathValidator.IsInside(path, Roots)) + return true; + + Logger.Instance.CreateWarningLog(nameof(TrustedPaths), $"Rejected {operation} for \"{path}\": the path is outside the application directories"); + return false; + } + + public static bool IsInStore(string? path, string operation) + { + if (TrustedPathValidator.IsInside(path, Roots.Select(root => root == null ? null : Path.Combine(root, "Store")))) + return true; + + Logger.Instance.CreateWarningLog(nameof(TrustedPaths), $"Rejected {operation} for \"{path}\": the path is outside the store directory"); + return false; + } + } +} diff --git a/CDPIUI.TrayIcon/Helper/UninstallHelper.cs b/CDPIUI.TrayIcon/Helper/UninstallHelper.cs new file mode 100644 index 00000000..e482904a --- /dev/null +++ b/CDPIUI.TrayIcon/Helper/UninstallHelper.cs @@ -0,0 +1,203 @@ +using CDPIUI.Shared.Basic.Filesystem; +using CDPIUI.TrayIcon.Helper.Basic; +using Microsoft.Win32; +using Microsoft.Win32.TaskScheduler; +using System.Diagnostics; + +namespace CDPIUI.TrayIcon.Helper +{ + /// + /// Shuts the application down before the MSI package removes its files + /// + public static class UninstallHelper + { + public const string CleanupArgument = "--uninstall-cleanup"; + + // Set by the uninstaller; every running tray then exits as if Exit was chosen in its menu, + // which stops the components and restores the system proxy settings. + private const string ShutdownEventName = @"Global\CDPIUI_TrayShutdown"; + private static readonly TimeSpan TrayExitTimeout = TimeSpan.FromSeconds(20); + + private static EventWaitHandle? _shutdownEvent; + private static RegisteredWaitHandle? _shutdownWait; + + public static void ListenForShutdown(Control invoker, System.Action exit) + { + try + { + _shutdownEvent = new EventWaitHandle(false, EventResetMode.ManualReset, ShutdownEventName); + _shutdownWait = ThreadPool.RegisterWaitForSingleObject(_shutdownEvent, (_, _) => + { + try { invoker.BeginInvoke(exit); } catch { } + }, null, Timeout.Infinite, executeOnlyOnce: true); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(UninstallHelper), $"Cannot listen for uninstall: {ex.Message}"); + } + } + + /// + /// Runs from the uninstaller as SYSTEM. Does not touch settings or logs, because they would be + /// created in the system profile. + /// + public static void Cleanup() + { + string installDirectory = AppContext.BaseDirectory; + int selfId = Environment.ProcessId; + + EventWaitHandle? shutdownEvent = null; + try + { + if (EventWaitHandle.TryOpenExisting(ShutdownEventName, out shutdownEvent)) + { + shutdownEvent.Set(); + WaitForTrayExit(selfId); + } + + KillApplicationProcesses(installDirectory, selfId); + RemoveDrivers(installDirectory); + RemoveAutorunTask(); + RemoveHostsBlock(installDirectory); + RemoveRuntimeFiles(installDirectory); + } + finally + { + try { shutdownEvent?.Reset(); } catch { } + shutdownEvent?.Dispose(); + } + } + + private static void WaitForTrayExit(int selfId) + { + var stopwatch = Stopwatch.StartNew(); + string trayName = Path.GetFileNameWithoutExtension(Environment.ProcessPath) ?? "CDPIUI_TrayIcon"; + while (stopwatch.Elapsed < TrayExitTimeout) + { + Process[] trays = Process.GetProcessesByName(trayName); + bool running = trays.Any(p => p.Id != selfId); + foreach (var p in trays) p.Dispose(); + if (!running) return; + + Thread.Sleep(250); + } + } + + private static void KillApplicationProcesses(string installDirectory, int selfId) + { + foreach (Process process in Process.GetProcesses()) + { + using (process) + { + if (process.Id == selfId) + continue; + + string? path; + try { path = process.MainModule?.FileName; } + catch { continue; } + + if (!ApplicationPathMatcher.BelongsToApplication(path, installDirectory)) + continue; + + try + { + process.Kill(); + process.WaitForExit(5000); + } + catch { } + } + } + } + + private static void RemoveDrivers(string installDirectory) + { + using RegistryKey? services = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services"); + if (services == null) + return; + + foreach (string name in services.GetSubKeyNames()) + { + if (!name.StartsWith("WinDivert", StringComparison.OrdinalIgnoreCase)) + continue; + + string? imagePath; + using (RegistryKey? service = services.OpenSubKey(name)) + { + imagePath = service?.GetValue("ImagePath") as string; + } + + // A driver installed by other software stays. + if (!ApplicationPathMatcher.BelongsToApplication(imagePath, installDirectory)) + continue; + + RunServiceControl($"stop \"{name}\""); + RunServiceControl($"delete \"{name}\""); + } + } + + private static void RunServiceControl(string arguments) + { + try + { + using var process = Process.Start(new ProcessStartInfo(Path.Combine(Environment.SystemDirectory, "sc.exe"), arguments) + { + UseShellExecute = false, + CreateNoWindow = true, + }); + process?.WaitForExit(15000); + } + catch { } + } + + private static void RemoveHostsBlock(string installDirectory) + { + string editor = Path.Combine(installDirectory, "EditHostFile.exe"); + if (!File.Exists(editor)) + return; + + try + { + using var process = Process.Start(new ProcessStartInfo(editor, "/remove") + { + UseShellExecute = false, + CreateNoWindow = true, + }); + process?.WaitForExit(15000); + } + catch { } + } + + // Files the application creates next to the installed ones; the package does not know them, + // so the installation directory would otherwise stay behind. + private static readonly string[] RuntimeDirectories = ["Logs", ".update-staging", ".update-backup", ".old", ".staging"]; + private static readonly string[] RuntimeFiles = ["update.log", ".update-manifest"]; + + private static void RemoveRuntimeFiles(string installDirectory) + { + foreach (string name in RuntimeDirectories) + { + try + { + string path = Path.Combine(installDirectory, name); + if (Directory.Exists(path)) Directory.Delete(path, true); + } + catch { } + } + foreach (string name in RuntimeFiles) + { + try { File.Delete(Path.Combine(installDirectory, name)); } + catch { } + } + } + + private static void RemoveAutorunTask() + { + try + { + using var taskService = new TaskService(); + taskService.RootFolder.DeleteTask(AutoStartManager.TaskName, false); + } + catch { } + } + } +} diff --git a/CDPIUI.TrayIcon/Helper/Utils.cs b/CDPIUI.TrayIcon/Helper/Utils.cs index d7f51575..3b9544b7 100644 --- a/CDPIUI.TrayIcon/Helper/Utils.cs +++ b/CDPIUI.TrayIcon/Helper/Utils.cs @@ -31,6 +31,25 @@ public static string GetDataDirectory() } } + // The MSI writes its install folder here (NewSetup/Product.wxs); the GUI uses the same check. + private static bool IsInstalledByMsi() + { + try + { + using var key = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64) + .OpenSubKey(@"Software\Storik4\CDPI UI"); + return key?.GetValue("InstallLocation") is string location && + !string.IsNullOrWhiteSpace(location) && + string.Equals(Path.GetFullPath(location).TrimEnd(Path.DirectorySeparatorChar), + Path.GetFullPath(AppContext.BaseDirectory).TrimEnd(Path.DirectorySeparatorChar), + StringComparison.OrdinalIgnoreCase); + } + catch (Exception) + { + return false; + } + } + public static string GetSettingsFile() { string filePath = Path.Combine(GetDataDirectory(), "Settings", "Settings.xml"); @@ -41,6 +60,16 @@ public static string GetSettingsFile() } else { + // The UI keeps its settings in the profile of the desktop user, which differs from + // the tray profile when the tray was elevated with another administrator account. + string? desktopUserFolder = TrustedPaths.GetDesktopUserDataDirectory(); + if (desktopUserFolder != null) + { + string desktopUserFile = Path.Combine(desktopUserFolder, "Settings", "Settings.xml"); + if (File.Exists(desktopUserFile)) + return desktopUserFile; + } + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); var targetFolder = Path.Combine(localAppData, "CDPIUI"); string localAppDataFile = Path.Combine(targetFolder, "Settings", "Settings.xml"); @@ -50,22 +79,53 @@ public static string GetSettingsFile() return localAppDataFile; } - return filePath; + // No settings yet. An MSI installation keeps the GUI settings in the profile of the + // desktop user; a file created next to the tray would later win over them. + if (desktopUserFolder != null && IsInstalledByMsi()) + return Path.Combine(desktopUserFolder, "Settings", "Settings.xml"); + + return filePath; } } - public static void StartUpdate(string targetFile) + public static async void StartUpdate(string targetFile) { - if (Path.GetExtension(targetFile).ToLower() == ".msi") + if (!TrustedPaths.IsAllowed(targetFile, "application update")) + { + NotifyHelper.ShowMessage("CDPI UI", LocaleHelper.GetLocaleString("UpdateFailure"), "UPDATE:OPEN_LOG"); + return; + } + + // The installer runs from a copy the user cannot change, checked where possible. + string stagedFile; + try + { + stagedFile = PackageStaging.Stage(targetFile); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(Utils), $"Cannot copy the update package: {ex.Message}"); + NotifyHelper.ShowMessage("CDPI UI", LocaleHelper.GetLocaleString("UpdateFailure"), "UPDATE:OPEN_LOG"); + return; + } + + if (string.Equals(Path.GetExtension(stagedFile), ".msi", StringComparison.OrdinalIgnoreCase)) { - RunHelper.Run("msiexec.exe", $"/i \"{targetFile}\" /qn+"); + // Published Setup.msi files are not Authenticode-signed, so there is no signature to check yet. + RunHelper.Run(Path.Combine(Environment.SystemDirectory, "msiexec.exe"), $"/i \"{stagedFile}\" /qn+"); _ = PipeHelper.SendApplicationPacket(ApplicationMessageIds.CloseApplicationUI); NotifyHelper.Instance.Dispose(); Application.Exit(); } else { - RunHelper.Run(Path.Combine(GetDataDirectory(), "Update.exe"), $"--directory-to-zip \"{targetFile}\" --destination-directory \"{GetDataDirectory()}\""); + if (!await PackageStaging.VerifyUpdatePackageAsync(stagedFile)) + { + PackageStaging.Release(stagedFile); + NotifyHelper.ShowMessage("CDPI UI", LocaleHelper.GetLocaleString("UpdateFailure"), "UPDATE:OPEN_LOG"); + return; + } + RunHelper.Run(Path.Combine(GetDataDirectory(), "Update.exe"), $"--directory-to-zip \"{stagedFile}\" --destination-directory \"{GetDataDirectory()}\""); } } @@ -88,6 +148,12 @@ public static float GetScalingFactorForMainDisplay() public static async void GrantAccess(string file, bool conptySignal) { + if (!TrustedPaths.IsInStore(file, "access change")) + { + if (conptySignal) await PipeHelper.SendGrantAccessPacket(false); + return; + } + try { bool exists = System.IO.Directory.Exists(file); @@ -97,24 +163,29 @@ public static async void GrantAccess(string file, bool conptySignal) } DirectoryInfo dInfo = new DirectoryInfo(file); DirectorySecurity dSecurity = dInfo.GetAccessControl(); + // Only the user the tray runs for gets write access: these directories hold + // the executables that the tray starts elevated. + SecurityIdentifier user = WindowsIdentity.GetCurrent().User + ?? throw new InvalidOperationException("Cannot get the current user."); dSecurity.AddAccessRule( new FileSystemAccessRule( - new SecurityIdentifier(WellKnownSidType.WorldSid, null), - FileSystemRights.FullControl, + user, + FileSystemRights.Modify, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, - PropagationFlags.NoPropagateInherit, + PropagationFlags.None, AccessControlType.Allow ) ); dInfo.SetAccessControl(dSecurity); - if (conptySignal) await PipeHelper.SendGrantAcessPacket(true); + if (conptySignal) await PipeHelper.SendGrantAccessPacket(true); + return; } catch (Exception ex) { Logger.Instance.CreateErrorLog(nameof(Utils), $"Cannot grant access for \"{file}\". Exception message: {ex.Message}"); } - if (conptySignal) await PipeHelper.SendGrantAcessPacket(false); + if (conptySignal) await PipeHelper.SendGrantAccessPacket(false); } } } diff --git a/CDPIUI.TrayIcon/Program.cs b/CDPIUI.TrayIcon/Program.cs index ddd5189c..6ed057de 100644 --- a/CDPIUI.TrayIcon/Program.cs +++ b/CDPIUI.TrayIcon/Program.cs @@ -1,4 +1,5 @@ -using CDPIUI.TrayIcon.Helper.Basic; +using CDPIUI.Shared.Basic.CommandLine; +using CDPIUI.TrayIcon.Helper.Basic; using CDPIUI.TrayIcon.Forms; using CDPIUI.TrayIcon.Helper; using Microsoft.Toolkit.Uwp.Notifications; @@ -15,10 +16,44 @@ class Programm { + private static Mutex? _singleInstanceMutex; + + private static bool TryAcquireSingleInstance() + { + _singleInstanceMutex = new Mutex(false, @"Global\CDPIUI_TrayInstance"); + try + { + return _singleInstanceMutex.WaitOne(TimeSpan.FromSeconds(10)); + } + catch (AbandonedMutexException) + { + // The previous tray ended without releasing it, for example during an update. + return true; + } + } [STAThread] static void Main(string[] args) { + if (args.Contains(UninstallHelper.CleanupArgument)) + { + UninstallHelper.Cleanup(); + return; + } + + // One tray per machine: the pipe name is global, and a second tray renamed _Update.exe, + // started the GUI and registered hot keys before it failed on the pipe. + if (!TryAcquireSingleInstance()) + { + Logger.Instance.CreateWarningLog("Startup", "Another tray is already running; exiting."); + return; + } + + // An exception in a UI event handler is logged instead of ending the tray, which owns + // the running components and the system proxy. + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += (_, e) => + Logger.Instance.CreateErrorLog("Tray", $"Unhandled UI exception: {e.Exception}"); AppDomain.CurrentDomain.UnhandledException += (_, e) => Logger.Instance.CreateDebugLog("Startup", $"Unhandled exception; terminating={e.IsTerminating}: {e.ExceptionObject}"); @@ -34,6 +69,9 @@ static void Main(string[] args) ToastNotificationManagerCompat.History.Clear(); + PackageStaging.CleanUp(); + RegeditHelper.RestoreLeftoverSnapshot(); + PipeServer.Instance.Init(); PipeServer.Instance.Start(); @@ -48,9 +86,13 @@ static void Main(string[] args) if (args.Contains("--show-ui") || args.Contains("--after-patching") || args.Contains("--after-failed-update") || migrationRequest != null) { + // Arguments of a GUI start that had to start the tray first. + string guiCommandLine = GuiActivationArguments.TryDecode(args, out var guiArgs) + ? GuiActivationArguments.JoinCommandLine(guiArgs) + : migrationRequest?.RawArgument ?? string.Empty; RunHelper.RunAsDesktopUser( Path.Combine(Utils.GetDataDirectory(), "CDPIUI.exe"), - migrationRequest?.RawArgument ?? string.Empty); + guiCommandLine); } string updateFilePath = Path.Combine(Utils.GetDataDirectory(), "Update.exe"); @@ -106,6 +148,7 @@ public TrayApplicationContext(bool runStartupActions = false) _trayForm = new EmptyForm(); _trayForm.AddIcon(notify: true); + UninstallHelper.ListenForShutdown(_trayForm, Application.Exit); try { ConditionalLaunchEngine.Instance.Start(_trayForm); diff --git a/CDPIUI.TrayIcon/README.md b/CDPIUI.TrayIcon/README.md new file mode 100644 index 00000000..9e697ccb --- /dev/null +++ b/CDPIUI.TrayIcon/README.md @@ -0,0 +1,229 @@ +# CDPIUI.TrayIcon + +`CDPIUI.TrayIcon` is the background process of CDPI UI (`CDPIUI_TrayIcon.exe`). +It is a WinForms application for `net8.0-windows10.0.17763.0`, x64. +`app.manifest` sets `requireAdministrator`, so the tray always runs elevated. +The GUI runs as the desktop user and sends commands to the tray through a named pipe. +The pipe protocol is in [CDPIUI.Shared/Pipe/README.md](../CDPIUI.Shared/Pipe/README.md). + +## Responsibilities + +- Show the notification area icon and its menu. +- Start, stop and restart the components (Zapret, ByeDPI, GoodbyeDPI and others) through ConPTY. +- Stop the components when the tray process ends. +- Set and restore the system proxy. +- Add the tray to autorun and remove it. +- Run Conditional Launch tasks. +- Show toast notifications. +- Install MSI and EXE packages and application updates. +- Run GoodCheck and grant directory access for the GUI. +- Clean up before the MSI package uninstalls the application. + +## Startup + +`Program.cs` (`Programm.Main`) does these steps: + +1. If the arguments contain `--uninstall-cleanup`, it runs `UninstallHelper.Cleanup` and exits. +2. It gets the mutex `Global\CDPIUI_TrayInstance`. It waits 10 seconds. If another tray has the mutex, it exits. An abandoned mutex is accepted. +3. It deletes the `.staging` directory (`PackageStaging.CleanUp`). +4. It restores a proxy backup that a previous tray left (`RegeditHelper.RestoreLeftoverSnapshot`). +5. It starts the pipe server. +6. If a toast notification started the process, it shows only the icon and handles the toast. +7. If the arguments request it, it starts the GUI as the desktop user (see the table below). +8. It renames `_Update.exe` to `Update.exe`, if `_Update.exe` exists. +9. It shows the notifications for update failure and for `--autorun`. +10. After 30 minutes, it asks the GUI to check for updates (`NOTIFICATIONS/appUpdates`). +11. After 60 minutes, it asks the GUI to start the compatibility check (`NOTIFICATIONS/compatibilityCheck`). +12. It creates `TrayApplicationContext`: the icon, the uninstall listener, the Conditional Launch engine and the notification handler. + +If the pipe server cannot create the pipe and another tray process runs, the tray stops its own process. + +### Command-line arguments + +| Argument | Effect | +|---|---| +| `--uninstall-cleanup` | Runs the uninstall cleanup and exits. | +| `--autorun` | Asks the GUI for the startup strings of all autorun components (`GetAllStartupStrings`). It tries again each 5 seconds until the send succeeds. It shows the "minimized to tray" notification if `NOTIFICATIONS/trayHide` is `true`. | +| `--show-ui` | Starts the GUI. | +| `--gui-args:` | With `--show-ui`: the arguments for the GUI. `GuiActivationArguments` decodes them. | +| `--gdpiui-migration=` | Starts the GUI with this argument. | +| `--after-patching` | Starts the GUI. If `_Update.exe` cannot be renamed, it shows an update failure notification. | +| `--after-failed-update` | Starts the GUI and shows an update failure notification. | + +The GUI starts the tray with `--autorun` or `--show-ui` when the GUI cannot connect to the pipe. +The autorun task starts the tray with `--autorun`. + +## Folders + +| Folder | Content | +|---|---| +| `ConditionalLaunch/` | `ConditionalLaunchEngine` (triggers) and `ConditionalActionExecutor` (actions) | +| `Controls/` | Custom WinForms controls of the tray menu | +| `Forms/` | `EmptyForm` (hidden window that owns the icon), `TrayMenuForm` (the menu) | +| `Helper/` | `ProcessManager`, `TasksHelper`, `PipeHelper`, `MsiInstallerHelper`, `PackageStaging`, `TrustedPaths`, `UninstallHelper`, `GoodCheckProcessHelper`, `StartupActionDispatcher`, `Utils` | +| `Helper/Basic/` | Pipe server, command handler, ConPTY, job object, run helper, registry, notifications, logger, error helper, taskbar | +| `Helper/Locale/` | `LocaleHelper`: tray strings in English and Russian, component names | +| `Helper/Settings/` | `SettingsManager` (`XMLSettingsService`), `AutoStartManager` | +| `Assets/` | Icons (embedded resources) | + +## Components and ConPTY + +- `TasksHelper` is a `TasksManageService`. Each component id has one task. +- `GetProcessIdFullOutput` and `GetProcessIdState` apply to the task of the `componentId` in the message. If `componentId` is empty, they apply to all tasks. `GetAllProcessStates` applies to all tasks. `TasksHelper.SendTasksOutput` and `TasksHelper.SendTasksState` read a snapshot of the task list. +- `ProcessManager` keeps the executable and the arguments of the last start. A restart uses them again, unless the GUI sent `ProcessIdStartupArgsChanged`. In that case, the tray sends `GetStartupString` to the GUI. +- `ConPTYHelper` starts the process in a pseudo console with `CreateProcessW` and sends the output to the GUI. +- `ConPTYHelper` changes known output lines to a `PrettyErrorCode`. The first match of a line is used. +- `ChildProcessJob` puts each component into one job object with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`. Windows stops the components when the tray process ends, also after a crash. +- GoodCheck runs in its own kill-on-close job (`GoodCheckProcessHelper`). The tray stops all components before it starts GoodCheck. The tray stops GoodCheck when the GUI disconnects. +- `StopService` stops the components that use WinDivert (`CSGIVS036`, `CSZTBN012`, `CSZTBN062`). Then it runs `sc stop WinDivert` and `sc delete WinDivert`. +- When the computer resumes, the tray waits up to 30 seconds for the network. Then it restarts the components that were running. +- On exit and on session end, the tray stops all components. It waits up to 5 seconds. + +### Messages to a closed GUI + +If the tray cannot send a message, `PipeHelper.TrySendMessage` starts `\CDPIUI.exe` as the desktop user with `--direct:`. +This applies to all message targets. For background messages, the tray also adds `--create-no-window`. +See [CDPIUI.Shared/Pipe/README.md](../CDPIUI.Shared/Pipe/README.md#messages-when-the-gui-is-not-connected). + +## Proxy + +`ProxyMessageIds.Setup` sets the proxy for a component. `proxyType` can have two values: + +- `ProxiFyre`: the tray starts ProxiFyre (from `Init`) in a second ConPTY. +- `AllSystem`: the tray writes `socks=:` to the Internet settings of the desktop user. + +The tray writes to `HKEY_USERS\` when the desktop user is not the tray user. Otherwise it writes to `HKEY_CURRENT_USER`. + +Backup and restore: + +1. Before the first change, the tray reads `ProxyEnable`, `ProxyServer` and `ProxyOverride`. +2. It writes this snapshot to `proxy-backup.json` in the tray directory. +3. When the component stops, the tray restores the snapshot and deletes the file. +4. If the tray did not restore the snapshot (crash, kill, shutdown), the next tray start restores it. + +When one component starts a proxy, the tray stops the other components that use a proxy. + +## Autorun + +`AutoStartManager` creates the Task Scheduler task `CDPIUI_Autostart`: + +- Trigger: logon of the current user. +- Action: the tray executable with `--autorun`. +- Run level: highest. Logon type: interactive token. +- No execution time limit. The task also runs on battery power. + +If `AddToAutorun` fails, the tray sends `Settings/AutorunFalse` to the GUI and shows a notification. + +## Conditional Launch + +`ConditionalLaunchEngine` reads the `.cdpitask` files from the tasks directory. +The default directory comes from the settings file path (`ConditionalTaskFileService.GetTasksDirectoryFromSettingsFile`). +The GUI can send a different directory in `ReloadTasks`. The tray accepts it only if `TrustedPaths.IsAllowed` accepts it. + +- A `FileSystemWatcher` reloads the tasks when a file changes. After a watcher error, the engine creates a new watcher. +- Hot key triggers use `RegisterHotKey` on the `EmptyForm` window. +- Process triggers poll the process list each second. +- When more than one task matches, the task with the highest priority runs. +- Actions that need the GUI (`ApplyPreset`, `StartComponent`, `ToggleComponent`, `StartAutorunComponents`, `CheckStoreUpdates`, `CheckApplicationUpdates`) go to the GUI as `ExecuteAction`. The tray waits 45 seconds for the result. +- The tray does the other actions itself, or it opens a GUI window. + +## Notifications + +`NotifyHelper.ShowMessage` shows a toast notification with an `action` argument. +`HandleToastActionFromBackground` handles the action when the user clicks the toast: + +| Action | Effect | +|---|---| +| `SHOW_MAIN_WINDOW` | Opens `MainWindow`. | +| `OPEN_PSEUDOCONSOLE()` | Opens `ViewWindow` for the component. | +| `OPEN_PROXY_SETUP` | Opens `ProxySetupUtilWindow`. | +| `OPEN_BEGIN_STORE_UPDATE_CHECK` | Opens the Store download page. | +| `UPDATE:OPEN_DOWNLOAD_PAGE` | Opens the update page of `MainWindow`. | +| `LOGGER:OPEN_TRAY_LOG`, `LOGGER:OPEN_MSI_LOG`, `UPDATE:OPEN_LOG` | Opens a log file. | + +Component start and stop notifications show only if `NOTIFICATIONS/procState` is `true`. + +## Packages and updates + +### MSI and EXE packages + +`MsiInstallerHelper` installs packages one at a time from a queue (`MSIInstallation/Begin`): + +1. The path must be accepted by `TrustedPaths.IsAllowed`. +2. `PackageStaging.Stage` copies the file to `\.staging\\`. +3. The copy must have a valid Authenticode signature (`WinVerifyTrust`, no revocation check). +4. The tray runs `msiexec.exe /i "" /passive /norestart` for `.msi`. For other files, it runs ` /install /passive /norestart`. +5. Exit codes 0 and 1638 give `Complete`. Exit codes 3010 and 1641 give `CompleteRestartRequest`. Other codes give `ExceptionHappens`. +6. The tray deletes the staging directory. + +`MSIInstallation/Kill` stops the running installer or removes the operation from the queue. + +### Application update + +`Utils.StartUpdate` handles `Update/BeginApplicationUpdate`: + +1. The path must be accepted by `TrustedPaths.IsAllowed`. +2. The tray copies the package to `.staging`. +3. For an `.msi` package, it runs `msiexec.exe /i "" /qn+`, closes the GUI and exits. The published `Setup.msi` has no signature, so there is no signature check. +4. For a ZIP package, it verifies the `catalog.cat` of each layer with `CatalogVerifier` and `Secret.TrustedCertificates`. Then it runs `Update.exe --directory-to-zip "" --destination-directory ""`. + +## Uninstall cleanup + +The MSI uninstaller runs `CDPIUI_TrayIcon.exe --uninstall-cleanup` as SYSTEM. `UninstallHelper.Cleanup` does these steps: + +1. It sets the event `Global\CDPIUI_TrayShutdown`. Each running tray exits as if the user clicked Exit, so it stops the components and restores the proxy. +2. It waits up to 20 seconds for the trays to exit. +3. It stops the processes that `ApplicationPathMatcher` finds in the installation directory or in `\AppData\Local\CDPIUI\`. +4. It stops and deletes the `WinDivert*` services whose `ImagePath` belongs to the application. +5. It deletes the `CDPIUI_Autostart` task. +6. It runs `EditHostFile.exe /remove`. +7. It deletes `Logs`, `.update-staging`, `.update-backup`, `.old`, `.staging`, `update.log` and `.update-manifest` in the installation directory. + +The cleanup does not change settings or logs, because the SYSTEM profile is not the user profile. + +## Settings and logs + +- `SettingsManager` uses the first `Settings.xml` that exists: + 1. `\Settings\Settings.xml` + 2. `%LOCALAPPDATA%\CDPIUI\Settings\Settings.xml` of the desktop user + 3. `%LOCALAPPDATA%\CDPIUI\Settings\Settings.xml` of the tray user +- If no file exists, the tray selects the path of a new file: + - For an MSI installation, the tray uses `%LOCALAPPDATA%\CDPIUI\Settings\Settings.xml` of the desktop user. The GUI of an MSI installation uses the same file. + - In all other conditions, the tray uses item 1. +- The tray finds an MSI installation with the registry value `HKLM\Software\Storik4\CDPI UI\InstallLocation` (`Utils.GetSettingsFile`). The value must be equal to the tray directory. The GUI uses the same check (`CDPIUI.Core/State.cs`). +- The GUI sends `Settings/ReloadSettings` after it changes a setting that the tray reads. +- The tray writes logs to `\Logs\`. `DEBUG` builds write all levels. Release builds use `DEBUG/logLevel` (default `WARN`). + +## Development + +You must start the tray elevated and the GUI as a normal user. Some GUI features do not work when the GUI is elevated. + +1. Create `CDPIUI.Shared/Secrets/Secret.cs` (see [CDPIUI.Shared/README.md](../CDPIUI.Shared/README.md#secrets)). +2. Start `CDPIUI.TrayIcon` as administrator. In Visual Studio, start Visual Studio as administrator. +3. Start `CDPIUI` as the normal desktop user. + +Notes: + +- In `DEBUG` builds, the pipe peer check compares only file names. Thus the GUI and the tray can run from different output directories. +- The tray starts only executables below its own directory or below `%LOCALAPPDATA%\CDPIUI` (`TrustedPaths`). To test components, run the GUI and the tray from the same directory. You can use the publish directory of `.github/scripts/build_portable.ps1`. +- The tray starts the GUI from `\CDPIUI.exe`. With different output directories, the tray cannot start the GUI. +- Two trays cannot run at the same time. Close the installed tray before you start a development build. + +## Security rules + +The tray is elevated, and the GUI is not. Treat each pipe message as data from a less trusted process. + +- Accept pipe clients only after the peer check and the `AuthGuid` authorization (see the pipe README). An empty `AuthGuid` authorizes no client. A failed authorization disconnects the client. +- Check each path from the GUI with `TrustedPaths` before you run, install or change it: + - `IsAllowed`: below the tray directory, `%LOCALAPPDATA%\CDPIUI` of the tray user, or `%LOCALAPPDATA%\CDPIUI` of the desktop user. + - `IsInStore`: below the `Store` directory of these roots. `GrantAccessRequest` uses it. + - `TrustedPathValidator` rejects a path that has a symbolic link or a junction below the root. +- Copy a package to `.staging` before you check it. Check and run the copy, not the original file. +- Use only signed packages: Authenticode for installers, catalogs with pinned thumbprints for application updates. +- Use full paths for system tools (`msiexec.exe`, `sc.exe`, `powershell.exe`, `explorer.exe`, `notepad.exe`). +- Start processes with `UseShellExecute = false` (`RunHelper.Run`). +- Do not use `ShellExecute` from the tray to open a file or a URL. File associations come from the user, so the handler can run elevated. Use `RunHelper.RunAsDesktopUser`, as `NotifyHelper.OpenFileInDefaultApp` does with `explorer.exe`. +- Start the GUI with `RunHelper.RunAsDesktopUser`. It uses the token of the shell process, so the GUI does not run elevated. +- `GrantAccess` gives `Modify` access only to the tray user. + +WARNING: Do not add a pipe handler that runs a file or changes access without a `TrustedPaths` check. diff --git a/CDPIUI/App.xaml.cs b/CDPIUI/App.xaml.cs index 6b2e69f2..e78cad49 100644 --- a/CDPIUI/App.xaml.cs +++ b/CDPIUI/App.xaml.cs @@ -61,6 +61,8 @@ public partial class App : Application private readonly Microsoft.UI.Dispatching.DispatcherQueue uiDispatcher = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); + public Microsoft.UI.Dispatching.DispatcherQueue UiDispatcher => uiDispatcher; + private sealed class DisabledOwner { public IntPtr Handle; @@ -78,10 +80,12 @@ private sealed class ModalSession public Windows.Foundation.TypedEventHandler ClosedHandler; } private readonly SemaphoreSlim migrationWindowActivationLock = new(1, 1); + private int isUnhandledErrorShown; public App() { this.InitializeComponent(); + RegisterUnhandledExceptionHandlers(); PipeClientService.Instance.MessageReceived += (m) => CommandsHandler.HandleCommand(m); CoreErrorsLookupService.Instance.Init(); @@ -95,6 +99,55 @@ public App() GetReadyFeatures(); } + private void RegisterUnhandledExceptionHandlers() + { + UnhandledException += App_UnhandledException; + AppDomain.CurrentDomain.UnhandledException += (_, e) => + Logger.Instance.CreateErrorLog(nameof(App), $"Fatal unhandled exception: {e.ExceptionObject}"); + TaskScheduler.UnobservedTaskException += (_, e) => + { + Logger.Instance.CreateErrorLog(nameof(App), $"Unobserved task exception: {e.Exception}"); + e.SetObserved(); + }; + } + + private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) + { + Logger.Instance.CreateErrorLog(nameof(App), $"Unhandled exception: {e.Exception?.ToString() ?? e.Message}"); + + // Exceptions from async void event handlers end up here. Keep the application + // running and show the error instead of closing every window without a trace. + e.Handled = true; + ShowUnhandledError(e.Exception); + } + + private async void ShowUnhandledError(Exception exception) + { + if (Interlocked.Exchange(ref isUnhandledErrorShown, 1) != 0) + return; + + try + { + XamlRoot xamlRoot = OpenWindows.LastOrDefault(w => w.Content?.XamlRoot != null)?.Content.XamlRoot; + if (xamlRoot == null) + return; + + string code = exception != null + ? ErrorsHelper.Convertor.GetPrettyErrorCode("APP", exception) + : "APP_UNKNOWN"; + await new Controls.Dialogs.Universal.ErrorContentDialog().ShowErrorDialogAsync( + Localizer.Get().GetLocalizedString("SomethingWentWrong"), code, xamlRoot); + } + catch (Exception ex) + { + Logger.Instance.CreateWarningLog(nameof(App), $"Cannot show unhandled exception: {ex.Message}"); + } + finally + { + Interlocked.Exchange(ref isUnhandledErrorShown, 0); + } + } + private void StoreStateChanged(bool isWorking) { try @@ -131,7 +184,7 @@ private async Task PipeConnectedActions() ComponentTasksManager.Instance.RequestComponentItemsInit(); bool isFileProcessed = false; - bool isActionPreffered = false; + bool isActionPreferred = false; bool isMigrationProcessed = false; try @@ -154,21 +207,21 @@ private async Task PipeConnectedActions() if (Program.InitialActivationArguments?.Kind is ExtendedActivationKind.Protocol && Program.InitialActivationArguments.Data is IProtocolActivatedEventArgs initialProtocolArgs) { - isActionPreffered = await CommandsHandler.HandleCommandAsync( + isActionPreferred = await CommandsHandler.HandleCommandAsync( initialProtocolArgs.Uri.ToString()); } if (!isFileProcessed) isFileProcessed = await ProcessFiles(arguments); - string protocolArg = isActionPreffered + string protocolArg = isActionPreferred ? null : arguments.FirstOrDefault(x => x.StartsWith("----ms-protocol:")); if (protocolArg != null) { string value = protocolArg["----ms-protocol:".Length..]; - isActionPreffered = await CommandsHandler.HandleCommandAsync(value); + isActionPreferred = await CommandsHandler.HandleCommandAsync(value); } string directArgs = arguments.FirstOrDefault(x => x.StartsWith("--direct:")); @@ -176,12 +229,12 @@ private async Task PipeConnectedActions() if (directArgs != null) { string value = directArgs["--direct:".Length..]; - isActionPreffered = await CoreCommandsHandler.HandleCommandAsync( + isActionPreferred = await CoreCommandsHandler.HandleCommandAsync( PipeModelConvertor.ConvertBack(value)); - if (!isActionPreffered) + if (!isActionPreferred) { - isActionPreffered = CommandsHandler.HandleCommand(value); + isActionPreferred = CommandsHandler.HandleCommand(value); } } } @@ -192,7 +245,7 @@ private async Task PipeConnectedActions() $"Cannot process startup activation: {ex}"); } - if (!isFileProcessed && !isActionPreffered && !isMigrationProcessed && + if (!isFileProcessed && !isActionPreferred && !isMigrationProcessed && !Program.IsMigrationActivationPending) await OpenStartupWindowAsync(); @@ -303,7 +356,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) PipeClientService.Instance.Start(); } - public async void PrefferRequestedActions(AppActivationArguments appActivationArguments) + public async void PreferRequestedActions(AppActivationArguments appActivationArguments) { bool result = false; try @@ -325,11 +378,20 @@ public async void PrefferRequestedActions(AppActivationArguments appActivationAr appActivationArguments.Data is ILaunchActivatedEventArgs fileActivatedEventArgs) { result = await TryHandleMigrationActivationAsync(fileActivatedEventArgs.Arguments); + string[] args = SplitCommandLine(fileActivatedEventArgs.Arguments); if (!result) { - string[] args = GetFilesFromStringRegex().Split(fileActivatedEventArgs.Arguments); - result = await ProcessFiles(args); + // The same --direct: commands as in a first start. + string directArgs = args.FirstOrDefault(x => x.StartsWith("--direct:")); + if (directArgs != null) + { + string value = directArgs["--direct:".Length..]; + result = await CoreCommandsHandler.HandleCommandAsync(PipeModelConvertor.ConvertBack(value)) + || CommandsHandler.HandleCommand(value); + } } + if (!result) + result = await ProcessFiles(args); } } catch (Exception ex) @@ -441,12 +503,27 @@ private async Task ShowSettingsRecoveryWarningAsync() } - public async void GetReadyFeatures() + // Windows read their strings from the localizer, so they are created only after it is built. + private Task _localizerReady = Task.CompletedTask; + + public void GetReadyFeatures() { - DatabaseInitializationService.QuickRestore(); - await InitializeLocalizer(); - ApplicationInfo.Instance.SetLocalization(Localizer.Get().GetCurrentLanguage()); - FileAssociationService.EnsureRegistered(); + _localizerReady = GetReadyFeaturesAsync(); + } + + private async Task GetReadyFeaturesAsync() + { + try + { + DatabaseInitializationService.QuickRestore(); + await InitializeLocalizer(); + ApplicationInfo.Instance.SetLocalization(Localizer.Get().GetCurrentLanguage()); + FileAssociationService.EnsureRegistered(); + } + catch (Exception ex) + { + Logger.Instance.CreateErrorLog(nameof(App), $"Cannot prepare the application: {ex}"); + } } public bool CheckWindow() where TWindow : Window @@ -463,12 +540,6 @@ public void ShowWindow() where TWindow : Window } } - public async void CreateEmptyWindow() - { - Window window = await SafeCreateNewWindow(); - window.Hide(); - } - public void CloseWindow() where TWindow : Window { foreach (var viewWindow in OpenWindows.OfType().ToList()) @@ -521,14 +592,16 @@ public static void ActivateWindow(Window window) } } + // Every overload waits for the localizer: windows opened by a link or a file during the + // start were built before their texts were available. public async Task UnsafeCreateNewWindow(Type windowType, bool activate = true, string id = "") { - var findWindows = OpenWindows.Where(w => windowType.IsInstanceOfType(w)).ToList(); - int findWindowCount = findWindows.Count; + await _localizerReady; - var activeFindWindow = findWindows.FirstOrDefault(w => w.DispatcherQueue != null); + var findWindows = OpenWindows.Where(w => windowType.IsInstanceOfType(w)).ToList(); - foreach (TemplateWindow _win in findWindows) + // Only template windows have an id; a cast of every window threw for other types. + foreach (TemplateWindow _win in findWindows.OfType()) { if (!string.IsNullOrEmpty(_win.Id) && string.Equals( _win.Id, @@ -552,6 +625,8 @@ public async Task UnsafeCreateNewWindow(Type windowType, bool activate = public async Task UnsafeCreateNewWindow(bool activate = true, string id = "") where TWindow : TemplateWindow, new() { + await _localizerReady; + var findWindows = OpenWindows.OfType().ToList(); int findWindowCount = findWindows.Count; @@ -580,6 +655,8 @@ public async Task UnsafeCreateNewWindow(Type windowType, bool activate = } public async Task SafeCreateNewWindow(Type windowType, bool activate = true) { + await _localizerReady; + var findWindows = OpenWindows.Where(w => windowType.IsInstanceOfType(w)).ToList(); int findWindowCount = findWindows.Count; @@ -613,6 +690,8 @@ public async Task SafeCreateNewWindow(Type windowType, bool activate = t public async Task SafeCreateNewWindow(bool activate = true) where TWindow : Window, new() { + await _localizerReady; + var findWindows = OpenWindows.OfType().ToList(); int findWindowCount = findWindows.Count; @@ -739,7 +818,7 @@ private void ExitIfIdle() } if (OpenWindows.Count != 0 || ApplicationTaskMonitor.IsStoreWorking() || - ApplicationTaskMonitor.IsGoodCheckRunned()) + ApplicationTaskMonitor.IsGoodCheckRunning()) { return; } @@ -948,7 +1027,10 @@ private async Task InitializeLocalizer() string lang = SettingsManager.Instance.GetValue("SYSTEM", "language"); var culture = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; - if (lang == "NaN") + // An unknown or damaged value would leave every text empty (issue #8). + if (string.IsNullOrWhiteSpace(lang) || lang == "NaN" || + lang.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || + !Directory.Exists(Path.Combine(stringsFolderPath, lang))) { lang = culture switch { @@ -993,7 +1075,35 @@ private async Task InitializeLocalizer() private const uint SWP_SHOWWINDOW = 0x0040; private const int GWLP_HWNDPARENT = -8; - [GeneratedRegex(@"""(.*?)\""")] - private static partial Regex GetFilesFromStringRegex(); + /// + /// Splits a command line the way the runtime splits the arguments of Main. File paths + /// with spaces and quoted arguments mixed with plain ones were split wrongly by a regex. + /// + private static string[] SplitCommandLine(string commandLine) + { + if (string.IsNullOrWhiteSpace(commandLine)) + return []; + + IntPtr argv = CommandLineToArgvW(commandLine, out int argc); + if (argv == IntPtr.Zero) + return []; + try + { + var result = new string[argc]; + for (int i = 0; i < argc; i++) + result[i] = Marshal.PtrToStringUni(Marshal.ReadIntPtr(argv, i * IntPtr.Size)) ?? string.Empty; + return result; + } + finally + { + LocalFree(argv); + } + } + + [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern IntPtr CommandLineToArgvW(string lpCmdLine, out int pNumArgs); + + [DllImport("kernel32.dll")] + private static extern IntPtr LocalFree(IntPtr hMem); } } diff --git a/CDPIUI/CDPIUI.csproj b/CDPIUI/CDPIUI.csproj index 83736740..64d5e69f 100644 --- a/CDPIUI/CDPIUI.csproj +++ b/CDPIUI/CDPIUI.csproj @@ -7,7 +7,9 @@ 10.0.17763.0 CDPIUI app.manifest - x86;x64 + + x64 win-x86;win-x64;win-arm64 win10-x86;win10-x64;win10-arm64 win-$(Platform).pubxml @@ -19,6 +21,8 @@ true en-US;ru-RU + + en-US;ru-RU embedded CDPIUI $(DefineConstants);DISABLE_XAML_GENERATED_MAIN @@ -36,7 +40,6 @@ - @@ -49,7 +52,6 @@ - @@ -58,9 +60,7 @@ - - @@ -75,17 +75,9 @@ - - - - - - - - @@ -106,15 +98,9 @@ - - - - - - @@ -165,7 +151,6 @@ - @@ -253,29 +238,22 @@ - - - - - - - @@ -292,31 +270,6 @@ - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - @@ -351,30 +304,15 @@ - - MSBuild:Compile - - - MSBuild:Compile - MSBuild:Compile MSBuild:Compile - - MSBuild:Compile - - - MSBuild:Compile - MSBuild:Compile - - MSBuild:Compile - MSBuild:Compile @@ -417,9 +355,6 @@ MSBuild:Compile - - MSBuild:Compile - MSBuild:Compile @@ -439,56 +374,16 @@ MSBuild:Compile - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - MSBuild:Compile - - - MSBuild:Compile - - MSBuild:Compile - - - MSBuild:Compile - - - - - MSBuild:Compile - - Static Assets\favicon.ico @@ -610,9 +505,6 @@ MSBuild:Compile - - MSBuild:Compile - MSBuild:Compile @@ -682,9 +574,6 @@ MSBuild:Compile - - PreserveNewest - PreserveNewest diff --git a/CDPIUI/Commands/CommandUriMapper.cs b/CDPIUI/Commands/CommandUriMapper.cs index a09e17ea..0ee5a31e 100644 --- a/CDPIUI/Commands/CommandUriMapper.cs +++ b/CDPIUI/Commands/CommandUriMapper.cs @@ -256,7 +256,13 @@ internal static class CommandUriMapper }) ]; - public static IPipeMessage? ConvertBack(string commandUri) + /// + /// Converts a cdpiui:// link to a command. Links from outside the application (browser, + /// other programs, help pages) may only use ; the internal pipe message + /// form is accepted only with , for links built by the + /// application itself. Otherwise any web page could forge tray responses. + /// + public static IPipeMessage? ConvertBack(string commandUri, bool allowPipeMessages = false) { if (!Uri.TryCreate(commandUri, UriKind.Absolute, out var uri) || !uri.Scheme.Equals(SharedConstants.Schema, StringComparison.OrdinalIgnoreCase)) @@ -273,7 +279,7 @@ internal static class CommandUriMapper return route.CreateCommand(parameters); } - if (routesForHost.Any()) + if (routesForHost.Any() || !allowPipeMessages) return null; try diff --git a/CDPIUI/Commands/CommandsHandler.cs b/CDPIUI/Commands/CommandsHandler.cs index 90dcb173..884ba95d 100644 --- a/CDPIUI/Commands/CommandsHandler.cs +++ b/CDPIUI/Commands/CommandsHandler.cs @@ -46,9 +46,12 @@ private void ExecuteCommand(object parameter) #endregion + /// + /// Handles a link built by the application itself; it may be an internal pipe message. + /// public static bool HandleCommand(string commandUri) { - var message = CommandUriMapper.ConvertBack(commandUri); + var message = CommandUriMapper.ConvertBack(commandUri, allowPipeMessages: true); return message != null && HandleCommand(message); } @@ -61,6 +64,10 @@ public static bool HandleCommand(IPipeMessage message) return true; } + /// + /// Handles a link that comes from outside the application, such as a cdpiui:// activation: + /// only the public routes are accepted. + /// public static async Task HandleCommandAsync(string commandUri) { var message = CommandUriMapper.ConvertBack(commandUri); @@ -114,7 +121,7 @@ private static void HandleGoodCheckMessage(GoodCheckMessageModel model) { switch (model.MessageType) { - case GoodCheckMessageIds.Runned: + case GoodCheckMessageIds.Running: return; case GoodCheckMessageIds.Died: @@ -133,7 +140,7 @@ private static void HandleGoodCheckMessage(GoodCheckMessageModel model) if (!int.TryParse(model.MessageData?["operationId"], out var id)) return; - GoodCheckProcessService.Instance.HandleProcessException(error!, id); + GoodCheckProcessService.Instance.OperationWithIdFailed(id, error); return; } } diff --git a/CDPIUI/Controls/ComponentSettings/ComponentHelpfulLinksUserControl.xaml.cs b/CDPIUI/Controls/ComponentSettings/ComponentHelpfulLinksUserControl.xaml.cs index c3f209fe..34e5b6c7 100644 --- a/CDPIUI/Controls/ComponentSettings/ComponentHelpfulLinksUserControl.xaml.cs +++ b/CDPIUI/Controls/ComponentSettings/ComponentHelpfulLinksUserControl.xaml.cs @@ -14,7 +14,7 @@ using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.Commands; using CDPIUI.Controls.Dialogs.ComponentSettings; using System.Threading.Tasks; @@ -75,7 +75,7 @@ private async void SettingsCard_Click(object sender, RoutedEventArgs e) { XamlRoot = this.XamlRoot }; - await dialog.ShowAsync(); + await dialog.ShowQueuedAsync(); break; case HelpfulLinkType.SetupProxy: CommandsHandler.HandleCommand($"cdpiui://Tools/Proxy"); diff --git a/CDPIUI/Controls/ComponentSettings/ConfigNeededUserControl.xaml.cs b/CDPIUI/Controls/ComponentSettings/ConfigNeededUserControl.xaml.cs index a7d60165..afe47a32 100644 --- a/CDPIUI/Controls/ComponentSettings/ConfigNeededUserControl.xaml.cs +++ b/CDPIUI/Controls/ComponentSettings/ConfigNeededUserControl.xaml.cs @@ -2,7 +2,7 @@ using CDPIUI.Controls.WindowControls; using CDPIUI.Core.Store.Data; using CDPIUI.Helper; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using CDPIUI.ViewModels; using CommunityToolkit.WinUI.Controls; using Microsoft.UI.Xaml; diff --git a/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml b/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml index 57b8c36e..4f71f92d 100644 --- a/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml +++ b/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml @@ -45,7 +45,7 @@ - diff --git a/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml.cs b/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml.cs index fa2c3fb2..81be84c6 100644 --- a/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml.cs +++ b/CDPIUI/Controls/ComponentSettings/ConfigSelector.xaml.cs @@ -60,7 +60,7 @@ private async void ShareButton_Click(object sender, RoutedEventArgs e) XamlRoot = XamlRoot, Title = Helper.AddOns.ConfigShare.ConfigShareUI.Text("ConfigShareError"), Message = Helper.AddOns.ConfigShare.ConfigShareUI.ErrorText(exception) }; - await error.ShowAsync(); + await error.ShowQueuedAsync(); } finally { ShareButton.IsEnabled = true; } } diff --git a/CDPIUI/Controls/ComponentSettings/SettingTile.xaml b/CDPIUI/Controls/ComponentSettings/SettingTile.xaml index 09f13613..9fe114d9 100644 --- a/CDPIUI/Controls/ComponentSettings/SettingTile.xaml +++ b/CDPIUI/Controls/ComponentSettings/SettingTile.xaml @@ -40,11 +40,11 @@ - - diff --git a/CDPIUI/Controls/CreateConfigHelper/ComponentSettingUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/ComponentSettingUserControl.xaml.cs index 2ef56654..b702abda 100644 --- a/CDPIUI/Controls/CreateConfigHelper/ComponentSettingUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/ComponentSettingUserControl.xaml.cs @@ -342,10 +342,10 @@ private void SelectFileButton_Click(object sender, RoutedEventArgs e) string filePath; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { - openFileDialog.Title = "Choose text file"; + openFileDialog.Title = WinUI3Localizer.Localizer.Get().GetLocalizedString("FileDialogChooseTextFileTitle"); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - openFileDialog.Filter = "TXT files (*.txt)|*.txt"; + openFileDialog.Filter = $"{WinUI3Localizer.Localizer.Get().GetLocalizedString("TextFiles")} (*.txt)|*.txt"; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) @@ -371,7 +371,7 @@ private async void ViewSelectedFileButton_Click(object sender, RoutedEventArgs e XamlRoot = this.XamlRoot, FilePath = TextValue.Replace("\"", ""), }; - await dialog.ShowAsync(); + await dialog.ShowQueuedAsync(); if (dialog.IsSuccess) SettingsManager.Instance.SetValue("FILEOPENACTIONS", "isDialogShown", true); } diff --git a/CDPIUI/Controls/CreateConfigHelper/ComponentSettingsExclusiveGroupUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/ComponentSettingsExclusiveGroupUserControl.xaml.cs index 7fecfe88..bb0e1a00 100644 --- a/CDPIUI/Controls/CreateConfigHelper/ComponentSettingsExclusiveGroupUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/ComponentSettingsExclusiveGroupUserControl.xaml.cs @@ -122,6 +122,9 @@ public ComponentSettingsExclusiveGroupUserControl() GraphicDesignerTextValueChangedCommand = new RelayCommand(p => NotifyValueChanged((Tuple)p)); GraphicDesignerBoolValueToggledCommand = new RelayCommand(p => NotifyToggled((Tuple)p)); + // Each control gets its own collection: a default in PropertyMetadata is one instance + // shared by every control, and each of them subscribed to it. + SetValue(AvailableVariantsProperty, new ObservableCollection()); AvailableVariants.CollectionChanged += AvailableVariants_CollectionChanged; } @@ -192,7 +195,9 @@ public ObservableCollection AvailableVariants { item.PropertyChanged -= Item_PropertyChanged; } - SetValue(AvailableVariantsProperty, value); + AvailableVariants.CollectionChanged -= AvailableVariants_CollectionChanged; + SetValue(AvailableVariantsProperty, value ?? new ObservableCollection()); + AvailableVariants.CollectionChanged += AvailableVariants_CollectionChanged; UpdateSelectorBar(); CheckSelectedItem(); HandleCollectionChanged(); @@ -201,7 +206,7 @@ public ObservableCollection AvailableVariants public static readonly DependencyProperty AvailableVariantsProperty = DependencyProperty.Register( - nameof(AvailableVariants), typeof(ObservableCollection), typeof(ComponentSettingsExclusiveGroupUserControl), new PropertyMetadata(new ObservableCollection() ) + nameof(AvailableVariants), typeof(ObservableCollection), typeof(ComponentSettingsExclusiveGroupUserControl), new PropertyMetadata(null) ); private void UpdateSelectorBar() diff --git a/CDPIUI/Controls/CreateConfigHelper/ConditionUserControl.xaml b/CDPIUI/Controls/CreateConfigHelper/ConditionUserControl.xaml index 6c9006ca..a629a7b0 100644 --- a/CDPIUI/Controls/CreateConfigHelper/ConditionUserControl.xaml +++ b/CDPIUI/Controls/CreateConfigHelper/ConditionUserControl.xaml @@ -42,7 +42,7 @@ - - - - @@ -271,7 +271,7 @@ - - - - - diff --git a/CDPIUI/Controls/CreateConfigHelper/ConfigUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/ConfigUserControl.xaml.cs index 3e46122c..d67d18dd 100644 --- a/CDPIUI/Controls/CreateConfigHelper/ConfigUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/ConfigUserControl.xaml.cs @@ -5,7 +5,7 @@ using System; using System.Collections.Generic; using System.Windows.Input; -using CDPIUI.Shared.Extentions; +using CDPIUI.Shared.Extensions; using WinUI3Localizer; // To learn more about WinUI, the WinUI project structure, diff --git a/CDPIUI/Controls/CreateConfigHelper/CreateConfigDesignerUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/CreateConfigDesignerUserControl.xaml.cs index ca5fe2f0..358569d9 100644 --- a/CDPIUI/Controls/CreateConfigHelper/CreateConfigDesignerUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/CreateConfigDesignerUserControl.xaml.cs @@ -151,8 +151,8 @@ private void ShowBadges() _badges.Add(new() { Mode = mode, - DisplayName = mode.ToString().ToUpper(), - DisplayTip = localizer.GetLocalizedString($"/Flashlight/{mode.ToString().ToUpper()}"), + DisplayName = mode.ToString().ToUpperInvariant(), + DisplayTip = localizer.GetLocalizedString($"/Flashlight/{mode.ToString().ToUpperInvariant()}"), }); } } @@ -168,7 +168,7 @@ private void ViewFullArgs() Args = [this.Args ?? string.Empty] }; - _ = dialog.ShowAsync(); + _ = dialog.ShowQueuedAsync(); } private async void EditByeDPIMode() @@ -177,7 +177,7 @@ private async void EditByeDPIMode() { XamlRoot = this.XamlRoot, }; - var result = await dialog.ShowAsync(); + var result = await dialog.ShowQueuedAsync(); if (result == ContentDialogResult.Primary) { Modes = dialog.Result; @@ -187,10 +187,8 @@ private async void EditByeDPIMode() private void NotifyModeChanged() { - if (ModeChangedParameter == null) - { - ModeChangedParameter = Tuple.Create(Guid, Modes); - } + // A parameter cached from the first edit sent the old modes on every later edit. + ModeChangedParameter = Tuple.Create(Guid, Modes); if (ModeChangedCommand != null && ModeChangedCommand.CanExecute(ModeChangedParameter)) { ModeChangedCommand.Execute(ModeChangedParameter); diff --git a/CDPIUI/Controls/CreateConfigHelper/FileConfigInspectUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/FileConfigInspectUserControl.xaml.cs index 85510d0a..52217474 100644 --- a/CDPIUI/Controls/CreateConfigHelper/FileConfigInspectUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/FileConfigInspectUserControl.xaml.cs @@ -142,12 +142,13 @@ private void SelectNewFile_Click(object sender, RoutedEventArgs e) { using (OpenFileDialog openFileDialog = new()) { - openFileDialog.Title = "Choose file"; + openFileDialog.Title = localizer.GetLocalizedString("FileDialogChooseFileTitle"); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openFileDialog.FilterIndex = 0; - openFileDialog.Filter = Path.GetExtension(FilePath) == ".txt" ? "TXT files (*.txt)|*.txt" : - Path.GetExtension(FilePath) == ".txt" ? "BIN files (*.bin)|*.bin" : "All files (*.*)|*.*"; + string extension = Path.GetExtension(FilePath); + openFileDialog.Filter = string.Equals(extension, ".txt", StringComparison.OrdinalIgnoreCase) ? "TXT files (*.txt)|*.txt" : + string.Equals(extension, ".bin", StringComparison.OrdinalIgnoreCase) ? "BIN files (*.bin)|*.bin" : "All files (*.*)|*.*"; openFileDialog.RestoreDirectory = false; if (openFileDialog.ShowDialog() == DialogResult.OK) diff --git a/CDPIUI/Controls/CreateConfigHelper/GoodCheckReportStrategyUserControl.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/GoodCheckReportStrategyUserControl.xaml.cs index e98c9cea..4d460833 100644 --- a/CDPIUI/Controls/CreateConfigHelper/GoodCheckReportStrategyUserControl.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/GoodCheckReportStrategyUserControl.xaml.cs @@ -30,7 +30,7 @@ namespace CDPIUI; public sealed partial class GoodCheckReportStrategyUserControl : UserControl { private bool IsPointerOnControl = false; - private bool isRunned = false; + private bool isRunning = false; private ILocalizer localizer = Localizer.Get(); @@ -141,7 +141,8 @@ private void SetFlag() { Flag = FlagIconButton.Checked; - if (FlagToggledCommandParameter == null) FlagToggledCommandParameter = Tuple.Create(Args, FlagIconButton.Checked); + // A cached parameter kept the first state, so the flag could not be removed. + FlagToggledCommandParameter = Tuple.Create(Args, FlagIconButton.Checked); if (FlagToggledCommand != null && FlagToggledCommand.CanExecute(FlagToggledCommandParameter)) @@ -160,7 +161,7 @@ private void Play() private async void PlayAsync() { - if (!isRunned) + if (!isRunning) { await ComponentTasksManager.Instance.StopTask(ComponentId); @@ -188,15 +189,15 @@ private void ProcessManager_onProcessStateChanged(Tuple tuple) PlayIconButton.Checked = true; PlayToolTip.Content = localizer.GetLocalizedString("StopTest"); PlayIconButton.Visibility = Visibility.Visible; - isRunned = true; + isRunning = true; } - else if (isRunned) + else if (isRunning) { PlayIconButton.Checked = false; PlayToolTip.Content = localizer.GetLocalizedString("TestThis"); if (!IsPointerOnControl) PlayIconButton.Visibility = Visibility.Collapsed; - isRunned = false; + isRunning = false; ComponentTasksManager.Instance.TaskStateUpdated -= ProcessManager_onProcessStateChanged; } } @@ -212,7 +213,7 @@ private void ViewFullArgs() Args = [this.Args ?? string.Empty] }; - _ = dialog.ShowAsync(); + _ = dialog.ShowQueuedAsync(); } private void SetVisible(bool isVisible) diff --git a/CDPIUI/Controls/CreateConfigHelper/SelectConfigKitToEditContentDialog.xaml.cs b/CDPIUI/Controls/CreateConfigHelper/SelectConfigKitToEditContentDialog.xaml.cs index 3c6d59b8..dd334910 100644 --- a/CDPIUI/Controls/CreateConfigHelper/SelectConfigKitToEditContentDialog.xaml.cs +++ b/CDPIUI/Controls/CreateConfigHelper/SelectConfigKitToEditContentDialog.xaml.cs @@ -51,7 +51,7 @@ private async void LoadConfigKits() StoreId = item.Id, Name = item.ShortName, Developer = item.Developer, - ColorHEX = item.BackgroudColor, + ColorHEX = item.BackgroundColor, ImageSource = new BitmapImage(UIHelper.GetUriFromString(LScriptLangHelper.ExecuteScript(item.IconPath, scriptArgs:item.Directory))) }); } diff --git a/CDPIUI/Controls/CreateConfigHelper/VariableUserControl.xaml b/CDPIUI/Controls/CreateConfigHelper/VariableUserControl.xaml index 86a3925b..e2103c50 100644 --- a/CDPIUI/Controls/CreateConfigHelper/VariableUserControl.xaml +++ b/CDPIUI/Controls/CreateConfigHelper/VariableUserControl.xaml @@ -48,7 +48,7 @@ - @@ -84,7 +84,7 @@ - @@ -115,7 +115,7 @@ diff --git a/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectConfigToEditContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectConfigToEditContentDialog.xaml.cs index 76f16a25..9e1b0488 100644 --- a/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectConfigToEditContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectConfigToEditContentDialog.xaml.cs @@ -106,7 +106,7 @@ private void InitModel(string componentId) DisplayName = item.name, Directory = item.file_name, PackId = item.packId, - PackName = DatabaseHelper.Instance.GetItemById(item.packId).ShortName, + PackName = DatabaseHelper.Instance.GetItemById(item.packId)?.ShortName ?? item.packId, }); } } diff --git a/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectUsedFilesForConfigContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectUsedFilesForConfigContentDialog.xaml.cs index f95b9200..fbeb5ea6 100644 --- a/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectUsedFilesForConfigContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/CreateConfigHelper/SelectUsedFilesForConfigContentDialog.xaml.cs @@ -95,6 +95,16 @@ private void SelectUsedFilesForConfigContentDialog_Loaded(object sender, RoutedE } } + // ':', '/', '?' and the like in a config name threw or produced nested folders. + private static string GetSafeFolderName(string name) + { + string value = (name ?? string.Empty).Unidecode().Replace(" ", "_"); + char[] invalid = Path.GetInvalidFileNameChars(); + value = new string(value.Select(c => Array.IndexOf(invalid, c) >= 0 ? '_' : c).ToArray()).Trim('.', '_'); + if (value.Length > 64) value = value[..64]; + return value.Length == 0 ? "config" : value; + } + private void CreateModels() { @@ -102,29 +112,27 @@ private void CreateModels() TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); int secondsSinceEpoch = (int)t.TotalSeconds; + string folderName = $"{GetSafeFolderName(ConfigName)}_converted_{secondsSinceEpoch}"; foreach (var file in _files) { string convDir; - if (Path.GetExtension(file).Equals(".txt", StringComparison.CurrentCultureIgnoreCase)) + if (Path.GetExtension(file).Equals(".txt", StringComparison.OrdinalIgnoreCase)) { convDir = Path.Combine( ConfigurationService.GetItemFolderFromPackId(SharedConstants.LocalUserItemsId), SharedConstants.LocalUserItemSiteListsFolder, - $"{ConfigName.Unidecode().Replace(" ", "_")}_converted_{secondsSinceEpoch}"); + folderName); } else { convDir = Path.Combine( ConfigurationService.GetItemFolderFromPackId(SharedConstants.LocalUserItemsId), SharedConstants.LocalUserItemBinsFolder, - $"{ConfigName.Unidecode().Replace(" ", "_")}_converted_{secondsSinceEpoch}"); - } - if (!Directory.Exists(convDir)) - { - Directory.CreateDirectory(convDir); + folderName); } + // The folder is created when a file is copied into it, not before the user confirms. string autoCorrectPath = FindAutoCorrectPath(file); Models.Add(new FileSelectModel() { @@ -192,7 +200,10 @@ private void AutoCorrectActions() Debug.WriteLine(model.AutoCorrectFilePath); if (!string.Equals(Path.GetFileNameWithoutExtension(model.AutoCorrectFilePath), "autohostlist", StringComparison.OrdinalIgnoreCase)) + { + Directory.CreateDirectory(model.ConvertDirectoryPath); File.Copy(model.AutoCorrectFilePath, Path.Combine(model.ConvertDirectoryPath, Path.GetFileName(filepath)), true); + } Files.Add( model.FilePath, diff --git a/CDPIUI/Controls/Dialogs/EditConfig/EditConfigWindowWelcomeContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/EditConfig/EditConfigWindowWelcomeContentDialog.xaml.cs index 39dbdea9..913311d7 100644 --- a/CDPIUI/Controls/Dialogs/EditConfig/EditConfigWindowWelcomeContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/EditConfig/EditConfigWindowWelcomeContentDialog.xaml.cs @@ -111,7 +111,7 @@ private void InitModel(string componentId) DisplayName = item.name, FileName = item.file_name, PackId = item.packId, - PackDisplayName = DatabaseHelper.Instance.GetItemById(item.packId).ShortName, + PackDisplayName = DatabaseHelper.Instance.GetItemById(item.packId)?.ShortName ?? item.packId, IsLegacyConfig = item.IsLegacy, }); } @@ -139,7 +139,7 @@ private async void LoadConfigKits() StoreId = item.Id, Name = item.ShortName, Developer = item.Developer, - ColorHEX = item.BackgroudColor, + ColorHEX = item.BackgroundColor, ImageSource = new BitmapImage(UIHelper.GetUriFromString(LScriptLangHelper.ExecuteScript(item.IconPath, scriptArgs: item.Directory))) }); } diff --git a/CDPIUI/Controls/Dialogs/MainPage/CommunityContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/MainPage/CommunityContentDialog.xaml.cs index ba56c0f1..9b9063bd 100644 --- a/CDPIUI/Controls/Dialogs/MainPage/CommunityContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/MainPage/CommunityContentDialog.xaml.cs @@ -54,7 +54,7 @@ private void Init() Models.Clear(); Models.Add(CreateNewModel("TelegramMainUrlName", "TelegramMainUrlDescription", UrlOpenHelper.TelegramMainUrl)); Models.Add(CreateNewModel("NTCPartyName", "NTCPartyDescription", UrlOpenHelper.NTCParty)); - Models.Add(CreateNewModel("BBDName", "BBDDecription", UrlOpenHelper.BBD)); + Models.Add(CreateNewModel("BBDName", "BBDDescription", UrlOpenHelper.BBD)); Models.Add(CreateNewModel("TelegramMemeUrlName", "TelegramMemeUrlDescription", UrlOpenHelper.TelegramMemeUrl)); Models.Add(CreateNewModel("TelegramLUrlName", "TelegramLUrlDescription", UrlOpenHelper.TelegramLUrl)); diff --git a/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml b/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml index cc32896f..a5e6a45d 100644 --- a/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml +++ b/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml @@ -70,7 +70,7 @@ - @@ -89,7 +89,7 @@ - diff --git a/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml.cs index 44d33e20..b2ad99f5 100644 --- a/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/ProxySetupUtil/SelectAppContentDialog.xaml.cs @@ -180,7 +180,7 @@ private void FileChooseButton_Click(object sender, RoutedEventArgs e) { openFileDialog.Title = localizer.GetLocalizedString("ChooseExeFile"); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - openFileDialog.Filter = "EXE files (*.exe)|*.exe"; + openFileDialog.Filter = $"{localizer.GetLocalizedString("FileDialogChooseExecutableFilter")} (*.exe)|*.exe"; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) diff --git a/CDPIUI/Controls/Dialogs/Store/AcceptLicenseContentDialog.xaml.cs b/CDPIUI/Controls/Dialogs/Store/AcceptLicenseContentDialog.xaml.cs index 40b1cbfc..551386ff 100644 --- a/CDPIUI/Controls/Dialogs/Store/AcceptLicenseContentDialog.xaml.cs +++ b/CDPIUI/Controls/Dialogs/Store/AcceptLicenseContentDialog.xaml.cs @@ -43,7 +43,7 @@ private void HyperlinkButton_Click(object sender, RoutedEventArgs e) if (url.StartsWith("$OPENINBROWSER")) { - _ = Windows.System.Launcher.LaunchUriAsync(new Uri(LScriptLangHelper.GetArgumentsFromScript(url))); + global::CDPIUI.Helper.UrlOpenHelper.LaunchUrl(LScriptLangHelper.GetArgumentsFromScript(url)); } else { diff --git a/CDPIUI/Controls/Dialogs/Universal/ErrorContentDialog.cs b/CDPIUI/Controls/Dialogs/Universal/ErrorContentDialog.cs index 783a7be8..33c4db02 100644 --- a/CDPIUI/Controls/Dialogs/Universal/ErrorContentDialog.cs +++ b/CDPIUI/Controls/Dialogs/Universal/ErrorContentDialog.cs @@ -123,7 +123,7 @@ public async Task ShowErrorDialogAsync(string content, string errorDetails, Xaml dialog.Content = scrollView; - await dialog.ShowAsync(); + await dialog.ShowQueuedAsync(); } private void ViewMoreHandler(object sender, RoutedEventArgs e) diff --git a/CDPIUI/Controls/GoodCheck/GoodCheckSitelistStrategyChooserControl.xaml b/CDPIUI/Controls/GoodCheck/GoodCheckSitelistStrategyChooserControl.xaml index 02d0327f..ad46c732 100644 --- a/CDPIUI/Controls/GoodCheck/GoodCheckSitelistStrategyChooserControl.xaml +++ b/CDPIUI/Controls/GoodCheck/GoodCheckSitelistStrategyChooserControl.xaml @@ -24,7 +24,7 @@ - - - @@ -84,7 +84,7 @@ - + diff --git a/CDPIUI/Controls/MainPage/FlashlightTipsWidgetUserControl.xaml.cs b/CDPIUI/Controls/MainPage/FlashlightTipsWidgetUserControl.xaml.cs index da84ecab..7dbb7a0c 100644 --- a/CDPIUI/Controls/MainPage/FlashlightTipsWidgetUserControl.xaml.cs +++ b/CDPIUI/Controls/MainPage/FlashlightTipsWidgetUserControl.xaml.cs @@ -48,6 +48,11 @@ public FlashlightTipsWidgetUserControl() public void ConnectHandlers() { + // Also called by MainPage after the constructor did it: remove first, or every + // animation step ran twice. + SlideOutStoryboard.Completed -= SlideOutStoryboard_Completed; + SlideInStoryboard.Completed -= SideInStoryboard_Completed; + this.Unloaded -= FlashlightTipsWidgetUserControl_Unloaded; SlideOutStoryboard.Completed += SlideOutStoryboard_Completed; SlideInStoryboard.Completed += SideInStoryboard_Completed; this.Unloaded += FlashlightTipsWidgetUserControl_Unloaded; @@ -73,8 +78,10 @@ private void LoadTips() TipTitleTextBlock.Text = localizer.GetLocalizedString("/Flashlight/AreYouKnow"); } - _currentIndex = Tips.Count; - MoveNext(); + // The first tip is shown directly: starting from Tips.Count skipped tip 0, and with a + // single tip MoveNext did nothing and the widget stayed empty. + _currentIndex = 0; + TipDescriptionTextBlock.Text = Tips.Count > 0 ? Tips[0] : string.Empty; } private void PreviousTipButton_Click(object sender, RoutedEventArgs e) @@ -124,7 +131,7 @@ private void StartTransition(double slideOutToX, double slideInFromX) private void SlideOutStoryboard_Completed(object sender, object e) { _currentIndex = _targetIndex; - TipDecriptionTextBlock.Text = Tips[_currentIndex]; + TipDescriptionTextBlock.Text = Tips[_currentIndex]; TextTransform.X = _slideInFromX; diff --git a/CDPIUI/Controls/MainPage/SmallComponentTileUserControl.xaml b/CDPIUI/Controls/MainPage/SmallComponentTileUserControl.xaml index b183f3b9..86bad726 100644 --- a/CDPIUI/Controls/MainPage/SmallComponentTileUserControl.xaml +++ b/CDPIUI/Controls/MainPage/SmallComponentTileUserControl.xaml @@ -143,7 +143,7 @@ - diff --git a/CDPIUI/Controls/Store/StoreDownloadItemButton.xaml.cs b/CDPIUI/Controls/Store/StoreDownloadItemButton.xaml.cs index 562cdd77..29efa596 100644 --- a/CDPIUI/Controls/Store/StoreDownloadItemButton.xaml.cs +++ b/CDPIUI/Controls/Store/StoreDownloadItemButton.xaml.cs @@ -2,7 +2,7 @@ using CDPIUI.Core.Store; using CDPIUI.Helper.Parsers; -using CDPIUI.Shared.PrettyErrorConvertionService; +using CDPIUI.Shared.PrettyErrorConversionService; using CDPIUI.Views.Store; using CommunityToolkit.Labs.WinUI.MarkdownTextBlock; using Microsoft.UI; @@ -329,6 +329,7 @@ private void RemoveHandlers() StoreHelper.Instance.ItemTimeRemainingChanged -= StoreHelper_ItemTimeRemainingChanged; StoreHelper.Instance.ItemDownloadSpeedChanged -= StoreHelper_ItemDownloadSpeedChanged; StoreHelper.Instance.ItemActionsStopped -= StoreHelper_ItemActionsStopped; + StoreHelper.Instance.ItemInstallingErrorHappens -= StoreHelper_ItemInstallingErrorHappens; } private void StoreHelper_ItemInstallingErrorHappens(Tuple obj) @@ -411,7 +412,7 @@ private void StoreHelper_ItemTimeRemainingChanged(Tuple obj) if (StoreHelper.Instance.GetItemIdFromOperationId(operationId) != StoreId) return; - TimeTextBlock.Text = UnitsParser.ConvertMinutesToPrettyText(time.Minutes); + TimeTextBlock.Text = UnitsParser.ConvertMinutesToPrettyText(time.TotalMinutes); } private void PreferItemDownloadingStateActions(string state) @@ -507,7 +508,7 @@ private async void ViewMoreButton_Click(object sender, RoutedEventArgs e) Text = Description }; - await dialog.ShowAsync(); + await dialog.ShowQueuedAsync(); } private void UpdateButton_Click(object sender, RoutedEventArgs e) diff --git a/CDPIUI/Controls/Store/StoreItemLargeButton.xaml b/CDPIUI/Controls/Store/StoreItemLargeButton.xaml index ac736ae2..4a2be2b0 100644 --- a/CDPIUI/Controls/Store/StoreItemLargeButton.xaml +++ b/CDPIUI/Controls/Store/StoreItemLargeButton.xaml @@ -19,10 +19,10 @@ - - @@ -66,7 +66,7 @@ diff --git a/CDPIUI/Controls/Store/StoreItemLargeButton.xaml.cs b/CDPIUI/Controls/Store/StoreItemLargeButton.xaml.cs index b6dca5f9..59fd0fc3 100644 --- a/CDPIUI/Controls/Store/StoreItemLargeButton.xaml.cs +++ b/CDPIUI/Controls/Store/StoreItemLargeButton.xaml.cs @@ -61,7 +61,7 @@ private void OnButtonPointerEntered(object sender, PointerRoutedEventArgs e) AnimateTranslateY(-3, durationMs: 200); - _bottomRect.Style = (Style)Resources["BackgroudRectangleHoverStyle"]; + _bottomRect.Style = (Style)Resources["BackgroundRectangleHoverStyle"]; PART_Button.Translation += shadowVector; } @@ -70,7 +70,7 @@ private void OnButtonPointerExited(object sender, PointerRoutedEventArgs e) VisualStateManager.GoToState(_button, "Normal", true); AnimateTranslateY(0, durationMs: 200); - _bottomRect.Style = (Style)Resources["BackgroudRectangleDefaultStyle"]; + _bottomRect.Style = (Style)Resources["BackgroundRectangleDefaultStyle"]; PART_Button.Translation -= shadowVector; } diff --git a/CDPIUI/Controls/Store/StoreItemLinkButton.xaml.cs b/CDPIUI/Controls/Store/StoreItemLinkButton.xaml.cs index 94d99dc1..0b428c96 100644 --- a/CDPIUI/Controls/Store/StoreItemLinkButton.xaml.cs +++ b/CDPIUI/Controls/Store/StoreItemLinkButton.xaml.cs @@ -69,8 +69,9 @@ public string ActionIcon nameof(ActionIcon), typeof(string), typeof(StoreItemLinkButton), new PropertyMetadata("\uE8A7") ); - private async void Button_Click(object sender, RoutedEventArgs e) + private void Button_Click(object sender, RoutedEventArgs e) { - _ = await Windows.System.Launcher.LaunchUriAsync(new Uri(Url)); + // The link comes from the store manifest: only web links are opened. + global::CDPIUI.Helper.UrlOpenHelper.LaunchUrl(Url); } } diff --git a/CDPIUI/Controls/Store/StoreItemMicroButton.xaml.cs b/CDPIUI/Controls/Store/StoreItemMicroButton.xaml.cs index 4324c514..12551a93 100644 --- a/CDPIUI/Controls/Store/StoreItemMicroButton.xaml.cs +++ b/CDPIUI/Controls/Store/StoreItemMicroButton.xaml.cs @@ -26,7 +26,16 @@ public sealed partial class StoreItemMicroButton : UserControl public StoreItemMicroButton() { InitializeComponent(); - + // StoreHelper outlives the control; without this the handlers kept the control alive. + Unloaded += (_, _) => RemoveHandlers(); + Loaded += (_, _) => + { + if (!string.IsNullOrEmpty(StoreId) && StoreHelper.Instance.GetOperationIdFromItemId(StoreId) != null) + { + RemoveHandlers(); + ConnectHandlers(); + } + }; } public string StoreId @@ -146,6 +155,8 @@ private void ConnectHandlers() _itemActionsStoppedHandler = (id) => { + if (id != StoreId) + return; RemoveHandlers(); GetReady(); }; @@ -184,6 +195,7 @@ private void PART_Button_Click(object sender, RoutedEventArgs e) if (DatabaseHelper.Instance.IsItemInstalled(StoreId) || StoreHelper.Instance.GetOperationIdFromItemId(StoreId) != null) return; + RemoveHandlers(); ConnectHandlers(); StoreHelper.Instance.AddItemToQueue(StoreId, string.Empty); } diff --git a/CDPIUI/Controls/Store/StoreItemSmallButton.xaml b/CDPIUI/Controls/Store/StoreItemSmallButton.xaml index ef59141e..f6a7e1b7 100644 --- a/CDPIUI/Controls/Store/StoreItemSmallButton.xaml +++ b/CDPIUI/Controls/Store/StoreItemSmallButton.xaml @@ -20,10 +20,10 @@ - - @@ -48,7 +48,7 @@ Click="PART_Button_Click"> - diff --git a/CDPIUI/Controls/Store/StoreItemSmallButton.xaml.cs b/CDPIUI/Controls/Store/StoreItemSmallButton.xaml.cs index 31684c1b..199f29d4 100644 --- a/CDPIUI/Controls/Store/StoreItemSmallButton.xaml.cs +++ b/CDPIUI/Controls/Store/StoreItemSmallButton.xaml.cs @@ -62,7 +62,7 @@ private void OnButtonPointerEntered(object sender, PointerRoutedEventArgs e) AnimateTranslateY(-3, durationMs: 200); - _bottomRect.Style = (Style)Resources["BackgroudRectangleHoverStyle"]; + _bottomRect.Style = (Style)Resources["BackgroundRectangleHoverStyle"]; PART_Button.Translation += shadowVector; } @@ -71,7 +71,7 @@ private void OnButtonPointerExited(object sender, PointerRoutedEventArgs e) VisualStateManager.GoToState(_button, "Normal", true); AnimateTranslateY(0, durationMs: 200); - _bottomRect.Style = (Style)Resources["BackgroudRectangleDefaultStyle"]; + _bottomRect.Style = (Style)Resources["BackgroundRectangleDefaultStyle"]; PART_Button.Translation -= shadowVector; } diff --git a/CDPIUI/Controls/Store/StoreLibraryItemButton.xaml b/CDPIUI/Controls/Store/StoreLibraryItemButton.xaml index 74732e96..4cc1d522 100644 --- a/CDPIUI/Controls/Store/StoreLibraryItemButton.xaml +++ b/CDPIUI/Controls/Store/StoreLibraryItemButton.xaml @@ -94,7 +94,7 @@ - - - - /// Who called this method /// Error code - /// (use + /// (use /// as instead of custom error) /// /// Why this happens public static void RaiseCriticalException(string sender, string errorCode, string why) { Logger.Instance.CreateErrorLog(sender, $"{errorCode} => {why}"); - CriticalErrorHandlerWindow window = new(where: sender, why: $"Because exception happens \n{why}", errorCode: errorCode); - window.Activate(); + ShowWindow(sender, $"Because exception happens \n{why}", errorCode); } /// @@ -40,8 +39,24 @@ public static void RaiseCriticalException(string sender, System.Exception except string readyToUseErrorCode = ErrorsHelper.Convertor.GetPrettyErrorCode("ERR_INTERNAL", exception); Logger.Instance.CreateErrorLog(sender, $"{readyToUseErrorCode} => {exception}"); - CriticalErrorHandlerWindow window = new(where: sender, why: $"Because exception happens \n{exception}", errorCode: readyToUseErrorCode); - window.Activate(); + ShowWindow(sender, $"Because exception happens \n{exception}", readyToUseErrorCode); + } + + // Core errors (a lost tray, for example) are raised on pool threads, where creating the + // window failed and the user never saw the error. + private static void ShowWindow(string sender, string why, string errorCode) + { + void Show() + { + CriticalErrorHandlerWindow window = new(where: sender, why: why, errorCode: errorCode); + window.Activate(); + } + + var dispatcher = (Microsoft.UI.Xaml.Application.Current as App)?.UiDispatcher; + if (dispatcher == null || dispatcher.HasThreadAccess) + Show(); + else if (!dispatcher.TryEnqueue(Show)) + Logger.Instance.CreateErrorLog(nameof(ErrorShowingHelper), $"Cannot show the critical error window for {errorCode}"); } } } diff --git a/CDPIUI/Helper/Basic/FileAssociationService.cs b/CDPIUI/Helper/Basic/FileAssociationService.cs index 87336f16..77eb729d 100644 --- a/CDPIUI/Helper/Basic/FileAssociationService.cs +++ b/CDPIUI/Helper/Basic/FileAssociationService.cs @@ -23,18 +23,30 @@ internal static class FileAssociationService new(".cdpipatch", "CDPIUI.Patch", "PatchFileTypeDisplayName", @"Assets\Store\Files\Pack.ico") ]; + private const string RegisteredPathValueName = "ExecutablePath"; + + /// + /// Registers the associations once per executable path: after the application was moved + /// or reinstalled elsewhere, the old registration points to a missing file. + /// internal static void EnsureRegistered() { - if (WasAutomaticRegistrationAttempted() || !TryCreateAutomaticRegistrationMarker()) + string executablePath = Environment.ProcessPath ?? string.Empty; + if (string.IsNullOrWhiteSpace(executablePath) || + string.Equals(GetRegisteredExecutablePath(), executablePath, StringComparison.OrdinalIgnoreCase)) return; + // Attempted once per path, also when a part failed, so the warnings are not repeated on every start. _ = RegisterAssociations(); + _ = TryCreateAutomaticRegistrationMarker(executablePath); } internal static bool RegisterManually() { - _ = TryCreateAutomaticRegistrationMarker(); - return RegisterAssociations(); + bool registered = RegisterAssociations(); + if (registered) + _ = TryCreateAutomaticRegistrationMarker(Environment.ProcessPath ?? string.Empty); + return registered; } private static bool RegisterAssociations() @@ -192,27 +204,28 @@ private static string GetAssociationIcon(Association association) return $"\"{iconPath}\",0"; } - private static bool WasAutomaticRegistrationAttempted() + private static string? GetRegisteredExecutablePath() { try { using var key = Registry.CurrentUser.OpenSubKey(AutomaticRegistrationMarkerPath); - return key is not null; + return key?.GetValue(RegisteredPathValueName) as string; } catch (Exception ex) { Logger.Instance.CreateWarningLog( nameof(FileAssociationService), $"Cannot read the automatic association registration marker: {ex.Message}"); - return false; + return null; } } - private static bool TryCreateAutomaticRegistrationMarker() + private static bool TryCreateAutomaticRegistrationMarker(string executablePath) { try { using var key = Registry.CurrentUser.CreateSubKey(AutomaticRegistrationMarkerPath); + key?.SetValue(RegisteredPathValueName, executablePath); return key is not null; } catch (Exception ex) diff --git a/CDPIUI/Helper/Basic/ProxyAddressWarning.cs b/CDPIUI/Helper/Basic/ProxyAddressWarning.cs new file mode 100644 index 00000000..45d2ba52 --- /dev/null +++ b/CDPIUI/Helper/Basic/ProxyAddressWarning.cs @@ -0,0 +1,33 @@ +using CDPIUI.Core.Proxy; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using System.Threading.Tasks; +using WinUI3Localizer; + +namespace CDPIUI.Helper.Basic; + +/// +/// Proxy components listen on the address set in the proxy setup. Anything but a loopback +/// address makes the proxy reachable from the network without authentication (issue #67). +/// +public static class ProxyAddressWarning +{ + public static async Task ConfirmAsync(XamlRoot xamlRoot, string ip) + { + if (ProxyHelper.IsLoopbackAddress(ip)) + return true; + + ILocalizer localizer = Localizer.Get(); + var dialog = new ContentDialog + { + Title = localizer.GetLocalizedString("ProxyNetworkAddressTitle"), + Content = string.Format(localizer.GetLocalizedString("ProxyNetworkAddressWarning"), ip), + PrimaryButtonText = localizer.GetLocalizedString("Continue"), + CloseButtonText = localizer.GetLocalizedString("Cancel"), + DefaultButton = ContentDialogButton.Close, + Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style, + XamlRoot = xamlRoot, + }; + return await dialog.ShowQueuedAsync() == ContentDialogResult.Primary; + } +} diff --git a/CDPIUI/Helper/Basic/Settings/IniSettingsHelper.cs b/CDPIUI/Helper/Basic/Settings/IniSettingsHelper.cs index b1fb6c2b..1940d85d 100644 --- a/CDPIUI/Helper/Basic/Settings/IniSettingsHelper.cs +++ b/CDPIUI/Helper/Basic/Settings/IniSettingsHelper.cs @@ -126,19 +126,36 @@ public T GetValue(string section, string key, T defaultValue) return defaultValue; } + /// + /// The value, or the default of (an empty string for strings) when + /// the key is missing: a config.ini of another GoodCheck version must not crash the page. + /// public T GetValue(string section, string key) { if (TryGetValue(section, key, out var val)) return val!; - throw new KeyNotFoundException($"Section '{section}' or key '{key}' not found or cannot convert to {typeof(T).Name}."); + return typeof(T) == typeof(string) ? (T)(object)string.Empty : default!; } public void Save() { lock (_lock) { - using (var sw = new StreamWriter(_filePath, false, FileEncoding)) + // Written next to the file and moved over it, so an interrupted save cannot leave a + // half-written config.ini. No BOM: GoodCheck does not expect one. + Encoding encoding = FileEncoding is UTF8Encoding ? new UTF8Encoding(false) : FileEncoding; + string tempPath = $"{_filePath}.{Guid.NewGuid():N}.tmp"; + try { - _parser.WriteData(sw, _data); + using (var sw = new StreamWriter(tempPath, false, encoding)) + { + _parser.WriteData(sw, _data); + } + File.Move(tempPath, _filePath, overwrite: true); + } + finally + { + if (File.Exists(tempPath)) + File.Delete(tempPath); } Reload(); @@ -149,14 +166,10 @@ public void Reload() { lock (_lock) { - if (File.Exists(_filePath)) - { - _data = _parser.ReadFile(_filePath); - } - else - { - throw new FileNotFoundException("INI file not found."); - } + // ReadFile without an encoding reads ASCII and breaks non-Latin values. + _data = File.Exists(_filePath) + ? _parser.ReadFile(_filePath, FileEncoding) + : new IniData(); } } diff --git a/CDPIUI/Helper/CreateConfigHelper/CreateConfigPageHelper.cs b/CDPIUI/Helper/CreateConfigHelper/CreateConfigPageHelper.cs index 00d469d0..4fcd59c8 100644 --- a/CDPIUI/Helper/CreateConfigHelper/CreateConfigPageHelper.cs +++ b/CDPIUI/Helper/CreateConfigHelper/CreateConfigPageHelper.cs @@ -73,7 +73,12 @@ public static PrettyLookTextResultModel ApplyPrettyFilesReplacement(string input model.ListDirectory = ListDirectoryReplaceRegex().Match(input).Groups[1].Value; model.BinDirectory = BinDirectoryReplaceRegex().Match(input).Groups[1].Value; - model.ResultText = input.Replace(model.ListDirectory, "list://").Replace(model.BinDirectory, "bin://"); + // Replace("") throws: when one of the directories was not found, the other + // shortcut was not applied either. + string text = input; + if (!string.IsNullOrEmpty(model.ListDirectory)) text = text.Replace(model.ListDirectory, "list://"); + if (!string.IsNullOrEmpty(model.BinDirectory)) text = text.Replace(model.BinDirectory, "bin://"); + model.ResultText = text; } catch { @@ -84,7 +89,9 @@ public static PrettyLookTextResultModel ApplyPrettyFilesReplacement(string input } public static string GetNormalText(string input, string listDirectory, string binDirectory) { - string text = input.Replace("list://", listDirectory).Replace("bin://", binDirectory); + string text = input + .Replace("list://", listDirectory ?? string.Empty) + .Replace("bin://", binDirectory ?? string.Empty); return text.Replace("\n", " "); } diff --git a/CDPIUI/Helper/CreateConfigHelper/GraphicDesignerHelper.cs b/CDPIUI/Helper/CreateConfigHelper/GraphicDesignerHelper.cs index 12956754..14605d5b 100644 --- a/CDPIUI/Helper/CreateConfigHelper/GraphicDesignerHelper.cs +++ b/CDPIUI/Helper/CreateConfigHelper/GraphicDesignerHelper.cs @@ -281,8 +281,10 @@ public static string ConvertStringToGraphicDesignerSettings( string value = string.Empty; if (token.Contains('=')) { - value = token.Split('=')[1]; - token = token.Split('=')[0]; + // The value may contain '=' itself (base64, fake payload hex and so on). + int separator = token.IndexOf('='); + value = token[(separator + 1)..]; + token = token[..separator]; } else if (tokens.Count-1 >= i + 1 && !tokens[i + 1].StartsWith('-') && !tokens[i + 1].StartsWith('/')) { diff --git a/CDPIUI/Helper/Database/DatabaseInitializationService.cs b/CDPIUI/Helper/Database/DatabaseInitializationService.cs index bf20af67..909e6221 100644 --- a/CDPIUI/Helper/Database/DatabaseInitializationService.cs +++ b/CDPIUI/Helper/Database/DatabaseInitializationService.cs @@ -92,6 +92,7 @@ private static void TryManualRestore(DatabaseStoreItem item) nameof(DatabaseHelper), $"Item {item.Id} is damaged and cannot be restore. Please, reinstall it" ); + DatabaseHelper.Instance.UnregisterMissingItem(item); } } diff --git a/CDPIUI/Helper/Items/CompatibilityCheckHelper.cs b/CDPIUI/Helper/Items/CompatibilityCheckHelper.cs index bee45bd3..e4b97efa 100644 --- a/CDPIUI/Helper/Items/CompatibilityCheckHelper.cs +++ b/CDPIUI/Helper/Items/CompatibilityCheckHelper.cs @@ -52,6 +52,7 @@ public async Task BeginCheck() if (config.target != null && config.target.Count == 2) { var databaseItem = DatabaseHelper.Instance.GetItemById(config.target[0]); + if (databaseItem == null) continue; string requiredVersion = config.target[1]; string installedVersion = databaseItem.CurrentVersion; diff --git a/CDPIUI/Helper/Migration/MigrationArchiveInspectionService.cs b/CDPIUI/Helper/Migration/MigrationArchiveInspectionService.cs index bfc1f1c9..2896882b 100644 --- a/CDPIUI/Helper/Migration/MigrationArchiveInspectionService.cs +++ b/CDPIUI/Helper/Migration/MigrationArchiveInspectionService.cs @@ -60,7 +60,7 @@ public VerifiedMigrationPackage InspectAndStage( .FindAll(group => group.Section == "COMPONENTS"); IReadOnlyList components = data.Presets - .FindAll(item => componentSettings.Find(setting => setting.Key.Equals(item.Component, StringComparison.CurrentCultureIgnoreCase))?.Value == "True") + .FindAll(item => componentSettings.Find(setting => setting.Key.Equals(item.Component, StringComparison.OrdinalIgnoreCase))?.Value == "True") .Select(GoodbyeDpiComponentMapper.Map) .GroupBy(item => item.InstallItemId, StringComparer.OrdinalIgnoreCase) .Select(group => group.First()) diff --git a/CDPIUI/Helper/Migration/MigrationImportService.cs b/CDPIUI/Helper/Migration/MigrationImportService.cs index 13958420..d5ebf0ad 100644 --- a/CDPIUI/Helper/Migration/MigrationImportService.cs +++ b/CDPIUI/Helper/Migration/MigrationImportService.cs @@ -289,7 +289,7 @@ private static void RewriteToken( { text = ReplaceOrdinalIgnoreCase(text, $"\"{candidate}\"", destination); text = ReplaceOrdinalIgnoreCase(text, $"'{candidate}'", destination); - text = ReplaceOrdinalIgnoreCase(text, candidate, destination); + text = ReplacePathIgnoreCase(text, candidate, destination); } } value.Value = text; @@ -312,6 +312,22 @@ private static string ReplaceOrdinalIgnoreCase(string input, string source, stri TimeSpan.FromSeconds(1)); } + /// + /// Replaces only where it is a whole path, so that rewriting + /// "list.txt" leaves "blacklist.txt" and "lists\list.txt.bak" alone. + /// + private static string ReplacePathIgnoreCase(string input, string source, string destination) + { + if (string.IsNullOrEmpty(source)) + return input; + return Regex.Replace( + input, + $@"(? destination, + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, + TimeSpan.FromSeconds(1)); + } + private static string QuotePath(string path) { return "\"" + path.Trim().Trim('"') + "\""; @@ -392,7 +408,7 @@ private static void CopyVerifiedEntry( Name = "Migrated from GoodbyeDPI-UI", ShortName = "Перенесенные из GoodbyeDPI-UI", Developer = "GDPIUI-Updater", - BackgroudColor = string.Empty + BackgroundColor = string.Empty }; if (!DatabaseHelper.Instance.AddOrUpdateItem(collection)) throw new InvalidOperationException("CDPIUI could not register the migrated preset collection."); @@ -436,6 +452,12 @@ private static int ApplySettings( switch (semanticKey) { case "ui.theme": + // A theme already chosen in CDPI UI wins over the old one; "Dark" is the + // value of a new settings file (Template/Settings/Settings.xml). + string currentTheme = SettingsManager.Instance.GetValue("APPEARANCE", "Theme") ?? string.Empty; + if (!currentTheme.Equals("Dark", StringComparison.OrdinalIgnoreCase) && + !currentTheme.Equals("Default", StringComparison.OrdinalIgnoreCase)) + break; string theme = (setting.Value ?? string.Empty).Trim().ToLowerInvariant() switch { "dark" or "1" => "Dark", @@ -460,12 +482,10 @@ private static int ApplySettings( } break; case "startup.autostart": - if (TryParseBoolean(setting.Value, out bool autostart)) + // Only adds: an autorun the user set up in CDPI UI is not removed by the import. + if (TryParseBoolean(setting.Value, out bool autostart) && autostart) { - if (autostart) - ApplicationAutorunManager.AddToAutorun(); - else - ApplicationAutorunManager.RemoveFromAutorun(); + ApplicationAutorunManager.AddToAutorun(); applied++; } break; diff --git a/CDPIUI/Helper/Parsers/HelpParser.cs b/CDPIUI/Helper/Parsers/HelpParser.cs index 04bda281..45ca4cf3 100644 --- a/CDPIUI/Helper/Parsers/HelpParser.cs +++ b/CDPIUI/Helper/Parsers/HelpParser.cs @@ -1,6 +1,7 @@ using CDPIUI.Core.Data; using CDPIUI.Shared.Basic.Filesystem; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -62,7 +63,8 @@ public static List GetHelpItemsForLanguage(string language) return null; } - string[] files = Directory.GetFiles(path); + // Sorted: the file system does not promise an order, and the menu followed it. + string[] files = Directory.GetFiles(path).OrderBy(p => p, StringComparer.OrdinalIgnoreCase).ToArray(); foreach (string file in files) { string id = Path.GetFileNameWithoutExtension(file); @@ -75,7 +77,7 @@ public static List GetHelpItemsForLanguage(string language) Items = [], }); } - string[] folders = Directory.GetDirectories(path); + string[] folders = Directory.GetDirectories(path).OrderBy(p => p, StringComparer.OrdinalIgnoreCase).ToArray(); foreach (string folder in folders) { @@ -83,7 +85,7 @@ public static List GetHelpItemsForLanguage(string language) List helpSubItems = []; - string[] subFiles = Directory.GetFiles(folder); + string[] subFiles = Directory.GetFiles(folder).OrderBy(p => p, StringComparer.OrdinalIgnoreCase).ToArray(); foreach (string subFile in subFiles) { string _id = Path.GetFileNameWithoutExtension(subFile); diff --git a/CDPIUI/Helper/Parsers/UnitsParser.cs b/CDPIUI/Helper/Parsers/UnitsParser.cs index 2a13de81..8765ba0f 100644 --- a/CDPIUI/Helper/Parsers/UnitsParser.cs +++ b/CDPIUI/Helper/Parsers/UnitsParser.cs @@ -57,7 +57,7 @@ public static string ConvertMinutesToPrettyText(double min) if (hours < 1.5) return localizer.GetLocalizedString("/UIHelper/Hour"); else if (hours >= 1.5 && hours <= 2.5) - return localizer.GetLocalizedString("/UIHelper/TwoHour"); + return localizer.GetLocalizedString("/UIHelper/TwoHours"); else return localizer.GetLocalizedString("/UIHelper/MoreThanThreeHours"); } diff --git a/CDPIUI/Helper/ReadyKitBrushFactory.cs b/CDPIUI/Helper/ReadyKitBrushFactory.cs index f5d541dd..4ae650eb 100644 --- a/CDPIUI/Helper/ReadyKitBrushFactory.cs +++ b/CDPIUI/Helper/ReadyKitBrushFactory.cs @@ -46,14 +46,7 @@ public static Brush Create(IEnumerable colors, string fallbackColor = nu private static Color? TryParseColor(string value) { - try - { - return UIHelper.HexToColorConverter(value!); - } - catch - { - return null; - } + return UIHelper.TryParseHexColor(value, out Color color) ? color : null; } private static Color CreateSecondShade(Color color) diff --git a/CDPIUI/Helper/ReadyKitViewModelFactory.cs b/CDPIUI/Helper/ReadyKitViewModelFactory.cs index 85823742..78bb6a50 100644 --- a/CDPIUI/Helper/ReadyKitViewModelFactory.cs +++ b/CDPIUI/Helper/ReadyKitViewModelFactory.cs @@ -29,7 +29,7 @@ public static StoreViewBundleItem Create(ReadyKitModel kit) CardSubtitle = LScriptLangHelper.ExecuteScript( kit.small_description, StoreLocalizationHelper.GetStoreLikeLocale()), - CardImageSource = new BitmapImage(new Uri(LScriptLangHelper.ExecuteScript(kit.icon))), + CardImageSource = CreateIcon(kit.icon), CardBackgroundBrush = ReadyKitBrushFactory.Create( kitItems.Select(item => item.background), kit.background), @@ -38,5 +38,17 @@ public static StoreViewBundleItem Create(ReadyKitModel kit) .ToList() }; } + + // A kit without an icon threw UriFormatException for new Uri(""). + private static BitmapImage CreateIcon(string icon) + { + if (string.IsNullOrWhiteSpace(icon)) + { + return null; + } + return Uri.TryCreate(LScriptLangHelper.ExecuteScript(icon), UriKind.Absolute, out Uri uri) + ? new BitmapImage(uri) + : null; + } } } diff --git a/CDPIUI/Helper/UIHelper.cs b/CDPIUI/Helper/UIHelper.cs index 53c34e23..b934b334 100644 --- a/CDPIUI/Helper/UIHelper.cs +++ b/CDPIUI/Helper/UIHelper.cs @@ -85,15 +85,27 @@ static public void AnnounceActionForAccessibility(UIElement ue, string annouceme AutomationNotificationProcessing.ImportantMostRecent, annoucement, activityID); } - static public Color HexToColorConverter(string hexColor) + /// + /// Parses #RRGGBB or #AARRGGBB. A color from a store manifest is not trusted to be valid. + /// + static public bool TryParseHexColor(string hexColor, out Color color) { - byte r = byte.Parse(hexColor.Substring(1, 2), NumberStyles.HexNumber); - byte g = byte.Parse(hexColor.Substring(3, 2), NumberStyles.HexNumber); - byte b = byte.Parse(hexColor.Substring(5, 2), NumberStyles.HexNumber); - - Logger.Instance.CreateDebugLog(nameof(UIHelper), $"{hexColor} is converted {r} {g} {b}"); - Color color = Color.FromArgb(255, r, g, b); + color = Color.FromArgb(0, 0, 0, 0); + string value = hexColor?.Trim().TrimStart('#'); + if (value == null || (value.Length != 6 && value.Length != 8) || + !uint.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint number)) + { + return false; + } + byte a = value.Length == 8 ? (byte)(number >> 24) : (byte)255; + color = Color.FromArgb(a, (byte)(number >> 16), (byte)(number >> 8), (byte)number); + return true; + } + static public Color HexToColorConverter(string hexColor) + { + if (!TryParseHexColor(hexColor, out Color color)) + Logger.Instance.CreateWarningLog(nameof(UIHelper), $"\"{hexColor}\" is not a color"); return color; } diff --git a/CDPIUI/Helper/UrlOpenHelper.cs b/CDPIUI/Helper/UrlOpenHelper.cs index 1a03e84a..8bc38558 100644 --- a/CDPIUI/Helper/UrlOpenHelper.cs +++ b/CDPIUI/Helper/UrlOpenHelper.cs @@ -18,9 +18,25 @@ public static class UrlOpenHelper public const string NTCParty = "https://ntc.party"; public const string BBD = "https://t.me/byebyedpi_group"; + /// + /// Opens a web link. Links come from store manifests and license data, so file:, ms-* + /// and other schemes that start local programs are refused. + /// public static async void LaunchUrl(string uri) { - _ = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri)); + if (!TryGetWebUri(uri, out Uri webUri)) + { + Core.Basic.Logger.Instance.CreateWarningLog(nameof(UrlOpenHelper), $"Refused to open \"{uri}\""); + return; + } + _ = await Windows.System.Launcher.LaunchUriAsync(webUri); + } + + public static bool TryGetWebUri(string uri, out Uri webUri) + { + return Uri.TryCreate(uri?.Trim(), UriKind.Absolute, out webUri) && + (webUri.Scheme == Uri.UriSchemeHttps || webUri.Scheme == Uri.UriSchemeHttp || + webUri.Scheme == Uri.UriSchemeMailto || webUri.Scheme == "tg"); } public static async void LaunchReportUrl() { diff --git a/CDPIUI/Helper/UserExperience/ConsoleFontHelper.cs b/CDPIUI/Helper/UserExperience/ConsoleFontHelper.cs index 329118c1..4b16ad54 100644 --- a/CDPIUI/Helper/UserExperience/ConsoleFontHelper.cs +++ b/CDPIUI/Helper/UserExperience/ConsoleFontHelper.cs @@ -69,8 +69,8 @@ public bool OnlyMonospace if (Equals(onlyMonospace, value)) return; - SettingsManager.Instance.SetValue("PSEUDOCONSOLE", "showOnlyMonospaceFontsInView", onlyMonospace); onlyMonospace = value; + SettingsManager.Instance.SetValue("PSEUDOCONSOLE", "showOnlyMonospaceFontsInView", onlyMonospace); LoadFonts(); Notify(nameof(OnlyMonospace)); } @@ -105,7 +105,7 @@ public async void ShowFontSettingsDialogForXamlRoot(XamlRoot xamlRoot) { FontSettingsContentDialog dialog = new() { XamlRoot = xamlRoot }; - if (await dialog.ShowAsync() == ContentDialogResult.Primary) + if (await dialog.ShowQueuedAsync() == ContentDialogResult.Primary) { FontFamily = dialog.FontName; FontSize = dialog.FontSize; diff --git a/CDPIUI/Helper/UserExperience/LocalizationHelper.cs b/CDPIUI/Helper/UserExperience/LocalizationHelper.cs index 4a19c541..4412ee7e 100644 --- a/CDPIUI/Helper/UserExperience/LocalizationHelper.cs +++ b/CDPIUI/Helper/UserExperience/LocalizationHelper.cs @@ -73,13 +73,16 @@ private LocalizationHelper() localizer.LanguageChanged += Localizer_LanguageChanged; } + // Falls back to the first language: an unknown id left CurrentLanguage null and the + // next access to CurrentLanguage.Id threw. private LanguageSelectModel FindLanguage(string id) { return Languages.FirstOrDefault( language => string.Equals( language.Id, id, - StringComparison.OrdinalIgnoreCase)); + StringComparison.OrdinalIgnoreCase)) + ?? Languages.First(); } private void Localizer_LanguageChanged( diff --git a/CDPIUI/Helper/ViewModels/WidgetHelper.cs b/CDPIUI/Helper/ViewModels/WidgetHelper.cs index f9656f6c..4582b3f3 100644 --- a/CDPIUI/Helper/ViewModels/WidgetHelper.cs +++ b/CDPIUI/Helper/ViewModels/WidgetHelper.cs @@ -168,7 +168,7 @@ public static async void RunActions(WidgetType actionType, object actionObject, dialog.XamlRoot = xamlRoot; - await dialog.ShowAsync(); + await dialog.ShowQueuedAsync(); break; case WidgetType.LaunchUrl: diff --git a/CDPIUI/Helper/WindowHelper/NativeWindowHelper.cs b/CDPIUI/Helper/WindowHelper/NativeWindowHelper.cs index 8e40c8e6..4cbcedf5 100644 --- a/CDPIUI/Helper/WindowHelper/NativeWindowHelper.cs +++ b/CDPIUI/Helper/WindowHelper/NativeWindowHelper.cs @@ -55,7 +55,7 @@ public static void ForceDisableMaximize(Window window) return nint.Zero; } - if (msg == WM_SYSCOMMAND && wParam.ToInt32() == SC_MAXIMIZE) + if (msg == WM_SYSCOMMAND && (wParam.ToInt64() & 0xFFF0) == SC_MAXIMIZE) { Debug.WriteLine("Maximize via system command suppressed."); return nint.Zero; diff --git a/CDPIUI/Helper/WindowHelper/WindowOpenHelper.cs b/CDPIUI/Helper/WindowHelper/WindowOpenHelper.cs index 8574160c..9cb1f428 100644 --- a/CDPIUI/Helper/WindowHelper/WindowOpenHelper.cs +++ b/CDPIUI/Helper/WindowHelper/WindowOpenHelper.cs @@ -86,12 +86,13 @@ public static async Task OpenAsync(NameValueCollection parameters) private static Type GetWindowType(string windowName) { - var fullName = $"CDPIUI.{windowName}"; - - return AppDomain.CurrentDomain.GetAssemblies() - .Select(a => a.GetType(fullName, false)) - .FirstOrDefault(t => t != null) - ?? null; + // Only windows of this application: the name can come from a cdpiui:// link, and any + // other type found by name would be instantiated before the cast. + var type = typeof(App).Assembly.GetType($"CDPIUI.{windowName}", false); + return type != null && typeof(Window).IsAssignableFrom(type) && !type.IsAbstract && + type.GetConstructor(Type.EmptyTypes) != null + ? type + : null; } private static Type GetPageType(string windowName, string pageName) diff --git a/CDPIUI/Helper/WindowHelper/WindowsPositionHelper.cs b/CDPIUI/Helper/WindowHelper/WindowsPositionHelper.cs index c709d398..e80a9c01 100644 --- a/CDPIUI/Helper/WindowHelper/WindowsPositionHelper.cs +++ b/CDPIUI/Helper/WindowHelper/WindowsPositionHelper.cs @@ -151,6 +151,10 @@ public static void SetWindowOwner(Window window, Window ownerWindow) SetWindowLongPtr(windowHandle, GWLP_HWNDPARENT, ownerHandle); } + private const int MinRestoredWindowSize = 200; + // Windows reports this position for minimized windows. + private const int MinimizedWindowCoordinate = -30000; + public static void SetCustomWindowSizeAndPositionFromSettings(Window window) { WindowResizeOptions resizeOptions = GetWindowResizeOptionsFromWindow(window); @@ -163,7 +167,23 @@ public static void SetCustomWindowSizeAndPositionFromSettings(Window window) bool isMaximized = SettingsManager.Instance.GetValue(["WINDOWS", window.GetType().Name], "isMaximized"); - RECT desiredRect = new RECT { left = x, top = y, right = x + width, bottom = y + height }; + if (width < MinRestoredWindowSize || height < MinRestoredWindowSize) + { + width = 0; + height = 0; + } + if (x <= MinimizedWindowCoordinate || y <= MinimizedWindowCoordinate) + { + x = 0; + y = 0; + } + + // The size is stored in DIPs, the position and the work area are physical pixels. + double scale = GetScaleFactor(window); + int physicalWidth = (int)Math.Round(width * scale); + int physicalHeight = (int)Math.Round(height * scale); + + RECT desiredRect = new RECT { left = x, top = y, right = x + physicalWidth, bottom = y + physicalHeight }; RECT workArea; if (!TryGetMonitorWorkArea(desiredRect, out workArea)) { @@ -172,17 +192,19 @@ public static void SetCustomWindowSizeAndPositionFromSettings(Window window) int maxWidth = workArea.right - workArea.left; int maxHeight = workArea.bottom - workArea.top; - int adjWidth = Math.Min(width, Math.Max(1, maxWidth)); - int adjHeight = Math.Min(height, Math.Max(1, maxHeight)); + int adjPhysicalWidth = Math.Min(physicalWidth, Math.Max(1, maxWidth)); + int adjPhysicalHeight = Math.Min(physicalHeight, Math.Max(1, maxHeight)); int adjX = x; if (adjX < workArea.left) adjX = workArea.left; - if (adjX + adjWidth > workArea.right) adjX = Math.Max(workArea.left, workArea.right - adjWidth); + if (adjX + adjPhysicalWidth > workArea.right) adjX = Math.Max(workArea.left, workArea.right - adjPhysicalWidth); int adjY = y; if (adjY < workArea.top) adjY = workArea.top; - if (adjY + adjHeight > workArea.bottom) adjY = Math.Max(workArea.top, workArea.bottom - adjHeight); + if (adjY + adjPhysicalHeight > workArea.bottom) adjY = Math.Max(workArea.top, workArea.bottom - adjPhysicalHeight); + int adjWidth = (int)Math.Round(adjPhysicalWidth / scale); + int adjHeight = (int)Math.Round(adjPhysicalHeight / scale); SetWindowSize(window, adjWidth, adjHeight); SetWindowPosition(window, adjX, adjY); @@ -213,6 +235,8 @@ public static void SaveWindowSizeAndPostionsettings(Window window) AppWindow appWindow = AppWindow.GetFromWindowId(windowId); var presenter = appWindow.Presenter as OverlappedPresenter; + if (presenter == null || presenter.State == OverlappedPresenterState.Minimized) + return; int width = appWindow.Size.Width; int height = appWindow.Size.Height; @@ -245,8 +269,9 @@ private static void Center(Window window) DisplayArea.GetFromWindowId(windowId, DisplayAreaFallback.Nearest) is DisplayArea displayArea) { PointInt32 CenteredPosition = appWindow.Position; - CenteredPosition.X = (displayArea.WorkArea.Width - appWindow.Size.Width) / 2; - CenteredPosition.Y = (displayArea.WorkArea.Height - appWindow.Size.Height) / 2; + // The work area of a secondary monitor does not start at (0, 0). + CenteredPosition.X = displayArea.WorkArea.X + (displayArea.WorkArea.Width - appWindow.Size.Width) / 2; + CenteredPosition.Y = displayArea.WorkArea.Y + (displayArea.WorkArea.Height - appWindow.Size.Height) / 2; appWindow.Move(CenteredPosition); } } diff --git a/CDPIUI/Program.cs b/CDPIUI/Program.cs index 81da8b8b..dea52ff5 100644 --- a/CDPIUI/Program.cs +++ b/CDPIUI/Program.cs @@ -70,8 +70,22 @@ private static bool DecideRedirection() } else { - isRedirect = true; - RedirectActivationTo(args, keyInstance); + isRedirect = RedirectActivationTo(args, keyInstance); + if (!isRedirect) + { + // The main instance did not take the activation, usually because it is + // exiting: try to become the main instance instead of leaving silently. + keyInstance = AppInstance.FindOrRegisterForKey("CDPI_GUIApp"); + if (keyInstance.IsCurrent) + { + InitialActivationArguments = args; + keyInstance.Activated += OnActivated; + } + else + { + isRedirect = true; + } + } } return isRedirect; @@ -120,7 +134,7 @@ private static void DispatchActivation(AppActivationArguments args) { if (Application.Current is App app) { - app.PrefferRequestedActions(args); + app.PreferRequestedActions(args); } } catch { } @@ -171,23 +185,38 @@ private static extern uint CoWaitForMultipleObjects( // Do the redirection on another thread, and use a non-blocking // wait method to wait for the redirection to complete. - public static void RedirectActivationTo(AppActivationArguments args, + // Returns false when the main instance did not accept the activation in time. + public static bool RedirectActivationTo(AppActivationArguments args, AppInstance keyInstance) { redirectEventHandle = CreateEvent(IntPtr.Zero, true, false, null); AllowSetForegroundWindow(keyInstance.ProcessId); + bool redirected = false; Task.Run(() => { - keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); - SetEvent(redirectEventHandle); + try + { + keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); + redirected = true; + } + catch (Exception) + { + // The main instance exited or crashed meanwhile. + } + finally + { + // Without the event the wait below never ends and the process stays without a window. + SetEvent(redirectEventHandle); + } }); uint CWMO_DEFAULT = 0; - uint INFINITE = 0xFFFFFFFF; + uint RedirectTimeoutMs = 30000; _ = CoWaitForMultipleObjects( - CWMO_DEFAULT, INFINITE, 1, + CWMO_DEFAULT, RedirectTimeoutMs, 1, [redirectEventHandle], out uint handleIndex); + return Volatile.Read(ref redirected); } } } diff --git a/CDPIUI/README.md b/CDPIUI/README.md new file mode 100644 index 00000000..c103c4e4 --- /dev/null +++ b/CDPIUI/README.md @@ -0,0 +1,359 @@ +# CDPIUI + +`CDPIUI` is the WinUI 3 GUI project (.NET 8, Windows App SDK). It shows the windows of the application. +The tray process `CDPIUI_TrayIcon.exe` keeps the components in operation. The GUI connects to the tray through a named pipe. +The logic that does not need the UI is in `CDPIUI.Core`, `CDPIUI.Shared` and `CDPIUI.AddOns`. + +## Startup + +### Program.cs + +`Program.Main` is the entry point. The XAML compiler does not add its own `Main` method to this project. + +1. `Main` calls `DecideRedirection`. +2. `DecideRedirection` registers the key `CDPI_GUIApp` with `AppInstance.FindOrRegisterForKey`. +3. If the current process owns the key, it keeps the activation arguments in `InitialActivationArguments`. Then it starts `App`. +4. If a different process owns the key, `RedirectActivationTo` sends the activation to that process. The wait limit is 30 seconds. +5. If the running instance does not accept the activation, the process tries to register the key again. This occurs when the running instance closes. + +The running instance receives a redirected activation in `OnActivated`. Activations that arrive before the UI thread is ready go into a queue. +After `App` is created, `DispatchPendingActivations` sends them to `App.PreferRequestedActions` on the UI thread. + +`Program` also finds the GoodbyeDPI migration argument (`GoodbyeDpiMigrationActivation`). While a migration is pending, the application does not open the usual startup window. + +### Tray start and `--gui-args:` + +At startup, `PipeClientService` connects to the tray pipe. If it cannot connect, it starts `CDPIUI_TrayIcon.exe` as administrator. Then the GUI process stops. + +- The GUI gives `--autorun` to the tray if the setting `APPEARANCE/hideToTrayOnStartup` is true. Else it gives `--show-ui`. +- The GUI encodes its own arguments with `GuiActivationArguments.Encode` (`CDPIUI.Shared/Basic/CommandLine`). The result is one `--gui-args:` argument. +- Each argument is UTF-8 and Base64. A dot separates the arguments. The maximum encoded length is 16 KiB. A longer value is not sent. +- The tray decodes the value with `GuiActivationArguments.TryDecode`. It starts `CDPIUI.exe` again with the original arguments. + +Thus an opened file, a `cdpiui://` link or a `--direct:` command is not lost when the tray is not running. + +### `--direct:` and other arguments + +The tray starts the GUI with `--direct:` when the GUI is not connected. This occurs for all message targets, for example `ShowWindow` messages. +For background actions, the tray also adds `--create-no-window`. The tray does not use `----ms-protocol:`. + +`App.PipeConnectedActions` processes the arguments after the pipe connects: + +| Argument or activation | Handler | +| ---------------------- | ------- | +| GoodbyeDPI migration argument | `TryHandleMigrationActivationAsync`, opens `WelcomeWindow` with `WelcomeWindow.MigrationWindowId` | +| File activation or file path | `ProcessFiles` | +| Protocol activation (`cdpiui://`) | `CommandsHandler.HandleCommandAsync` (public routes only) | +| `----ms-protocol:` | `CommandsHandler.HandleCommandAsync` (public routes only) | +| `--direct:` | `CoreCommandsHandler.HandleCommandAsync`, then `CommandsHandler.HandleCommand` (pipe messages accepted) | + +`ProcessFiles` accepts `.cdpisignedpack`, `.cdpiconfigpack`, `.cdpipatch`, `.cdpitask` and `.cdpiconfig`. It processes only the first supported file. + +If no argument opens a window, `OpenStartupWindowAsync` opens `WelcomeWindow` or `ModernMainWindow`. `WelcomeWindow` opens until the setting `WELCOMEWIZARD/Shown` is true. + +A redirected activation goes to `App.PreferRequestedActions`. It processes protocol links, files, the migration argument and `--direct:` in the same way. + +### App.xaml.cs + +The `App` constructor does these steps: + +1. It registers handlers for unhandled exceptions. A XAML exception is logged and shown in `ErrorContentDialog`. The application does not close. +2. It connects `PipeClientService.MessageReceived` to `CommandsHandler.HandleCommand`. +3. It applies the theme from `APPEARANCE/Theme`. +4. It starts `PipeClientService` and subscribes to `Connected`. +5. It calls `GetReadyFeatures`. + +`GetReadyFeatures` starts `GetReadyFeaturesAsync` and keeps the task in `_localizerReady`. The task restores the database (`DatabaseInitializationService.QuickRestore`), builds the localizer and registers the file associations and the `cdpiui` protocol (`FileAssociationService`). + +`OnLaunched` creates `PrepareWindow` and calls `PipeClientService.Start`. `PrepareWindow` closes after the startup actions. `App` also creates it when a Store operation runs and no other window is open. + +The application closes in `ExitIfIdle` when no window is open and no Store or GoodCheck operation runs. + +### Localizer readiness + +The windows read their strings from the localizer in their constructors. Thus a window must not be created before the localizer is built. +All overloads of `SafeCreateNewWindow` and `UnsafeCreateNewWindow` wait for `_localizerReady` before they create a window. + +CAUTION: Do not create a window with `new` during startup. Use the `App` window methods, because they wait for the localizer. + +## Windows + +### Namespace rule + +All windows are in the `CDPIUI` namespace, not in `CDPIUI.Windows`. The files are in the `Windows` folder. +`WindowOpenHelper` finds a window type by the name `CDPIUI.`. A window in a different namespace cannot open from a link. +The dialog windows in `Windows/Messages` that use `CDPIUI.Messages` cannot open from a link. `TemplateWindow` is in `CDPIUI.Default`. + +### TemplateWindow + +Most windows derive from `TemplateWindow` (`Windows/Default/TemplateWindow.cs`), which derives from `WinUIEx.WindowEx`. The legacy `MainWindow` derives directly from `WindowEx`. +`TemplateWindow` gives these functions: + +- The custom title bar (`CustomTitleBarUserControl`, `TitleBarMode` from `APPEARANCE/titleBarMode`). +- `WindowTitle`, `IconUri` and `WindowMinSize`. +- `Id`: an identifier for windows that can have more than one instance. +- `MainFrame` and `NavigateToPageWithParameter`, which sends a `NameValueCollection` to the page. +- `NavigateBackWithParameter` and `RemoveAndGoBackTo` for back navigation. + +### Open a window + +`App.OpenWindows` contains all open windows. `RegisterWindow` adds a window to the list, applies the theme and subscribes to `Closed`. +When a window closes, `App` saves its size and position (`WindowsPositionHelper`) and removes it from the list. + +| Method | Use | +| ------ | --- | +| `SafeCreateNewWindow()` | One instance of a window type. If one instance is open, the method activates it. If more instances are open, the method closes them and makes a new window. | +| `SafeCreateNewWindow(Type)` | The same, for a type from a link. | +| `UnsafeCreateNewWindow(activate, id)` | More instances of one type. A window with the same `Id` is activated. Else a new window is created. | +| `UnsafeCreateNewWindow(Type, activate, id)` | The same, for a type from a link. | +| `ShowWindowModalAsync` / `MakeWindowNormal` | Disable the owner windows while a window is modal, then enable them again. | +| `CloseWindow()` / `CloseWindow(id)` | Close all windows of a type, or one window with an `Id`. | + +Do not create a window with `new` and `Activate` directly. Use the `App` methods, else `OpenWindows`, the theme and `ExitIfIdle` do not include the window. + +### WindowOpenHelper + +`Helper/WindowHelper/WindowOpenHelper.OpenAsync` opens a window from a `ShowWindow` message. The message parameters are `windowName`, `page`, `id` and page parameters. + +- `MainWindow` changes to `ModernMainWindow`. `LegacyMainWindow` changes to `MainWindow`. +- If `WelcomeWindow` is open or the welcome wizard was not completed, a request for the main window opens the startup window. +- The type must be in the application assembly and in the `CDPIUI` namespace. It must derive from `Window`, must not be abstract and must have a public constructor without parameters. +- With `id`, the helper uses `UnsafeCreateNewWindow`. Without `id`, it uses `SafeCreateNewWindow`. +- For `OfflineHelpWindow`, the `helpUrl` parameter goes to `NavigateToPage`. +- Only a `TemplateWindow` can receive a page. The helper finds the page type with `GetPageType`. + +`GetPageType` tries to find `CDPIUI.Views..`. For `ModernMainWindow`, it uses `CDPIUI.Views.Main.` and `CDPIUI.Views.`. `HomePage` gives `ModernMainWindow.GetMainPage()`. +For `StoreWindow`, `HomePage` is `CDPIUI.HomePage`. A page name can contain a sub-namespace, for example `Settings.Memory.MemoryViewLogsDetailsPage`. + +## The `cdpiui://` link scheme + +The files are in `Commands`: + +- `CommandUriMapper` converts a link to an `IPipeMessage`. +- `CommandsHandler` runs the message. +- `ExternalUriLauncher` opens web links in XAML with `UrlOpenHelper`. + +The host and path segments are not case-sensitive. A `{name}` segment is a parameter. A `{*name}` segment takes the rest of the path. + +### Public routes + +| Link | Result | +| ---- | ------ | +| `cdpiui://AppFeatures` | `AppFeaturesWindow` | +| `cdpiui://Welcome` | `WelcomeWindow` | +| `cdpiui://Main` | Main window | +| `cdpiui://Main/Home` | Main window, main page | +| `cdpiui://Main/Utilities` | Main window, `UtilsPage` | +| `cdpiui://Main/Settings` | Main window, `SettingsPage` | +| `cdpiui://Main/Settings/Autorun` | Main window, `Settings.AutorunPage` | +| `cdpiui://Main/Settings/Personalization` | Main window, `Settings.PersonalizePage` | +| `cdpiui://Main/About` | Main window, `AboutPage` | +| `cdpiui://Main/Updates` | `AboutPage` with `isUpdateRequested=True` | +| `cdpiui://Main/Components/{componentId}` | `Components.ViewComponentSettingsPage` with `componentId` | +| `cdpiui://Store` | `StoreWindow` | +| `cdpiui://Store/Home` | `StoreWindow`, `HomePage` | +| `cdpiui://Store/Catalog/{itemId}` | `ItemViewPage` with `itemId` and `setFocus=ItemActionButton` | +| `cdpiui://Store/Category/{categoryId}` | `CategoryViewPage` with `categoryId` | +| `cdpiui://Store/Downloads` | `DownloadsPage` | +| `cdpiui://Store/Updates` | `DownloadsPage` with `isUpdateRequested=True` | +| `cdpiui://Store/Library` | `LibraryPage` | +| `cdpiui://Store/Settings` | `SettingsPage` | +| `cdpiui://Store/Settings/Memory` | `Settings.MemoryViewPage` | +| `cdpiui://Store/Settings/Memory/Application` | `MemoryViewApplicationFilesDetailsPage` | +| `cdpiui://Store/Settings/Memory/InstalledItems` | `MemoryViewInstalledItemsDetailsPage` | +| `cdpiui://Store/Settings/Memory/Logs` | `MemoryViewLogsDetailsPage` | +| `cdpiui://Store/Settings/Memory/Settings` | `MemoryViewSettingsDetailsPage` | +| `cdpiui://Store/Settings/Memory/StoreCache` | `MemoryViewStoreCachePage` | +| `cdpiui://Store/Settings/Memory/ConditionalLaunch` | `MemoryViewConditionalLaunchDetailsPage` | +| `cdpiui://Tools/Console/{componentId}` | `ViewWindow` with `id` | +| `cdpiui://Tools/Service` | `ServiceWindow` | +| `cdpiui://Tools/ConditionalLaunch` | `ConditionalLaunchWindow` | +| `cdpiui://Tools/AutoConfig` | `CreateConfigUtilWindow` | +| `cdpiui://Tools/AutoConfig/Zapret2` | `CreateConfigUtilWindow`, `MainPage`, Zapret2, `action=BlockCheck2` | +| `cdpiui://Tools/AutoConfig/Zapret2/Reports` | The same, `action=BlockCheck2Reports` | +| `cdpiui://Tools/AutoConfig/{componentId}` | `CreateConfigUtilWindow`, `MainPage` with `componentId` | +| `cdpiui://Tools/ConfigEditor` | `ConfigMakerWindow` | +| `cdpiui://Tools/ConfigEditorLegacy` | `CreateConfigHelperWindow` | +| `cdpiui://Tools/ImportConfig` | `ConfigImportUtilWindow` | +| `cdpiui://Tools/ImportConfig/{componentId}` | `ConfigImportUtilWindow`, `MainPage` with `componentId` | +| `cdpiui://Tools/CreateConfig/{componentId}` | `ConfigMakerWindow` with `id` | +| `cdpiui://Tools/EditConfigKit/{kitId}` | `CreateConfigHelperWindow`, `EditConfigKitPage` with `kitId` | +| `cdpiui://Tools/Help` | `OfflineHelpWindow` | +| `cdpiui://Tools/Proxy` | `ProxySetupUtilWindow` | +| `cdpiui://Tools/Troubleshooting` | `TroubleshootingWindow` | +| `cdpiui://Tools/Troubleshooting/BasicCheck` | `WorkPage`, `action=BeginBasicCheck` | +| `cdpiui://Tools/Troubleshooting/StoreCheck` | `WorkPage`, `action=BeginStoreRepoCheck` | +| `cdpiui://Tools/PresetTest` | `ConfigTestWindow` | +| `cdpiui://Tools/Hosts` | `EditHostFileWindow` | +| `cdpiui://Help` | `OfflineHelpWindow` | +| `cdpiui://Help/{*helpUrl}` | `OfflineHelpWindow` with `helpUrl=/` | +| `cdpiui://Actions/CheckForUpdates` | Application update check with a notification | +| `cdpiui://Actions/CompatibilityCheck` | Start of the compatibility check | + +The order of the routes is important. The first route that matches is used. For example, `Tools/AutoConfig/Zapret2` is before `Tools/AutoConfig/{componentId}`. + +### Pipe messages are not accepted from links + +`CommandUriMapper.ConvertBack` has the parameter `allowPipeMessages`. Its default value is `false`. + +- If a host has routes and no route matches, the result is `null`. +- If the host has no routes, the link is converted with `PipeModelConvertor` only when `allowPipeMessages` is `true`. + +`CommandsHandler.HandleCommandAsync(string)` is for links from outside the application: protocol activation, `----ms-protocol:`, the in-app help. It accepts only public routes. +`CommandsHandler.HandleCommand(string)` is for links that the application makes itself, for example `CommandParameter` in XAML. It also accepts pipe messages. + +WARNING: Do not send a link from a browser, a file or a help page to `HandleCommand(string)`. A web page could then forge tray messages. + +`CommandsHandler` runs `PresentationMessageModel` (`ShowWindow` only), `GoodCheckMessageModel`, `UtilsMessageModel`, `UpdateMessageModel` and `CompatibilityCheckMessageModel`. It ignores other message types. + +To bind a link in XAML, use `Command="{x:Bind commands:CommandsHandler.Instance.Command}"` and put the link in `CommandParameter`. + +### Add a route + +1. Add a `CommandRoute` to `CommandUriMapper.Routes`. For a window, use `CreateWindowRoute`. +2. Put a specific route before a route with a parameter. +3. Make sure that the window is in the `CDPIUI` namespace. +4. Make sure that the page is in `CDPIUI.Views.`. + +## Pages and views + +The pages are in `Views`. Most pages derive from `TemplatePage` (`Controls/Default/TemplatePage.cs`). + +| Folder | Window | +| ------ | ------ | +| `Views` (root) | Main window pages: `MainPage`, `ModernMainPage`, `UtilsPage`, `SettingsPage`, `AboutPage` | +| `Views/Settings` | Main window settings pages | +| `Views/Main/Components` | Component settings pages | +| `Views/Store`, `Views/Store/Settings`, `Views/Store/Settings/Memory` | `StoreWindow` | +| `Views/CreateConfigUtil` | `CreateConfigUtilWindow` | +| `Views/BlockCheck2` | Zapret2 preset selection | +| `Views/ConfigImportUtil` | `ConfigImportUtilWindow` | +| `Views/CreateConfigHelper` | `CreateConfigHelperWindow` (legacy preset editor) | +| `Views/EditHostsFile` | `EditHostFileWindow` | +| `Views/SetupProxy` | `ProxySetupUtilWindow` | +| `Views/Troubleshooting` | `TroubleshootingWindow` | + +The namespace of a page agrees with its folder, with one exception: `Views/Store/HomePage` is in `CDPIUI`. +The pages get parameters as a `NameValueCollection` in `OnNavigatedTo`. + +## ContentDialogQueue + +A window can show only one `ContentDialog` at a time. A second `ShowAsync` call causes an exception. +`ContentDialogQueue.ShowQueuedAsync` (`Helper/Basic/ContentDialogQueue.cs`) is an extension method. It waits for the previous dialogs in the same `XamlRoot`. + +```csharp +dialog.XamlRoot = this.XamlRoot; +var result = await dialog.ShowQueuedAsync(); +``` + +Set `XamlRoot` before the call. If `XamlRoot` is `null`, the method calls `ShowAsync` without the queue. +Use `ShowQueuedAsync` for all new `ContentDialog` calls. `MessageDialog` is a different API and does not use this queue. + +## Localization + +The application uses `WinUI3Localizer`. The strings are in `Strings//*.resw`. The languages are `en-us` and `ru`. + +| File | Prefix in code | +| ---- | -------------- | +| `Resources.resw` | None, for example `GetLocalizedString("SomethingWentWrong")` | +| Other files, for example `Help.resw` | `//`, for example `GetLocalizedString("/Help/NotFoundTemplate")` | + +In XAML, use `l:Uids.Uid="Key"`. The key `Key.Text` or `Key.Content` sets the property. + +`App.InitializeLocalizer` reads `SYSTEM/language`. If the value is empty, `NaN`, not a valid folder name or not a folder in `Strings`, it uses the UI culture: `ru` gives `ru`, other cultures give `en-us`. +`LocalizationHelper.SaveLanguageSettings` changes the language and saves `SYSTEM/language`. + +To add a string: + +1. Add the key to the `.resw` file in `Strings/en-us`. +2. Add the same key to the same file in `Strings/ru`. + +The `.resw` files are `Content` items and are not compiled into the PRI file (`PRIResource Remove` in `CDPIUI.csproj`). If you add a new `.resw` file, add the same two entries in `CDPIUI.csproj`. + +## In-app help + +The help is a set of Markdown files in `Help//`. `CDPIUI.csproj` copies all files with ``. A new file does not need a project entry. +`OfflineHelpWindow` shows the files with `MarkdownTextBlock` (CommunityToolkit Labs). + +### Language + +`OfflineHelpWindow` calls `HelpParser.GetHelpItemsForLanguage(StoreLocalizationHelper.GetStoreLikeLocale())`. `ru` gives `RU`. All other languages give `EN`. +There is no fallback for one article. If an article is missing in `RU`, the Russian help does not show it, and a link to it shows `/Help/NotFoundTemplate`. If the language folder is missing, the menu is empty. + +CAUTION: Add each article to `Help/EN` and to `Help/RU` with the same path and file name. + +### Menu structure + +`HelpParser` reads the language folder once, when the window opens: + +- A file in the root of the language folder is a top-level menu item. `WelcomeToHelp.md` is selected at the start. +- A subfolder is a category. The files in the subfolder are the articles of the category. Deeper folders are not read. +- The menu name is the `/Help/` string from `Help.resw`. If the string is missing, the menu shows the file or folder name. +- The category icon comes from `HelpParser.HelpIconGlyphsPairs`. A category without an entry has no icon. +- `HelpParser` sorts the files and the folders by name (ordinal, not case-sensitive). The root files come before the categories. A number prefix, for example `1AboutConfigImport.md`, keeps the articles in the correct sequence. + +To add an article: + +1. Add `Help/EN//
.md`. +2. Add `Help/RU//
.md`. +3. Add the `
` key to `Strings/en-us/Help.resw` and `Strings/ru/Help.resw`. +4. For a new category, also add the `` key and an icon in `HelpParser.HelpIconGlyphsPairs`. + +Example: the article `Help/EN/Utils/TroubleshootingUtility.md` has the key `TroubleshootingUtility` in both `Help.resw` files. + +### Routes and links + +The help route is `cdpiui://Help//
/`. The article name is the file name without `.md`. The comparison is not case-sensitive. `cdpiui://Help//` selects the category. +`OfflineHelpWindow.NavigateToPage` uses only the first two path segments. + +The window compares the route segments with the file or folder name without `.md`. Thus a root article opens with `cdpiui://Help/
/`, for example `cdpiui://Help/WelcomeToHelp/`. `cdpiui://Help/` opens the window with `WelcomeToHelp.md`. + +In an article, link to a different article with a `cdpiui://Help/...` link. Links to other public routes are also possible, for example `cdpiui://Tools/ImportConfig`. +`MarkdownTextBlock_OnLinkClicked` sends `cdpiui://` links to `CommandsHandler.HandleCommandAsync`. Thus only public routes operate in help pages. + +The help window sends all other links to `UrlOpenHelper.LaunchUrl`. This method opens only `http`, `https`, `mailto` and `tg` links. It writes a warning to the log for other links. + +Code opens help with `CommandsHandler.HandleCommand("cdpiui://Help//
/")`, with the `HelpUrl` property of `UtilityButtonControls`, or with `$INTERNAL_HELP(/
)` in `Flashlight.resw`. + +## ELUA + +The end user terms are in `ELUA/EN/ELUA.md` and `ELUA/RU/ELUA.md`. `CDPIUI.csproj` copies them as `Content`. +`WelcomeWindow.TryLoadLicense` loads `ELUA//ELUA.md` from `Directories.ELUADirectory` (application directory). The language comes from `GetStoreLikeLocale`. +If the file cannot load, the agreement check box is disabled and `/WelcomeWizard/UnableLoadLicense` shows. When the user accepts, `WELCOMEWIZARD/Shown` becomes `true`. + +## Settings + +`SettingsManager` (`CDPIUI.Core`) reads and writes `Settings/Settings.xml` in `Directories.DataDirectory`. The data directory is the application directory, or `%LOCALAPPDATA%\CDPIUI` if the application directory is protected or the application has an MSI installation (`State.IsApplicationBuildAsMsi`). +The file has groups and keys: `GetValue("GROUP", "key")`, `SetValue("GROUP", "key", value)` and `GetValueOrDefault`. + +The default values are in `Template/Settings/Settings.xml`. If a key is missing in the user file, `SettingsManager` reads the default value from this template. + +To add a setting: + +1. Add the `Setting` element to the correct `Group` in `Template/Settings/Settings.xml`. +2. Read it with `SettingsManager.Instance.GetValue`. + +Frequent groups are `APPEARANCE`, `SYSTEM`, `NOTIFICATIONS`, `PSEUDOCONSOLE`, `CONFIGS` and `ZAPRET2`. Some keys, for example `WELCOMEWIZARD/Shown`, are not in the template. Read these keys with `GetValueOrDefault`. + +## Folder structure + +| Folder | Contents | +| ------ | -------- | +| `Assets` | Icons, images, Store file icons, previews | +| `Commands` | `cdpiui://` link mapping and handling | +| `ConditionalLaunch` | UI catalog of Conditional Launch triggers and actions | +| `Controls` | User controls and dialogs (`Controls/Dialogs/`), `TemplatePage` | +| `Converters` | XAML value converters | +| `ELUA` | End user terms (`EN`, `RU`) | +| `Extensions` | Extension methods | +| `Help` | In-app help articles (`EN`, `RU`) | +| `Helper` | UI helpers: windows, help parser, database, migration, add-on UI, `ContentDialogQueue` | +| `Strings` | `.resw` string resources (`en-us`, `ru`) | +| `Template` | Default settings (`Template/Settings/Settings.xml`) | +| `ViewModels` | View models and `RelayCommand` | +| `Views` | Pages, by window | +| `Windows` | Windows (`CDPIUI` namespace), `Windows/Default/TemplateWindow.cs`, `Windows/Messages` (dialog windows), `Windows/Service` (`PrepareWindow`, `CriticalErrorHandlerWindow`) | + +The root contains `App.xaml`, `App.xaml.cs`, `Program.cs`, `Package.appxmanifest`, `app.manifest` and `RemoveLanguageFolders.targets`. + +The `CDPIUI.Tests` project does not test this project. See `CDPIUI.Tests/README.md`. diff --git a/CDPIUI/Strings/en-us/GoodCheckAdditionalSettings.resw b/CDPIUI/Strings/en-us/GoodCheckAdditionalSettings.resw index ce248d6d..ab1dacd7 100644 --- a/CDPIUI/Strings/en-us/GoodCheckAdditionalSettings.resw +++ b/CDPIUI/Strings/en-us/GoodCheckAdditionalSettings.resw @@ -186,4 +186,7 @@ Requests + + DoH resolvers + \ No newline at end of file diff --git a/CDPIUI/Strings/en-us/Help.resw b/CDPIUI/Strings/en-us/Help.resw index f1d8a918..10392bc5 100644 --- a/CDPIUI/Strings/en-us/Help.resw +++ b/CDPIUI/Strings/en-us/Help.resw @@ -188,6 +188,9 @@ Unfortunately we can't find page your looking for. Please, make sure program has Hosts file utility + + Troubleshooting utility + Autoselection utilities diff --git a/CDPIUI/Strings/en-us/Resources.resw b/CDPIUI/Strings/en-us/Resources.resw index 6395c4fd..e8b6bfde 100644 --- a/CDPIUI/Strings/en-us/Resources.resw +++ b/CDPIUI/Strings/en-us/Resources.resw @@ -133,11 +133,22 @@ Add new component from the Store + + The proxy will be reachable from the network + + + The proxy component will listen on {0}. Other devices in your network, and possibly the internet, will be able to use it without a password. +Use 127.0.0.1 if the proxy is only needed on this computer. + + + The files of add-on "{0}" are incomplete: "{1}" is missing. Continuation is impossible. +Please, reinstall "{0}" in Store + AddOn "{0}" with Id "{1}" not installed. Continuation is impossible. Please, install lates version of "{0}" in Store - + Application is runed in system tray @@ -666,7 +677,7 @@ Please, install lates version of "{0}" in Store Stop - + Stop downloading @@ -1622,7 +1633,7 @@ Some features may work not as planned — don't worry. Just say about it to deve What does not work as planned? - All configs not work to me + None of the configs work for me My problem is not on the list @@ -1657,7 +1668,7 @@ Some features may work not as planned — don't worry. Just say about it to deve Exclusive group - + Remove File @@ -1724,7 +1735,7 @@ DO NOT install packs from source you don't trust. Application patch - All supported file extentions + All supported file extensions GitHub @@ -1976,7 +1987,7 @@ Here is error code in case you need it. Cancel - + Back @@ -2590,7 +2601,7 @@ Original developer: Niko ByeByeDPI Community - + Discussion group about BBD – mobile ByeDPI client @@ -6169,4 +6180,97 @@ By enabling this setting, you acknowledge that you have read this information. Connect Telegram to proxy + + Arguments + + + Remove + + + Something went wrong + + + Where: + + + Why: + + + Error code: + + + Additional information: + + + If this error persists, please contact app support and report the issue. + + + View log folder + + + Report problem + + + Report problem + + + Copy + + + Copied + + + Critical error + + + Choose a preset file + + + Choose a GoodCheck report file + + + Choose a text file + + + Choose a file + + + JSON presets + + + BAT presets + + + CMD presets + + + All supported presets + + + XML data files + + + Add widget to home page + + + More actions + + + Share + + + Restart required + + + An installed package asked for a system restart. Components that use it may not work until you restart the computer. + + + Restart now + + + Later + + + GoodCheck has no strategy lists for this component. Install a strategy pack that supports it or reinstall the add-on. + \ No newline at end of file diff --git a/CDPIUI/Strings/en-us/Troubleshooting.resw b/CDPIUI/Strings/en-us/Troubleshooting.resw index 38837108..7bd3ca84 100644 --- a/CDPIUI/Strings/en-us/Troubleshooting.resw +++ b/CDPIUI/Strings/en-us/Troubleshooting.resw @@ -243,7 +243,7 @@ Preparing... - + RUNNED diff --git a/CDPIUI/Strings/ru/GoodCheckAdditionalSettings.resw b/CDPIUI/Strings/ru/GoodCheckAdditionalSettings.resw index 47216bcc..6a1b8516 100644 --- a/CDPIUI/Strings/ru/GoodCheckAdditionalSettings.resw +++ b/CDPIUI/Strings/ru/GoodCheckAdditionalSettings.resw @@ -186,4 +186,7 @@ Запросы + + DoH резолверы + \ No newline at end of file diff --git a/CDPIUI/Strings/ru/Help.resw b/CDPIUI/Strings/ru/Help.resw index 1365203b..70df231b 100644 --- a/CDPIUI/Strings/ru/Help.resw +++ b/CDPIUI/Strings/ru/Help.resw @@ -188,6 +188,9 @@ Утилита изменения файла hosts + + Утилита устранения неполадок + Средства автоподбора diff --git a/CDPIUI/Strings/ru/Resources.resw b/CDPIUI/Strings/ru/Resources.resw index 5cfb9372..0b5c398a 100644 --- a/CDPIUI/Strings/ru/Resources.resw +++ b/CDPIUI/Strings/ru/Resources.resw @@ -133,11 +133,22 @@ Загрузите новые компоненты из Магазина + + Прокси будет доступен из сети + + + Компонент будет принимать подключения на адресе {0}. Другие устройства в вашей сети, а возможно и из интернета, смогут пользоваться прокси без пароля. +Укажите 127.0.0.1, если прокси нужен только на этом компьютере. + + + Файлы надстройки "{0}" повреждены: отсутствует "{1}". Продолжение невозможно. +Переустановите "{0}" в Магазине + Надстройка "{0}" с уникальным идентификатором "{1}" не установлена. Продолжение невозможно. Перейдите в Магазин, чтобы установить последнюю версию "{0}" - + Приложение запущено в системном лотке уведомлений @@ -666,7 +677,7 @@ Остановить - + Остановить загрузку @@ -1660,7 +1671,7 @@ Взаимоисключающая группа - + Удалить файл @@ -1978,7 +1989,7 @@ ERR_STORE_LATEST_VERSION_ALREADY_INSTALLED Отмена - + Назад @@ -2592,7 +2603,7 @@ ERR_STORE_LATEST_VERSION_ALREADY_INSTALLED Сообщество ByeByeDPI - + Группа обсуждения ByeByeDPI – мобильного клиента ByeDPI @@ -5599,7 +5610,7 @@ https://discord.com/app Оценить эту функцию приложения - Недавние поборы пресета для Zapret2 + Недавние подборы пресета для Zapret2 Подключение к Магазину... @@ -6172,4 +6183,97 @@ https://discord.com/app Подключить Telegram к прокси + + Аргументы + + + Удалить + + + Что-то пошло не так + + + Где: + + + Почему: + + + Код ошибки: + + + Дополнительные сведения: + + + Если ошибка повторяется, сообщите о ней в поддержку приложения. + + + Открыть папку журналов + + + Сообщить о проблеме + + + Сообщить о проблеме + + + Копировать + + + Скопировано + + + Критическая ошибка + + + Выберите файл пресета + + + Выберите файл отчёта GoodCheck + + + Выберите текстовый файл + + + Выберите файл + + + Пресеты JSON + + + Пресеты BAT + + + Пресеты CMD + + + Все поддерживаемые пресеты + + + Файлы данных XML + + + Добавить виджет на главную + + + Другие действия + + + Поделиться + + + Нужна перезагрузка + + + Установленный пакет запросил перезагрузку системы. Компоненты, которые его используют, могут не работать до перезагрузки компьютера. + + + Перезагрузить + + + Позже + + + У GoodCheck нет списков стратегий для этого компонента. Установите набор стратегий с его поддержкой или переустановите дополнение. + \ No newline at end of file diff --git a/CDPIUI/Strings/ru/Troubleshooting.resw b/CDPIUI/Strings/ru/Troubleshooting.resw index 9675de39..02ce3a3c 100644 --- a/CDPIUI/Strings/ru/Troubleshooting.resw +++ b/CDPIUI/Strings/ru/Troubleshooting.resw @@ -157,7 +157,7 @@ Прокси-сервер - Службы SmartByte + Службы SmartByte TCP timestamps @@ -243,7 +243,7 @@ Выполняется подготовка... - + ЗАПУЩЕНО diff --git a/CDPIUI/ViewModels/BlockCheck2ResultViewModel.cs b/CDPIUI/ViewModels/BlockCheck2ResultViewModel.cs index fd3fa69a..7b0db041 100644 --- a/CDPIUI/ViewModels/BlockCheck2ResultViewModel.cs +++ b/CDPIUI/ViewModels/BlockCheck2ResultViewModel.cs @@ -207,7 +207,7 @@ public void ApplyStrategyView(string? searchText, int sortMode, int filterMode) { StrategyEvidence.Add(item); BlockCheck2SiteStrategyGroup? group = StrategyGroups.FirstOrDefault(candidate => - string.Equals(candidate.SiteUrl, item.SiteUrl, StringComparison.CurrentCultureIgnoreCase)); + string.Equals(candidate.SiteUrl, item.SiteUrl, StringComparison.OrdinalIgnoreCase)); if (group == null) { group = new BlockCheck2SiteStrategyGroup(item.SiteUrl); diff --git a/CDPIUI/ViewModels/BlockCheck2ViewModel.cs b/CDPIUI/ViewModels/BlockCheck2ViewModel.cs index 4e6b9b71..37d94213 100644 --- a/CDPIUI/ViewModels/BlockCheck2ViewModel.cs +++ b/CDPIUI/ViewModels/BlockCheck2ViewModel.cs @@ -343,6 +343,7 @@ public double RequestsPerDomain ResetResultState(); remainingTimeEstimator.Reset(); runStopwatch.Restart(); + CancellationTokenSource? runCancellation = null; try { BlockCheckSessionPreparationResult prepared = preparationService.Prepare(CreateSessionRequest()); @@ -357,7 +358,8 @@ public double RequestsPerDomain return null; } - cancellation = new CancellationTokenSource(); + runCancellation = new CancellationTokenSource(); + cancellation = runCancellation; SetState(BlockCheckSessionState.Running); Progress progress = new(UpdateProgress); BlockCheckRunResult runResult = await addOnService.RunWithCdpiuiAdaptersAsync( @@ -374,7 +376,7 @@ public double RequestsPerDomain ConnectTimeout = TimeSpan.FromSeconds(connectTimeoutSeconds), RequestTimeout = TimeSpan.FromSeconds(requestTimeoutSeconds), }, - cancellationToken: cancellation.Token); + cancellationToken: runCancellation.Token); BlockCheckRunResult result = new() { @@ -408,13 +410,15 @@ public double RequestsPerDomain issues = lastReport.Issues; NotifyResult(); CompleteDisplayedProgress(); - SetState(result.WasCanceled + BlockCheckSessionState finalState = result.WasCanceled ? BlockCheckSessionState.Canceled : result.Success ? BlockCheckSessionState.Completed : result.Synthesis?.IsBestEffort == true ? BlockCheckSessionState.CompletedWithWarnings - : BlockCheckSessionState.Failed); + : BlockCheckSessionState.Failed; + // The final state allows a new run, so it is set only after the report is saved; + // otherwise a second run started meanwhile had its CTS disposed by this one. try { await reportHistoryService.SaveAsync(lastReport); @@ -427,6 +431,7 @@ public double RequestsPerDomain nameof(BlockCheck2ViewModel), $"Cannot save BlockCheck2 report to history: {exception.Message}"); } + SetState(finalState); return result; } catch (OperationCanceledException) @@ -448,8 +453,11 @@ public double RequestsPerDomain estimatedOverallRemainingTime = null; Notify(nameof(EstimatedRemainingTime)); Notify(nameof(EstimatedOverallRemainingTime)); - cancellation?.Dispose(); - cancellation = null; + runCancellation?.Dispose(); + if (ReferenceEquals(cancellation, runCancellation)) + { + cancellation = null; + } CleanupGeneratedSiteListsIfUnviewable(); NotifyActionState(); } diff --git a/CDPIUI/ViewModels/ConfigImportPresetViewModel.cs b/CDPIUI/ViewModels/ConfigImportPresetViewModel.cs index 51fa8323..19125703 100644 --- a/CDPIUI/ViewModels/ConfigImportPresetViewModel.cs +++ b/CDPIUI/ViewModels/ConfigImportPresetViewModel.cs @@ -50,6 +50,15 @@ public ConfigImportPresetViewModel( public string FileName => Path.GetFileName(Result.SourcePath); + // Shown as written in the preset, without expanding variables or running scripts, + // so the user sees what the component will be started with before testing or saving it. + public string Arguments => Result.Config?.startup_string ?? string.Empty; + + public string ArgumentsHeader => localizer.GetLocalizedString("ConfigImportArguments"); + + public Visibility ArgumentsVisibility => + string.IsNullOrWhiteSpace(Arguments) ? Visibility.Collapsed : Visibility.Visible; + public IReadOnlyCollection MissingFileResolutions => missingFileResolutions.Values; diff --git a/CDPIUI/Views/AboutPage.xaml.cs b/CDPIUI/Views/AboutPage.xaml.cs index a44dad91..68cf90d9 100644 --- a/CDPIUI/Views/AboutPage.xaml.cs +++ b/CDPIUI/Views/AboutPage.xaml.cs @@ -100,9 +100,9 @@ public AboutPage() SetServerVersion(ApplicationUpdate.Instance.ServerVersion); } - ApplicationUpdate.Instance.ErrorHappens += ApplicationUpdateHelper_ErrorHappens; - ApplicationUpdate.Instance.CheckForUpdatesCompleted += ApplicationUpdateHelper_CheckForUpdatesCompleted; - ApplicationUpdate.Instance.CheckForUpdatesStarted += ApplicationUpdateHelper_CheckForUpdatesStarted; + ConnectEvents(); + Loaded += (_, _) => ConnectEvents(); + Unloaded += (_, _) => DisconnectEvents(); RepoRun.Text = UrlOpenHelper.MainRepoUrl; @@ -110,6 +110,27 @@ public AboutPage() RequirementsListView.ItemsSource = RequrementsList; } + // The services are singletons: a page that is not shown must not keep receiving their events. + private bool eventsConnected; + + private void ConnectEvents() + { + if (eventsConnected) return; + eventsConnected = true; + ApplicationUpdate.Instance.ErrorHappens += ApplicationUpdateHelper_ErrorHappens; + ApplicationUpdate.Instance.CheckForUpdatesCompleted += ApplicationUpdateHelper_CheckForUpdatesCompleted; + ApplicationUpdate.Instance.CheckForUpdatesStarted += ApplicationUpdateHelper_CheckForUpdatesStarted; + } + + private void DisconnectEvents() + { + if (!eventsConnected) return; + eventsConnected = false; + ApplicationUpdate.Instance.ErrorHappens -= ApplicationUpdateHelper_ErrorHappens; + ApplicationUpdate.Instance.CheckForUpdatesCompleted -= ApplicationUpdateHelper_CheckForUpdatesCompleted; + ApplicationUpdate.Instance.CheckForUpdatesStarted -= ApplicationUpdateHelper_CheckForUpdatesStarted; + } + protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); @@ -128,7 +149,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) private void ApplicationUpdateHelper_ErrorHappens() { - _ = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(() => + _ = DispatcherQueue?.TryEnqueue(() => { SetUpdateButtonStatus(ApplicationUpdate.Instance.IsUpdateAvailable? UpdateButtonStatus.ActionInstallAsk : UpdateButtonStatus.Ready); SetBadgeStatus(BadgeStatus.Error); @@ -138,7 +159,7 @@ private void ApplicationUpdateHelper_ErrorHappens() private void ApplicationUpdateHelper_CheckForUpdatesStarted() { - _ = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(() => + _ = DispatcherQueue?.TryEnqueue(() => { SetUpdateButtonStatus(UpdateButtonStatus.Work); SetBadgeStatus(BadgeStatus.Work); @@ -146,8 +167,12 @@ private void ApplicationUpdateHelper_CheckForUpdatesStarted() } private void ApplicationUpdateHelper_CheckForUpdatesCompleted() { - _ = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(() => + // Raised on the thread of the check, which may have no dispatcher queue. + _ = DispatcherQueue?.TryEnqueue(() => { + // After a failed check the error handler has already set the state. + if (ApplicationUpdate.Instance.ErrorHappened) + return; SetUpdateButtonStatus(ApplicationUpdate.Instance.IsUpdateAvailable ? UpdateButtonStatus.ActionInstallAsk : UpdateButtonStatus.Ready); SetBadgeStatus(ApplicationUpdate.Instance.IsUpdateAvailable ? BadgeStatus.NewVersionAvailable : BadgeStatus.NewestInstalled); SetServerVersion(ApplicationUpdate.Instance.ServerVersion); diff --git a/CDPIUI/Views/BlockCheck2/MainPage.xaml b/CDPIUI/Views/BlockCheck2/MainPage.xaml index a97fd71b..2fb7b558 100644 --- a/CDPIUI/Views/BlockCheck2/MainPage.xaml +++ b/CDPIUI/Views/BlockCheck2/MainPage.xaml @@ -210,7 +210,7 @@ - - - - - - - - - - - @@ -273,7 +273,7 @@ -