Skip to content

Commit

Permalink
Updates to .net 9. Closes #934 (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored Nov 14, 2024
1 parent 5d5b3a7 commit 0a1b61a
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions dev-proxy-abstractions/dev-proxy-abstractions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Microsoft.DevProxy.Abstractions</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5" />
Expand Down
8 changes: 4 additions & 4 deletions dev-proxy-plugins/dev-proxy-plugins.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Microsoft.DevProxy.Plugins</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -17,15 +17,15 @@
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10">
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.1.2">
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.2.0">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
Expand Down
260 changes: 130 additions & 130 deletions dev-proxy/CertificateDiskCache.cs
Original file line number Diff line number Diff line change
@@ -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<X509Certificate2?> 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<X509Certificate2?> 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<X509Certificate2?> 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<X509Certificate2?> 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;
}
}
Loading

0 comments on commit 0a1b61a

Please sign in to comment.