Skip to content

Commit

Permalink
Update project versions and refactor China deployment logic
Browse files Browse the repository at this point in the history
Updated Elf.Api.csproj and ElfAdmin.csproj to version 6.10.1.
Upgraded Edi.ChinaDetector package in Elf.Api.csproj to 1.2.251.
Refactored China deployment logic in WebApplicationExtensions.cs:
- Removed Experimental:DetectChina config setting.
- Instantiated OfflineChinaDetectService directly.
- Simplified DealWithChina method:
  - Removed switch-case logic.
  - Introduced Prevent method for blocking.
  - Updated block message and status code.
  - In dev environments, log warning and continue running.
  • Loading branch information
EdiWang committed Jul 31, 2024
1 parent 4359c57 commit a73e205
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/API/Elf.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Edi Wang</Authors>
<Company>edi.wang</Company>
<Copyright>(C) 2024 edi.wang@outlook.com</Copyright>
<Version>6.10.0</Version>
<Version>6.10.1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -22,7 +22,7 @@
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.22.0" />
<PackageReference Include="Edi.ChinaDetector" Version="1.1.251" />
<PackageReference Include="Edi.ChinaDetector" Version="1.2.251" />

<PackageReference Include="Microsoft.Identity.Web" Version="3.0.1" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="3.5.0" />
Expand Down
46 changes: 21 additions & 25 deletions src/API/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,35 @@ public static class WebApplicationExtensions
{
public static async Task DetectChina(this WebApplication app)
{
// Read config `Experimental:DetectChina` to decide how to deal with China
// Refer: https://go.edi.wang/aka/os251
var detectChina = app.Configuration["DetectChina"];
if (!string.IsNullOrWhiteSpace(detectChina))
// Learn more at https://go.edi.wang/aka/os251
var service = new OfflineChinaDetectService();
var result = await service.Detect(DetectionMethod.TimeZone | DetectionMethod.Culture | DetectionMethod.Behavior);
if (result.Rank >= 1)
{
var service = new OfflineChinaDetectService();
var result = await service.Detect(DetectionMethod.TimeZone | DetectionMethod.Culture | DetectionMethod.Behavior);
if (result.Rank >= 1)
{
DealWithChina(app, detectChina);
}
DealWithChina(app);
}
}

private static void DealWithChina(WebApplication app, string detectChina)
private static void DealWithChina(WebApplication app)
{
switch (detectChina.ToLower())
void Prevent()
{
case "block":
app.Logger.LogError("Positive China detection, application stopped.");
app.Logger.LogError("Positive China detection, application stopped.");

app.MapGet("/", () => Results.Text(
"Due to legal and regulation concerns, we regret to inform you that deploying Elf on servers located in Mainland China is currently not possible",
statusCode: 251
));
app.Run();

break;
case "allow":
default:
app.Logger.LogInformation("Current server is suspected to be located in Mainland China, Elf will still run on full functionality.");
app.MapGet("/", () => Results.Text(
"Due to legal and regulation concerns, we regret to inform you that deploying Moonglade on servers located in China (including Hong Kong) is currently not possible",
statusCode: 251
));
app.Run();
}

break;
if (app.Environment.IsDevelopment())
{
app.Logger.LogWarning("Current deployment is suspected to be located in China, Moonglade will still run on full functionality in development environment.");
}
else
{
Prevent();
}
}
}
2 changes: 1 addition & 1 deletion src/Admin/ElfAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Edi Wang</Authors>
<Company>edi.wang</Company>
<Copyright>(C) 2024 edi.wang@outlook.com</Copyright>
<Version>6.10.0</Version>
<Version>6.10.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit a73e205

Please sign in to comment.