Browse Source

Run agains production ksp.mff.cuni.cz

(switchable in appsettings)
mj-deploy
Standa Lukeš 4 years ago
parent
commit
a16a2f25bd
  1. 13
      server/Ksp.WebServer/Controllers/GrafikPageController.cs
  2. 8
      server/Ksp.WebServer/KspProxyConfig.cs
  3. 16
      server/Ksp.WebServer/Properties/launchSettings.json
  4. 31
      server/Ksp.WebServer/Startup.cs
  5. 10
      server/Ksp.WebServer/appsettings.Development.json
  6. 6
      server/Ksp.WebServer/appsettings.json

13
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<TasksController> logger;
private readonly IWebHostEnvironment env;
private readonly KspProxyConfig kspProxyConfig;
public GrafikPageController(
ILogger<TasksController> logger,
IWebHostEnvironment env)
IWebHostEnvironment env,
IOptions<KspProxyConfig> kspProxyConfig)
{
this.env = env;
this.kspProxyConfig = kspProxyConfig.Value;
this.logger = logger;
}
async Task<string> 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);

8
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; }
}
}

16
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,

31
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<KspProxyConfig>(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> 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;

10
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"
}
}

6
server/Ksp.WebServer/appsettings.json

@ -6,5 +6,9 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"KspProxyConfig": {
"Host": "https://ksp.mff.cuni.cz",
"Authorization": null
}
}