about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-02-27 22:07:15 +0100
committerRory& <root@rory.gay>2024-02-27 22:07:15 +0100
commitebdd85e0f8145867e621e928e851537b5b42c335 (patch)
tree98cb63a93752d2daffd9454dd668b7589c72bd92
parentSystemd logger (diff)
downloadMatrixMediaGate-ebdd85e0f8145867e621e928e851537b5b42c335.tar.xz
Fix bug with URL encoding
-rw-r--r--MatrixMediaGate/Program.cs15
-rw-r--r--MatrixMediaGate/appsettings.Development.json2
2 files changed, 6 insertions, 11 deletions
diff --git a/MatrixMediaGate/Program.cs b/MatrixMediaGate/Program.cs

index ec4e755..658ef89 100644 --- a/MatrixMediaGate/Program.cs +++ b/MatrixMediaGate/Program.cs
@@ -1,13 +1,12 @@ -using System.Net; using System.Net.Http.Headers; using System.Text.Json; using MatrixMediaGate; using MatrixMediaGate.Services; +using Microsoft.AspNetCore.Http.Extensions; var builder = WebApplication.CreateBuilder(args); builder.Host.UseSystemd(); builder.Services.AddSingleton<ProxyConfiguration>(); -builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); builder.Services.AddSingleton<AuthValidator>(); builder.Services.AddSingleton<HttpClient>(services => { var cfg = services.GetRequiredService<ProxyConfiguration>(); @@ -20,11 +19,9 @@ builder.Services.AddSingleton<HttpClient>(services => { var app = builder.Build(); async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger<Program> logger) { - var path = ctx.Request.Path.Value; - if (path is null) return; + var path = ctx.Request.GetEncodedPathAndQuery(); if (path.StartsWith('/')) path = path[1..]; - path += ctx.Request.QueryString.Value; var method = new HttpMethod(ctx.Request.Method); using var req = new HttpRequestMessage(method, path); @@ -41,7 +38,7 @@ async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger if (ctx.Request.ContentLength != null) req.Content.Headers.ContentLength = ctx.Request.ContentLength; } - logger.LogInformation($"Proxying {method} {path} to {hc.BaseAddress}{path}"); + logger.LogInformation("Proxying {method} {path} to {target}", method, path, hc.BaseAddress + path); using var response = await hc.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); ctx.Response.Headers.Clear(); @@ -62,7 +59,6 @@ async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger async Task ProxyMaybeAuth(HttpClient hc, ProxyConfiguration cfg, AuthValidator auth, HttpContext ctx, ILogger<Program> logger) { await auth.UpdateAuth(ctx); - await Proxy(hc, cfg, ctx, logger); } @@ -75,8 +71,7 @@ async Task ProxyMedia(string serverName, ProxyConfiguration cfg, HttpClient hc, ctx.Response.StatusCode = 403; ctx.Response.ContentType = "application/json"; await ctx.Response.StartAsync(); - var json = JsonSerializer.Serialize(new { errcode = "M_FORBIDDEN", error = "Unauthenticated access to remote media has been disabled on this server." }); - await ctx.Response.WriteAsync(json); + await JsonSerializer.SerializeAsync(ctx.Response.Body, new { errcode = "M_FORBIDDEN", error = "Unauthenticated access to remote media has been disabled on this server." }); await ctx.Response.Body.FlushAsync(); await ctx.Response.CompleteAsync(); } @@ -92,4 +87,4 @@ foreach (var route in (string[]) [ // Require recent auth for these routes ]) app.Map(route, ProxyMedia); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/MatrixMediaGate/appsettings.Development.json b/MatrixMediaGate/appsettings.Development.json
index 2763a9a..56b83e6 100644 --- a/MatrixMediaGate/appsettings.Development.json +++ b/MatrixMediaGate/appsettings.Development.json
@@ -2,7 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Information", + "Microsoft.AspNetCore": "Warning", "Microsoft.AspNetCore.Routing": "Warning", "Microsoft.AspNetCore.Mvc": "Warning" }