shitty commit 2
This commit is contained in:
@ -13,13 +13,16 @@ namespace MicrosocksGUI.ViewModels;
|
|||||||
|
|
||||||
public partial class MainWindowViewModel : ViewModelBase
|
public partial class MainWindowViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
|
private record Arguments(string Ip, string Port);
|
||||||
|
|
||||||
[Reactive] private bool _isWorking;
|
[Reactive] private bool _isWorking;
|
||||||
[Reactive] private bool _isAutoloading;
|
[Reactive] private bool _isAutoloading;
|
||||||
[Reactive] private string? _ip;
|
[Reactive] private string? _ip;
|
||||||
[Reactive] private string? _port;
|
[Reactive] private string? _port;
|
||||||
|
|
||||||
private Timer _timer;
|
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()
|
public MainWindowViewModel()
|
||||||
{
|
{
|
||||||
@ -32,19 +35,102 @@ public partial class MainWindowViewModel : ViewModelBase
|
|||||||
[ReactiveCommand]
|
[ReactiveCommand]
|
||||||
private void SetServiceStatus()
|
private void SetServiceStatus()
|
||||||
{
|
{
|
||||||
var serviceProps = _serviceManager.GetMethodParameters("Change");
|
/*var serviceProps = _serviceManager.GetMethodParameters("Change");
|
||||||
serviceProps["StartMode"] = IsAutoloading ? "Automatic" : "Manual";
|
serviceProps["StartMode"] = IsAutoloading ? "Automatic" : "Manual";
|
||||||
_serviceManager.InvokeMethod("Change", serviceProps, null);
|
_serviceManager.InvokeMethod("Change", serviceProps, null);*/
|
||||||
_serviceManager.InvokeMethod(IsWorking ? "StartService" : "StopService", null, 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)
|
private void GetServiceStatus(object? state)
|
||||||
{
|
{
|
||||||
_serviceManager.Get();
|
//_sc.Refresh();
|
||||||
IsWorking = _serviceManager["State"]?.ToString() == "Running";
|
//_serviceManager.Get();
|
||||||
IsAutoloading = _serviceManager["StartMode"]?.ToString() == "Auto";
|
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()
|
private void GetMicroSocksArguments()
|
||||||
{
|
{
|
||||||
using var process = new Process();
|
using var process = new Process();
|
||||||
@ -60,19 +146,31 @@ public partial class MainWindowViewModel : ViewModelBase
|
|||||||
};
|
};
|
||||||
|
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|
||||||
var output = process.StandardOutput.ReadToEnd();
|
var output = process.StandardOutput.ReadToEnd();
|
||||||
var error = process.StandardError.ReadToEnd();
|
|
||||||
|
|
||||||
process.WaitForExit();
|
process.WaitForExit();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(error))
|
var arguments = ParseArguments(output);
|
||||||
{
|
Ip = arguments.Ip;
|
||||||
Console.WriteLine(error);
|
Port = arguments.Port;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine(output.Replace("\0", "").Trim());
|
[ReactiveCommand]
|
||||||
|
private void SetMicroSocksArguments()
|
||||||
|
{
|
||||||
|
using var process = new Process();
|
||||||
|
process.StartInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
|
||||||
//IsAutoloading = output.Replace("\0", "").Trim() == "SERVICE_AUTO_START";
|
RestartService();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,14 +59,16 @@
|
|||||||
<Button
|
<Button
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
HorizontalAlignment="Stretch"
|
HorizontalAlignment="Stretch"
|
||||||
HorizontalContentAlignment="Center">
|
HorizontalContentAlignment="Center"
|
||||||
|
Command="{Binding SetMicroSocksArgumentsCommand}">
|
||||||
Применить
|
Применить
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
HorizontalAlignment="Stretch"
|
HorizontalAlignment="Stretch"
|
||||||
HorizontalContentAlignment="Center">
|
HorizontalContentAlignment="Center"
|
||||||
|
Command="{Binding GetMicroSocksArgumentsCommand}">
|
||||||
Сбросить
|
Сбросить
|
||||||
</Button>
|
</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
Reference in New Issue
Block a user