Mercurial > servermonitor
comparison ServerMonitor/Objects/Server.cs @ 0:3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 31 Dec 2018 18:32:14 -0500 |
parents | |
children | 3142e52cbe69 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e1a2131f897 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 using System.Security.Cryptography; | |
6 using System.ComponentModel; | |
7 using Renci.SshNet; | |
8 using System.Runtime.Serialization; | |
9 | |
10 namespace ServerMonitorApp | |
11 { | |
12 public enum LoginType { PrivateKey = 0, Password = 1 }; | |
13 | |
14 public class Server | |
15 { | |
16 private string _host; | |
17 private int _port; | |
18 private string _username; | |
19 private LoginType _loginType; | |
20 private string _keyFile; | |
21 private SshClient _sshClient; | |
22 private byte[] passwordHash; | |
23 | |
24 public event EventHandler CheckModified; | |
25 | |
26 public readonly BindingList<Check> Checks = new BindingList<Check>(); | |
27 | |
28 public int Id { get; set; } | |
29 | |
30 public string Name { get; set; } | |
31 | |
32 public string Host | |
33 { | |
34 get { return _host; } | |
35 set { _host = value; InvalidateSshConnection(); } | |
36 } | |
37 | |
38 public int Port | |
39 { | |
40 get { return _port; } | |
41 set { _port = value; InvalidateSshConnection(); } | |
42 } | |
43 | |
44 public string Username | |
45 { | |
46 get { return _username; } | |
47 set { _username = value; InvalidateSshConnection(); } | |
48 } | |
49 | |
50 public LoginType LoginType | |
51 { | |
52 get { return _loginType; } | |
53 set { _loginType = value; InvalidateSshConnection(); } | |
54 } | |
55 | |
56 public string KeyFile | |
57 { | |
58 get { return _keyFile; } | |
59 set { _keyFile = value; InvalidateSshConnection(); } | |
60 } | |
61 | |
62 public string Password | |
63 { | |
64 get { | |
65 return passwordHash == null ? null : | |
66 Encoding.UTF8.GetString(ProtectedData.Unprotect(passwordHash, Encoding.UTF8.GetBytes("Server".Reverse().ToString()), DataProtectionScope.CurrentUser)); | |
67 } | |
68 set | |
69 { | |
70 passwordHash = ProtectedData.Protect(Encoding.UTF8.GetBytes(value), | |
71 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy | |
72 DataProtectionScope.CurrentUser); | |
73 } | |
74 } | |
75 | |
76 public bool Enabled { get; set; } = true; | |
77 | |
78 public SshClient SshClient | |
79 { | |
80 get | |
81 { | |
82 if (_sshClient == null) | |
83 { | |
84 AuthenticationMethod auth = null; | |
85 if (LoginType == LoginType.Password) | |
86 auth = new PasswordAuthenticationMethod(Username, Password); | |
87 else | |
88 new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile)); | |
89 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth); | |
90 _sshClient = new SshClient(info); | |
91 } | |
92 return _sshClient; | |
93 } | |
94 } | |
95 | |
96 /*public Server() { } | |
97 | |
98 public Server(Server server) | |
99 { | |
100 Name = server.Name; | |
101 Host = server.Host; | |
102 Port = server.Port; | |
103 Username = server.Username; | |
104 LoginType = server.LoginType; | |
105 KeyFile = server.KeyFile; | |
106 Enabled = server.Enabled; | |
107 }*/ | |
108 | |
109 public void DeleteCheck(Check check) | |
110 { | |
111 Checks.Remove(check); | |
112 check.Server = null; | |
113 CheckModified?.Invoke(check, new EventArgs()); | |
114 } | |
115 | |
116 public string Validate() | |
117 { | |
118 string message = string.Empty; | |
119 if (Name.Length == 0) | |
120 message += "\"Name\" must not be empty" + Environment.NewLine; | |
121 if (Host.Length == 0) | |
122 message += "\"Host\" must not be empty" + Environment.NewLine; | |
123 return message.Length > 0 ? message : null; | |
124 } | |
125 | |
126 public void UpdateCheck(Check check) | |
127 { | |
128 Check oldCheck = Checks.FirstOrDefault(c => c.Id == check.Id); | |
129 if (!ReferenceEquals(check, oldCheck)) | |
130 { | |
131 int index = Checks.IndexOf(oldCheck); | |
132 if (index == -1) | |
133 Checks.Add(check); | |
134 else | |
135 Checks[index] = check; | |
136 } | |
137 CheckModified?.Invoke(check, new EventArgs()); | |
138 } | |
139 | |
140 private void InvalidateSshConnection() | |
141 { | |
142 _sshClient?.Dispose(); | |
143 _sshClient = null; | |
144 } | |
145 } | |
146 } |