-
Notifications
You must be signed in to change notification settings - Fork 5
/
Form_Main.cs
614 lines (501 loc) · 22.1 KB
/
Form_Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Globalization;
using Renci.SshNet;
namespace MikrotikSSHBackup
{
public partial class Form_Main : Form
{
#region FormAction
public Form_Main()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
saveDataSet();
Settings.EncryptPassword = "";
SettingAction.WriteXml();
}
private void Form1_Load(object sender, EventArgs e)
{
var arguments = Environment.GetCommandLineArgs();
SettingAction.ReadXml();
if (arguments.Length > 1 && arguments[1] == "Backup")
{
if (!File.Exists("data.xml")) Environment.Exit(0);
if (arguments.Length == 2 && !Settings.EncryptEnable)
{
loadDataSet();
dataGridView1.DataSource = mikrotikListBindingSource;
StartBackup();
SendEmailReport();
Environment.Exit(0);
}
else if (arguments.Length == 3 && !String.IsNullOrEmpty(arguments[2]) && Settings.EncryptEnable)
{
if (LoadCryptDataSet("data.xml", arguments[2], dataSet1))
{
dataGridView1.DataSource = mikrotikListBindingSource;
EmailStatic.EmailServer = dataSet1.Tables["Email"].Rows[0]["Server"].ToString();
EmailStatic.EmailPort = dataSet1.Tables["Email"].Rows[0]["Port"].ToString();
EmailStatic.EnableEmailSSL = Convert.ToBoolean(dataSet1.Tables["Email"].Rows[0]["EnableSSL"]);
EmailStatic.EmailUser = dataSet1.Tables["Email"].Rows[0]["User"].ToString();
EmailStatic.EmailPassword = dataSet1.Tables["Email"].Rows[0]["Password"].ToString();
EmailStatic.EmailAddress = dataSet1.Tables["Email"].Rows[0]["Address"].ToString();
StartBackup();
SendEmailReport();
}
Environment.Exit(0);
}
else if (arguments.Length == 2 && Settings.EncryptEnable)
{
MessageBox.Show("\nData file encrypted!!! Run: \"MikrotikSSHBackup.exe Backup Password\"\n");
Environment.Exit(0);
}
}
else if (arguments.Length > 1 && arguments[1] != "Backup")
{
MessageBox.Show("\nError parameters. Run: \"MikrotikSSHBackup,exe Backup Password\"\n");
Environment.Exit(0);
}
else
{
toolStripProgressBar1.MarqueeAnimationSpeed = 0;
loadDataSet();
}
}
#endregion
#region Tool Strip Button Action
private void tsb_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void tsb_Add_Click(object sender, EventArgs e)
{
addMikrotik("", "", "22", "admin", "");
}
private void tsb_Edit_Click(object sender, EventArgs e)
{
editMikrotik();
}
private void tsb_Delete_Click(object sender, EventArgs e)
{
deleteMikrotik();
}
private void tsb_About_Click(object sender, EventArgs e)
{
Form_About about = new Form_About();
about.ShowDialog();
}
#endregion
#region Context menu
private void cmAdd_Click(object sender, EventArgs e)
{
addMikrotik("", "", "22", "admin", "");
}
private void cmEdit_Click(object sender, EventArgs e)
{
editMikrotik();
}
private void cmDelete_Click(object sender, EventArgs e)
{
deleteMikrotik();
}
private void cmCopy_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow == null) return;
var name = dataGridView1.CurrentRow.Cells["nameDataGridViewTextBoxColumn"].Value.ToString();
var ip = dataGridView1.CurrentRow.Cells["iPDataGridViewTextBoxColumn"].Value.ToString();
var sshport = dataGridView1.CurrentRow.Cells["sSHPortDataGridViewTextBoxColumn"].Value.ToString();
var login = dataGridView1.CurrentRow.Cells["loginDataGridViewTextBoxColumn"].Value.ToString();
var password = dataGridView1.CurrentRow.Cells["passwordDataGridViewTextBoxColumn"].Value.ToString();
addMikrotik(name, ip, sshport, login, password);
}
private void cmConnectOverWinbox_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow == null) return;
var ip = dataGridView1.CurrentRow.Cells["iPDataGridViewTextBoxColumn"].Value.ToString();
var login = dataGridView1.CurrentRow.Cells["loginDataGridViewTextBoxColumn"].Value.ToString();
var password = dataGridView1.CurrentRow.Cells["passwordDataGridViewTextBoxColumn"].Value.ToString();
ExecuteProgram(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("MikrotikSSHBackup.exe", "winbox.exe"),
ip + " " + login + " " + password);
}
#endregion
#region Datagrid Action
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
if (e.Button != MouseButtons.Right || e.RowIndex < 0 || e.ColumnIndex < 0) return;
var pt = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
pt.X += e.Location.X;
pt.Y += e.Location.Y;
contextMenuStrip1.Show(dataGridView1, pt);
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1) editMikrotik();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dataGridViewColumn = dataGridView1.Columns["LastBackup"];
if (dataGridViewColumn != null && (e.RowIndex < 0 || e.ColumnIndex !=
dataGridViewColumn.Index)) return;
if (dataGridView1.CurrentRow == null) return;
var text = GetLastFileDirectory(
GetFolderName(dataGridView1.CurrentRow.Cells[0].Value.ToString(), dataGridView1.CurrentRow.Cells[1].Value.ToString()), dataGridView1.CurrentRow.Cells[0].Value.ToString());
if (text != "")
{
text = File.ReadAllText(text);
Form_LastBackup lb = new Form_LastBackup(text);
lb.ShowDialog();
}
else
{
Form_LastBackup lb = new Form_LastBackup("Not found backup copy");
lb.ShowDialog();
}
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox tb = e.Control as TextBox;
if (tb == null) return;
if (dataGridView1.CurrentCell.ColumnIndex == 4)
{
tb.PasswordChar = '*';
}
else
{
tb.PasswordChar = (char)0;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex != 4) return;
if (e.Value != null && e.Value.ToString().Length > 0)
{
e.Value = new string('*', e.Value.ToString().Length);
}
}
#endregion
#region Backup
private void btn_StartBackup_Click(object sender, EventArgs e)
{
toolStrip1.Enabled = false;
btn_StartBackup.Enabled = false;
dataGridView1.Enabled = false;
toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
toolStripProgressBar1.MarqueeAnimationSpeed = 100;
foreach (var row in dataGridView1.Rows.Cast<DataGridViewRow>().Where(row => row.Cells[0].Value != null))
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.White;
}
row.Cells[5].Value = "";
}
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
StartBackup();
SendEmailReport();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
toolStripProgressBar1.MarqueeAnimationSpeed = 0;
toolStripProgressBar1.Style = ProgressBarStyle.Continuous;
toolStrip1.Enabled = true;
btn_StartBackup.Enabled = true;
dataGridView1.Enabled = true;
}
private void StartBackup()
{
foreach (var row in dataGridView1.Rows.Cast<DataGridViewRow>().Where(row => row.Cells[0].Value != null))
{
try
{
Directory.CreateDirectory(GetFolderName(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString()));
var filename = GetFolderName(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString()) +
row.Cells[0].Value + ", IP " +
row.Cells[1].Value + " " +
DateTime.Now.ToString(CultureInfo.InvariantCulture)
.Replace(@"/", "-")
.Replace(":", "-")+".txt";
saveAsOwnTextFormat(
filename,
MikrotikExportCompact(row.Cells["iPDataGridViewTextBoxColumn"].Value.ToString(),
Convert.ToInt32(row.Cells["sSHPortDataGridViewTextBoxColumn"].Value),
row.Cells["loginDataGridViewTextBoxColumn"].Value.ToString(),
row.Cells["passwordDataGridViewTextBoxColumn"].Value.ToString()));
foreach (DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.LightGreen;
}
row.Cells[5].Value = "OK";
}
catch
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.LightPink;
}
row.Cells[5].Value = "Error";
}
}
}
#region GetMikrotikExportCompactOverSSH
private string MikrotikExportCompact(string MikrotikIP, int MikrotikSSHPort, string MikrotikUser, string MikrotikPassword)
{
ConnectionInfo sLogin = new PasswordConnectionInfo(MikrotikIP, MikrotikSSHPort, MikrotikUser, MikrotikPassword);
SshClient sClient = new SshClient(sLogin);
sClient.Connect();
SshCommand appStatCmd = sClient.CreateCommand("export compact");
appStatCmd.Execute();
sClient.Disconnect();
sClient.Dispose();
return appStatCmd.Result;
}
#endregion
#endregion
#region SaveLoadDataSet
private void saveDataSet()
{
if (EmailStatic.EmailAddress != null)
{
dataSet1.Tables["Email"].Rows[0]["Server"] = EmailStatic.EmailServer;
dataSet1.Tables["Email"].Rows[0]["Port"] = EmailStatic.EmailPort;
dataSet1.Tables["Email"].Rows[0]["EnableSSL"] = EmailStatic.EnableEmailSSL;
dataSet1.Tables["Email"].Rows[0]["User"] = EmailStatic.EmailUser;
dataSet1.Tables["Email"].Rows[0]["Password"] = EmailStatic.EmailPassword;
dataSet1.Tables["Email"].Rows[0]["Address"] = EmailStatic.EmailAddress;
}
switch (Settings.EncryptEnable)
{
case false:
dataSet1.WriteXml("data.xml", XmlWriteMode.IgnoreSchema);
break;
default:
SaveCryptDataSet("data.xml", dataSet1, Settings.EncryptPassword);
break;
}
}
private void loadDataSet()
{
if (!File.Exists("data.xml")) return;
dataSet1.Clear();
if (Settings.EncryptEnable == false && String.IsNullOrEmpty(Settings.EncryptPassword))
{
dataSet1.ReadXml("data.xml");
dataGridView1.DataSource = mikrotikListBindingSource;
}
else if (Settings.EncryptEnable == true && String.IsNullOrEmpty(Settings.EncryptPassword))
{
do
{
PasswordRequest();
} while (!LoadCryptDataSet("data.xml", Settings.EncryptPassword, dataSet1));
dataGridView1.DataSource = mikrotikListBindingSource;
}
else
{
LoadCryptDataSet("data.xml", Settings.EncryptPassword, dataSet1);
}
if (dataSet1.Tables["Email"].Rows.Count < 1) dataSet1.Tables["Email"].Rows.Add("Server", "Port", false, "User", "Password", "adress");
EmailStatic.EmailServer = dataSet1.Tables["Email"].Rows[0]["Server"].ToString();
EmailStatic.EmailPort = dataSet1.Tables["Email"].Rows[0]["Port"].ToString();
EmailStatic.EnableEmailSSL = Convert.ToBoolean(dataSet1.Tables["Email"].Rows[0]["EnableSSL"]);
EmailStatic.EmailUser = dataSet1.Tables["Email"].Rows[0]["User"].ToString();
EmailStatic.EmailPassword = dataSet1.Tables["Email"].Rows[0]["Password"].ToString();
EmailStatic.EmailAddress = dataSet1.Tables["Email"].Rows[0]["Address"].ToString();
}
#endregion
#region File Action
private static string GetFolderName(string name, string ip)
{
return System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("MikrotikSSHBackup.exe", "") +
"Backup" + "\\" +
name + " " +
ip + "\\";
}
private static string GetLastFileDirectory(string folderName, string MikrotikName)
{
try
{
var directory = new DirectoryInfo(folderName);
var myFile = (from f in directory.GetFiles(MikrotikName + "*")
orderby f.LastWriteTime descending
select f).First();
return folderName + myFile;
}
catch
{
return "";
}
}
private void saveAsOwnTextFormat(string filename, string textToSave)
{
try
{
var sw = File.CreateText(filename);
sw.WriteLine(textToSave);
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
#endregion
#region Add, Edit, Delete
private void addMikrotik(string name, string ip, string sshport, string username, string password)
{
Form_AddEdit fae = new Form_AddEdit(name, ip, sshport, username, password);
fae.ShowDialog();
if (fae.DialogResult != DialogResult.OK) return;
dataSet1.Tables["MikrotikList"].Rows.Add(fae.tb_Name.Text, fae.tb_IP.Text, fae.tb_Login.Text, fae.tb_Password.Text, Convert.ToInt32(fae.tbSSHPort.Text));
saveDataSet();
}
private void editMikrotik()
{
if (dataGridView1.CurrentRow == null) return;
var name = dataGridView1.CurrentRow.Cells["nameDataGridViewTextBoxColumn"].Value.ToString();
var ip = dataGridView1.CurrentRow.Cells["iPDataGridViewTextBoxColumn"].Value.ToString();
var sshport = dataGridView1.CurrentRow.Cells["sSHPortDataGridViewTextBoxColumn"].Value.ToString();
var login = dataGridView1.CurrentRow.Cells["loginDataGridViewTextBoxColumn"].Value.ToString();
var password = dataGridView1.CurrentRow.Cells["passwordDataGridViewTextBoxColumn"].Value.ToString();
Form_AddEdit fae = new Form_AddEdit(name, ip, sshport, login, password);
fae.ShowDialog();
if (fae.DialogResult != DialogResult.OK) return;
var mikrotikRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
mikrotikRow["Name"] = fae.tb_Name.Text;
mikrotikRow["IP"] = fae.tb_IP.Text;
mikrotikRow["SSHPort"] = fae.tbSSHPort.Text;
mikrotikRow["Login"] = fae.tb_Login.Text;
mikrotikRow["Password"] = fae.tb_Password.Text;
saveDataSet();
}
private void deleteMikrotik()
{
if (dataGridView1.CurrentRow == null) return;
var result = MessageBox.Show("Are you sure you want to remove the current item?",
"Delete",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result != DialogResult.Yes) return;
mikrotikListBindingSource.RemoveCurrent();
saveDataSet();
}
#endregion
#region Email
private void tsb_Email_Click(object sender, EventArgs e)
{
Form_Email formEmail = new Form_Email();
formEmail.ShowDialog();
}
private void SendEmailReport()
{
if (!Settings.EnableEmail) return;
var i = 0;
var Report = "WARNING!!! A backup copy of the following elements has not been created" + Environment.NewLine;
foreach (var row in from DataGridViewRow row in dataGridView1.Rows where row.Cells[0].Value != null where row.Cells[5].Value.ToString() == "Error" select row)
{
i++;
Report += "Name: " + row.Cells[0].Value + " IP: " + row.Cells[1].Value + Environment.NewLine;
}
if (i <= 0) return;
SendEmailReport ser = new SendEmailReport();
ser.SendEmail(Report, EmailStatic.EmailServer, EmailStatic.EmailPort, EmailStatic.EmailUser, EmailStatic.EmailPassword, EmailStatic.EnableEmailSSL, EmailStatic.EmailAddress);
}
#endregion
#region Start winbox
public static void ExecuteProgram(string fileName, string arguments)
{
if (!File.Exists(fileName))
{
MessageBox.Show("Winbox.exe not found in the program directory!");
return;
}
Process prc = null;
try
{
prc = new Process {StartInfo = {FileName = fileName, Arguments = arguments}};
prc.Start();
}
finally
{
if (prc != null) prc.Close();
}
}
#endregion
#region Password
private void tsbEncrypt_Click(object sender, EventArgs e)
{
Form_Encryption formEncryption = new Form_Encryption();
formEncryption.ShowDialog();
SettingAction.WriteXml();
}
private static void PasswordRequest()
{
Form_RequestPassword formRP = new Form_RequestPassword();
formRP.ShowDialog();
}
private static bool LoadCryptDataSet(string file, string key, DataSet ds)
{
var crypto = Rijndael.Create();
crypto.IV = Encoding.ASCII.GetBytes("qwert".PadRight(16, 'x'));
crypto.Key = Encoding.ASCII.GetBytes(key.PadRight(16, 'x'));
crypto.Padding = PaddingMode.Zeros;
using (FileStream stream = new FileStream(file, FileMode.Open))
{
using (
CryptoStream cryptoStream = new CryptoStream(stream, crypto.CreateDecryptor(), CryptoStreamMode.Read)
)
{
try
{
ds.ReadXml(cryptoStream);
}
catch
{
MessageBox.Show("Password not correct!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
cryptoStream.Close();
stream.Close();
}
}
return true;
}
private static void SaveCryptDataSet(string file, DataSet ds, string key)
{
var crypto = Rijndael.Create();
crypto.IV = Encoding.ASCII.GetBytes("qwert".PadRight(16, 'x'));
crypto.Key = Encoding.ASCII.GetBytes(key.PadRight(16, 'x'));
crypto.Padding = PaddingMode.Zeros;
File.Delete(file);
using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate))
{
using (
CryptoStream cryptoStream = new CryptoStream(stream, crypto.CreateEncryptor(),
CryptoStreamMode.Write))
{
ds.WriteXml(cryptoStream);
cryptoStream.Flush();
stream.Flush();
cryptoStream.Close();
stream.Close();
}
}
}
#endregion
}
}