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

Commit 8cbf0f4

Browse files
authored
Merge pull request #710 from justcoding121/master
beta
2 parents 312dca6 + d21ea35 commit 8cbf0f4

File tree

8 files changed

+71
-36
lines changed

8 files changed

+71
-36
lines changed

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

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

210+
var clientLocalIp = e.ClientLocalEndPoint.Address;
211+
if (!clientLocalIp.Equals(IPAddress.Loopback) && !clientLocalIp.Equals(IPAddress.IPv6Loopback))
212+
{
213+
e.HttpClient.UpStreamEndPoint = new IPEndPoint(clientLocalIp, 0);
214+
}
215+
216+
if (e.HttpClient.Request.Url.Contains("yahoo.com"))
217+
{
218+
e.CustomUpStreamProxy = new ExternalProxy("localhost", 8888);
219+
}
220+
210221
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
211222
await writeToConsole(e.HttpClient.Request.Url);
212223

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoin
5959

6060
ClientStream = clientStream;
6161
HttpClient = new HttpWebClient(connectRequest, request, new Lazy<int>(() => clientStream.Connection.GetProcessId(endPoint)));
62-
LocalEndPoint = endPoint;
62+
ProxyEndPoint = endPoint;
6363
EnableWinAuth = server.EnableWinAuth && isWindowsAuthenticationSupported;
6464
}
6565

@@ -103,6 +103,9 @@ public bool EnableWinAuth
103103
/// </summary>
104104
public IPEndPoint ClientRemoteEndPoint => (IPEndPoint)ClientConnection.RemoteEndPoint;
105105

106+
[Obsolete("Use ClientRemoteEndPoint instead.")]
107+
public IPEndPoint ClientEndPoint => ClientRemoteEndPoint;
108+
106109
/// <summary>
107110
/// The web client used to communicate with server for this session.
108111
/// </summary>
@@ -111,6 +114,14 @@ public bool EnableWinAuth
111114
[Obsolete("Use HttpClient instead.")]
112115
public HttpWebClient WebSession => HttpClient;
113116

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+
114125
/// <summary>
115126
/// Are we using a custom upstream HTTP(S) proxy?
116127
/// </summary>
@@ -119,12 +130,15 @@ public bool EnableWinAuth
119130
/// <summary>
120131
/// Local endpoint via which we make the request.
121132
/// </summary>
122-
public ProxyEndPoint LocalEndPoint { get; }
133+
public ProxyEndPoint ProxyEndPoint { get; }
134+
135+
[Obsolete("Use ProxyEndPoint instead.")]
136+
public ProxyEndPoint LocalEndPoint => ProxyEndPoint;
123137

124138
/// <summary>
125139
/// Is this a transparent endpoint?
126140
/// </summary>
127-
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
141+
public bool IsTransparent => ProxyEndPoint is TransparentProxyEndPoint;
128142

129143
/// <summary>
130144
/// The last exception that happened.

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
}

src/Titanium.Web.Proxy/WinAuthHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private async Task rewriteUnauthorizedResponse(SessionEventArgs args)
175175
// Add custom div to body to clarify that the proxy (not the client browser) failed authentication
176176
string authErrorMessage =
177177
"<div class=\"inserted-by-proxy\"><h2>NTLM authentication through Titanium.Web.Proxy (" +
178-
args.ClientConnection.LocalEndPoint +
178+
args.ClientLocalEndPoint +
179179
") failed. Please check credentials.</h2></div>";
180180
string originalErrorMessage =
181181
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>";

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)