about summary refs log tree commit diff
path: root/MatrixMediaGate/Program.cs
blob: 19ae4f8efdd0d650af9083b84611807af3f093c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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<AuthValidator>();
builder.Services.AddSingleton<HttpClient>(services => {
    var cfg = services.GetRequiredService<ProxyConfiguration>();
    return new HttpClient() {
        BaseAddress = new Uri(cfg.Upstream),
        MaxResponseContentBufferSize = 1 * 1024 * 1024 // 1MB
    };
});

var app = builder.Build();

app.Map("{*_}", ProxyMaybeAuth);
app.Map("/_matrix/federation/{*_}", Proxy); // Don't bother with auth for federation

foreach (var route in (string[]) [ // Require recent auth for these routes
             "/_matrix/media/{version}/download/{serverName}/{mediaId}",
             "/_matrix/media/{version}/download/{serverName}/{mediaId}/{fileName}",
             "/_matrix/media/{version}/thumbnail/{serverName}/{mediaId}",
         ])
    app.Map(route, ProxyMedia);

app.Run();

// Proxy a request
async Task Proxy(HttpClient hc, ProxyConfiguration cfg, HttpContext ctx, ILogger<Program> logger) {
    HttpRequestMessage? upstreamRequest = null;
    HttpResponseMessage? upstreamResponse = null;
    Exception? exception = null;
    try {
        var path = ctx.Request.GetEncodedPathAndQuery();
        if (path.StartsWith('/'))
            path = path[1..];

        var method = new HttpMethod(ctx.Request.Method);
        upstreamRequest = new HttpRequestMessage(method, path);
        hc.DefaultRequestHeaders.Clear();
        upstreamRequest.Headers.Clear();
        foreach (var header in ctx.Request.Headers) {
            if (header.Key != "Accept-Encoding" && header.Key != "Content-Type" && header.Key != "Content-Length") {
                // req.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
                upstreamRequest.Headers.Remove(header.Key);
                upstreamRequest.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
            }
        }

        upstreamRequest.Headers.Host = cfg.Host;

        if (ctx.Request.ContentLength > 0) {
            upstreamRequest.Content = new StreamContent(ctx.Request.Body);
            if (ctx.Request.ContentType != null) upstreamRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(ctx.Request.ContentType);

            if (ctx.Request.ContentLength != null) upstreamRequest.Content.Headers.ContentLength = ctx.Request.ContentLength;
        }

        logger.LogInformation("Proxying {method} {path} to {target}", method, path, hc.BaseAddress + path);

        upstreamResponse = await hc.SendAsync(upstreamRequest, HttpCompletionOption.ResponseHeadersRead);
        ctx.Response.Headers.Clear();
        foreach (var header in upstreamResponse.Headers) {
            if (header.Key != "Transfer-Encoding")
                ctx.Response.Headers[header.Key] = header.Value.ToArray();
        }

        ctx.Response.StatusCode = (int)upstreamResponse.StatusCode;
        ctx.Response.ContentType = upstreamResponse.Content.Headers.ContentType?.ToString() ?? "application/json";
        if (upstreamResponse.Content.Headers.ContentLength != null) ctx.Response.ContentLength = upstreamResponse.Content.Headers.ContentLength;
        await ctx.Response.StartAsync();
        await using var content = await upstreamResponse.Content.ReadAsStreamAsync();
        await content.CopyToAsync(ctx.Response.Body);
    }
    catch (HttpRequestException e) {
        exception = e;
        logger.LogError(e, "Failed to proxy request");
        ctx.Response.StatusCode = 502;
        ctx.Response.ContentType = "application/json";
        await ctx.Response.StartAsync();
        await JsonSerializer.SerializeAsync(ctx.Response.Body, new { errcode = "M_UNAVAILABLE", error = "Failed to proxy request" });
    }
    finally {
        await ctx.Response.Body.FlushAsync();
        await ctx.Response.CompleteAsync();
        if (ctx.Response.StatusCode >= 400) {
            await ProxyDump(cfg, ctx, upstreamRequest, upstreamResponse, exception);
        }
        
        upstreamRequest?.Dispose();
        upstreamResponse?.Dispose();
    }

}

// We attempt to update auth, but we don't require it
async Task ProxyMaybeAuth(HttpClient hc, ProxyConfiguration cfg, AuthValidator auth, HttpContext ctx, ILogger<Program> logger) {
    await auth.UpdateAuth(ctx);
    await Proxy(hc, cfg, ctx, logger);
}

// We know this is a media path, we require recent auth here to prevent abuse
async Task ProxyMedia(string serverName, ProxyConfiguration cfg, HttpClient hc, AuthValidator auth, HttpContext ctx, ILogger<Program> logger) {
    // Some clients may send Authorization header, so we handle this last...
    if (cfg.TrustedServers.Contains(serverName) || auth.ValidateAuth(ctx) || await auth.UpdateAuth(ctx)) {
        await Proxy(hc, cfg, ctx, logger);
    }
    else {
        ctx.Response.StatusCode = 403;
        ctx.Response.ContentType = "application/json";
        await ctx.Response.StartAsync();
        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();
    }
}

var jsonOptions = new JsonSerializerOptions {
    WriteIndented = true
};
// We dump failed requests to disk
async Task ProxyDump(ProxyConfiguration cfg, HttpContext ctx, HttpRequestMessage? req, HttpResponseMessage? resp, Exception? e) {
    if (ctx.Response.StatusCode >= 400 && cfg.DumpFailedRequests) {
        var path = Path.Combine(cfg.DumpPath, "failed_requests", $"{resp.StatusCode}-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}-{ctx.Request.Path}.json");
        await using var file = File.Create(path);
        await JsonSerializer.SerializeAsync(file, new {
            Self = new {
                Request = new {
                    ctx.Request.Method,
                    Url = ctx.Request.GetEncodedUrl(),
                    ctx.Request.Headers
                },
                Response = new {
                    ctx.Response.StatusCode,
                    ctx.Response.Headers,
                    ctx.Response.ContentType,
                    ctx.Response.ContentLength
                }
            },
            Upstream = new {
                Request = new {
                    req?.Method,
                    Url = req?.RequestUri,
                    req?.Headers
                },
                Response = new {
                    resp.StatusCode,
                    resp.Headers,
                    resp.Content.Headers.ContentType,
                    resp.Content.Headers.ContentLength
                }
            },
            Exception = new {
                Type = e?.GetType().ToString(),
                Message = e?.Message.ReplaceLineEndings().Split(Environment.NewLine),
                StackTrace = e?.StackTrace?.ReplaceLineEndings().Split(Environment.NewLine)
            }
        });
    }
}