Browse Source

watch PAC changes

tags/2.3
clowwindy 10 years ago
parent
commit
085532e8fa
2 changed files with 36 additions and 0 deletions
  1. +29
    -0
      shadowsocks-csharp/Controller/PACServer.cs
  2. +7
    -0
      shadowsocks-csharp/Controller/ShadowsocksController.cs

+ 29
- 0
shadowsocks-csharp/Controller/PACServer.cs View File

@@ -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());
}
}
}
}

+ 7
- 0
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -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();
}
}
}

Loading…
Cancel
Save