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 22, 2023
2 parents b124503 + 73e99a9 commit 8f108a8
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 746 deletions.
12 changes: 12 additions & 0 deletions src/TestProxy/BaseBindTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProxy
{
public abstract class BaseBindTest : BaseConnectTest
{
}
}
41 changes: 41 additions & 0 deletions src/TestProxy/BaseClassTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.ProxyServers;

namespace TestProxy
{
public abstract class BaseClassTest : IDisposable
{
protected readonly HttpClient _httpClient;
readonly BaseProxyServer _proxyServer;

protected BaseClassTest()
{
_proxyServer = CreateServer(GetProxySource());
_proxyServer.StartListen();
_httpClient = new HttpClient(CreateHttpMessageHandler(_proxyServer), true);
}
~BaseClassTest()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing)
{
_httpClient.Dispose();
_proxyServer.Dispose();
}
protected abstract IProxySource GetProxySource();
protected abstract BaseProxyServer CreateServer(IProxySource proxySource);
protected abstract HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer);
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
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
namespace TestProxy
{
[TestClass]
public class LocalProxySourceTest
public abstract class BaseConnectTest : BaseClassTest
{
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);
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");
Expand All @@ -51,12 +23,9 @@ public async Task HttpGet()
[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);
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");
Expand All @@ -66,7 +35,7 @@ public async Task HttpGetTwoTimes()
{
//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);
using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
string content = await httpResponseMessage.Content.ReadAsStringAsync();
Assert.AreEqual(content, "TqkLibrary.Proxy data");
}
Expand All @@ -75,13 +44,10 @@ public async Task HttpGetTwoTimes()
[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);
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");
Expand All @@ -92,11 +58,8 @@ public async Task HttpPost()
[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);
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");
Expand All @@ -105,13 +68,10 @@ public async Task HttpsGet()
[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);
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");
Expand Down
7 changes: 7 additions & 0 deletions src/TestProxy/BaseUdpTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestProxy
{
public abstract class BaseUdpTest : BaseBindTest
{

}
}
146 changes: 0 additions & 146 deletions src/TestProxy/HttpProxyServerTest/HttpProxySourceIpV6Test.cs

This file was deleted.

Loading

0 comments on commit 8f108a8

Please sign in to comment.