Skip to content

Commit

Permalink
Remove forward limit on known proxies. Convert IPs that are IPv4-Mapp…
Browse files Browse the repository at this point in the history
…ed-To-IPv6 to IPv4 format.
  • Loading branch information
bitbound committed Nov 13, 2020
1 parent e1d0499 commit 7769ed9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
14 changes: 12 additions & 2 deletions Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public Task<bool> DeviceCameOnline(Device device)
return Task.FromResult(false);
}

device.PublicIP = Context.GetHttpContext()?.Connection?.RemoteIpAddress?.ToString();
var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;
if (ip != null && ip.IsIPv4MappedToIPv6)
{
ip = ip.MapToIPv4();
}
device.PublicIP = ip?.ToString();

if (DataService.AddOrUpdateDevice(device, out var updatedDevice))
{
Expand Down Expand Up @@ -129,7 +134,12 @@ public Task<bool> DeviceCameOnline(Device device)

public Task DeviceHeartbeat(Device device)
{
device.PublicIP = Context.GetHttpContext()?.Connection?.RemoteIpAddress?.ToString();
var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;
if (ip != null && ip.IsIPv4MappedToIPv6)
{
ip = ip.MapToIPv4();
}
device.PublicIP = ip?.ToString();
DataService.AddOrUpdateDevice(device, out var updatedDevice);
Device = updatedDevice;

Expand Down
21 changes: 11 additions & 10 deletions Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Remotely.Shared.Models;
using System;
using System.IO;
using System.Linq;
using System.Net;

namespace Remotely.Server
Expand Down Expand Up @@ -93,17 +94,19 @@ public void ConfigureServices(IServiceCollection services)
}

var knownProxies = Configuration.GetSection("ApplicationOptions:KnownProxies").Get<string[]>();
if (knownProxies != null)
services.Configure<ForwardedHeadersOptions>(options =>
{
services.Configure<ForwardedHeadersOptions>(options =>
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.ForwardLimit = null;
if (knownProxies?.Any() == true)
{
foreach (var proxy in knownProxies)
{
options.KnownProxies.Add(IPAddress.Parse(proxy));
options.ForwardLimit = 2;
}
});
}
}
});

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0).AddJsonOptions(options =>
Expand Down Expand Up @@ -144,6 +147,9 @@ public void ConfigureServices(IServiceCollection services)
DataService dataService,
ILoggerFactory loggerFactory)
{

app.UseForwardedHeaders();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -164,11 +170,6 @@ public void ConfigureServices(IServiceCollection services)

ConfigureStaticFiles(app);

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseSwagger();

app.UseSwaggerUI(c =>
Expand Down

0 comments on commit 7769ed9

Please sign in to comment.