Skip to content

Commit

Permalink
refactor: DNS part
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 20, 2021
1 parent f61c688 commit 30de30e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 59 deletions.
57 changes: 24 additions & 33 deletions NatTypeTester-Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,32 @@
using STUN.Client;
using System;
using System.Net;
using System.Threading.Tasks;

namespace NatTypeTester
//stun.qq.com:3478 0.0.0.0:0
var server = @"stun.syncthing.net";
ushort port = 3478;
var local = new IPEndPoint(IPAddress.Any, 0);

if (args.Length > 0 && StunServer.TryParse(args[0], out var stun))
{
internal static class Program
{
/// <summary>
/// stun.qq.com:3478 0.0.0.0:0
/// </summary>
private static async Task Main(string[] args)
{
var server = @"stun.syncthing.net";
ushort port = 3478;
var local = new IPEndPoint(IPAddress.Any, 0);
server = stun.Hostname;
port = stun.Port;
}

if (args.Length > 0 && StunServer.TryParse(args[0], out var stun))
{
server = stun.Hostname;
port = stun.Port;
}
if (args.Length > 1)
{
IPEndPoint.TryParse(args[2], out local);
}
if (args.Length > 1)
{
IPEndPoint.TryParse(args[2], out local);
}

using var client = new StunClient5389UDP(new DefaultDnsClient(), server, port, local);
await client.QueryAsync();
var res = client.Status;
var dnsClient = new DefaultDnsClient();
var ip = await dnsClient.QueryAsync(server);
using var client = new StunClient5389UDP(ip, port, local);
await client.QueryAsync();
var res = client.Status;

Console.WriteLine($@"Other address is {res.OtherEndPoint}");
Console.WriteLine($@"Binding test: {res.BindingTestResult}");
Console.WriteLine($@"Local address: {res.LocalEndPoint}");
Console.WriteLine($@"Mapped address: {res.PublicEndPoint}");
Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}");
Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}");
}
}
}
Console.WriteLine($@"Other address is {res.OtherEndPoint}");
Console.WriteLine($@"Binding test: {res.BindingTestResult}");
Console.WriteLine($@"Local address: {res.LocalEndPoint}");
Console.WriteLine($@"Mapped address: {res.PublicEndPoint}");
Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}");
Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}");
1 change: 1 addition & 0 deletions NatTypeTester.ViewModels/NatTypeTester.ViewModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Import Project="..\common.props" />

<ItemGroup>
<PackageReference Include="Dns.Net.Abstractions" Version="0.1.0" />
<PackageReference Include="ReactiveUI" Version="15.1.1" />
<PackageReference Include="Volo.Abp.Core" Version="4.4.0" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion NatTypeTester.ViewModels/RFC3489ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private async Task TestClassicNatTypeImpl(CancellationToken token)
Config.ProxyUser, Config.ProxyPassword
);

using var client = new StunClient3489(DnsClient, server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);
var ip = await DnsClient.QueryAsync(server.Hostname, token);
using var client = new StunClient3489(ip, server.Port, Result3489.LocalEndPoint, proxy);

Result3489 = client.Status;
using (Observable.Interval(TimeSpan.FromSeconds(0.1))
Expand Down
3 changes: 2 additions & 1 deletion NatTypeTester.ViewModels/RFC5780ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private async Task DiscoveryNatTypeImpl(CancellationToken token)
Config.ProxyUser, Config.ProxyPassword
);

using var client = new StunClient5389UDP(DnsClient, server.Hostname, server.Port, Result5389.LocalEndPoint, proxy);
var ip = await DnsClient.QueryAsync(server.Hostname, token);
using var client = new StunClient5389UDP(ip, server.Port, Result5389.LocalEndPoint, proxy);

Result5389 = client.Status;
using (Observable.Interval(TimeSpan.FromSeconds(0.1))
Expand Down
21 changes: 6 additions & 15 deletions STUN/Client/StunClient3489.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dns.Net.Abstractions;
using Microsoft;
using STUN.Enums;
using STUN.Messages;
using STUN.Proxy;
Expand Down Expand Up @@ -36,23 +36,14 @@ public TimeSpan Timeout

public ClassicStunResult Status { get; } = new();

public StunClient3489(IDnsClient dnsQuery, string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
public StunClient3489(IPAddress server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
{
Proxy = proxy ?? new NoneUdpProxy(local);

if (string.IsNullOrEmpty(server))
{
throw new ArgumentException(@"Please specify STUN server !");
}
Requires.NotNull(server, nameof(server));
Requires.Argument(port > 0, nameof(port), @"Port value must be >= 1 !");

if (port < 1)
{
throw new ArgumentException(@"Port value must be >= 1 !");
}

var ip = dnsQuery.Query(server);
Proxy = proxy ?? new NoneUdpProxy(local);

Server = ip;
Server = server;
Port = port;

Timeout = TimeSpan.FromSeconds(1.6);
Expand Down
5 changes: 2 additions & 3 deletions STUN/Client/StunClient5389UDP.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Dns.Net.Abstractions;
using STUN.Enums;
using STUN.Messages;
using STUN.Proxy;
Expand All @@ -19,8 +18,8 @@ public class StunClient5389UDP : StunClient3489
{
public new StunResult5389 Status { get; } = new();

public StunClient5389UDP(IDnsClient dnsQuery, string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
: base(dnsQuery, server, port, local, proxy)
public StunClient5389UDP(IPAddress server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
: base(server, port, local, proxy)
{
Timeout = TimeSpan.FromSeconds(3);
Status.LocalEndPoint = local;
Expand Down
1 change: 0 additions & 1 deletion STUN/STUN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dns.Net.Abstractions" Version="0.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="16.10.34" />
</ItemGroup>

Expand Down
18 changes: 13 additions & 5 deletions UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Dns.Net.Abstractions;
using Dns.Net.Clients;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using STUN.Client;
Expand All @@ -12,10 +13,13 @@ namespace UnitTest
[TestClass]
public class UnitTest
{
private readonly IDnsClient dnsClient = new DefaultDnsClient();

[TestMethod]
public async Task BindingTest()
{
using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
var server = await dnsClient.QueryAsync(@"stun.syncthing.net");
using var client = new StunClient5389UDP(server, 3478, new IPEndPoint(IPAddress.Any, 0));
await client.BindingTestAsync();
var result = client.Status;

Expand All @@ -31,7 +35,8 @@ public async Task BindingTest()
[TestMethod]
public async Task MappingBehaviorTest()
{
using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
var server = await dnsClient.QueryAsync(@"stun.syncthing.net");
using var client = new StunClient5389UDP(server, 3478, new IPEndPoint(IPAddress.Any, 0));
await client.MappingBehaviorTestAsync();
var result = client.Status;

Expand All @@ -52,7 +57,8 @@ MappingBehavior.AddressDependent or
[TestMethod]
public async Task FilteringBehaviorTest()
{
using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
var server = await dnsClient.QueryAsync(@"stun.syncthing.net");
using var client = new StunClient5389UDP(server, 3478, new IPEndPoint(IPAddress.Any, 0));
await client.FilteringBehaviorTestAsync();
var result = client.Status;

Expand All @@ -72,7 +78,8 @@ FilteringBehavior.AddressDependent or
[TestMethod]
public async Task CombiningTest()
{
using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
var server = await dnsClient.QueryAsync(@"stun.syncthing.net");
using var client = new StunClient5389UDP(server, 3478, new IPEndPoint(IPAddress.Any, 0));
await client.QueryAsync();
var result = client.Status;

Expand All @@ -98,7 +105,8 @@ FilteringBehavior.AddressDependent or
public async Task ProxyTest()
{
using var proxy = ProxyFactory.CreateProxy(ProxyType.Socks5, IPEndPoint.Parse(@"0.0.0.0:0"), IPEndPoint.Parse(@"127.0.0.1:10000"));
using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0), proxy);
var server = await dnsClient.QueryAsync(@"stun.syncthing.net");
using var client = new StunClient5389UDP(server, 3478, new IPEndPoint(IPAddress.Any, 0));
await client.QueryAsync();
var result = client.Status;

Expand Down

0 comments on commit 30de30e

Please sign in to comment.