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 5.0 kB

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