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. 4
      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;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using AngleSharp.Html; using AngleSharp.Html;
using Microsoft.Extensions.Options;
namespace Ksp.WebServer.Controllers namespace Ksp.WebServer.Controllers
{ {
@ -20,23 +21,27 @@ namespace Ksp.WebServer.Controllers
{ {
private readonly ILogger<TasksController> logger; private readonly ILogger<TasksController> logger;
private readonly IWebHostEnvironment env; private readonly IWebHostEnvironment env;
private readonly KspProxyConfig kspProxyConfig;
public GrafikPageController( public GrafikPageController(
ILogger<TasksController> logger, ILogger<TasksController> logger,
IWebHostEnvironment env) IWebHostEnvironment env,
IOptions<KspProxyConfig> kspProxyConfig)
{ {
this.env = env; this.env = env;
this.kspProxyConfig = kspProxyConfig.Value;
this.logger = logger; this.logger = logger;
} }
async Task<string> FetchBlankPage() async Task<string> FetchBlankPage()
{ {
var c = new HttpClient(); 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("text/html"));
rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml"));
rq.Headers.Authorization = if (!string.IsNullOrEmpty(kspProxyConfig.Authorization))
new AuthenticationHeaderValue("Basic", "SECRET"); rq.Headers.Authorization =
AuthenticationHeaderValue.Parse(kspProxyConfig.Authorization);
if (HttpContext.Request.Headers.TryGetValue("Cookie", out var x)) if (HttpContext.Request.Headers.TryGetValue("Cookie", out var x))
rq.Headers.Add("Cookie", x.AsEnumerable()); rq.Headers.Add("Cookie", x.AsEnumerable());
var rs = await c.SendAsync(rq); 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", "$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:45302",
"sslPort": 44318
}
},
"profiles": { "profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "index.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Ksp.WebServer": { "Ksp.WebServer": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,

31
server/Ksp.WebServer/Startup.cs

@ -18,6 +18,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Ksp.WebServer namespace Ksp.WebServer
{ {
@ -42,11 +43,14 @@ namespace Ksp.WebServer
}; };
}); });
services.AddProxies(); 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. // 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.UseDeveloperExceptionPage();
app.UseRouting(); app.UseRouting();
@ -70,35 +74,32 @@ namespace Ksp.WebServer
app.RunProxy(proxy => proxy app.RunProxy(proxy => proxy
.UseHttp((context, args) => .UseHttp((context, args) =>
{ {
return "https://ksp-test.ks.matfyz.cz"; return kspProxyConfig.Value.Host;
}, opt => { }, opt => {
var baseUri = new Uri(kspProxyConfig.Value.Host);
opt.WithHttpClientName("RedirectClient"); opt.WithHttpClientName("RedirectClient");
opt.WithBeforeSend((cx, request) => { opt.WithBeforeSend((cx, request) => {
if (request.Headers.Authorization is null) if (request.Headers.Authorization is null && !string.IsNullOrEmpty(kspProxyConfig.Value.Authorization))
{ {
request.Headers.Authorization = request.Headers.Authorization =
new AuthenticationHeaderValue("Basic", "SECRET"); AuthenticationHeaderValue.Parse(kspProxyConfig.Value.Authorization);
} }
if (request.Headers.Referrer is object) if (request.Headers.Referrer is object)
{
request.Headers.Referrer = request.Headers.Referrer =
new UriBuilder(request.Headers.Referrer) { new UriBuilder(request.Headers.Referrer) {
Host = "ksp-test.ks.matfyz.cz", Host = baseUri.Host,
Port = 443, Port = baseUri.Port,
Scheme = "https" Scheme = baseUri.Scheme
}.Uri; }.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); // Console.WriteLine(request);
return Task.CompletedTask; return Task.CompletedTask;
}); });
opt.WithAfterReceive((cx, response) => { opt.WithAfterReceive((cx, response) => {
// Console.WriteLine(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) { response.Headers.Location = new UriBuilder(response.Headers.Location) {
Host = cx.Request.Host.Host, Host = cx.Request.Host.Host,
@ -111,7 +112,7 @@ namespace Ksp.WebServer
response.Headers.Remove("Set-Cookie"); response.Headers.Remove("Set-Cookie");
response.Headers.Add("Set-Cookie", v.Select(s => response.Headers.Add("Set-Cookie", v.Select(s =>
s.Replace("; secure", "") 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; return Task.CompletedTask;

4
server/Ksp.WebServer/appsettings.Development.json

@ -5,5 +5,9 @@
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" "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" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"KspProxyConfig": {
"Host": "https://ksp.mff.cuni.cz",
"Authorization": null
}
} }