diff --git a/server/Ksp.WebServer/Controllers/GrafikPageController.cs b/server/Ksp.WebServer/Controllers/GrafikPageController.cs index 99a5d4a..cca73d2 100644 --- a/server/Ksp.WebServer/Controllers/GrafikPageController.cs +++ b/server/Ksp.WebServer/Controllers/GrafikPageController.cs @@ -11,6 +11,7 @@ using AngleSharp.Dom; using System.Net.Http; using System.Net.Http.Headers; using AngleSharp.Html; +using Microsoft.Extensions.Options; namespace Ksp.WebServer.Controllers { @@ -20,23 +21,27 @@ namespace Ksp.WebServer.Controllers { private readonly ILogger logger; private readonly IWebHostEnvironment env; + private readonly KspProxyConfig kspProxyConfig; public GrafikPageController( ILogger logger, - IWebHostEnvironment env) + IWebHostEnvironment env, + IOptions kspProxyConfig) { this.env = env; + this.kspProxyConfig = kspProxyConfig.Value; this.logger = logger; } async Task FetchBlankPage() { var c = new HttpClient(); - var rq = new HttpRequestMessage(HttpMethod.Get, "https://ksp-test.ks.matfyz.cz/blank"); + var rq = new HttpRequestMessage(HttpMethod.Get, $"{kspProxyConfig.Host}/blank"); rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); - rq.Headers.Authorization = - new AuthenticationHeaderValue("Basic", "SECRET"); + if (!string.IsNullOrEmpty(kspProxyConfig.Authorization)) + rq.Headers.Authorization = + AuthenticationHeaderValue.Parse(kspProxyConfig.Authorization); if (HttpContext.Request.Headers.TryGetValue("Cookie", out var x)) rq.Headers.Add("Cookie", x.AsEnumerable()); var rs = await c.SendAsync(rq); diff --git a/server/Ksp.WebServer/KspProxyConfig.cs b/server/Ksp.WebServer/KspProxyConfig.cs new file mode 100644 index 0000000..8b590af --- /dev/null +++ b/server/Ksp.WebServer/KspProxyConfig.cs @@ -0,0 +1,8 @@ +namespace Ksp.WebServer +{ + public class KspProxyConfig + { + public string Host { get; set; } + public string Authorization { get; set; } + } +} diff --git a/server/Ksp.WebServer/Properties/launchSettings.json b/server/Ksp.WebServer/Properties/launchSettings.json index c6f9a41..f219997 100644 --- a/server/Ksp.WebServer/Properties/launchSettings.json +++ b/server/Ksp.WebServer/Properties/launchSettings.json @@ -1,22 +1,6 @@ { "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:45302", - "sslPort": 44318 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "index.html", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, "Ksp.WebServer": { "commandName": "Project", "launchBrowser": true, diff --git a/server/Ksp.WebServer/Startup.cs b/server/Ksp.WebServer/Startup.cs index d696567..e944f4b 100644 --- a/server/Ksp.WebServer/Startup.cs +++ b/server/Ksp.WebServer/Startup.cs @@ -18,6 +18,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace Ksp.WebServer { @@ -42,11 +43,14 @@ namespace Ksp.WebServer }; }); services.AddProxies(); + services.Configure(Configuration.GetSection(nameof(KspProxyConfig))); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions kspProxyConfig) { + Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}"); + app.UseDeveloperExceptionPage(); app.UseRouting(); @@ -70,35 +74,32 @@ namespace Ksp.WebServer app.RunProxy(proxy => proxy .UseHttp((context, args) => { - return "https://ksp-test.ks.matfyz.cz"; + return kspProxyConfig.Value.Host; }, opt => { + var baseUri = new Uri(kspProxyConfig.Value.Host); opt.WithHttpClientName("RedirectClient"); opt.WithBeforeSend((cx, request) => { - if (request.Headers.Authorization is null) + if (request.Headers.Authorization is null && !string.IsNullOrEmpty(kspProxyConfig.Value.Authorization)) { request.Headers.Authorization = - new AuthenticationHeaderValue("Basic", "SECRET"); + AuthenticationHeaderValue.Parse(kspProxyConfig.Value.Authorization); } if (request.Headers.Referrer is object) + { request.Headers.Referrer = new UriBuilder(request.Headers.Referrer) { - Host = "ksp-test.ks.matfyz.cz", - Port = 443, - Scheme = "https" + Host = baseUri.Host, + Port = baseUri.Port, + Scheme = baseUri.Scheme }.Uri; - // request.Headers.Remove("X-Forwarded-For"); - // request.Headers.Remove("X-Forwarded-Proto"); - // request.Headers.Remove("X-Forwarded-Host"); - // request.Headers.Remove("Forwarded"); - // request.Headers.Remove("Origin"); - // request.Headers.Add("Origin", "https://ksp-test.ks.matfyz.cz"); + } // Console.WriteLine(request); return Task.CompletedTask; }); opt.WithAfterReceive((cx, response) => { // Console.WriteLine(response); - if (response.Headers.Location is object && response.Headers.Location.Host == "ksp-test.ks.matfyz.cz") + if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host) { response.Headers.Location = new UriBuilder(response.Headers.Location) { Host = cx.Request.Host.Host, @@ -111,7 +112,7 @@ namespace Ksp.WebServer response.Headers.Remove("Set-Cookie"); response.Headers.Add("Set-Cookie", v.Select(s => s.Replace("; secure", "") - .Replace("; domain=ksp-test.ks.matfyz.cz", $"; domain={cx.Request.Host.Host}") + .Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}") )); } return Task.CompletedTask; diff --git a/server/Ksp.WebServer/appsettings.Development.json b/server/Ksp.WebServer/appsettings.Development.json index dba68eb..17f4b03 100644 --- a/server/Ksp.WebServer/appsettings.Development.json +++ b/server/Ksp.WebServer/appsettings.Development.json @@ -1,9 +1,13 @@ { "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } + }, + "KspProxyConfig": { + "Host": "https://ksp-test.ks.matfyz.cz", + "Authorization": "Basic SECRET" } } diff --git a/server/Ksp.WebServer/appsettings.json b/server/Ksp.WebServer/appsettings.json index 81ff877..30ea8e7 100644 --- a/server/Ksp.WebServer/appsettings.json +++ b/server/Ksp.WebServer/appsettings.json @@ -6,5 +6,9 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "KspProxyConfig": { + "Host": "https://ksp.mff.cuni.cz", + "Authorization": null + } }