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

Commit de0ec9c

Browse files
committed
use certificate only if SSL is enabled
1 parent 9c6b022 commit de0ec9c

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,45 +118,55 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
118118
}
119119

120120
//This is called when requests are routed through router to this endpoint
121+
//For ssl requests
121122
private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tcpClient)
122123
{
123-
var sslStream = new SslStream(tcpClient.GetStream(), true);
124+
Stream clientStream = tcpClient.GetStream();
124125
CustomBinaryReader clientStreamReader = null;
125126
StreamWriter clientStreamWriter = null;
126127
X509Certificate2 certificate = null;
127128

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);
135-
136-
try
129+
if (endPoint.EnableSsl)
137130
{
138-
//Successfully managed to authenticate the client using the fake certificate
139-
sslStream.AuthenticateAsServer(certificate, false,
140-
SslProtocols.Tls, false);
131+
var sslStream = new SslStream(clientStream, true);
132+
//if(endPoint.UseServerNameIndication)
133+
//{
134+
// //implement in future once SNI supported by SSL stream
135+
// certificate = CertManager.CreateCertificate(endPoint.GenericCertificateName);
136+
//}
137+
//else
138+
certificate = CertManager.CreateCertificate(endPoint.GenericCertificateName);
139+
140+
try
141+
{
142+
//Successfully managed to authenticate the client using the fake certificate
143+
sslStream.AuthenticateAsServer(certificate, false,
144+
SslProtocols.Tls, false);
141145

142-
clientStreamReader = new CustomBinaryReader(sslStream, Encoding.ASCII);
143-
clientStreamWriter = new StreamWriter(sslStream);
144-
//HTTPS server created - we can now decrypt the client's traffic
146+
clientStreamReader = new CustomBinaryReader(sslStream, Encoding.ASCII);
147+
clientStreamWriter = new StreamWriter(sslStream);
148+
//HTTPS server created - we can now decrypt the client's traffic
145149

150+
}
151+
catch (Exception)
152+
{
153+
if (sslStream != null)
154+
sslStream.Dispose();
155+
156+
Dispose(tcpClient, sslStream, clientStreamReader, clientStreamWriter, null);
157+
return;
158+
}
159+
clientStream = sslStream;
146160
}
147-
catch (Exception)
161+
else
148162
{
149-
if (sslStream != null)
150-
sslStream.Dispose();
151-
152-
Dispose(tcpClient, sslStream, clientStreamReader, clientStreamWriter, null);
153-
return;
163+
clientStreamReader = new CustomBinaryReader(clientStream, Encoding.ASCII);
154164
}
155165

156166
var httpCmd = clientStreamReader.ReadLine();
157167

158168
//Now create the request
159-
HandleHttpSessionRequest(tcpClient, httpCmd, sslStream, clientStreamReader, clientStreamWriter,
169+
HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
160170
true);
161171
}
162172

0 commit comments

Comments
 (0)