From 34ab170e4c40348f2290b6e891e2bbde3375b7b2 Mon Sep 17 00:00:00 2001 From: icylogic Date: Thu, 13 Aug 2015 18:29:17 +0800 Subject: [PATCH] fix serveral minor bugs --- .../Service/AvailabilityStatistics.cs | 32 +++++++++++-------- .../Controller/ShadowsocksController.cs | 11 +++---- .../SimplyChooseByStatisticsStrategy.cs | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs index a6ddc578..532ec466 100644 --- a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs +++ b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs @@ -23,6 +23,7 @@ namespace Shadowsocks.Controller private const int Timeout = 500; private const int Repeat = 4; //repeat times every evaluation private const int Interval = 10*60*1000; //evaluate proxies every 15 minutes + private const int delayBeforeStart = 1*1000; //delay 1 second before start private Timer _timer; private State _state; private List _servers; @@ -40,16 +41,10 @@ namespace Shadowsocks.Controller { try { - if (enabled) - { - if (_timer?.Change(0, Interval) != null) return true; - _state = new State(); - _timer = new Timer(Evaluate, _state, 0, Interval); - } - else - { - _timer?.Dispose(); - } + _timer?.Dispose(); + if (!enabled) return true; + _state = new State(); + _timer = new Timer(Evaluate, _state, delayBeforeStart, Interval); return true; } catch (Exception e) @@ -65,12 +60,21 @@ namespace Shadowsocks.Controller { Logging.Debug("Retrive information of geolocation and isp"); const string api = "http://ip-api.com/json"; - var jsonString = await new HttpClient().GetStringAsync(api); var ret = new DataList { new DataUnit(State.Geolocation, State.Unknown), new DataUnit(State.ISP, State.Unknown), }; + string jsonString; + try + { + jsonString = await new HttpClient().GetStringAsync(api); + } + catch (HttpRequestException e) + { + Logging.LogUsefulException(e); + return ret; + } dynamic obj; if (!global::SimpleJson.SimpleJson.TryDeserializeObject(jsonString, out obj)) return ret; string country = obj["country"]; @@ -86,6 +90,7 @@ namespace Shadowsocks.Controller private static async Task> ICMPTest(Server server) { Logging.Debug("eveluating " + server.FriendlyName()); + if (server.server == "") return null; var ping = new Ping(); var ret = new List(); foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))) @@ -109,9 +114,10 @@ namespace Shadowsocks.Controller var geolocationAndIsp = getGeolocationAndISP(); foreach (var dataLists in await TaskEx.WhenAll(_servers.Select(ICMPTest))) { - await geolocationAndIsp; - foreach (var dataList in dataLists) + if (dataLists == null) continue; + foreach (var dataList in dataLists.Where(dataList => dataList != null)) { + await geolocationAndIsp; Append(dataList, geolocationAndIsp.Result); } } diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 2c0442aa..4ecb3759 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -301,17 +301,16 @@ namespace Shadowsocks.Controller gfwListUpdater.Error += pacServer_PACUpdateError; } - if (_listener != null) - { - _listener.Stop(); - } - if (_availabilityStatics == null) { _availabilityStatics = new AvailabilityStatistics(); - _availabilityStatics.UpdateConfiguration(_config); } + _availabilityStatics.UpdateConfiguration(_config); + if (_listener != null) + { + _listener.Stop(); + } // don't put polipoRunner.Start() before pacServer.Stop() // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1 // though UseShellExecute is set to true now diff --git a/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs b/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs index 5528fc0c..9da0aac5 100644 --- a/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs +++ b/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs @@ -101,7 +101,7 @@ namespace Shadowsocks.Controller.Strategy private void ChooseNewServer(List servers) { - if (_statistics == null) + if (_statistics == null || servers.Count == 0) { return; }