Skip to content

Commit

Permalink
解决静态文件域名解析
Browse files Browse the repository at this point in the history
  • Loading branch information
239573049 committed May 16, 2024
1 parent d60780c commit 9920a45
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 48 deletions.
22 changes: 11 additions & 11 deletions src/FastGateway/BackgroundServices/StatisticsBackgroundService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Concurrent;
using IP2Region.Net.Abstractions;
using System.Collections.Concurrent;
using System.Threading.Channels;
using IP2Region.Net.Abstractions;

namespace FastGateway.BackgroundServices;

Expand Down Expand Up @@ -173,15 +173,15 @@ await freeSql.Update<StatisticIp>()
{
// 直接添加
await freeSql.Insert(new StatisticIp()
{
ServiceId = requestCountDto.ServiceId,
Count = requestCountDto.Count,
Year = year,
Month = month,
Day = day,
Ip = requestCountDto.Ip,
Location = requestCountDto.Location,
})
{
ServiceId = requestCountDto.ServiceId,
Count = requestCountDto.Count,
Year = year,
Month = month,
Day = day,
Ip = requestCountDto.Ip,
Location = requestCountDto.Location,
})
.ExecuteAffrowsAsync();
}
}
Expand Down
102 changes: 65 additions & 37 deletions src/FastGateway/Services/ApiServiceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,47 +581,75 @@ private static async Task BuilderService(object state)
// 用于HTTPS证书签名校验
app.MapGet("/.well-known/acme-challenge/{token}", AcmeChallenge.Challenge);

foreach (var location in service.Locations.SelectMany(x => x.LocationService)
.Where(x => x.Type == ApiServiceType.StaticProxy))
foreach (var value in service.Locations.Select(x =>
new
{
x.LocationService,
x.ServiceNames,
})
.Where(x => x.LocationService.Any(a => a.Type == ApiServiceType.StaticProxy)))
{
app.Map(location.Path.TrimEnd('/'), app =>
var (locations, serviceNames) = (value.LocationService, value.ServiceNames);
foreach (var location in locations)
{
app.Run((async context =>
app.Map(location.Path.TrimEnd('/'), app =>
{
var path = Path.Combine(location.Root, context.Request.Path.Value[1..]);
if (File.Exists(path))
{
DefaultContentTypeProvider.TryGetContentType(path, out var contentType);
context.Response.Headers.ContentType = contentType;
await context.Response.SendFileAsync(path);
return;
}
if (location.TryFiles == null || location.TryFiles.Length == 0)
{
context.Response.StatusCode = 404;
return;
}
// 搜索 try_files
foreach (var tryFile in location.TryFiles)
app.Use((async (context,next) =>
{
var tryPath = Path.Combine(location.Root, tryFile);
if (!File.Exists(tryPath)) continue;
DefaultContentTypeProvider.TryGetContentType(tryPath, out var contentType);
context.Response.Headers.ContentType = contentType;
await context.Response.SendFileAsync(tryPath);
return;
}
}));
});
// 域名解析是否符合,域名可能存在 *解析
if (serviceNames.Any(x => x.Contains('*')))
{
if (!serviceNames.Any(x =>
x.Contains('*') && context.Request.Host.Host.EndsWith(x.Split('*')[1])))
{
await next(context);
return;
}
}
else
{
if (!serviceNames.Contains(context.Request.Host.Host))
{
await next(context);
return;
}
}
var path = Path.Combine(location.Root, context.Request.Path.Value[1..]);
if (File.Exists(path))
{
DefaultContentTypeProvider.TryGetContentType(path, out var contentType);
context.Response.Headers.ContentType = contentType;
await context.Response.SendFileAsync(path);
return;
}
if (location.TryFiles == null || location.TryFiles.Length == 0)
{
context.Response.StatusCode = 404;
return;
}
// 搜索 try_files
foreach (var tryFile in location.TryFiles)
{
var tryPath = Path.Combine(location.Root, tryFile);
if (!File.Exists(tryPath)) continue;
DefaultContentTypeProvider.TryGetContentType(tryPath, out var contentType);
context.Response.Headers.ContentType = contentType;
await context.Response.SendFileAsync(tryPath);
return;
}
}));
});
}
}

app.MapReverseProxy();
Expand Down

0 comments on commit 9920a45

Please sign in to comment.