NetworkResurrector.Agent.Wrapper

master
Tudor Stanciu 2022-01-18 17:43:16 +02:00
parent 1558165581
commit 46f47dfa9f
8 changed files with 60 additions and 6 deletions

View File

@ -9,5 +9,5 @@ dotnet nuget push NetworkResurrector.Server.Wrapper.1.0.3.3.nupkg -k ***REMOVED*
# Agent
dotnet nuget push NetworkResurrector.Agent.PublishedLanguage.1.0.3.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget
dotnet nuget push NetworkResurrector.Agent.Wrapper.1.0.3.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget
dotnet nuget push NetworkResurrector.Agent.Wrapper.1.0.3.1.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget
#######################################################################################################################################################

View File

@ -1,12 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using NetworkResurrector.Agent.Wrapper.Models;
using NetworkResurrector.Agent.Wrapper.Services;
using NetworkResurrector.Agent.Wrapper.Services.Internal;
namespace NetworkResurrector.Agent.Wrapper
{
public static class DependencyInjectionExtension
{
public static void UseResurrectorAgentServices(this IServiceCollection services)
public static void UseResurrectorAgentServices(this IServiceCollection services, bool forwardHeaders = true)
{
services.AddSingleton(new ServiceConfiguration(forwardHeaders));
services.AddScoped<HttpHeadersService>();
services.AddHttpClient<IResurrectorAgentService, ResurrectorAgentService>();
}
}

View File

@ -0,0 +1,12 @@
namespace NetworkResurrector.Agent.Wrapper.Models
{
internal class ServiceConfiguration
{
public bool ForwardHeaders { get; }
public ServiceConfiguration(bool forwardHeaders)
{
ForwardHeaders = forwardHeaders;
}
}
}

View File

@ -6,11 +6,14 @@
<PackageProjectUrl>https://dev.azure.com/tstanciu94/NetworkResurrector</PackageProjectUrl>
<RepositoryType>Git</RepositoryType>
<RepositoryUrl>https://dev.azure.com/tstanciu94/NetworkResurrector</RepositoryUrl>
<Version>1.0.3.1</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
<PackageReference Include="NDB.Extensions.Http" Version="1.0.0" />
<PackageReference Include="NetworkResurrector.Agent.PublishedLanguage" Version="1.0.3" />
</ItemGroup>

View File

@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using System.Net.Http;
namespace NetworkResurrector.Agent.Wrapper.Services.Internal
{
internal class HttpHeadersService
{
private readonly string[] _headersToForward = new string[] { HeaderNames.Authorization };
private readonly IHttpContextAccessor _httpAccessor;
public HttpHeadersService(IHttpContextAccessor httpAccessor)
{
_httpAccessor=httpAccessor;
}
public void SetHeadersFromContext(HttpClient httpClient)
{
foreach (var headerKey in _headersToForward)
{
var header = _httpAccessor.HttpContext.Request.Headers[headerKey];
if (header == StringValues.Empty)
continue;
httpClient.DefaultRequestHeaders.Add(headerKey, header.ToString());
}
}
}
}

View File

@ -3,6 +3,8 @@ using NetworkResurrector.Agent.PublishedLanguage.Commands;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using NetworkResurrector.Agent.PublishedLanguage.Events;
using NetworkResurrector.Agent.Wrapper.Constants;
using NetworkResurrector.Agent.Wrapper.Models;
using NetworkResurrector.Agent.Wrapper.Services.Internal;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
@ -15,10 +17,15 @@ namespace NetworkResurrector.Agent.Wrapper.Services
private const string _contentType = "application/json";
private readonly HttpClient _httpClient;
public ResurrectorAgentService(HttpClient httpClient)
public ResurrectorAgentService(HttpClient httpClient, ServiceConfiguration configuration, HttpHeadersService headersService)
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));
if (configuration.ForwardHeaders)
{
headersService.SetHeadersFromContext(httpClient);
}
_httpClient = httpClient;
}

View File

@ -1,11 +1,9 @@
using AutoMapper;
using MediatR;
using MediatR.Pipeline;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants;
using NDB.Security.Authentication.Identity;

View File

@ -7,7 +7,7 @@ namespace NetworkResurrector.Server.Wrapper.Services.Internal
{
internal class HttpHeadersService
{
private readonly string[] _headersToForward = new string[] { HeaderNames.Authorization, "rr" };
private readonly string[] _headersToForward = new string[] { HeaderNames.Authorization };
private readonly IHttpContextAccessor _httpAccessor;
public HttpHeadersService(IHttpContextAccessor httpAccessor)