From dafe33f7edeb05d2c6a3bdee5e350af8d25cd155 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 25 Jul 2015 19:05:37 +0800 Subject: [PATCH] fix a crash --- .../Controller/Service/TCPRelay.cs | 33 +++++++++++++++---- .../Controller/Service/UDPRelay.cs | 2 +- .../Controller/ShadowsocksController.cs | 11 +++++++ shadowsocks-csharp/View/MenuViewController.cs | 2 +- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/TCPRelay.cs b/shadowsocks-csharp/Controller/Service/TCPRelay.cs index 39e98f49..e81ecc60 100644 --- a/shadowsocks-csharp/Controller/Service/TCPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/TCPRelay.cs @@ -82,7 +82,7 @@ namespace Shadowsocks.Controller public void CreateRemote() { - Server server = controller.GetCurrentStrategy().GetAServer(IStrategyCallerType.TCP, (IPEndPoint)connection.RemoteEndPoint); + Server server = controller.GetAServer(IStrategyCallerType.TCP, (IPEndPoint)connection.RemoteEndPoint); this.encryptor = EncryptorFactory.GetEncryptor(server.method, server.password); this.server = server; } @@ -373,7 +373,11 @@ namespace Shadowsocks.Controller return; } Server server = ((ServerTimer)sender).Server; - controller.GetCurrentStrategy().SetFailure(server); + IStrategy strategy = controller.GetCurrentStrategy(); + if (strategy != null) + { + strategy.SetFailure(server); + } Console.WriteLine(String.Format("{0} timed out", server.FriendlyName())); remote.Close(); RetryConnect(); @@ -417,7 +421,11 @@ namespace Shadowsocks.Controller // remote.RemoteEndPoint.ToString()); var latency = DateTime.Now - _startConnectTime; - controller.GetCurrentStrategy().UpdateLatency(server, latency); + IStrategy strategy = controller.GetCurrentStrategy(); + if (strategy != null) + { + strategy.UpdateLatency(server, latency); + } StartPipe(); } @@ -428,7 +436,11 @@ namespace Shadowsocks.Controller { if (server != null) { - controller.GetCurrentStrategy().SetFailure(server); + IStrategy strategy = controller.GetCurrentStrategy(); + if (strategy != null) + { + strategy.SetFailure(server); + } } Logging.LogUsefulException(e); RetryConnect(); @@ -479,7 +491,11 @@ namespace Shadowsocks.Controller } connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null); - controller.GetCurrentStrategy().UpdateLastRead(this.server); + IStrategy strategy = controller.GetCurrentStrategy(); + if (strategy != null) + { + strategy.UpdateLastRead(this.server); + } } else { @@ -527,7 +543,12 @@ namespace Shadowsocks.Controller } remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null); - controller.GetCurrentStrategy().UpdateLastWrite(this.server); + + IStrategy strategy = controller.GetCurrentStrategy(); + if (strategy != null) + { + strategy.UpdateLastWrite(this.server); + } } else { diff --git a/shadowsocks-csharp/Controller/Service/UDPRelay.cs b/shadowsocks-csharp/Controller/Service/UDPRelay.cs index 53ee32ac..83a8c5b2 100644 --- a/shadowsocks-csharp/Controller/Service/UDPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/UDPRelay.cs @@ -35,7 +35,7 @@ namespace Shadowsocks.Controller UDPHandler handler = _cache.get(remoteEndPoint); if (handler == null) { - handler = new UDPHandler(socket, _controller.GetCurrentStrategy().GetAServer(IStrategyCallerType.UDP, remoteEndPoint), remoteEndPoint); + handler = new UDPHandler(socket, _controller.GetAServer(IStrategyCallerType.UDP, remoteEndPoint), remoteEndPoint); _cache.add(remoteEndPoint, handler); } handler.Send(firstPacket, length); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 19637c3d..14d705ed 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Net.Sockets; using Shadowsocks.Controller.Strategy; +using System.Net; namespace Shadowsocks.Controller { @@ -101,6 +102,16 @@ namespace Shadowsocks.Controller return null; } + public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint) + { + IStrategy strategy = GetCurrentStrategy(); + if (strategy != null) + { + return strategy.GetAServer(type, localIPEndPoint); + } + return GetCurrentServer(); + } + public void SaveServers(List servers, int localPort) { _config.configs = servers; diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index 629f248b..586eed61 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -125,7 +125,7 @@ namespace Shadowsocks.View _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); string serverInfo = null; - if (config.strategy != null) + if (controller.GetCurrentStrategy() != null) { serverInfo = controller.GetCurrentStrategy().Name; }