diff --git a/shadowsocks-csharp/Model/Server.cs b/shadowsocks-csharp/Model/Server.cs index b4486db9..00209d11 100755 --- a/shadowsocks-csharp/Model/Server.cs +++ b/shadowsocks-csharp/Model/Server.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.IO; @@ -50,7 +50,8 @@ namespace Shadowsocks.Model string[] r1 = Regex.Split(ssURL, "ss://", RegexOptions.IgnoreCase); string base64 = r1[1].ToString(); byte[] bytes = null; - for (var i = 0; i < 3; i++) { + for (var i = 0; i < 3; i++) + { try { bytes = System.Convert.FromBase64String(base64); @@ -64,11 +65,25 @@ namespace Shadowsocks.Model { throw new FormatException(); } - string[] parts = Encoding.UTF8.GetString(bytes).Split(new char[2] { ':', '@' }); - this.method = parts[0].ToString(); - this.password = parts[1].ToString(); - this.server = parts[2].ToString(); - this.server_port = int.Parse(parts[3].ToString()); + try + { + string data = Encoding.UTF8.GetString(bytes); + int indexLastAt = data.LastIndexOf('@'); + + string afterAt = data.Substring(indexLastAt + 1); + int indexLastColon = afterAt.LastIndexOf(':'); + this.server_port = int.Parse(afterAt.Substring(indexLastColon + 1)); + this.server = afterAt.Substring(0, indexLastColon); + + string beforeAt = data.Substring(0, indexLastAt); + string[] parts = beforeAt.Split(new[] { ':' }); + this.method = parts[0]; + this.password = parts[1]; + } + catch (IndexOutOfRangeException) + { + throw new FormatException(); + } } } }