From 3bfef4ba038ecb00d0bf3a119156ba3517823eb2 Mon Sep 17 00:00:00 2001 From: Dencher12 Date: Sat, 22 Nov 2025 13:17:25 +0300 Subject: [PATCH] WMI for managment, nssm for editing paramns of service --- MicrosocksGUI/MicrosocksGUI.csproj | 4 +- .../ViewModels/MainWindowViewModel.cs | 118 ++++++------------ MicrosocksGUI/Views/MainWindow.axaml | 5 +- MicrosocksGUI/app.manifest | 11 ++ 4 files changed, 55 insertions(+), 83 deletions(-) diff --git a/MicrosocksGUI/MicrosocksGUI.csproj b/MicrosocksGUI/MicrosocksGUI.csproj index 85c6258..d49259d 100644 --- a/MicrosocksGUI/MicrosocksGUI.csproj +++ b/MicrosocksGUI/MicrosocksGUI.csproj @@ -1,7 +1,7 @@  WinExe - net9.0 + net9.0-windows enable app.manifest true @@ -27,5 +27,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs index a9a493b..628c508 100644 --- a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs +++ b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs @@ -1,10 +1,13 @@ using System; using System.Diagnostics; using System.IO; +using System.Management; +using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; using ReactiveUI; using ReactiveUI.SourceGenerators; +using System.ServiceProcess; namespace MicrosocksGUI.ViewModels; @@ -16,105 +19,60 @@ public partial class MainWindowViewModel : ViewModelBase [Reactive] private string? _port; private Timer _timer; - - private ProcessStartInfo _getStatusProcessInfo = new() - { - FileName = ".\\nssm.exe", - Arguments = "status Microsocks", - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true, - WorkingDirectory = Path.GetDirectoryName(".") - }; - - private ProcessStartInfo _getStartTypeProcessInfo = new() - { - FileName = ".\\nssm.exe", - Arguments = "get Microsocks Start", - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true, - WorkingDirectory = Path.GetDirectoryName(".") - }; - - + private readonly ManagementObject _serviceManager = new("Win32_Service.Name='Microsocks'"); public MainWindowViewModel() { // TODO Костыль, таймер запускается через секунду, а должен после инициализации ReactiveUI - _timer = new Timer(GetWorkingStatusFromService, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100)); + _timer = new Timer(GetServiceStatus, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100)); + + GetMicroSocksArguments(); } [ReactiveCommand] - private async Task SetWorkingStatusToService() + private void SetServiceStatus() { - - var args = _isWorking ? "start Microsocks" : "stop Microsocks"; - - - ProcessStartInfo processInfo = new() + var serviceProps = _serviceManager.GetMethodParameters("Change"); + serviceProps["StartMode"] = IsAutoloading ? "Automatic" : "Manual"; + _serviceManager.InvokeMethod("Change", serviceProps, null); + _serviceManager.InvokeMethod(IsWorking ? "StartService" : "StopService", null, null); + } + + private void GetServiceStatus(object? state) + { + _serviceManager.Get(); + IsWorking = _serviceManager["State"]?.ToString() == "Running"; + IsAutoloading = _serviceManager["StartMode"]?.ToString() == "Auto"; + } + + private void GetMicroSocksArguments() + { + using var process = new Process(); + process.StartInfo = new ProcessStartInfo { FileName = ".\\nssm.exe", - Arguments = args, + Arguments = "get MicroSocks AppParameters", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, WorkingDirectory = Path.GetDirectoryName(".") }; - - using var process = new Process(); - process.StartInfo = processInfo; + process.Start(); - } - - private async void GetWorkingStatusFromService(object? state) - { - using (Process process = new Process()) + + var output = process.StandardOutput.ReadToEnd(); + var error = process.StandardError.ReadToEnd(); + + process.WaitForExit(); + + if (!string.IsNullOrEmpty(error)) { - process.StartInfo = _getStatusProcessInfo; - process.Start(); - - - var output = await process.StandardOutput.ReadToEndAsync(); - var error = await process.StandardError.ReadToEndAsync(); - - await process.WaitForExitAsync(); - - if (!string.IsNullOrEmpty(error)) - { - //Console.WriteLine(error); - } - - //Console.WriteLine(output.Replace("\0", "").Trim()); - - if (output.Replace("\0", "").Trim() == "SERVICE_RUNNING") - { - //Console.WriteLine("yes"); - //IsWorking = true; - } - - IsWorking = output.Replace("\0", "").Trim() == "SERVICE_RUNNING"; + Console.WriteLine(error); } - - using (Process process = new Process()) - { - process.StartInfo = _getStartTypeProcessInfo; - process.Start(); + + Console.WriteLine(output.Replace("\0", "").Trim()); - var output = await process.StandardOutput.ReadToEndAsync(); - var error = await process.StandardError.ReadToEndAsync(); - - await process.WaitForExitAsync(); - - if (!string.IsNullOrEmpty(error)) - { - Console.WriteLine(error); - } - - IsAutoloading = output.Replace("\0", "").Trim() == "SERVICE_AUTO_START"; - } + //IsAutoloading = output.Replace("\0", "").Trim() == "SERVICE_AUTO_START"; } } \ No newline at end of file diff --git a/MicrosocksGUI/Views/MainWindow.axaml b/MicrosocksGUI/Views/MainWindow.axaml index f38c762..c738ec6 100644 --- a/MicrosocksGUI/Views/MainWindow.axaml +++ b/MicrosocksGUI/Views/MainWindow.axaml @@ -29,7 +29,7 @@ HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" IsChecked="{Binding IsWorking}" - Command="{Binding SetWorkingStatusToServiceCommand}"> + Command="{Binding SetServiceStatusCommand}"> Работа @@ -37,7 +37,8 @@ Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" - IsChecked="{Binding IsAutoloading}"> + IsChecked="{Binding IsAutoloading}" + Command="{Binding SetServiceStatusCommand}"> Автозапуск diff --git a/MicrosocksGUI/app.manifest b/MicrosocksGUI/app.manifest index 36fadb2..0858869 100644 --- a/MicrosocksGUI/app.manifest +++ b/MicrosocksGUI/app.manifest @@ -15,4 +15,15 @@ + + + +