Skip to content

Commit

Permalink
Use ushort for ports, instead of int.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirin Geyer authored and HMBSbige committed Nov 7, 2018
1 parent 8718d35 commit 7c39e1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 5 additions & 5 deletions shadowsocks-csharp/Model/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public class Server
{
public string id;
public string server;
public int server_port;
public int server_udp_port;
public ushort server_port;
public ushort server_udp_port;
public string password;
public string method;
public string protocol;
Expand Down Expand Up @@ -382,7 +382,7 @@ public void ServerFromSSR(string ssrURL, string force_group)
throw new FormatException();

server = match.Groups[1].Value;
server_port = int.Parse(match.Groups[2].Value);
server_port = ushort.Parse(match.Groups[2].Value);
protocol = match.Groups[3].Value.Length == 0 ? "origin" : match.Groups[3].Value;
protocol = protocol.Replace("_compatible", "");
method = match.Groups[4].Value;
Expand Down Expand Up @@ -414,7 +414,7 @@ public void ServerFromSSR(string ssrURL, string force_group)
}
if (params_dict.ContainsKey("udpport"))
{
server_udp_port = int.Parse(params_dict["udpport"]);
server_udp_port = ushort.Parse(params_dict["udpport"]);
}
if (!String.IsNullOrEmpty(force_group))
group = force_group;
Expand All @@ -437,7 +437,7 @@ public void ServerFromSS(string ssURL, string force_group)
method = match.Groups["method"].Value;
password = match.Groups["password"].Value;
server = match.Groups["hostname"].Value;
server_port = int.Parse(match.Groups["port"].Value);
server_port = ushort.Parse(match.Groups["port"].Value);
if (!String.IsNullOrEmpty(force_group))
group = force_group;
else
Expand Down
4 changes: 2 additions & 2 deletions shadowsocks-csharp/View/ConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ private int SaveOldSelectedServer()
Server server = new Server
{
server = IPTextBox.Text.Trim(),
server_port = Convert.ToInt32(NumServerPort.Value),
server_udp_port = Convert.ToInt32(NumUDPPort.Value),
server_port = Convert.ToUInt16(NumServerPort.Value),
server_udp_port = Convert.ToUInt16(NumUDPPort.Value),
password = PasswordTextBox.Text,
method = EncryptionSelect.Text,
protocol = TCPProtocolComboBox.Text,
Expand Down
19 changes: 17 additions & 2 deletions test/ServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TestServerFromSSR()
server.ServerFromSSR(nornameCase, "");

Assert.AreEqual<string>(server.server, "127.0.0.1");
Assert.AreEqual<int>(server.server_port, 1234);
Assert.AreEqual<ushort>(server.server_port, 1234);
Assert.AreEqual<string>(server.protocol, "auth_aes128_md5");
Assert.AreEqual<string>(server.method, "aes-128-cfb");
Assert.AreEqual<string>(server.obfs, "tls1.2_ticket_auth");
Expand All @@ -31,7 +31,7 @@ public void TestServerFromSSR()
server.ServerFromSSR(normalCaseWithRemark, "firewallAirport");

Assert.AreEqual<string>(server.server, "127.0.0.1");
Assert.AreEqual<int>(server.server_port, 1234);
Assert.AreEqual<ushort>(server.server_port, 1234);
Assert.AreEqual<string>(server.protocol, "auth_aes128_md5");
Assert.AreEqual<string>(server.method, "aes-128-cfb");
Assert.AreEqual<string>(server.obfs, "tls1.2_ticket_auth");
Expand All @@ -57,5 +57,20 @@ public void TestHideServerName()
Assert.AreEqual(addrs[key], val);
}
}

[TestMethod]
public void TestBadPortNumber()
{
Server server = new Server();

string link = "ssr://MTI3LjAuMC4xOjgwOmF1dGhfc2hhMV92NDpjaGFjaGEyMDpodHRwX3NpbXBsZTplaWZnYmVpd3ViZ3IvP29iZnNwYXJhbT0mcHJvdG9wYXJhbT0mcmVtYXJrcz0mZ3JvdXA9JnVkcHBvcnQ9NDY0MzgxMzYmdW90PTQ2MDA3MTI4";
try {
server.ServerFromSSR(link, "");
} catch (System.OverflowException e)
{
Console.Write(e.ToString());
}

}
}
}

0 comments on commit 7c39e1a

Please sign in to comment.