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

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