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

Commit 0800ff4

Browse files
Merge pull request #538 from justcoding121/master
Beta 3.0.866+
2 parents 29901cf + b6aaa81 commit 0800ff4

File tree

7 files changed

+95
-71
lines changed

7 files changed

+95
-71
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
## Titanium Web Proxy
22

3-
A light weight HTTP(S) proxy server written in C#
3+
A lightweight HTTP(S) proxy server written in C#.
44

55
<a href="https://ci.appveyor.com/project/justcoding121/titanium-web-proxy">![Build Status](https://ci.appveyor.com/api/projects/status/p5vvtbpx9yp250ol?svg=true)</a> [![Join the chat at https://gitter.im/Titanium-Web-Proxy/Lobby](https://badges.gitter.im/Titanium-Web-Proxy/Lobby.svg)](https://gitter.im/Titanium-Web-Proxy/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
66

7-
Kindly report only issues/bugs here . For programming help or questions use [StackOverflow](http://stackoverflow.com/questions/tagged/titanium-web-proxy) with the tag Titanium-Web-Proxy.
7+
Kindly report only issues/bugs here. For programming help or questions use [StackOverflow](http://stackoverflow.com/questions/tagged/titanium-web-proxy) with the tag Titanium-Web-Proxy.
88

99
* [API Documentation](https://justcoding121.github.io/Titanium-Web-Proxy/docs/api/Titanium.Web.Proxy.ProxyServer.html)
1010
* [Uncyclo & Contribution guidelines](https://github.com/justcoding121/Titanium-Web-Proxy/wiki)
1111

1212
### Features
1313

14-
* Multithreaded & fully asynchronous proxy employing server connection pooling, certificate cache & buffer pooling
15-
* View/modify/redirect/block requests & responses
14+
* Multi-threaded fully asynchronous proxy employing server connection pooling, certificate cache, and buffer pooling
15+
* View/modify/redirect/block requests and responses
1616
* Supports mutual SSL authentication, proxy authentication & automatic upstream proxy detection
1717
* Kerberos/NTLM authentication over HTTP protocols for windows domain
1818

@@ -48,7 +48,7 @@ Supports
4848

4949
### Usage
5050

51-
Refer the HTTP Proxy Server library in your project, look up Test project to learn usage.
51+
Refer the HTTP Proxy Server library in your project and look up the test project to learn usage.
5252

5353
Setup HTTP proxy:
5454

@@ -228,7 +228,7 @@ public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs
228228
```
229229
### Note to contributors
230230

231-
#### Roadmap
231+
#### Road map
232232

233233
* Support HTTP 2.0
234234

docs/api/Titanium.Web.Proxy.Helpers.RunTime.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ <h3 id="properties">Properties
128128
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsLinux.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsLinux%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>
129129
</span>
130130
<span class="small pull-right mobile-hide">
131-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L50">View Source</a>
131+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L47">View Source</a>
132132
</span>
133133
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux*"></a>
134134
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux">IsLinux</h4>
@@ -158,7 +158,7 @@ <h5 class="propertyValue">Property Value</h5>
158158
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsMac.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsMac%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>
159159
</span>
160160
<span class="small pull-right mobile-hide">
161-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L63">View Source</a>
161+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L60">View Source</a>
162162
</span>
163163
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsMac_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac*"></a>
164164
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsMac" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac">IsMac</h4>
@@ -188,7 +188,7 @@ <h5 class="propertyValue">Property Value</h5>
188188
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows%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>
189189
</span>
190190
<span class="small pull-right mobile-hide">
191-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L58">View Source</a>
191+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L55">View Source</a>
192192
</span>
193193
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows*"></a>
194194
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows">IsUwpOnWindows</h4>
@@ -218,7 +218,7 @@ <h5 class="propertyValue">Property Value</h5>
218218
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsWindows%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>
219219
</span>
220220
<span class="small pull-right mobile-hide">
221-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L56">View Source</a>
221+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L53">View Source</a>
222222
</span>
223223
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows*"></a>
224224
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows">IsWindows</h4>
@@ -254,7 +254,7 @@ <h5 class="propertyValue">Property Value</h5>
254254
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime%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" class="contribution-link">Improve this Doc</a>
255255
</li>
256256
<li>
257-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L13" class="contribution-link">View Source</a>
257+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L10" class="contribution-link">View Source</a>
258258
</li>
259259
</ul>
260260
</div>
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Net;
4+
using System.Net.NetworkInformation;
35
using System.Net.Sockets;
46

57
namespace Titanium.Web.Proxy.Helpers
68
{
79
internal class NetworkHelper
810
{
11+
private static readonly string localhostName = Dns.GetHostName();
12+
private static readonly IPHostEntry localhostEntry = Dns.GetHostEntry(string.Empty);
13+
914
/// <summary>
1015
/// Adapated from below link
1116
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
@@ -19,55 +24,52 @@ internal static bool IsLocalIpAddress(IPAddress address)
1924
return true;
2025
}
2126

22-
// get local IP addresses
23-
var localIPs = Dns.GetHostAddresses(Dns.GetHostName());
24-
25-
// test if any host IP equals to any local IP or to localhost
26-
return localIPs.Contains(address);
27+
// test if host IP equals any local IP
28+
return localhostEntry.AddressList.Contains(address);
2729
}
2830

2931
internal static bool IsLocalIpAddress(string hostName)
3032
{
31-
hostName = hostName.ToLower();
32-
33-
if (hostName == "127.0.0.1"
34-
|| hostName == "localhost")
33+
if (IPAddress.TryParse(hostName, out var ipAddress)
34+
&& IsLocalIpAddress(ipAddress))
3535
{
3636
return true;
3737
}
3838

39-
var localhostDnsName = Dns.GetHostName().ToLower();
40-
41-
//if hostname matches current machine DNS name
42-
if (hostName == localhostDnsName)
39+
if (hostName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
4340
{
4441
return true;
4542
}
4643

47-
var isLocalhost = false;
48-
IPHostEntry hostEntry = null;
49-
50-
//check if parsable to an IP Address
51-
if (IPAddress.TryParse(hostName, out var ipAddress))
44+
//if hostname matches local host name
45+
if (hostName.Equals(localhostName, StringComparison.OrdinalIgnoreCase))
5246
{
53-
hostEntry = Dns.GetHostEntry(localhostDnsName);
54-
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
47+
return true;
5548
}
5649

57-
if (!isLocalhost)
50+
// if hostname matches fully qualified local DNS name
51+
if (hostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase))
5852
{
59-
try
60-
{
61-
hostEntry = Dns.GetHostEntry(hostName);
62-
isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
63-
}
64-
catch (SocketException)
53+
return true;
54+
}
55+
56+
try
57+
{
58+
// do reverse DNS lookup even if hostName is an IP address
59+
var hostEntry = Dns.GetHostEntry(hostName);
60+
// if DNS resolved hostname matches local DNS name,
61+
// or if host IP address list contains any local IP address
62+
if (hostEntry.HostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase)
63+
|| hostEntry.AddressList.Any(hostIP => localhostEntry.AddressList.Contains(hostIP)))
6564
{
65+
return true;
6666
}
6767
}
68+
catch (SocketException)
69+
{
70+
}
6871

69-
70-
return isLocalhost;
72+
return false;
7173
}
7274
}
7375
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
2-
using System.Runtime.InteropServices;
32
using System.Text;
4-
#if NETSTANDARD2_0
53
using System.Runtime.InteropServices;
6-
#endif
74

85
namespace Titanium.Web.Proxy.Helpers
96
{

src/Titanium.Web.Proxy/Network/DefaultCertificateDiskCache.cs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Reflection;
44
using System.Security.Cryptography.X509Certificates;
5+
using Titanium.Web.Proxy.Helpers;
56

67
namespace Titanium.Web.Proxy.Network
78
{
@@ -13,24 +14,24 @@ internal sealed class DefaultCertificateDiskCache : ICertificateCache
1314
private string rootCertificatePath;
1415
private string certificatePath;
1516

16-
public X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags)
17+
public X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags)
1718
{
18-
string filePath = getRootCertificatePath(name);
19-
return loadCertificate(filePath, password, storageFlags);
19+
string path = getRootCertificatePath(pathOrName);
20+
return loadCertificate(path, password, storageFlags);
2021
}
2122

22-
public void SaveRootCertificate(string name, string password, X509Certificate2 certificate)
23+
public void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate)
2324
{
24-
string filePath = getRootCertificatePath(name);
25+
string path = getRootCertificatePath(pathOrName);
2526
byte[] exported = certificate.Export(X509ContentType.Pkcs12, password);
26-
File.WriteAllBytes(filePath, exported);
27+
File.WriteAllBytes(path, exported);
2728
}
2829

2930
/// <inheritdoc />
3031
public X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
3132
{
32-
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
33-
return loadCertificate(filePath, string.Empty, storageFlags);
33+
string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
34+
return loadCertificate(path, string.Empty, storageFlags);
3435
}
3536

3637
/// <inheritdoc />
@@ -55,12 +56,12 @@ public void Clear()
5556
certificatePath = null;
5657
}
5758

58-
private X509Certificate2 loadCertificate(string filePath, string password, X509KeyStorageFlags storageFlags)
59+
private X509Certificate2 loadCertificate(string path, string password, X509KeyStorageFlags storageFlags)
5960
{
6061
byte[] exported;
6162
try
6263
{
63-
exported = File.ReadAllBytes(filePath);
64+
exported = File.ReadAllBytes(path);
6465
}
6566
catch (IOException)
6667
{
@@ -71,15 +72,15 @@ private X509Certificate2 loadCertificate(string filePath, string password, X509K
7172
return new X509Certificate2(exported, password, storageFlags);
7273
}
7374

74-
private string getRootCertificatePath(string filePath)
75+
private string getRootCertificatePath(string pathOrName)
7576
{
76-
if (Path.IsPathRooted(filePath))
77+
if (Path.IsPathRooted(pathOrName))
7778
{
78-
return filePath;
79+
return pathOrName;
7980
}
8081

8182
return Path.Combine(getRootCertificateDirectory(),
82-
string.IsNullOrEmpty(filePath) ? defaultRootCertificateFileName : filePath);
83+
string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
8384
}
8485

8586
private string getCertificatePath()
@@ -104,17 +105,32 @@ private string getRootCertificateDirectory()
104105
{
105106
if (rootCertificatePath == null)
106107
{
107-
string assemblyLocation = GetType().Assembly.Location;
108-
109-
// dynamically loaded assemblies returns string.Empty location
110-
if (assemblyLocation == string.Empty)
108+
if (RunTime.IsUwpOnWindows)
109+
{
110+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
111+
}
112+
else if (RunTime.IsLinux)
111113
{
112-
assemblyLocation = Assembly.GetEntryAssembly().Location;
114+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
113115
}
116+
else if (RunTime.IsMac)
117+
{
118+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
119+
}
120+
else
121+
{
122+
string assemblyLocation = GetType().Assembly.Location;
114123

115-
string path = Path.GetDirectoryName(assemblyLocation);
124+
// dynamically loaded assemblies returns string.Empty location
125+
if (assemblyLocation == string.Empty)
126+
{
127+
assemblyLocation = Assembly.GetEntryAssembly().Location;
128+
}
116129

117-
rootCertificatePath = path ?? throw new NullReferenceException();
130+
string path = Path.GetDirectoryName(assemblyLocation);
131+
132+
rootCertificatePath = path ?? throw new NullReferenceException();
133+
}
118134
}
119135

120136
return rootCertificatePath;

src/Titanium.Web.Proxy/Network/ICertificateCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public interface ICertificateCache
77
/// <summary>
88
/// Loads the root certificate from the storage.
99
/// </summary>
10-
X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags);
10+
X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags);
1111

1212
/// <summary>
1313
/// Saves the root certificate to the storage.
1414
/// </summary>
15-
void SaveRootCertificate(string name, string password, X509Certificate2 certificate);
15+
void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate);
1616

1717
/// <summary>
1818
/// Loads certificate from the storage. Returns true if certificate does not exist.

src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@
1414
<copyright>Copyright &#x00A9; Titanium. All rights reserved.</copyright>
1515
<tags></tags>
1616
<dependencies>
17-
<dependency id="StreamExtended" version="1.0.179" />
18-
<dependency id="Portable.BouncyCastle" version="1.8.2" />
19-
<dependency id="BrotliSharpLib" version="0.3.1" />
17+
<group targetFramework="net45">
18+
<dependency id="StreamExtended" version="1.0.179" />
19+
<dependency id="Portable.BouncyCastle" version="1.8.2" />
20+
<dependency id="BrotliSharpLib" version="0.3.1" />
21+
</group>
22+
<group targetFramework="netstandard2.0">
23+
<dependency id="StreamExtended" version="1.0.179" />
24+
<dependency id="Portable.BouncyCastle" version="1.8.2" />
25+
<dependency id="BrotliSharpLib" version="0.3.1" />
26+
<dependency id="Microsoft.Win32.Registry" version="4.4.0" />
27+
<dependency id="System.Security.Principal.Windows" version="4.4.1" />
28+
</group>
2029
</dependencies>
2130
</metadata>
2231
<files>
2332
<file src="bin\$configuration$\net45\Titanium.Web.Proxy.dll" target="lib\net45" />
24-
<file src="bin\$configuration$\netstandard2.0\Titanium.Web.Proxy.*" target="lib\netstandard2.0" />
33+
<file src="bin\$configuration$\netstandard2.0\Titanium.Web.Proxy.dll" target="lib\netstandard2.0" />
2534
</files>
2635
</package>

0 commit comments

Comments
 (0)