Skip to content

Commit

Permalink
Switches to Unobtanium.Web.Proxy (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored Jul 12, 2024
1 parent da21c6e commit 95663aa
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dev-proxy-abstractions/dev-proxy-abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.15" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Titanium.Web.Proxy" Version="3.2.0" />
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Behavior/RetryAfterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private Task OnRequest(object? sender, ProxyRequestArgs e)
{
if (e.ResponseState.HasBeenSet ||
UrlsToWatch is null ||
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS" ||
String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) ||
!e.ShouldExecute(UrlsToWatch))
{
return Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Guidance/CachingGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private Task BeforeRequest(object? sender, ProxyRequestArgs e)
{
if (UrlsToWatch is null ||
!e.HasRequestUrlMatch(UrlsToWatch) ||
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS")
String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Task AfterResponse(object? sender, ProxyResponseArgs e)
Request request = e.Session.HttpClient.Request;
if (UrlsToWatch is not null &&
e.HasRequestUrlMatch(UrlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
!String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) &&
ProxyUtils.IsGraphBetaRequest(request))
Logger.LogRequest(BuildBetaSupportMessage(request), MessageType.Warning, new LoggingContext(e.Session));
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Task BeforeRequest(object? sender, ProxyRequestArgs e)
Request request = e.Session.HttpClient.Request;
if (UrlsToWatch is not null &&
e.HasRequestUrlMatch(UrlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
!String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) &&
WarnNoClientRequestId(request))
{
Logger.LogRequest(BuildAddClientRequestIdMessage(request), MessageType.Warning, new LoggingContext(e.Session));
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Guidance/GraphConnectorGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private Task BeforeRequest(object sender, ProxyRequestArgs e)
{
if (UrlsToWatch is null ||
!e.HasRequestUrlMatch(UrlsToWatch) ||
e.Session.HttpClient.Request.Method.ToUpper() != "PATCH")
!String.Equals(e.Session.HttpClient.Request.Method, "PATCH", StringComparison.OrdinalIgnoreCase))
{
return Task.CompletedTask;
}
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private Task OnAfterResponse(object? sender, ProxyResponseArgs e)
if (e.Session.HttpClient.Response.StatusCode >= 400 &&
UrlsToWatch is not null &&
e.HasRequestUrlMatch(UrlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
!String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) &&
WarnNoSdk(request))
{
Logger.LogRequest(MessageUtils.BuildUseSdkForErrorsMessage(request), MessageType.Tip, new LoggingContext(e.Session));
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Guidance/GraphSelectGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private Task AfterResponse(object? sender, ProxyResponseArgs e)
Request request = e.Session.HttpClient.Request;
if (UrlsToWatch is not null &&
e.HasRequestUrlMatch(UrlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
!String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) &&
WarnNoSelect(request))
Logger.LogRequest(BuildUseSelectMessage(request), MessageType.Warning, new LoggingContext(e.Session));

Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Task BeforeRequest(object sender, ProxyRequestArgs e)
Request request = e.Session.HttpClient.Request;
if (UrlsToWatch is not null &&
e.HasRequestUrlMatch(UrlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
!String.Equals(e.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase) &&
WarnDeprecatedSearch(request))
Logger.LogRequest(BuildUseGraphSearchMessage(), MessageType.Warning, new LoggingContext(e.Session));

Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/Mocks/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected virtual Task OnRequest(object? sender, ProxyRequestArgs e)
Request = new()
{
Url = request.Url,
Method = request.Method
Method = request.Method ?? ""
},
Response = new()
{
Expand Down
3 changes: 2 additions & 1 deletion dev-proxy-plugins/Mocks/OpenAIMockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ private async Task OnRequest(object sender, ProxyRequestArgs e)
using var scope = Logger.BeginScope(Name);

var request = e.Session.HttpClient.Request;
if (!request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase) ||
if (request.Method is null ||
!request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase) ||
!request.HasBody)
{
return;
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public GraphRandomErrorPlugin(IPluginEvents pluginEvents, IProxyContext context,
private void FailResponse(ProxyRequestArgs e)
{
// pick a random error response for the current request method
var methodStatusCodes = _methodStatusCode[e.Session.HttpClient.Request.Method];
var methodStatusCodes = _methodStatusCode[e.Session.HttpClient.Request.Method ?? "GET"];
var errorStatus = methodStatusCodes[_random.Next(0, methodStatusCodes.Length)];
UpdateProxyResponse(e, errorStatus);
}
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/RequestLogs/HttpFileGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ request.Context is null ||
}

if (!_configuration.IncludeOptionsRequests &&
request.Context.Session.HttpClient.Request.Method.ToUpperInvariant() == "OPTIONS")
String.Equals(request.Context.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
{
Logger.LogDebug("Skipping OPTIONS request {url}...", request.Context.Session.HttpClient.Request.RequestUri);
continue;
Expand Down
4 changes: 2 additions & 2 deletions dev-proxy-plugins/RequestLogs/OpenApiSpecGeneratorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ request.Context is null ||
}

if (!_configuration.IncludeOptionsRequests &&
request.Context.Session.HttpClient.Request.Method.ToUpperInvariant() == "OPTIONS")
String.Equals(request.Context.Session.HttpClient.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
{
Logger.LogDebug("Skipping OPTIONS request {url}...", request.Context.Session.HttpClient.Request.RequestUri);
continue;
Expand Down Expand Up @@ -480,7 +480,7 @@ private OpenApiPathItem GetOpenApiPathItem(SessionEventArgs session)
var resource = GetLastNonTokenSegment(request.RequestUri.Segments);
var path = new OpenApiPathItem();

var method = request.Method.ToUpperInvariant() switch
var method = request.Method?.ToUpperInvariant() switch
{
"DELETE" => OperationType.Delete,
"GET" => OperationType.Get,
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy-plugins/dev-proxy-plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Titanium.Web.Proxy" Version="3.2.0">
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
Expand Down
18 changes: 9 additions & 9 deletions dev-proxy/CertificateDiskCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License.

using System.Security.Cryptography.X509Certificates;
using Titanium.Web.Proxy.Certificates.Cache;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Network;

namespace Microsoft.DevProxy;

Expand All @@ -17,32 +17,32 @@ public sealed class CertificateDiskCache : ICertificateCache

private string? rootCertificatePath;

public X509Certificate2? LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags)
public Task<X509Certificate2?> LoadRootCertificateAsync(string pathOrName, string password, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken)
{
var path = GetRootCertificatePath(pathOrName, false);
return LoadCertificate(path, password, storageFlags);
return Task.FromResult(LoadCertificate(path, password, storageFlags));
}

public void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate)
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;
}

/// <inheritdoc />
public X509Certificate2? LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
public Task<X509Certificate2?> LoadCertificateAsync(string subjectName, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken)
{
var filePath = Path.Combine(GetCertificatePath(false), subjectName + DefaultCertificateFileExtension);
return LoadCertificate(filePath, string.Empty, storageFlags);
return Task.FromResult(LoadCertificate(filePath, string.Empty, storageFlags));
}

/// <inheritdoc />
public void SaveCertificate(string subjectName, X509Certificate2 certificate)
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()
Expand Down
20 changes: 11 additions & 9 deletions dev-proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static ProxyEngine()
// we need to change this to a value lower than 397
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
_proxyServer.CertificateManager.CertificateValidDays = 365;
_proxyServer.CertificateManager.CreateRootCertificate();
_ = _proxyServer.CertificateManager.LoadOrCreateRootCertificateAsync().Result;
}

public ProxyEngine(ProxyConfiguration config, ISet<UrlToWatch> urlsToWatch, PluginEvents pluginEvents, ILogger logger)
Expand Down Expand Up @@ -116,15 +116,17 @@ public async Task Run(CancellationToken? cancellationToken)
_explicitEndPoint.BeforeTunnelConnectRequest += OnBeforeTunnelConnectRequest;
if (_config.InstallCert)
{
_proxyServer.CertificateManager.EnsureRootCertificate();
await _proxyServer.CertificateManager.EnsureRootCertificateAsync();
}
else
{
_explicitEndPoint.GenericCertificate = _proxyServer.CertificateManager.LoadRootCertificate();
_explicitEndPoint.GenericCertificate = await _proxyServer
.CertificateManager
.LoadRootCertificateAsync(cancellationToken ?? CancellationToken.None);
}

_proxyServer.AddEndPoint(_explicitEndPoint);
_proxyServer.Start();
await _proxyServer.StartAsync();

// run first-run setup on macOS
FirstRunSetup();
Expand Down Expand Up @@ -436,7 +438,7 @@ private int GetProcessId(TunnelConnectSessionEventArgs e)
var psi = new ProcessStartInfo
{
FileName = "lsof",
Arguments = $"-i :{e.ClientRemoteEndPoint.Port}",
Arguments = $"-i :{e.ClientRemoteEndPoint?.Port}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
Expand All @@ -450,7 +452,7 @@ private int GetProcessId(TunnelConnectSessionEventArgs e)
proc.WaitForExit();

var lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var matchingLine = lines.FirstOrDefault(l => l.Contains($"{e.ClientRemoteEndPoint.Port}->"));
var matchingLine = lines.FirstOrDefault(l => l.Contains($"{e.ClientRemoteEndPoint?.Port}->"));
if (matchingLine is null)
{
return -1;
Expand Down Expand Up @@ -530,7 +532,7 @@ async Task OnRequest(object sender, SessionEventArgs e)
await e.GetRequestBodyAsString();
}

using var scope = _logger.BeginScope(e.HttpClient.Request.Method, e.HttpClient.Request.Url, e.GetHashCode());
using var scope = _logger.BeginScope(e.HttpClient.Request.Method ?? "", e.HttpClient.Request.Url, e.GetHashCode());

e.UserData = e.HttpClient.Request;
_logger.LogRequest(new[] { $"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}" }, MessageType.InterceptedRequest, new LoggingContext(e));
Expand Down Expand Up @@ -608,7 +610,7 @@ async Task OnBeforeResponse(object sender, SessionEventArgs e)
return;
}

using var scope = _logger.BeginScope(e.HttpClient.Request.Method, e.HttpClient.Request.Url, e.GetHashCode());
using var scope = _logger.BeginScope(e.HttpClient.Request.Method ?? "", e.HttpClient.Request.Url, e.GetHashCode());

// necessary to make the response body available to plugins
e.HttpClient.Response.KeepBody = true;
Expand Down Expand Up @@ -641,7 +643,7 @@ async Task OnAfterResponse(object sender, SessionEventArgs e)
// of mocked requests available to plugins
e.HttpClient.Response.KeepBody = true;

using var scope = _logger.BeginScope(e.HttpClient.Request.Method, e.HttpClient.Request.Url, e.GetHashCode());
using var scope = _logger.BeginScope(e.HttpClient.Request.Method ?? "", e.HttpClient.Request.Url, e.GetHashCode());

var message = $"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}";
_logger.LogRequest([message], MessageType.InterceptedResponse, new LoggingContext(e));
Expand Down
2 changes: 1 addition & 1 deletion dev-proxy/dev-proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.3" />
<PackageReference Include="Titanium.Web.Proxy" Version="3.2.0" />
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 95663aa

Please sign in to comment.