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

Commit 3d74694

Browse files
committed
Allow to set upstream proxy in before request handler
1 parent b8cefa4 commit 3d74694

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ private async Task onRequest(object sender, SessionEventArgs e)
207207
{
208208
e.GetState().PipelineInfo.AppendLine(nameof(onRequest) + ":" + e.HttpClient.Request.RequestUri);
209209

210+
if (e.HttpClient.Request.Url.Contains("yahoo.com"))
211+
{
212+
e.CustomUpStreamProxy = new ExternalProxy("localhost", 8888);
213+
}
214+
210215
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
211216
await writeToConsole(e.HttpClient.Request.Url);
212217

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public bool EnableWinAuth
114114
[Obsolete("Use HttpClient instead.")]
115115
public HttpWebClient WebSession => HttpClient;
116116

117+
/// <summary>
118+
/// Gets or sets the custom up stream proxy.
119+
/// </summary>
120+
/// <value>
121+
/// The custom up stream proxy.
122+
/// </value>
123+
public IExternalProxy? CustomUpStreamProxy { get; set; }
124+
117125
/// <summary>
118126
/// Are we using a custom upstream HTTP(S) proxy?
119127
/// </summary>

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
114114
}
115115

116116
// TODO: Apply authorization
117-
var systemProxy = new ExternalProxy
118-
{
119-
HostName = proxyStr,
120-
Port = port
121-
};
117+
var systemProxy = new ExternalProxy(proxyStr, port);
122118

123119
return systemProxy;
124120
}
@@ -134,12 +130,7 @@ public bool GetAutoProxies(Uri destination, out IList<string>? proxyList)
134130
HttpSystemProxyValue? value = null;
135131
if (ProxyInfo?.Proxies?.TryGetValue(protocolType.Value, out value) == true)
136132
{
137-
var systemProxy = new ExternalProxy
138-
{
139-
HostName = value!.HostName,
140-
Port = value.Port
141-
};
142-
133+
var systemProxy = new ExternalProxy(value!.HostName, value.Port);
143134
return systemProxy;
144135
}
145136
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ public string? Password
6969
/// </summary>
7070
public int Port { get; set; }
7171

72+
/// <summary>
73+
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
74+
/// </summary>
75+
public ExternalProxy()
76+
{
77+
}
78+
79+
/// <summary>
80+
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
81+
/// </summary>
82+
/// <param name="hostName">Name of the host.</param>
83+
/// <param name="port">The port.</param>
84+
public ExternalProxy(string hostName, int port)
85+
{
86+
HostName = hostName;
87+
Port = port;
88+
}
89+
90+
/// <summary>
91+
/// Initializes a new instance of the <see cref="ExternalProxy"/> class.
92+
/// </summary>
93+
/// <param name="hostName">Name of the host.</param>
94+
/// <param name="port">The port.</param>
95+
/// <param name="userName">Name of the user.</param>
96+
/// <param name="password">The password.</param>
97+
public ExternalProxy(string hostName, int port, string userName, string password)
98+
{
99+
HostName = hostName;
100+
Port = port;
101+
UserName = userName;
102+
Password = password;
103+
}
104+
72105
/// <summary>
73106
/// returns data in Hostname:port format.
74107
/// </summary>
@@ -77,6 +110,5 @@ public override string ToString()
77110
{
78111
return $"{HostName}:{Port}";
79112
}
80-
81113
}
82114
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
114114
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
115115
}
116116

117-
IExternalProxy? customUpStreamProxy = null;
117+
IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy;
118118

119119
bool isHttps = session.IsHttps;
120-
if (server.GetCustomUpStreamProxyFunc != null)
120+
if (customUpStreamProxy == null && server.GetCustomUpStreamProxyFunc != null)
121121
{
122122
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(session);
123123
}
@@ -169,10 +169,10 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
169169
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer, SessionEventArgsBase session, bool isConnect,
170170
List<SslApplicationProtocol>? applicationProtocols, bool noCache, CancellationToken cancellationToken)
171171
{
172-
IExternalProxy? customUpStreamProxy = null;
172+
IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy;
173173

174174
bool isHttps = session.IsHttps;
175-
if (proxyServer.GetCustomUpStreamProxyFunc != null)
175+
if (customUpStreamProxy == null && proxyServer.GetCustomUpStreamProxyFunc != null)
176176
{
177177
customUpStreamProxy = await proxyServer.GetCustomUpStreamProxyFunc(session);
178178
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData()
5959
{
6060
Assert.AreEqual("Test", session.UserData);
6161

62-
return await Task.FromResult(new Models.ExternalProxy
63-
{
64-
HostName = "localhost",
65-
Port = proxy2.ProxyEndPoints[0].Port
66-
});
62+
return await Task.FromResult(new Models.ExternalProxy("localhost", proxy2.ProxyEndPoints[0].Port));
6763
};
6864

6965
var client = testSuite.GetClient(proxy1, true);

tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,8 @@ public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null)
2222

2323
if (upStreamProxy != null)
2424
{
25-
ProxyServer.UpStreamHttpProxy = new ExternalProxy
26-
{
27-
HostName = "localhost",
28-
Port = upStreamProxy.ProxyEndPoints[0].Port
29-
};
30-
31-
ProxyServer.UpStreamHttpsProxy = new ExternalProxy
32-
{
33-
HostName = "localhost",
34-
Port = upStreamProxy.ProxyEndPoints[0].Port
35-
};
25+
ProxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", upStreamProxy.ProxyEndPoints[0].Port);
26+
ProxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", upStreamProxy.ProxyEndPoints[0].Port);
3627
}
3728

3829
ProxyServer.Start();

0 commit comments

Comments
 (0)