You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PACDaemon.cs 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Shadowsocks.Properties;
  2. using Shadowsocks.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Shadowsocks.Controller
  10. {
  11. /// <summary>
  12. /// Processing the PAC file content
  13. /// </summary>
  14. public class PACDaemon
  15. {
  16. public const string PAC_FILE = "pac.txt";
  17. public const string USER_RULE_FILE = "user-rule.txt";
  18. public const string USER_ABP_FILE = "abp.txt";
  19. FileSystemWatcher PACFileWatcher;
  20. FileSystemWatcher UserRuleFileWatcher;
  21. public event EventHandler PACFileChanged;
  22. public event EventHandler UserRuleFileChanged;
  23. public PACDaemon()
  24. {
  25. this.WatchPacFile();
  26. this.WatchUserRuleFile();
  27. }
  28. public string TouchPACFile()
  29. {
  30. if (File.Exists(PAC_FILE))
  31. {
  32. return PAC_FILE;
  33. }
  34. else
  35. {
  36. FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt);
  37. return PAC_FILE;
  38. }
  39. }
  40. internal string TouchUserRuleFile()
  41. {
  42. if (File.Exists(USER_RULE_FILE))
  43. {
  44. return USER_RULE_FILE;
  45. }
  46. else
  47. {
  48. File.WriteAllText(USER_RULE_FILE, Resources.user_rule);
  49. return USER_RULE_FILE;
  50. }
  51. }
  52. internal string GetPACContent()
  53. {
  54. if (File.Exists(PAC_FILE))
  55. {
  56. return File.ReadAllText(PAC_FILE, Encoding.UTF8);
  57. }
  58. else
  59. {
  60. return Utils.UnGzip(Resources.proxy_pac_txt);
  61. }
  62. }
  63. private void WatchPacFile()
  64. {
  65. PACFileWatcher?.Dispose();
  66. PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  67. PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  68. PACFileWatcher.Filter = PAC_FILE;
  69. PACFileWatcher.Changed += PACFileWatcher_Changed;
  70. PACFileWatcher.Created += PACFileWatcher_Changed;
  71. PACFileWatcher.Deleted += PACFileWatcher_Changed;
  72. PACFileWatcher.Renamed += PACFileWatcher_Changed;
  73. PACFileWatcher.EnableRaisingEvents = true;
  74. }
  75. private void WatchUserRuleFile()
  76. {
  77. UserRuleFileWatcher?.Dispose();
  78. UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
  79. UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  80. UserRuleFileWatcher.Filter = USER_RULE_FILE;
  81. UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed;
  82. UserRuleFileWatcher.Created += UserRuleFileWatcher_Changed;
  83. UserRuleFileWatcher.Deleted += UserRuleFileWatcher_Changed;
  84. UserRuleFileWatcher.Renamed += UserRuleFileWatcher_Changed;
  85. UserRuleFileWatcher.EnableRaisingEvents = true;
  86. }
  87. #region FileSystemWatcher.OnChanged()
  88. // FileSystemWatcher Changed event is raised twice
  89. // http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
  90. // Add a short delay to avoid raise event twice in a short period
  91. private void PACFileWatcher_Changed(object sender, FileSystemEventArgs e)
  92. {
  93. if (PACFileChanged != null)
  94. {
  95. Logging.Info($"Detected: PAC file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
  96. Task.Factory.StartNew(() =>
  97. {
  98. ((FileSystemWatcher)sender).EnableRaisingEvents = false;
  99. System.Threading.Thread.Sleep(10);
  100. PACFileChanged(this, new EventArgs());
  101. ((FileSystemWatcher)sender).EnableRaisingEvents = true;
  102. });
  103. }
  104. }
  105. private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e)
  106. {
  107. if (UserRuleFileChanged != null)
  108. {
  109. Logging.Info($"Detected: User Rule file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
  110. Task.Factory.StartNew(() =>
  111. {
  112. ((FileSystemWatcher)sender).EnableRaisingEvents = false;
  113. System.Threading.Thread.Sleep(10);
  114. UserRuleFileChanged(this, new EventArgs());
  115. ((FileSystemWatcher)sender).EnableRaisingEvents = true;
  116. });
  117. }
  118. }
  119. #endregion
  120. }
  121. }