This repository has been archived on 2021-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
graf-uloh/server/Ksp.WebServer/Startup.cs

134 lines
5.6 KiB
C#
Raw Normal View History

2020-09-27 11:10:29 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
2020-10-17 19:56:40 +02:00
using System.Text;
2020-09-27 11:10:29 +02:00
using System.Threading.Tasks;
using AspNetCore.Proxy;
using AspNetCore.Proxy.Builders;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
2020-09-27 11:10:29 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2020-09-27 11:10:29 +02:00
namespace Ksp.WebServer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient("RedirectClient")
.ConfigurePrimaryHttpMessageHandler(h => {
return new HttpClientHandler {
AllowAutoRedirect = false,
2020-10-17 19:56:40 +02:00
UseCookies = false,
AutomaticDecompression = DecompressionMethods.All
2020-09-27 11:10:29 +02:00
};
});
services.AddProxies();
services.Configure<KspProxyConfig>(Configuration.GetSection(nameof(KspProxyConfig)));
2020-10-17 19:56:40 +02:00
services.AddSingleton<KspPageRewriter>();
services.AddSingleton<KspAuthenticator>();
2020-09-27 11:10:29 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2020-10-17 19:56:40 +02:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<KspProxyConfig> kspProxyConfig, KspPageRewriter pageRewriter)
2020-09-27 11:10:29 +02:00
{
Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}");
2020-10-17 18:26:39 +02:00
app.UseDeveloperExceptionPage();
2020-09-27 11:10:29 +02:00
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseRewriter(new RewriteOptions()
.AddRewrite("^grafik$", "grafik.html", true)
);
2020-09-27 11:10:29 +02:00
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "../../frontend/public")),
});
app.RunProxy(proxy => proxy
.UseHttp((context, args) =>
{
return kspProxyConfig.Value.Host;
2020-09-27 11:10:29 +02:00
}, opt => {
var baseUri = new Uri(kspProxyConfig.Value.Host);
2020-09-27 11:10:29 +02:00
opt.WithHttpClientName("RedirectClient");
opt.WithBeforeSend((cx, request) => {
if (request.Headers.Authorization is null && !string.IsNullOrEmpty(kspProxyConfig.Value.Authorization))
2020-10-04 00:18:24 +02:00
{
request.Headers.Authorization =
AuthenticationHeaderValue.Parse(kspProxyConfig.Value.Authorization);
2020-10-04 00:18:24 +02:00
}
2020-09-27 11:10:29 +02:00
if (request.Headers.Referrer is object)
{
2020-09-27 11:10:29 +02:00
request.Headers.Referrer =
new UriBuilder(request.Headers.Referrer) {
Host = baseUri.Host,
Port = baseUri.Port,
Scheme = baseUri.Scheme
2020-09-27 11:10:29 +02:00
}.Uri;
}
// Console.WriteLine(request);
return Task.CompletedTask;
2020-09-27 11:10:29 +02:00
});
2020-10-17 19:56:40 +02:00
opt.WithAfterReceive(async (cx, response) => {
// Console.WriteLine(response);
if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host)
2020-09-27 11:10:29 +02:00
{
response.Headers.Location = new UriBuilder(response.Headers.Location) {
Host = cx.Request.Host.Host,
2020-09-30 17:55:58 +02:00
Port = cx.Request.Host.Port ?? (cx.Request.Scheme == "https" ? 443 : 80),
2020-09-27 11:10:29 +02:00
Scheme = cx.Request.Scheme
}.Uri;
}
if (response.Headers.TryGetValues("Set-Cookie", out var v))
{
response.Headers.Remove("Set-Cookie");
response.Headers.Add("Set-Cookie", v.Select(s =>
s.Replace("; secure", "")
.Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}")
2020-09-27 11:10:29 +02:00
));
}
2020-10-17 19:56:40 +02:00
2020-10-17 21:13:18 +02:00
if (new [] { "text/html", "application/xhtml+xml" }.Contains(response.Content?.Headers?.ContentType?.MediaType))
2020-10-17 19:56:40 +02:00
{
var str = await response.Content.ReadAsStringAsync();
str = pageRewriter.RewriteHtml(str, cx);
response.Content = new StringContent(str, Encoding.UTF8, "text/html");
}
2020-09-27 11:10:29 +02:00
});
}));
}
}
}