From 8ba2cf383d8a3b74900f3f0800a5bca3436b9ab9 Mon Sep 17 00:00:00 2001 From: exyi Date: Sun, 27 Sep 2020 09:10:29 +0000 Subject: [PATCH] Add hack proxy project --- server/Ksp.WebServer/.gitignore | 2 + .../Controllers/WeatherForecastController.cs | 39 ++++++ server/Ksp.WebServer/Ksp.WebServer.csproj | 12 ++ server/Ksp.WebServer/Program.cs | 26 ++++ .../Properties/launchSettings.json | 30 +++++ server/Ksp.WebServer/Startup.cs | 119 ++++++++++++++++++ server/Ksp.WebServer/WeatherForecast.cs | 15 +++ .../appsettings.Development.json | 9 ++ server/Ksp.WebServer/appsettings.json | 10 ++ 9 files changed, 262 insertions(+) create mode 100644 server/Ksp.WebServer/.gitignore create mode 100644 server/Ksp.WebServer/Controllers/WeatherForecastController.cs create mode 100644 server/Ksp.WebServer/Ksp.WebServer.csproj create mode 100644 server/Ksp.WebServer/Program.cs create mode 100644 server/Ksp.WebServer/Properties/launchSettings.json create mode 100644 server/Ksp.WebServer/Startup.cs create mode 100644 server/Ksp.WebServer/WeatherForecast.cs create mode 100644 server/Ksp.WebServer/appsettings.Development.json create mode 100644 server/Ksp.WebServer/appsettings.json diff --git a/server/Ksp.WebServer/.gitignore b/server/Ksp.WebServer/.gitignore new file mode 100644 index 0000000..1746e32 --- /dev/null +++ b/server/Ksp.WebServer/.gitignore @@ -0,0 +1,2 @@ +bin +obj diff --git a/server/Ksp.WebServer/Controllers/WeatherForecastController.cs b/server/Ksp.WebServer/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..6f4276f --- /dev/null +++ b/server/Ksp.WebServer/Controllers/WeatherForecastController.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace Ksp.WebServer.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/server/Ksp.WebServer/Ksp.WebServer.csproj b/server/Ksp.WebServer/Ksp.WebServer.csproj new file mode 100644 index 0000000..b4ad4b5 --- /dev/null +++ b/server/Ksp.WebServer/Ksp.WebServer.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/server/Ksp.WebServer/Program.cs b/server/Ksp.WebServer/Program.cs new file mode 100644 index 0000000..4893692 --- /dev/null +++ b/server/Ksp.WebServer/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Ksp.WebServer +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/server/Ksp.WebServer/Properties/launchSettings.json b/server/Ksp.WebServer/Properties/launchSettings.json new file mode 100644 index 0000000..c6f9a41 --- /dev/null +++ b/server/Ksp.WebServer/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$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, + "launchUrl": "index.html", + "applicationUrl": "https://ksp.localhost:5001;http://ksp.localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/server/Ksp.WebServer/Startup.cs b/server/Ksp.WebServer/Startup.cs new file mode 100644 index 0000000..20552e8 --- /dev/null +++ b/server/Ksp.WebServer/Startup.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +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.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +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, + UseCookies = false + }; + }); + services.AddProxies(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseHttpsRedirection(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + + app.UseStaticFiles(new StaticFileOptions { + FileProvider = new PhysicalFileProvider( + Path.Combine(env.ContentRootPath, "../../frontend/public")), + }); + + app.RunProxy(proxy => proxy + .UseHttp((context, args) => + { + return "https://ksp-test.ks.matfyz.cz"; + }, opt => { + opt.WithHttpClientName("RedirectClient"); + + opt.WithBeforeSend(async (cx, request) => { + request.Headers.Authorization = + new AuthenticationHeaderValue("Basic", "SECRET"); + if (request.Headers.Referrer is object) + request.Headers.Referrer = + new UriBuilder(request.Headers.Referrer) { + Host = "ksp-test.ks.matfyz.cz", + Port = 443, + Scheme = "https" + }.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); + }); + opt.WithAfterReceive(async (cx, response) => { + Console.WriteLine(response); + if (response.Headers.Location is object && response.Headers.Location.Host == "ksp-test.ks.matfyz.cz") + { + response.Headers.Location = new UriBuilder(response.Headers.Location) { + Host = cx.Request.Host.Host, + Port = cx.Request.Host.Port.Value, + 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=ksp-test.ks.matfyz.cz", $"; domain={cx.Request.Host.Host}") + )); + } + }); + })); + } + } +} diff --git a/server/Ksp.WebServer/WeatherForecast.cs b/server/Ksp.WebServer/WeatherForecast.cs new file mode 100644 index 0000000..39dfce4 --- /dev/null +++ b/server/Ksp.WebServer/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace Ksp.WebServer +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/server/Ksp.WebServer/appsettings.Development.json b/server/Ksp.WebServer/appsettings.Development.json new file mode 100644 index 0000000..dba68eb --- /dev/null +++ b/server/Ksp.WebServer/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/server/Ksp.WebServer/appsettings.json b/server/Ksp.WebServer/appsettings.json new file mode 100644 index 0000000..81ff877 --- /dev/null +++ b/server/Ksp.WebServer/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}