NetworkResurrector.Server.Wrapper updated to forward headers

master
Tudor Stanciu 2022-01-18 09:43:52 +02:00
parent 68f6cc5332
commit 9402e57ec9
6 changed files with 49 additions and 7 deletions

View File

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

View File

@ -1,14 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using NetworkResurrector.Server.Wrapper.Models;
using NetworkResurrector.Server.Wrapper.Services;
using NetworkResurrector.Server.Wrapper.Services.Internal;
namespace NetworkResurrector.Server.Wrapper
{
public static class DependencyInjectionExtension
{
public static void UseResurrectorServices(this IServiceCollection services, string baseAddress)
public static void UseResurrectorServices(this IServiceCollection services, string baseAddress, bool forwardHeaders = true)
{
services.AddSingleton(new ServiceConfiguration(baseAddress));
services.AddSingleton(new ServiceConfiguration(baseAddress, forwardHeaders));
services.AddScoped<HttpHeadersService>();
services.AddHttpClient<IResurrectorService, ResurrectorService>();
}
}

View File

@ -3,10 +3,12 @@
internal class ServiceConfiguration
{
public string BaseAddress { get; }
public bool ForwardHeaders { get; }
public ServiceConfiguration(string baseAddress)
public ServiceConfiguration(string baseAddress, bool forwardHeaders)
{
BaseAddress = baseAddress;
ForwardHeaders = forwardHeaders;
}
}
}

View File

@ -6,12 +6,14 @@
<PackageProjectUrl>https://dev.azure.com/tstanciu94/NetworkResurrector</PackageProjectUrl>
<RepositoryUrl>https://dev.azure.com/tstanciu94/NetworkResurrector</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<Version>1.0.3.2</Version>
<Version>1.0.3.3</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.Server.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.Server.Wrapper.Services.Internal
{
internal class HttpHeadersService
{
private readonly string[] _headersToForward = new string[] { HeaderNames.Authorization, "rr" };
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,7 @@ using NetworkResurrector.Server.PublishedLanguage.Commands;
using NetworkResurrector.Server.PublishedLanguage.Events;
using NetworkResurrector.Server.Wrapper.Constants;
using NetworkResurrector.Server.Wrapper.Models;
using NetworkResurrector.Server.Wrapper.Services.Internal;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
@ -15,11 +16,16 @@ namespace NetworkResurrector.Server.Wrapper.Services
private const string _contentType = "application/json";
private readonly HttpClient _httpClient;
public ResurrectorService(HttpClient httpClient, ServiceConfiguration configuration)
public ResurrectorService(HttpClient httpClient, ServiceConfiguration configuration, HttpHeadersService headersService)
{
httpClient.BaseAddress = new Uri(configuration.BaseAddress);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));
if (configuration.ForwardHeaders)
{
headersService.SetHeadersFromContext(httpClient);
}
_httpClient = httpClient;
}
@ -33,7 +39,7 @@ namespace NetworkResurrector.Server.Wrapper.Services
public async Task<MachinePinged> Ping(string ipAddressOrMachineName)
{
var body = new PingMachine(ipAddressOrMachineName);
var result = await _httpClient.ExecutePostRequest<MachinePinged, PingMachine>(ApiRoutes.Wake, body);
var result = await _httpClient.ExecutePostRequest<MachinePinged, PingMachine>(ApiRoutes.Ping, body);
return result;
}