Skip to content

Commit

Permalink
refactor: Remake StunServer
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 20, 2021
1 parent 235fcc0 commit 49fdbd9
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 174 deletions.
5 changes: 2 additions & 3 deletions NatTypeTester.ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using DynamicData.Binding;
using NatTypeTester.Models;
using ReactiveUI;
using STUN.Utils;
using STUN;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -62,10 +62,9 @@ public void LoadStunServer()
return;
}

var stun = new StunServer();
foreach (var line in File.ReadLines(path))
{
if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
if (!string.IsNullOrWhiteSpace(line) && StunServer.TryParse(line, out var stun))
{
List.Add(stun.ToString());
}
Expand Down
9 changes: 3 additions & 6 deletions NatTypeTester.ViewModels/RFC3489ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Dns.Net.Abstractions;
using JetBrains.Annotations;
using Microsoft;
using NatTypeTester.Models;
using ReactiveUI;
using STUN;
using STUN.Client;
using STUN.Proxy;
using STUN.StunResult;
using STUN.Utils;
using System;
using System.Net;
using System.Reactive;
Expand Down Expand Up @@ -37,11 +38,7 @@ public RFC3489ViewModel()

private async Task TestClassicNatTypeImpl(CancellationToken token)
{
var server = new StunServer();
if (!server.Parse(Config.StunServer))
{
throw new Exception(@"Wrong STUN Server!");
}
Verify.Operation(StunServer.TryParse(Config.StunServer, out var server), @"Wrong STUN Server!");

using var proxy = ProxyFactory.CreateProxy(
Config.ProxyType,
Expand Down
9 changes: 3 additions & 6 deletions NatTypeTester.ViewModels/RFC5780ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Dns.Net.Abstractions;
using JetBrains.Annotations;
using Microsoft;
using NatTypeTester.Models;
using ReactiveUI;
using STUN;
using STUN.Client;
using STUN.Proxy;
using STUN.StunResult;
using STUN.Utils;
using System;
using System.Net;
using System.Reactive;
Expand Down Expand Up @@ -37,11 +38,7 @@ public RFC5780ViewModel()

private async Task DiscoveryNatTypeImpl(CancellationToken token)
{
var server = new StunServer();
if (!server.Parse(Config.StunServer))
{
throw new Exception(@"Wrong STUN Server!");
}
Verify.Operation(StunServer.TryParse(Config.StunServer, out var server), @"Wrong STUN Server!");

using var proxy = ProxyFactory.CreateProxy(
Config.ProxyType,
Expand Down
94 changes: 94 additions & 0 deletions STUN/StunServer.cs
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}";
}
}
}
97 changes: 0 additions & 97 deletions STUN/Utils/StunServer.cs

This file was deleted.

70 changes: 70 additions & 0 deletions UnitTest/StunServerTest.cs
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);
}
}
}
Loading

0 comments on commit 49fdbd9

Please sign in to comment.