Skip to content

Commit

Permalink
Update to .NET 8 and C# 12 (#952)
Browse files Browse the repository at this point in the history
* Initial SDK and dependency bumps

* Bump Pomelo.EntityFrameworkCore.MySql to 8.0.0-beta.2

Should fix the MissingMethodException error in unit tests

* Update CI workflow to use .NET 8

* Apply suggested change from compile time warnings

* Fix digest tests

* Bump dependencies once more

* Update xunit

* Remove obsolete ISystemClock from TokenAuthHandler

* Update dependencies

* Add digest debug preprocessors back

* Maybe don't break #966

* Bump EF Driver and update various NET7 references

* Fix warnings in digest middleware tests

---------

Co-authored-by: Slendy <[email protected]>
  • Loading branch information
sudokoko and Slendy authored Feb 26, 2024
1 parent a2eaedc commit 01e6fa1
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 110 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/data
**/bin
**/obj
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
if: ${{ matrix.os.database }}
run: mysql -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }} -h 127.0.0.1 -e "CREATE DATABASE ${{ env.DB_DATABASE }};";

- name: Install .NET 7.0
- name: Install .NET 8.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: "7.0.x"
dotnet-version: "8.0.x"

- name: Compile
run: |
Expand All @@ -59,7 +59,7 @@ jobs:
if: ${{ matrix.os.webTest }}
continue-on-error: true
run: dotnet test -c Release --no-build --logger "trx;LogFileName=${{github.workspace}}/TestResults-${{matrix.os.prettyName}}-WebsiteTests.trx" ProjectLighthouse.Tests.WebsiteTests

# Attempt to upload results even if test fails.
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#always
- name: Upload Test Results
Expand Down
13 changes: 0 additions & 13 deletions .run/Development Database.run.xml

This file was deleted.

5 changes: 2 additions & 3 deletions .run/Lighthouse API.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse API" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.API/bin/Debug/net7.0/LBPUnion.ProjectLighthouse.Servers.API" />
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.API/bin/Debug/net8.0/LBPUnion.ProjectLighthouse.Servers.API" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand All @@ -15,9 +15,8 @@
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net7.0" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse Gameserver" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/bin/Debug/net7.0/LBPUnion.ProjectLighthouse.Servers.GameServer" />
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/bin/Debug/net8.0/LBPUnion.ProjectLighthouse.Servers.GameServer" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand All @@ -15,9 +15,8 @@
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net7.0" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
Expand Down
5 changes: 2 additions & 3 deletions .run/Lighthouse Website.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse Website" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/bin/Debug/net7.0/LBPUnion.ProjectLighthouse.Servers.Website" />
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/bin/Debug/net8.0/LBPUnion.ProjectLighthouse.Servers.Website" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand All @@ -15,9 +15,8 @@
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net7.0" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /ProjectLighthouse
COPY *.sln ./
COPY **/*.csproj ./
Expand All @@ -13,10 +13,10 @@ RUN dotnet sln list | grep ".csproj" \
RUN dotnet restore

COPY . .
RUN dotnet publish -c Release --property:OutputPath=/ProjectLighthouse/publish/ --no-restore
RUN dotnet publish -c Release --property:OutputPath=/ProjectLighthouse/out/ --no-restore

# Final running container
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS final
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final

# Add non-root user
RUN addgroup -S lighthouse --gid 1001 && \
Expand All @@ -29,7 +29,7 @@ apk add --no-cache icu-libs su-exec tzdata
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

# Copy build files
COPY --from=build /ProjectLighthouse/publish /lighthouse/app
COPY --from=build /ProjectLighthouse/out/publish /lighthouse/app
COPY --from=build /ProjectLighthouse/ProjectLighthouse/StaticFiles /lighthouse/temp/StaticFiles
COPY --from=build /ProjectLighthouse/scripts-and-tools/docker-entrypoint.sh /lighthouse

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>LBPUnion.ProjectLighthouse.Localization</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>LBPUnion.ProjectLighthouse.Servers.API</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public DigestMiddleware(RequestDelegate next, bool computeDigests) : base(next)
public override async Task InvokeAsync(HttpContext context)
{
// Client digest check.
if (!context.Request.Cookies.TryGetValue("MM_AUTH", out string? authCookie))
authCookie = string.Empty;
if (!context.Request.Cookies.TryGetValue("MM_AUTH", out string? authCookie)) authCookie = string.Empty;
string digestPath = context.Request.Path;
#if !DEBUG
const string url = "/LITTLEBIGPLANETPS3_XML";
Expand All @@ -55,8 +54,11 @@ public override async Task InvokeAsync(HttpContext context)
excludeBodyFromDigest = true;
}

string clientRequestDigest = CryptoHelper.ComputeDigest
(digestPath, authCookie, bodyBytes, ServerConfiguration.Instance.DigestKey.PrimaryDigestKey, excludeBodyFromDigest);
string clientRequestDigest = CryptoHelper.ComputeDigest(digestPath,
authCookie,
bodyBytes,
ServerConfiguration.Instance.DigestKey.PrimaryDigestKey,
excludeBodyFromDigest);

// Check the digest we've just calculated against the digest header if the game set the header. They should match.
if (context.Request.Headers.TryGetValue(digestHeaderKey, out StringValues sentDigest))
Expand All @@ -66,8 +68,11 @@ public override async Task InvokeAsync(HttpContext context)
// If we got here, the normal ServerDigestKey failed to validate. Lets try again with the alternate digest key.
usedAlternateDigestKey = true;

clientRequestDigest = CryptoHelper.ComputeDigest
(digestPath, authCookie, bodyBytes, ServerConfiguration.Instance.DigestKey.AlternateDigestKey, excludeBodyFromDigest);
clientRequestDigest = CryptoHelper.ComputeDigest(digestPath,
authCookie,
bodyBytes,
ServerConfiguration.Instance.DigestKey.AlternateDigestKey,
excludeBodyFromDigest);
if (clientRequestDigest != sentDigest)
{
#if DEBUG
Expand All @@ -82,17 +87,18 @@ public override async Task InvokeAsync(HttpContext context)
}
}
}

#if !DEBUG
// The game doesn't start sending digests until after the announcement so if it's not one of those requests
// and it doesn't include a digest we need to reject the request
// and it doesn't include a digest we need to reject the request
else if (!exemptPathList.Contains(strippedPath))
{
context.Response.StatusCode = 403;
return;
}
#endif

context.Response.Headers.Add("X-Digest-B", clientRequestDigest);
context.Response.Headers.Append("X-Digest-B", clientRequestDigest);
context.Request.Body.Position = 0;
}

Expand All @@ -104,19 +110,21 @@ public override async Task InvokeAsync(HttpContext context)
await this.next(context); // Handle the request so we can get the server digest hash
responseBuffer.Position = 0;

if (responseBuffer.Length > 1000 && context.Request.Headers.AcceptEncoding.Contains("deflate") && (context.Response.ContentType ?? string.Empty).Contains("text/xml"))
if (responseBuffer.Length > 1000 &&
context.Request.Headers.AcceptEncoding.Contains("deflate") &&
(context.Response.ContentType ?? string.Empty).Contains("text/xml"))
{
context.Response.Headers.Add("X-Original-Content-Length", responseBuffer.Length.ToString());
context.Response.Headers.Add("Vary", "Accept-Encoding");
context.Response.Headers.Append("X-Original-Content-Length", responseBuffer.Length.ToString());
context.Response.Headers.Append("Vary", "Accept-Encoding");
MemoryStream resultStream = new();
const int defaultCompressionLevel = 6;
await using ZOutputStreamLeaveOpen stream = new(resultStream, defaultCompressionLevel);
await stream.WriteAsync(responseBuffer.ToArray());
stream.Finish();

resultStream.Position = 0;
context.Response.Headers.Add("Content-Length", resultStream.Length.ToString());
context.Response.Headers.Add("Content-Encoding", "deflate");
context.Response.Headers.Append("Content-Length", resultStream.Length.ToString());
context.Response.Headers.Append("Content-Encoding", "deflate");
responseBuffer.SetLength(0);
await resultStream.CopyToAsync(responseBuffer);
}
Expand All @@ -125,7 +133,7 @@ public override async Task InvokeAsync(HttpContext context)
string headerName = !context.Response.Headers.ContentLength.HasValue
? "Content-Length"
: "X-Original-Content-Length";
context.Response.Headers.Add(headerName, responseBuffer.Length.ToString());
context.Response.Headers.Append(headerName, responseBuffer.Length.ToString());
}

// Compute the server digest hash.
Expand All @@ -138,8 +146,9 @@ public override async Task InvokeAsync(HttpContext context)
: ServerConfiguration.Instance.DigestKey.PrimaryDigestKey;

// Compute the digest for the response.
string serverDigest = CryptoHelper.ComputeDigest(context.Request.Path, authCookie, responseBuffer.ToArray(), digestKey);
context.Response.Headers.Add("X-Digest-A", serverDigest);
string serverDigest =
CryptoHelper.ComputeDigest(context.Request.Path, authCookie, responseBuffer.ToArray(), digestKey);
context.Response.Headers.Append("X-Digest-A", serverDigest);
}

// Copy the buffered response to the actual response stream.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>LBPUnion.ProjectLighthouse.Servers.GameServer</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public TokenAuthHandler
(
IOptionsMonitor<AuthenticationSchemeOptions> options,
UrlEncoder encoder,
ISystemClock clock,
DatabaseContext database
// I said I don't want any damn vegetables (logs)
) : base(options, new NullLoggerFactory(), encoder, clock)
) : base(options, new NullLoggerFactory(), encoder)
{
this.database = database;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>LBPUnion.ProjectLighthouse.Servers.Website</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 01e6fa1

Please sign in to comment.