Bruce Wayne 4 年之前
父節點
當前提交
197245da89

+ 1 - 1
NatTypeTester/ViewModels/MainWindowViewModel.cs

@@ -104,7 +104,7 @@ namespace NatTypeTester.ViewModels
 				@"stun.stunprotocol.org"
 		};
 
-		private SourceList<string> List { get; } = new SourceList<string>();
+		private SourceList<string> List { get; } = new();
 		public readonly IObservableCollection<string> StunServers = new ObservableCollectionExtended<string>();
 
 		#endregion

+ 25 - 27
STUN/Client/StunClient3489.cs

@@ -23,14 +23,14 @@ namespace STUN.Client
 	{
 		#region Subject
 
-		private readonly Subject<NatType> _natTypeSubj = new Subject<NatType>();
+		private readonly Subject<NatType> _natTypeSubj = new();
 		public IObservable<NatType> NatTypeChanged => _natTypeSubj.AsObservable();
 
-		protected readonly Subject<IPEndPoint> PubSubj = new Subject<IPEndPoint>();
-		public IObservable<IPEndPoint> PubChanged => PubSubj.AsObservable();
+		protected readonly Subject<IPEndPoint?> PubSubj = new();
+		public IObservable<IPEndPoint?> PubChanged => PubSubj.AsObservable();
 
-		protected readonly Subject<IPEndPoint> LocalSubj = new Subject<IPEndPoint>();
-		public IObservable<IPEndPoint> LocalChanged => LocalSubj.AsObservable();
+		protected readonly Subject<IPEndPoint?> LocalSubj = new();
+		public IObservable<IPEndPoint?> LocalChanged => LocalSubj.AsObservable();
 
 		#endregion
 
@@ -45,11 +45,11 @@ namespace STUN.Client
 		protected readonly IPAddress Server;
 		protected readonly ushort Port;
 
-		public IPEndPoint RemoteEndPoint => Server == null ? null : new IPEndPoint(Server, Port);
+		public IPEndPoint RemoteEndPoint => new(Server, Port);
 
 		protected readonly IUdpProxy Proxy;
 
-		public StunClient3489(string server, ushort port = 3478, IPEndPoint local = null, IUdpProxy proxy = null, IDnsQuery dnsQuery = null)
+		public StunClient3489(string server, ushort port = 3478, IPEndPoint? local = null, IUdpProxy? proxy = null, IDnsQuery? dnsQuery = null)
 		{
 			Proxy = proxy ?? new NoneUdpProxy(local);
 
@@ -65,11 +65,9 @@ namespace STUN.Client
 
 			dnsQuery ??= new DefaultDnsQuery();
 
-			Server = dnsQuery.Query(server);
-			if (Server == null)
-			{
-				throw new ArgumentException(@"Wrong STUN server !");
-			}
+			var ip = dnsQuery.Query(server);
+
+			Server = ip ?? throw new ArgumentException(@"Wrong STUN server !");
 			Port = port;
 
 			Timeout = TimeSpan.FromSeconds(1.6);
@@ -89,23 +87,23 @@ namespace STUN.Client
 				var test1 = new StunMessage5389 { StunMessageType = StunMessageType.BindingRequest, MagicCookie = 0 };
 
 				var (response1, remote1, local1) = await TestAsync(test1, RemoteEndPoint, RemoteEndPoint, cts.Token);
-				if (response1 == null)
+				if (response1 is null || remote1 is null)
 				{
 					res.NatType = NatType.UdpBlocked;
 					return res;
 				}
 
-				if (local1 != null)
+				if (local1 is not null)
 				{
 					LocalSubj.OnNext(LocalEndPoint);
 				}
 
-				var mappedAddress1 = AttributeExtensions.GetMappedAddressAttribute(response1);
-				var changedAddress1 = AttributeExtensions.GetChangedAddressAttribute(response1);
+				var mappedAddress1 = response1.GetMappedAddressAttribute();
+				var changedAddress1 = response1.GetChangedAddressAttribute();
 
 				// 某些单 IP 服务器的迷惑操作
-				if (mappedAddress1 == null
-				|| changedAddress1 == null
+				if (mappedAddress1 is null
+				|| changedAddress1 is null
 				|| Equals(changedAddress1.Address, remote1.Address)
 				|| changedAddress1.Port == remote1.Port)
 				{
@@ -124,12 +122,12 @@ namespace STUN.Client
 
 				// test II
 				var (response2, remote2, _) = await TestAsync(test2, RemoteEndPoint, changedAddress1, cts.Token);
-				var mappedAddress2 = AttributeExtensions.GetMappedAddressAttribute(response2);
+				var mappedAddress2 = response2.GetMappedAddressAttribute();
 
 				if (Equals(mappedAddress1.Address, local1) && mappedAddress1.Port == LocalEndPoint.Port)
 				{
 					// No NAT
-					if (response2 == null)
+					if (response2 is null)
 					{
 						res.NatType = NatType.SymmetricUdpFirewall;
 						res.PublicEndPoint = mappedAddress1;
@@ -141,7 +139,7 @@ namespace STUN.Client
 				}
 
 				// NAT
-				if (response2 != null)
+				if (response2 is not null && remote2 is not null)
 				{
 					// 有些单 IP 服务器并不能测 NAT 类型,比如 Google 的
 					var type = Equals(remote1.Address, remote2.Address) || remote1.Port == remote2.Port ? NatType.UnsupportedServer : NatType.FullCone;
@@ -153,9 +151,9 @@ namespace STUN.Client
 				// Test I(#2)
 				var test12 = new StunMessage5389 { StunMessageType = StunMessageType.BindingRequest, MagicCookie = 0 };
 				var (response12, _, _) = await TestAsync(test12, changedAddress1, changedAddress1, cts.Token);
-				var mappedAddress12 = AttributeExtensions.GetMappedAddressAttribute(response12);
+				var mappedAddress12 = response12.GetMappedAddressAttribute();
 
-				if (mappedAddress12 == null)
+				if (mappedAddress12 is null)
 				{
 					res.NatType = NatType.Unknown;
 					return res;
@@ -176,8 +174,8 @@ namespace STUN.Client
 					Attributes = new[] { AttributeExtensions.BuildChangeRequest(false, true) }
 				};
 				var (response3, _, _) = await TestAsync(test3, changedAddress1, changedAddress1, cts.Token);
-				var mappedAddress3 = AttributeExtensions.GetMappedAddressAttribute(response3);
-				if (mappedAddress3 != null)
+				var mappedAddress3 = response3.GetMappedAddressAttribute();
+				if (mappedAddress3 is not null)
 				{
 					res.NatType = NatType.RestrictedCone;
 					res.PublicEndPoint = mappedAddress3;
@@ -195,7 +193,7 @@ namespace STUN.Client
 			}
 		}
 
-		protected async Task<(StunMessage5389, IPEndPoint, IPAddress)> TestAsync(StunMessage5389 sendMessage, IPEndPoint remote, IPEndPoint receive, CancellationToken token)
+		protected async Task<(StunMessage5389?, IPEndPoint?, IPAddress?)> TestAsync(StunMessage5389 sendMessage, IPEndPoint remote, IPEndPoint receive, CancellationToken token)
 		{
 			try
 			{
@@ -232,7 +230,7 @@ namespace STUN.Client
 
 		public virtual void Dispose()
 		{
-			Proxy?.Dispose();
+			Proxy.Dispose();
 			_natTypeSubj.OnCompleted();
 			PubSubj.OnCompleted();
 			LocalSubj.OnCompleted();

+ 11 - 11
STUN/Client/StunClient5389UDP.cs

@@ -21,13 +21,13 @@ namespace STUN.Client
 	{
 		#region Subject
 
-		private readonly Subject<BindingTestResult> _bindingSubj = new Subject<BindingTestResult>();
+		private readonly Subject<BindingTestResult> _bindingSubj = new();
 		public IObservable<BindingTestResult> BindingTestResultChanged => _bindingSubj.AsObservable();
 
-		private readonly Subject<MappingBehavior> _mappingBehaviorSubj = new Subject<MappingBehavior>();
+		private readonly Subject<MappingBehavior> _mappingBehaviorSubj = new();
 		public IObservable<MappingBehavior> MappingBehaviorChanged => _mappingBehaviorSubj.AsObservable();
 
-		private readonly Subject<FilteringBehavior> _filteringBehaviorSubj = new Subject<FilteringBehavior>();
+		private readonly Subject<FilteringBehavior> _filteringBehaviorSubj = new();
 		public IObservable<FilteringBehavior> FilteringBehaviorChanged => _filteringBehaviorSubj.AsObservable();
 
 		#endregion
@@ -67,7 +67,7 @@ namespace STUN.Client
 				}
 
 				// MappingBehaviorTest test II
-				var result2 = await BindingTestBaseAsync(new IPEndPoint(result.OtherEndPoint.Address, RemoteEndPoint.Port), false, cts.Token);
+				var result2 = await BindingTestBaseAsync(new IPEndPoint(result.OtherEndPoint!.Address, RemoteEndPoint.Port), false, cts.Token);
 				if (result2.BindingTestResult != BindingTestResult.Success)
 				{
 					result.MappingBehavior = MappingBehavior.Fail;
@@ -121,13 +121,13 @@ namespace STUN.Client
 			var (response1, _, local1) = await TestAsync(test, remote, remote, token);
 			var mappedAddress1 = AttributeExtensions.GetXorMappedAddressAttribute(response1);
 			var otherAddress = AttributeExtensions.GetOtherAddressAttribute(response1);
-			var local = local1 == null ? null : new IPEndPoint(local1, LocalEndPoint.Port);
+			var local = local1 is null ? null : new IPEndPoint(local1, LocalEndPoint.Port);
 
-			if (response1 == null)
+			if (response1 is null)
 			{
 				res = BindingTestResult.Fail;
 			}
-			else if (mappedAddress1 == null)
+			else if (mappedAddress1 is null)
 			{
 				res = BindingTestResult.UnsupportedServer;
 			}
@@ -166,7 +166,7 @@ namespace STUN.Client
 					return result;
 				}
 
-				if (result.OtherEndPoint == null
+				if (result.OtherEndPoint is null
 					|| Equals(result.OtherEndPoint.Address, RemoteEndPoint.Address)
 					|| result.OtherEndPoint.Port == RemoteEndPoint.Port)
 				{
@@ -224,7 +224,7 @@ namespace STUN.Client
 					return result1;
 				}
 
-				if (result1.OtherEndPoint == null
+				if (result1.OtherEndPoint is null
 					|| Equals(result1.OtherEndPoint.Address, RemoteEndPoint.Address)
 					|| result1.OtherEndPoint.Port == RemoteEndPoint.Port)
 				{
@@ -240,7 +240,7 @@ namespace STUN.Client
 				};
 				var (response2, _, _) = await TestAsync(test2, RemoteEndPoint, result1.OtherEndPoint, token);
 
-				if (response2 != null)
+				if (response2 is not null)
 				{
 					result1.FilteringBehavior = FilteringBehavior.EndpointIndependent;
 					return result1;
@@ -254,7 +254,7 @@ namespace STUN.Client
 				};
 				var (response3, remote3, _) = await TestAsync(test3, RemoteEndPoint, RemoteEndPoint, token);
 
-				if (response3 == null)
+				if (response3 is null || remote3 is null)
 				{
 					result1.FilteringBehavior = FilteringBehavior.AddressAndPortDependent;
 					return result1;

+ 16 - 6
STUN/DnsClients/DefaultDnsQuery.cs

@@ -6,12 +6,17 @@ namespace STUN.DnsClients
 {
 	public class DefaultDnsQuery : IDnsQuery
 	{
-		public async Task<IPAddress> QueryAsync(string host)
+		public async Task<IPAddress?> QueryAsync(string? host)
 		{
 			try
 			{
+				if (host is null)
+				{
+					return null;
+				}
+
 				var ip = IsIPAddress(host);
-				if (ip != null)
+				if (ip is not null)
 				{
 					return ip;
 				}
@@ -24,12 +29,17 @@ namespace STUN.DnsClients
 			}
 		}
 
-		public IPAddress Query(string host)
+		public IPAddress? Query(string? host)
 		{
 			try
 			{
+				if (host is null)
+				{
+					return null;
+				}
+
 				var ip = IsIPAddress(host);
-				if (ip != null)
+				if (ip is not null)
 				{
 					return ip;
 				}
@@ -42,9 +52,9 @@ namespace STUN.DnsClients
 			}
 		}
 
-		private static IPAddress IsIPAddress(string host)
+		private static IPAddress? IsIPAddress(string? host)
 		{
-			if (host != null && IPAddress.TryParse(host, out var ip))
+			if (host is not null && IPAddress.TryParse(host, out var ip))
 			{
 				return ip;
 			}

+ 2 - 2
STUN/DnsClients/IDnsQuery.cs

@@ -5,7 +5,7 @@ namespace STUN.DnsClients
 {
 	public interface IDnsQuery
 	{
-		public Task<IPAddress> QueryAsync(string host);
-		public IPAddress Query(string host);
+		public Task<IPAddress?> QueryAsync(string? host);
+		public IPAddress? Query(string? host);
 	}
 }

+ 2 - 2
STUN/Message/Attributes/AddressAttribute.cs

@@ -17,7 +17,7 @@ namespace STUN.Message.Attributes
 		{
 			get
 			{
-				if (Address == null)
+				if (Address is null)
 				{
 					return Array.Empty<byte>();
 				}
@@ -32,7 +32,7 @@ namespace STUN.Message.Attributes
 
 		public ushort Port { get; set; }
 
-		public IPAddress Address { get; set; }
+		public IPAddress? Address { get; set; }
 
 		public virtual bool TryParse(byte[] bytes)
 		{

+ 20 - 17
STUN/Message/Attributes/ChangeRequestAttribute.cs

@@ -4,27 +4,30 @@ using System.Collections.Generic;
 
 namespace STUN.Message.Attributes
 {
-    /// <summary>
-    /// https://tools.ietf.org/html/rfc5780#section-7.2
-    /// </summary>
-    public class ChangeRequestAttribute : IAttribute
-    {
-        public IEnumerable<byte> Bytes => new byte[] { 0, 0, 0, (byte)(Convert.ToInt32(ChangeIp) << 2 | Convert.ToInt32(ChangePort) << 1) };
+	/// <summary>
+	/// https://tools.ietf.org/html/rfc5780#section-7.2
+	/// </summary>
+	public class ChangeRequestAttribute : IAttribute
+	{
+		public IEnumerable<byte> Bytes => new byte[] { 0, 0, 0, (byte)(Convert.ToInt32(ChangeIp) << 2 | Convert.ToInt32(ChangePort) << 1) };
 
-        public bool ChangeIp { get; set; }
+		public bool ChangeIp { get; set; }
 
-        public bool ChangePort { get; set; }
+		public bool ChangePort { get; set; }
 
-        public bool TryParse(byte[] bytes)
-        {
-            if (bytes.Length != 4) return false;
+		public bool TryParse(byte[] bytes)
+		{
+			if (bytes.Length != 4)
+			{
+				return false;
+			}
 
-            var bits = new BitArray(bytes);
+			var bits = new BitArray(bytes);
 
-            ChangeIp = bits[29];
-            ChangePort = bits[30];
+			ChangeIp = bits[29];
+			ChangePort = bits[30];
 
-            return true;
-        }
-    }
+			return true;
+		}
+	}
 }

+ 1 - 1
STUN/Message/Attributes/UnknownAttribute.cs

@@ -24,7 +24,7 @@ namespace STUN.Message.Attributes
 			}
 		}
 
-		public IEnumerable<AttributeType> Types { get; set; }
+		public IEnumerable<AttributeType> Types { get; set; } = Array.Empty<AttributeType>();
 
 		public bool TryParse(byte[] bytes)
 		{

+ 3 - 2
STUN/Message/Attributes/UselessAttribute.cs

@@ -1,3 +1,4 @@
+using System;
 using System.Collections.Generic;
 
 namespace STUN.Message.Attributes
@@ -9,12 +10,12 @@ namespace STUN.Message.Attributes
 	{
 		public IEnumerable<byte> Bytes => _bytes;
 
-		private byte[] _bytes;
+		private byte[] _bytes = Array.Empty<byte>();
 
 		public bool TryParse(byte[] bytes)
 		{
 			_bytes = bytes;
-			return _bytes != null;
+			return true;
 		}
 	}
 }

+ 2 - 2
STUN/Message/Attributes/XorMappedAddressAttribute.cs

@@ -24,7 +24,7 @@ namespace STUN.Message.Attributes
 		{
 			get
 			{
-				if (Address == null)
+				if (Address is null)
 				{
 					return Array.Empty<byte>();
 				}
@@ -45,7 +45,7 @@ namespace STUN.Message.Attributes
 
 			Port = Xor(Port);
 
-			Address = Xor(Address);
+			Address = Xor(Address!);
 
 			return true;
 		}

+ 1 - 1
STUN/Proxy/ProxyFactory.cs

@@ -6,7 +6,7 @@ namespace STUN.Proxy
 {
 	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)
 		{
 			return type switch
 			{