From bc88ce480ee164db2216278372c79a820e8f342f Mon Sep 17 00:00:00 2001 From: Soul Dark <1012179378@qq.com> Date: Sat, 11 Aug 2018 22:31:03 +0800 Subject: [PATCH] update to 5.0.7 --- .../Controller/ShadowsocksController.cs | 5 +- .../Controller/UpdateChecker.cs | 15 +--- shadowsocks-csharp/Model/Server.cs | 51 ++++++++++++- shadowsocks-csharp/Program.cs | 2 +- shadowsocks-csharp/View/MenuViewController.cs | 72 +++++++++++++++---- shadowsocks-csharp/ssr-win-4.0.xml | 2 +- 6 files changed, 112 insertions(+), 35 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index f7094207..b9086484 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -81,10 +81,7 @@ public void Start() protected void ReportError(Exception e) { - if (Errored != null) - { - Errored(this, new ErrorEventArgs(e)); - } + Errored?.Invoke(this, new ErrorEventArgs(e)); } public void ReloadIPRange() diff --git a/shadowsocks-csharp/Controller/UpdateChecker.cs b/shadowsocks-csharp/Controller/UpdateChecker.cs index 4a9efc2d..c60ab2f4 100755 --- a/shadowsocks-csharp/Controller/UpdateChecker.cs +++ b/shadowsocks-csharp/Controller/UpdateChecker.cs @@ -1,10 +1,7 @@ using Shadowsocks.Model; using System; -using System.Collections; using System.Collections.Generic; using System.Net; -using System.Reflection; -using System.Text; using System.Text.RegularExpressions; using System.Xml; using System.Windows.Forms; @@ -21,7 +18,7 @@ public class UpdateChecker public const string Name = "ShadowsocksR"; public const string Copyright = "Copyright © BreakWa11 2017. Fork from Shadowsocks by clowwindy"; - public const string Version = "5.0.5"; + public const string Version = "5.0.7"; #if !_CONSOLE public const string NetVer = "4.0"; #else @@ -194,10 +191,7 @@ private void http_DownloadStringCompleted(object sender, DownloadStringCompleted SortVersions(versions); LatestVersionURL = versions[versions.Count - 1]; LatestVersionNumber = ParseVersionFromURL(LatestVersionURL); - if (NewVersionFound != null) - { - NewVersionFound(this, new EventArgs()); - } + NewVersionFound?.Invoke(this, new EventArgs()); } catch (Exception ex) { @@ -206,10 +200,7 @@ private void http_DownloadStringCompleted(object sender, DownloadStringCompleted Logging.Debug(e.Error.ToString()); } Logging.Debug(ex.ToString()); - if (NewVersionFound != null) - { - NewVersionFound(this, new EventArgs()); - } + NewVersionFound?.Invoke(this, new EventArgs()); return; } } diff --git a/shadowsocks-csharp/Model/Server.cs b/shadowsocks-csharp/Model/Server.cs index 81fa6773..090655a7 100755 --- a/shadowsocks-csharp/Model/Server.cs +++ b/shadowsocks-csharp/Model/Server.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Text; -using System.IO; using System.Diagnostics; #if !_CONSOLE using SimpleJson; @@ -10,7 +9,7 @@ using System.Text.RegularExpressions; using System.Net; using System.Net.Sockets; -using Shadowsocks.Encryption; +using System.Linq; namespace Shadowsocks.Model { @@ -20,7 +19,7 @@ public class DnsBuffer public DateTime updateTime; public string host; public bool force_expired; - public bool isExpired(string host) + public bool isExpired(string host) { if (updateTime == null) return true; if (this.host != host) return true; @@ -120,6 +119,13 @@ public class Server public bool enable; public bool udp_over_tcp; + public int latency; + + public static int LATENCY_ERROR = -2; + public static int LATENCY_PENDING = -1; + public static int LATENCY_TESTING = 0; + + private object protocoldata; private object obfsdata; private ServerSpeedLog serverSpeedLog = new ServerSpeedLog(); @@ -296,6 +302,7 @@ public Server() group = "FreeSSR-public"; udp_over_tcp = false; enable = true; + latency = LATENCY_PENDING; byte[] id = new byte[16]; Util.Utils.RandBytes(id, id.Length); this.id = BitConverter.ToString(id).Replace("-", ""); @@ -506,5 +513,43 @@ public void setProtocolData(object data) { this.protocoldata = data; } + + public void tcpingLatency() + { + var latencies = new List(); + var sock = new TcpClient(); + var stopwatch = new Stopwatch(); + stopwatch.Start(); + try + { + Dns.GetHostAddresses(server); + } + catch (Exception) + { + latency = LATENCY_ERROR; + return; + } + + var result = sock.BeginConnect(server, server_port, null, null); + if (result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2))) + { + stopwatch.Stop(); + latencies.Add(stopwatch.Elapsed.TotalMilliseconds); + sock.EndConnect(result); + } + else + { + stopwatch.Stop(); + } + + if (latencies.Count != 0) + { + latency = (int)latencies.Average(); + } + else + { + latency = LATENCY_ERROR; + } + } } } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 8da5602a..4b3c951c 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -31,7 +31,7 @@ static void Main(string[] args) { if (arg == "--setautorun") { - if (!Controller.AutoStartup.Switch()) + if (!AutoStartup.Switch()) { Environment.ExitCode = 1; } diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index 5c0c39ae..88f38d59 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -14,6 +14,9 @@ using System.Threading; using System.Text.RegularExpressions; using Shadowsocks.Util; +using System.Net.Sockets; +using System.Net; +using System.Linq; namespace Shadowsocks.View { @@ -40,6 +43,8 @@ public class MenuViewController private UpdateFreeNode updateFreeNodeChecker; private UpdateSubscribeManager updateSubscribeManager; + private bool isBeingUsed = false; + private NotifyIcon _notifyIcon; private ContextMenu contextMenu1; @@ -67,8 +72,9 @@ public class MenuViewController private SubscribeForm subScribeForm; private LogForm logForm; private string _urlToOpen; - private System.Timers.Timer timerDetect360; + private System.Timers.Timer timerDetectVirus; private System.Timers.Timer timerDelayCheckUpdate; + private System.Timers.Timer timerUpdateLatency; private bool configfrom_open = false; private List eventList = new List(); @@ -105,16 +111,21 @@ public MenuViewController(ShadowsocksController controller) updateSubscribeManager = new UpdateSubscribeManager(); LoadCurrentConfiguration(); - timerDetect360 = new System.Timers.Timer(1000.0 * 30); - timerDetect360.Elapsed += timerDetect360_Elapsed; - timerDetect360.Start(); + timerDetectVirus = new System.Timers.Timer(1000.0 * 30); + timerDetectVirus.Elapsed += timerDetectVirus_Elapsed; + timerDetectVirus.Start(); + //this interval will change timerDelayCheckUpdate = new System.Timers.Timer(1000.0 * 10); timerDelayCheckUpdate.Elapsed += timerDelayCheckUpdate_Elapsed; timerDelayCheckUpdate.Start(); + + timerUpdateLatency = new System.Timers.Timer(1000.0 * 3); + timerUpdateLatency.Elapsed += timerUpdateLatency_Elapsed; + timerUpdateLatency.Start(); } - private void timerDetect360_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + private void timerDetectVirus_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (Utils.isVirusExist()) { @@ -144,6 +155,16 @@ private void timerDelayCheckUpdate_Elapsed(object sender, System.Timers.ElapsedE } } + private void timerUpdateLatency_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + timerUpdateLatency.Interval = 1000.0 * 60; + Configuration configuration = _controller.GetCurrentConfiguration(); + foreach (var server in configuration.configs) + { + server.tcpingLatency(); + } + UpdateServersMenu(); + } void controller_Errored(object sender, System.IO.ErrorEventArgs e) { MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message)); @@ -659,12 +680,12 @@ void updateChecker_NewVersionFound(object sender, EventArgs e) void UpdateItem_Clicked(object sender, EventArgs e) { - System.Diagnostics.Process.Start(updateChecker.LatestVersionURL); + Process.Start(updateChecker.LatestVersionURL); } void notifyIcon1_BalloonTipClicked(object sender, EventArgs e) { - System.Diagnostics.Process.Start(updateChecker.LatestVersionURL); + Process.Start(updateChecker.LatestVersionURL); _notifyIcon.BalloonTipClicked -= notifyIcon1_BalloonTipClicked; } @@ -717,7 +738,24 @@ private void UpdateServersMenu() else group_name = server.group; - MenuItem item = new MenuItem(server.FriendlyName()); + string latency; + if (server.latency == Server.LATENCY_TESTING) + { + latency = "[testing]"; + } + else if (server.latency == Server.LATENCY_ERROR) + { + latency = "[error]"; + } + else if (server.latency == Server.LATENCY_PENDING) + { + latency = "[pending]"; + } + else + { + latency = "[" + server.latency.ToString() + "ms]"; + } + MenuItem item = new MenuItem(latency + " " + server.FriendlyName()); item.Tag = i; item.Click += AServerItem_Click; if (configuration.index == i) @@ -900,7 +938,7 @@ void configForm_FormClosed(object sender, FormClosedEventArgs e) { configForm = null; configfrom_open = false; - Util.Utils.ReleaseMemory(); + Utils.ReleaseMemory(); if (eventList.Count > 0) { foreach (EventParams p in eventList) @@ -914,7 +952,7 @@ void configForm_FormClosed(object sender, FormClosedEventArgs e) void settingsForm_FormClosed(object sender, FormClosedEventArgs e) { settingsForm = null; - Util.Utils.ReleaseMemory(); + Utils.ReleaseMemory(); } void serverLogForm_FormClosed(object sender, FormClosedEventArgs e) @@ -992,11 +1030,11 @@ private void Quit() serverLogForm.Close(); serverLogForm = null; } - if (timerDetect360 != null) + if (timerDetectVirus != null) { - timerDetect360.Elapsed -= timerDetect360_Elapsed; - timerDetect360.Stop(); - timerDetect360 = null; + timerDetectVirus.Elapsed -= timerDetectVirus_Elapsed; + timerDetectVirus.Stop(); + timerDetectVirus = null; } if (timerDelayCheckUpdate != null) { @@ -1004,6 +1042,12 @@ private void Quit() timerDelayCheckUpdate.Stop(); timerDelayCheckUpdate = null; } + if (timerUpdateLatency != null) + { + timerUpdateLatency.Elapsed -= timerUpdateLatency_Elapsed; + timerUpdateLatency.Stop(); + timerUpdateLatency = null; + } _notifyIcon.Visible = false; Application.Exit(); } diff --git a/shadowsocks-csharp/ssr-win-4.0.xml b/shadowsocks-csharp/ssr-win-4.0.xml index 0099b07c..18453517 100644 --- a/shadowsocks-csharp/ssr-win-4.0.xml +++ b/shadowsocks-csharp/ssr-win-4.0.xml @@ -2,7 +2,7 @@ - +