annotate ServerMonitor/Objects/ServerMonitor.cs @ 6:c1dffaac66fa

- Don't show multiple password dialogs for the same key if the first one was cancelled. - Add option to trim log files.
author Brad Greco <brad@bgreco.net>
date Fri, 01 Mar 2019 21:38:22 -0500
parents b6fe203af9d5
children 052aa62cb42a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
1 using Renci.SshNet;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
2 using Renci.SshNet.Common;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
3 using ServerMonitorApp.Properties;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
4 using System;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Diagnostics;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.IO;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
9 using System.Net.NetworkInformation;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 using System.Threading;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 using System.Threading.Tasks;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
13 using System.Windows.Forms;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 using System.Xml.Serialization;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 public class ServerMonitor
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 private readonly string configFileDir;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 private readonly Logger logger;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 private readonly Dictionary<int, CancellationTokenSource> tokens = new Dictionary<int, CancellationTokenSource>();
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
23 private readonly Dictionary<string, PrivateKeyFile> privateKeys = new Dictionary<string, PrivateKeyFile>();
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
24 private readonly List<int> pausedChecks = new List<int>();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
25 private bool running, networkAvailable;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 private Dictionary<Task<CheckResult>, int> tasks;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
27 private ServerSummaryForm mainForm;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
28
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 //private List<Task<CheckResult>> tasks;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 public event EventHandler<CheckStatusChangedEventArgs> CheckStatusChanged;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 public List<Server> Servers { get; private set; } = new List<Server>();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
35 public IEnumerable<Check> Checks => Servers.SelectMany(s => s.Checks);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 public string ConfigFile { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
39 public IEnumerable<string> LockedKeys { get { return privateKeys.Where(kvp => kvp.Value == null).Select(kvp => kvp.Key); } }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
40
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
41 public ServerMonitor(ServerSummaryForm mainForm)
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
43 this.mainForm = mainForm;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ServerMonitor");
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 ConfigFile = Path.Combine(configFileDir, "servers.xml");
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 logger = new Logger(Path.Combine(configFileDir, "monitor.log"));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 public void AddServer(Server server)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 Servers.Add(server);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
52 SaveServers();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
53 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
54
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
55 public void DeleteServer(Server server)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
56 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
57 Servers.Remove(server);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
58 SaveServers();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 public void LoadServers()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 TextReader reader = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 reader = new StreamReader(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 XmlSerializer serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 Servers.Clear();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 Servers.AddRange((List<Server>)serializer.Deserialize(reader));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 // Update the Checks so they know what Server they belong to.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 // Would rather do this in the Server object on deserialization, but
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 // that doesn't work when using the XML serializer for some reason.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 foreach (Server server in Servers)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 {
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
75 if (server.LoginType == LoginType.PrivateKey && server.PrivateKeyFile == null)
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
76 OpenPrivateKey(server.KeyFile);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 foreach (Check check in server.Checks)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 check.Server = server;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
80 if (check.Status == CheckStatus.Running)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
81 check.Status = check.LastRunStatus;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 server.CheckModified += Server_CheckModified;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
84 server.EnabledChanged += Server_EnabledChanged;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 // If the file doesn't exist, no special handling is needed. It will be created later.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 catch (FileNotFoundException) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 catch (DirectoryNotFoundException) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 catch (InvalidOperationException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 //TODO log
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 throw;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95 finally
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 reader?.Close();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
99 NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
100 logger.TrimLog();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 Run();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 public void SaveServers()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 GenerateIds();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 TextWriter writer = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 XmlSerializer serializer = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 writer = new StreamWriter(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 serializer.Serialize(writer, Servers);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
115 catch (DirectoryNotFoundException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
116 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
117 Directory.CreateDirectory(configFileDir);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
118 writer = new StreamWriter(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120 serializer.Serialize(writer, Servers);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
121 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
122 catch (Exception)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
123 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
124 //TODO log
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
125 throw;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
126 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
127 finally
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
128 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
129 writer?.Close();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
130 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
131 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
132
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
133 private async void Run()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
134 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
135 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
136 return;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
137 running = true;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
138 networkAvailable = Helpers.IsNetworkAvailable();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
139 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
140 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
141 foreach (int id in pausedChecks)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
142 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
143 await ExecuteCheckAsync(Checks.FirstOrDefault(c => c.Id == id));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
144 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
145 pausedChecks.Clear();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
146 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
147 //TODO subscribe to power events. Find any check's NextExecutionTime is in the past. Cancel waiting task and run immediately (or after short delay).
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
148 //tasks = Checks.Select(c => ScheduleExecuteCheckAsync(c)).ToList();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
149 tasks = Checks.ToDictionary(c => ScheduleExecuteCheckAsync(c), c => c.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
150 while (tasks.Count > 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
151 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
152 Task<CheckResult> task = await Task.WhenAny(tasks.Keys);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
153 tasks.Remove(task);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
154 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
155 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
156 CheckResult result = await task;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
157 // Result will be null if a scheduled check was disabled
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
158 if (result != null && result.CheckStatus != CheckStatus.Disabled)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
159 tasks.Add(ScheduleExecuteCheckAsync(result.Check), result.Check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
160 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
161 catch (OperationCanceledException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
162 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
163
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
164 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
165 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
166 running = false;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
167 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
168
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
169 public async Task<CheckResult> ExecuteCheckAsync(Check check, CancellationToken token = default(CancellationToken))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
170 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
171 check.Status = CheckStatus.Running;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
172 OnCheckStatusChanged(check);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
173 CheckResult result = await check.ExecuteAsync(token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
174 OnCheckStatusChanged(check, result);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
175 HandleResultAsync(result);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
176 return result;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
177 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
178
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
179 private void HandleResultAsync(CheckResult result)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
180 {
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
181 logger.Log(result);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
182 if (result.FailAction == FailAction.FlashTaskbar)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
183 mainForm.AlertServerForm(result.Check);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
184 if (result.FailAction.In(FailAction.FlashTaskbar, FailAction.NotificationBalloon))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
185 mainForm.ShowBalloon(result);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
186 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
187
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
188 public IList<CheckResult> GetLog(Server server)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
189 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
190 return logger.Read(server);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
191 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
192
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
193 private void OnCheckStatusChanged(Check check, CheckResult result = null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
194 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
195 SaveServers();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
196 CheckStatusChanged?.Invoke(check, new CheckStatusChangedEventArgs(check, result));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
197 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
198
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
199 private void Server_CheckModified(object sender, EventArgs e)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
200 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
201 Check check = (Check)sender;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
202 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
203 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
204 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
205 if (task == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
206 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
207 // No tasks associated with the check, so schedule a new one
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
208 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
209 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
210 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
211 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
212 // Check was modified or deleted, so remove any waiting tasks
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
213 CancelCheck(check);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
214 if (check.Server != null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
215 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
216 // If the check was not deleted, schedule the new check.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
217 // But only if it's still running, otherwise restarting the monitor below
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
218 // will create a duplicate run.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
219 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
220 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
221 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
222 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
223 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
224 // Run again in case removing a task above caused it to stop
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
225 Run();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
226 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
227
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
228 private void Server_EnabledChanged(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
229 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
230 Server server = (Server)sender;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
231 if (server.Enabled)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
232 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
233 Run();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
234 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
235 else
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
236 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
237 foreach (Check check in server.Checks)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
238 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
239 CancelCheck(check);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
240 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
241 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
242 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
243
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
244 private void CancelCheck(Check check)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
245 {
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
246 if (tasks == null)
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
247 return;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
248 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key;
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
249 if (task != null)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
250 tasks.Remove(task);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
251 pausedChecks.RemoveAll(id => id == check.Id);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
252 if (tokens.TryGetValue(check.Id, out CancellationTokenSource cts))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
253 cts.Cancel();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
254 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
255
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
256 private async Task<CheckResult> ScheduleExecuteCheckAsync(Check check)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
257 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
258 if (!check.Enabled || !check.Server.Enabled)
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
259 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
260
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
261 CancellationTokenSource cts = new CancellationTokenSource();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
262 tokens[check.Id] = cts;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
263 check.NextRunTime = check.Schedule.GetNextTime(check.LastScheduledRunTime);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
264 await Task.Delay(check.NextRunTime - DateTime.Now, cts.Token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
265 check.LastScheduledRunTime = check.NextRunTime;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
266 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
267 return await ExecuteCheckAsync(check, cts.Token);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
268 else
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
269 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
270 if (!pausedChecks.Contains(check.Id))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
271 pausedChecks.Add(check.Id);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
272 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
273 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
274 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
275
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
276 private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
277 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
278 networkAvailable = Helpers.IsNetworkAvailable();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
279 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
280 mainForm.Invoke((MethodInvoker)(() => Run()));
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
281 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
282
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
283 public KeyStatus OpenPrivateKey(string path, string password = null)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
284 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
285 KeyStatus keyStatus;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
286 if (path == null)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
287 return KeyStatus.NotAccessible;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
288 if (privateKeys.TryGetValue(path, out PrivateKeyFile key) && key != null)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
289 return KeyStatus.Open;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
290 try
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
291 {
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 5
diff changeset
292 key = new PrivateKeyFile(path, password);
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
293 keyStatus = KeyStatus.Open;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
294 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
295 catch (Exception e) when (e is SshPassPhraseNullOrEmptyException || e is InvalidOperationException)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
296 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
297 keyStatus = KeyStatus.NeedPassword;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
298 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
299 catch (Exception)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
300 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
301 keyStatus = KeyStatus.NotAccessible;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
302 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
303 foreach (Server server in Servers)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
304 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
305 if (server.KeyFile == path)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
306 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
307 server.PrivateKeyFile = key;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
308 server.KeyStatus = keyStatus;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
309 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
310 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
311 privateKeys[path] = key;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
312 return keyStatus;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
313 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
314
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
315 private void GenerateIds()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
316 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
317 if (Servers.Any())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
318 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
319 int id = Servers.Max(s => s.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
320 foreach (Server server in Servers)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
321 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
322 if (server.Id == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
323 server.Id = ++id;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
324 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
325 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
326
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
327 if (Checks.Any())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
328 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
329 int id = Math.Max(Settings.Default.MaxCheckId, Checks.Max(c => c.Id));
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
330 foreach (Check check in Checks)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
331 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
332 if (check.Id == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
333 check.Id = ++id;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
334 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
335 Settings.Default.MaxCheckId = id;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
336 Settings.Default.Save();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
337 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
338 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
339
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
340 private XmlSerializer CreateXmlSerializer()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
341 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
342 return new XmlSerializer(typeof(List<Server>), Check.CheckTypes);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
343 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
344 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
345
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
346 public class CheckStatusChangedEventArgs : EventArgs
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
347 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
348 public Check Check { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
349
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
350 public CheckResult CheckResult { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
351
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
352 public CheckStatusChangedEventArgs(Check check, CheckResult result)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
353 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
354 Check = check;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
355 CheckResult = result;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
356 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
357 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
358
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
359 public enum FailAction { FlashTaskbar = 0, NotificationBalloon = 1, None = 10 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
360 }