diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/AS1024.NetworkQuality.Server.sln b/AS1024.NetworkQuality.Server.sln new file mode 100644 index 0000000..e2a3f85 --- /dev/null +++ b/AS1024.NetworkQuality.Server.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34408.163 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS1024.NetworkQuality.Server", "AS1024.NetworkQuality.Server\AS1024.NetworkQuality.Server.csproj", "{71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71ABE0E0-226A-46E1-8CC9-76CDD7F5506F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {418E03E1-4880-487E-8E24-0A3AF27ABE76} + EndGlobalSection +EndGlobal diff --git a/AS1024.NetworkQuality.Server/AS1024.NetworkQuality.Server.csproj b/AS1024.NetworkQuality.Server/AS1024.NetworkQuality.Server.csproj new file mode 100644 index 0000000..e2a9130 --- /dev/null +++ b/AS1024.NetworkQuality.Server/AS1024.NetworkQuality.Server.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + enable + enable + true + true + Linux + + + + + + + diff --git a/AS1024.NetworkQuality.Server/Dockerfile b/AS1024.NetworkQuality.Server/Dockerfile new file mode 100644 index 0000000..49d7adc --- /dev/null +++ b/AS1024.NetworkQuality.Server/Dockerfile @@ -0,0 +1,29 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER app +WORKDIR /app +EXPOSE 8080 + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +# Install clang/zlib1g-dev dependencies for publishing to native +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + clang zlib1g-dev +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["AS1024.NetworkQuality.Server/AS1024.NetworkQuality.Server.csproj", "AS1024.NetworkQuality.Server/"] +RUN dotnet restore "./AS1024.NetworkQuality.Server/./AS1024.NetworkQuality.Server.csproj" +COPY . . +WORKDIR "/src/AS1024.NetworkQuality.Server" +RUN dotnet build "./AS1024.NetworkQuality.Server.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./AS1024.NetworkQuality.Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true + +FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final +WORKDIR /app +EXPOSE 8080 +COPY --from=publish /app/publish . +ENTRYPOINT ["./AS1024.NetworkQuality.Server"] \ No newline at end of file diff --git a/AS1024.NetworkQuality.Server/LargeStreamResult.cs b/AS1024.NetworkQuality.Server/LargeStreamResult.cs new file mode 100644 index 0000000..63fbb2b --- /dev/null +++ b/AS1024.NetworkQuality.Server/LargeStreamResult.cs @@ -0,0 +1,44 @@ +internal class LargeStreamResult : System.IO.Stream +{ + private long _position = 0; + private const long _length = 8L * 1024L * 1024L * 1024L; // 8 GB + + public override bool CanRead => true; + public override bool CanSeek => true; + public override bool CanWrite => false; + public override long Length => _length; + public override long Position { get => _position; set => _position = value; } + + public override void Flush() { } + + public override int Read(byte[] buffer, int offset, int count) + { + int bytesToRead = (int)Math.Min(count, _length - _position); + if (bytesToRead <= 0) + return 0; + + _position += bytesToRead; + return bytesToRead; + } + + public override long Seek(long offset, System.IO.SeekOrigin origin) + { + switch (origin) + { + case System.IO.SeekOrigin.Begin: + _position = offset; + break; + case System.IO.SeekOrigin.Current: + _position += offset; + break; + case System.IO.SeekOrigin.End: + _position = _length + offset; + break; + } + return _position; + } + + public override void SetLength(long value) { } + + public override void Write(byte[] buffer, int offset, int count) { } +} diff --git a/AS1024.NetworkQuality.Server/Program.cs b/AS1024.NetworkQuality.Server/Program.cs new file mode 100644 index 0000000..bb5bae1 --- /dev/null +++ b/AS1024.NetworkQuality.Server/Program.cs @@ -0,0 +1,84 @@ +using AS1024.NetworkQuality.Server; +using System.Text.Json.Serialization; +const string schemeNetQuality = "https"; +var builder = WebApplication.CreateSlimBuilder(args); + +builder.Services.ConfigureHttpJsonOptions(options => +{ +}); + +var app = builder.Build(); + +app.MapGet("/api/v1/config", (HttpRequest request) => +{ + string host = request.Host.Host; + int port = request.Host.Port ?? + (request.IsHttps ? 443 : 80); + Uri smallDownloadUrl = new UriBuilder + { + Scheme = schemeNetQuality, + Host = host, + Port = port, + Path = "/api/v1/small" + }.Uri; + + Uri largeDownloadUrl = new UriBuilder + { + Scheme = schemeNetQuality, + Host = host, + Port = port, + Path = "/api/v1/large" + }.Uri; + + Uri uploadUrl = new UriBuilder + { + Scheme = schemeNetQuality, + Host = host, + Port = port, + Path = "/api/v1/upload" + }.Uri; + + ConfigResponse config = new() + { + Version = 1, + Urls = new Urls + { + SmallDownloadUrl = smallDownloadUrl.AbsoluteUri, + LargeDownloadUrl = largeDownloadUrl.AbsoluteUri, + UploadUrl = uploadUrl.AbsoluteUri + } + }; + + return Results.Json(config, + AppJsonSerializerContext.Default); +}); + +app.MapGet("/api/v1/small", (HttpResponse response) => +{ + response.ContentType = "application/octet-stream"; + return Results.File(new byte[] { 0 }); +}); + +app.MapGet("/api/v1/large", (HttpResponse response) => +{ + response.ContentType = "application/octet-stream"; + return Results.Stream(new LargeStreamResult()); +}); + +app.MapPost("/api/v1/upload", async (HttpRequest request) => +{ + using (var stream = new System.IO.MemoryStream()) + { + await request.Body.CopyToAsync(stream); + } + return Results.Ok(); +}); + +app.Run(); + + +[JsonSerializable(typeof(ConfigResponse))] +internal partial class AppJsonSerializerContext : JsonSerializerContext +{ + +} \ No newline at end of file diff --git a/AS1024.NetworkQuality.Server/Properties/launchSettings.json b/AS1024.NetworkQuality.Server/Properties/launchSettings.json new file mode 100644 index 0000000..7287860 --- /dev/null +++ b/AS1024.NetworkQuality.Server/Properties/launchSettings.json @@ -0,0 +1,24 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/v1/config", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5294" + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/v1/config", + "environmentVariables": { + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json" +} \ No newline at end of file diff --git a/AS1024.NetworkQuality.Server/ResponseModel.cs b/AS1024.NetworkQuality.Server/ResponseModel.cs new file mode 100644 index 0000000..230d77c --- /dev/null +++ b/AS1024.NetworkQuality.Server/ResponseModel.cs @@ -0,0 +1,15 @@ +namespace AS1024.NetworkQuality.Server +{ + public class ConfigResponse + { + public int Version { get; set; } + public required Urls Urls { get; set; } + } + + public class Urls + { + public required string SmallDownloadUrl { get; set; } + public required string LargeDownloadUrl { get; set; } + public required string UploadUrl { get; set; } + } +} diff --git a/AS1024.NetworkQuality.Server/appsettings.Development.json b/AS1024.NetworkQuality.Server/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/AS1024.NetworkQuality.Server/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/AS1024.NetworkQuality.Server/appsettings.json b/AS1024.NetworkQuality.Server/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/AS1024.NetworkQuality.Server/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}