diff --git a/MicrosocksGUI/MicrosocksGUI.csproj b/MicrosocksGUI/MicrosocksGUI.csproj index 787d395..85c6258 100644 --- a/MicrosocksGUI/MicrosocksGUI.csproj +++ b/MicrosocksGUI/MicrosocksGUI.csproj @@ -23,5 +23,9 @@ All + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs index 519dd6e..a9a493b 100644 --- a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs +++ b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs @@ -1,6 +1,120 @@ -namespace MicrosocksGUI.ViewModels; +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ReactiveUI; +using ReactiveUI.SourceGenerators; -public class MainWindowViewModel : ViewModelBase +namespace MicrosocksGUI.ViewModels; + +public partial class MainWindowViewModel : ViewModelBase { - public string Greeting { get; } = "Welcome to Avalonia!"; + [Reactive] private bool _isWorking; + [Reactive] private bool _isAutoloading; + [Reactive] private string? _ip; + [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(".") + }; + + + + public MainWindowViewModel() + { + // TODO Костыль, таймер запускается через секунду, а должен после инициализации ReactiveUI + _timer = new Timer(GetWorkingStatusFromService, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100)); + } + + [ReactiveCommand] + private async Task SetWorkingStatusToService() + { + + var args = _isWorking ? "start Microsocks" : "stop Microsocks"; + + + ProcessStartInfo processInfo = new() + { + FileName = ".\\nssm.exe", + Arguments = args, + 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()) + { + 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"; + } + + using (Process process = new Process()) + { + process.StartInfo = _getStartTypeProcessInfo; + process.Start(); + + 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"; + } + } } \ No newline at end of file diff --git a/MicrosocksGUI/Views/MainWindow.axaml b/MicrosocksGUI/Views/MainWindow.axaml index ca4c8d6..f38c762 100644 --- a/MicrosocksGUI/Views/MainWindow.axaml +++ b/MicrosocksGUI/Views/MainWindow.axaml @@ -27,14 +27,17 @@ + HorizontalContentAlignment="Center" + IsChecked="{Binding IsWorking}" + Command="{Binding SetWorkingStatusToServiceCommand}"> Работа + HorizontalContentAlignment="Center" + IsChecked="{Binding IsAutoloading}"> Автозапуск @@ -43,12 +46,12 @@ - + - +