diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index c5d8f7d3..a9f76b9d 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -1,6 +1,7 @@ using shadowsocks_csharp.Properties; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Net; @@ -14,6 +15,10 @@ namespace shadowsocks_csharp.Controller private static string PAC_FILE = "pac.txt"; Socket listener; + FileSystemWatcher watcher; + + public event EventHandler PACFileChanged; + public void Start() { // Create a TCP/IP socket. @@ -69,6 +74,7 @@ namespace shadowsocks_csharp.Controller if (File.Exists(PAC_FILE)) { + watchPACFile(); return File.ReadAllText(PAC_FILE, Encoding.UTF8); } else @@ -134,5 +140,28 @@ Connection: Close conn.Shutdown(SocketShutdown.Send); } + private void watchPACFile() + { + if (watcher != null) + { + watcher.Dispose(); + } + watcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); + watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; + watcher.Filter = PAC_FILE; + watcher.Changed += watcher_Changed; + watcher.Created += watcher_Changed; + watcher.Deleted += watcher_Changed; + watcher.Renamed += watcher_Changed; + watcher.EnableRaisingEvents = true; + } + + void watcher_Changed(object sender, FileSystemEventArgs e) + { + if (PACFileChanged != null) + { + PACFileChanged(this, new EventArgs()); + } + } } } diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 32042d48..593b0f00 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -46,6 +46,7 @@ namespace shadowsocks_csharp.Controller { local.Start(); pacServer = new PACServer(); + pacServer.PACFileChanged += pacServer_PACFileChanged; pacServer.Start(); } catch (Exception e) @@ -125,5 +126,11 @@ namespace shadowsocks_csharp.Controller SystemProxy.Disable(); } } + + private void pacServer_PACFileChanged(object sender, EventArgs e) + { + updateSystemProxy(); + } + } }