Warning on forms with passwords
This commit is contained in:
parent
a16a2f25bd
commit
f6ca2e163b
4 changed files with 121 additions and 4 deletions
34
server/Ksp.WebServer/.vscode/launch.json
vendored
Normal file
34
server/Ksp.WebServer/.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": ".NET Core Launch (web)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Ksp.WebServer.dll",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"serverReadyAction": {
|
||||||
|
"action": "openExternally",
|
||||||
|
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"sourceFileMap": {
|
||||||
|
"/Views": "${workspaceFolder}/Views"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ".NET Core Attach",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "attach",
|
||||||
|
"processId": "${command:pickProcess}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
42
server/Ksp.WebServer/.vscode/tasks.json
vendored
Normal file
42
server/Ksp.WebServer/.vscode/tasks.json
vendored
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/Ksp.WebServer.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "publish",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"publish",
|
||||||
|
"${workspaceFolder}/Ksp.WebServer.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"${workspaceFolder}/Ksp.WebServer.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
32
server/Ksp.WebServer/KspPageRewriter.cs
Normal file
32
server/Ksp.WebServer/KspPageRewriter.cs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
using System.IO;
|
||||||
|
using AngleSharp.Html;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace Ksp.WebServer
|
||||||
|
{
|
||||||
|
public class KspPageRewriter
|
||||||
|
{
|
||||||
|
public string RewriteHtml(string source, HttpContext context)
|
||||||
|
{
|
||||||
|
var p = new AngleSharp.Html.Parser.HtmlParser();
|
||||||
|
var document = p.ParseDocument(source);
|
||||||
|
|
||||||
|
foreach (var form in document.QuerySelectorAll("form"))
|
||||||
|
{
|
||||||
|
if (form.QuerySelector("input[type=password]") is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var warning = document.CreateElement("div");
|
||||||
|
warning.SetAttribute("style", "color: red; font-size: 3em; font-weight: bold");
|
||||||
|
warning.TextContent = "Web běží na magické proxy, které byste měli věřit!!!";
|
||||||
|
form.Prepend(warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var outputHtml = new StringWriter();
|
||||||
|
document.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
|
||||||
|
return outputHtml.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AspNetCore.Proxy;
|
using AspNetCore.Proxy;
|
||||||
using AspNetCore.Proxy.Builders;
|
using AspNetCore.Proxy.Builders;
|
||||||
|
@ -39,15 +40,17 @@ namespace Ksp.WebServer
|
||||||
.ConfigurePrimaryHttpMessageHandler(h => {
|
.ConfigurePrimaryHttpMessageHandler(h => {
|
||||||
return new HttpClientHandler {
|
return new HttpClientHandler {
|
||||||
AllowAutoRedirect = false,
|
AllowAutoRedirect = false,
|
||||||
UseCookies = false
|
UseCookies = false,
|
||||||
|
AutomaticDecompression = DecompressionMethods.All
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
services.AddProxies();
|
services.AddProxies();
|
||||||
services.Configure<KspProxyConfig>(Configuration.GetSection(nameof(KspProxyConfig)));
|
services.Configure<KspProxyConfig>(Configuration.GetSection(nameof(KspProxyConfig)));
|
||||||
|
services.AddSingleton<KspPageRewriter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, IOptions<KspProxyConfig> kspProxyConfig)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<KspProxyConfig> kspProxyConfig, KspPageRewriter pageRewriter)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}");
|
Console.WriteLine($"Running {env.EnvironmentName} env, root={env.ContentRootPath}, host={kspProxyConfig.Value.Host}");
|
||||||
|
|
||||||
|
@ -97,7 +100,7 @@ namespace Ksp.WebServer
|
||||||
// Console.WriteLine(request);
|
// Console.WriteLine(request);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
});
|
});
|
||||||
opt.WithAfterReceive((cx, response) => {
|
opt.WithAfterReceive(async (cx, response) => {
|
||||||
// Console.WriteLine(response);
|
// Console.WriteLine(response);
|
||||||
if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host)
|
if (response.Headers.Location is object && response.Headers.Location.Host == baseUri.Host)
|
||||||
{
|
{
|
||||||
|
@ -115,7 +118,13 @@ namespace Ksp.WebServer
|
||||||
.Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}")
|
.Replace($"; domain={baseUri.Host}", $"; domain={cx.Request.Host.Host}")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
|
||||||
|
if (new [] { "text/html", "application/xhtml+xml" }.Contains(response.Content.Headers.ContentType.MediaType))
|
||||||
|
{
|
||||||
|
var str = await response.Content.ReadAsStringAsync();
|
||||||
|
str = pageRewriter.RewriteHtml(str, cx);
|
||||||
|
response.Content = new StringContent(str, Encoding.UTF8, "text/html");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue