Skip to content

Commit

Permalink
Removed Brotli.NET that depends on native libs and added BrotliCompre…
Browse files Browse the repository at this point in the history
…ss tool that uses .net native brotli compression.
  • Loading branch information
stavroskasidis committed Nov 23, 2022
1 parent bc08434 commit 09af483
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 20 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ This work was inspired by the post in https://github.com/dotnet/aspnetcore/issue

## Release Notes

<details open="open"><summary>2.2.0</summary>
<details open="open"><summary>2.3.0</summary>

>- Removed Brotli.NET that depends on native libs and added BrotliCompress tool that uses .net native brotli compression.
Fixes [#36](https://github.com/stavroskasidis/BlazorWasmAntivirusProtection/issues/36), [#42](https://github.com/stavroskasidis/BlazorWasmAntivirusProtection/issues/42)
(Contribution by [jsakamoto](https://github.com/jsakamoto))
</details>

<details><summary>2.2.0</summary>

>- Fix when publishing from Visual Studio [#36](https://github.com/stavroskasidis/BlazorWasmAntivirusProtection/issues/36)
</details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.0" PrivateAssets="all" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="2.2.1-test" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="2.2.2-test" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.0" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="2.2.1-test" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="2.2.2-test" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions sampleapps/BlazorHostedSampleApp/Server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ WORKDIR "/src/Server"
RUN dotnet build "BlazorHostedSampleApp.Server.csproj" -c Release -o /app/build

FROM build AS publish
RUN apt-get update && apt-get install -y libc6-dev
RUN dotnet publish "BlazorHostedSampleApp.Server.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions src/BlazorWasmAntivirusProtection.BrotliCompress/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO.Compression;

try
{
var bootJsonPath = args[0];
var bootJsonBrPath = args[1];
var compressionLeveText = args[2];

File.Delete(bootJsonBrPath);
var compressionLevel = Enum.TryParse<CompressionLevel>(compressionLeveText, out var level) ? level : CompressionLevel.Optimal;
using var fileStream = File.OpenRead(bootJsonPath);
using var stream = File.Create(bootJsonBrPath);
using var destination = new BrotliStream(stream, compressionLevel);
fileStream.CopyTo(destination);
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Brotli.NET" Version="2.1.1" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.10.0" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.10.0" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
Expand Down
25 changes: 15 additions & 10 deletions src/BlazorWasmAntivirusProtection.Tasks/RenameDlls.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace BlazorWasmAntivirusProtection.Tasks
{
using Brotli;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
Expand All @@ -17,6 +16,8 @@ public class RenameDlls : Task
{
[Required]
public string PublishDir { get; set; }
[Required]
public string BrotliCompressToolPath { get; set; }

public string RenameDllsTo { get; set; } = "bin";
public bool DisableRenamingDlls { get; set; }
Expand Down Expand Up @@ -127,16 +128,20 @@ private bool BrotliCompress(string bootJsonPath, string bootJsonBrPath)
{
try
{
File.Delete(bootJsonBrPath);
var compressionLevel = Enum.TryParse<CompressionLevel>(CompressionLevel, out var level) ? level : System.IO.Compression.CompressionLevel.Optimal;
using var fileStream = File.OpenRead(bootJsonPath);
using var stream = File.Create(bootJsonBrPath);
fileStream.CompressToBrotli(stream, compressionLevel switch
// NOTE: This MSBuild Custom Task will run not only on .NET 6 or later but also on .NET Framework 4.x.
// Therefore the `BrotliStream` class is not usable in this MSBuild Custom Task due to
// the `BrotliStream` class is not provided on.NET Framework.Instead, we can do that
// with execution as an out process.
var startInfo = new ProcessStartInfo
{
System.IO.Compression.CompressionLevel.Optimal => 11,
System.IO.Compression.CompressionLevel.Fastest => 5,
System.IO.Compression.CompressionLevel.NoCompression=> 0,
});
FileName = "dotnet",
Arguments = $"exec \"{BrotliCompressToolPath}\" \"{bootJsonPath}\" \"{bootJsonBrPath}\" \"{CompressionLevel}\""
};
Log.LogMessage(MessageImportance.Low, $"{startInfo.FileName} {startInfo.Arguments}");
var process = Process.Start(startInfo);
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception($"The exit code of recompressing with Brotli command was not 0. (it was {process.ExitCode})");
}
catch (Exception ex)
{
Expand Down
14 changes: 10 additions & 4 deletions src/BlazorWasmAntivirusProtection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "configs", "configs", "{298E
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorHostedSampleLazyLoading.Server", "..\sampleapps\BlazorHostedSampleLazyLoading\Server\BlazorHostedSampleLazyLoading.Server.csproj", "{9E4C4D90-0E4C-4679-AD28-7F990B286344}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleLazyLoading.Server", "..\sampleapps\BlazorHostedSampleLazyLoading\Server\BlazorHostedSampleLazyLoading.Server.csproj", "{9E4C4D90-0E4C-4679-AD28-7F990B286344}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorHostedSampleLazyLoading.Client", "..\sampleapps\BlazorHostedSampleLazyLoading\Client\BlazorHostedSampleLazyLoading.Client.csproj", "{ABC6E5FE-4B5E-42AC-A955-35754B2805A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleLazyLoading.Client", "..\sampleapps\BlazorHostedSampleLazyLoading\Client\BlazorHostedSampleLazyLoading.Client.csproj", "{ABC6E5FE-4B5E-42AC-A955-35754B2805A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorHostedSampleLazyLoading.Shared", "..\sampleapps\BlazorHostedSampleLazyLoading\Shared\BlazorHostedSampleLazyLoading.Shared.csproj", "{4C7F227E-2E90-4D0B-960D-0247DA937117}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleLazyLoading.Shared", "..\sampleapps\BlazorHostedSampleLazyLoading\Shared\BlazorHostedSampleLazyLoading.Shared.csproj", "{4C7F227E-2E90-4D0B-960D-0247DA937117}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BlazorHostedSampleLazyLoading", "BlazorHostedSampleLazyLoading", "{B8531AC8-E394-4875-AB1F-3167F4C7149C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorHostedSampleLazyLoading.Counter", "..\sampleapps\BlazorHostedSampleLazyLoading\BlazorHostedSampleLazyLoading.Counter\BlazorHostedSampleLazyLoading.Counter.csproj", "{D1EC6118-9D76-42C1-9F4B-E9819AEC6472}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHostedSampleLazyLoading.Counter", "..\sampleapps\BlazorHostedSampleLazyLoading\BlazorHostedSampleLazyLoading.Counter\BlazorHostedSampleLazyLoading.Counter.csproj", "{D1EC6118-9D76-42C1-9F4B-E9819AEC6472}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWasmAntivirusProtection.BrotliCompress", "BlazorWasmAntivirusProtection.BrotliCompress\BlazorWasmAntivirusProtection.BrotliCompress.csproj", "{DF24E023-EE53-4C3A-B0A2-927CCD39F2AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -100,6 +102,10 @@ Global
{D1EC6118-9D76-42C1-9F4B-E9819AEC6472}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1EC6118-9D76-42C1-9F4B-E9819AEC6472}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1EC6118-9D76-42C1-9F4B-E9819AEC6472}.Release|Any CPU.Build.0 = Release|Any CPU
{DF24E023-EE53-4C3A-B0A2-927CCD39F2AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF24E023-EE53-4C3A-B0A2-927CCD39F2AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF24E023-EE53-4C3A-B0A2-927CCD39F2AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF24E023-EE53-4C3A-B0A2-927CCD39F2AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/stavroskasidis/BlazorWasmAntivirusProtection</PackageProjectUrl>
<Description>This package attempts to guard against false positives from antiviruses that flag Blazor Wasm as malware</Description>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
</PropertyGroup>

Expand All @@ -31,5 +31,19 @@
</ItemGroup>
</Target>

<Target Name="GetToolsOutputDlls" BeforeTargets="CoreCompile">
<MSBuild Projects="..\BlazorWasmAntivirusProtection.BrotliCompress\BlazorWasmAntivirusProtection.BrotliCompress.csproj" Targets="Publish;PublishItemsOutputGroup" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="_ToolsProjectOutputs" />
</MSBuild>
<ItemGroup>
<Content Include="@(_ToolsProjectOutputs)" Condition="('$(Configuration)' == 'Release' And '%(_ToolsProjectOutputs.Extension)' == '.dll')
Or ('$(Configuration)' == 'Debug' And ('%(_ToolsProjectOutputs.Extension)' == '.dll' Or '%(_ToolsProjectOutputs.Extension)' == '.pdb'))
Or ('%(_ToolsProjectOutputs.Extension)' == '.json')"
Pack="true"
PackagePath="tools\%(_ToolsProjectOutputs.TargetPath)"
KeepMetadata="Pack;PackagePath" />
</ItemGroup>
</Target>


</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<PropertyGroup>
<ComputeBlazorExtensionsDependsOn>$(ComputeBlazorExtensionsDependsOn);_ObfuscateDlls</ComputeBlazorExtensionsDependsOn>
<_BlazorWAPBrotliCompressDll>$(MSBuildThisFileDirectory)..\..\tools\BlazorWasmAntivirusProtection.BrotliCompress.dll</_BlazorWAPBrotliCompressDll>
</PropertyGroup>

<!-- Runs in the client project -->
Expand All @@ -33,7 +34,10 @@

<!-- Runs in the published project (server if hosted)-->
<Target Name="_ChangeDLLFileExtensions" AfterTargets="Publish">
<RenameDlls PublishDir="$(PublishDir)" RenameDllsTo="$(RenameDllsTo)" DisableRenamingDlls="$(DisableRenamingDlls)" BlazorEnableCompression="$(BlazorEnableCompression)" CompressionLevel="$(_BlazorBrotliCompressionLevel)"></RenameDlls>
<RenameDlls PublishDir="$(PublishDir)" RenameDllsTo="$(RenameDllsTo)" DisableRenamingDlls="$(DisableRenamingDlls)"
BlazorEnableCompression="$(BlazorEnableCompression)" CompressionLevel="$(_BlazorBrotliCompressionLevel)"
BrotliCompressToolPath="$(_BlazorWAPBrotliCompressDll)">
</RenameDlls>
</Target>

</Project>

0 comments on commit 09af483

Please sign in to comment.