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

Commit

Permalink
Merge pull request #732 from justcoding121/master
Browse files Browse the repository at this point in the history
stable
  • Loading branch information
honfika authored Jan 14, 2020
2 parents 82ac963 + fcb8301 commit 2b38dec
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 72 deletions.
16 changes: 8 additions & 8 deletions docs/api/Titanium.Web.Proxy.Http.HttpWebClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ <h3 id="properties">Properties
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L66">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L67">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_ConnectRequest" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ConnectRequest">ConnectRequest</h4>
Expand Down Expand Up @@ -159,7 +159,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_IsHttps.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.IsHttps%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L87">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L88">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_IsHttps_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.IsHttps*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_IsHttps" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.IsHttps">IsHttps</h4>
Expand Down Expand Up @@ -190,7 +190,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_ProcessId.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.ProcessId%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L82">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L83">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_ProcessId_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ProcessId*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_ProcessId" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.ProcessId">ProcessId</h4>
Expand Down Expand Up @@ -222,7 +222,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_Request.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.Request%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L71">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L72">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_Request_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Request*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_Request" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Request">Request</h4>
Expand Down Expand Up @@ -253,7 +253,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_Response.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.Response%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L76">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L77">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_Response_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Response*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_Response" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.Response">Response</h4>
Expand Down Expand Up @@ -284,7 +284,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L61">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L62">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_UpStreamEndPoint" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UpStreamEndPoint">UpStreamEndPoint</h4>
Expand Down Expand Up @@ -315,7 +315,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient_UserData.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient.UserData%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>
</span>
<span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L56">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L57">View Source</a>
</span>
<a id="Titanium_Web_Proxy_Http_HttpWebClient_UserData_" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UserData*"></a>
<h4 id="Titanium_Web_Proxy_Http_HttpWebClient_UserData" data-uid="Titanium.Web.Proxy.Http.HttpWebClient.UserData">UserData</h4>
Expand Down Expand Up @@ -352,7 +352,7 @@ <h5 class="propertyValue">Property Value</h5>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Http_HttpWebClient.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Http.HttpWebClient%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>
</li>
<li>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L13" class="contribution-link">View Source</a>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Http/HttpWebClient.cs/#L14" class="contribution-link">View Source</a>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/ExplicitClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
}

// write back successful CONNECT response
var response = ConnectResponse.CreateSuccessfulConnectResponse(requestLine.Version);
var response = ConnectResponse.CreateSuccessfulConnectResponse(connectRequest.HttpVersion);

// Set ContentLength explicitly to properly handle HTTP 1.0
response.ContentLength = 0;
Expand Down
45 changes: 44 additions & 1 deletion src/Titanium.Web.Proxy/Extensions/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
using System;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.Extensions
{
internal static class UriExtensions
{
internal static string GetOriginalPathAndQuery(this Uri uri)
public static string GetOriginalPathAndQuery(this Uri uri)
{
string leftPart = uri.GetLeftPart(UriPartial.Authority);
if (uri.OriginalString.StartsWith(leftPart))
return uri.OriginalString.Substring(leftPart.Length);

return uri.IsWellFormedOriginalString() ? uri.PathAndQuery : uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
}

public static ByteString GetScheme(ByteString str)
{
if (str.Length < 3)
{
return ByteString.Empty;
}

// regex: "^[a-z]*://"
int i;

for (i = 0; i < str.Length - 3; i++)
{
byte ch = str[i];
if (ch == ':')
{
break;
}

if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
{
return ByteString.Empty;
}
}

if (str[i++] != ':')
{
return ByteString.Empty;
}

if (str[i++] != '/')
{
return ByteString.Empty;
}

if (str[i] != '/')
{
return ByteString.Empty;
}

return new ByteString(str.Data.Slice(0, i - 2));
}
}
}
19 changes: 18 additions & 1 deletion src/Titanium.Web.Proxy/Http/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network.Tcp;

Expand Down Expand Up @@ -113,10 +114,21 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
string? upstreamProxyPassword = null;

string url;
if (!useUpstreamProxy || isTransparent)
if (isTransparent)
{
url = Request.RequestUriString;
}
else if (!useUpstreamProxy)
{
if (UriExtensions.GetScheme(Request.RequestUriString8).Length == 0)
{
url = Request.RequestUriString;
}
else
{
url = Request.RequestUri.GetOriginalPathAndQuery();
}
}
else
{
url = Request.RequestUri.ToString();
Expand All @@ -129,6 +141,11 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
}
}

if (url == string.Empty)
{
url = "/";
}

// prepare the request & headers
var headerBuilder = new HeaderBuilder();
headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);
Expand Down
48 changes: 3 additions & 45 deletions src/Titanium.Web.Proxy/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal ByteString RequestUriString8
set
{
requestUriString8 = value;
var scheme = getUriScheme(value);
var scheme = UriExtensions.GetScheme(value);
if (scheme.Length > 0)
{
IsHttps = scheme.Equals(ProxyServer.UriSchemeHttps8);
Expand Down Expand Up @@ -71,7 +71,7 @@ public string Url
get
{
string url = RequestUriString8.GetString();
if (getUriScheme(RequestUriString8).Length == 0)
if (UriExtensions.GetScheme(RequestUriString8).Length == 0)
{
string? hostAndPath = Host ?? Authority.GetString();

Expand Down Expand Up @@ -105,7 +105,7 @@ public string RequestUriString
{
RequestUriString8 = (ByteString)value;

var scheme = getUriScheme(RequestUriString8);
var scheme = UriExtensions.GetScheme(RequestUriString8);
if (scheme.Length > 0 && Host != null)
{
var uri = new Uri(value);
Expand Down Expand Up @@ -307,47 +307,5 @@ private static bool isAllUpper(string input)

return true;
}

private ByteString getUriScheme(ByteString str)
{
if (str.Length < 3)
{
return ByteString.Empty;
}

// regex: "^[a-z]*://"
int i;

for (i = 0; i < str.Length - 3; i++)
{
byte ch = str[i];
if (ch == ':')
{
break;
}

if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
{
return ByteString.Empty;
}
}

if (str[i++] != ':')
{
return ByteString.Empty;
}

if (str[i++] != '/')
{
return ByteString.Empty;
}

if (str[i] != '/')
{
return ByteString.Empty;
}

return new ByteString(str.Data.Slice(0, i - 2));
}
}
}
18 changes: 11 additions & 7 deletions src/Titanium.Web.Proxy/Network/DefaultCertificateDiskCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public void SaveRootCertificate(string pathOrName, string password, X509Certific
/// <inheritdoc />
public X509Certificate2? LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
{
string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
return loadCertificate(path, string.Empty, storageFlags);
string filePath = Path.Combine(getCertificatePath(false), subjectName + defaultCertificateFileExtension);
return loadCertificate(filePath, string.Empty, storageFlags);
}

/// <inheritdoc />
public void SaveCertificate(string subjectName, X509Certificate2 certificate)
{
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
string filePath = Path.Combine(getCertificatePath(true), subjectName + defaultCertificateFileExtension);
byte[] exported = certificate.Export(X509ContentType.Pkcs12);
File.WriteAllBytes(filePath, exported);
}
Expand All @@ -46,9 +46,13 @@ public void Clear()
{
try
{
Directory.Delete(getCertificatePath(), true);
string path = getCertificatePath(false);
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
catch (DirectoryNotFoundException)
catch (Exception)
{
// do nothing
}
Expand Down Expand Up @@ -89,14 +93,14 @@ private string getRootCertificatePath(string pathOrName)
string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
}

private string getCertificatePath()
private string getCertificatePath(bool create)
{
if (certificatePath == null)
{
string path = getRootCertificateDirectory();

string certPath = Path.Combine(path, defaultCertificateDirectoryName);
if (!Directory.Exists(certPath))
if (create && !Directory.Exists(certPath))
{
Directory.CreateDirectory(certPath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/ProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType p
if (systemProxySettingsManager == null)
{
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
Please manually confugure you operating system to use this proxy's port and address.");
Please manually configure you operating system to use this proxy's port and address.");
}

validateEndPointAsSystemProxy(endPoint);
Expand Down
16 changes: 8 additions & 8 deletions src/Titanium.Web.Proxy/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
UserData = connectArgs?.UserData
};

var request = args.HttpClient.Request;
if (isHttps)
{
args.HttpClient.Request.IsHttps = true;
request.IsHttps = true;
}

try
Expand All @@ -81,7 +82,6 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
cancellationToken);

var request = args.HttpClient.Request;
if (connectRequest != null)
{
request.IsHttps = connectRequest.IsHttps;
Expand All @@ -93,6 +93,12 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
request.Method = requestLine.Method;
request.HttpVersion = requestLine.Version;

// we need this to syphon out data from connection if API user changes them.
request.SetOriginalHeaders();

// If user requested interception do it
await onBeforeRequest(args);

if (!args.IsTransparent && !args.IsSocks)
{
// proxy authorization check
Expand All @@ -117,12 +123,6 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
await args.GetRequestBody(cancellationToken);
}

// we need this to syphon out data from connection if API user changes them.
request.SetOriginalHeaders();

// If user requested interception do it
await onBeforeRequest(args);

var response = args.HttpClient.Response;

if (request.CancelRequest)
Expand Down

0 comments on commit 2b38dec

Please sign in to comment.