Merge upstream
# Conflicts: # README.md # shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs # shadowsocks-csharp/Controller/Service/PACServer.cs # shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs # shadowsocks-csharp/Data/cn.txt # shadowsocks-csharp/Model/Configuration.cs # shadowsocks-csharp/Model/LogViewerConfig.cs # shadowsocks-csharp/View/LogForm.Designer.cs # shadowsocks-csharp/View/LogForm.cs # shadowsocks-csharp/View/MenuViewController.cs
This commit is contained in:
@@ -32,11 +32,20 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
}
|
||||
|
||||
public static void Error(object o)
|
||||
{
|
||||
Console.WriteLine("[E] "+ o);
|
||||
}
|
||||
|
||||
public static void Info(object o)
|
||||
{
|
||||
Console.WriteLine(o);
|
||||
}
|
||||
|
||||
public static void Debug(object o)
|
||||
{
|
||||
|
||||
#if DEBUG
|
||||
Console.WriteLine(o);
|
||||
Console.WriteLine("[D] " + o);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -126,8 +126,7 @@ namespace Shadowsocks.Controller
|
||||
var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6));
|
||||
var ping = new Ping();
|
||||
var ret = new List<DataList>();
|
||||
foreach (
|
||||
var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern)))
|
||||
foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern)))
|
||||
{
|
||||
//ICMP echo. we can also set options and special bytes
|
||||
try
|
||||
@@ -146,7 +145,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"An exception occured when eveluating {server.FriendlyName()}");
|
||||
Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
|
||||
Logging.LogUsefulException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Utils.GetTempPath() + "\\gfwlist.txt", e.Result, Encoding.UTF8);
|
||||
List<string> lines = ParseResult(e.Result);
|
||||
if (File.Exists(USER_RULE_FILE))
|
||||
{
|
||||
@@ -92,7 +93,7 @@ namespace Shadowsocks.Controller
|
||||
http.DownloadStringAsync(new Uri(GFWLIST_URL));
|
||||
}
|
||||
|
||||
public List<string> ParseResult(string response)
|
||||
public static List<string> ParseResult(string response)
|
||||
{
|
||||
byte[] bytes = Convert.FromBase64String(response);
|
||||
string content = Encoding.ASCII.GetString(bytes);
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace Shadowsocks.Controller
|
||||
_tcpSocket.Listen(1024);
|
||||
|
||||
// Start an asynchronous socket to listen for connections.
|
||||
Console.WriteLine("Shadowsocks started");
|
||||
Logging.Info("Shadowsocks started");
|
||||
_tcpSocket.BeginAccept(
|
||||
new AsyncCallback(AcceptCallback),
|
||||
_tcpSocket);
|
||||
@@ -163,7 +163,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
Logging.LogUsefulException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -208,7 +208,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
Logging.LogUsefulException(e);
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Shadowsocks.Properties;
|
||||
using Shadowsocks.Util;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
@@ -15,19 +16,20 @@ namespace Shadowsocks.Controller
|
||||
class PACServer : Listener.Service
|
||||
{
|
||||
public static string PAC_FILE = "pac.txt";
|
||||
|
||||
public static string USER_RULE_FILE = "user-rule.txt";
|
||||
|
||||
public static string USER_ABP_FILE = "abp.txt";
|
||||
|
||||
FileSystemWatcher watcher;
|
||||
FileSystemWatcher PACFileWatcher;
|
||||
FileSystemWatcher UserRuleFileWatcher;
|
||||
private Configuration _config;
|
||||
|
||||
public event EventHandler PACFileChanged;
|
||||
public event EventHandler UserRuleFileChanged;
|
||||
|
||||
public PACServer()
|
||||
{
|
||||
this.WatchPacFile();
|
||||
this.WatchUserRuleFile();
|
||||
}
|
||||
|
||||
public void UpdateConfiguration(Configuration config)
|
||||
@@ -151,7 +153,7 @@ Connection: Close
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
Logging.LogUsefulException(e);
|
||||
socket.Close();
|
||||
}
|
||||
}
|
||||
@@ -169,28 +171,81 @@ Connection: Close
|
||||
|
||||
private void WatchPacFile()
|
||||
{
|
||||
if (watcher != null)
|
||||
if (PACFileWatcher != null)
|
||||
{
|
||||
watcher.Dispose();
|
||||
PACFileWatcher.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;
|
||||
PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
|
||||
PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
|
||||
PACFileWatcher.Filter = PAC_FILE;
|
||||
PACFileWatcher.Changed += PACFileWatcher_Changed;
|
||||
PACFileWatcher.Created += PACFileWatcher_Changed;
|
||||
PACFileWatcher.Deleted += PACFileWatcher_Changed;
|
||||
PACFileWatcher.Renamed += PACFileWatcher_Changed;
|
||||
PACFileWatcher.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
private void Watcher_Changed(object sender, FileSystemEventArgs e)
|
||||
private void WatchUserRuleFile()
|
||||
{
|
||||
if (PACFileChanged != null)
|
||||
if (UserRuleFileWatcher != null)
|
||||
{
|
||||
PACFileChanged(this, new EventArgs());
|
||||
UserRuleFileWatcher.Dispose();
|
||||
}
|
||||
UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
|
||||
UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
|
||||
UserRuleFileWatcher.Filter = USER_RULE_FILE;
|
||||
UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed;
|
||||
UserRuleFileWatcher.Created += UserRuleFileWatcher_Changed;
|
||||
UserRuleFileWatcher.Deleted += UserRuleFileWatcher_Changed;
|
||||
UserRuleFileWatcher.Renamed += UserRuleFileWatcher_Changed;
|
||||
UserRuleFileWatcher.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
#region FileSystemWatcher.OnChanged()
|
||||
|
||||
// FileSystemWatcher Changed event is raised twice
|
||||
// http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
|
||||
private static Hashtable fileChangedTime = new Hashtable();
|
||||
|
||||
private void PACFileWatcher_Changed(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath.ToString();
|
||||
string currentLastWriteTime = File.GetLastWriteTime(e.FullPath).ToString();
|
||||
|
||||
// if there is no path info stored yet or stored path has different time of write then the one now is inspected
|
||||
if (!fileChangedTime.ContainsKey(path) || fileChangedTime[path].ToString() != currentLastWriteTime)
|
||||
{
|
||||
if (PACFileChanged != null)
|
||||
{
|
||||
Logging.Info($"Detected: PAC file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
|
||||
PACFileChanged(this, new EventArgs());
|
||||
}
|
||||
|
||||
//lastly we update the last write time in the hashtable
|
||||
fileChangedTime[path] = currentLastWriteTime;
|
||||
}
|
||||
}
|
||||
|
||||
private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath.ToString();
|
||||
string currentLastWriteTime = File.GetLastWriteTime(e.FullPath).ToString();
|
||||
|
||||
// if there is no path info stored yet or stored path has different time of write then the one now is inspected
|
||||
if (!fileChangedTime.ContainsKey(path) || fileChangedTime[path].ToString() != currentLastWriteTime)
|
||||
{
|
||||
if (UserRuleFileChanged != null)
|
||||
{
|
||||
Logging.Info($"Detected: User Rule file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
|
||||
UserRuleFileChanged(this, new EventArgs());
|
||||
}
|
||||
//lastly we update the last write time in the hashtable
|
||||
fileChangedTime[path] = currentLastWriteTime;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks)
|
||||
{
|
||||
//try
|
||||
@@ -204,7 +259,7 @@ Connection: Close
|
||||
//}
|
||||
//catch (Exception e)
|
||||
//{
|
||||
// Console.WriteLine(e);
|
||||
// Logging.LogUsefulException(e);
|
||||
//}
|
||||
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
Logging.LogUsefulException(e);
|
||||
}
|
||||
}
|
||||
string polipoConfig = Resources.privoxy_conf;
|
||||
@@ -94,7 +94,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
Logging.LogUsefulException(e);
|
||||
}
|
||||
_process = null;
|
||||
}
|
||||
|
||||
@@ -140,7 +140,6 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
else
|
||||
{
|
||||
//Console.WriteLine("bytesRead: " + bytesRead.ToString());
|
||||
_local.Shutdown(SocketShutdown.Send);
|
||||
_localShutdown = true;
|
||||
CheckClose();
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Shadowsocks.Controller
|
||||
lock (this.Handlers)
|
||||
{
|
||||
this.Handlers.Add(handler);
|
||||
Logging.Debug($"connections: {Handlers.Count}");
|
||||
Logging.Debug($"TCP connections: {Handlers.Count}");
|
||||
DateTime now = DateTime.Now;
|
||||
if (now - _lastSweepTime > TimeSpan.FromSeconds(1))
|
||||
{
|
||||
@@ -65,10 +65,10 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
foreach (Handler handler1 in handlersToClose)
|
||||
{
|
||||
Logging.Debug("Closing timed out connection");
|
||||
Logging.Debug("Closing timed out TCP connection.");
|
||||
handler1.Close();
|
||||
}
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
lock (relay.Handlers)
|
||||
{
|
||||
Logging.Debug($"connections: {relay.Handlers.Count}");
|
||||
Logging.Debug($"TCP connections: {relay.Handlers.Count}");
|
||||
relay.Handlers.Remove(this);
|
||||
}
|
||||
lock (this)
|
||||
@@ -213,7 +213,7 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
// reject socks 4
|
||||
response = new byte[] { 0, 91 };
|
||||
Console.WriteLine("socks 5 protocol error");
|
||||
Logging.Error("socks 5 protocol error");
|
||||
}
|
||||
Logging.Debug($"======Send Local Port, size:" + response.Length);
|
||||
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
|
||||
@@ -284,7 +284,7 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("failed to recv data in handshakeReceive2Callback");
|
||||
Logging.Error("failed to recv data in handshakeReceive2Callback");
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
@@ -431,7 +431,7 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
strategy.SetFailure(server);
|
||||
}
|
||||
Console.WriteLine(String.Format("{0} timed out", server.FriendlyName()));
|
||||
Logging.Info($"{server.FriendlyName()} timed out");
|
||||
remote.Close();
|
||||
RetryConnect();
|
||||
}
|
||||
@@ -470,8 +470,7 @@ namespace Shadowsocks.Controller
|
||||
|
||||
connected = true;
|
||||
|
||||
//Console.WriteLine("Socket connected to {0}",
|
||||
// remote.RemoteEndPoint.ToString());
|
||||
Logging.Debug($"Socket connected to {remote.RemoteEndPoint}");
|
||||
|
||||
var latency = DateTime.Now - _startConnectTime;
|
||||
IStrategy strategy = controller.GetCurrentStrategy();
|
||||
@@ -556,7 +555,6 @@ namespace Shadowsocks.Controller
|
||||
}
|
||||
else
|
||||
{
|
||||
//Console.WriteLine("bytesRead: " + bytesRead.ToString());
|
||||
connection.Shutdown(SocketShutdown.Send);
|
||||
connectionShutdown = true;
|
||||
CheckClose();
|
||||
|
||||
@@ -7,6 +7,8 @@ using System.Threading;
|
||||
using System.Net.Sockets;
|
||||
using Shadowsocks.Controller.Strategy;
|
||||
using System.Net;
|
||||
using Shadowsocks.Util;
|
||||
using Shadowsocks.Properties;
|
||||
|
||||
namespace Shadowsocks.Controller
|
||||
{
|
||||
@@ -314,6 +316,7 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
_pacServer = new PACServer();
|
||||
_pacServer.PACFileChanged += pacServer_PACFileChanged;
|
||||
_pacServer.UserRuleFileChanged += pacServer_UserRuleFileChanged;
|
||||
}
|
||||
_pacServer.UpdateConfiguration(_config);
|
||||
if (gfwListUpdater == null)
|
||||
@@ -424,6 +427,39 @@ namespace Shadowsocks.Controller
|
||||
UpdatePACFromGFWListError(this, e);
|
||||
}
|
||||
|
||||
private void pacServer_UserRuleFileChanged(object sender, EventArgs e)
|
||||
{
|
||||
// TODO: this is a dirty hack. (from code GListUpdater.http_DownloadStringCompleted())
|
||||
if (!File.Exists(Utils.GetTempPath() + "\\gfwlist.txt"))
|
||||
{
|
||||
UpdatePACFromGFWList();
|
||||
return;
|
||||
}
|
||||
List<string> lines = GFWListUpdater.ParseResult(File.ReadAllText(Utils.GetTempPath() + "\\gfwlist.txt"));
|
||||
if (File.Exists(PACServer.USER_RULE_FILE))
|
||||
{
|
||||
string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8);
|
||||
string[] rules = local.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string rule in rules)
|
||||
{
|
||||
if (rule.StartsWith("!") || rule.StartsWith("["))
|
||||
continue;
|
||||
lines.Add(rule);
|
||||
}
|
||||
}
|
||||
string abpContent = Utils.UnGzip(Resources.abp_js);
|
||||
abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines));
|
||||
if (File.Exists(PACServer.PAC_FILE))
|
||||
{
|
||||
string original = File.ReadAllText(PACServer.PAC_FILE, Encoding.UTF8);
|
||||
if (original == abpContent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
|
||||
}
|
||||
|
||||
private void StartReleasingMemory()
|
||||
{
|
||||
_ramThread = new Thread(new ThreadStart(ReleaseMemory));
|
||||
|
||||
@@ -132,14 +132,14 @@ namespace Shadowsocks.Controller.Strategy
|
||||
if (_currentServer == null || max.score - _currentServer.score > 200)
|
||||
{
|
||||
_currentServer = max;
|
||||
Console.WriteLine("HA switching to server: {0}", _currentServer.server.FriendlyName());
|
||||
Logging.Info($"HA switching to server: {_currentServer.server.FriendlyName()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateLatency(Model.Server server, TimeSpan latency)
|
||||
{
|
||||
Logging.Debug(String.Format("latency: {0} {1}", server.FriendlyName(), latency));
|
||||
Logging.Debug($"latency: {server.FriendlyName()} {latency}");
|
||||
|
||||
ServerStatus status;
|
||||
if (_serverStatus.TryGetValue(server, out status))
|
||||
@@ -151,7 +151,7 @@ namespace Shadowsocks.Controller.Strategy
|
||||
|
||||
public void UpdateLastRead(Model.Server server)
|
||||
{
|
||||
Logging.Debug(String.Format("last read: {0}", server.FriendlyName()));
|
||||
Logging.Debug($"last read: {server.FriendlyName()}");
|
||||
|
||||
ServerStatus status;
|
||||
if (_serverStatus.TryGetValue(server, out status))
|
||||
@@ -162,7 +162,7 @@ namespace Shadowsocks.Controller.Strategy
|
||||
|
||||
public void UpdateLastWrite(Model.Server server)
|
||||
{
|
||||
Logging.Debug(String.Format("last write: {0}", server.FriendlyName()));
|
||||
Logging.Debug($"last write: {server.FriendlyName()}");
|
||||
|
||||
ServerStatus status;
|
||||
if (_serverStatus.TryGetValue(server, out status))
|
||||
@@ -173,7 +173,7 @@ namespace Shadowsocks.Controller.Strategy
|
||||
|
||||
public void SetFailure(Model.Server server)
|
||||
{
|
||||
Logging.Debug(String.Format("failure: {0}", server.FriendlyName()));
|
||||
Logging.Debug($"failure: {server.FriendlyName()}");
|
||||
|
||||
ServerStatus status;
|
||||
if (_serverStatus.TryGetValue(server, out status))
|
||||
|
||||
Reference in New Issue
Block a user