소스 검색

Refactor: Remake IPEndPoint

Bruce Wayne 4 년 전
부모
커밋
13f6f9b4cc

+ 1 - 2
NatTypeTester-Console/Program.cs

@@ -1,6 +1,5 @@
 using Dns.Net.Clients;
 using STUN.Client;
-using STUN.Utils;
 using System;
 using System.Net;
 using System.Threading.Tasks;
@@ -27,7 +26,7 @@ namespace NatTypeTester
 			}
 			if (args.Length > 2)
 			{
-				local = NetUtils.ParseEndpoint(args[2]);
+				local = IPEndPoint.Parse(args[2]);
 			}
 
 			using var client = new StunClient5389UDP(new DefaultDnsClient(), server, port, local);

+ 1 - 1
NatTypeTester.ViewModels/RFC3489ViewModel.cs

@@ -48,7 +48,7 @@ namespace NatTypeTester.ViewModels
 			using var proxy = ProxyFactory.CreateProxy(
 					Config.ProxyType,
 					Result3489.LocalEndPoint,
-					NetUtils.ParseEndpoint(Config.ProxyServer),
+					IPEndPoint.Parse(Config.ProxyServer),
 					Config.ProxyUser, Config.ProxyPassword
 			);
 

+ 1 - 1
NatTypeTester.ViewModels/RFC5780ViewModel.cs

@@ -48,7 +48,7 @@ namespace NatTypeTester.ViewModels
 			using var proxy = ProxyFactory.CreateProxy(
 					Config.ProxyType,
 					Result5389.LocalEndPoint,
-					NetUtils.ParseEndpoint(Config.ProxyServer),
+					IPEndPoint.Parse(Config.ProxyServer),
 					Config.ProxyUser, Config.ProxyPassword
 			);
 

+ 54 - 0
NatTypeTester.ViewModels/ValueConverters/StringToIPEndpointTypeConverter.cs

@@ -0,0 +1,54 @@
+using JetBrains.Annotations;
+using ReactiveUI;
+using System;
+using System.Net;
+using Volo.Abp.DependencyInjection;
+
+namespace NatTypeTester.ViewModels.ValueConverters
+{
+	[ExposeServices(typeof(IBindingTypeConverter))]
+	[UsedImplicitly]
+	public class StringToIPEndpointTypeConverter : IBindingTypeConverter, ISingletonDependency
+	{
+		public int GetAffinityForObjects(Type fromType, Type toType)
+		{
+			if (fromType == typeof(string) && toType == typeof(IPEndPoint))
+			{
+				return 11;
+			}
+
+			if (fromType == typeof(IPEndPoint) && toType == typeof(string))
+			{
+				return 11;
+			}
+
+			return 0;
+		}
+
+		public bool TryConvert(object? from, Type toType, object? conversionHint, out object? result)
+		{
+			if (toType == typeof(IPEndPoint) && from is string str)
+			{
+				if (IPEndPoint.TryParse(str, out var ipe))
+				{
+					result = ipe;
+					return true;
+				}
+
+				result = null;
+				return false;
+			}
+
+			if (from is IPEndPoint fromIPEndPoint)
+			{
+				result = fromIPEndPoint.ToString();
+			}
+			else
+			{
+				result = string.Empty;
+			}
+
+			return true;
+		}
+	}
+}

+ 3 - 20
NatTypeTester/Views/RFC3489View.xaml.cs

@@ -2,7 +2,6 @@ using JetBrains.Annotations;
 using NatTypeTester.Utils;
 using NatTypeTester.ViewModels;
 using ReactiveUI;
-using STUN.Utils;
 using System;
 using System.Reactive.Disposables;
 using System.Reactive.Linq;
@@ -23,27 +22,11 @@ namespace NatTypeTester.Views
 
 			this.WhenActivated(d =>
 			{
-				this.OneWayBind(ViewModel,
-								vm => vm.Result3489.NatType,
-								v => v.NatTypeTextBox.Text,
-								type => type.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result3489.NatType, v => v.NatTypeTextBox.Text).DisposeWith(d);
 
-				this.Bind(ViewModel,
-								vm => vm.Result3489.LocalEndPoint,
-								v => v.LocalEndTextBox.Text,
-								ipe => ipe is null ? string.Empty : ipe.ToString(),
-								NetUtils.ParseEndpoint
-						)
-						.DisposeWith(d);
+				this.Bind(ViewModel, vm => vm.Result3489.LocalEndPoint, v => v.LocalEndTextBox.Text).DisposeWith(d);
 
-				this.OneWayBind(ViewModel,
-								vm => vm.Result3489.PublicEndPoint,
-								v => v.PublicEndTextBox.Text,
-								ipe => ipe is null ? string.Empty : ipe.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result3489.PublicEndPoint, v => v.PublicEndTextBox.Text).DisposeWith(d);
 
 				this.BindCommand(ViewModel, vm => vm.TestClassicNatType, v => v.TestButton).DisposeWith(d);
 

+ 5 - 32
NatTypeTester/Views/RFC5780View.xaml.cs

@@ -2,7 +2,6 @@ using JetBrains.Annotations;
 using NatTypeTester.Utils;
 using NatTypeTester.ViewModels;
 using ReactiveUI;
-using STUN.Utils;
 using System;
 using System.Reactive.Disposables;
 using System.Reactive.Linq;
@@ -23,41 +22,15 @@ namespace NatTypeTester.Views
 
 			this.WhenActivated(d =>
 			{
-				this.OneWayBind(ViewModel,
-								vm => vm.Result5389.BindingTestResult,
-								v => v.BindingTestTextBox.Text,
-								res => res.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result5389.BindingTestResult, v => v.BindingTestTextBox.Text).DisposeWith(d);
 
-				this.OneWayBind(ViewModel,
-								vm => vm.Result5389.MappingBehavior,
-								v => v.MappingBehaviorTextBox.Text,
-								res => res.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result5389.MappingBehavior, v => v.MappingBehaviorTextBox.Text).DisposeWith(d);
 
-				this.OneWayBind(ViewModel,
-								vm => vm.Result5389.FilteringBehavior,
-								v => v.FilteringBehaviorTextBox.Text,
-								res => res.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result5389.FilteringBehavior, v => v.FilteringBehaviorTextBox.Text).DisposeWith(d);
 
-				this.Bind(ViewModel,
-								vm => vm.Result5389.LocalEndPoint,
-								v => v.LocalAddressTextBox.Text,
-								ipe => ipe is null ? string.Empty : ipe.ToString(),
-								NetUtils.ParseEndpoint
-						)
-						.DisposeWith(d);
+				this.Bind(ViewModel, vm => vm.Result5389.LocalEndPoint, v => v.LocalAddressTextBox.Text).DisposeWith(d);
 
-				this.OneWayBind(ViewModel,
-								vm => vm.Result5389.PublicEndPoint,
-								v => v.MappingAddressTextBox.Text,
-								ipe => ipe is null ? string.Empty : ipe.ToString()
-						)
-						.DisposeWith(d);
+				this.OneWayBind(ViewModel, vm => vm.Result5389.PublicEndPoint, v => v.MappingAddressTextBox.Text).DisposeWith(d);
 
 				this.BindCommand(ViewModel, vm => vm.DiscoveryNatType, v => v.DiscoveryButton).DisposeWith(d);
 

+ 0 - 38
STUN/Utils/NetUtils.cs

@@ -9,44 +9,6 @@ namespace STUN.Utils
 {
 	public static class NetUtils
 	{
-		public static IPEndPoint? ParseEndpoint(string? str)
-		{
-			if (str is null)
-			{
-				return null;
-			}
-
-			var ipPort = str.Trim().Split(':');
-			if (ipPort.Length < 2)
-			{
-				return null;
-			}
-
-			IPAddress? ip = null;
-			if (ipPort.Length == 2 && IPAddress.TryParse(ipPort[0], out ip))
-			{
-				if (!IPAddress.TryParse(ipPort[0], out ip))
-				{
-					return null;
-				}
-			}
-			else if (ipPort.Length > 2)
-			{
-				var ipStr = string.Join(@":", ipPort, 0, ipPort.Length - 1);
-				if (!ipStr.StartsWith(@"[") || !ipStr.EndsWith(@"]") || !IPAddress.TryParse(ipStr, out ip))
-				{
-					return null;
-				}
-			}
-
-			if (ip != null && ushort.TryParse(ipPort.Last(), out var port))
-			{
-				return new IPEndPoint(ip, port);
-			}
-
-			return null;
-		}
-
 		public static TcpState GetState(this TcpClient tcpClient)
 		{
 			var foo = IPGlobalProperties

+ 0 - 22
UnitTest/UnitTest.cs

@@ -4,7 +4,6 @@ using STUN.Client;
 using STUN.Enums;
 using STUN.Messages.StunAttributeValues;
 using STUN.Proxy;
-using STUN.Utils;
 using System;
 using System.Linq;
 using System.Net;
@@ -75,27 +74,6 @@ namespace UnitTest
 			Assert.IsTrue(temp[..length6].SequenceEqual(_ipv6Response));
 		}
 
-		[TestMethod]
-		[DataRow(@"1.2.3.4")]
-		[DataRow(@"1.2.256.5:80")]
-		[DataRow(@"2001:db8:1234:5678:11:2233:4455:6677:32853")]
-		[DataRow(@"2001:db8:1234:5678:11:2233:4455:6677")]
-		public void ParseEndpointTestFail(string ipStr)
-		{
-			Assert.IsNull(NetUtils.ParseEndpoint(ipStr));
-		}
-
-		[TestMethod]
-		[DataRow(@"0.0.0.0:123")]
-		[DataRow(@"192.168.1.1:2136")]
-		[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]:32853")]
-		[DataRow(@"[2001:db8:1234:5678:11:2233:4455:6677]:0")]
-		[DataRow(@"[::1]:0")]
-		public void ParseEndpointTestSuccess(string ipStr)
-		{
-			Assert.AreEqual(NetUtils.ParseEndpoint(ipStr), IPEndPoint.Parse(ipStr));
-		}
-
 		[TestMethod]
 		public async Task BindingTest()
 		{