可切換內網訪問

This commit is contained in:
owenchen committed 2026-10-21 10:51:07 +08:00
1 parent 1ff69f3cb3
commit fd67fa8f63
10 files changed
+493 -40

No files matched your search

@@ -4,6 +4,7 @@ using CloudBuilder.Core.DependencyInjection.Policy;
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Extensions;
using CloudBuilder.Core.Policy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.DependencyInjection;
using System;
@@ -83,6 +84,30 @@ namespace CloudBuilder.Security.Policy
if (!IsVideo(orderKey)) return null;
return PackUrl(service, createdBy, orderKey, YesNoPolicy.YES, false);
}
/// <summary>
/// 资源地址基座:优先按当前请求的 Scheme+Host 拼(App 切内网直连后,缩略图/原图/视频/头像
/// 返回的就是内网地址,资源自动跟随线路);无请求上下文(后台任务/启动阶段)回退 appsettings 的 ApplicationUrl。
/// 公网反代场景(nginx → Kestrel 是 http,对外是 https)不能直接用 request.Scheme:
/// 优先取 X-Forwarded-Proto,其次 Host 等于配置域名时沿用配置 scheme,其余(内网 IP/自定义域名)用请求 scheme。
/// </summary>
public static string BuildApplicationUrl(IApplicationService service)
{
string configured = service.Configuration[AppsettingsPolicy.ApplicationUrl] ?? "";
IHttpContextAccessor accessor = service.ServiceProvider.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
HttpRequest request = accessor?.HttpContext?.Request;
if (request == null || !request.Host.HasValue) return configured;
string scheme = request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? string.Empty;
if (string.IsNullOrEmpty(scheme))
{
bool sameAsConfigured = configured.StartsWith("https", StringComparison.OrdinalIgnoreCase)
&& Uri.TryCreate(configured, UriKind.Absolute, out Uri configuredUri)
&& string.Equals(configuredUri.Host, request.Host.Host, StringComparison.OrdinalIgnoreCase);
scheme = sameAsConfigured ? "https" : request.Scheme;
}
return scheme + "://" + request.Host;
}
public static string PackPicUrl(IApplicationService service, string createdBy, string orderKey)
{
return PackUrl(service, createdBy, orderKey, YesNoPolicy.YES, true);
@@ -107,14 +132,14 @@ namespace CloudBuilder.Security.Policy
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
url = BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
else
{
var scale = (ServiceSettingsPolicy.GetDmsFileScaleDirectory(service) ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(scale, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
url = BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
}
else if (GetType(orderKey!) == AUDIO)
@@ -122,7 +147,7 @@ namespace CloudBuilder.Security.Policy
var backup = (ServiceSettingsPolicy.GetDmsFileBackupDirectory(service) ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
url = BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
else if (GetType(orderKey!) == VIDEO)
{
@@ -134,14 +159,14 @@ namespace CloudBuilder.Security.Policy
var filePathRel = Path.Combine(upload, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
// HH 才是 24 小时制:写成 hh 会让上午/下午的时间重复,缓存穿透失效
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + JPG + "?v=" + DateTime.Now.ToString("yyyyMMddHHmmss");
url = BuildApplicationUrl(service) + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + JPG + "?v=" + DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
var backup = (ServiceSettingsPolicy.GetDmsFileBackupDirectory(service) ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, M3U8.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + M3U8;
url = BuildApplicationUrl(service) + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + M3U8;
}
}
else
@@ -149,7 +174,7 @@ namespace CloudBuilder.Security.Policy
var backup = (ServiceSettingsPolicy.GetDmsFileBackupDirectory(service) ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
url = BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
}
else if (GetType(orderKey!) == LYRIC)
@@ -157,7 +182,7 @@ namespace CloudBuilder.Security.Policy
var upload = ServiceSettingsPolicy.GetDmsFileBackupDirectory(service).Replace(phy, "");
var filePathRel = Path.Combine(upload, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
url = BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
return url;
}
@@ -178,9 +203,9 @@ namespace CloudBuilder.Security.Policy
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
if (YesNoPolicy.IsYes(transcodeIndc))
{
return (service.Configuration[AppsettingsPolicy.ApplicationUrl] ?? "") + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + M3U8;
return BuildApplicationUrl(service) + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + M3U8;
}
return (service.Configuration[AppsettingsPolicy.ApplicationUrl] ?? "") + webRootPath + filePath + orderKey;
return BuildApplicationUrl(service) + webRootPath + filePath + orderKey;
}
public static string PackFilePath(IApplicationService service, string createdBy, string orderKey)
@@ -208,7 +233,7 @@ namespace CloudBuilder.Security.Policy
var filePathRel = Path.Combine(backup, folderName ?? "", fileExtension, "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
return service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + fileName;
return BuildApplicationUrl(service) + webRootPath + filePath + fileName;
}
}
}
@@ -8,7 +8,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5235"
"applicationUrl": "http://172.16.253.53:5235"
},
"IIS Express": {
"commandName": "IISExpress",
@@ -3,7 +3,7 @@
"LogFileDirectory": "D:\\Net8\\FileServer\\Log",
"MaxFileSizeMB": 20
},
"UrlOrigins": "http://192.168.195.68,http://localhost,http://localhost:8088,http://localhost:5173,http://localhost:5235", //这个参数是反向指向的地址,也是跨域访问的地址,包含Vue内WebSitePolicy设置WebSite
"UrlOrigins": "http://192.168.195.68,http://172.16.253.53:5235,http://localhost,http://localhost:8088,http://localhost:5173,http://localhost:5235", //这个参数是反向指向的地址,也是跨域访问的地址,包含Vue内WebSitePolicy设置WebSite
"ApplicationUrl": "http://localhost:5235", //這個地址是最外层用户访问的,不是反向指向的地址https://uat.youjiala.com
"CloudBuilder": {
"Culture": "zh-cn",