Skip to content

Commit

Permalink
Merge pull request #2904 from studentmain/always-sip002
Browse files Browse the repository at this point in the history
Always sip002
  • Loading branch information
celeron533 committed Jun 27, 2020
2 parents 2030163 + f6fcab4 commit d423a24
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 158 deletions.
12 changes: 6 additions & 6 deletions shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void ping_Completed(object sender, MyPing.CompletedEventArgs e)
{
AppendRecord(server.Identifier(), record);
}
logger.Debug($"Ping {server.FriendlyName()} {e.RoundtripTime.Count} times, {(100 - record.PackageLoss * 100)}% packages loss, min {record.MinResponse} ms, max {record.MaxResponse} ms, avg {record.AverageResponse} ms");
logger.Debug($"Ping {server} {e.RoundtripTime.Count} times, {(100 - record.PackageLoss * 100)}% packages loss, min {record.MinResponse} ms, max {record.MaxResponse} ms, avg {record.AverageResponse} ms");
if (Interlocked.Decrement(ref state.counter) == 0)
{
Save();
Expand Down Expand Up @@ -450,7 +450,7 @@ private void ICMPTest(int delay, object userstate)
{
try
{
logger.Debug($"Ping {server.FriendlyName()}");
logger.Debug($"Ping {server}");
if (ip == null)
{
ip = Dns.GetHostAddresses(server.server)
Expand All @@ -466,7 +466,7 @@ private void ICMPTest(int delay, object userstate)
}
catch (Exception e)
{
logger.Error($"An exception occured while eveluating {server.FriendlyName()}");
logger.Error($"An exception occured while eveluating {server}");
logger.LogUsefulException(e);
FireCompleted(e, userstate);
}
Expand All @@ -478,19 +478,19 @@ private void Ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (e.Reply.Status == IPStatus.Success)
{
logger.Debug($"Ping {server.FriendlyName()} {e.Reply.RoundtripTime} ms");
logger.Debug($"Ping {server} {e.Reply.RoundtripTime} ms");
RoundtripTime.Add((int?)e.Reply.RoundtripTime);
}
else
{
logger.Debug($"Ping {server.FriendlyName()} timeout");
logger.Debug($"Ping {server} timeout");
RoundtripTime.Add(null);
}
TestNext(e.UserState);
}
catch (Exception ex)
{
logger.Error($"An exception occured while eveluating {server.FriendlyName()}");
logger.Error($"An exception occured while eveluating {server}");
logger.LogUsefulException(ex);
FireCompleted(ex, e.UserState);
}
Expand Down
4 changes: 2 additions & 2 deletions shadowsocks-csharp/Controller/Service/TCPRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ private void DestConnectTimer_Elapsed(object sender, ElapsedEventArgs e)
AsyncSession session = timer.Session;
Server server = timer.Server;
OnFailed?.Invoke(this, new SSRelayEventArgs(_server));
Logger.Info($"{server.FriendlyName()} timed out");
Logger.Info($"{server.ToString()} timed out");
session.Remote.Close();
Close();
}
Expand All @@ -830,7 +830,7 @@ private void ConnectCallback(IAsyncResult ar)

_destConnected = true;

Logger.Debug($"Socket connected to ss server: {_server.FriendlyName()}");
Logger.Debug($"Socket connected to ss server: {_server.ToString()}");

TimeSpan latency = DateTime.Now - _startConnectTime;

Expand Down
43 changes: 1 addition & 42 deletions shadowsocks-csharp/Controller/ShadowsocksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,48 +371,7 @@ public void TouchUserRuleFile()

public string GetServerURLForCurrentServer()
{
Server server = GetCurrentServer();
return GetServerURL(server);
}

public static string GetServerURL(Server server)
{
string tag = string.Empty;
string url = string.Empty;

if (string.IsNullOrWhiteSpace(server.plugin))
{
// For backwards compatiblity, if no plugin, use old url format
string parts = $"{server.method}:{server.password}@{server.server}:{server.server_port}";
string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
url = base64;
}
else
{
// SIP002
string parts = $"{server.method}:{server.password}";
string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
string websafeBase64 = base64.Replace('+', '-').Replace('/', '_').TrimEnd('=');

string pluginPart = server.plugin;
if (!string.IsNullOrWhiteSpace(server.plugin_opts))
{
pluginPart += ";" + server.plugin_opts;
}

url = string.Format(
"{0}@{1}:{2}/?plugin={3}",
websafeBase64,
server.FormatHostName(server.server),
server.server_port,
HttpUtility.UrlEncode(pluginPart, Encoding.UTF8));
}

if (!server.remarks.IsNullOrEmpty())
{
tag = $"#{HttpUtility.UrlEncode(server.remarks, Encoding.UTF8)}";
}
return $"ss://{url}{tag}";
return GetCurrentServer().GetURL(_config.generateLegacyUrl);
}

public void UpdateStatisticsConfiguration(bool enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void ChooseNewServer()
100 * 1000 * Math.Min(5 * 60, (now - status.lastFailure).TotalSeconds)
-2 * 5 * (Math.Min(2000, status.latency.TotalMilliseconds) / (1 + (now - status.lastTimeDetectLatency).TotalSeconds / 30 / 10) +
-0.5 * 200 * Math.Min(5, (status.lastRead - status.lastWrite).TotalSeconds));
logger.Debug(String.Format("server: {0} latency:{1} score: {2}", status.server.FriendlyName(), status.latency, status.score));
logger.Debug(String.Format("server: {0} latency:{1} score: {2}", status.server.ToString(), status.latency, status.score));
}
ServerStatus max = null;
foreach (var status in servers)
Expand All @@ -136,14 +136,14 @@ public void ChooseNewServer()
if (_currentServer == null || max.score - _currentServer.score > 200)
{
_currentServer = max;
logger.Info($"HA switching to server: {_currentServer.server.FriendlyName()}");
logger.Info($"HA switching to server: {_currentServer.server.ToString()}");
}
}
}

public void UpdateLatency(Model.Server server, TimeSpan latency)
{
logger.Debug($"latency: {server.FriendlyName()} {latency}");
logger.Debug($"latency: {server.ToString()} {latency}");

ServerStatus status;
if (_serverStatus.TryGetValue(server, out status))
Expand All @@ -155,7 +155,7 @@ public void UpdateLatency(Model.Server server, TimeSpan latency)

public void UpdateLastRead(Model.Server server)
{
logger.Debug($"last read: {server.FriendlyName()}");
logger.Debug($"last read: {server.ToString()}");

ServerStatus status;
if (_serverStatus.TryGetValue(server, out status))
Expand All @@ -166,7 +166,7 @@ public void UpdateLastRead(Model.Server server)

public void UpdateLastWrite(Model.Server server)
{
logger.Debug($"last write: {server.FriendlyName()}");
logger.Debug($"last write: {server.ToString()}");

ServerStatus status;
if (_serverStatus.TryGetValue(server, out status))
Expand All @@ -177,7 +177,7 @@ public void UpdateLastWrite(Model.Server server)

public void SetFailure(Model.Server server)
{
logger.Debug($"failure: {server.FriendlyName()}");
logger.Debug($"failure: {server.ToString()}");

ServerStatus status;
if (_serverStatus.TryGetValue(server, out status))
Expand Down
4 changes: 2 additions & 2 deletions shadowsocks-csharp/Controller/Strategy/StatisticsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ select new
var bestResult = serversWithStatistics
.Aggregate((server1, server2) => server1.score > server2.score ? server1 : server2);

LogWhenEnabled($"Switch to server: {bestResult.server.FriendlyName()} by statistics: score {bestResult.score}");
LogWhenEnabled($"Switch to server: {bestResult.server.ToString()} by statistics: score {bestResult.score}");
_currentServer = bestResult.server;
}
catch (Exception e)
Expand Down Expand Up @@ -147,7 +147,7 @@ public void ReloadServers()

public void SetFailure(Server server)
{
logger.Debug($"failure: {server.FriendlyName()}");
logger.Debug($"failure: {server.ToString()}");
}

public void UpdateLastRead(Server server)
Expand Down
34 changes: 16 additions & 18 deletions shadowsocks-csharp/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Shadowsocks.Model
public class Configuration
{
[JsonIgnore]
private static Logger logger = LogManager.GetCurrentClassLogger();
private static readonly Logger logger = LogManager.GetCurrentClassLogger();

public string version;

Expand All @@ -24,21 +24,26 @@ public class Configuration
public bool enabled;
public bool shareOverLan;
public bool isDefault;
public bool isIPv6Enabled = false;
public int localPort;
public bool portableMode = true;
public bool showPluginOutput;
public string pacUrl;
public string geositeUrl;
public string geositeGroup = "geolocation-!cn";
public bool geositeBlacklistMode = true;

public bool useOnlinePac;
public bool secureLocalPac = true;
public bool availabilityStatistics;
public bool autoCheckUpdate;
public bool checkPreRelease;
public bool isVerboseLogging;

// hidden options
public bool isIPv6Enabled = false; // for experimental ipv6 support
public bool generateLegacyUrl = false; // for pre-sip002 url compatibility
public string geositeUrl; // for custom geosite source (and rule group)
public string geositeGroup = "geolocation-!cn";
public bool geositeBlacklistMode = true;


//public NLogConfig.LogLevel logLevel;
public LogViewerConfig logViewer;
public ProxyConfig proxy;
Expand All @@ -48,11 +53,10 @@ public class Configuration
NLogConfig nLogConfig;

private static readonly string CONFIG_FILE = "gui-config.json";
private static readonly NLogConfig.LogLevel verboseLogLevel =
#if DEBUG
NLogConfig.LogLevel.Trace;
private static readonly NLogConfig.LogLevel verboseLogLevel = NLogConfig.LogLevel.Trace;
#else
NLogConfig.LogLevel.Debug;
private static readonly NLogConfig.LogLevel verboseLogLevel = NLogConfig.LogLevel.Debug;
#endif


Expand Down Expand Up @@ -194,9 +198,9 @@ public static void Save(Configuration config)
sw.Flush();
}
try
{
// apply changs to NLog.config
config.nLogConfig.SetLogLevel(config.isVerboseLogging? verboseLogLevel : NLogConfig.LogLevel.Info);
{
// apply changes to NLog.config
config.nLogConfig.SetLogLevel(config.isVerboseLogging ? verboseLogLevel : NLogConfig.LogLevel.Info);
NLogConfig.SaveXML(config.nLogConfig);
}
catch (Exception e)
Expand All @@ -212,7 +216,7 @@ public static void Save(Configuration config)

public static Server AddDefaultServerOrServer(Configuration config, Server server = null, int? index = null)
{
if (config != null && config.configs != null)
if (config?.configs != null)
{
server = (server ?? GetDefaultServer());

Expand All @@ -231,12 +235,6 @@ public static Server GetDefaultServer()
return new Server();
}

private static void Assert(bool condition)
{
if (!condition)
throw new Exception(I18N.GetString("assertion failure"));
}

public static void CheckPort(int port)
{
if (port <= 0 || port > 65535)
Expand Down
Loading

0 comments on commit d423a24

Please sign in to comment.