Added validation for remote and local shutdowns

This commit is contained in:
2019-04-19 12:26:07 +01:00
parent 2cae359adf
commit fe66efba31
5 changed files with 94 additions and 30 deletions
+4
View File
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
namespace TimedShutdown
@@ -76,6 +77,9 @@ namespace TimedShutdown
private static string RemoteShutdown(string arg)
{
if (string.IsNullOrEmpty(arg)) return string.Empty;
if (IPAddress.TryParse(arg, out IPAddress address) == false) { throw new FormatException(); }
if (arg.StartsWith(@"\\"))
{
return $"-m {arg}";
+77 -19
View File
@@ -63,22 +63,14 @@ namespace TimedShutdown.ViewModels
public bool ShutdownOptionsForce { get; set; }
private string _remoteShutdown;
public string RemoteShutdown
private string _IPRemoteShutdown;
public string IPRemoteShutdown
{
get { return _remoteShutdown; }
get { return _IPRemoteShutdown; }
set
{
// Show remote shutdown alert upon checking for a non-loopback
// address.
if (!string.IsNullOrEmpty(value)
&& value != "localhost"
&& value != "127.0.0.1")
{
Messaging alert = new Messaging();
alert.Show(Messaging.Message.RemoteShutdown, value);
}
_remoteShutdown = value;
if (IPAddress.TryParse(value, out IPAddress address) == false) { value = string.Empty; } // Temporary validation.
_IPRemoteShutdown = value;
}
}
@@ -93,7 +85,7 @@ namespace TimedShutdown.ViewModels
if (value == true)
{
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
pingWidget.InitatePingRequest(IPAddress.Parse(IPRemoteShutdown));
}
else
{
@@ -128,7 +120,7 @@ namespace TimedShutdown.ViewModels
#endregion
#region Buttons
#region Controls
public bool CanShutdown { get; set; } = true;
public void Shutdown()
@@ -137,7 +129,9 @@ namespace TimedShutdown.ViewModels
ShutdownManager.Operation.Shutdown,
ShutdownTime,
ShutdownOptionsForce,
RemoteShutdown);
IPRemoteShutdown);
ProcessRemoteShutdownAlert();
CanReboot = false;
CanShutdown = false;
@@ -153,7 +147,9 @@ namespace TimedShutdown.ViewModels
ShutdownManager.Operation.Reboot,
ShutdownTime,
ShutdownOptionsForce,
RemoteShutdown);
IPRemoteShutdown);
ProcessRemoteShutdownAlert();
CanReboot = false;
CanShutdown = false;
@@ -168,13 +164,75 @@ namespace TimedShutdown.ViewModels
Minutes = 0;
Hours = 0;
_shutdownManager.AbortOperation(RemoteShutdown);
_shutdownManager.AbortOperation(IPRemoteShutdown);
CanReboot = true;
CanShutdown = true;
NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown);
NotifyOfPropertyChange(() => RemoteShutdown);
NotifyOfPropertyChange(() => IPRemoteShutdown);
}
private bool _contextRemoteShutdown;
public bool ContextRemoteShutdown
{
get { return _contextRemoteShutdown; }
set
{
_contextRemoteShutdown = value;
if (value == true)
{
IPRemoteShutdownEnabled = true;
PingRemoteHostEnabled = true;
NotifyOfPropertyChange(() => IPRemoteShutdownEnabled);
NotifyOfPropertyChange(() => PingRemoteHostEnabled);
}
}
}
private bool _contextLocalShutdown = true;
public bool ContextLocalShutdown
{
get { return _contextLocalShutdown; }
set
{
_contextLocalShutdown = value;
if (value == true)
{
IPRemoteShutdownEnabled = false;
PingRemoteHostEnabled = false;
IPRemoteShutdown = string.Empty;
NotifyOfPropertyChange(() => IPRemoteShutdown);
NotifyOfPropertyChange(() => IPRemoteShutdownEnabled);
NotifyOfPropertyChange(() => PingRemoteHostEnabled);
}
}
}
public bool IPRemoteShutdownEnabled { get; set; }
public bool PingRemoteHostEnabled { get; set; }
#endregion
#region Alerts
private void ProcessRemoteShutdownAlert()
{
// Show remote shutdown alert upon checking for a non-loopback
// address.
if (!string.IsNullOrEmpty(IPRemoteShutdown)
&& IPRemoteShutdown != "localhost"
&& IPRemoteShutdown != "127.0.0.1")
{
Messaging alert = new Messaging();
alert.Show(Messaging.Message.RemoteShutdown, IPRemoteShutdown);
}
}
#endregion
+5 -3
View File
@@ -98,20 +98,22 @@
<!-- Row 6: Shutdown Type -->
<Label Content="Shutdown Type:" Grid.Row="0" Grid.Column="0" />
<RadioButton Content="Local" Grid.Row="0" Grid.Column="1"
<RadioButton IsChecked="{Binding ContextLocalShutdown}" Content="Local" Grid.Row="0" Grid.Column="1"
Margin="10,5,0,0"/>
<RadioButton Content="Remote" Grid.Row="0" Grid.Column="2"
<RadioButton IsChecked="{Binding ContextRemoteShutdown}" Content="Remote" Grid.Row="0" Grid.Column="2"
Margin="10,5,0,0"/>
<!-- Row 7: Hostname/IP -->
<Label Content="Hostname/IP Address:" Grid.Row="1" Grid.Column="0" Margin="0,5,0,0"/>
<TextBox Text="{Binding RemoteShutdown, Mode=TwoWay}" Grid.Row="1" Grid.Column="1"
<TextBox Text="{Binding IPRemoteShutdown, Mode=TwoWay}" Grid.Row="1" Grid.Column="1"
IsEnabled="{Binding IPRemoteShutdownEnabled, Mode=TwoWay}"
Padding="5" Margin="10,5,5,0" Grid.ColumnSpan="2"
ToolTip="Enter the machine name or IP address to remotely shutdown. NOTE: This will only work on machines part of the same Windows Domain."/>
<!-- Row 8: Ping remote host -->
<Label Content="Options:" Grid.Row="2" Grid.Column="0"/>
<CheckBox IsChecked="{Binding PingRemoteHost, Mode=OneWayToSource}" Content="Ping remote host"
IsEnabled="{Binding PingRemoteHostEnabled, Mode=TwoWay}"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="10,5,0,0"/>
<!-- Row 9: Ping status -->
Binary file not shown.
Binary file not shown.