Added IP check on loopback
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using LightlessSync.API.Routes;
|
using LightlessSync.API.Routes;
|
||||||
using LightlessSyncAuthService.Services;
|
using LightlessSyncAuthService.Services;
|
||||||
|
using LightlessSyncAuthService.Utils;
|
||||||
using LightlessSyncShared;
|
using LightlessSyncShared;
|
||||||
using LightlessSyncShared.Data;
|
using LightlessSyncShared.Data;
|
||||||
using LightlessSyncShared.Services;
|
using LightlessSyncShared.Services;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using LightlessSync.API.Routes;
|
using LightlessSync.API.Routes;
|
||||||
using LightlessSyncAuthService.Services;
|
using LightlessSyncAuthService.Services;
|
||||||
|
using LightlessSyncAuthService.Utils;
|
||||||
using LightlessSyncShared;
|
using LightlessSyncShared;
|
||||||
using LightlessSyncShared.Data;
|
using LightlessSyncShared.Data;
|
||||||
using LightlessSyncShared.Services;
|
using LightlessSyncShared.Services;
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using LightlessSyncShared;
|
using LightlessSyncAuthService.Utils;
|
||||||
|
using LightlessSyncShared;
|
||||||
using LightlessSyncShared.Services;
|
using LightlessSyncShared.Services;
|
||||||
using LightlessSyncShared.Utils.Configuration;
|
using LightlessSyncShared.Utils.Configuration;
|
||||||
using MaxMind.GeoIP2;
|
using MaxMind.GeoIP2;
|
||||||
using MaxMind.GeoIP2.Model;
|
using MaxMind.GeoIP2.Model;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.Net;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace LightlessSyncAuthService.Services;
|
namespace LightlessSyncAuthService.Services;
|
||||||
@@ -35,7 +37,9 @@ public class GeoIPService : IHostedService
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ip = httpContextAccessor.GetIpAddress();
|
var ip = httpContextAccessor.GetClientIpAddress();
|
||||||
|
if (ip is null || IPAddress.IsLoopback(ip))
|
||||||
|
return "*";
|
||||||
|
|
||||||
using CancellationTokenSource waitCts = new();
|
using CancellationTokenSource waitCts = new();
|
||||||
waitCts.CancelAfter(TimeSpan.FromSeconds(5));
|
waitCts.CancelAfter(TimeSpan.FromSeconds(5));
|
||||||
@@ -148,33 +152,29 @@ public class GeoIPService : IHostedService
|
|||||||
internal async Task<string> GetCountryFromIP(IHttpContextAccessor httpContextAccessor)
|
internal async Task<string> GetCountryFromIP(IHttpContextAccessor httpContextAccessor)
|
||||||
{
|
{
|
||||||
if (!_useGeoIP)
|
if (!_useGeoIP)
|
||||||
{
|
|
||||||
return "*";
|
return "*";
|
||||||
}
|
|
||||||
|
var ip = httpContextAccessor.GetClientIpAddress();
|
||||||
|
if (ip is null || IPAddress.IsLoopback(ip))
|
||||||
|
return "*";
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ip = httpContextAccessor.GetIpAddress();
|
using CancellationTokenSource waitCts = new(TimeSpan.FromSeconds(5));
|
||||||
|
while (_processingReload)
|
||||||
using CancellationTokenSource waitCts = new();
|
await Task.Delay(100, waitCts.Token).ConfigureAwait(false);
|
||||||
waitCts.CancelAfter(TimeSpan.FromSeconds(5));
|
|
||||||
while (_processingReload) await Task.Delay(100, waitCts.Token).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (_dbReader!.TryCity(ip, out var response))
|
if (_dbReader!.TryCity(ip, out var response))
|
||||||
{
|
{
|
||||||
string? country = response?.Country.IsoCode;
|
var country = response?.Country?.IsoCode;
|
||||||
|
return !string.IsNullOrEmpty(country) ? country : "*";
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(country)
|
|
||||||
&& response?.Location.Longitude != null)
|
|
||||||
{
|
|
||||||
return country;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "*";
|
return "*";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "Error handling Geo IP country in request");
|
_logger.LogError(ex, "GeoIP lookup failed for {Ip}", ip);
|
||||||
return "*";
|
return "*";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace LightlessSyncAuthService.Utils
|
||||||
|
{
|
||||||
|
public static class HttpContextAccessorExtensions
|
||||||
|
{
|
||||||
|
public static IPAddress? GetClientIpAddress(this IHttpContextAccessor accessor)
|
||||||
|
{
|
||||||
|
var context = accessor.HttpContext;
|
||||||
|
if (context == null) return null;
|
||||||
|
|
||||||
|
string[] headerKeys = { "CF-Connecting-IP", "X-Forwarded-For", "X-Real-IP" };
|
||||||
|
foreach (var key in headerKeys)
|
||||||
|
{
|
||||||
|
if (context.Request.Headers.TryGetValue(key, out var values))
|
||||||
|
{
|
||||||
|
var ipCandidate = values.FirstOrDefault()?.Split(',').FirstOrDefault()?.Trim();
|
||||||
|
if (IPAddress.TryParse(ipCandidate, out var parsed))
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.Connection?.RemoteIpAddress;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user