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

+ 27 - 78
NatTypeTester/ViewModels/MainWindowViewModel.cs

@@ -2,6 +2,7 @@ using DynamicData;
 using DynamicData.Binding;
 using DynamicData.Binding;
 using NatTypeTester.Model;
 using NatTypeTester.Model;
 using ReactiveUI;
 using ReactiveUI;
+using ReactiveUI.Fody.Helpers;
 using STUN.Client;
 using STUN.Client;
 using STUN.Enums;
 using STUN.Enums;
 using STUN.Proxy;
 using STUN.Proxy;
@@ -20,26 +21,14 @@ namespace NatTypeTester.ViewModels
 	{
 	{
 		#region RFC3489
 		#region RFC3489
 
 
-		private string _classicNatType;
-		public string ClassicNatType
-		{
-			get => _classicNatType;
-			set => this.RaiseAndSetIfChanged(ref _classicNatType, value);
-		}
+		[Reactive]
+		public string? ClassicNatType { get; set; }
 
 
-		private string _localEnd = NetUtils.DefaultLocalEnd;
-		public string LocalEnd
-		{
-			get => _localEnd;
-			set => this.RaiseAndSetIfChanged(ref _localEnd, value);
-		}
+		[Reactive]
+		public string LocalEnd { get; set; } = NetUtils.DefaultLocalEnd;
 
 
-		private string _publicEnd;
-		public string PublicEnd
-		{
-			get => _publicEnd;
-			set => this.RaiseAndSetIfChanged(ref _publicEnd, value);
-		}
+		[Reactive]
+		public string? PublicEnd { get; set; }
 
 
 		public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
 		public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
 
 
@@ -47,40 +36,20 @@ namespace NatTypeTester.ViewModels
 
 
 		#region RFC5780
 		#region RFC5780
 
 
-		private string _bindingTest;
-		public string BindingTest
-		{
-			get => _bindingTest;
-			set => this.RaiseAndSetIfChanged(ref _bindingTest, value);
-		}
+		[Reactive]
+		public string? BindingTest { get; set; }
 
 
-		private string _mappingBehavior;
-		public string MappingBehavior
-		{
-			get => _mappingBehavior;
-			set => this.RaiseAndSetIfChanged(ref _mappingBehavior, value);
-		}
+		[Reactive]
+		public string? MappingBehavior { get; set; }
 
 
-		private string _filteringBehavior;
-		public string FilteringBehavior
-		{
-			get => _filteringBehavior;
-			set => this.RaiseAndSetIfChanged(ref _filteringBehavior, value);
-		}
+		[Reactive]
+		public string? FilteringBehavior { get; set; }
 
 
-		private string _localAddress = NetUtils.DefaultLocalEnd;
-		public string LocalAddress
-		{
-			get => _localAddress;
-			set => this.RaiseAndSetIfChanged(ref _localAddress, value);
-		}
+		[Reactive]
+		public string? LocalAddress { get; set; }
 
 
-		private string _mappingAddress;
-		public string MappingAddress
-		{
-			get => _mappingAddress;
-			set => this.RaiseAndSetIfChanged(ref _mappingAddress, value);
-		}
+		[Reactive]
+		public string? MappingAddress { get; set; }
 
 
 		public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }
 		public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }
 
 
@@ -88,12 +57,8 @@ namespace NatTypeTester.ViewModels
 
 
 		#region Servers
 		#region Servers
 
 
-		private string _stunServer;
-		public string StunServer
-		{
-			get => _stunServer;
-			set => this.RaiseAndSetIfChanged(ref _stunServer, value);
-		}
+		[Reactive]
+		public string StunServer { get; set; } = @"stun.syncthing.net";
 
 
 		private readonly IEnumerable<string> _defaultServers = new HashSet<string>
 		private readonly IEnumerable<string> _defaultServers = new HashSet<string>
 		{
 		{
@@ -111,33 +76,17 @@ namespace NatTypeTester.ViewModels
 
 
 		#region Proxy
 		#region Proxy
 
 
-		private ProxyType _proxyType = ProxyType.Socks5;
-		public ProxyType ProxyType
-		{
-			get => _proxyType;
-			set => this.RaiseAndSetIfChanged(ref _proxyType, value);
-		}
+		[Reactive]
+		public ProxyType ProxyType { get; set; } = ProxyType.Socks5;
 
 
-		private string _proxyServer = @"127.0.0.1:1080";
-		public string ProxyServer
-		{
-			get => _proxyServer;
-			set => this.RaiseAndSetIfChanged(ref _proxyServer, value);
-		}
+		[Reactive]
+		public string ProxyServer { get; set; } = @"127.0.0.1:1080";
 
 
-		private string _proxyUser;
-		public string ProxyUser
-		{
-			get => _proxyUser;
-			set => this.RaiseAndSetIfChanged(ref _proxyUser, value);
-		}
+		[Reactive]
+		public string? ProxyUser { get; set; }
 
 
-		private string _proxyPassword;
-		public string ProxyPassword
-		{
-			get => _proxyPassword;
-			set => this.RaiseAndSetIfChanged(ref _proxyPassword, value);
-		}
+		[Reactive]
+		public string? ProxyPassword { get; set; }
 
 
 		#endregion
 		#endregion
 
 

+ 6 - 6
STUN/Message/Attribute.cs

@@ -31,7 +31,7 @@ namespace STUN.Message
 
 
 		public int RealLength => Type == AttributeType.Useless ? 0 : 4 + Length + (4 - Length % 4) % 4;
 		public int RealLength => Type == AttributeType.Useless ? 0 : 4 + Length + (4 - Length % 4) % 4;
 
 
-		public IAttribute? Value { get; set; }
+		public IAttribute Value { get; set; } = new UselessAttribute();
 
 
 		private readonly byte[] _magicCookie;
 		private readonly byte[] _magicCookie;
 		private readonly byte[] _transactionId;
 		private readonly byte[] _transactionId;
@@ -60,10 +60,7 @@ namespace STUN.Message
 
 
 			res.AddRange(Convert.ToUInt16(Type).ToBe());
 			res.AddRange(Convert.ToUInt16(Type).ToBe());
 			res.AddRange(Length.ToBe());
 			res.AddRange(Length.ToBe());
-			if (Value != null)
-			{
-				res.AddRange(Value.Bytes);
-			}
+			res.AddRange(Value.Bytes);
 
 
 			var n = (4 - res.Count % 4) % 4; // 填充的字节数
 			var n = (4 - res.Count % 4) % 4; // 填充的字节数
 			res.AddRange(BitUtils.GetRandomBytes(n));
 			res.AddRange(BitUtils.GetRandomBytes(n));
@@ -105,7 +102,10 @@ namespace STUN.Message
 				AttributeType.ErrorCode => new ErrorCodeAttribute(),
 				AttributeType.ErrorCode => new ErrorCodeAttribute(),
 				_ => new UselessAttribute()
 				_ => new UselessAttribute()
 			};
 			};
-			Value = t.TryParse(value) ? t : null;
+			if (t.TryParse(value))
+			{
+				Value = t;
+			}
 
 
 			return 4 + Length + (4 - Length % 4) % 4; // 对齐
 			return 4 + Length + (4 - Length % 4) % 4; // 对齐
 		}
 		}

+ 5 - 1
STUN/Proxy/ProxyFactory.cs

@@ -6,8 +6,12 @@ namespace STUN.Proxy
 {
 {
 	public static class ProxyFactory
 	public static class ProxyFactory
 	{
 	{
-		public static IUdpProxy CreateProxy(ProxyType type, IPEndPoint? local, IPEndPoint proxy, string? user = null, string? password = null)
+		public static IUdpProxy CreateProxy(ProxyType type, IPEndPoint? local, IPEndPoint? proxy, string? user = null, string? password = null)
 		{
 		{
+			if (proxy is null)
+			{
+				throw new ArgumentNullException(nameof(proxy), @"Proxy server is null");
+			}
 			return type switch
 			return type switch
 			{
 			{
 				ProxyType.Plain => new NoneUdpProxy(local),
 				ProxyType.Plain => new NoneUdpProxy(local),

+ 4 - 4
STUN/Utils/AttributeExtensions.cs

@@ -28,7 +28,7 @@ namespace STUN.Utils
 			}
 			}
 
 
 			var mapped = (MappedAddressAttribute)mappedAddressAttribute.Value;
 			var mapped = (MappedAddressAttribute)mappedAddressAttribute.Value;
-			return new IPEndPoint(mapped.Address, mapped.Port);
+			return new IPEndPoint(mapped.Address!, mapped.Port);
 		}
 		}
 
 
 		public static IPEndPoint? GetChangedAddressAttribute(this StunMessage5389? response)
 		public static IPEndPoint? GetChangedAddressAttribute(this StunMessage5389? response)
@@ -41,7 +41,7 @@ namespace STUN.Utils
 			}
 			}
 
 
 			var address = (ChangedAddressAttribute)changedAddressAttribute.Value;
 			var address = (ChangedAddressAttribute)changedAddressAttribute.Value;
-			return new IPEndPoint(address.Address, address.Port);
+			return new IPEndPoint(address.Address!, address.Port);
 		}
 		}
 
 
 		public static IPEndPoint? GetXorMappedAddressAttribute(this StunMessage5389? response)
 		public static IPEndPoint? GetXorMappedAddressAttribute(this StunMessage5389? response)
@@ -56,7 +56,7 @@ namespace STUN.Utils
 			}
 			}
 
 
 			var mapped = (AddressAttribute)mappedAddressAttribute.Value;
 			var mapped = (AddressAttribute)mappedAddressAttribute.Value;
-			return new IPEndPoint(mapped.Address, mapped.Port);
+			return new IPEndPoint(mapped.Address!, mapped.Port);
 		}
 		}
 
 
 		public static IPEndPoint? GetOtherAddressAttribute(this StunMessage5389? response)
 		public static IPEndPoint? GetOtherAddressAttribute(this StunMessage5389? response)
@@ -71,7 +71,7 @@ namespace STUN.Utils
 			}
 			}
 
 
 			var address = (AddressAttribute)addressAttribute.Value;
 			var address = (AddressAttribute)addressAttribute.Value;
-			return new IPEndPoint(address.Address, address.Port);
+			return new IPEndPoint(address.Address!, address.Port);
 		}
 		}
 	}
 	}
 }
 }

+ 6 - 1
STUN/Utils/NetUtils.cs

@@ -9,8 +9,13 @@ namespace STUN.Utils
 	{
 	{
 		public const string DefaultLocalEnd = @"0.0.0.0:0";
 		public const string DefaultLocalEnd = @"0.0.0.0:0";
 
 
-		public static IPEndPoint? ParseEndpoint(string str)
+		public static IPEndPoint? ParseEndpoint(string? str)
 		{
 		{
+			if (str is null)
+			{
+				return null;
+			}
+
 			var ipPort = str.Trim().Split(':');
 			var ipPort = str.Trim().Split(':');
 			if (ipPort.Length < 2)
 			if (ipPort.Length < 2)
 			{
 			{

+ 5 - 5
UnitTest/UnitTest.cs

@@ -92,7 +92,7 @@ namespace UnitTest
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
-			Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any);
+			Assert.AreNotEqual(result.LocalEndPoint!.Address, IPAddress.Any);
 			Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown);
 			Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown);
 			Assert.AreEqual(result.FilteringBehavior, FilteringBehavior.Unknown);
 			Assert.AreEqual(result.FilteringBehavior, FilteringBehavior.Unknown);
 		}
 		}
@@ -107,7 +107,7 @@ namespace UnitTest
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
-			Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any);
+			Assert.AreNotEqual(result.LocalEndPoint!.Address, IPAddress.Any);
 			Assert.IsTrue(result.MappingBehavior is MappingBehavior.Direct
 			Assert.IsTrue(result.MappingBehavior is MappingBehavior.Direct
 			or MappingBehavior.EndpointIndependent
 			or MappingBehavior.EndpointIndependent
 			or MappingBehavior.AddressDependent
 			or MappingBehavior.AddressDependent
@@ -126,7 +126,7 @@ namespace UnitTest
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
-			Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any);
+			Assert.AreNotEqual(result.LocalEndPoint!.Address, IPAddress.Any);
 			Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown);
 			Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown);
 			Assert.IsTrue(result.FilteringBehavior is FilteringBehavior.EndpointIndependent
 			Assert.IsTrue(result.FilteringBehavior is FilteringBehavior.EndpointIndependent
 			or FilteringBehavior.AddressDependent
 			or FilteringBehavior.AddressDependent
@@ -144,7 +144,7 @@ namespace UnitTest
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
-			Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any);
+			Assert.AreNotEqual(result.LocalEndPoint!.Address, IPAddress.Any);
 			Assert.IsTrue(result.MappingBehavior is MappingBehavior.Direct
 			Assert.IsTrue(result.MappingBehavior is MappingBehavior.Direct
 						  or MappingBehavior.EndpointIndependent
 						  or MappingBehavior.EndpointIndependent
 						  or MappingBehavior.AddressDependent
 						  or MappingBehavior.AddressDependent
@@ -167,7 +167,7 @@ namespace UnitTest
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.LocalEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.PublicEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
 			Assert.IsNotNull(result.OtherEndPoint);
-			Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any);
+			Assert.AreNotEqual(result.LocalEndPoint!.Address, IPAddress.Any);
 			Assert.IsTrue(
 			Assert.IsTrue(
 				result.MappingBehavior is MappingBehavior.Direct
 				result.MappingBehavior is MappingBehavior.Direct
 				or MappingBehavior.EndpointIndependent
 				or MappingBehavior.EndpointIndependent