Browse Source

Add hack proxy project

mj-deploy
Standa Lukeš 4 years ago
parent
commit
8ba2cf383d
  1. 2
      server/Ksp.WebServer/.gitignore
  2. 39
      server/Ksp.WebServer/Controllers/WeatherForecastController.cs
  3. 12
      server/Ksp.WebServer/Ksp.WebServer.csproj
  4. 26
      server/Ksp.WebServer/Program.cs
  5. 30
      server/Ksp.WebServer/Properties/launchSettings.json
  6. 119
      server/Ksp.WebServer/Startup.cs
  7. 15
      server/Ksp.WebServer/WeatherForecast.cs
  8. 9
      server/Ksp.WebServer/appsettings.Development.json
  9. 10
      server/Ksp.WebServer/appsettings.json

2
server/Ksp.WebServer/.gitignore

@ -0,0 +1,2 @@
bin
obj

39
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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> 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();
}
}
}

12
server/Ksp.WebServer/Ksp.WebServer.csproj

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.Proxy" Version="4.1.0" />
</ItemGroup>
</Project>

26
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<Startup>();
});
}
}

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

119
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}")
));
}
});
}));
}
}
}

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

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

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

10
server/Ksp.WebServer/appsettings.json

@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}