Skip to content

Commit 13a806b

Browse files
committed
feat(proxy): add basic HTTP proxy configuration options
- Introduces `BasicHttpProxyOptions` for configuring HTTP proxies, including options for address, credentials, and bypass rules. - Adds `NetworkCredentialOptions` for managing authentication details. - Implements extension method `BuildBasicHttpProxy` to create `IWebProxy` instances from the new configuration options. Renames existing extension namespaces for better organization.
1 parent 10e024c commit 13a806b

5 files changed

Lines changed: 103 additions & 2 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using JetBrains.Annotations;
2+
3+
namespace ES.FX.Net;
4+
5+
/// <summary>
6+
/// Represents configuration options for an HTTP proxy,
7+
/// including address, bypass rules, credential usage, and authentication details.
8+
/// </summary>
9+
[PublicAPI]
10+
public class BasicHttpProxyOptions
11+
{
12+
/// <summary>
13+
/// Gets or sets the URI address of the proxy server.
14+
/// Example: <c>http://proxy.example.com:8080</c>.
15+
/// </summary>
16+
public string? Address { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets a value indicating whether to bypass the proxy server for local addresses.
20+
/// </summary>
21+
public bool BypassProxyOnLocal { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the list of addresses that should bypass the proxy server.
25+
/// Wildcard patterns can be used.
26+
/// </summary>
27+
public string[]? BypassList { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets a value indicating whether the default system credentials should be used for authentication with the proxy server.
31+
/// </summary>
32+
public bool UseDefaultCredentials { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the network credentials to use for authenticating with the proxy server.
36+
/// If <see cref="UseDefaultCredentials"/> is <c>false</c> and credentials are provided, these will be used.
37+
/// </summary>
38+
public NetworkCredentialOptions? Credentials { get; set; }
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Net;
2+
using JetBrains.Annotations;
3+
4+
namespace ES.FX.Net.Extensions;
5+
6+
[PublicAPI]
7+
public static class WebProxyExtensions
8+
{
9+
/// <summary>
10+
/// Builds an <see cref="IWebProxy"/> instance from the specified <see cref="BasicHttpProxyOptions"/>.
11+
/// </summary>
12+
/// <param name="options">The proxy options to use for building the proxy.</param>
13+
/// <returns>A configured <see cref="IWebProxy"/> instance, or <c>null</c> if no address is specified.</returns>
14+
public static IWebProxy? BuildBasicHttpProxy(this BasicHttpProxyOptions? options)
15+
{
16+
if (options == null || string.IsNullOrWhiteSpace(options.Address))
17+
return null;
18+
19+
var proxy = new WebProxy(options.Address)
20+
{
21+
BypassProxyOnLocal = options.BypassProxyOnLocal,
22+
BypassList = options.BypassList ?? [],
23+
UseDefaultCredentials = options.UseDefaultCredentials,
24+
Credentials = options.Credentials is not null
25+
? new NetworkCredential(
26+
options.Credentials.UserName,
27+
options.Credentials.Password,
28+
options.Credentials.Domain)
29+
: null
30+
};
31+
32+
return proxy;
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using JetBrains.Annotations;
2+
3+
namespace ES.FX.Net
4+
{
5+
/// <summary>
6+
/// Represents configuration options for network credentials,
7+
/// including username, password, and optional domain information.
8+
/// </summary>
9+
[PublicAPI]
10+
public class NetworkCredentialOptions
11+
{
12+
/// <summary>
13+
/// Gets or sets the username associated with the credentials.
14+
/// </summary>
15+
public string? UserName { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the password associated with the credentials.
19+
/// </summary>
20+
public string? Password { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the domain associated with the credentials.
24+
/// Used primarily for NTLM or Kerberos authentication.
25+
/// </summary>
26+
public string? Domain { get; set; }
27+
}
28+
}

src/ES.FX/Primitives/EnumExtensions.cs renamed to src/ES.FX/Primitives/Extensions/EnumExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using JetBrains.Annotations;
22

3-
namespace ES.FX.Primitives;
3+
namespace ES.FX.Primitives.Extensions;
44

55
[PublicAPI]
66
public static class EnumExtensions

src/ES.FX/Primitives/StringExtensions.cs renamed to src/ES.FX/Primitives/Extensions/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Globalization;
33
using JetBrains.Annotations;
44

5-
namespace ES.FX.Primitives;
5+
namespace ES.FX.Primitives.Extensions;
66

77
[PublicAPI]
88
public static class StringExtensions

0 commit comments

Comments
 (0)