diff --git a/TimedShutdown/ShutdownManager.cs b/TimedShutdown/ShutdownManager.cs index 6479900..3ef05e6 100644 --- a/TimedShutdown/ShutdownManager.cs +++ b/TimedShutdown/ShutdownManager.cs @@ -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}"; diff --git a/TimedShutdown/ViewModels/ShellRootViewModel.cs b/TimedShutdown/ViewModels/ShellRootViewModel.cs index 7ffc821..d1777f2 100644 --- a/TimedShutdown/ViewModels/ShellRootViewModel.cs +++ b/TimedShutdown/ViewModels/ShellRootViewModel.cs @@ -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,13 +129,15 @@ namespace TimedShutdown.ViewModels ShutdownManager.Operation.Shutdown, ShutdownTime, ShutdownOptionsForce, - RemoteShutdown); + IPRemoteShutdown); - CanReboot = false; - CanShutdown = false; + ProcessRemoteShutdownAlert(); - NotifyOfPropertyChange(() => CanReboot); - NotifyOfPropertyChange(() => CanShutdown); + CanReboot = false; + CanShutdown = false; + + NotifyOfPropertyChange(() => CanReboot); + NotifyOfPropertyChange(() => CanShutdown); } public bool CanReboot { get; set; } = true; @@ -153,13 +147,15 @@ namespace TimedShutdown.ViewModels ShutdownManager.Operation.Reboot, ShutdownTime, ShutdownOptionsForce, - RemoteShutdown); + IPRemoteShutdown); - CanReboot = false; - CanShutdown = false; + ProcessRemoteShutdownAlert(); - NotifyOfPropertyChange(() => CanReboot); - NotifyOfPropertyChange(() => CanShutdown); + CanReboot = false; + CanShutdown = false; + + NotifyOfPropertyChange(() => CanReboot); + NotifyOfPropertyChange(() => CanShutdown); } public void AbortShutdown() @@ -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 diff --git a/TimedShutdown/Views/ShellRootView.xaml b/TimedShutdown/Views/ShellRootView.xaml index c3765a5..af2d57b 100644 --- a/TimedShutdown/Views/ShellRootView.xaml +++ b/TimedShutdown/Views/ShellRootView.xaml @@ -98,20 +98,22 @@