-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
245 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
namespace STUN | ||
{ | ||
public class StunServer | ||
{ | ||
public string Hostname { get; } | ||
public ushort Port { get; } | ||
|
||
private const ushort DefaultPort = 3478; | ||
|
||
public StunServer() | ||
{ | ||
Hostname = @"stun.syncthing.net"; | ||
Port = DefaultPort; | ||
} | ||
|
||
private StunServer(string host, ushort port) | ||
{ | ||
Hostname = host; | ||
Port = port; | ||
} | ||
|
||
public static bool TryParse(string str, [NotNullWhen(true)] out StunServer? server) | ||
{ | ||
server = null; | ||
if (string.IsNullOrEmpty(str)) | ||
{ | ||
return false; | ||
} | ||
|
||
var hostLength = str.Length; | ||
var pos = str.LastIndexOf(':'); | ||
|
||
if (pos > 0) | ||
{ | ||
if (str[pos - 1] is ']') | ||
{ | ||
hostLength = pos; | ||
} | ||
else if (str.AsSpan(0, pos).LastIndexOf(':') is -1) | ||
{ | ||
hostLength = pos; | ||
} | ||
} | ||
|
||
var host = str[..hostLength]; | ||
var type = Uri.CheckHostName(host); | ||
switch (type) | ||
{ | ||
case UriHostNameType.Dns: | ||
case UriHostNameType.IPv4: | ||
case UriHostNameType.IPv6: | ||
{ | ||
break; | ||
} | ||
default: | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
if (hostLength == str.Length) | ||
{ | ||
server = new StunServer(host, DefaultPort); | ||
return true; | ||
} | ||
|
||
if (ushort.TryParse(str.AsSpan(hostLength + 1), out var port)) | ||
{ | ||
server = new StunServer(host, port); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
if (Port is DefaultPort) | ||
{ | ||
return Hostname; | ||
} | ||
if (IPAddress.TryParse(Hostname, out var ip) && ip.AddressFamily is AddressFamily.InterNetworkV6) | ||
{ | ||
return $@"[{ip}]:{Port}"; | ||
} | ||
return $@"{Hostname}:{Port}"; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using STUN; | ||
|
||
namespace UnitTest | ||
{ | ||
[TestClass] | ||
public class StunServerTest | ||
{ | ||
[TestMethod] | ||
[DataRow(@"www.google.com", ushort.MinValue)] | ||
[DataRow(@"1.1.1.1", (ushort)1)] | ||
[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]", (ushort)1919)] | ||
public void IsTrue(string host, ushort port) | ||
{ | ||
var str = $@"{host}:{port}"; | ||
Assert.IsTrue(StunServer.TryParse(str, out var server)); | ||
Assert.IsNotNull(server); | ||
Assert.AreEqual(host, server.Hostname); | ||
Assert.AreEqual(port, server.Port); | ||
Assert.AreEqual(str, server.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(@"")] | ||
[DataRow(@"www.google.com:114514")] | ||
[DataRow(@"/dw.[/[:114")] | ||
[DataRow(@"2001:db8:1234:5678:11:2233:4455:6677:65535")] | ||
public void IsFalse(string str) | ||
{ | ||
Assert.IsFalse(StunServer.TryParse(str, out var server)); | ||
Assert.IsNull(server); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(@"www.google.com")] | ||
[DataRow(@"1.1.1.1")] | ||
[DataRow(@"2001:db8:1234:5678:11:2233:4455:6677")] | ||
[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]")] | ||
[DataRow(@"2001:db8:1234:5678:11:2233:4455:db8")] | ||
public void TestDefaultPort(string str) | ||
{ | ||
Assert.IsTrue(StunServer.TryParse(str, out var server)); | ||
Assert.IsNotNull(server); | ||
Assert.AreEqual(str, server.Hostname); | ||
Assert.AreEqual(3478, server.Port); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(@"stun.syncthing.net:114", @"stun.syncthing.net:114")] | ||
[DataRow(@"stun.syncthing.net:3478", @"stun.syncthing.net")] | ||
[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]", @"[2001:db8:1234:5678:11:2233:4455:6677]")] | ||
[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]:3478", @"[2001:db8:1234:5678:11:2233:4455:6677]")] | ||
[DataRow(@"1.1.1.1:3478", @"1.1.1.1")] | ||
[DataRow(@"1.1.1.1:1919", @"1.1.1.1:1919")] | ||
public void ToString(string str, string expected) | ||
{ | ||
Assert.IsTrue(StunServer.TryParse(str, out var server)); | ||
Assert.IsNotNull(server); | ||
Assert.AreEqual(expected, server.ToString()); | ||
} | ||
|
||
[TestMethod] | ||
public void DefaultServer() | ||
{ | ||
var server = new StunServer(); | ||
Assert.AreEqual(@"stun.syncthing.net", server.Hostname); | ||
Assert.AreEqual(3478, server.Port); | ||
} | ||
} | ||
} |
Oops, something went wrong.