Skip to content

Commit

Permalink
feat: Add proxy options (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo committed Jun 4, 2024
2 parents 57fe8ce + 5b1c81b commit 97bb6ef
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Options/GrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,24 @@ public bool HasClientCertificate
/// If the handler is not set, the best one will be used.
/// </summary>
public string HttpMessageHandler { get; set; } = "";

/// <summary>
/// Proxy configuration.
/// If empty, the default proxy configuration is used.
/// If "none", proxy is disabled.
/// If "system", the system proxy is used
/// Otherwise, it is the URL of the proxy to use
/// </summary>
public string Proxy { get; set; } = "";

/// <summary>
/// Username used for proxy authentication
/// </summary>
public string ProxyUsername { get; set; } = "";

/// <summary>
/// Password used for proxy authentication
/// </summary>
public string ProxyPassword { get; set; } = "";
}
}
97 changes: 97 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Submitter/GrpcChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ public static class GrpcChannelFactory
/// <param name="insecure">Whether the Server Certificate should be validated or not</param>
/// <param name="caCert">Root certificate to validate the server certificate against</param>
/// <param name="clientCert">Client certificate to be used for mTLS</param>
/// <param name="proxy">Proxy configuration to use</param>
/// <param name="proxyType">Type of the proxy used</param>
/// <param name="handlerType">Which HttpMessageHandler type to use</param>
/// <param name="logger">Optional logger</param>
/// <returns>HttpMessageHandler</returns>
private static HttpMessageHandler CreateHttpMessageHandler(bool https,
bool insecure,
X509Certificate? caCert,
X509Certificate2? clientCert,
IWebProxy? proxy,
ProxyType proxyType,
HandlerType handlerType,
ILogger? logger = null)
{
Expand All @@ -165,6 +169,22 @@ private static HttpMessageHandler CreateHttpMessageHandler(bool htt
httpHandler.SslProtocols = sslProtocols;
httpHandler.ServerCertificateCustomValidationCallback = validationCallback;

switch (proxyType)
{
case ProxyType.None:
httpHandler.Proxy = null;
httpHandler.UseProxy = false;
break;
case ProxyType.System:
httpHandler.Proxy = null;
httpHandler.UseProxy = true;
break;
case ProxyType.Custom:
httpHandler.Proxy = proxy;
httpHandler.UseProxy = true;
break;
}

if (clientCert is not null)
{
httpHandler.ClientCertificates.Add(clientCert);
Expand All @@ -191,6 +211,22 @@ private static HttpMessageHandler CreateHttpMessageHandler(bool htt
}
}

switch (proxyType)
{
case ProxyType.None:
winHandler.Proxy = null;
winHandler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.DoNotUseProxy;
break;
case ProxyType.System:
winHandler.Proxy = null;
winHandler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinHttpProxy;
break;
case ProxyType.Custom:
winHandler.Proxy = proxy;
winHandler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseCustomProxy;
break;
}

if (handlerType is HandlerType.Web)
{
return new GrpcWebHandler(winHandler);
Expand Down Expand Up @@ -292,6 +328,42 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
? GetCertificate(optionsGrpcClient)
: null;

ICredentials? proxyCredentials = null;
if (!string.IsNullOrEmpty(optionsGrpcClient.ProxyUsername) || !string.IsNullOrEmpty(optionsGrpcClient.ProxyPassword))
{
proxyCredentials = new NetworkCredential(optionsGrpcClient.ProxyUsername,
optionsGrpcClient.ProxyPassword);
}

IWebProxy? proxy = null;
ProxyType proxyType;

switch (optionsGrpcClient.Proxy)
{
case "":
proxyType = ProxyType.Undefined;
break;
case "none":
case "None":
logger?.LogDebug("Unsetting proxy for the gRPC channel");
proxyType = ProxyType.None;
break;
case "system":
case "System":
logger?.LogDebug("Using system proxy for the gRPC channel");
proxyType = ProxyType.System;
break;
default:
logger?.LogDebug("Using custom proxy for the gRPC channel: {Proxy}",
optionsGrpcClient.Proxy);
proxyType = ProxyType.Custom;
proxy = new WebProxy(optionsGrpcClient.Proxy,
false,
Array.Empty<string>(),
proxyCredentials);
break;
}

switch (handlerType)
{
case HandlerType.Http:
Expand All @@ -312,6 +384,8 @@ private static GrpcChannel CreateChannelInternal(GrpcClient optionsGrpcClient,
optionsGrpcClient.AllowUnsafeConnection,
caCert,
clientCert,
proxy,
proxyType,
handlerType,
logger);

Expand Down Expand Up @@ -510,6 +584,29 @@ public static X509Certificate2 GetCertificate(GrpcClient optionsGrpcClient)
_ => throw new ArgumentException($"Invalid HandlerType: {handler}"),
};

private enum ProxyType
{
/// <summary>
/// No proxy configuration defined
/// </summary>
Undefined,

/// <summary>
/// Do not use any proxy, even if the system has one configured
/// </summary>
None,

/// <summary>
/// Use the proxy configured on the system
/// </summary>
System,

/// <summary>
/// Use a custom proxy
/// </summary>
Custom,
}

private enum HandlerType
{
/// <summary>
Expand Down

0 comments on commit 97bb6ef

Please sign in to comment.