浏览代码

Refactor: Change dns client [WIP]

Bruce Wayne 4 年之前
父节点
当前提交
763d668084

+ 4 - 0
NatTypeTester-Console/NatTypeTester_Console.csproj

@@ -8,6 +8,10 @@
     <AssemblyName>NatTypeTester.Console</AssemblyName>
   </PropertyGroup>
 
+  <ItemGroup>
+    <PackageReference Include="Dns.Net" Version="0.1.0" />
+  </ItemGroup>
+
   <ItemGroup>
     <ProjectReference Include="..\STUN\STUN.csproj" />
   </ItemGroup>

+ 2 - 1
NatTypeTester-Console/Program.cs

@@ -1,3 +1,4 @@
+using Dns.Net.Clients;
 using STUN.Client;
 using STUN.Utils;
 using System;
@@ -29,7 +30,7 @@ namespace NatTypeTester
 				local = NetUtils.ParseEndpoint(args[2]);
 			}
 
-			using var client = new StunClient5389UDP(server, port, local);
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), server, port, local);
 			await client.QueryAsync();
 			var res = client.Status;
 

+ 4 - 1
NatTypeTester.ViewModels/RFC3489ViewModel.cs

@@ -1,3 +1,4 @@
+using Dns.Net.Abstractions;
 using JetBrains.Annotations;
 using NatTypeTester.Models;
 using ReactiveUI;
@@ -23,6 +24,8 @@ namespace NatTypeTester.ViewModels
 
 		private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
+		private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
+
 		[Reactive]
 		public ClassicStunResult Result3489 { get; set; }
 
@@ -49,7 +52,7 @@ namespace NatTypeTester.ViewModels
 					Config.ProxyUser, Config.ProxyPassword
 			);
 
-			using var client = new StunClient3489(server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);
+			using var client = new StunClient3489(DnsClient, server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);
 
 			Result3489 = client.Status;
 			using (Observable.Interval(TimeSpan.FromSeconds(0.1))

+ 4 - 1
NatTypeTester.ViewModels/RFC5780ViewModel.cs

@@ -1,3 +1,4 @@
+using Dns.Net.Abstractions;
 using JetBrains.Annotations;
 using NatTypeTester.Models;
 using ReactiveUI;
@@ -23,6 +24,8 @@ namespace NatTypeTester.ViewModels
 
 		private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
+		private IDnsClient DnsClient => LazyServiceProvider.LazyGetRequiredService<IDnsClient>();
+
 		[Reactive]
 		public StunResult5389 Result5389 { get; set; }
 
@@ -49,7 +52,7 @@ namespace NatTypeTester.ViewModels
 					Config.ProxyUser, Config.ProxyPassword
 			);
 
-			using var client = new StunClient5389UDP(server.Hostname, server.Port, Result5389.LocalEndPoint, proxy);
+			using var client = new StunClient5389UDP(DnsClient, server.Hostname, server.Port, Result5389.LocalEndPoint, proxy);
 
 			Result5389 = client.Status;
 			using (Observable.Interval(TimeSpan.FromSeconds(0.1))

+ 1 - 0
NatTypeTester/NatTypeTester.csproj

@@ -11,6 +11,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Dns.Net" Version="0.1.0" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
     <PackageReference Include="ModernWpfUI" Version="0.9.4" />
     <PackageReference Include="ReactiveUI.Events.WPF" Version="14.3.10" />

+ 3 - 0
NatTypeTester/NatTypeTesterModule.cs

@@ -1,3 +1,5 @@
+using Dns.Net.Abstractions;
+using Dns.Net.Clients;
 using JetBrains.Annotations;
 using Microsoft.Extensions.DependencyInjection.Extensions;
 using NatTypeTester.Models;
@@ -28,6 +30,7 @@ namespace NatTypeTester
 		public override void ConfigureServices(ServiceConfigurationContext context)
 		{
 			context.Services.TryAddTransient<RoutingState>();
+			context.Services.TryAddTransient<IDnsClient, DefaultDnsClient>();
 		}
 	}
 }

+ 3 - 5
STUN/Client/StunClient3489.cs

@@ -1,4 +1,4 @@
-using STUN.DnsClients;
+using Dns.Net.Abstractions;
 using STUN.Enums;
 using STUN.Message;
 using STUN.Proxy;
@@ -36,7 +36,7 @@ namespace STUN.Client
 
 		public ClassicStunResult Status { get; } = new();
 
-		public StunClient3489(string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null, IDnsQuery? dnsQuery = null)
+		public StunClient3489(IDnsClient dnsQuery, string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
 		{
 			Proxy = proxy ?? new NoneUdpProxy(local);
 
@@ -50,11 +50,9 @@ namespace STUN.Client
 				throw new ArgumentException(@"Port value must be >= 1 !");
 			}
 
-			dnsQuery ??= new DefaultDnsQuery();
-
 			var ip = dnsQuery.Query(server);
 
-			Server = ip ?? throw new ArgumentException(@"Wrong STUN server !");
+			Server = ip;
 			Port = port;
 
 			Timeout = TimeSpan.FromSeconds(1.6);

+ 3 - 3
STUN/Client/StunClient5389UDP.cs

@@ -1,4 +1,4 @@
-using STUN.DnsClients;
+using Dns.Net.Abstractions;
 using STUN.Enums;
 using STUN.Message;
 using STUN.Proxy;
@@ -19,8 +19,8 @@ namespace STUN.Client
 	{
 		public new StunResult5389 Status { get; } = new();
 
-		public StunClient5389UDP(string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null, IDnsQuery? dnsQuery = null)
-		: base(server, port, local, proxy, dnsQuery)
+		public StunClient5389UDP(IDnsClient dnsQuery, string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null)
+		: base(dnsQuery, server, port, local, proxy)
 		{
 			Timeout = TimeSpan.FromSeconds(3);
 			Status.LocalEndPoint = local;

+ 0 - 64
STUN/DnsClients/DefaultDnsQuery.cs

@@ -1,64 +0,0 @@
-using System.Linq;
-using System.Net;
-using System.Threading.Tasks;
-
-namespace STUN.DnsClients
-{
-	public class DefaultDnsQuery : IDnsQuery
-	{
-		public async Task<IPAddress?> QueryAsync(string? host)
-		{
-			try
-			{
-				if (host is null)
-				{
-					return null;
-				}
-
-				var ip = IsIPAddress(host);
-				if (ip is not null)
-				{
-					return ip;
-				}
-				var res = await Dns.GetHostAddressesAsync(host);
-				return res.FirstOrDefault();
-			}
-			catch
-			{
-				return null;
-			}
-		}
-
-		public IPAddress? Query(string? host)
-		{
-			try
-			{
-				if (host is null)
-				{
-					return null;
-				}
-
-				var ip = IsIPAddress(host);
-				if (ip is not null)
-				{
-					return ip;
-				}
-				var res = Dns.GetHostAddresses(host);
-				return res.FirstOrDefault();
-			}
-			catch
-			{
-				return null;
-			}
-		}
-
-		private static IPAddress? IsIPAddress(string? host)
-		{
-			if (host is not null && IPAddress.TryParse(host, out var ip))
-			{
-				return ip;
-			}
-			return null;
-		}
-	}
-}

+ 0 - 11
STUN/DnsClients/IDnsQuery.cs

@@ -1,11 +0,0 @@
-using System.Net;
-using System.Threading.Tasks;
-
-namespace STUN.DnsClients
-{
-	public interface IDnsQuery
-	{
-		public Task<IPAddress?> QueryAsync(string? host);
-		public IPAddress? Query(string? host);
-	}
-}

+ 4 - 0
STUN/STUN.csproj

@@ -16,4 +16,8 @@
     <PackageId>Stun.Net</PackageId>
   </PropertyGroup>
 
+  <ItemGroup>
+    <PackageReference Include="Dns.Net.Abstractions" Version="0.1.0" />
+  </ItemGroup>
+
 </Project>

+ 6 - 5
UnitTest/UnitTest.cs

@@ -1,3 +1,4 @@
+using Dns.Net.Clients;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using STUN.Client;
 using STUN.Enums;
@@ -92,7 +93,7 @@ namespace UnitTest
 		[TestMethod]
 		public async Task BindingTest()
 		{
-			using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
 			await client.BindingTestAsync();
 			var result = client.Status;
 
@@ -108,7 +109,7 @@ namespace UnitTest
 		[TestMethod]
 		public async Task MappingBehaviorTest()
 		{
-			using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
 			await client.MappingBehaviorTestAsync();
 			var result = client.Status;
 
@@ -129,7 +130,7 @@ namespace UnitTest
 		[TestMethod]
 		public async Task FilteringBehaviorTest()
 		{
-			using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
 			await client.FilteringBehaviorTestAsync();
 			var result = client.Status;
 
@@ -149,7 +150,7 @@ namespace UnitTest
 		[TestMethod]
 		public async Task CombiningTest()
 		{
-			using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0));
 			await client.QueryAsync();
 			var result = client.Status;
 
@@ -175,7 +176,7 @@ namespace UnitTest
 		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(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0), proxy);
+			using var client = new StunClient5389UDP(new DefaultDnsClient(), @"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0), proxy);
 			await client.QueryAsync();
 			var result = client.Status;
 

+ 1 - 0
UnitTest/UnitTest.csproj

@@ -7,6 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="Dns.Net" Version="0.1.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
     <PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
     <PackageReference Include="MSTest.TestFramework" Version="2.2.5" />