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 20, 2023
2 parents 35d40f6 + 0b2e7ca commit a20a048
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
25 changes: 21 additions & 4 deletions ConsoleTest/ProxyWraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TqkLibrary.Proxy.Authentications;
Expand All @@ -16,21 +17,37 @@ internal static class ProxyWraper
const string listen = "127.0.0.1:16615";
public static async Task RunAsync()

Check warning on line 18 in ConsoleTest/ProxyWraper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in ConsoleTest/ProxyWraper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
using var server = new HttpProxyServer(IPEndPoint.Parse(listen), GetProxySource());
string strHostName = Dns.GetHostName();
IPHostEntry iPHostEntry = Dns.GetHostEntry(strHostName);
IPAddress? ipaddress = null;

ipaddress = iPHostEntry
.AddressList
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);

IPEndPoint ipEndPoint = IPEndPoint.Parse(listen);
if (ipaddress is not null)
{
ipEndPoint = new IPEndPoint(ipaddress, 0);
}

ipEndPoint = new IPEndPoint(IPAddress.Any, 0);

using var server = new HttpProxyServer(ipEndPoint, GetProxySource());
server.StartListen();
Console.WriteLine($"Listening {listen}");
Console.WriteLine($"Listening {server.IPEndPoint}");
Console.ReadLine();
}

static IProxySource GetProxySource()
{
HttpProxyAuthentication? auth = null;// new HttpProxyAuthentication("", "");

Check warning on line 44 in ConsoleTest/ProxyWraper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

The variable 'auth' is assigned but its value is never used

Check warning on line 44 in ConsoleTest/ProxyWraper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

The variable 'auth' is assigned but its value is never used
HttpProxySource httpProxySource = new HttpProxySource(new Uri("http://167.99.142.56:20128"));
HttpProxySource httpProxySource = new HttpProxySource(new Uri("http://88.99.245.58:8903"));
Socks4ProxySource socks4ProxySource = new Socks4ProxySource(IPEndPoint.Parse("93.104.63.65:80"));
Socks5ProxySource socks5ProxySource = new Socks5ProxySource(IPEndPoint.Parse("138.201.120.118:29127"));
LocalProxySource localProxySource = new LocalProxySource();

return localProxySource;
return httpProxySource;
}
}
}
3 changes: 1 addition & 2 deletions TqkLibrary.Proxy/ProxyServers/BaseProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace TqkLibrary.Proxy.ProxyServers
{
public abstract class BaseProxyServer : IProxyServer
{
public IPEndPoint IPEndPoint { get; }
public IPEndPoint? IPEndPoint { get { return _tcpListener.LocalEndpoint as IPEndPoint; } }
public IProxySource ProxySource { get; private set; }
public int Timeout { get; set; } = 30000;

Expand All @@ -40,7 +40,6 @@ BaseProxyServerHandler handler
this._baseProxyServerHandler = handler ?? throw new ArgumentNullException(nameof(handler));
this.ProxySource = proxySource ?? throw new ArgumentNullException(nameof(proxySource));
this._tcpListener = new TcpListener(iPEndPoint);
this.IPEndPoint = iPEndPoint;
_logger = Singleton.LoggerFactory?.CreateLogger(this.GetType());
}
~BaseProxyServer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal override async Task ProxyWorkAsync()
if (_client_HeaderLines.Count == 0)
return;//client stream closed

_logger?.LogInformation($"{_clientEndPoint} -> ", _client_HeaderLines.ToArray());
_logger?.LogInformation($"{_clientEndPoint} -> \r\n{string.Join("\r\n", _client_HeaderLines)}");

_client_HeaderParse = HeaderRequestParse.ParseRequest(_client_HeaderLines);

Expand Down Expand Up @@ -92,13 +92,14 @@ await _proxyServer.Handler.CheckAuthenticationAsync(new HttpProxyAuthentication(
{
using IConnectSource connectSource = _proxyServer.ProxySource.GetConnectSource();
await connectSource.InitAsync(_client_HeaderParse.Uri, _cancellationToken);
using Stream source_stream = await connectSource.GetStreamAsync();
if ("CONNECT".Equals(_client_HeaderParse.Method, StringComparison.OrdinalIgnoreCase))
{
should_continue = await _HttpsTransfer(connectSource);
should_continue = await _HttpsTransfer(source_stream);
}
else
{
should_continue = await _HttpTransfer(connectSource);
should_continue = await _HttpTransfer(source_stream);
}
}
else
Expand All @@ -110,23 +111,23 @@ await _proxyServer.Handler.CheckAuthenticationAsync(new HttpProxyAuthentication(
while ((client_isKeepAlive || should_continue));
}

async Task<bool> _HttpsTransfer(IConnectSource connectSource)
async Task<bool> _HttpsTransfer(Stream source_stream)
{
if (_client_HeaderParse is null)
throw new InvalidOperationException();

await _WriteResponse(200, "Connection established", true);

using var remote_stream = await connectSource.GetStreamAsync();
await new StreamTransferHelper(_clientStream, remote_stream)
await new StreamTransferHelper(_clientStream, source_stream)
.DebugName(_clientEndPoint, _client_HeaderParse?.Uri)
.WaitUntilDisconnect(_cancellationToken);
return false;
}

async Task<bool> _HttpTransfer(IConnectSource connectSource)
async Task<bool> _HttpTransfer(Stream source_stream)
{
using Stream target_Stream = await connectSource.GetStreamAsync();
if (_client_HeaderParse is null || _client_HeaderLines is null)
throw new InvalidOperationException();

//send header to target
List<string> headerLines = new List<string>();
Expand All @@ -141,29 +142,29 @@ async Task<bool> _HttpTransfer(IConnectSource connectSource)
headerLines.Add(line);
}

await target_Stream.WriteLineAsync(string.Join("\r\n", headerLines), _cancellationToken);
_logger?.LogInformation($"{_client_HeaderParse.Uri.Host} <- ", headerLines.ToArray());
await source_stream.WriteLineAsync(string.Join("\r\n", headerLines), _cancellationToken);
_logger?.LogInformation($"{_client_HeaderParse.Uri.Host} <- \r\n{string.Join("\r\n", headerLines)}");

await target_Stream.WriteLineAsync(_cancellationToken);
await source_stream.WriteLineAsync(_cancellationToken);

//Transfer content from client to target if have
await _clientStream.TransferAsync(target_Stream, _client_HeaderParse.ContentLength, cancellationToken: _cancellationToken);
await _clientStream.TransferAsync(source_stream, _client_HeaderParse.ContentLength, cancellationToken: _cancellationToken);
_logger?.LogInformation($"[{_clientEndPoint} -> {_client_HeaderParse.Uri.Host}] {_client_HeaderParse.ContentLength} bytes");

await target_Stream.FlushAsync(_cancellationToken);
await source_stream.FlushAsync(_cancellationToken);

//-----------------------------------------------------
//read header from target, and send back to client
IReadOnlyList<string> target_response_HeaderLines = await target_Stream.ReadHeadersAsync(_cancellationToken);
IReadOnlyList<string> target_response_HeaderLines = await source_stream.ReadHeadersAsync(_cancellationToken);
int ContentLength = target_response_HeaderLines.GetContentLength();

await _clientStream.WriteLineAsync(string.Join("\r\n", target_response_HeaderLines), _cancellationToken);
_logger?.LogInformation($"{_client_HeaderParse.Uri.Host} -> ", target_response_HeaderLines.ToArray());
_logger?.LogInformation($"{_client_HeaderParse.Uri.Host} ->\r\n{string.Join("\r\n", target_response_HeaderLines)}");

await _clientStream.WriteLineAsync(_cancellationToken);

//Transfer content from target to client if have
await target_Stream.TransferAsync(_clientStream, ContentLength, cancellationToken: _cancellationToken);
await source_stream.TransferAsync(_clientStream, ContentLength, cancellationToken: _cancellationToken);
_logger?.LogInformation($"[{_clientEndPoint} <- {_client_HeaderParse.Uri.Host}] {ContentLength} bytes");

await _clientStream.FlushAsync(_cancellationToken);
Expand Down Expand Up @@ -221,7 +222,7 @@ async Task<bool> _WriteResponse(IEnumerable<string> headers, byte[]? body = null
}

await _clientStream.WriteHeadersAsync(headers, _cancellationToken);
_logger?.LogInformation($"{_clientEndPoint} <-", headers.ToArray());
_logger?.LogInformation($"{_clientEndPoint} <-\r\n{string.Join("\r\n", headers)}");

if (body is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ async Task<bool> _CONNECT_Async(Uri address, CancellationToken cancellationToken
}

await _stream.WriteHeadersAsync(headers, cancellationToken);
_logger?.LogInformation($"{_proxySource._proxy.Host}:{_proxySource._proxy.Port} <-", headers.ToArray());
_logger?.LogInformation($"{_proxySource._proxy.Host}:{_proxySource._proxy.Port} <-\r\n{string.Join("\r\n", headers)}");

await _stream.FlushAsync(cancellationToken);

//-----------------------///

IReadOnlyList<string> response_HeaderLines = await _stream.ReadHeadersAsync(cancellationToken);
_logger?.LogInformation($"{_proxySource._proxy.Host}:{_proxySource._proxy.Port} ->", response_HeaderLines.ToArray());
_logger?.LogInformation($"{_proxySource._proxy.Host}:{_proxySource._proxy.Port} ->\r\n{string.Join("\r\n", response_HeaderLines)}");

var headerResponseParse = HeaderResponseParse.ParseResponse(response_HeaderLines);

Expand Down
24 changes: 12 additions & 12 deletions TqkLibrary.Proxy/TqkLibrary.Proxy.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
<version>1.0.0-build$buildDay$$buildIndex$</version>
<description>my private lib</description>
<authors>tqk2811</authors>
<projectUrl>https://github.com/tqk2811/TqkLibrary.Adb</projectUrl>
<repository type="git" url="https://github.com/tqk2811/TqkLibrary.Adb.git"/>
<projectUrl>https://github.com/tqk2811/TqkLibrary.Proxy</projectUrl>
<repository type="git" url="https://github.com/tqk2811/TqkLibrary.Proxy.git"/>
<license type="expression">MIT</license>
<dependencies>
<group targetFramework="net462">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<dependency id="Microsoft.Extensions.Logging" version="8.0.0" />
<dependency id="System.Net.Http" version="4.3.4" />
</group>
<group targetFramework="netstandard2.0">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<dependency id="Microsoft.Extensions.Logging" version="8.0.0" />
<dependency id="System.Net.Http" version="4.3.4" />
</group>
<group targetFramework="net5.0">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<dependency id="Microsoft.Extensions.Logging" version="8.0.0" />
<dependency id="System.Net.Http" version="4.3.4" />
</group>
<group targetFramework="net6.0">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<dependency id="Microsoft.Extensions.Logging" version="8.0.0" />
<dependency id="System.Net.Http" version="4.3.4" />
</group>
<group targetFramework="net7.0">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<dependency id="Microsoft.Extensions.Logging" version="8.0.0" />
<dependency id="System.Net.Http" version="4.3.4" />
</group>
</dependencies>
</metadata>
Expand Down

0 comments on commit a20a048

Please sign in to comment.