shitty commit 2

This commit is contained in:
2025-11-23 00:20:02 +03:00
parent 3bfef4ba03
commit d23a5c74ce
2 changed files with 120 additions and 20 deletions

View File

@ -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();
}
}

View File

@ -59,14 +59,16 @@
<Button
Grid.Column="0"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center">
HorizontalContentAlignment="Center"
Command="{Binding SetMicroSocksArgumentsCommand}">
Применить
</Button>
<Button
Grid.Column="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center">
HorizontalContentAlignment="Center"
Command="{Binding GetMicroSocksArgumentsCommand}">
Сбросить
</Button>
</Grid>