소스 검색

IUdpProxy 实现 IDisposable

Student Main 5 년 전
부모
커밋
8b22656c88
4개의 변경된 파일38개의 추가작업 그리고 31개의 파일을 삭제
  1. 6 10
      STUN/Client/StunClient3489.cs
  2. 2 2
      STUN/Proxy/IUdpProxy.cs
  3. 10 1
      STUN/Proxy/NoneUdpProxy.cs
  4. 20 18
      STUN/Proxy/Socks5UdpProxy.cs

+ 6 - 10
STUN/Client/StunClient3489.cs

@@ -34,16 +34,14 @@ namespace STUN.Client
 
         #endregion
 
-        public IPEndPoint LocalEndPoint => (IPEndPoint)UdpClient.Client.LocalEndPoint;
+        public IPEndPoint LocalEndPoint => Proxy.LocalEndPoint;
 
         public TimeSpan Timeout
         {
-            get => TimeSpan.FromMilliseconds(UdpClient.Client.ReceiveTimeout);
-            set => UdpClient.Client.ReceiveTimeout = Convert.ToInt32(value.TotalMilliseconds);
+            get => Proxy.Timeout;
+            set => Proxy.Timeout = value;
         }
 
-        protected readonly UdpClient UdpClient;
-
         protected readonly IPAddress Server;
         protected readonly ushort Port;
 
@@ -53,6 +51,7 @@ namespace STUN.Client
 
         public StunClient3489(string server, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
         {
+            Proxy = new NoneUdpProxy(local, null);
             Func<string, IPAddress> dnsQuery1;
             if (string.IsNullOrEmpty(server))
             {
@@ -80,8 +79,6 @@ namespace STUN.Client
             }
             Port = port;
 
-            UdpClient = local == null ? new UdpClient() : new UdpClient(local);
-
             Timeout = TimeSpan.FromSeconds(1.6);
         }
 
@@ -215,8 +212,7 @@ namespace STUN.Client
                 {
                     try
                     {
-                        // var (receive1, ipe, local) = await Proxy.RecieveAsync(b1, remote, receive);
-                        var (receive1, ipe, local) = await UdpClient.UdpReceiveAsync(b1, remote, receive);
+                        var (receive1, ipe, local) = await Proxy.RecieveAsync(b1, remote, receive);
 
                         var message = new StunMessage5389();
                         if (message.TryParse(receive1) &&
@@ -240,7 +236,7 @@ namespace STUN.Client
 
         public virtual void Dispose()
         {
-            UdpClient?.Dispose();
+            Proxy?.Dispose();
             _natTypeSubj.OnCompleted();
             PubSubj.OnCompleted();
             LocalSubj.OnCompleted();

+ 2 - 2
STUN/Proxy/IUdpProxy.cs

@@ -4,11 +4,11 @@ using System.Threading.Tasks;
 
 namespace STUN.Proxy
 {
-    public interface IUdpProxy
+    public interface IUdpProxy : IDisposable
     {
         TimeSpan Timeout { get; set; }
         IPEndPoint LocalEndPoint { get; }
-        Task ConnectAsync(IPEndPoint local, IPEndPoint remote);
+        Task ConnectAsync();
         Task<(byte[], IPEndPoint, IPAddress)> RecieveAsync(byte[] bytes, IPEndPoint remote, EndPoint receive);
         Task DisconnectAsync();
     }

+ 10 - 1
STUN/Proxy/NoneUdpProxy.cs

@@ -22,9 +22,13 @@ namespace STUN.Proxy
 
         protected UdpClient UdpClient;
 
-        public Task ConnectAsync(IPEndPoint local, IPEndPoint remote)
+        public NoneUdpProxy(IPEndPoint local, IPEndPoint proxy)
         {
             UdpClient = local == null ? new UdpClient() : new UdpClient(local);
+        }
+
+        public Task ConnectAsync()
+        {
             return Task.CompletedTask;
         }
 
@@ -55,5 +59,10 @@ namespace STUN.Proxy
                     (IPEndPoint)receive
                     , local);
         }
+
+        public void Dispose()
+        {
+            UdpClient?.Dispose();
+        }
     }
 }

+ 20 - 18
STUN/Proxy/Socks5UdpProxy.cs

@@ -24,22 +24,23 @@ namespace STUN.Proxy
             set => UdpClient.Client.ReceiveTimeout = Convert.ToInt32(value.TotalMilliseconds);
         }
 
-        public IPEndPoint LocalEndPoint => throw new NotImplementedException();
+        public IPEndPoint LocalEndPoint { get; private set; }
 
         UdpClient UdpClient;
 
         string user;
         string passwd;
-        public Socks5UdpProxy(IPEndPoint proxy)
+        public Socks5UdpProxy(IPEndPoint local, IPEndPoint proxy)
         {
+            LocalEndPoint = local;
+            UdpClient = local == null ? new UdpClient() : new UdpClient(local);
             socksTcpEndPoint = proxy;
         }
 
-        public async Task ConnectAsync(IPEndPoint local, IPEndPoint remote)
+        public async Task ConnectAsync()
         {
             byte[] buf = new byte[1024];
 
-            UdpClient = local == null ? new UdpClient() : new UdpClient(local);
             await assoc.ConnectAsync(socksTcpEndPoint.Address, socksTcpEndPoint.Port);
             try
             {
@@ -94,20 +95,15 @@ namespace STUN.Proxy
                 buf[0] = 5;
                 buf[1] = 3;
                 buf[2] = 0;
+
                 int addrLen;
                 int port;
-                if (remote is IPEndPoint ir)
-                {
-                    byte[] abyte = ir.Address.GetAddressBytes();
-                    addrLen = abyte.Length;
-                    buf[3] = (byte)(abyte.Length == 4 ? 1 : 4);
-                    Array.Copy(abyte, 0, buf, 4, addrLen);
-                    port = ir.Port;
-                }
-                else
-                {
-                    throw new NotImplementedException();
-                }
+                byte[] abyte = IPAddress.Any.GetAddressBytes();
+                addrLen = abyte.Length;
+                buf[3] = (byte)(abyte.Length == 4 ? 1 : 4);
+                Array.Copy(abyte, 0, buf, 4, addrLen);
+                port = 0;
+
                 buf[addrLen + 4] = (byte)(port / 256);
                 buf[addrLen + 5] = (byte)(port % 256);
 
@@ -211,9 +207,15 @@ namespace STUN.Proxy
             byte[] ret = new byte[ipbyte.Length + 3];
             ret[0] = (byte)(ipbyte.Length == 1 ? 4 : 16);
             Array.Copy(ipbyte, 0, ret, 1, ipbyte.Length);
-            ret[ipbyte.Length + 1] = (byte)(ep.Port % 256);
-            ret[ipbyte.Length + 2] = (byte)(ep.Port / 256);
+            ret[ipbyte.Length + 1] = (byte)(ep.Port / 256);
+            ret[ipbyte.Length + 2] = (byte)(ep.Port % 256);
             return ret;
         }
+    
+        public void Dispose()
+        {
+            UdpClient?.Dispose();
+            assoc?.Dispose();
+        }
     }
 }