Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tqk2811 committed Dec 11, 2023
2 parents f32f294 + cf69b8a commit 5b049d7
Show file tree
Hide file tree
Showing 29 changed files with 790 additions and 269 deletions.
5 changes: 4 additions & 1 deletion ConsoleTest/DebugTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Filters;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;
using TqkLibrary.Proxy.ProxySources;
Expand All @@ -23,8 +24,10 @@ public static async Task Test()
NetworkCredential networkCredential = new NetworkCredential("admin", "admin");
credentialCache.Add(new Uri($"http://{address}"), "Basic", networkCredential);

HttpProxyServerFilter filter = new HttpProxyServerFilter();
filter.WithAuthentications(networkCredential);

HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(address), proxySource, networkCredential);
HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(address), proxySource, filter);
httpProxyServer.StartListen();

using HttpClientHandler httpClientHandler = new HttpClientHandler()
Expand Down
16 changes: 14 additions & 2 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ConsoleTest;
using System.Net.Sockets;
using System.Net;

Uri uri0 = new Uri("http://127.0.0.1:13566");
Uri uri1 = new Uri("http://[::1]:13566");
Expand All @@ -10,6 +12,16 @@
Uri uri7 = new Uri("tcp://127.0.0.1:13566");
Uri uri8 = new Uri("udp://[::1]:13566");

string strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);

await DebugTest.Test();
//await RealTest.Test();
IPHostEntry iPHostEntry = Dns.GetHostEntry(strHostName);
var ip = iPHostEntry
.AddressList
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);

TcpListener tcpListener = new TcpListener(IPAddress.Any, 0);
tcpListener.Start();

//await DebugTest.Test();
RealTest.HttpProxyServerTest();
64 changes: 64 additions & 0 deletions ConsoleTest/RealTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;
using TqkLibrary.Proxy.ProxySources;

namespace ConsoleTest
{
internal static class RealTest
{
const string address = "127.0.0.1:13566";
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();
Console.WriteLine("server started");
Console.ReadLine();
httpProxyServer.ChangeSource(new HttpProxySource(new Uri("http://103.178.231.186:10003")), true);
Console.ReadLine();
httpProxyServer.StopListen();
Console.ReadLine();
}

const string address2 = "117.2.46.2:5678";
public static void Socks4ProxySourceTest()
{
//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"),new NetworkCredential("user","pass"));
//HttpProxyServer httpProxyServer = new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:13566"), proxySource);
//httpProxyServer.StartListen();

//Console.WriteLine("server started");
//Console.ReadLine();
//httpProxyServer.ChangeSource(new HttpProxySource(new Uri("http://103.178.231.186:10003")), true);
//Console.ReadLine();
//httpProxyServer.StopListen();
//Console.ReadLine();



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

httpProxyServer.StartListen();//xài

//code..... dùng proxy 127.0.0.1:13566

httpProxyServer.StopListen();//xài xong nhớ tắt


Console.ReadLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.Filters;
using TqkLibrary.Proxy.Authentications;

namespace TestProxy.HttpProxyServerTest
{
[TestClass]
public class LocalHttpProxySourceIpV6Test
public class LocalProxySourceIpV6Test
{
static readonly IProxySource localProxySource;

static readonly HttpClientHandler httpClientHandler;
static readonly HttpClient httpClient;
static readonly NetworkCredential networkCredential = new NetworkCredential("user", "password");
static LocalHttpProxySourceIpV6Test()
static readonly HttpProxyServerFilter filter = new HttpProxyServerFilter();
static LocalProxySourceIpV6Test()
{
localProxySource = new LocalProxySource();

Expand All @@ -34,12 +37,13 @@ static LocalHttpProxySourceIpV6Test()
DefaultProxyCredentials = networkCredential,
};
httpClient = new HttpClient(httpClientHandler, false);
filter.WithAuthentications(networkCredential);
}

[TestMethod]
public async Task HttpGet()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get");
Expand All @@ -52,7 +56,7 @@ public async Task HttpGet()
[TestMethod]
public async Task HttpGetTwoTimes()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, filter);
httpProxyServer.StartListen();

{
Expand All @@ -76,7 +80,7 @@ public async Task HttpGetTwoTimes()
[TestMethod]
public async Task HttpPost()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post");
Expand All @@ -93,7 +97,7 @@ public async Task HttpPost()
[TestMethod]
public async Task HttpsGet()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get");
Expand All @@ -106,7 +110,7 @@ public async Task HttpsGet()
[TestMethod]
public async Task HttpsPost()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;
using TqkLibrary.Proxy.Filters;
using TqkLibrary.Proxy.Authentications;

namespace TestProxy.HttpProxyServerTest
{
[TestClass]
public class LocalHttpProxySourceTest
public class LocalProxySourceTest
{
static readonly IProxySource localProxySource;

static readonly HttpClientHandler httpClientHandler;
static readonly HttpClient httpClient;
static readonly NetworkCredential networkCredential = new NetworkCredential("user", "password");
static LocalHttpProxySourceTest()
static readonly HttpProxyServerFilter filter = new HttpProxyServerFilter();
static LocalProxySourceTest()
{
localProxySource = new LocalProxySource();

Expand All @@ -34,12 +37,14 @@ static LocalHttpProxySourceTest()
DefaultProxyCredentials = networkCredential,
};
httpClient = new HttpClient(httpClientHandler, false);
filter.WithAuthentications(networkCredential);
}

//------------------Connect Test----------------//
[TestMethod]
public async Task HttpGet()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get");
Expand All @@ -52,7 +57,7 @@ public async Task HttpGet()
[TestMethod]
public async Task HttpGetTwoTimes()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, filter);
httpProxyServer.StartListen();

{
Expand All @@ -76,7 +81,7 @@ public async Task HttpGetTwoTimes()
[TestMethod]
public async Task HttpPost()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post");
Expand All @@ -93,7 +98,7 @@ public async Task HttpPost()
[TestMethod]
public async Task HttpsGet()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get");
Expand All @@ -106,7 +111,7 @@ public async Task HttpsGet()
[TestMethod]
public async Task HttpsPost()
{
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, networkCredential);
using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, filter);
httpProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post");
Expand All @@ -118,5 +123,11 @@ public async Task HttpsPost()
Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/post");
Assert.AreEqual(json["data"]?.ToString(), "Test post");
}


//------------------Bind Test----------------//



}
}
121 changes: 121 additions & 0 deletions TestProxy/Socks4ProxyServerTest/LocalProxySourceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxySources;
using TqkLibrary.Proxy.ProxyServers;
using Newtonsoft.Json;

namespace TestProxy.Socks4ProxyServerTest
{
[TestClass]
public class LocalProxySourceTest
{
static readonly IProxySource localProxySource;

static readonly SocketsHttpHandler httpClientHandler;
static readonly HttpClient httpClient;
static LocalProxySourceTest()
{
localProxySource = new LocalProxySource();
//.Net6 support socks4 and socks5
//https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#socks-proxy-support
httpClientHandler = new SocketsHttpHandler()
{
Proxy = new WebProxy()
{
Address = new Uri($"socks4://{Singleton.Address0}"),
},
UseCookies = false,
UseProxy = true,
};
httpClient = new HttpClient(httpClientHandler, false);
}

[TestMethod]
public async Task HttpGet()
{
using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);
socks4ProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(content);
Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get");
}

[TestMethod]
public async Task HttpGetTwoTimes()
{
using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);
socks4ProxyServer.StartListen();

{
using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(content);
Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get");
}

//Test make new request on 1 connection with proxy
{
//github will redirect (301) http -> https -> new connection proxy using CONNECT method
using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
Assert.AreEqual(content, "TqkLibrary.Proxy data");
}
}

[TestMethod]
public async Task HttpPost()
{
using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);
socks4ProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post");
httpRequestMessage.Headers.Add("Accept", "application/json");
httpRequestMessage.Content = new StringContent("Test post");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(content);
Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/post");
Assert.AreEqual(json["data"]?.ToString(), "Test post");
}


[TestMethod]
public async Task HttpsGet()
{
using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);
socks4ProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(content);
Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/get");
}

[TestMethod]
public async Task HttpsPost()
{
using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);
socks4ProxyServer.StartListen();

using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post");
httpRequestMessage.Headers.Add("Accept", "application/json");
httpRequestMessage.Content = new StringContent("Test post");
using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(content);
Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/post");
Assert.AreEqual(json["data"]?.ToString(), "Test post");
}
}
}
Loading

0 comments on commit 5b049d7

Please sign in to comment.