bugfix: 修复外网在url中传输token导致401的问题

This commit is contained in:
范露尧
2023-01-13 13:35:20 +08:00
parent f785a068aa
commit b6e5d146e2
6 changed files with 162 additions and 64 deletions

View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpModule.Proxy.Core", "HttpModule.Proxy.Core\HttpModule.Proxy.Core.csproj", "{C070FB18-9DA3-4EA6-A69C-AE0D25AD8617}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C070FB18-9DA3-4EA6-A69C-AE0D25AD8617}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C070FB18-9DA3-4EA6-A69C-AE0D25AD8617}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C070FB18-9DA3-4EA6-A69C-AE0D25AD8617}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C070FB18-9DA3-4EA6-A69C-AE0D25AD8617}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A67D4C98-498C-424B-83DB-E41D588C994F}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,40 @@
using System;
namespace HttpModule.Proxy.Core
{
public class MyActionFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
//============== 这里是执行方法之前获取数据 ====================
// 获取控制器、路由信息
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
// 获取请求的方法
var method = actionDescriptor!.MethodInfo;
// 获取 HttpContext 和 HttpRequest 对象
var httpContext = context.HttpContext;
var httpRequest = httpContext.Request;
// 获取客户端 Ipv4 地址
var remoteIPv4 = httpContext.GetRemoteIpAddressToIPv4();
// 获取请求的 Url 地址
var requestUrl = httpRequest.GetRequestUrlAddress();
// 获取来源 Url 地址
var refererUrl = httpRequest.GetRefererUrlAddress();
// 获取请求参数(写入日志,需序列化成字符串后存储),可以自由篡改!!!!!!
var parameters = context.ActionArguments;
// 获取操作人(必须授权访问才有值)"userId" 为你存储的 claims typejwt 授权对应的是 payload 中存储的键名
var userId = httpContext.User?.FindFirstValue("userId");
// 请求时间
var requestedTime = DateTimeOffset.Now;
//============== 这里是执行方法之后获取数据 ====================
var actionContext = await next();
// 获取返回的结果
var returnResult = actionContext.Result;
// 判断是否请求成功,没有异常就是请求成功
var isRequestSucceed = actionContext.Exception == null;
// 获取调用堆栈信息,提供更加简单明了的调用和异常堆栈
var stackTrace = EnhancedStackTrace.Current();
// 其他操作,如写入日志
}
}
}

View File

@@ -8,74 +8,78 @@ using System.Text;
namespace Getf.Service.Transfer.Core namespace Getf.Service.Transfer.Core
{ {
public class Session : AppSession<Session, TransInfo> public class Session : AppSession<Session, TransInfo>
{ {
static ArraySegment<byte> Heart = new ArraySegment<byte>(new byte[] { 0x40, 0x40, 0, 0, 0, 0, 0, 0, 0, 0 }); static ArraySegment<byte> Heart = new ArraySegment<byte>(new byte[] { 0x40, 0x40, 0, 0, 0, 0, 0, 0, 0, 0 });
protected override int GetMaxRequestLength() protected override int GetMaxRequestLength()
{ {
return 100 * 1024 * 1024; return 100 * 1024 * 1024;
} }
protected override void OnSessionStarted() protected override void OnSessionStarted()
{ {
base.OnSessionStarted(); base.OnSessionStarted();
Logger.Debug("SessionStarted:" + this.SessionID); Logger.Debug("SessionStarted:" + this.SessionID);
} }
public void SendHeart() public void SendHeart()
{ {
base.Send(Heart); base.Send(Heart);
} }
/// <summary> /// <summary>
/// 异常捕捉 /// 异常捕捉
/// </summary> /// </summary>
/// <param name="e"></param> /// <param name="e"></param>
protected override void HandleException(Exception e) protected override void HandleException(Exception e)
{ {
Logger.Error(e.Message, e); Logger.Error(e.Message, e);
base.HandleException(e); base.HandleException(e);
} }
protected override void HandleUnknownRequest(TransInfo requestInfo) protected override void HandleUnknownRequest(TransInfo requestInfo)
{ {
Logger.Warn("UnknownRequest" + JsonConvert.SerializeObject(requestInfo)); Logger.Warn("UnknownRequest" + JsonConvert.SerializeObject(requestInfo));
base.HandleUnknownRequest(requestInfo); base.HandleUnknownRequest(requestInfo);
} }
public void Send(object entity) public void Send(object entity)
{ {
List<byte> bufferHead = new List<byte>(); List<byte> bufferHead = new List<byte>();
bufferHead.AddRange(Encoding.UTF8.GetBytes("@@")); bufferHead.AddRange(Encoding.UTF8.GetBytes("@@"));
List<byte> bufferBody = new List<byte>(); List<byte> bufferBody = new List<byte>();
//bufferBody.AddRange(new Guid(this.Key).ToByteArray()); //bufferBody.AddRange(new Guid(this.Key).ToByteArray());
var json = JsonConvert.SerializeObject(entity); var json = JsonConvert.SerializeObject(entity);
bufferBody.AddRange(Encoding.UTF8.GetBytes(json)); bufferBody.AddRange(Encoding.UTF8.GetBytes(json));
bufferHead.AddRange(BitConverter.GetBytes(bufferBody.Count)); bufferHead.AddRange(BitConverter.GetBytes(bufferBody.Count));
bufferHead.AddRange(BitConverter.GetBytes(0)); bufferHead.AddRange(BitConverter.GetBytes(0));
bufferHead.AddRange(bufferBody); bufferHead.AddRange(bufferBody);
ArraySegment<byte> arraySegment = new ArraySegment<byte>(bufferHead.ToArray()); ArraySegment<byte> arraySegment = new ArraySegment<byte>(bufferHead.ToArray());
base.Send(arraySegment); base.Send(arraySegment);
} }
public void Send(TransInfo transInfo) public void Send(TransInfo transInfo)
{ {
List<byte> bufferHead = new List<byte>(); if (transInfo.TransResultInfo == null)
bufferHead.AddRange(Encoding.UTF8.GetBytes("@@")); transInfo.TransResultInfo = new TransResult { Code = 200 };
List<byte> bufferBody = new List<byte>(); else
//bufferBody.AddRange(new Guid(this.Key).ToByteArray()); transInfo.TransResultInfo.Code = 200;
var json = JsonConvert.SerializeObject(transInfo); List<byte> bufferHead = new List<byte>();
bufferBody.AddRange(Encoding.UTF8.GetBytes(json)); bufferHead.AddRange(Encoding.UTF8.GetBytes("@@"));
bufferHead.AddRange(BitConverter.GetBytes(bufferBody.Count)); List<byte> bufferBody = new List<byte>();
bufferHead.AddRange(BitConverter.GetBytes(transInfo.Data == null ? 0 : transInfo.Data.Length)); //bufferBody.AddRange(new Guid(this.Key).ToByteArray());
bufferHead.AddRange(bufferBody); var json = JsonConvert.SerializeObject(transInfo);
if (transInfo.Data != null) bufferBody.AddRange(Encoding.UTF8.GetBytes(json));
{ bufferHead.AddRange(BitConverter.GetBytes(bufferBody.Count));
bufferHead.AddRange(transInfo.Data); bufferHead.AddRange(BitConverter.GetBytes(transInfo.Data == null ? 0 : transInfo.Data.Length));
} bufferHead.AddRange(bufferBody);
ArraySegment<byte> arraySegment = new ArraySegment<byte>(bufferHead.ToArray()); if (transInfo.Data != null)
base.Send(arraySegment); {
} bufferHead.AddRange(transInfo.Data);
} }
ArraySegment<byte> arraySegment = new ArraySegment<byte>(bufferHead.ToArray());
base.Send(arraySegment);
}
}
} }

View File

@@ -12,6 +12,7 @@
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Disk</InstallFrom>
@@ -24,7 +25,6 @@
<MapFileExtensions>true</MapFileExtensions> <MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision> <ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>

View File

@@ -11,6 +11,21 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
@@ -85,5 +100,12 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>