From ae3657410bba2b4f7c63bcb4348fa6ed7073d5f6 Mon Sep 17 00:00:00 2001 From: tqk2811 Date: Mon, 1 Apr 2024 16:02:51 +0700 Subject: [PATCH] update console test --- src/ConsoleTest/DebugTest.cs | 30 ++++++++++++++++++++---------- src/ConsoleTest/ProxyWraper.cs | 2 +- src/ConsoleTest/RealTest.cs | 12 ++++++------ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/ConsoleTest/DebugTest.cs b/src/ConsoleTest/DebugTest.cs index d0f5262..79975e4 100644 --- a/src/ConsoleTest/DebugTest.cs +++ b/src/ConsoleTest/DebugTest.cs @@ -4,6 +4,7 @@ using System.Net; using System.Text; using System.Threading.Tasks; +using TqkLibrary.Proxy.Authentications; using TqkLibrary.Proxy.Handlers; using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.ProxyServers; @@ -13,22 +14,31 @@ namespace ConsoleTest { internal static class DebugTest { + class MyBaseProxyServerHandler : BaseProxyServerHandler + { + readonly HttpProxyAuthentication _httpProxyAuthentication; + public MyBaseProxyServerHandler(HttpProxyAuthentication httpProxyAuthentication, IProxySource proxySource) : base(proxySource) + { + _httpProxyAuthentication = httpProxyAuthentication; + } + public override Task IsAcceptUserAsync(IUserInfo userInfo, CancellationToken cancellationToken = default) + { + if (userInfo.Authentication is HttpProxyAuthentication httpProxyAuthentication) + { + return Task.FromResult(httpProxyAuthentication.Equals(_httpProxyAuthentication)); + } + return base.IsAcceptUserAsync(userInfo, cancellationToken); + } + } public static async Task Test() { //const string address = "127.0.0.1:13566"; const string address = "[::1]:13566"; IProxySource proxySource = new LocalProxySource(); - - - CredentialCache credentialCache = new CredentialCache(); NetworkCredential networkCredential = new NetworkCredential("admin", "admin"); - credentialCache.Add(new Uri($"http://{address}"), "Basic", networkCredential); - - HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(proxySource); - handler.WithAuthentications(networkCredential); - HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(address), handler); - httpProxyServer.StartListen(); + ProxyServer proxyServer = new ProxyServer(IPEndPoint.Parse(address), new MyBaseProxyServerHandler(networkCredential, proxySource)); + proxyServer.StartListen(); using HttpClientHandler httpClientHandler = new HttpClientHandler() { @@ -81,7 +91,7 @@ public static async Task Test() Console.ReadLine(); - httpProxyServer.StopListen(); + proxyServer.StopListen(); Console.ReadLine(); } diff --git a/src/ConsoleTest/ProxyWraper.cs b/src/ConsoleTest/ProxyWraper.cs index 07a9850..af69ec0 100644 --- a/src/ConsoleTest/ProxyWraper.cs +++ b/src/ConsoleTest/ProxyWraper.cs @@ -33,7 +33,7 @@ public static async Task RunAsync() //ipEndPoint = new IPEndPoint(IPAddress.Any, 0); - using var server = new HttpProxyServer(ipEndPoint, GetProxySource()); + using var server = new ProxyServer(ipEndPoint, GetProxySource()); server.StartListen(); Console.WriteLine($"Listening {server.IPEndPoint}"); Console.ReadLine(); diff --git a/src/ConsoleTest/RealTest.cs b/src/ConsoleTest/RealTest.cs index 8f19b98..146df93 100644 --- a/src/ConsoleTest/RealTest.cs +++ b/src/ConsoleTest/RealTest.cs @@ -18,13 +18,13 @@ public static void HttpProxyServerTest() //IProxySource proxySource = new LocalProxySource(); //IProxySource proxySource = new HttpProxySource(new Uri("http://103.178.231.186:10003")); IProxySource proxySource = new HttpProxySource(new Uri("http://svhn1.proxyno1.com:41352")); - HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(address), proxySource); - httpProxyServer.StartListen(); + ProxyServer proxyServer = new ProxyServer(IPEndPoint.Parse(address), proxySource); + proxyServer.StartListen(); Console.WriteLine("server started"); Console.ReadLine(); //httpProxyServer.ChangeSource(new HttpProxySource(new Uri("http://103.178.231.186:10003")), true); Console.ReadLine(); - httpProxyServer.StopListen(); + proxyServer.StopListen(); Console.ReadLine(); } @@ -49,13 +49,13 @@ public static void Socks4ProxySourceTest() IProxySource proxySource = new Socks4ProxySource(IPEndPoint.Parse("212.213.132.5:31632"));//địa chỉ sock4 - HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:13566"), proxySource);//địa chỉ http proxy host lại + ProxyServer proxyServer = new ProxyServer(IPEndPoint.Parse("127.0.0.1:13566"), proxySource);//địa chỉ http proxy host lại - httpProxyServer.StartListen();//xài + proxyServer.StartListen();//xài //code..... dùng proxy 127.0.0.1:13566 - httpProxyServer.StopListen();//xài xong nhớ tắt + proxyServer.StopListen();//xài xong nhớ tắt Console.ReadLine();