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

Commit c2e5c44

Browse files
authored
Merge pull request #617 from justcoding121/master
Master to beta
2 parents 0e8e25c + 1b3d813 commit c2e5c44

20 files changed

+315
-73
lines changed

docs/api/Titanium.Web.Proxy.ProxyServer.html

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ <h5 class="parameters">Parameters</h5>
12451245
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
12461246
</span>
12471247
<span class="small pull-right mobile-hide">
1248-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L536">View Source</a>
1248+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L550">View Source</a>
12491249
</span>
12501250
<a id="Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies*"></a>
12511251
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies" data-uid="Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies">DisableAllSystemProxies()</h4>
@@ -1293,7 +1293,7 @@ <h5 class="decalaration">Declaration</h5>
12931293
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_Titanium_Web_Proxy_Models_ProxyProtocolType_.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableSystemProxy(Titanium.Web.Proxy.Models.ProxyProtocolType)%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
12941294
</span>
12951295
<span class="small pull-right mobile-hide">
1296-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L522">View Source</a>
1296+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L536">View Source</a>
12971297
</span>
12981298
<a id="Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemProxy*"></a>
12991299
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_Titanium_Web_Proxy_Models_ProxyProtocolType_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemProxy(Titanium.Web.Proxy.Models.ProxyProtocolType)">DisableSystemProxy(ProxyProtocolType)</h4>
@@ -1326,7 +1326,7 @@ <h5 class="parameters">Parameters</h5>
13261326
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Dispose.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Dispose%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
13271327
</span>
13281328
<span class="small pull-right mobile-hide">
1329-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L884">View Source</a>
1329+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L898">View Source</a>
13301330
</span>
13311331
<a id="Titanium_Web_Proxy_ProxyServer_Dispose_" data-uid="Titanium.Web.Proxy.ProxyServer.Dispose*"></a>
13321332
<h4 id="Titanium_Web_Proxy_ProxyServer_Dispose" data-uid="Titanium.Web.Proxy.ProxyServer.Dispose">Dispose()</h4>
@@ -1372,6 +1372,22 @@ <h5 class="parameters">Parameters</h5>
13721372
</tr>
13731373
</tbody>
13741374
</table>
1375+
<span class="small pull-right mobile-hide">
1376+
<span class="divider">|</span>
1377+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_RestoreOriginalProxySettings.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
1378+
</span>
1379+
<span class="small pull-right mobile-hide">
1380+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L522">View Source</a>
1381+
</span>
1382+
<a id="Titanium_Web_Proxy_ProxyServer_RestoreOriginalProxySettings_" data-uid="Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings*"></a>
1383+
<h4 id="Titanium_Web_Proxy_ProxyServer_RestoreOriginalProxySettings" data-uid="Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings">RestoreOriginalProxySettings()</h4>
1384+
<div class="markdown level1 summary"><p>Restores the original proxy settings.</p>
1385+
</div>
1386+
<div class="markdown level1 conceptual"></div>
1387+
<h5 class="decalaration">Declaration</h5>
1388+
<div class="codewrapper">
1389+
<pre><code class="lang-csharp hljs">public void RestoreOriginalProxySettings()</code></pre>
1390+
</div>
13751391
<span class="small pull-right mobile-hide">
13761392
<span class="divider">|</span>
13771393
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_SetAsSystemHttpProxy_Titanium_Web_Proxy_Models_ExplicitProxyEndPoint_.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.SetAsSystemHttpProxy(Titanium.Web.Proxy.Models.ExplicitProxyEndPoint)%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
@@ -1485,7 +1501,7 @@ <h5 class="parameters">Parameters</h5>
14851501
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Start.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Start%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
14861502
</span>
14871503
<span class="small pull-right mobile-hide">
1488-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L550">View Source</a>
1504+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L564">View Source</a>
14891505
</span>
14901506
<a id="Titanium_Web_Proxy_ProxyServer_Start_" data-uid="Titanium.Web.Proxy.ProxyServer.Start*"></a>
14911507
<h4 id="Titanium_Web_Proxy_ProxyServer_Start" data-uid="Titanium.Web.Proxy.ProxyServer.Start">Start()</h4>
@@ -1501,7 +1517,7 @@ <h5 class="decalaration">Declaration</h5>
15011517
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Stop.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Stop%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
15021518
</span>
15031519
<span class="small pull-right mobile-hide">
1504-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L610">View Source</a>
1520+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L624">View Source</a>
15051521
</span>
15061522
<a id="Titanium_Web_Proxy_ProxyServer_Stop_" data-uid="Titanium.Web.Proxy.ProxyServer.Stop*"></a>
15071523
<h4 id="Titanium_Web_Proxy_ProxyServer_Stop" data-uid="Titanium.Web.Proxy.ProxyServer.Stop">Stop()</h4>

docs/index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/xrefmap.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,19 @@ references:
37443744
isSpec: "True"
37453745
fullName: Titanium.Web.Proxy.ProxyServer.RemoveEndPoint
37463746
nameWithType: ProxyServer.RemoveEndPoint
3747+
- uid: Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings
3748+
name: RestoreOriginalProxySettings()
3749+
href: api/Titanium.Web.Proxy.ProxyServer.html#Titanium_Web_Proxy_ProxyServer_RestoreOriginalProxySettings
3750+
commentId: M:Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings
3751+
fullName: Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings()
3752+
nameWithType: ProxyServer.RestoreOriginalProxySettings()
3753+
- uid: Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings*
3754+
name: RestoreOriginalProxySettings
3755+
href: api/Titanium.Web.Proxy.ProxyServer.html#Titanium_Web_Proxy_ProxyServer_RestoreOriginalProxySettings_
3756+
commentId: Overload:Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings
3757+
isSpec: "True"
3758+
fullName: Titanium.Web.Proxy.ProxyServer.RestoreOriginalProxySettings
3759+
nameWithType: ProxyServer.RestoreOriginalProxySettings
37473760
- uid: Titanium.Web.Proxy.ProxyServer.ReuseSocket
37483761
name: ReuseSocket
37493762
href: api/Titanium.Web.Proxy.ProxyServer.html#Titanium_Web_Proxy_ProxyServer_ReuseSocket

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void Stop()
119119
private async Task onBeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
120120
{
121121
string hostname = e.HttpClient.Request.RequestUri.Host;
122-
await writeToConsole("Tunnel to: " + hostname);
122+
//await writeToConsole("Tunnel to: " + hostname);
123123

124124
if (hostname.Contains("dropbox.com"))
125125
{
@@ -138,8 +138,8 @@ private Task onBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEv
138138
// intecept & cancel redirect or update requests
139139
private async Task onRequest(object sender, SessionEventArgs e)
140140
{
141-
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
142-
await writeToConsole(e.HttpClient.Request.Url);
141+
//await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
142+
//await writeToConsole(e.HttpClient.Request.Url);
143143

144144
// store it in the UserData property
145145
// It can be a simple integer, Guid, or any type
@@ -189,7 +189,7 @@ private async Task multipartRequestPartSent(object sender, MultipartRequestPartS
189189

190190
private async Task onResponse(object sender, SessionEventArgs e)
191191
{
192-
await writeToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);
192+
//await writeToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);
193193

194194
string ext = System.IO.Path.GetExtension(e.HttpClient.Request.RequestUri.AbsolutePath);
195195

examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</TabItem>
5555
</TabControl>
5656
<StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal">
57+
<ToggleButton IsChecked="True" Content="On/Off" Click="ButtonProxyOnOff_OnClick" />
5758
<TextBlock Text="ClientConnectionCount:" />
5859
<TextBlock Text="{Binding ClientConnectionCount}" Margin="10,0,20,0" />
5960
<TextBlock Text="ServerConnectionCount:" />

examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using System.Windows;
1010
using System.Windows.Controls;
11+
using System.Windows.Controls.Primitives;
1112
using System.Windows.Input;
1213
using System.Windows.Media.Imaging;
1314
using Titanium.Web.Proxy.EventArguments;
@@ -95,7 +96,7 @@ public MainWindow()
9596
Dispatcher.Invoke(() => { ServerConnectionCount = proxyServer.ServerConnectionCount; });
9697
};
9798
proxyServer.Start();
98-
99+
99100
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
100101

101102
InitializeComponent();
@@ -284,30 +285,44 @@ private void ListViewSessions_OnKeyDown(object sender, KeyEventArgs e)
284285
{
285286
if (e.Key == Key.Delete)
286287
{
288+
bool isSelected = false;
287289
var selectedItems = ((ListView)sender).SelectedItems;
288290
Sessions.SuppressNotification = true;
289291
foreach (var item in selectedItems.Cast<SessionListItem>().ToArray())
290292
{
293+
if (item == SelectedSession)
294+
{
295+
isSelected = true;
296+
}
297+
291298
Sessions.Remove(item);
292299
sessionDictionary.Remove(item.HttpClient);
293300
}
294301

295302
Sessions.SuppressNotification = false;
303+
304+
if (isSelected)
305+
{
306+
SelectedSession = null;
307+
}
296308
}
297309
}
298310

299311
private void selectedSessionChanged()
300312
{
301313
if (SelectedSession == null)
302314
{
315+
TextBoxRequest.Text = null;
316+
TextBoxResponse.Text = string.Empty;
317+
ImageResponse.Source = null;
303318
return;
304319
}
305320

306321
const int truncateLimit = 1024;
307322

308323
var session = SelectedSession.HttpClient;
309324
var request = session.Request;
310-
var fullData = (request.IsBodyRead ? request.Body : null) ?? new byte[0];
325+
var fullData = (request.IsBodyRead ? request.Body : null) ?? Array.Empty<byte>();
311326
var data = fullData;
312327
bool truncated = data.Length > truncateLimit;
313328
if (truncated)
@@ -324,7 +339,7 @@ private void selectedSessionChanged()
324339
TextBoxRequest.Text = sb.ToString();
325340

326341
var response = session.Response;
327-
fullData = (response.IsBodyRead ? response.Body : null) ?? new byte[0];
342+
fullData = (response.IsBodyRead ? response.Body : null) ?? Array.Empty<byte>();
328343
data = fullData;
329344
truncated = data.Length > truncateLimit;
330345
if (truncated)
@@ -348,16 +363,33 @@ private void selectedSessionChanged()
348363

349364
try
350365
{
351-
using (MemoryStream stream = new MemoryStream(fullData))
366+
if (fullData.Length > 0)
352367
{
353-
ImageResponse.Source =
354-
BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
368+
using (var stream = new MemoryStream(fullData))
369+
{
370+
ImageResponse.Source =
371+
BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
372+
}
355373
}
356374
}
357375
catch
358376
{
359377
ImageResponse.Source = null;
360378
}
361379
}
380+
381+
private void ButtonProxyOnOff_OnClick(object sender, RoutedEventArgs e)
382+
{
383+
var button = (ToggleButton)sender;
384+
if (button.IsChecked == true)
385+
{
386+
proxyServer.SetAsSystemProxy((ExplicitProxyEndPoint)proxyServer.ProxyEndPoints[0],
387+
ProxyProtocolType.AllHttp);
388+
}
389+
else
390+
{
391+
proxyServer.RestoreOriginalProxySettings();
392+
}
393+
}
362394
}
363395
}

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
265265

266266
try
267267
{
268-
await clientStream.ReadAsync(data, 0, available, cancellationToken);
269268
// clientStream.Available should be at most BufferSize because it is using the same buffer size
269+
await clientStream.ReadAsync(data, 0, available, cancellationToken);
270270
await connection.StreamWriter.WriteAsync(data, 0, available, true, cancellationToken);
271271
}
272272
finally
@@ -279,10 +279,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
279279
((ConnectResponse)connectArgs.HttpClient.Response).ServerHelloInfo = serverHelloInfo;
280280
}
281281

282-
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool, BufferSize,
283-
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
284-
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
285-
connectArgs.CancellationTokenSource, ExceptionFunc);
282+
if (!clientStream.IsClosed && !connection.Stream.IsClosed)
283+
{
284+
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool, BufferSize,
285+
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
286+
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
287+
connectArgs.CancellationTokenSource, ExceptionFunc);
288+
}
286289
}
287290
finally
288291
{

src/Titanium.Web.Proxy/Helpers/HttpHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net;
23
using System.Text;
34
using System.Text.RegularExpressions;
45
using System.Threading;
@@ -99,7 +100,13 @@ internal static string GetWildCardDomainName(string hostname)
99100
{
100101
// only for subdomains we need wild card
101102
// example www.google.com or gstatic.google.com
102-
// but NOT for google.com
103+
// but NOT for google.com or IP address
104+
105+
if (IPAddress.TryParse(hostname, out _))
106+
{
107+
return hostname;
108+
}
109+
103110
if (hostname.Split(ProxyConstants.DotSplit).Length > 2)
104111
{
105112
int idx = hostname.IndexOf(ProxyConstants.DotSplit);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ private async Task clearOutdatedConnections()
481481
{
482482
try
483483
{
484+
var cutOff = DateTime.Now.AddSeconds(-1 * Server.ConnectionTimeOutSeconds);
484485
foreach (var item in cache)
485486
{
486487
var queue = item.Value;
@@ -489,7 +490,6 @@ private async Task clearOutdatedConnections()
489490
{
490491
if (queue.TryDequeue(out var connection))
491492
{
492-
var cutOff = DateTime.Now.AddSeconds(-1 * Server.ConnectionTimeOutSeconds);
493493
if (!Server.EnableConnectionPool
494494
|| connection.LastAccess < cutOff)
495495
{
@@ -512,7 +512,7 @@ private async Task clearOutdatedConnections()
512512
var emptyKeys = cache.Where(x => x.Value.Count == 0).Select(x => x.Key).ToList();
513513
foreach (string key in emptyKeys)
514514
{
515-
cache.TryRemove(key, out var _);
515+
cache.TryRemove(key, out _);
516516
}
517517
}
518518
finally

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ public void DisableSystemHttpsProxy()
516516
DisableSystemProxy(ProxyProtocolType.Https);
517517
}
518518

519+
/// <summary>
520+
/// Restores the original proxy settings.
521+
/// </summary>
522+
public void RestoreOriginalProxySettings()
523+
{
524+
if (!RunTime.IsWindows)
525+
{
526+
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
527+
Please manually configure your operating system to use this proxy's port and address.");
528+
}
529+
530+
systemProxySettingsManager.RestoreOriginalSettings();
531+
}
532+
519533
/// <summary>
520534
/// Clear the specified proxy setting for current machine.
521535
/// </summary>
@@ -524,7 +538,7 @@ public void DisableSystemProxy(ProxyProtocolType protocolType)
524538
if (!RunTime.IsWindows)
525539
{
526540
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
527-
Please manually confugure you operating system to use this proxy's port and address.");
541+
Please manually configure your operating system to use this proxy's port and address.");
528542
}
529543

530544
systemProxySettingsManager.RemoveProxy(protocolType);

0 commit comments

Comments
 (0)