Compare commits
No commits in common. "7765bc08628445e3b07a5dcfa2e2d76b4e44933a" and "d976cf58e512462c21ba5d488c41a2219e5c2bcd" have entirely different histories.
7765bc0862
...
d976cf58e5
@ -26,77 +26,56 @@ namespace FastGithub.HttpServer.TcpMiddlewares
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InvokeAsync(ConnectionDelegate next, ConnectionContext context)
|
public async Task InvokeAsync(ConnectionDelegate next, ConnectionContext context)
|
||||||
{
|
{
|
||||||
var input = context.Transport.Input;
|
var result = await context.Transport.Input.ReadAsync();
|
||||||
var output = context.Transport.Output;
|
var httpRequest = this.GetHttpRequestHandler(result, out var consumed);
|
||||||
var request = new HttpRequestHandler();
|
|
||||||
|
|
||||||
while (context.ConnectionClosed.IsCancellationRequested == false)
|
// 协议错误
|
||||||
|
if (consumed == 0L)
|
||||||
{
|
{
|
||||||
var result = await input.ReadAsync();
|
await context.Transport.Output.WriteAsync(this.http400, context.ConnectionClosed);
|
||||||
if (result.IsCanceled)
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 隧道代理连接请求
|
||||||
|
if (httpRequest.ProxyProtocol == ProxyProtocol.TunnelProxy)
|
||||||
{
|
{
|
||||||
break;
|
var position = result.Buffer.GetPosition(consumed);
|
||||||
|
context.Transport.Input.AdvanceTo(position);
|
||||||
|
await context.Transport.Output.WriteAsync(this.http200, context.ConnectionClosed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var position = result.Buffer.Start;
|
||||||
|
context.Transport.Input.AdvanceTo(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
context.Features.Set<IHttpProxyFeature>(httpRequest);
|
||||||
{
|
await next(context);
|
||||||
if (this.ParseRequest(result, request, out var consumed))
|
|
||||||
{
|
|
||||||
if (request.ProxyProtocol == ProxyProtocol.TunnelProxy)
|
|
||||||
{
|
|
||||||
input.AdvanceTo(consumed);
|
|
||||||
await output.WriteAsync(this.http200, context.ConnectionClosed);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
input.AdvanceTo(result.Buffer.Start);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.Features.Set<IHttpProxyFeature>(request);
|
|
||||||
await next(context);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
input.AdvanceTo(result.Buffer.Start, result.Buffer.End);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsCompleted)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
await output.WriteAsync(this.http400, context.ConnectionClosed);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 解析http请求
|
/// 获取http请求处理者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="result"></param>
|
/// <param name="result"></param>
|
||||||
/// <param name="requestHandler"></param>
|
|
||||||
/// <param name="consumed"></param>
|
/// <param name="consumed"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private bool ParseRequest(ReadResult result, HttpRequestHandler request, out SequencePosition consumed)
|
private HttpRequestHandler GetHttpRequestHandler(ReadResult result, out long consumed)
|
||||||
{
|
{
|
||||||
|
var handler = new HttpRequestHandler();
|
||||||
var reader = new SequenceReader<byte>(result.Buffer);
|
var reader = new SequenceReader<byte>(result.Buffer);
|
||||||
if (this.httpParser.ParseRequestLine(request, ref reader) &&
|
|
||||||
this.httpParser.ParseHeaders(request, ref reader))
|
if (this.httpParser.ParseRequestLine(handler, ref reader) &&
|
||||||
|
this.httpParser.ParseHeaders(handler, ref reader))
|
||||||
{
|
{
|
||||||
consumed = reader.Position;
|
consumed = reader.Consumed;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
consumed = default;
|
consumed = 0L;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -60,19 +60,25 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InterceptAsync(CancellationToken cancellationToken)
|
public async Task InterceptAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
|
||||||
using var divert = new WinDivert(filter, WinDivertLayer.Network);
|
using var divert = new WinDivert(filter, WinDivertLayer.Network);
|
||||||
|
cancellationToken.Register(d =>
|
||||||
|
{
|
||||||
|
((WinDivert)d!).Dispose();
|
||||||
|
DnsFlushResolverCache();
|
||||||
|
}, divert);
|
||||||
|
|
||||||
|
var addr = new WinDivertAddress();
|
||||||
using var packet = new WinDivertPacket();
|
using var packet = new WinDivertPacket();
|
||||||
using var addr = new WinDivertAddress();
|
|
||||||
|
|
||||||
DnsFlushResolverCache();
|
DnsFlushResolverCache();
|
||||||
cancellationToken.Register(DnsFlushResolverCache);
|
|
||||||
|
|
||||||
while (cancellationToken.IsCancellationRequested == false)
|
while (cancellationToken.IsCancellationRequested == false)
|
||||||
{
|
{
|
||||||
await divert.RecvAsync(packet, addr, cancellationToken);
|
divert.Recv(packet, ref addr);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.ModifyDnsPacket(packet, addr);
|
this.ModifyDnsPacket(packet, ref addr);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -80,7 +86,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
await divert.SendAsync(packet, addr, cancellationToken);
|
divert.Send(packet, ref addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,7 +96,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="packet"></param>
|
/// <param name="packet"></param>
|
||||||
/// <param name="addr"></param>
|
/// <param name="addr"></param>
|
||||||
unsafe private void ModifyDnsPacket(WinDivertPacket packet, WinDivertAddress addr)
|
unsafe private void ModifyDnsPacket(WinDivertPacket packet, ref WinDivertAddress addr)
|
||||||
{
|
{
|
||||||
var result = packet.GetParseResult();
|
var result = packet.GetParseResult();
|
||||||
var requestPayload = result.DataSpan.ToArray();
|
var requestPayload = result.DataSpan.ToArray();
|
||||||
@ -119,17 +125,46 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.IPv6Loopback;
|
var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.IPv6Loopback;
|
||||||
var record = new IPAddressResourceRecord(domain, loopback, this.ttl);
|
var record = new IPAddressResourceRecord(domain, loopback, this.ttl);
|
||||||
response.AnswerRecords.Add(record);
|
response.AnswerRecords.Add(record);
|
||||||
|
var responsePayload = response.ToArray();
|
||||||
|
|
||||||
// 修改payload
|
// 修改payload和包长
|
||||||
var writer = packet.GetWriter(packet.Length - result.DataLength);
|
responsePayload.CopyTo(new Span<byte>(result.Data, responsePayload.Length));
|
||||||
writer.Write(response.ToArray());
|
packet.Length = packet.Length + responsePayload.Length - requestPayload.Length;
|
||||||
|
|
||||||
packet.ReverseEndPoint();
|
// 修改ip包
|
||||||
packet.ApplyLengthToHeaders();
|
IPAddress destAddress;
|
||||||
packet.CalcChecksums(addr);
|
if (result.IPV4Header != null)
|
||||||
packet.CalcOutboundFlag(addr);
|
{
|
||||||
|
destAddress = result.IPV4Header->DstAddr;
|
||||||
|
result.IPV4Header->DstAddr = result.IPV4Header->SrcAddr;
|
||||||
|
result.IPV4Header->SrcAddr = destAddress;
|
||||||
|
result.IPV4Header->Length = (ushort)packet.Length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
destAddress = result.IPV6Header->DstAddr;
|
||||||
|
result.IPV6Header->DstAddr = result.IPV6Header->SrcAddr;
|
||||||
|
result.IPV6Header->SrcAddr = destAddress;
|
||||||
|
result.IPV6Header->Length = (ushort)(packet.Length - sizeof(IPV6Header));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改udp包
|
||||||
|
var destPort = result.UdpHeader->DstPort;
|
||||||
|
result.UdpHeader->DstPort = result.UdpHeader->SrcPort;
|
||||||
|
result.UdpHeader->SrcPort = destPort;
|
||||||
|
result.UdpHeader->Length = (ushort)(sizeof(UdpHeader) + responsePayload.Length);
|
||||||
|
|
||||||
addr.Flags |= WinDivertAddressFlag.Impostor;
|
addr.Flags |= WinDivertAddressFlag.Impostor;
|
||||||
|
if (addr.Flags.HasFlag(WinDivertAddressFlag.Loopback))
|
||||||
|
{
|
||||||
|
addr.Flags |= WinDivertAddressFlag.Outbound;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
addr.Flags ^= WinDivertAddressFlag.Outbound;
|
||||||
|
}
|
||||||
|
|
||||||
|
packet.CalcChecksums(ref addr);
|
||||||
this.logger.LogInformation($"{domain}->{loopback}");
|
this.logger.LogInformation($"{domain}->{loopback}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="DNS" Version="7.0.0" />
|
<PackageReference Include="DNS" Version="7.0.0" />
|
||||||
<PackageReference Include="WindivertDotnet" Version="1.1.2" />
|
<PackageReference Include="WindivertDotnet" Version="1.0.0-beta2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -50,10 +50,9 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
using var divert = new WinDivert(this.filter, WinDivertLayer.Network);
|
await Task.Yield();
|
||||||
using var packet = new WinDivertPacket();
|
|
||||||
using var addr = new WinDivertAddress();
|
|
||||||
|
|
||||||
|
using var divert = new WinDivert(this.filter, WinDivertLayer.Network, 0, WinDivertFlag.None);
|
||||||
if (Socket.OSSupportsIPv4)
|
if (Socket.OSSupportsIPv4)
|
||||||
{
|
{
|
||||||
this.logger.LogInformation($"{IPAddress.Loopback}:{this.oldServerPort} <=> {IPAddress.Loopback}:{this.newServerPort}");
|
this.logger.LogInformation($"{IPAddress.Loopback}:{this.oldServerPort} <=> {IPAddress.Loopback}:{this.newServerPort}");
|
||||||
@ -62,13 +61,18 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
{
|
{
|
||||||
this.logger.LogInformation($"{IPAddress.IPv6Loopback}:{this.oldServerPort} <=> {IPAddress.IPv6Loopback}:{this.newServerPort}");
|
this.logger.LogInformation($"{IPAddress.IPv6Loopback}:{this.oldServerPort} <=> {IPAddress.IPv6Loopback}:{this.newServerPort}");
|
||||||
}
|
}
|
||||||
|
cancellationToken.Register(d => ((WinDivert)d!).Dispose(), divert);
|
||||||
|
|
||||||
|
var addr = new WinDivertAddress();
|
||||||
|
using var packet = new WinDivertPacket();
|
||||||
while (cancellationToken.IsCancellationRequested == false)
|
while (cancellationToken.IsCancellationRequested == false)
|
||||||
{
|
{
|
||||||
await divert.RecvAsync(packet, addr, cancellationToken);
|
addr.Clear();
|
||||||
|
divert.Recv(packet, ref addr);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.ModifyTcpPacket(packet, addr);
|
this.ModifyTcpPacket(packet, ref addr);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -76,7 +80,7 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
await divert.SendAsync(packet, addr, cancellationToken);
|
divert.Send(packet, ref addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,9 +90,18 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="packet"></param>
|
/// <param name="packet"></param>
|
||||||
/// <param name="addr"></param>
|
/// <param name="addr"></param>
|
||||||
unsafe private void ModifyTcpPacket(WinDivertPacket packet, WinDivertAddress addr)
|
unsafe private void ModifyTcpPacket(WinDivertPacket packet, ref WinDivertAddress addr)
|
||||||
{
|
{
|
||||||
var result = packet.GetParseResult();
|
var result = packet.GetParseResult();
|
||||||
|
if (result.IPV4Header != null && result.IPV4Header->SrcAddr.Equals(IPAddress.Loopback) == false)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result.IPV6Header != null && result.IPV6Header->SrcAddr.Equals(IPAddress.IPv6Loopback) == false)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (result.TcpHeader->DstPort == oldServerPort)
|
if (result.TcpHeader->DstPort == oldServerPort)
|
||||||
{
|
{
|
||||||
result.TcpHeader->DstPort = this.newServerPort;
|
result.TcpHeader->DstPort = this.newServerPort;
|
||||||
@ -98,7 +111,7 @@ namespace FastGithub.PacketIntercept.Tcp
|
|||||||
result.TcpHeader->SrcPort = oldServerPort;
|
result.TcpHeader->SrcPort = oldServerPort;
|
||||||
}
|
}
|
||||||
addr.Flags |= WinDivertAddressFlag.Impostor;
|
addr.Flags |= WinDivertAddressFlag.Impostor;
|
||||||
packet.CalcChecksums(addr);
|
packet.CalcChecksums(ref addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,15 @@
|
|||||||
# FastGithub
|
# FastGithub
|
||||||
github加速神器,解决github打不开、用户头像无法加载、releases无法上传下载、git-clone、git-pull、git-push失败等问题。
|
github加速神器,解决github打不开、用户头像无法加载、releases无法上传下载、git-clone、git-pull、git-push失败等问题。
|
||||||
|
|
||||||
### 1 写在前面
|
### 0 写在前面
|
||||||
* **fastgithub不具备“翻墙”功能,也没有相关的计划**
|
* **fastgithub不具备“翻墙”功能,也没有相关的计划**
|
||||||
* **fastgithub不支持Windows7等已被发行方停止支持的操作系统,并且也不会主动提供支持**
|
* **fastgithub不支持Windows7等已被发行方停止支持的操作系统,并且也不会主动提供支持**
|
||||||
* **fastgithub不能为您的游戏加速**
|
* **fastgithub不能为您的游戏加速**
|
||||||
* **fastgithub没有主动在github之外的任何渠道发布**
|
* **fastgithub没有主动在github和fastgithub@qq.com之外的任何渠道发布**
|
||||||
|
|
||||||
|
### 1 程序下载
|
||||||
|
* [github-release下载](https://github.com/dotnetcore/fastgithub/releases)
|
||||||
|
* 发送任意邮件到fastgithub@qq.com
|
||||||
|
|
||||||
### 2 部署方式
|
### 2 部署方式
|
||||||
#### 2.1 windows-x64桌面
|
#### 2.1 windows-x64桌面
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user