Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 347c310

Browse files
authored
Merge pull request #662 from ByronAP/use-interfaces
ExternalProxy interface
2 parents 21f8190 + 9727950 commit 347c310

File tree

8 files changed

+55
-18
lines changed

8 files changed

+55
-18
lines changed

src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public bool EnableWinAuth
106106
/// <summary>
107107
/// Are we using a custom upstream HTTP(S) proxy?
108108
/// </summary>
109-
public ExternalProxy? CustomUpStreamProxyUsed { get; internal set; }
109+
public IExternalProxy? CustomUpStreamProxyUsed { get; internal set; }
110110

111111
/// <summary>
112112
/// Local endpoint via which we make the request.

src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
9595
return true;
9696
}
9797

98-
public ExternalProxy? GetProxy(Uri destination)
98+
public IExternalProxy? GetProxy(Uri destination)
9999
{
100100
if (GetAutoProxies(destination, out var proxies))
101101
{

src/Titanium.Web.Proxy/Models/ExternalProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Titanium.Web.Proxy.Models
66
/// <summary>
77
/// An upstream proxy this proxy uses if any.
88
/// </summary>
9-
public class ExternalProxy
9+
public class ExternalProxy : IExternalProxy
1010
{
1111
private static readonly Lazy<NetworkCredential> defaultCredentials =
1212
new Lazy<NetworkCredential>(() => CredentialCache.DefaultNetworkCredentials);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Titanium.Web.Proxy.Models
2+
{
3+
public interface IExternalProxy
4+
{
5+
/// <summary>
6+
/// Use default windows credentials?
7+
/// </summary>
8+
bool UseDefaultCredentials { get; set; }
9+
10+
/// <summary>
11+
/// Bypass this proxy for connections to localhost?
12+
/// </summary>
13+
bool BypassLocalhost { get; set; }
14+
15+
/// <summary>
16+
/// Username.
17+
/// </summary>
18+
string? UserName { get; set; }
19+
20+
/// <summary>
21+
/// Password.
22+
/// </summary>
23+
string? Password { get; set; }
24+
25+
/// <summary>
26+
/// Host name.
27+
/// </summary>
28+
string HostName { get; set; }
29+
30+
/// <summary>
31+
/// Port.
32+
/// </summary>
33+
int Port { get; set; }
34+
35+
string ToString();
36+
}
37+
}

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal TcpConnectionFactory(ProxyServer server)
4949

5050
internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
5151
bool isHttps, List<SslApplicationProtocol>? applicationProtocols,
52-
IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy)
52+
IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy)
5353
{
5454
// http version is ignored since its an application level decision b/w HTTP 1.0/1.1
5555
// also when doing connect request MS Edge browser sends http 1.0 but uses 1.1 after server sends 1.1 its response.
@@ -115,7 +115,7 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
115115
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
116116
}
117117

118-
ExternalProxy? customUpStreamProxy = null;
118+
IExternalProxy? customUpStreamProxy = null;
119119

120120
bool isHttps = session.IsHttps;
121121
if (server.GetCustomUpStreamProxyFunc != null)
@@ -170,7 +170,7 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer server, Sessi
170170
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect,
171171
List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken)
172172
{
173-
ExternalProxy? customUpStreamProxy = null;
173+
IExternalProxy? customUpStreamProxy = null;
174174

175175
bool isHttps = session.IsHttps;
176176
if (server.GetCustomUpStreamProxyFunc != null)
@@ -208,7 +208,7 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,
208208
/// <returns></returns>
209209
internal async Task<TcpServerConnection> GetServerConnection(string remoteHostName, int remotePort,
210210
Version httpVersion, bool isHttps, List<SslApplicationProtocol>? applicationProtocols, bool isConnect,
211-
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy,
211+
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy,
212212
bool noCache, CancellationToken cancellationToken)
213213
{
214214
var sslProtocol = session?.ProxyClient.Connection.SslProtocol ?? SslProtocols.None;
@@ -262,7 +262,7 @@ internal async Task<TcpServerConnection> GetServerConnection(string remoteHostNa
262262
/// <returns></returns>
263263
private async Task<TcpServerConnection> createServerConnection(string remoteHostName, int remotePort,
264264
Version httpVersion, bool isHttps, SslProtocols sslProtocol, List<SslApplicationProtocol>? applicationProtocols, bool isConnect,
265-
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, ExternalProxy? externalProxy, string cacheKey,
265+
ProxyServer proxyServer, SessionEventArgsBase? session, IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy, string cacheKey,
266266
CancellationToken cancellationToken)
267267
{
268268
// deny connection to proxy end points to avoid infinite connection loop.
@@ -304,7 +304,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
304304
bool retry = true;
305305
var enabledSslProtocols = sslProtocol;
306306

307-
retry:
307+
retry:
308308
try
309309
{
310310
string hostname = useUpstreamProxy ? externalProxy!.HostName : remoteHostName;

src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class TcpServerConnection : IDisposable
1717
{
1818
internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, CustomBufferedStream stream,
1919
string hostName, int port, bool isHttps, SslApplicationProtocol negotiatedApplicationProtocol,
20-
Version version, bool useUpstreamProxy, ExternalProxy? upStreamProxy, IPEndPoint? upStreamEndPoint, string cacheKey)
20+
Version version, bool useUpstreamProxy, IExternalProxy? upStreamProxy, IPEndPoint? upStreamEndPoint, string cacheKey)
2121
{
2222
this.tcpClient = tcpClient;
2323
LastAccess = DateTime.Now;
@@ -41,7 +41,7 @@ internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, Custo
4141

4242
internal bool IsClosed => Stream.IsClosed;
4343

44-
internal ExternalProxy? UpStreamProxy { get; set; }
44+
internal IExternalProxy? UpStreamProxy { get; set; }
4545

4646
internal string HostName { get; set; }
4747

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public partial class ProxyServer : IDisposable
6161
/// </summary>
6262
private WinHttpWebProxyFinder? systemProxyResolver;
6363

64-
64+
6565
/// <inheritdoc />
6666
/// <summary>
6767
/// Initializes a new instance of ProxyServer class with provided parameters.
@@ -145,7 +145,7 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
145145
/// Defaults to false.
146146
/// </summary>
147147
public bool EnableWinAuth { get; set; }
148-
148+
149149
/// <summary>
150150
/// Enable disable HTTP/2 support.
151151
/// Warning: HTTP/2 support is very limited
@@ -253,12 +253,12 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
253253
/// <summary>
254254
/// External proxy used for Http requests.
255255
/// </summary>
256-
public ExternalProxy? UpStreamHttpProxy { get; set; }
256+
public IExternalProxy? UpStreamHttpProxy { get; set; }
257257

258258
/// <summary>
259259
/// External proxy used for Https requests.
260260
/// </summary>
261-
public ExternalProxy? UpStreamHttpsProxy { get; set; }
261+
public IExternalProxy? UpStreamHttpsProxy { get; set; }
262262

263263
/// <summary>
264264
/// Local adapter/NIC endpoint where proxy makes request via.
@@ -275,7 +275,7 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
275275
/// A callback to provide authentication credentials for up stream proxy this proxy is using for HTTP(S) requests.
276276
/// User should return the ExternalProxy object with valid credentials.
277277
/// </summary>
278-
public Func<SessionEventArgsBase, Task<ExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; }
278+
public Func<SessionEventArgsBase, Task<IExternalProxy?>>? GetCustomUpStreamProxyFunc { get; set; }
279279

280280
/// <summary>
281281
/// Callback for error events in this proxy instance.
@@ -709,7 +709,7 @@ private void validateEndPointAsSystemProxy(ExplicitProxyEndPoint endPoint)
709709
/// </summary>
710710
/// <param name="sessionEventArgs">The session.</param>
711711
/// <returns>The external proxy as task result.</returns>
712-
private Task<ExternalProxy?> getSystemUpStreamProxy(SessionEventArgsBase sessionEventArgs)
712+
private Task<IExternalProxy?> getSystemUpStreamProxy(SessionEventArgsBase sessionEventArgs)
713713
{
714714
var proxy = systemProxyResolver!.GetProxy(sessionEventArgs.HttpClient.Request.RequestUri);
715715
return Task.FromResult(proxy);

tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData()
6969
};
7070

7171
var client = testSuite.GetClient(proxy1, true);
72-
72+
7373
var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
7474
new StringContent("hello server. I am a client."));
7575

0 commit comments

Comments
 (0)