Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
e68cc842
Commit
e68cc842
authored
May 16, 2017
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move Dispose all to finally blocks
parent
5c4fc480
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
106 deletions
+50
-106
CustomBinaryReader.cs
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
+1
-1
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+1
-2
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+46
-101
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+2
-2
No files found.
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
View file @
e68cc842
...
...
@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy.Helpers
private
static
readonly
ConcurrentQueue
<
byte
[
]>
buffers
=
new
ConcurrentQueue
<
byte
[
]>
();
private
volatile
bool
disposed
=
false
;
private
volatile
bool
disposed
;
internal
CustomBinaryReader
(
CustomBufferedStream
stream
,
int
bufferSize
)
{
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
e68cc842
...
...
@@ -383,8 +383,7 @@ namespace Titanium.Web.Proxy
#if !DEBUG
firefoxProxySettingsManager
.
AddFirefox
();
#endif
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
}
/// <summary>
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
e68cc842
...
...
@@ -117,6 +117,7 @@ namespace Titanium.Web.Proxy
//Successfully managed to authenticate the client using the fake certificate
await
sslStream
.
AuthenticateAsServerAsync
(
certificate
,
false
,
SupportedSslProtocols
,
false
);
//HTTPS server created - we can now decrypt the client's traffic
clientStream
=
new
CustomBufferedStream
(
sslStream
,
BufferSize
);
...
...
@@ -138,6 +139,7 @@ namespace Titanium.Web.Proxy
{
//Siphon out CONNECT request headers
await
clientStreamReader
.
ReadAndIgnoreAllLinesAsync
();
//write back successfull CONNECT response
await
WriteConnectResponse
(
clientStreamWriter
,
version
);
...
...
@@ -172,8 +174,7 @@ namespace Titanium.Web.Proxy
//So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
{
var
disposed
=
false
;
bool
disposed
=
false
;
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
clientStream
.
ReadTimeout
=
ConnectionTimeOutSeconds
*
1000
;
...
...
@@ -182,52 +183,39 @@ namespace Titanium.Web.Proxy
CustomBinaryReader
clientStreamReader
=
null
;
StreamWriter
clientStreamWriter
=
null
;
if
(
endPoint
.
EnableSsl
)
try
{
var
sslStream
=
new
SslStream
(
clientStream
,
true
);
if
(
endPoint
.
EnableSsl
)
{
var
sslStream
=
new
SslStream
(
clientStream
,
true
);
clientStream
=
new
CustomBufferedStream
(
sslStream
,
BufferSize
);
//implement in future once SNI supported by SSL stream, for now use the same certificate
var
certificate
=
CertificateManager
.
CreateCertificate
(
endPoint
.
GenericCertificateName
,
false
);
//implement in future once SNI supported by SSL stream, for now use the same certificate
var
certificate
=
CertificateManager
.
CreateCertificate
(
endPoint
.
GenericCertificateName
,
false
);
try
{
//Successfully managed to authenticate the client using the fake certificate
await
sslStream
.
AuthenticateAsServerAsync
(
certificate
,
false
,
SslProtocols
.
Tls
,
false
);
clientStream
=
new
CustomBufferedStream
(
sslStream
,
BufferSize
);
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
clientStreamWriter
=
new
StreamWriter
(
clientStream
)
{
NewLine
=
ProxyConstants
.
NewLine
};
//HTTPS server created - we can now decrypt the client's traffic
}
catch
(
Exception
)
{
sslStream
.
Dispose
();
Dispose
(
sslStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
disposed
=
true
;
return
;
}
}
else
{
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
clientStreamWriter
=
new
StreamWriter
(
clientStream
)
{
NewLine
=
ProxyConstants
.
NewLine
};
}
//now read the request line
var
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
//Now create the request
disposed
=
await
HandleHttpSessionRequest
(
tcpClient
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
endPoint
.
EnableSsl
?
endPoint
.
GenericCertificateName
:
null
,
endPoint
,
null
);
//now read the request line
var
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
if
(!
disposed
)
//Now create the request
disposed
=
await
HandleHttpSessionRequest
(
tcpClient
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
endPoint
.
EnableSsl
?
endPoint
.
GenericCertificateName
:
null
,
endPoint
,
null
);
}
finally
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
if
(!
disposed
)
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
}
}
}
...
...
@@ -271,9 +259,10 @@ namespace Titanium.Web.Proxy
}
private
async
Task
<
bool
>
HandleHttpSessionRequestInternal
(
TcpConnection
connection
,
SessionEventArgs
args
,
bool
closeConnection
)
private
async
Task
<
bool
>
HandleHttpSessionRequestInternal
(
TcpConnection
connection
,
SessionEventArgs
args
,
bool
closeConnection
)
{
bool
dispose
=
true
;
try
{
args
.
WebSession
.
Request
.
RequestLocked
=
true
;
...
...
@@ -281,11 +270,6 @@ namespace Titanium.Web.Proxy
//If request was cancelled by user then dispose the client
if
(
args
.
WebSession
.
Request
.
CancelRequest
)
{
Dispose
(
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
ProxyClient
.
ClientStreamWriter
,
args
.
WebSession
.
ServerConnection
);
return
true
;
}
...
...
@@ -355,45 +339,38 @@ namespace Titanium.Web.Proxy
//already disposed inside above method
if
(
disposed
)
{
return
disposed
;
dispose
=
false
;
return
true
;
}
}
//if connection is closing exit
if
(
args
.
WebSession
.
Response
.
ResponseKeepAlive
==
false
)
{
Dispose
(
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
ProxyClient
.
ClientStreamWriter
,
args
.
WebSession
.
ServerConnection
);
return
true
;
}
if
(!
closeConnection
)
{
dispose
=
false
;
return
false
;
}
}
catch
(
Exception
e
)
{
ExceptionFunc
(
new
ProxyHttpException
(
"Error occured whilst handling session request (internal)"
,
e
,
args
));
Dispose
(
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
ProxyClient
.
ClientStreamWriter
,
args
.
WebSession
.
ServerConnection
);
return
true
;
}
if
(
closeConnection
)
finally
{
//dispose
Dispose
(
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
ProxyClient
.
ClientStreamWriter
,
args
.
WebSession
.
ServerConnection
);
return
true
;
if
(
dispose
)
{
//dispose
Dispose
(
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
ProxyClient
.
ClientStreamWriter
,
args
.
WebSession
.
ServerConnection
);
}
}
return
fals
e
;
return
tru
e
;
}
/// <summary>
...
...
@@ -412,7 +389,7 @@ namespace Titanium.Web.Proxy
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
httpsHostName
,
ProxyEndPoint
endPoint
,
List
<
HttpHeader
>
connectHeaders
)
{
var
disposed
=
false
;
bool
disposed
=
false
;
TcpConnection
connection
=
null
;
...
...
@@ -422,12 +399,6 @@ namespace Titanium.Web.Proxy
{
if
(
string
.
IsNullOrEmpty
(
httpCmd
))
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
...
...
@@ -489,12 +460,6 @@ namespace Titanium.Web.Proxy
await
CheckAuthorization
(
clientStreamWriter
,
args
.
WebSession
.
Request
.
RequestHeaders
.
Values
)
==
false
)
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
...
...
@@ -515,12 +480,6 @@ namespace Titanium.Web.Proxy
httpCmd
,
httpVersion
,
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
IsHttps
,
clientStream
,
tcpConnectionFactory
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
...
...
@@ -540,24 +499,12 @@ namespace Titanium.Web.Proxy
if
(
args
.
WebSession
.
Request
.
CancelRequest
)
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
//if connection is closing exit
if
(
args
.
WebSession
.
Response
.
ResponseKeepAlive
==
false
)
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
...
...
@@ -567,18 +514,16 @@ namespace Titanium.Web.Proxy
catch
(
Exception
e
)
{
ExceptionFunc
(
new
ProxyHttpException
(
"Error occured whilst handling session request"
,
e
,
args
));
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
disposed
=
true
;
break
;
}
}
return
disposed
;
if
(!
disposed
)
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connection
);
}
return
true
;
}
/// <summary>
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
e68cc842
...
...
@@ -53,8 +53,8 @@ namespace Titanium.Web.Proxy
}
var
connection
=
await
GetServerConnection
(
args
);
var
result
=
await
HandleHttpSessionRequestInternal
(
null
,
args
,
true
);
return
result
;
var
disposed
=
await
HandleHttpSessionRequestInternal
(
null
,
args
,
true
);
return
disposed
;
}
args
.
WebSession
.
Response
.
ResponseLocked
=
true
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment