summary refs log tree commit diff
path: root/MxApiExtensions/Controllers/Other/GenericProxyController.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-11-05 17:59:38 +0100
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-11-05 17:59:38 +0100
commit2abb132234546e61bb0aff3897dc49e72ea84f5d (patch)
treec885c03d35e7a0a6b8fc21bd0b259216c61c877c /MxApiExtensions/Controllers/Other/GenericProxyController.cs
parentUpdate (diff)
downloadMxApiExtensions-2abb132234546e61bb0aff3897dc49e72ea84f5d.tar.xz
Working sync proxy
Diffstat (limited to 'MxApiExtensions/Controllers/Other/GenericProxyController.cs')
-rw-r--r--MxApiExtensions/Controllers/Other/GenericProxyController.cs176
1 files changed, 176 insertions, 0 deletions
diff --git a/MxApiExtensions/Controllers/Other/GenericProxyController.cs b/MxApiExtensions/Controllers/Other/GenericProxyController.cs
new file mode 100644

index 0000000..bae07c0 --- /dev/null +++ b/MxApiExtensions/Controllers/Other/GenericProxyController.cs
@@ -0,0 +1,176 @@ +using System.Net.Http.Headers; +using Microsoft.AspNetCore.Mvc; +using MxApiExtensions.Classes.LibMatrix; +using MxApiExtensions.Services; + +namespace MxApiExtensions.Controllers; + +[ApiController] +[Route("/{*_}")] +public class GenericController(ILogger<GenericController> logger, MxApiExtensionsConfiguration config, AuthenticationService authenticationService, + AuthenticatedHomeserverProviderService authenticatedHomeserverProviderService) + : ControllerBase { + [HttpGet] + public async Task Proxy([FromQuery] string? access_token, string? _) { + try { + // access_token ??= _authenticationService.GetToken(fail: false); + // var mxid = await _authenticationService.GetMxidFromToken(fail: false); + var hs = await authenticatedHomeserverProviderService.GetRemoteHomeserver(); + + logger.LogInformation("Proxying request: {}{}", 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.ClientHttpClient.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); + + using var hc = new HttpClient(); + hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token); + hc.Timeout = TimeSpan.FromMinutes(10); + //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.ClientHttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, $"{Request.Path}{Request.QueryString}") { + Method = HttpMethod.Post, + Content = new StreamContent(Request.Body) + }); + + 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.WriteAsJsonAsync(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(); + } + } + + [HttpPut] + public async Task ProxyPut([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); + + using var hc = new HttpClient(); + hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token); + hc.Timeout = TimeSpan.FromMinutes(10); + //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.ClientHttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Put, $"{Request.Path}{Request.QueryString}") { + Method = HttpMethod.Put, + Content = new StreamContent(Request.Body) + }); + + 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.WriteAsJsonAsync(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(); + } + } +}