/
Main.cs
631 lines (580 loc) · 26.2 KB
/
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Xml;
using DamirM.CommonLibrary;
namespace DamirM.AutoUpdate
{
public partial class Main : Form
{
//public const string constDefaultServerAddress = "http://www.mobilis.hr/_program/update/";
public const string constDefaultServerAddress = "http://update.ubertools.net/";
//public const string constDefaultServerAddress = "http://update.local/";
public const string constUpdateXMLServer = "update.xml";
public const string constUpdateXMLLocal = "upd.xml";
public const string constAutoUpdateExeName = "AutoUpdate.exe"; // TODO: ovo ne bi trebala biti const
public const string constAutoUpdateProductName = "autoupdate";
public const string constAuthorization_username = "admin";
public const string constAuthorization_password = "admin";
// parametars links
public const string constURLParams_get_update_version = "update.php?action=get_update_version&productname={0}&productversion={1}";
public const string constURLParams_get_update_xml = "update.php?action=get_update_xml&productid={0}";
public const string constURLParams_disable_application = "index.php?action=disabled_application&productid={0}&version={1}";
public const string constURLParams_get_product_list = "update.php?action=get_product_list";
public const string constURLParams_get_product_from_product_name = "update.php?action=get_product_from_product_name&productname={0}";
public const string constURLParams_get_product_from_product_id = "update.php?action=get_product_from_product_id&productid={0}";
public const string constURLParams_authorization = "update.php?action=authorization";
public const string constURLParams_download_file = "update.php?action=download_file&fileid={0}";
Update update;
public enum LogType
{
Normal, Append, RewriteLast
}
public Main(string serverAddress, Product product)
{
InitializeComponent();
this.Text = "Damir Marijanovic - AutoUpdate application (" + Application.ProductVersion + ")";
ShowInfo("AutoUpdate application", LogType.Normal);
ShowInfo("Update for " + product.name, LogType.Normal);
ShowInfo("Copyright © 2008 - 2011", LogType.Normal);
// process
//applicationPath = Common.SetSlashOnEndOfDirectory(Application.StartupPath);
//webCalls = WebCalls.Create();
//webCalls.DownloadProgress += new WebCalls.delWebCallsDownloadProgress(webCalls_DownloadProgress);
update = new Update(this, serverAddress, product);
Log.NewMessage += new Log.delGenericLogMessage(Log_NewMessage);
}
public void Log_NewMessage(LogEntery logEntery)
{
if (logEntery.showExternal)
{
foreach (string line in logEntery.GetTextAsArray())
{
ShowInfo(line, LogType.Normal);
}
}
}
public void webCalls_DownloadProgress(long current, long max)
{
try
{
int percentage;
if (max > 0)
{
percentage = (int)(((decimal)current / (decimal)max) * 100);
}
else
{
percentage = 0;
}
pbDownloadFile.Value = percentage;
}
catch (Exception ex)
{
Log.Write(ex, this, "webCalls_DownloadProgress", Log.LogType.ERROR);
}
}
private void Main_Load(object sender, EventArgs e)
{
this.Visible = true;
Application.DoEvents();
// Start main update procedure
update.MainUpdateProcedure();
}
/// <summary>
/// Dali je novi update dostupan
/// </summary>
/// <param name="updateURL"></param>
/// <returns>Ako vrati true, novi update je dostupan</returns>
//public static bool TimeToUpdate(string productUpdateURL, string productID, out string message)
//{
// bool result = false;
// string requestURL;
// string buffer;
// Product product = null;
// //http://update.local/index.php?action=get_update_version&productid={0}
// // Get server version of this product
// requestURL = productUpdateURL + string.Format(constURLParams_get_update_version, productID);
// if (GetHTTPBody(requestURL, out buffer))
// {
// // All ok, in buffer is server update version
// buffer = buffer.Replace("\r\n", "");
// Main.ShowInfo("TimeToUpdate: " + buffer);
// // Load local old update xml file and get update version
// LoadUpdateXML_local(null, out product);
// product.versionServer = buffer;
// if (product.versionServer != product.versionLocal)
// {
// result = true;
// }
// message = "UpdateVersion: " + product.versionServer + " \r\nLocalVersion: " + product.versionLocal;
// }
// else
// {
// Main.ShowInfo("TimeToUpdate: " + buffer);
// message = "Error connecting to update server";
// }
// return result;
//}
/// <summary>
/// This will disable application, server application will deside this
/// </summary>
/// <param name="updateURL">URL</param>
/// <param name="messageText">Message text</param>
/// <returns></returns>
//public static bool DisableMainApplication(string productUpdateURL, string productID, out string message)
//{
// string requestURL;
// string buffer;
// Product product = null;
// bool disableApplication = false;
// LoadUpdateXML_local(null, out product);
// //http://update.local/index.php?action=disabled_application&productid={0}&version={1}
// requestURL = productUpdateURL + string.Format(constURLParams_disable_application, productID, product.versionLocal);
// if (GetHTTPBody(requestURL, out buffer))
// {
// buffer = buffer.Replace("\r\n", "");
// Main.ShowInfo("TimeToUpdate: " + buffer);
// // If response text is yes, then disable application
// if (buffer.ToLower().Equals("yes"))
// {
// disableApplication = true;
// message = "Application disabled, pleas update";
// }
// else
// {
// message = "OK";
// }
// }
// else
// {
// // Error geting response from server, application will not be disabled
// Main.ShowInfo("TimeToUpdate: " + buffer);
// message = "Error connecting to update server";
// }
// return disableApplication;
//}
/// <summary>
/// HTTP request na URL
/// </summary>
/// <param name="netUrl">URL destinacije</param>
/// <returns>Vraca body HTTP requesta</returns>
//public static bool GetHTTPBody(string netUrl, out string buffer)
//{
// bool result = false;
// HttpWebRequest webreq;
// HttpWebResponse webresp;
// StreamReader sr;
// Main.ShowInfo("GetHTTPBody: " + netUrl);
// try
// {
// webreq = (HttpWebRequest)WebRequest.Create(netUrl);
// webresp = (HttpWebResponse)webreq.GetResponse();
// if (webresp.ResponseUri.AbsoluteUri == netUrl)
// {
// sr = new StreamReader(webresp.GetResponseStream());
// buffer = sr.ReadToEnd();
// result = true;
// }
// else
// {
// buffer = "Error";
// }
// Main.ShowInfo("GetHTTPBody: " + buffer);
// }
// catch (Exception e)
// {
// Main.ShowInfo("GetHTTPBody: " + e.Message);
// result = false;
// buffer = e.Message;
// }
// Main.ShowInfo("GetHTTPBody Exit " + result);
// return result;
//}
//private bool SelfUpdateProcedure()
//{
// Product autoUpdateProduct;
// Stream stream;
// string requestURL;
// bool result;
// int fileDownloadCount;
// // Get product info, create product object
// requestURL = serverAddress + AutoUpdate.Main.constURLParams_get_product_from_product_name;
// requestURL = string.Format(requestURL, constAutoUpdateProductName);
// stream = webCalls.WebGETMethod(requestURL);
// autoUpdateProduct = Product.Create(stream);
// // Get File list for autoupdate
// requestURL = serverAddress + string.Format(constURLParams_get_update_xml, autoUpdateProduct.productID);
// stream = webCalls.WebGETMethod(requestURL);
// result = LoadUpdateXML_server(stream, autoUpdateProduct);
// // Find autoupdate.exe filesruct and modified it
// FileStruct fileStruct = autoUpdateProduct.GetFile(constAutoUpdateExeName);
// fileStruct.name = "AutoUpdateUPD.exe";
// fileStruct.autoStartArgs = string.Format("server={0} pid={1}", serverAddress, product.productID);
// Log.Write(autoUpdateProduct.Files, this, "SelfUpdateProcedure", Log.LogType.DEBUG);
// if (result == true)
// {
// if (DownloadAllFile(autoUpdateProduct.Files, out fileDownloadCount))
// {
// if (fileDownloadCount > 0)
// {
// // New AutoUpdate version available
// Log.Write("New AutoUpdate version available", this, "SelfUpdateProcedure", Log.LogType.DEBUG);
// ShowInfo("New AutoUpdate version available", LogType.Normal);
// ShowInfo("Restarting AutoUpdate in 3...", LogType.Normal);
// System.Threading.Thread.Sleep(1000);
// ShowInfo("Restarting AutoUpdate in 2...", LogType.RewriteLast);
// System.Threading.Thread.Sleep(1000);
// ShowInfo("Restarting AutoUpdate in 1...", LogType.RewriteLast);
// AutoStartDownloadFiles(autoUpdateProduct.Files);
// // Exit application now
// Log.Write("Exiting application", this, "SelfUpdateProcedure", Log.LogType.DEBUG);
// Application.Exit();
// }
// else
// {
// return true;
// }
// }
// else
// {
// // Error upgrading
// ShowInfo("", LogType.Normal);
// ShowInfo("An error occurred while trying to update", LogType.Normal);
// Log.Write("An error occurred while trying to update", this, "SelfUpdateProcedure", Log.LogType.WARNING);
// }
// }
// else
// {
// ShowInfo("", LogType.Normal);
// ShowInfo("Error connecting to update server", LogType.Normal);
// Log.Write("Error connecting to update server", this, "SelfUpdateProcedure", Log.LogType.WARNING);
// }
// return false;
//}
//private void MainUpdateProcedure()
//{
// Stream stream;
// bool result = false;
// string updateXMLURL;
// int fileDownloadCount;
// // Get File list for selected product
// updateXMLURL = serverAddress + string.Format(constURLParams_get_update_xml, product.productID);
// stream = webCalls.WebGETMethod(updateXMLURL);
// result = LoadUpdateXML_server(stream, product);
// //GetUpdateLog(autoUpdateProduct, txtLog, "autoupdatelog.txt");
// //GetUpdateLog(product, txtLog, "log.txt");
// Log.Write("Skiping update change log...", this, "MainUpdateProcedure", Log.LogType.INFO);
// // List all files for download
// Log.Write(product.Files, this, "MainUpdateProcedure", Log.LogType.DEBUG);
// // Start main update method
// if (result == true)
// {
// // Load local update xml file,
// Product dummyProduct = new Product();
// LoadUpdateXML_local(product.Files, out dummyProduct);
// // Download all file
// if (DownloadAllFile(product.Files, out fileDownloadCount))
// {
// // Upgrade successfull
// WriteXML(applicationPath + constUpdateXMLLocal);
// ShowInfo("", LogType.Normal);
// if (fileDownloadCount == 0)
// {
// Log.Write("Done", this, "MainUpdateProcedure", Log.LogType.DEBUG);
// ShowInfo("Done", LogType.Normal);
// }
// else
// {
// Log.Write("Update successful", this, "MainUpdateProcedure", Log.LogType.DEBUG);
// ShowInfo("Update successful", LogType.Normal);
// }
// btnAzuriraj.Enabled = true;
// }
// else
// {
// // Error upgrading
// ShowInfo("", LogType.Normal);
// ShowInfo("An error occurred while trying to update", LogType.Normal);
// Log.Write("An error occurred while trying to update", this, "MainUpdateProcedure", Log.LogType.WARNING);
// }
// }
// else
// {
// ShowInfo("", LogType.Normal);
// ShowInfo("Error connecting to update server", LogType.Normal);
// Log.Write("Error connecting to update server", this, "MainUpdateProcedure", Log.LogType.WARNING);
// }
//}
//private bool LoadUpdateXML_server(Stream stream, Product product)
//{
// bool result = false;
// FileStruct fileStruct;
// try
// {
// XmlTextReader reader = new XmlTextReader(stream);
// while (reader.Read())
// {
// if (reader.NodeType == XmlNodeType.Element && reader.Name == "File")
// {
// fileStruct = new FileStruct(serverAddress + constURLParams_download_file, applicationPath);
// bool autoStart = reader.GetAttribute("autoStart") == "1";
// int fileID = int.Parse(reader.GetAttribute("id"));
// int versionServer = int.Parse(reader.GetAttribute("version"));
// fileStruct.AddFileInfo(reader.GetAttribute("name"), fileID, reader.GetAttribute("savePath"), reader.GetAttribute("url"), versionServer, reader.GetAttribute("md5"), autoStart);
// fileStruct.md5Local = MD5Hash(fileStruct.SavePath);
// product.AddFile(fileStruct);
// }
// }
// reader.Close();
// result = true;
// }
// catch (Exception ex)
// {
// ShowInfo("ERROR: " + ex.Message, LogType.Normal);
// Log.Write(ex, this, "LoadUpdateXML_server", Log.LogType.ERROR);
// }
// return result;
//}
//public static void LoadUpdateXML_local(ArrayList fileList, out Product product)
//{
// //string autoUpdateVersion_local = "";
// string applicationPath = Application.StartupPath;
// string xmlPath = "";
// string fileName = "";
// int localVersion;
// FileStruct fileStruct = null;
// product = new Product();
// if (!applicationPath.EndsWith("\\"))
// applicationPath += "\\";
// xmlPath = applicationPath + constUpdateXMLLocal;
// Main.ShowInfo("Debug LoadUpdateXML_local Application.StartupPath " + Application.StartupPath);
// Main.ShowInfo("Debug LoadUpdateXML_local URL " + xmlPath);
// // Ako upd.xml ne postoji, predpostavka je da je update potreban
// if (!File.Exists(xmlPath))
// {
// return;
// }
// // parse xml
// XmlTextReader reader = new XmlTextReader(xmlPath);
// try
// {
// while (reader.Read())
// {
// if (reader.NodeType == XmlNodeType.Element && reader.Name == "Update")
// {
// product.versionLocal = reader.GetAttribute("verzija");
// product.productID = int.Parse(reader.GetAttribute("productid"));
// Main.ShowInfo("Debug LoadUpdateXML_local versionLocal " + product.versionLocal);
// Main.ShowInfo("Debug LoadUpdateXML_local productID " + product.productID);
// }
// else if (reader.NodeType == XmlNodeType.Element && reader.Name == "Datoteka")
// {
// // Ako je fileList null, tada poziv metodi dolati od vanjske aplikacije, aplikacija kojoj treba samo verzija zadnjeg update-a
// if (fileList != null)
// {
// // get file name
// fileName = reader.GetAttribute("ime").ToLower();
// // search for collection of file from server xml list
// for (int i = 0; i < fileList.Count; i++)
// {
// // cast to file list type, make reference to list ithem
// fileStruct = (FileStruct)fileList[i];
// //
// if (fileStruct.name.ToLower() == fileName)
// {
// // update local version of file
// int.TryParse(reader.GetAttribute("ver"), out localVersion);
// fileStruct.versionLocal = localVersion;
// //fileStruct.md5Local = reader.GetAttribute("md5");
// break;
// }
// }
// }
// }
// }
// }
// catch (Exception ex)
// {
// Log.Write(ex, typeof(Main), "LoadUpdateXML_local", Log.LogType.ERROR);
// }
// finally
// {
// reader.Close();
// }
//}
//private bool DownloadAllFile(ArrayList files, out int fileDownloadCount)
//{
// int errorCounter = 0;
// fileDownloadCount = 0;
// foreach (FileStruct fileStruct in files)
// {
// if (FileStruct.FileHaveNewVersion(fileStruct))
// {
// // Download new file from server
// fileStruct.Preuzeto = webCalls.DownloadFile(fileStruct.DownloadURL, fileStruct.SavePath);
// if (fileStruct.Preuzeto == false)
// {
// errorCounter++;
// }
// else
// {
// fileDownloadCount++;
// }
// }
// }
// if (errorCounter > 0)
// {
// // Error exit
// return false;
// }
// else
// {
// // OK exit
// return true;
// }
//}
private void btnIzlaz_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnAzuriraj_Click(object sender, EventArgs e)
{
update.AutoStartDownloadFiles(update.Product.Files);
Application.Exit();
}
//private void AutoStartDownloadFiles(ArrayList fileUpdateList)
//{
// foreach (FileStruct fileStruct in fileUpdateList)
// {
// if (fileStruct.AutoStart)
// {
// ExecuteFileStart(fileStruct.SavePath, fileStruct.autoStartArgs, 1000);
// }
// }
//}
//private bool ExecuteFileStart(string filePath, string args, int sleep)
//{
// bool result = false;
// ShowInfo("Starting " + filePath, LogType.Normal);
// Log.Write(new string[] { "FilePath: " + filePath, "Args:" + args }, this, "ExecuteFileStart", Log.LogType.DEBUG);
// if (File.Exists(filePath))
// {
// try
// {
// if (sleep > 0)
// {
// System.Threading.Thread.Sleep(sleep);
// }
// Process process = Process.Start(filePath, args);
// if (process != null)
// {
// result = true;
// }
// else
// {
// ShowInfo("Error starting file", LogType.Normal);
// Log.Write("Error starting file", this, "ExecuteFileStart", Log.LogType.WARNING);
// }
// }
// catch (Exception ex)
// {
// Log.Write(ex, this, "ExecuteFileStart", Log.LogType.ERROR);
// }
// }
// else
// {
// ShowInfo("Error starting file", LogType.Normal);
// Log.Write("Error starting file", this, "ExecuteFileStart", Log.LogType.WARNING);
// }
// return result;
//}
//private void WriteXML(string putanja)
//{
// const string format = "<Datoteka ime=\"{0}\" local=\"\" url=\"\" ver=\"{1}\" md5=\"{2}\" />";
// StreamWriter writer = new StreamWriter(putanja);
// writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
// writer.WriteLine("<Update verzija=\"{0}\" productid=\"{1}\">", product.versionServer, product.productID);
// foreach (FileStruct file in product.Files)
// {
// int ver;
// string md5;
// if (file.Preuzeto)
// {
// ver = file.versionServer;
// md5 = file.md5Server;
// }
// else
// {
// ver = file.versionLocal;
// md5 = file.md5Local;
// }
// writer.WriteLine(format, file.name, ver, md5);
// }
// writer.WriteLine("</Update>");
// writer.Flush();
// writer.Close();
//}
//public string MD5Hash(string sFilePath)
//{
// try
// {
// if (File.Exists(sFilePath))
// {
// System.Security.Cryptography.MD5CryptoServiceProvider md5Provider
// = new System.Security.Cryptography.MD5CryptoServiceProvider();
// FileStream fs
// = new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
// Byte[] hashCode
// = md5Provider.ComputeHash(fs);
// string ret = "";
// foreach (byte a in hashCode)
// {
// if (a < 16)
// ret += "0" + a.ToString("x");
// else
// ret += a.ToString("x");
// }
// fs.Close();
// return ret;
// }
// else
// {
// Log.Write(new string[] { "Text: File not found", "File: " + sFilePath }, this, "MD5Hash", Log.LogType.WARNING);
// return "";
// }
// }
// catch (Exception ex)
// {
// Log.Write(ex, this, "MD5Hash", Log.LogType.ERROR);
// return "";
// }
//}
public void ShowInfo(string text, LogType logType)
{
if (LogType.Append == logType)
{
lsbLog.Items[lsbLog.Items.Count - 1] = lsbLog.Items[lsbLog.Items.Count - 1] + text;
}
else if (LogType.RewriteLast == logType)
{
lsbLog.Items[lsbLog.Items.Count - 1] = text;
}
else
{
lsbLog.Items.Add(text);
}
lsbLog.SelectedIndex = lsbLog.Items.Count - 1;
Application.DoEvents();
//Log.Write(text, this, "ShowInfo", Log.LogType.INFO);
}
}
}