From 9402e57ec903a2d4ca3d132453dc112c983e1b7b Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 18 Jan 2022 09:43:52 +0200 Subject: [PATCH] NetworkResurrector.Server.Wrapper updated to forward headers --- notes/nugets.txt | 2 +- .../DependencyInjectionExtension.cs | 6 ++-- .../Models/ServiceConfiguration.cs | 4 ++- .../NetworkResurrector.Server.Wrapper.csproj | 4 ++- .../Services/Internal/HttpHeadersService.cs | 30 +++++++++++++++++++ .../Services/ResurrectorService.cs | 10 +++++-- 6 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/server/NetworkResurrector.Server.Wrapper/Services/Internal/HttpHeadersService.cs diff --git a/notes/nugets.txt b/notes/nugets.txt index a626553..4e07ccb 100644 --- a/notes/nugets.txt +++ b/notes/nugets.txt @@ -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 diff --git a/src/server/NetworkResurrector.Server.Wrapper/DependencyInjectionExtension.cs b/src/server/NetworkResurrector.Server.Wrapper/DependencyInjectionExtension.cs index c3cc218..9491e05 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/DependencyInjectionExtension.cs +++ b/src/server/NetworkResurrector.Server.Wrapper/DependencyInjectionExtension.cs @@ -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(); services.AddHttpClient(); } } diff --git a/src/server/NetworkResurrector.Server.Wrapper/Models/ServiceConfiguration.cs b/src/server/NetworkResurrector.Server.Wrapper/Models/ServiceConfiguration.cs index ddfa5ec..d6d1bf7 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/Models/ServiceConfiguration.cs +++ b/src/server/NetworkResurrector.Server.Wrapper/Models/ServiceConfiguration.cs @@ -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; } } } diff --git a/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj b/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj index bdc1d8b..7fef3b3 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj +++ b/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj @@ -6,12 +6,14 @@ https://dev.azure.com/tstanciu94/NetworkResurrector https://dev.azure.com/tstanciu94/NetworkResurrector Git - 1.0.3.2 + 1.0.3.3 + + diff --git a/src/server/NetworkResurrector.Server.Wrapper/Services/Internal/HttpHeadersService.cs b/src/server/NetworkResurrector.Server.Wrapper/Services/Internal/HttpHeadersService.cs new file mode 100644 index 0000000..731deed --- /dev/null +++ b/src/server/NetworkResurrector.Server.Wrapper/Services/Internal/HttpHeadersService.cs @@ -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()); + } + } + } +} diff --git a/src/server/NetworkResurrector.Server.Wrapper/Services/ResurrectorService.cs b/src/server/NetworkResurrector.Server.Wrapper/Services/ResurrectorService.cs index 1dff873..5a53aa2 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/Services/ResurrectorService.cs +++ b/src/server/NetworkResurrector.Server.Wrapper/Services/ResurrectorService.cs @@ -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 Ping(string ipAddressOrMachineName) { var body = new PingMachine(ipAddressOrMachineName); - var result = await _httpClient.ExecutePostRequest(ApiRoutes.Wake, body); + var result = await _httpClient.ExecutePostRequest(ApiRoutes.Ping, body); return result; }