diff --git a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs
index 628c508..d9e8642 100644
--- a/MicrosocksGUI/ViewModels/MainWindowViewModel.cs
+++ b/MicrosocksGUI/ViewModels/MainWindowViewModel.cs
@@ -13,13 +13,16 @@ namespace MicrosocksGUI.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
+ private record Arguments(string Ip, string Port);
+
[Reactive] private bool _isWorking;
[Reactive] private bool _isAutoloading;
[Reactive] private string? _ip;
[Reactive] private string? _port;
private Timer _timer;
- private readonly ManagementObject _serviceManager = new("Win32_Service.Name='Microsocks'");
+ //private readonly ManagementObject _serviceManager = new("Win32_Service.Name='Microsocks'");
+ private readonly ServiceController _sc = new("MicroSocks");
public MainWindowViewModel()
{
@@ -32,19 +35,102 @@ public partial class MainWindowViewModel : ViewModelBase
[ReactiveCommand]
private void SetServiceStatus()
{
- var serviceProps = _serviceManager.GetMethodParameters("Change");
+ /*var serviceProps = _serviceManager.GetMethodParameters("Change");
serviceProps["StartMode"] = IsAutoloading ? "Automatic" : "Manual";
- _serviceManager.InvokeMethod("Change", serviceProps, null);
- _serviceManager.InvokeMethod(IsWorking ? "StartService" : "StopService", null, null);
+ _serviceManager.InvokeMethod("Change", serviceProps, null);*/
+
+ SetServiceAutoloading(IsAutoloading);
+
+
+ if (IsWorking)
+ {
+ StartService();
+ }
+ else
+ {
+ StopService();
+ }
+
+
+
+ //_serviceManager.InvokeMethod(IsWorking ? "StartService" : "StopService", null, null);
+ }
+
+ private void StartService()
+ {
+ _sc.Refresh();
+ if (_sc.Status != ServiceControllerStatus.Stopped) return;
+
+ _sc.Start();
+ _sc.WaitForStatus(ServiceControllerStatus.Running);
+ //_serviceManager.InvokeMethod("StartService", null, null);
+ }
+
+ private void StopService()
+ {
+ _sc.Refresh();
+ if (_sc.Status != ServiceControllerStatus.Running) return;
+
+ _sc.Stop();
+ _sc.WaitForStatus(ServiceControllerStatus.Stopped);
+ //_serviceManager.InvokeMethod("StopService", null, null);
+ }
+
+ private void RestartService()
+ {
+ StopService();
+ StartService();
+ }
+
+ private bool ServiceIsRunning()
+ {
+ _sc.Refresh();
+ return _sc.Status == ServiceControllerStatus.Running;
+ }
+
+ private bool ServiceIsAutoloading()
+ {
+ _sc.Refresh();
+ return _sc.StartType == ServiceStartMode.Automatic;
+ }
+
+ private void SetServiceAutoloading(bool isAutoloading)
+ {
+ using var process = new Process();
+
+ var param = isAutoloading ? "SERVICE_AUTO_START" : "SERVICE_DEMAND_START";
+ process.StartInfo = new ProcessStartInfo
+ {
+ FileName = ".\\nssm.exe",
+ Arguments = $"set MicroSocks Start {param}",
+ UseShellExecute = false,
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ CreateNoWindow = true,
+ WorkingDirectory = Path.GetDirectoryName(".")
+ };
+ process.Start();
+ process.WaitForExit();
}
private void GetServiceStatus(object? state)
{
- _serviceManager.Get();
- IsWorking = _serviceManager["State"]?.ToString() == "Running";
- IsAutoloading = _serviceManager["StartMode"]?.ToString() == "Auto";
+ //_sc.Refresh();
+ //_serviceManager.Get();
+ IsWorking = ServiceIsRunning();
+ IsAutoloading = ServiceIsAutoloading();
+
+ //IsWorking = _serviceManager["State"]?.ToString() == "Running";
+ //IsAutoloading = _serviceManager["StartMode"]?.ToString() == "Auto";
}
+ Arguments ParseArguments(string arguments)
+ {
+ var argumentsList = arguments.Replace("\0", "").Trim().Split();
+ return new Arguments(argumentsList[1], argumentsList[3]);
+ }
+
+ [ReactiveCommand]
private void GetMicroSocksArguments()
{
using var process = new Process();
@@ -60,19 +146,31 @@ public partial class MainWindowViewModel : ViewModelBase
};
process.Start();
-
var output = process.StandardOutput.ReadToEnd();
- var error = process.StandardError.ReadToEnd();
-
process.WaitForExit();
-
- if (!string.IsNullOrEmpty(error))
+
+ var arguments = ParseArguments(output);
+ Ip = arguments.Ip;
+ Port = arguments.Port;
+ }
+
+ [ReactiveCommand]
+ private void SetMicroSocksArguments()
+ {
+ using var process = new Process();
+ process.StartInfo = new ProcessStartInfo
{
- Console.WriteLine(error);
- }
-
- Console.WriteLine(output.Replace("\0", "").Trim());
-
- //IsAutoloading = output.Replace("\0", "").Trim() == "SERVICE_AUTO_START";
+ FileName = ".\\nssm.exe",
+ Arguments = $"set MicroSocks AppParameters -i {_ip} -p {_port}",
+ UseShellExecute = false,
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ CreateNoWindow = true,
+ WorkingDirectory = Path.GetDirectoryName(".")
+ };
+ process.Start();
+ process.WaitForExit();
+
+ RestartService();
}
}
\ No newline at end of file
diff --git a/MicrosocksGUI/Views/MainWindow.axaml b/MicrosocksGUI/Views/MainWindow.axaml
index c738ec6..1deef4a 100644
--- a/MicrosocksGUI/Views/MainWindow.axaml
+++ b/MicrosocksGUI/Views/MainWindow.axaml
@@ -59,14 +59,16 @@