summary refs log tree commit diff
path: root/MxApiExtensions/Controllers/GenericProxyController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MxApiExtensions/Controllers/GenericProxyController.cs')
-rw-r--r--MxApiExtensions/Controllers/GenericProxyController.cs98
1 files changed, 77 insertions, 21 deletions
diff --git a/MxApiExtensions/Controllers/GenericProxyController.cs b/MxApiExtensions/Controllers/GenericProxyController.cs

index f0ad4e7..4e27b4a 100644 --- a/MxApiExtensions/Controllers/GenericProxyController.cs +++ b/MxApiExtensions/Controllers/GenericProxyController.cs
@@ -1,5 +1,6 @@ using System.Net.Http.Headers; using Microsoft.AspNetCore.Mvc; +using MxApiExtensions.Services; namespace MxApiExtensions.Controllers; @@ -7,21 +8,75 @@ namespace MxApiExtensions.Controllers; [Route("/{*_}")] public class GenericController : ControllerBase { private readonly ILogger<GenericController> _logger; - private readonly CacheConfiguration _config; - private readonly Auth _auth; + private readonly MxApiExtensionsConfiguration _config; + private readonly AuthenticationService _authenticationService; + private readonly AuthenticatedHomeserverProviderService _authenticatedHomeserverProviderService; private static Dictionary<string, string> _tokenMap = new(); - public GenericController(ILogger<GenericController> logger, CacheConfiguration config, Auth auth) { + public GenericController(ILogger<GenericController> logger, MxApiExtensionsConfiguration config, AuthenticationService authenticationService, + AuthenticatedHomeserverProviderService authenticatedHomeserverProviderService) { _logger = logger; _config = config; - _auth = auth; + _authenticationService = authenticationService; + _authenticatedHomeserverProviderService = authenticatedHomeserverProviderService; } [HttpGet] - public async Task Proxy([FromQuery] string? access_token, string _) { + public async Task Proxy([FromQuery] string? access_token, string? _) { try { - access_token ??= _auth.GetToken(fail: false); - var mxid = _auth.GetUserId(fail: false); + access_token ??= _authenticationService.GetToken(fail: false); + var mxid = await _authenticationService.GetMxidFromToken(fail: false); + var hs = await _authenticatedHomeserverProviderService.GetHomeserver(); + + _logger.LogInformation("Proxying request for {}: {}{}", mxid, Request.Path, Request.QueryString); + + //remove access_token from query string + Request.QueryString = new QueryString( + Request.QueryString.Value?.Replace("&access_token", "access_token") + .Replace($"access_token={access_token}", "") + ); + + var resp = await hs._httpClient.GetAsync($"{Request.Path}{Request.QueryString}"); + + if (resp.Content is null) { + throw new MxApiMatrixException { + ErrorCode = "M_UNKNOWN", + Error = "No content in response" + }; + } + + Response.StatusCode = (int)resp.StatusCode; + Response.ContentType = resp.Content.Headers.ContentType?.ToString() ?? "application/json"; + await Response.StartAsync(); + await using var stream = await resp.Content.ReadAsStreamAsync(); + await stream.CopyToAsync(Response.Body); + await Response.Body.FlushAsync(); + await Response.CompleteAsync(); + } + catch (MxApiMatrixException e) { + _logger.LogError(e, "Matrix error"); + Response.StatusCode = StatusCodes.Status500InternalServerError; + Response.ContentType = "application/json"; + + await Response.WriteAsync(e.GetAsJson()); + await Response.CompleteAsync(); + } + catch (Exception e) { + _logger.LogError(e, "Unhandled error"); + Response.StatusCode = StatusCodes.Status500InternalServerError; + Response.ContentType = "text/plain"; + + await Response.WriteAsync(e.ToString()); + await Response.CompleteAsync(); + } + } + + [HttpPost] + public async Task ProxyPost([FromQuery] string? access_token, string _) { + try { + access_token ??= _authenticationService.GetToken(fail: false); + var mxid = await _authenticationService.GetMxidFromToken(fail: false); + var hs = await _authenticatedHomeserverProviderService.GetHomeserver(); _logger.LogInformation("Proxying request for {}: {}{}", mxid, Request.Path, Request.QueryString); @@ -35,10 +90,13 @@ public class GenericController : ControllerBase { .Replace($"access_token={access_token}", "") ); - var resp = await hc.GetAsync($"{_config.Homeserver}{Request.Path}{Request.QueryString}"); + var resp = await hs._httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, $"{Request.Path}{Request.QueryString}") { + Method = HttpMethod.Post, + Content = new StreamContent(Request.Body) + }); if (resp.Content is null) { - throw new MatrixException { + throw new MxApiMatrixException { ErrorCode = "M_UNKNOWN", Error = "No content in response" }; @@ -51,9 +109,8 @@ public class GenericController : ControllerBase { await stream.CopyToAsync(Response.Body); await Response.Body.FlushAsync(); await Response.CompleteAsync(); - } - catch (MatrixException e) { + catch (MxApiMatrixException e) { _logger.LogError(e, "Matrix error"); Response.StatusCode = StatusCodes.Status500InternalServerError; Response.ContentType = "application/json"; @@ -71,11 +128,12 @@ public class GenericController : ControllerBase { } } - [HttpPost] - public async Task ProxyPost([FromQuery] string? access_token, string _) { + [HttpPut] + public async Task ProxyPut([FromQuery] string? access_token, string _) { try { - access_token ??= _auth.GetToken(fail: false); - var mxid = _auth.GetUserId(fail: false); + access_token ??= _authenticationService.GetToken(fail: false); + var mxid = await _authenticationService.GetMxidFromToken(fail: false); + var hs = await _authenticatedHomeserverProviderService.GetHomeserver(); _logger.LogInformation("Proxying request for {}: {}{}", mxid, Request.Path, Request.QueryString); @@ -89,14 +147,13 @@ public class GenericController : ControllerBase { .Replace($"access_token={access_token}", "") ); - var resp = await hc.SendAsync(new HttpRequestMessage { - Method = HttpMethod.Post, - RequestUri = new Uri($"{_config.Homeserver}{Request.Path}{Request.QueryString}"), + var resp = await hs._httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, $"{Request.Path}{Request.QueryString}") { + Method = HttpMethod.Put, Content = new StreamContent(Request.Body) }); if (resp.Content is null) { - throw new MatrixException { + throw new MxApiMatrixException { ErrorCode = "M_UNKNOWN", Error = "No content in response" }; @@ -109,9 +166,8 @@ public class GenericController : ControllerBase { await stream.CopyToAsync(Response.Body); await Response.Body.FlushAsync(); await Response.CompleteAsync(); - } - catch (MatrixException e) { + catch (MxApiMatrixException e) { _logger.LogError(e, "Matrix error"); Response.StatusCode = StatusCodes.Status500InternalServerError; Response.ContentType = "application/json";