add "auto encrypt" function for *Main.xml

This commit is contained in:
Robert Paciorek 2024-02-04 12:32:52 +00:00
parent bd6df27c92
commit e1a8f47a27
2 changed files with 20 additions and 2 deletions

View File

@ -5,6 +5,8 @@ public class AssetServerConfig {
public string URLPrefix { get; set; } = string.Empty; public string URLPrefix { get; set; } = string.Empty;
public AssetServerMode Mode { get; set; } public AssetServerMode Mode { get; set; }
public string ProviderURL { get; set; } = string.Empty; public string ProviderURL { get; set; } = string.Empty;
public string AutoEncryptRegexp { get; set; } = string.Empty;
public string AutoEncryptKey { get; set; } = string.Empty;
public bool SubstituteMissingLocalAssets { get; set; } = false; public bool SubstituteMissingLocalAssets { get; set; } = false;
public bool UseCache { get; set; } = true; public bool UseCache { get; set; } = true;
} }

View File

@ -1,18 +1,25 @@
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Net; using System.Net;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using sodoff.Configuration; using sodoff.Configuration;
using sodoff.Util;
namespace sodoff.Middleware; namespace sodoff.Middleware;
public class AssetMiddleware public class AssetMiddleware
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly IOptions<AssetServerConfig> config; private readonly IOptions<AssetServerConfig> config;
private readonly Regex autoEncryptRegexp;
public AssetMiddleware(RequestDelegate next, IOptions<AssetServerConfig> config) public AssetMiddleware(RequestDelegate next, IOptions<AssetServerConfig> config)
{ {
_next = next; _next = next;
this.config = config; this.config = config;
if (config.Value.AutoEncryptRegexp != "")
autoEncryptRegexp = new Regex(config.Value.AutoEncryptRegexp);
else
autoEncryptRegexp = null;
} }
public async Task Invoke(HttpContext context) public async Task Invoke(HttpContext context)
@ -33,7 +40,6 @@ public class AssetMiddleware
} }
string assetPath = path.Remove(0, config.Value.URLPrefix.Length + 1); string assetPath = path.Remove(0, config.Value.URLPrefix.Length + 1);
string localPath = GetLocalPath("assets/" + assetPath); string localPath = GetLocalPath("assets/" + assetPath);
if (localPath == string.Empty && config.Value.Mode == AssetServerMode.Partial && config.Value.UseCache) if (localPath == string.Empty && config.Value.Mode == AssetServerMode.Partial && config.Value.UseCache)
@ -47,9 +53,19 @@ public class AssetMiddleware
} }
else { else {
context.Response.Headers["Content-Type"] = "application/octet-stream"; context.Response.Headers["Content-Type"] = "application/octet-stream";
bool needEncrypt = autoEncryptRegexp != null && autoEncryptRegexp.IsMatch(localPath);
if (needEncrypt) {
await context.Response.WriteAsync(
TripleDES.EncryptASCII(
System.IO.File.ReadAllText(localPath),
config.Value.AutoEncryptKey
)
);
} else {
await context.Response.SendFileAsync(Path.GetFullPath(localPath)); await context.Response.SendFileAsync(Path.GetFullPath(localPath));
} }
} }
}
private async Task GetRemoteAsset(HttpContext context, string path) private async Task GetRemoteAsset(HttpContext context, string path)
{ {