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

Commit 9c6b022

Browse files
committed
option to specifi certificate name for transparent endpoint
1 parent 534ea74 commit 9c6b022

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

Titanium.Web.Proxy/Models/EndPoint.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
3737

3838
public class TransparentProxyEndPoint : ProxyEndPoint
3939
{
40+
//Name of the Certificate need to be sent (same as the hostname we want to proxy)
41+
//This is valid only when UseServerNameIndication is set to false
42+
public string GenericCertificateName { get; set; }
43+
44+
45+
// public bool UseServerNameIndication { get; set; }
46+
4047
public TransparentProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
4148
: base(IpAddress, Port, EnableSsl)
4249
{
43-
50+
this.GenericCertificateName = "localhost";
4451
}
4552
}
53+
4654
}

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
using Titanium.Web.Proxy.Helpers;
1616
using Titanium.Web.Proxy.Network;
1717
using Titanium.Web.Proxy.Models;
18+
using System.Security.Cryptography.X509Certificates;
1819

1920
namespace Titanium.Web.Proxy
2021
{
2122
partial class ProxyServer
2223
{
24+
//This is called when client is aware of proxy
2325
private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient client)
2426
{
2527
Stream clientStream = client.GetStream();
@@ -54,7 +56,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
5456
var excluded = endPoint.ExcludedHostNameRegex != null ? endPoint.ExcludedHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false;
5557

5658
//Client wants to create a secure tcp tunnel (its a HTTPS request)
57-
if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port!=80)
59+
if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port != 80)
5860
{
5961
httpRemoteUri = new Uri("https://" + httpCmdSplit[1]);
6062
clientStreamReader.ReadAllLines();
@@ -105,7 +107,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
105107
}
106108

107109
//Now create the request
108-
110+
109111
HandleHttpSessionRequest(client, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
110112
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? true : false);
111113
}
@@ -115,12 +117,21 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
115117
}
116118
}
117119

118-
private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient client)
120+
//This is called when requests are routed through router to this endpoint
121+
private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tcpClient)
119122
{
120-
var sslStream = new SslStream(client.GetStream(), true);
123+
var sslStream = new SslStream(tcpClient.GetStream(), true);
121124
CustomBinaryReader clientStreamReader = null;
122125
StreamWriter clientStreamWriter = null;
123-
var certificate = CertManager.CreateCertificate("127.0.0.1");
126+
X509Certificate2 certificate = null;
127+
128+
//if(endPoint.UseServerNameIndication)
129+
//{
130+
// //implement in future once SNI supported by SSL stream
131+
// certificate = CertManager.CreateCertificate(endPoint.GenericCertificateName);
132+
//}
133+
//else
134+
certificate = CertManager.CreateCertificate(endPoint.GenericCertificateName);
124135

125136
try
126137
{
@@ -133,20 +144,19 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient cl
133144
//HTTPS server created - we can now decrypt the client's traffic
134145

135146
}
136-
137-
catch (Exception e)
147+
catch (Exception)
138148
{
139149
if (sslStream != null)
140150
sslStream.Dispose();
141151

142-
Dispose(client, sslStream, clientStreamReader, clientStreamWriter, null);
152+
Dispose(tcpClient, sslStream, clientStreamReader, clientStreamWriter, null);
143153
return;
144154
}
145155

146156
var httpCmd = clientStreamReader.ReadLine();
147157

148158
//Now create the request
149-
HandleHttpSessionRequest(client, httpCmd, sslStream, clientStreamReader, clientStreamWriter,
159+
HandleHttpSessionRequest(tcpClient, httpCmd, sslStream, clientStreamReader, clientStreamWriter,
150160
true);
151161
}
152162

0 commit comments

Comments
 (0)