OneDrive Backup
OneDrive Backup
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using hassio_onedrive_backup;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using onedrive_backup.Hass;
|
||||
|
||||
namespace onedrive_backup.Middleware
|
||||
{
|
||||
public class ExtractHassUrlMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private HassContext _ingressSettings;
|
||||
private IHostEnvironment _env;
|
||||
|
||||
public ExtractHassUrlMiddleware(RequestDelegate next, HassContext ingressSettings, IHostEnvironment env)
|
||||
{
|
||||
_next = next;
|
||||
_ingressSettings = ingressSettings;
|
||||
_env = env;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
const string HeaderKeyName = "X-Ingress-Path";
|
||||
if (context.Request.Headers.TryGetValue(HeaderKeyName, out StringValues headerValue))
|
||||
{
|
||||
_ingressSettings.HeaderIngressPath = headerValue.ToString();
|
||||
ConsoleLogger.LogInfo($"X-Ingress-Path: {_ingressSettings.HeaderIngressPath}");
|
||||
}
|
||||
else if (_env.IsDevelopment())
|
||||
{
|
||||
_ingressSettings.HeaderIngressPath = "/";
|
||||
}
|
||||
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using hassio_onedrive_backup;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace onedrive_backup.Middleware
|
||||
{
|
||||
public class IncomingHassFirewallMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IHostEnvironment _env;
|
||||
private const string _allowedIP = "172.30.32.2";
|
||||
|
||||
public IncomingHassFirewallMiddleware(RequestDelegate next, IHostEnvironment env)
|
||||
{
|
||||
_next = next;
|
||||
_env = env;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var ip = context.Connection.RemoteIpAddress.MapToIPv4();
|
||||
ConsoleLogger.LogVerbose($"Source IP: {ip}");
|
||||
if (_env.IsDevelopment() ==false && ip.ToString().Equals(_allowedIP, StringComparison.OrdinalIgnoreCase) == false)
|
||||
{
|
||||
ConsoleLogger.LogError($"Blocking request from unauthorized source IP : {ip.ToString()}");
|
||||
}
|
||||
|
||||
// await context.Response.WriteAsync($"Request origin ({ip}) blocked.");
|
||||
await _next(context);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user