annotate ServerMonitor/Objects/Server.cs @ 40:c4fc74593a78 default tip

Mono fix
author Brad Greco <brad@bgreco.net>
date Sat, 13 Jun 2020 13:28:20 -0400
parents 7645122aa7a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
1 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System.Linq;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Security.Cryptography;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using Renci.SshNet;
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
7 using System.Xml.Serialization;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
11 /// <summary>Types of SSH logins supported by the server monitor.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 public enum LoginType { PrivateKey = 0, Password = 1 };
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
14 /// <summary>Remote server that checks can be run against.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public class Server
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 private string _host;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 private int _port;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 private string _username;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 private LoginType _loginType;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 private string _keyFile;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 private SshClient _sshClient;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
23 private bool _enabled = true;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 private byte[] passwordHash;
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
25 private PrivateKeyFile _privateKeyFile;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
27 /// <summary>Fires when a check belonging to this server is modifed.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 public event EventHandler CheckModified;
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
29 /// <summary>Fires when the server enabled state changes.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
30 public event EventHandler EnabledChanged;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
32 /// <summary>The checks that belong to this server.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 public readonly BindingList<Check> Checks = new BindingList<Check>();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
35 /// <summary>Internal ID of the server.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 public int Id { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
38 /// <summary>Name of the server.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 public string Name { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
41 /// <summary>Hostname of the server.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 public string Host
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 get { return _host; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 set { _host = value; InvalidateSshConnection(); }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
48 /// <summary>Port to use when connecting using SSH.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 public int Port
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 get { return _port; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 set { _port = value; InvalidateSshConnection(); }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
55 /// <summary>Username to use when connecting using SSH.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 public string Username
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 get { return _username; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 set { _username = value; InvalidateSshConnection(); }
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
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
62 /// <summary>Login type to use when connecting using SSH.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 public LoginType LoginType
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 get { return _loginType; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 set { _loginType = value; InvalidateSshConnection(); }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
69 /// <summary>Path to the private key file to use when connecting using SSH.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 public string KeyFile
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 get { return _keyFile; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 set { _keyFile = value; InvalidateSshConnection(); }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
76 /// <summary>Password to use when connecting using SSH.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
77 /// <remarks>The password is encrypted using the current Windows user account.</remarks>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 public string Password
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 get {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 return passwordHash == null ? null :
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
82 Encoding.UTF8.GetString(ProtectedData.Unprotect(passwordHash
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
83 , Encoding.UTF8.GetBytes("Server".Reverse().ToString()) // Super-secure obfuscation of additional entropy
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
84 , DataProtectionScope.CurrentUser));
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 set
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 passwordHash = ProtectedData.Protect(Encoding.UTF8.GetBytes(value),
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
89 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Super-secure obfuscation of additional entropy
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
90 DataProtectionScope.CurrentUser);
0
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 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
94 /// <summary>Private key file to use when connecting using SSH.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
95 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
96 /// If the private key file is encrypted, will be null until the user enters
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
97 /// the decryption key.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
98 /// </remarks>
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
99 [XmlIgnore]
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
100 public PrivateKeyFile PrivateKeyFile
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
101 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
102 get { return _privateKeyFile; }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
103 set
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
104 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
105 _privateKeyFile = value;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
106 if (LoginType == LoginType.PrivateKey)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
107 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
108 if (_privateKeyFile == null)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
109 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
110 // The private key has not been opened yet.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
111 // Disable the server until the user enters the decryption key,
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
112 // and set the KeyStatus to indicate why the server was disabled.
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
113 KeyStatus = KeyStatus.Closed;
26
b5502ce8cb1f Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
Brad Greco <brad@bgreco.net>
parents: 24
diff changeset
114 if (HasSshChecks)
b5502ce8cb1f Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
Brad Greco <brad@bgreco.net>
parents: 24
diff changeset
115 Enabled = false;
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
116 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
117 else
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
118 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
119 // The private key is open and accessible.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
120 // Automatically re-enable the server if it was previously disabled
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
121 // due to a locked or inaccessible private key (i.e. disabled
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
122 // programatically and not by user request).
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
123 if (!KeyStatus.In(KeyStatus.Open, KeyStatus.Closed))
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
124 Enabled = true;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
125 KeyStatus = KeyStatus.Open;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
126 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
127 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
128 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
129 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
130
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
131 /// <summary>The current status of the private key file.</summary>
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
132 public KeyStatus KeyStatus { get; set; }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
133
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
134 /// <summary>Whether this server's checks will be automatically executed on their schedules.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
135 public bool Enabled
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
136 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
137 get { return _enabled; }
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
138 set
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
139 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
140 // Do not allow enabling the server if the private key is not accessible.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
141 // Do not fire the EnabledChanged event if the Enabled state is not actually changing
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
142 // from its existing value.
26
b5502ce8cb1f Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
Brad Greco <brad@bgreco.net>
parents: 24
diff changeset
143 if ((LoginType == LoginType.PrivateKey && PrivateKeyFile == null && value == true && HasSshChecks) || value == _enabled)
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
144 return;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
145 _enabled = value;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
146 EnabledChanged?.Invoke(this, new EventArgs());
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
147 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
148 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
149
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
150 /// <summary>The status of the server.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
151 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
152 /// The status of the server is the most severe status of all its enabled checks.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
153 /// The integer value of the CheckStatus enum increases with the severity,
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
154 /// so the maximum value of all checks gives the most severe status.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
155 /// </remarks>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
156 public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
157 .Where(c => c.Enabled)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
158 .Select(c => c.LastRunStatus)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
159 .DefaultIfEmpty(CheckStatus.Success)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
160 .Max();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
161
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
162 /// <summary>The SSH client to use when running checks on the server.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
163 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
164 /// The connection is stored and kept open at the server level so it can be reused
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
165 /// by all SSH checks.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
166 /// </remarks>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
167 public SshClient SshClient
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 get
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 if (_sshClient == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
172 {
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
173 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, GetAuthentication());
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
174 _sshClient = new SshClient(info);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
175 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
176 return _sshClient;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
177 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
178 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
179
26
b5502ce8cb1f Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
Brad Greco <brad@bgreco.net>
parents: 24
diff changeset
180 public bool HasSshChecks => Checks.Any(c => c is SshCheck);
b5502ce8cb1f Fix servers getting disabled at program launch if no SSH key is set, even if they have no SSH checks.
Brad Greco <brad@bgreco.net>
parents: 24
diff changeset
181
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
182 /// <summary>Deletes a check from the server.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
183 /// <param name="check">The check to delete.</param>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
184 public void DeleteCheck(Check check)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
185 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
186 Checks.Remove(check);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
187 check.Server = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
188 CheckModified?.Invoke(check, new EventArgs());
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
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
191 /// <summary>Validates server settings.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
192 /// <returns>An empty string if the server is valid, or the reason the server is invalid.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
193 public string Validate()
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 string message = string.Empty;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
196 if (Name.Length == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
197 message += "\"Name\" must not be empty" + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
198 if (Host.Length == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
199 message += "\"Host\" must not be empty" + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
200 return message.Length > 0 ? message : null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
201 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
202
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
203 /// <summary>Updates a check.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
204 public void UpdateCheck(Check check)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
205 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
206 // See if there is already a check with this ID.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
207 Check oldCheck = Checks.FirstOrDefault(c => c.Id == check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
208 if (!ReferenceEquals(check, oldCheck))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
209 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
210 // If there is already a check, but it is a different object instance,
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
211 // replace the old check with the new one (or add it if it is new).
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
212 int index = Checks.IndexOf(oldCheck);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
213 if (index == -1)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
214 Checks.Add(check);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
215 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
216 Checks[index] = check;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
217 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
218 CheckModified?.Invoke(check, new EventArgs());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
219 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
220
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
221 /// <summary>Returns true if the server looks empty (no user data has been entered).</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
222 public bool IsEmpty()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
223 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
224 return Name.IsNullOrEmpty()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
225 && Host.IsNullOrEmpty()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
226 && Checks.Count == 0;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
227 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
228
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
229 /// <summary>Generates the authentication method based on user preferences.</summary>
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
230 private AuthenticationMethod GetAuthentication()
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
231 {
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
232 if (LoginType == LoginType.Password)
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
233 return new PasswordAuthenticationMethod(Username, Password);
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
234 else
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
235 return new PrivateKeyAuthenticationMethod(Username, PrivateKeyFile);
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
236 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
237
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
238 /// <summary>Releases the open SSH connection.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
239 private void InvalidateSshConnection()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
240 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
241 _sshClient?.Dispose();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
242 _sshClient = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
243 }
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
244
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
245 public override string ToString()
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 {
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 Name.IsNullOrEmpty() ? Host : Name;
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
248 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
249 }
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
250
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
251 /// <summary>Possible statuses of the private key file.</summary>
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
252 public enum KeyStatus
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
253 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
254 /// <summary>The private key file is closed for an unspecified reason.</summary>
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 26
diff changeset
255 Closed = 0,
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
256 /// <summary>The private key file is accessible and open.</summary>
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 26
diff changeset
257 Open = 1,
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
258 /// <summary>The private key file is not accessible (missing, access denied, etc).</summary>
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 26
diff changeset
259 NotAccessible = 2,
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
260 /// <summary>The private key file is encrypted and the user has not entered the password yet.</summary>
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 26
diff changeset
261 NeedPassword = 3,
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
262 }
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
263
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
264 }