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;
using System.Diagnostics; using System.Diagnostics;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
namespace TimedShutdown namespace TimedShutdown
@@ -76,6 +77,9 @@ namespace TimedShutdown
private static string RemoteShutdown(string arg) private static string RemoteShutdown(string arg)
{ {
if (string.IsNullOrEmpty(arg)) return string.Empty; if (string.IsNullOrEmpty(arg)) return string.Empty;
if (IPAddress.TryParse(arg, out IPAddress address) == false) { throw new FormatException(); }
if (arg.StartsWith(@"\\")) if (arg.StartsWith(@"\\"))
{ {
return $"-m {arg}"; return $"-m {arg}";
+85 -27
View File
@@ -63,22 +63,14 @@ namespace TimedShutdown.ViewModels
public bool ShutdownOptionsForce { get; set; } public bool ShutdownOptionsForce { get; set; }
private string _remoteShutdown; private string _IPRemoteShutdown;
public string RemoteShutdown public string IPRemoteShutdown
{ {
get { return _remoteShutdown; } get { return _IPRemoteShutdown; }
set set
{ {
// Show remote shutdown alert upon checking for a non-loopback if (IPAddress.TryParse(value, out IPAddress address) == false) { value = string.Empty; } // Temporary validation.
// address. _IPRemoteShutdown = value;
if (!string.IsNullOrEmpty(value)
&& value != "localhost"
&& value != "127.0.0.1")
{
Messaging alert = new Messaging();
alert.Show(Messaging.Message.RemoteShutdown, value);
}
_remoteShutdown = value;
} }
} }
@@ -93,7 +85,7 @@ namespace TimedShutdown.ViewModels
if (value == true) if (value == true)
{ {
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown)); pingWidget.InitatePingRequest(IPAddress.Parse(IPRemoteShutdown));
} }
else else
{ {
@@ -128,7 +120,7 @@ namespace TimedShutdown.ViewModels
#endregion #endregion
#region Buttons #region Controls
public bool CanShutdown { get; set; } = true; public bool CanShutdown { get; set; } = true;
public void Shutdown() public void Shutdown()
@@ -137,13 +129,15 @@ namespace TimedShutdown.ViewModels
ShutdownManager.Operation.Shutdown, ShutdownManager.Operation.Shutdown,
ShutdownTime, ShutdownTime,
ShutdownOptionsForce, ShutdownOptionsForce,
RemoteShutdown); IPRemoteShutdown);
CanReboot = false; ProcessRemoteShutdownAlert();
CanShutdown = false;
NotifyOfPropertyChange(() => CanReboot); CanReboot = false;
NotifyOfPropertyChange(() => CanShutdown); CanShutdown = false;
NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown);
} }
public bool CanReboot { get; set; } = true; public bool CanReboot { get; set; } = true;
@@ -153,13 +147,15 @@ namespace TimedShutdown.ViewModels
ShutdownManager.Operation.Reboot, ShutdownManager.Operation.Reboot,
ShutdownTime, ShutdownTime,
ShutdownOptionsForce, ShutdownOptionsForce,
RemoteShutdown); IPRemoteShutdown);
CanReboot = false; ProcessRemoteShutdownAlert();
CanShutdown = false;
NotifyOfPropertyChange(() => CanReboot); CanReboot = false;
NotifyOfPropertyChange(() => CanShutdown); CanShutdown = false;
NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown);
} }
public void AbortShutdown() public void AbortShutdown()
@@ -168,13 +164,75 @@ namespace TimedShutdown.ViewModels
Minutes = 0; Minutes = 0;
Hours = 0; Hours = 0;
_shutdownManager.AbortOperation(RemoteShutdown); _shutdownManager.AbortOperation(IPRemoteShutdown);
CanReboot = true; CanReboot = true;
CanShutdown = true; CanShutdown = true;
NotifyOfPropertyChange(() => CanReboot); NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown); 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 #endregion
+5 -3
View File
@@ -98,20 +98,22 @@
<!-- Row 6: Shutdown Type --> <!-- Row 6: Shutdown Type -->
<Label Content="Shutdown Type:" Grid.Row="0" Grid.Column="0" /> <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"/> 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"/> Margin="10,5,0,0"/>
<!-- Row 7: Hostname/IP --> <!-- Row 7: Hostname/IP -->
<Label Content="Hostname/IP Address:" Grid.Row="1" Grid.Column="0" Margin="0,5,0,0"/> <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" 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."/> 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 --> <!-- Row 8: Ping remote host -->
<Label Content="Options:" Grid.Row="2" Grid.Column="0"/> <Label Content="Options:" Grid.Row="2" Grid.Column="0"/>
<CheckBox IsChecked="{Binding PingRemoteHost, Mode=OneWayToSource}" Content="Ping remote host" <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"/> Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="10,5,0,0"/>
<!-- Row 9: Ping status --> <!-- Row 9: Ping status -->
Binary file not shown.
Binary file not shown.