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

Commit 066d430

Browse files
committed
#495 Add Dns Resolution datetime to timeline
1 parent 87ec92c commit 066d430

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"**/*.Basic.csproj/": true,
1616
"**/*.Docs.csproj/": true,
1717
"**/*.Proxy.csproj/": true,
18-
"**/*.Proxy.csproj" : true
18+
"**/*.Mono.csproj" : true
1919
},
2020
"search.exclude": {
2121
"**/.build": true,
@@ -32,6 +32,6 @@
3232
"**/*.Basic.csproj/": true,
3333
"**/*.Docs.csproj/": true,
3434
"**/*.Proxy.csproj/": true,
35-
"**/*.Proxy.csproj" : true
35+
"**/*.Mono.csproj" : true
3636
}
3737
}

omnisharp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fileOptions": {
33
"excludeSearchPatterns": [
4+
"**/*.sln",
45
"**/*.Docs.csproj",
56
"**/tests/",
67
"**/Titanium.Web.Proxy.Examples.Wpf/",

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
7676
/// <summary>
7777
/// Gets the connection cache key.
7878
/// </summary>
79-
/// <param name="args">The session event arguments.</param>
79+
/// <param name="session">The session event arguments.</param>
8080
/// <param name="applicationProtocol"></param>
8181
/// <returns></returns>
82-
internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEventArgsBase args,
82+
internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEventArgsBase session,
8383
SslApplicationProtocol applicationProtocol)
8484
{
8585
List<SslApplicationProtocol> applicationProtocols = null;
@@ -90,32 +90,32 @@ internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEve
9090

9191
ExternalProxy customUpStreamProxy = null;
9292

93-
bool isHttps = args.IsHttps;
93+
bool isHttps = session.IsHttps;
9494
if (server.GetCustomUpStreamProxyFunc != null)
9595
{
96-
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(args);
96+
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(session);
9797
}
9898

99-
args.CustomUpStreamProxyUsed = customUpStreamProxy;
99+
session.CustomUpStreamProxyUsed = customUpStreamProxy;
100100

101101
return GetConnectionCacheKey(
102-
args.WebSession.Request.RequestUri.Host,
103-
args.WebSession.Request.RequestUri.Port,
102+
session.WebSession.Request.RequestUri.Host,
103+
session.WebSession.Request.RequestUri.Port,
104104
isHttps, applicationProtocols,
105-
server, args.WebSession.UpStreamEndPoint ?? server.UpStreamEndPoint,
105+
server, session.WebSession.UpStreamEndPoint ?? server.UpStreamEndPoint,
106106
customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy));
107107
}
108108

109109

110110
/// <summary>
111111
/// Create a server connection.
112112
/// </summary>
113-
/// <param name="args">The session event arguments.</param>
113+
/// <param name="session">The session event arguments.</param>
114114
/// <param name="isConnect">Is this a CONNECT request.</param>
115115
/// <param name="applicationProtocol"></param>
116116
/// <param name="cancellationToken">The cancellation token for this async task.</param>
117117
/// <returns></returns>
118-
internal Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase args, bool isConnect,
118+
internal Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect,
119119
SslApplicationProtocol applicationProtocol, bool noCache, CancellationToken cancellationToken)
120120
{
121121
List<SslApplicationProtocol> applicationProtocols = null;
@@ -124,36 +124,36 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer server, Sessi
124124
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
125125
}
126126

127-
return GetServerConnection(server, args, isConnect, applicationProtocols, noCache, cancellationToken);
127+
return GetServerConnection(server, session, isConnect, applicationProtocols, noCache, cancellationToken);
128128
}
129129

130130
/// <summary>
131131
/// Create a server connection.
132132
/// </summary>
133-
/// <param name="args">The session event arguments.</param>
133+
/// <param name="session">The session event arguments.</param>
134134
/// <param name="isConnect">Is this a CONNECT request.</param>
135135
/// <param name="applicationProtocols"></param>
136136
/// <param name="cancellationToken">The cancellation token for this async task.</param>
137137
/// <returns></returns>
138-
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase args, bool isConnect,
138+
internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server, SessionEventArgsBase session, bool isConnect,
139139
List<SslApplicationProtocol> applicationProtocols, bool noCache, CancellationToken cancellationToken)
140140
{
141141
ExternalProxy customUpStreamProxy = null;
142142

143-
bool isHttps = args.IsHttps;
143+
bool isHttps = session.IsHttps;
144144
if (server.GetCustomUpStreamProxyFunc != null)
145145
{
146-
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(args);
146+
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(session);
147147
}
148148

149-
args.CustomUpStreamProxyUsed = customUpStreamProxy;
149+
session.CustomUpStreamProxyUsed = customUpStreamProxy;
150150

151151
return await GetServerConnection(
152-
args.WebSession.Request.RequestUri.Host,
153-
args.WebSession.Request.RequestUri.Port,
154-
args.WebSession.Request.HttpVersion,
152+
session.WebSession.Request.RequestUri.Host,
153+
session.WebSession.Request.RequestUri.Port,
154+
session.WebSession.Request.HttpVersion,
155155
isHttps, applicationProtocols, isConnect,
156-
server, args.WebSession.UpStreamEndPoint ?? server.UpStreamEndPoint,
156+
server, session, session.WebSession.UpStreamEndPoint ?? server.UpStreamEndPoint,
157157
customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy),
158158
noCache, cancellationToken);
159159
}
@@ -174,7 +174,7 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer server,
174174
/// <returns></returns>
175175
internal async Task<TcpServerConnection> GetServerConnection(string remoteHostName, int remotePort,
176176
Version httpVersion, bool isHttps, List<SslApplicationProtocol> applicationProtocols, bool isConnect,
177-
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
177+
ProxyServer proxyServer, SessionEventArgsBase session, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
178178
bool noCache, CancellationToken cancellationToken)
179179
{
180180
var cacheKey = GetConnectionCacheKey(remoteHostName, remotePort,
@@ -205,7 +205,7 @@ internal async Task<TcpServerConnection> GetServerConnection(string remoteHostNa
205205
}
206206

207207
var connection = await createServerConnection(remoteHostName, remotePort, httpVersion, isHttps,
208-
applicationProtocols, isConnect, proxyServer, upStreamEndPoint, externalProxy, cancellationToken);
208+
applicationProtocols, isConnect, proxyServer, session, upStreamEndPoint, externalProxy, cancellationToken);
209209

210210
connection.CacheKey = cacheKey;
211211

@@ -222,13 +222,14 @@ internal async Task<TcpServerConnection> GetServerConnection(string remoteHostNa
222222
/// <param name="applicationProtocols">The list of HTTPS application level protocol to negotiate if needed.</param>
223223
/// <param name="isConnect">Is this a CONNECT request.</param>
224224
/// <param name="proxyServer">The current ProxyServer instance.</param>
225+
/// <param name="session">The http session.</param>
225226
/// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param>
226227
/// <param name="externalProxy">The external proxy to make request via.</param>
227228
/// <param name="cancellationToken">The cancellation token for this async task.</param>
228229
/// <returns></returns>
229230
private async Task<TcpServerConnection> createServerConnection(string remoteHostName, int remotePort,
230231
Version httpVersion, bool isHttps, List<SslApplicationProtocol> applicationProtocols, bool isConnect,
231-
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
232+
ProxyServer proxyServer, SessionEventArgsBase session, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
232233
CancellationToken cancellationToken)
233234
{
234235
//deny connection to proxy end points to avoid infinite connection loop.
@@ -288,14 +289,14 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
288289
var port = useUpstreamProxy ? externalProxy.Port : remotePort;
289290

290291
var ipHostEntry = await Dns.GetHostEntryAsync(hostname);
291-
292292
if (ipHostEntry == null || ipHostEntry.AddressList.Length == 0)
293293
{
294294
throw new Exception($"Could not resolve the hostname {hostname}");
295295
}
296296

297+
session.TimeLine["Dns Resolved"] = DateTime.Now;
298+
297299
var ipAddresses = ipHostEntry.AddressList.OrderBy(x => rnd.Next()).ToArray();
298-
299300

300301
for (int i = 0; i < ipAddresses.Length; i++)
301302
{
@@ -308,11 +309,13 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
308309
{
309310
if (i == ipAddresses.Length - 1)
310311
{
311-
throw new Exception($"Could not resolve the hostname {hostname}", e);
312+
throw new Exception($"Could not establish connection to {hostname}", e);
312313
}
313314
}
314315
}
315316

317+
session.TimeLine["Connection Established"] = DateTime.Now;
318+
316319
await proxyServer.InvokeConnectionCreateEvent(tcpClient, false);
317320

318321
stream = new CustomBufferedStream(tcpClient.GetStream(), proxyServer.BufferPool, proxyServer.BufferSize);
@@ -368,6 +371,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
368371
#if NETCOREAPP2_1
369372
negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol;
370373
#endif
374+
session.TimeLine["HTTPS Established"] = DateTime.Now;
371375
}
372376
}
373377
catch (Exception)

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
9494
bool userTrustRootCertificate = true, bool machineTrustRootCertificate = false,
9595
bool trustRootCertificateAsAdmin = false)
9696
{
97-
// default values
98-
ConnectionTimeOutSeconds = 60;
9997

10098
if (BufferPool == null)
10199
{
@@ -186,7 +184,7 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
186184
/// This will also determine the pool eviction time when connection pool is enabled.
187185
/// Default value is 60 seconds.
188186
/// </summary>
189-
public int ConnectionTimeOutSeconds { get; set; }
187+
public int ConnectionTimeOutSeconds { get; set; } = 60;
190188

191189
/// <summary>
192190
/// Maximum number of concurrent connections per remote host in cache.

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ await clientStreamWriter.WriteResponseAsync(args.WebSession.Response,
207207
//for connection pool, retry fails until cache is exhausted.
208208
var result = await retryPolicy<ServerConnectionException>().ExecuteAsync(async (serverConnection) =>
209209
{
210-
args.TimeLine["Server Connection Created"] = DateTime.Now;
210+
args.TimeLine["Connection Ready"] = DateTime.Now;
211211

212212
// if upgrading to websocket then relay the request without reading the contents
213213
if (request.UpgradeToWebSocket)
@@ -369,6 +369,8 @@ await args.WebSession.SendRequest(Enable100ContinueBehaviour, args.IsTransparent
369369
}
370370
}
371371

372+
args.TimeLine["Request Sent"] = DateTime.Now;
373+
372374
// If not expectation failed response was returned by server then parse response
373375
if (!request.ExpectationFailed)
374376
{

src/Titanium.Web.Proxy/TransparentClientHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
6969
//it could cause floating server connections when client exits
7070
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
7171
httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false,
72-
proxyServer: this, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
72+
proxyServer: this, session: null, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
7373
noCache: false, cancellationToken: CancellationToken.None);
7474
}
7575

@@ -102,7 +102,7 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
102102
{
103103
var connection = await tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
104104
httpVersion: null, isHttps: false, applicationProtocols: null,
105-
isConnect: true, proxyServer: this, upStreamEndPoint: UpStreamEndPoint,
105+
isConnect: true, proxyServer: this, session:null, upStreamEndPoint: UpStreamEndPoint,
106106
externalProxy: UpStreamHttpsProxy, noCache: true, cancellationToken: cancellationToken);
107107

108108
try

0 commit comments

Comments
 (0)