From 4fbaf143ba601edf8f7ee4561c1a8b9b0adbaa19 Mon Sep 17 00:00:00 2001 From: waldekmastykarz Date: Wed, 13 Nov 2024 16:09:41 +0100 Subject: [PATCH] Updates to .net 9. Closes #934 --- .github/workflows/codeql-analysis.yml | 2 +- .github/workflows/create-release.yml | 6 +- .github/workflows/dotnet.yml | 2 +- .vscode/launch.json | 4 +- CONTRIBUTING.md | 2 +- .../dev-proxy-abstractions.csproj | 12 +- dev-proxy-plugins/dev-proxy-plugins.csproj | 8 +- dev-proxy/CertificateDiskCache.cs | 260 +++++++++--------- dev-proxy/dev-proxy.csproj | 18 +- scripts/local-build.ps1 | 2 +- 10 files changed, 158 insertions(+), 158 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0c6bfc3a..c43b0938 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -46,7 +46,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + dotnet-version: 9.0.x # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 5a2d24fc..6b05a9fc 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -22,7 +22,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + dotnet-version: 9.0.x - name: Rename executable for beta if: contains(github.ref_name, '-beta') run: | @@ -47,7 +47,7 @@ jobs: if: matrix.architecture == 'win-x64' run: dotnet build ./dev-proxy-abstractions/dev-proxy-abstractions.csproj -p:InformationalVersion=$("${{ github.ref_name }}".Substring(1)) -c Release --no-self-contained - name: Add plugins to output - run: cp ./dev-proxy/bin/Release/net8.0/${{ matrix.architecture }}/plugins ./${{ env.release }} -r + run: cp ./dev-proxy/bin/Release/net9.0/${{ matrix.architecture }}/plugins ./${{ env.release }} -r - name: Remove unnecessary files run: | pushd @@ -74,7 +74,7 @@ jobs: uses: thedoctor0/zip-release@master with: filename: '../../../../dev-proxy-abstractions-${{ github.ref_name }}.zip' - directory: './dev-proxy-abstractions/bin/Release/net8.0' + directory: './dev-proxy-abstractions/bin/Release/net9.0' exclusions: '*.json' - name: Upload abstractions if: matrix.architecture == 'win-x64' diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index d7967d0e..68a44c51 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -19,7 +19,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + dotnet-version: 9.0.x - name: Restore workloads run: dotnet workload restore - name: Restore dependencies diff --git a/.vscode/launch.json b/.vscode/launch.json index 21a9738f..3aeb94fc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,9 +10,9 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/dev-proxy/bin/Debug/net8.0/devproxy.dll", + "program": "${workspaceFolder}/dev-proxy/bin/Debug/net9.0/devproxy.dll", "args": [], - "cwd": "${workspaceFolder}/dev-proxy/bin/Debug/net8.0", + "cwd": "${workspaceFolder}/dev-proxy/bin/Debug/net9.0", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "integratedTerminal", "stopAtEntry": false, diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6bcadbe9..0554e327 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ The best way to contribute initially is to download and try Dev Proxy and then g ## Our foundation -The Dev Proxy is built with .NET 6 and uses the [Titanium.Web.Proxy](https://github.com/justcoding121/titanium-web-proxy). +The Dev Proxy is built with .NET 9 and uses the [Unobtanium Web Proxy](https://github.com/svrooij/unobtanium-web-proxy). ## Reporting issues and suggesting new features diff --git a/dev-proxy-abstractions/dev-proxy-abstractions.csproj b/dev-proxy-abstractions/dev-proxy-abstractions.csproj index a0dc0223..9e07a16d 100644 --- a/dev-proxy-abstractions/dev-proxy-abstractions.csproj +++ b/dev-proxy-abstractions/dev-proxy-abstractions.csproj @@ -1,7 +1,7 @@ - net8.0 + net9.0 Microsoft.DevProxy.Abstractions enable enable @@ -10,11 +10,11 @@ - - - - - + + + + + diff --git a/dev-proxy-plugins/dev-proxy-plugins.csproj b/dev-proxy-plugins/dev-proxy-plugins.csproj index 5d33bbd4..3ccf9479 100644 --- a/dev-proxy-plugins/dev-proxy-plugins.csproj +++ b/dev-proxy-plugins/dev-proxy-plugins.csproj @@ -1,6 +1,6 @@  - net8.0 + net9.0 Microsoft.DevProxy.Plugins enable enable @@ -17,15 +17,15 @@ false runtime - + false runtime - + false runtime - + false runtime diff --git a/dev-proxy/CertificateDiskCache.cs b/dev-proxy/CertificateDiskCache.cs index 1f8778fb..40799500 100644 --- a/dev-proxy/CertificateDiskCache.cs +++ b/dev-proxy/CertificateDiskCache.cs @@ -1,131 +1,131 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -using System.Security.Cryptography.X509Certificates; -using Titanium.Web.Proxy.Certificates.Cache; -using Titanium.Web.Proxy.Helpers; - -namespace Microsoft.DevProxy; - -// based on https://github.com/justcoding121/titanium-web-proxy/blob/9e71608d204e5b67085656dd6b355813929801e4/src/Titanium.Web.Proxy/Certificates/Cache/DefaultCertificateDiskCache.cs -public sealed class CertificateDiskCache : ICertificateCache -{ - private const string DefaultCertificateDirectoryName = "crts"; - private const string DefaultCertificateFileExtension = ".pfx"; - private const string DefaultRootCertificateFileName = "rootCert" + DefaultCertificateFileExtension; - private const string ProxyConfigurationFolderName = "dev-proxy"; - - private string? rootCertificatePath; - - public Task LoadRootCertificateAsync(string pathOrName, string password, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) - { - var path = GetRootCertificatePath(pathOrName, false); - return Task.FromResult(LoadCertificate(path, password, storageFlags)); - } - - public Task SaveRootCertificateAsync(string pathOrName, string password, X509Certificate2 certificate, CancellationToken cancellationToken) - { - var path = GetRootCertificatePath(pathOrName, true); - var exported = certificate.Export(X509ContentType.Pkcs12, password); - File.WriteAllBytes(path, exported); - return Task.CompletedTask; - } - - public Task LoadCertificateAsync(string subjectName, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) - { - var filePath = Path.Combine(GetCertificatePath(false), subjectName + DefaultCertificateFileExtension); - return Task.FromResult(LoadCertificate(filePath, string.Empty, storageFlags)); - } - - public Task SaveCertificateAsync(string subjectName, X509Certificate2 certificate, CancellationToken cancellationToken) - { - var filePath = Path.Combine(GetCertificatePath(true), subjectName + DefaultCertificateFileExtension); - var exported = certificate.Export(X509ContentType.Pkcs12); - File.WriteAllBytes(filePath, exported); - return Task.CompletedTask; - } - - public void Clear() - { - try - { - var path = GetCertificatePath(false); - if (Directory.Exists(path)) Directory.Delete(path, true); - } - catch (Exception) - { - // do nothing - } - } - - private static X509Certificate2? LoadCertificate(string path, string password, X509KeyStorageFlags storageFlags) - { - byte[] exported; - - if (!File.Exists(path)) return null; - - try - { - exported = File.ReadAllBytes(path); - } - catch (IOException) - { - // file or directory not found - return null; - } - - return new X509Certificate2(exported, password, storageFlags); - } - - private string GetRootCertificatePath(string pathOrName, bool create) - { - if (Path.IsPathRooted(pathOrName)) return pathOrName; - - return Path.Combine(GetRootCertificateDirectory(create), - string.IsNullOrEmpty(pathOrName) ? DefaultRootCertificateFileName : pathOrName); - } - - private string GetCertificatePath(bool create) - { - var path = GetRootCertificateDirectory(create); - - var certPath = Path.Combine(path, DefaultCertificateDirectoryName); - if (create && !Directory.Exists(certPath)) Directory.CreateDirectory(certPath); - - return certPath; - } - - private string GetRootCertificateDirectory(bool create) - { - if (rootCertificatePath == null) - { - if (RunTime.IsUwpOnWindows) - { - rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProxyConfigurationFolderName); - } - else if (RunTime.IsLinux) - { - rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); - } - else if (RunTime.IsMac) - { - rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); - } - else - { - var assemblyLocation = AppContext.BaseDirectory; - - var path = Path.GetDirectoryName(assemblyLocation); - - rootCertificatePath = path ?? throw new NullReferenceException(); - } - } - - if (create && !Directory.Exists(rootCertificatePath)) - { - Directory.CreateDirectory(rootCertificatePath); - } - - return rootCertificatePath; - } +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Security.Cryptography.X509Certificates; +using Titanium.Web.Proxy.Certificates.Cache; +using Titanium.Web.Proxy.Helpers; + +namespace Microsoft.DevProxy; + +// based on https://github.com/justcoding121/titanium-web-proxy/blob/9e71608d204e5b67085656dd6b355813929801e4/src/Titanium.Web.Proxy/Certificates/Cache/DefaultCertificateDiskCache.cs +public sealed class CertificateDiskCache : ICertificateCache +{ + private const string DefaultCertificateDirectoryName = "crts"; + private const string DefaultCertificateFileExtension = ".pfx"; + private const string DefaultRootCertificateFileName = "rootCert" + DefaultCertificateFileExtension; + private const string ProxyConfigurationFolderName = "dev-proxy"; + + private string? rootCertificatePath; + + public Task LoadRootCertificateAsync(string pathOrName, string password, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) + { + var path = GetRootCertificatePath(pathOrName, false); + return Task.FromResult(LoadCertificate(path, password, storageFlags)); + } + + public Task SaveRootCertificateAsync(string pathOrName, string password, X509Certificate2 certificate, CancellationToken cancellationToken) + { + var path = GetRootCertificatePath(pathOrName, true); + var exported = certificate.Export(X509ContentType.Pkcs12, password); + File.WriteAllBytes(path, exported); + return Task.CompletedTask; + } + + public Task LoadCertificateAsync(string subjectName, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) + { + var filePath = Path.Combine(GetCertificatePath(false), subjectName + DefaultCertificateFileExtension); + return Task.FromResult(LoadCertificate(filePath, string.Empty, storageFlags)); + } + + public Task SaveCertificateAsync(string subjectName, X509Certificate2 certificate, CancellationToken cancellationToken) + { + var filePath = Path.Combine(GetCertificatePath(true), subjectName + DefaultCertificateFileExtension); + var exported = certificate.Export(X509ContentType.Pkcs12); + File.WriteAllBytes(filePath, exported); + return Task.CompletedTask; + } + + public void Clear() + { + try + { + var path = GetCertificatePath(false); + if (Directory.Exists(path)) Directory.Delete(path, true); + } + catch (Exception) + { + // do nothing + } + } + + private static X509Certificate2? LoadCertificate(string path, string password, X509KeyStorageFlags storageFlags) + { + byte[] exported; + + if (!File.Exists(path)) return null; + + try + { + exported = File.ReadAllBytes(path); + } + catch (IOException) + { + // file or directory not found + return null; + } + + return X509CertificateLoader.LoadPkcs12(exported, password, storageFlags); + } + + private string GetRootCertificatePath(string pathOrName, bool create) + { + if (Path.IsPathRooted(pathOrName)) return pathOrName; + + return Path.Combine(GetRootCertificateDirectory(create), + string.IsNullOrEmpty(pathOrName) ? DefaultRootCertificateFileName : pathOrName); + } + + private string GetCertificatePath(bool create) + { + var path = GetRootCertificateDirectory(create); + + var certPath = Path.Combine(path, DefaultCertificateDirectoryName); + if (create && !Directory.Exists(certPath)) Directory.CreateDirectory(certPath); + + return certPath; + } + + private string GetRootCertificateDirectory(bool create) + { + if (rootCertificatePath == null) + { + if (RunTime.IsUwpOnWindows) + { + rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProxyConfigurationFolderName); + } + else if (RunTime.IsLinux) + { + rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); + } + else if (RunTime.IsMac) + { + rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); + } + else + { + var assemblyLocation = AppContext.BaseDirectory; + + var path = Path.GetDirectoryName(assemblyLocation); + + rootCertificatePath = path ?? throw new NullReferenceException(); + } + } + + if (create && !Directory.Exists(rootCertificatePath)) + { + Directory.CreateDirectory(rootCertificatePath); + } + + return rootCertificatePath; + } } \ No newline at end of file diff --git a/dev-proxy/dev-proxy.csproj b/dev-proxy/dev-proxy.csproj index c025ecb9..6490e22b 100644 --- a/dev-proxy/dev-proxy.csproj +++ b/dev-proxy/dev-proxy.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 Microsoft.DevProxy enable enable @@ -30,16 +30,16 @@ - - - - - - + + + + + + - + - + diff --git a/scripts/local-build.ps1 b/scripts/local-build.ps1 index aaa62914..0075268f 100644 --- a/scripts/local-build.ps1 +++ b/scripts/local-build.ps1 @@ -5,7 +5,7 @@ Remove-Item ../bld -Recurse -Force dotnet publish ../dev-proxy/dev-proxy.csproj -c Release -p:PublishSingleFile=true -r win-x64 --self-contained -o ../bld -p:InformationalVersion=$version dotnet build ../dev-proxy-plugins/dev-proxy-plugins.csproj -c Release -r win-x64 --no-self-contained -p:InformationalVersion=$version -cp -R ../dev-proxy/bin/Release/net8.0/win-x64/plugins ../bld +cp -R ../dev-proxy/bin/Release/net9.0/win-x64/plugins ../bld pushd cd ../bld