Browse Source

featured: save bandwidth in config.json file

tags/3.0
kimw 8 years ago
parent
commit
f9bd1c9150
4 changed files with 26 additions and 51 deletions
  1. +1
    -1
      shadowsocks-csharp/Controller/Service/UpdateChecker.cs
  2. +7
    -1
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  3. +6
    -36
      shadowsocks-csharp/Model/Configuration.cs
  4. +12
    -13
      shadowsocks-csharp/Model/Server.cs

+ 1
- 1
shadowsocks-csharp/Controller/Service/UpdateChecker.cs View File

@@ -23,7 +23,7 @@ namespace Shadowsocks.Controller
public string LatestVersionLocalName; public string LatestVersionLocalName;
public event EventHandler CheckUpdateCompleted; public event EventHandler CheckUpdateCompleted;
public const string Version = "2.5.8.1";
public const string Version = "2.5.8.2";
private class CheckUpdateTimer : System.Timers.Timer private class CheckUpdateTimer : System.Timers.Timer
{ {


+ 7
- 1
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -63,6 +63,8 @@ namespace Shadowsocks.Controller
public ShadowsocksController() public ShadowsocksController()
{ {
_config = Configuration.Load(); _config = Configuration.Load();
inboundCounter = _config.bandwidthIn;
outboundCounter = _config.bandwidthOut;
StatisticsConfiguration = StatisticsStrategyConfiguration.Load(); StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
_strategyManager = new StrategyManager(this); _strategyManager = new StrategyManager(this);
StartReleasingMemory(); StartReleasingMemory();
@@ -253,7 +255,7 @@ namespace Shadowsocks.Controller
public static string GetQRCode(Server server) public static string GetQRCode(Server server)
{ {
string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port; string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port;
string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
return "ss://" + base64; return "ss://" + base64;
} }
@@ -310,17 +312,21 @@ namespace Shadowsocks.Controller
public void UpdateInboundCounter(long n) public void UpdateInboundCounter(long n)
{ {
Interlocked.Add(ref inboundCounter, n); Interlocked.Add(ref inboundCounter, n);
_config.bandwidthIn = inboundCounter;
} }
public void UpdateOutboundCounter(long n) public void UpdateOutboundCounter(long n)
{ {
Interlocked.Add(ref outboundCounter, n); Interlocked.Add(ref outboundCounter, n);
_config.bandwidthOut = outboundCounter;
} }
protected void Reload() protected void Reload()
{ {
// some logic in configuration updated the config when saving, we need to read it again // some logic in configuration updated the config when saving, we need to read it again
_config = Configuration.Load(); _config = Configuration.Load();
inboundCounter = _config.bandwidthIn;
outboundCounter = _config.bandwidthOut;
StatisticsConfiguration = StatisticsStrategyConfiguration.Load(); StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
if (polipoRunner == null) if (polipoRunner == null)


+ 6
- 36
shadowsocks-csharp/Model/Configuration.cs View File

@@ -25,19 +25,17 @@ namespace Shadowsocks.Model
public bool availabilityStatistics; public bool availabilityStatistics;
public bool autoCheckUpdate; public bool autoCheckUpdate;
public LogViewerConfig logViewer; public LogViewerConfig logViewer;
public long bandwidthIn;
public long bandwidthOut;
private static string CONFIG_FILE = "gui-config.json"; private static string CONFIG_FILE = "gui-config.json";
public Server GetCurrentServer() public Server GetCurrentServer()
{ {
if (index >= 0 && index < configs.Count) if (index >= 0 && index < configs.Count)
{
return configs[index]; return configs[index];
}
else else
{
return GetDefaultServer(); return GetDefaultServer();
}
} }
public static void CheckServer(Server server) public static void CheckServer(Server server)
@@ -55,24 +53,15 @@ namespace Shadowsocks.Model
Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent); Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
config.isDefault = false; config.isDefault = false;
if (config.localPort == 0) if (config.localPort == 0)
{
config.localPort = 1080; config.localPort = 1080;
}
if (config.index == -1)
{
if (config.strategy == null)
{
config.index = 0;
}
}
if (config.index == -1 && config.strategy == null)
config.index = 0;
return config; return config;
} }
catch (Exception e) catch (Exception e)
{ {
if (!(e is FileNotFoundException)) if (!(e is FileNotFoundException))
{
Logging.LogUsefulException(e); Logging.LogUsefulException(e);
}
return new Configuration return new Configuration
{ {
index = 0, index = 0,
@@ -90,20 +79,11 @@ namespace Shadowsocks.Model
public static void Save(Configuration config) public static void Save(Configuration config)
{ {
if (config.index >= config.configs.Count) if (config.index >= config.configs.Count)
{
config.index = config.configs.Count - 1; config.index = config.configs.Count - 1;
}
if (config.index < -1) if (config.index < -1)
{
config.index = -1; config.index = -1;
}
if (config.index == -1)
{
if (config.strategy == null)
{
config.index = 0;
}
}
if (config.index == -1 && config.strategy == null)
config.index = 0;
config.isDefault = false; config.isDefault = false;
try try
{ {
@@ -128,42 +108,32 @@ namespace Shadowsocks.Model
private static void Assert(bool condition) private static void Assert(bool condition)
{ {
if (!condition) if (!condition)
{
throw new Exception(I18N.GetString("assertion failure")); throw new Exception(I18N.GetString("assertion failure"));
}
} }
public static void CheckPort(int port) public static void CheckPort(int port)
{ {
if (port <= 0 || port > 65535) if (port <= 0 || port > 65535)
{
throw new ArgumentException(I18N.GetString("Port out of range")); throw new ArgumentException(I18N.GetString("Port out of range"));
}
} }
public static void CheckLocalPort(int port) public static void CheckLocalPort(int port)
{ {
CheckPort(port); CheckPort(port);
if (port == 8123) if (port == 8123)
{
throw new ArgumentException(I18N.GetString("Port can't be 8123")); throw new ArgumentException(I18N.GetString("Port can't be 8123"));
}
} }
private static void CheckPassword(string password) private static void CheckPassword(string password)
{ {
if (string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(password))
{
throw new ArgumentException(I18N.GetString("Password can not be blank")); throw new ArgumentException(I18N.GetString("Password can not be blank"));
}
} }
private static void CheckServer(string server) private static void CheckServer(string server)
{ {
if (string.IsNullOrEmpty(server)) if (string.IsNullOrEmpty(server))
{
throw new ArgumentException(I18N.GetString("Server IP can not be blank")); throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
}
} }
} }
} }

+ 12
- 13
shadowsocks-csharp/Model/Server.cs View File

@@ -24,7 +24,7 @@ namespace Shadowsocks.Model
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
Server o2 = (Server)obj; Server o2 = (Server)obj;
return this.server == o2.server && this.server_port == o2.server_port;
return server == o2.server && server_port == o2.server_port;
} }
public string FriendlyName() public string FriendlyName()
@@ -45,12 +45,12 @@ namespace Shadowsocks.Model
public Server() public Server()
{ {
this.server = "";
this.server_port = 8388;
this.method = "aes-256-cfb";
this.password = "";
this.remarks = "";
this.auth = false;
server = "";
server_port = 8388;
method = "aes-256-cfb";
password = "";
remarks = "";
auth = false;
} }
public Server(string ssURL) : this() public Server(string ssURL) : this()
@@ -62,7 +62,7 @@ namespace Shadowsocks.Model
{ {
try try
{ {
bytes = System.Convert.FromBase64String(base64);
bytes = Convert.FromBase64String(base64);
} }
catch (FormatException) catch (FormatException)
{ {
@@ -80,16 +80,15 @@ namespace Shadowsocks.Model
string afterAt = data.Substring(indexLastAt + 1); string afterAt = data.Substring(indexLastAt + 1);
int indexLastColon = afterAt.LastIndexOf(':'); int indexLastColon = afterAt.LastIndexOf(':');
this.server_port = int.Parse(afterAt.Substring(indexLastColon + 1));
this.server = afterAt.Substring(0, indexLastColon);
server_port = int.Parse(afterAt.Substring(indexLastColon + 1));
server = afterAt.Substring(0, indexLastColon);
string beforeAt = data.Substring(0, indexLastAt); string beforeAt = data.Substring(0, indexLastAt);
string[] parts = beforeAt.Split(new[] { ':' }); string[] parts = beforeAt.Split(new[] { ':' });
this.method = parts[0];
this.password = parts[1];
method = parts[0];
password = parts[1];
//TODO: read one_time_auth //TODO: read one_time_auth
} }
catch (IndexOutOfRangeException) catch (IndexOutOfRangeException)
{ {


Loading…
Cancel
Save