Преглед изворни кода

代理与 ViewModel 对接

Student Main пре 5 година
родитељ
комит
19783814e5

+ 55 - 2
NatTypeTester/ViewModels/MainWindowViewModel.cs

@@ -10,6 +10,8 @@ using DynamicData.Binding;
 using NatTypeTester.Model;
 using ReactiveUI;
 using STUN.Client;
+using STUN.Enums;
+using STUN.Proxy;
 using STUN.Utils;
 
 namespace NatTypeTester.ViewModels
@@ -107,6 +109,38 @@ namespace NatTypeTester.ViewModels
 
         #endregion
 
+        #region Proxy
+
+        private ProxyType _proxyType = ProxyType.Socks5;
+        public ProxyType ProxyType
+        {
+            get => _proxyType;
+            set => this.RaiseAndSetIfChanged(ref _proxyType, value);
+        }
+
+        private string _proxyServer = "127.0.0.1:1081";
+        public string ProxyServer
+        {
+            get => _proxyServer;
+            set => this.RaiseAndSetIfChanged(ref _proxyServer, value);
+        }
+
+        private string _proxyUser;
+        public string ProxyUser
+        {
+            get => _proxyUser;
+            set => this.RaiseAndSetIfChanged(ref _proxyUser, value);
+        }
+
+        private string _proxyPassword;
+        public string ProxyPassword
+        {
+            get => _proxyPassword;
+            set => this.RaiseAndSetIfChanged(ref _proxyPassword, value);
+        }
+
+        #endregion
+
         public MainWindowViewModel()
         {
             LoadStunServer();
@@ -152,8 +186,18 @@ namespace NatTypeTester.ViewModels
                     var server = new StunServer();
                     if (server.Parse(StunServer))
                     {
-                        using var client = new StunClient3489(server.Hostname, server.Port,
+                        var proxy = ProxyFactory.CreateProxy(
+                            ProxyType,
+                            NetUtils.ParseEndpoint(LocalEnd),
+                            NetUtils.ParseEndpoint(ProxyServer),
+                            ProxyUser, ProxyPassword
+                            );
+
+                        using var client = new StunClient3489(server.Hostname, proxy, server.Port,
                                 NetUtils.ParseEndpoint(LocalEnd));
+
+
+
                         client.NatTypeChanged.ObserveOn(RxApp.MainThreadScheduler)
                                 .Subscribe(t => ClassicNatType = $@"{t}");
                         client.PubChanged.ObserveOn(RxApp.MainThreadScheduler).Subscribe(t => PublicEnd = $@"{t}");
@@ -181,7 +225,16 @@ namespace NatTypeTester.ViewModels
                     var server = new StunServer();
                     if (server.Parse(StunServer))
                     {
-                        using var client = new StunClient5389UDP(server.Hostname, server.Port, NetUtils.ParseEndpoint(LocalAddress));
+                        var proxy = ProxyFactory.CreateProxy(
+                            ProxyType,
+                            NetUtils.ParseEndpoint(LocalEnd),
+                            NetUtils.ParseEndpoint(ProxyServer),
+                            ProxyUser, ProxyPassword
+                            );
+
+                        using var client = new StunClient5389UDP(server.Hostname, proxy, server.Port, NetUtils.ParseEndpoint(LocalAddress));
+
+
 
                         client.BindingTestResultChanged
                                 .ObserveOn(RxApp.MainThreadScheduler)

+ 3 - 2
STUN/Client/StunClient3489.cs

@@ -49,9 +49,10 @@ namespace STUN.Client
 
         protected IUdpProxy Proxy;
 
-        public StunClient3489(string server, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
+        public StunClient3489(string server, IUdpProxy proxy = null, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
         {
-            Proxy = new Socks5UdpProxy(local, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1081));
+            Proxy = proxy ?? new NoneUdpProxy(local, null);
+
             Func<string, IPAddress> dnsQuery1;
             if (string.IsNullOrEmpty(server))
             {

+ 3 - 2
STUN/Client/StunClient5389UDP.cs

@@ -1,6 +1,7 @@
 using STUN.Enums;
 using STUN.Interfaces;
 using STUN.Message;
+using STUN.Proxy;
 using STUN.StunResult;
 using STUN.Utils;
 using System;
@@ -32,8 +33,8 @@ namespace STUN.Client
 
         #endregion
 
-        public StunClient5389UDP(string server, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
-        : base(server, port, local, dnsQuery)
+        public StunClient5389UDP(string server, IUdpProxy proxy, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
+        : base(server, proxy, port, local, dnsQuery)
         {
             Timeout = TimeSpan.FromSeconds(3);
         }

+ 24 - 0
STUN/Proxy/ProxyFactory.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Text;
+using STUN.Enums;
+
+namespace STUN.Proxy
+{
+    public static class ProxyFactory
+    {
+        public static IUdpProxy CreateProxy(ProxyType type, IPEndPoint local, IPEndPoint proxy, string user, string password)
+        {
+            switch (type)
+            {
+                case ProxyType.Plain:
+                    return new NoneUdpProxy(local, null);
+                case ProxyType.Socks5:
+                    return new Socks5UdpProxy(local, proxy);
+                default:
+                    throw new NotSupportedException(type.ToString());
+            }
+        }
+    }
+}

+ 2 - 1
STUN/Utils/NetUtils.cs

@@ -45,7 +45,8 @@ namespace STUN.Utils
 
         public static async Task<StunResult5389> NatBehaviorDiscovery(string server, ushort port, IPEndPoint local)
         {
-            using var client = new StunClient5389UDP(server, port, local);
+            // proxy is not supported yet
+            using var client = new StunClient5389UDP(server, null, port, local);
             return await client.QueryAsync();
         }