AMain.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. using System.IO.Compression;
  2. using WinSCP;
  3. namespace AutoPatcherAdmin
  4. {
  5. public partial class AMain : Form
  6. {
  7. public const string PatchFileName = @"PList.gz";
  8. public const string TempUploadDirectory = "Out";
  9. public const string TempDownloadDirectory = "In";
  10. public string[] ExcludeList = new string[] { "Thumbs.db" };
  11. public List<FileInformation> OldList, NewList;
  12. public Queue<FileInformation> UploadList;
  13. public bool Completed;
  14. public AMain()
  15. {
  16. InitializeComponent();
  17. ClientTextBox.Text = Settings.Client;
  18. HostTextBox.Text = Settings.Host;
  19. LoginTextBox.Text = Settings.Login;
  20. PasswordTextBox.Text = Settings.Password;
  21. AllowCleanCheckBox.Checked = Settings.AllowCleanUp;
  22. ProtocolDropDown.SelectedIndex = ProtocolDropDown.FindString(Settings.Protocol);
  23. DeleteDirectory(TempDownloadDirectory);
  24. DeleteDirectory(TempUploadDirectory);
  25. }
  26. private void CompleteDownload()
  27. {
  28. FileLabel.Text = "Complete...";
  29. SpeedLabel.Text = "Complete...";
  30. ActionLabel.Text = "Complete...";
  31. progressBar1.Value = 100;
  32. progressBar2.Value = 100;
  33. DownloadExistingButton.Enabled = true;
  34. Completed = true;
  35. }
  36. private void CompleteUpload()
  37. {
  38. FileLabel.Text = "Complete...";
  39. SpeedLabel.Text = "Complete...";
  40. ActionLabel.Text = "Complete...";
  41. progressBar1.Value = 100;
  42. progressBar2.Value = 100;
  43. ProcessButton.Enabled = true;
  44. ListButton.Enabled = true;
  45. btnFixGZ.Enabled = true;
  46. Completed = true;
  47. }
  48. private void CleanUp()
  49. {
  50. if (!Settings.AllowCleanUp) return;
  51. var rootPath = (new Uri(Settings.Host)).AbsolutePath;
  52. using Session session = new();
  53. OpenSession(session);
  54. for (int i = 0; i < OldList.Count; i++)
  55. {
  56. if (NeedFile(OldList[i].FileName)) continue;
  57. var compressed = OldList[i].Length != OldList[i].Compressed;
  58. try
  59. {
  60. var filename = OldList[i].FileName + (compressed ? ".gz" : "");
  61. var filePath = Path.Combine(rootPath, filename).Replace(@"\", "/");
  62. if (session.FileExists(filePath))
  63. {
  64. session.RemoveFile(filePath);
  65. }
  66. }
  67. catch
  68. {
  69. }
  70. }
  71. }
  72. private bool NeedFile(string fileName)
  73. {
  74. for (int i = 0; i < NewList.Count; i++)
  75. {
  76. if (fileName.EndsWith(NewList[i].FileName) && !InExcludeList(NewList[i].FileName))
  77. return true;
  78. }
  79. return false;
  80. }
  81. private void GetOldFileList()
  82. {
  83. OldList = new List<FileInformation>();
  84. byte[] data = DownloadFile(PatchFileName);
  85. if (data != null)
  86. {
  87. using MemoryStream stream = new MemoryStream(data);
  88. using BinaryReader reader = new BinaryReader(stream);
  89. int count = reader.ReadInt32();
  90. for (int i = 0; i < count; i++)
  91. {
  92. OldList.Add(new FileInformation(reader));
  93. }
  94. }
  95. }
  96. private byte[] CreateNewList()
  97. {
  98. using MemoryStream stream = new MemoryStream();
  99. using BinaryWriter writer = new BinaryWriter(stream);
  100. writer.Write(NewList.Count);
  101. for (int i = 0; i < NewList.Count; i++)
  102. {
  103. NewList[i].Save(writer);
  104. }
  105. return stream.ToArray();
  106. }
  107. private void GetNewFileList()
  108. {
  109. NewList = new List<FileInformation>();
  110. string[] files = Directory.GetFiles(Settings.Client, "*.*" ,SearchOption.AllDirectories);
  111. for (int i = 0; i < files.Length; i++)
  112. {
  113. NewList.Add(GetFileInformation(files[i]));
  114. }
  115. }
  116. private bool InExcludeList(string fileName)
  117. {
  118. foreach (var item in ExcludeList)
  119. {
  120. if (fileName.EndsWith(item)) return true;
  121. }
  122. return false;
  123. }
  124. private void FixFilenameExtensions()
  125. {
  126. var rootPath = (new Uri(Settings.Host)).AbsolutePath;
  127. using Session session = new Session();
  128. OpenSession(session);
  129. for (int i = 0; i < OldList.Count; i++)
  130. {
  131. FileInformation old = OldList[i];
  132. try
  133. {
  134. ActionLabel.Text = old.FileName;
  135. Refresh();
  136. if (old.Compressed == old.Length)
  137. {
  138. //exists, but not compressed, lets rename it
  139. var compressedFilename = OldList[i].FileName + ".gz";
  140. var compressedFilePath = Path.Combine(rootPath, compressedFilename).Replace(@"\", "/");
  141. var uncompressedFilename = OldList[i].FileName;
  142. var uncompressedFilePath = Path.Combine(rootPath, compressedFilename).Replace(@"\", "/");
  143. if (session.FileExists(compressedFilePath))
  144. {
  145. session.MoveFile(compressedFilePath, uncompressedFilename);
  146. }
  147. }
  148. else
  149. {
  150. //exists, but its compressed and ends with .gz, so its correct
  151. }
  152. }
  153. catch
  154. {
  155. }
  156. }
  157. }
  158. private bool NeedUpdate(FileInformation info)
  159. {
  160. for (int i = 0; i < OldList.Count; i++)
  161. {
  162. FileInformation old = OldList[i];
  163. if (old.FileName != info.FileName) continue;
  164. if (old.Length != info.Length) return true;
  165. if (old.Creation != info.Creation) return true;
  166. return false;
  167. }
  168. return true;
  169. }
  170. private FileInformation? GetFileInformation(string fileName)
  171. {
  172. if (!File.Exists(fileName))
  173. {
  174. return null;
  175. }
  176. FileInfo info = new(fileName);
  177. FileInformation file = new()
  178. {
  179. FileName = fileName.Remove(0, Settings.Client.Length).TrimStart('\\'),
  180. Length = (int)info.Length,
  181. Creation = info.LastWriteTime
  182. };
  183. return file;
  184. }
  185. private void OpenSession(Session session)
  186. {
  187. if (session.Opened) return;
  188. Uri uri = null;
  189. if (!string.IsNullOrEmpty(Settings.Host))
  190. {
  191. uri = new Uri(Settings.Host);
  192. }
  193. if (!Protocol.TryParse(Settings.Protocol, true, out Protocol protocol))
  194. {
  195. protocol = Protocol.Ftp;
  196. }
  197. SessionOptions sessionOptions = new SessionOptions
  198. {
  199. Protocol = protocol,
  200. HostName = uri.Host,
  201. UserName = Settings.Login,
  202. Password = Settings.Password
  203. };
  204. if (sessionOptions.Protocol == Protocol.Sftp)
  205. {
  206. var fingerprint = session.ScanFingerprint(sessionOptions, "SHA-256");
  207. sessionOptions.SshHostKeyFingerprint = fingerprint;
  208. }
  209. session.Open(sessionOptions);
  210. }
  211. private void BeginUpload()
  212. {
  213. if (UploadList == null) return;
  214. progressBar1.Value = 0;
  215. progressBar2.Value = 0;
  216. int uploadCount = UploadList.Count;
  217. while (UploadList.Count > 0)
  218. {
  219. FileInformation info = UploadList.Dequeue();
  220. CreateTempUploadFiles(info, File.ReadAllBytes(Settings.Client + (info.FileName)));
  221. }
  222. CleanUp();
  223. CreateTempUploadFiles(new FileInformation { FileName = PatchFileName }, CreateNewList());
  224. UploadFiles(++uploadCount);
  225. UploadList = null;
  226. }
  227. private void CreateTempUploadFiles(FileInformation info, byte[] raw)
  228. {
  229. string fileName = info.FileName.Replace(@"\", "/");
  230. byte[] data = (!Settings.CompressFiles || fileName == "PList.gz") ? raw : Compress(raw);
  231. info.Compressed = data.Length;
  232. if (fileName != "PList.gz" && Settings.CompressFiles)
  233. {
  234. fileName += ".gz";
  235. }
  236. var sourceDir = Path.GetDirectoryName(fileName);
  237. var tempSourceDir = Path.Combine(TempUploadDirectory, sourceDir);
  238. var tempFilePath = Path.Combine(TempUploadDirectory, fileName).Replace(@"\", "/");
  239. if (!Directory.Exists(tempSourceDir))
  240. {
  241. Directory.CreateDirectory(tempSourceDir);
  242. }
  243. File.WriteAllBytes(tempFilePath, data);
  244. File.SetLastWriteTime(tempFilePath, info.Creation);
  245. }
  246. private void UploadFiles(int uploadCount = 0)
  247. {
  248. var rootPath = (new Uri(Settings.Host)).AbsolutePath;
  249. using Session session = new Session();
  250. session.FileTransferProgress += (o, e) =>
  251. {
  252. int value = (int)(e.OverallProgress * 100);
  253. progressBar1.Value = value > progressBar1.Maximum ? progressBar1.Maximum : value;
  254. int value2 = (int)(e.FileProgress * 100);
  255. progressBar2.Value = value2 > progressBar2.Maximum ? progressBar2.Maximum : value2;
  256. FileLabel.Text = e.FileName.TrimStart(TempUploadDirectory.ToCharArray()).TrimStart('\\');
  257. SpeedLabel.Text = ((double)e.CPS / 1024).ToString("0.##") + " KB/s";
  258. ActionLabel.Text = string.Format("Uploading... Files: {0}", uploadCount);
  259. };
  260. session.FileTransferred += (o, e) =>
  261. {
  262. uploadCount--;
  263. if (uploadCount <= 0)
  264. {
  265. uploadCount = 0;
  266. CompleteUpload();
  267. }
  268. };
  269. OpenSession(session);
  270. TransferOptions transferOptions = new TransferOptions
  271. {
  272. TransferMode = TransferMode.Binary,
  273. OverwriteMode = OverwriteMode.Overwrite
  274. };
  275. if (!session.FileExists(rootPath))
  276. {
  277. session.CreateDirectory(rootPath);
  278. }
  279. var result = session.PutFilesToDirectory(TempUploadDirectory, rootPath, options: transferOptions);
  280. result.Check();
  281. DeleteDirectory(TempUploadDirectory);
  282. }
  283. private byte[] DownloadFile(string fileName)
  284. {
  285. using Session session = new Session();
  286. OpenSession(session);
  287. try
  288. {
  289. if (!Directory.Exists(TempDownloadDirectory))
  290. {
  291. Directory.CreateDirectory(TempDownloadDirectory);
  292. }
  293. TransferOptions transferOptions = new TransferOptions
  294. {
  295. TransferMode = TransferMode.Binary,
  296. OverwriteMode = OverwriteMode.Overwrite
  297. };
  298. var rootPath = (new Uri(Settings.Host)).AbsolutePath;
  299. var result = session.GetFiles(Path.Combine(rootPath, fileName), Path.Combine(TempDownloadDirectory, fileName), options: transferOptions);
  300. result.Check();
  301. MemoryStream ms = new MemoryStream();
  302. using (FileStream fs = new FileStream(Path.Combine(TempDownloadDirectory, fileName), FileMode.Open, FileAccess.Read))
  303. fs.CopyTo(ms);
  304. DeleteDirectory(TempDownloadDirectory);
  305. return ms.ToArray();
  306. }
  307. catch
  308. {
  309. return null;
  310. }
  311. }
  312. private void DownloadFiles()
  313. {
  314. var rootPath = (new Uri(Settings.Host)).AbsolutePath;
  315. using Session session = new Session();
  316. session.FileTransferProgress += (o, e) =>
  317. {
  318. int value = (int)(e.OverallProgress * 100);
  319. progressBar1.Value = value > progressBar1.Maximum ? progressBar1.Maximum : value;
  320. int value2 = (int)(e.FileProgress * 100);
  321. progressBar2.Value = value2 > progressBar2.Maximum ? progressBar2.Maximum : value2;
  322. FileLabel.Text = e.FileName.Replace(rootPath, "");
  323. SpeedLabel.Text = ((double)e.CPS / 1024).ToString("0.##") + " KB/s";
  324. ActionLabel.Text = "Downloading... Files";
  325. };
  326. session.FileTransferred += (o, e) =>
  327. {
  328. };
  329. OpenSession(session);
  330. TransferOptions transferOptions = new TransferOptions
  331. {
  332. TransferMode = TransferMode.Binary,
  333. OverwriteMode = OverwriteMode.Overwrite
  334. };
  335. if (!Directory.Exists(TempDownloadDirectory))
  336. {
  337. Directory.CreateDirectory(TempDownloadDirectory);
  338. }
  339. var result = session.GetFilesToDirectory(rootPath, TempDownloadDirectory, options: transferOptions);
  340. result.Check();
  341. CompleteDownload();
  342. }
  343. private void MoveTempDownloadedFiles()
  344. {
  345. for (int i = 0; i < OldList.Count; i++)
  346. {
  347. var info = OldList[i];
  348. var compressed = OldList[i].Length != OldList[i].Compressed;
  349. var filename = OldList[i].FileName + (compressed ? ".gz" : "");
  350. var currentPath = Path.Combine(TempDownloadDirectory, filename);
  351. var relativeDestDir = Path.GetDirectoryName(info.FileName);
  352. var destDir = Path.Combine(Settings.Client, relativeDestDir);
  353. var destFilename = Path.Combine(Settings.Client, info.FileName);
  354. if (!Directory.Exists(destDir))
  355. {
  356. Directory.CreateDirectory(destDir);
  357. }
  358. if (File.Exists(destFilename))
  359. {
  360. File.Delete(destFilename);
  361. }
  362. if (compressed)
  363. {
  364. byte[] raw = File.ReadAllBytes(currentPath);
  365. File.WriteAllBytes(destFilename, Decompress(raw));
  366. }
  367. else
  368. {
  369. File.Move(currentPath, destFilename);
  370. }
  371. File.SetLastWriteTime(destFilename, info.Creation);
  372. }
  373. DeleteDirectory(TempDownloadDirectory);
  374. }
  375. private void ListButton_Click(object sender, EventArgs e)
  376. {
  377. try
  378. {
  379. ListButton.Enabled = false;
  380. Settings.Client = ClientTextBox.Text;
  381. Settings.Host = HostTextBox.Text;
  382. Settings.Login = LoginTextBox.Text;
  383. Settings.Password = PasswordTextBox.Text;
  384. Settings.Protocol = (string)ProtocolDropDown.SelectedItem;
  385. GetOldFileList();
  386. GetNewFileList();
  387. for (int i = 0; i < NewList.Count; i++)
  388. {
  389. FileInformation info = NewList[i];
  390. for (int o = 0; o < OldList.Count; o++)
  391. {
  392. if (OldList[o].FileName != info.FileName) continue;
  393. NewList[i].Compressed = OldList[o].Compressed;
  394. break;
  395. }
  396. if (info.Compressed == 0)
  397. {
  398. //We've uploaded a new file which is unknown to the existing PList (or no PList available). Assume this file was uploaded uncompressed and set to file length.
  399. info.Compressed = info.Length;
  400. }
  401. }
  402. CreateTempUploadFiles(new FileInformation { FileName = PatchFileName }, CreateNewList());
  403. UploadFiles(1);
  404. }
  405. catch (Exception ex)
  406. {
  407. ListButton.Enabled = true;
  408. MessageBox.Show(ex.ToString());
  409. ActionLabel.Text = "Error...";
  410. }
  411. }
  412. private void ProcessButton_Click(object sender, EventArgs e)
  413. {
  414. try
  415. {
  416. ProcessButton.Enabled = false;
  417. Settings.Client = ClientTextBox.Text;
  418. Settings.Host = HostTextBox.Text;
  419. Settings.Login = LoginTextBox.Text;
  420. Settings.Password = PasswordTextBox.Text;
  421. Settings.AllowCleanUp = AllowCleanCheckBox.Checked;
  422. Settings.Protocol = (string)ProtocolDropDown.SelectedItem;
  423. UploadList = new Queue<FileInformation>();
  424. GetOldFileList();
  425. ActionLabel.Text = "Checking Files...";
  426. Refresh();
  427. GetNewFileList();
  428. for (int i = 0; i < NewList.Count; i++)
  429. {
  430. FileInformation info = NewList[i];
  431. if (InExcludeList(info.FileName)) continue;
  432. if (NeedUpdate(info))
  433. {
  434. UploadList.Enqueue(info);
  435. }
  436. else
  437. {
  438. for (int o = 0; o < OldList.Count; o++)
  439. {
  440. if (OldList[o].FileName != info.FileName) continue;
  441. NewList[i] = OldList[o];
  442. break;
  443. }
  444. }
  445. }
  446. BeginUpload();
  447. }
  448. catch (Exception ex)
  449. {
  450. ProcessButton.Enabled = true;
  451. MessageBox.Show(ex.ToString());
  452. ActionLabel.Text = "Error...";
  453. }
  454. }
  455. private void btnFixGZ_Click(object sender, EventArgs e)
  456. {
  457. try
  458. {
  459. btnFixGZ.Enabled = false;
  460. Settings.Client = ClientTextBox.Text;
  461. Settings.Host = HostTextBox.Text;
  462. Settings.Login = LoginTextBox.Text;
  463. Settings.Password = PasswordTextBox.Text;
  464. Settings.Protocol = (string)ProtocolDropDown.SelectedItem;
  465. GetOldFileList();
  466. GetNewFileList();
  467. for (int i = 0; i < NewList.Count; i++)
  468. {
  469. FileInformation info = NewList[i];
  470. for (int o = 0; o < OldList.Count; o++)
  471. {
  472. if (OldList[o].FileName != info.FileName) continue;
  473. NewList[i].Compressed = OldList[o].Length;
  474. break;
  475. }
  476. }
  477. CreateTempUploadFiles(new FileInformation { FileName = PatchFileName }, CreateNewList());
  478. UploadFiles(1);
  479. FixFilenameExtensions();
  480. }
  481. catch(Exception ex)
  482. {
  483. btnFixGZ.Enabled = true;
  484. MessageBox.Show(ex.ToString(), "Error");
  485. ActionLabel.Text = "Error...";
  486. }
  487. }
  488. private void DownloadExistingButton_Click(object sender, EventArgs e)
  489. {
  490. try
  491. {
  492. DownloadExistingButton.Enabled = false;
  493. Settings.Client = ClientTextBox.Text;
  494. Settings.Host = HostTextBox.Text;
  495. Settings.Login = LoginTextBox.Text;
  496. Settings.Password = PasswordTextBox.Text;
  497. Settings.AllowCleanUp = AllowCleanCheckBox.Checked;
  498. Settings.Protocol = (string)ProtocolDropDown.SelectedItem;
  499. GetOldFileList();
  500. DownloadFiles();
  501. MoveTempDownloadedFiles();
  502. }
  503. catch (Exception ex)
  504. {
  505. DownloadExistingButton.Enabled = true;
  506. MessageBox.Show(ex.ToString(), "Error");
  507. ActionLabel.Text = "Error...";
  508. }
  509. }
  510. private void AMain_Load(object sender, EventArgs e)
  511. {
  512. }
  513. private void DeleteDirectory(string target_dir)
  514. {
  515. if (!Directory.Exists(target_dir)) return;
  516. string[] files = Directory.GetFiles(target_dir);
  517. string[] dirs = Directory.GetDirectories(target_dir);
  518. foreach (string file in files)
  519. {
  520. File.SetAttributes(file, FileAttributes.Normal);
  521. File.Delete(file);
  522. }
  523. foreach (string dir in dirs)
  524. {
  525. DeleteDirectory(dir);
  526. }
  527. Directory.Delete(target_dir, false);
  528. }
  529. private byte[] Decompress(byte[] raw)
  530. {
  531. using (GZipStream gStream = new GZipStream(new MemoryStream(raw), CompressionMode.Decompress))
  532. {
  533. const int size = 4096; //4kb
  534. byte[] buffer = new byte[size];
  535. using (MemoryStream mStream = new MemoryStream())
  536. {
  537. int count;
  538. do
  539. {
  540. count = gStream.Read(buffer, 0, size);
  541. if (count > 0)
  542. {
  543. mStream.Write(buffer, 0, count);
  544. }
  545. } while (count > 0);
  546. return mStream.ToArray();
  547. }
  548. }
  549. }
  550. private byte[] Compress(byte[] raw)
  551. {
  552. using (MemoryStream mStream = new MemoryStream())
  553. {
  554. using (GZipStream gStream = new GZipStream(mStream, CompressionMode.Compress, true))
  555. gStream.Write(raw, 0, raw.Length);
  556. return mStream.ToArray();
  557. }
  558. }
  559. }
  560. public class FileInformation
  561. {
  562. public string FileName; //Relative.
  563. public int Length, Compressed;
  564. public DateTime Creation;
  565. public FileInformation()
  566. {
  567. Creation = DateTime.Now;
  568. }
  569. public FileInformation(BinaryReader reader)
  570. {
  571. FileName = reader.ReadString();
  572. Length = reader.ReadInt32();
  573. Compressed = reader.ReadInt32();
  574. Creation = DateTime.FromBinary(reader.ReadInt64());
  575. }
  576. public void Save(BinaryWriter writer)
  577. {
  578. writer.Write(FileName);
  579. writer.Write(Length);
  580. writer.Write(Compressed);
  581. writer.Write(Creation.ToBinary());
  582. }
  583. }
  584. }