Skip to content

Commit

Permalink
Added usage of de facto standard proxy headers to GatewayRouter Service
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyDevelopment committed Dec 19, 2024
1 parent 8e9661b commit 9b037fd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
26 changes: 19 additions & 7 deletions src/Fancy.ResourceLinker.Gateway/Routing/GatewayRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Fancy.ResourceLinker.Models.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using System.Diagnostics;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -103,6 +102,22 @@ public GatewayRouter(GatewayRoutingSettings settings, IHttpForwarder forwarder,
_forwarderTransformer = new GatewayForwarderHttpTransformer();
}

/// <summary>
/// Sets the proxy headers to the request.
/// </summary>
/// <param name="request">The request.</param>
private void SetProxyHeaders(HttpRequestMessage request)
{
if (_settings.ResourceProxy != null)
{
string[] proxyParts = _settings.ResourceProxy.Split("://");
string proto = proxyParts[0];
string host = proxyParts[1];
request.Headers.Add("X-Forwarded-Proto", proto);
request.Headers.Add("X-Forwarded-Host", host);
}
}

/// <summary>
/// Sends a request and deserializes the response into a given type.
/// </summary>
Expand All @@ -112,8 +127,8 @@ public GatewayRouter(GatewayRoutingSettings settings, IHttpForwarder forwarder,
/// <returns>The result deserialized into the specified resource type.</returns>
private async Task<TResource?> SendAsync<TResource>(HttpRequestMessage request, string routeName) where TResource : class
{
if (_settings.ResourceProxy != null) request.Headers.Add("X-Forwarded-Host", _settings.ResourceProxy);
SetProxyHeaders(request);

// Set authentication to request
IRouteAuthenticationStrategy authStrategy = await _routeAuthManager.GetAuthStrategyAsync(routeName);
await authStrategy.SetAuthenticationAsync(_serviceProvider, request);
Expand All @@ -138,10 +153,7 @@ public GatewayRouter(GatewayRoutingSettings settings, IHttpForwarder forwarder,
/// <param name="routeName">The name of the route to use.</param>
private async Task SendAsync(HttpRequestMessage request, string routeName)
{
if (_settings.ResourceProxy != null)
{
request.Headers.Add("X-Forwarded-Host", _settings.ResourceProxy);
}
SetProxyHeaders(request);

// Set authentication to request
IRouteAuthenticationStrategy authStrategy = await _routeAuthManager.GetAuthStrategyAsync(routeName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down

0 comments on commit 9b037fd

Please sign in to comment.