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
44d042ae
Commit
44d042ae
authored
Oct 28, 2016
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add request Id; refactor process id as Lazy<int>
parent
1acc1ad8
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
468 additions
and
412 deletions
+468
-412
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+1
-1
CertificateHandler.cs
Titanium.Web.Proxy/CertificateHandler.cs
+11
-10
CertificateSelectionEventArgs.cs
...Web.Proxy/EventArguments/CertificateSelectionEventArgs.cs
+7
-11
Network.cs
Titanium.Web.Proxy/Helpers/Network.cs
+60
-0
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+15
-9
ProxyAuthorizationHandler.cs
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
+96
-96
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+24
-32
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+253
-253
Titanium.Web.Proxy.csproj
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
+1
-0
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
44d042ae
...
@@ -117,7 +117,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -117,7 +117,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
var
responseHeaders
=
e
.
WebSession
.
Response
.
ResponseHeaders
;
var
responseHeaders
=
e
.
WebSession
.
Response
.
ResponseHeaders
;
// print out process id of current session
// print out process id of current session
Console
.
WriteLine
(
$"PID:
{
e
.
WebSession
.
ProcessId
}
"
);
Console
.
WriteLine
(
$"PID:
{
e
.
WebSession
.
ProcessId
.
Value
}
"
);
//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
if
(
e
.
WebSession
.
Request
.
Method
==
"GET"
||
e
.
WebSession
.
Request
.
Method
==
"POST"
)
if
(
e
.
WebSession
.
Request
.
Method
==
"GET"
||
e
.
WebSession
.
Request
.
Method
==
"POST"
)
...
...
Titanium.Web.Proxy/CertificateHandler.cs
View file @
44d042ae
...
@@ -60,9 +60,10 @@ namespace Titanium.Web.Proxy
...
@@ -60,9 +60,10 @@ namespace Titanium.Web.Proxy
/// Call back to select client certificate used for mutual authentication
/// Call back to select client certificate used for mutual authentication
/// </summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="targetHost"></param>
/// <param name="chain"></param>
/// <param name="localCertificates"></param>
/// <param name="sslPolicyErrors"></param>
/// <param name="remoteCertificate"></param>
/// <param name="acceptableIssuers"></param>
/// <returns></returns>
/// <returns></returns>
internal
X509Certificate
SelectClientCertificate
(
internal
X509Certificate
SelectClientCertificate
(
object
sender
,
object
sender
,
...
@@ -101,11 +102,11 @@ namespace Titanium.Web.Proxy
...
@@ -101,11 +102,11 @@ namespace Titanium.Web.Proxy
{
{
var
args
=
new
CertificateSelectionEventArgs
();
var
args
=
new
CertificateSelectionEventArgs
();
args
.
t
argetHost
=
targetHost
;
args
.
T
argetHost
=
targetHost
;
args
.
l
ocalCertificates
=
localCertificates
;
args
.
L
ocalCertificates
=
localCertificates
;
args
.
r
emoteCertificate
=
remoteCertificate
;
args
.
R
emoteCertificate
=
remoteCertificate
;
args
.
a
cceptableIssuers
=
acceptableIssuers
;
args
.
A
cceptableIssuers
=
acceptableIssuers
;
args
.
c
lientCertificate
=
clientCertificate
;
args
.
C
lientCertificate
=
clientCertificate
;
Delegate
[]
invocationList
=
ClientCertificateSelectionCallback
.
GetInvocationList
();
Delegate
[]
invocationList
=
ClientCertificateSelectionCallback
.
GetInvocationList
();
Task
[]
handlerTasks
=
new
Task
[
invocationList
.
Length
];
Task
[]
handlerTasks
=
new
Task
[
invocationList
.
Length
];
...
@@ -117,7 +118,7 @@ namespace Titanium.Web.Proxy
...
@@ -117,7 +118,7 @@ namespace Titanium.Web.Proxy
Task
.
WhenAll
(
handlerTasks
).
Wait
();
Task
.
WhenAll
(
handlerTasks
).
Wait
();
return
args
.
c
lientCertificate
;
return
args
.
C
lientCertificate
;
}
}
return
clientCertificate
;
return
clientCertificate
;
...
...
Titanium.Web.Proxy/EventArguments/CertificateSelectionEventArgs.cs
View file @
44d042ae
...
@@ -6,19 +6,15 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -6,19 +6,15 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// <summary>
/// An argument passed on to user for client certificate selection during mutual SSL authentication
/// An argument passed on to user for client certificate selection during mutual SSL authentication
/// </summary>
/// </summary>
public
class
CertificateSelectionEventArgs
:
EventArgs
,
IDisposable
public
class
CertificateSelectionEventArgs
:
EventArgs
{
{
public
object
s
ender
{
get
;
internal
set
;
}
public
object
S
ender
{
get
;
internal
set
;
}
public
string
t
argetHost
{
get
;
internal
set
;
}
public
string
T
argetHost
{
get
;
internal
set
;
}
public
X509CertificateCollection
l
ocalCertificates
{
get
;
internal
set
;
}
public
X509CertificateCollection
L
ocalCertificates
{
get
;
internal
set
;
}
public
X509Certificate
r
emoteCertificate
{
get
;
internal
set
;
}
public
X509Certificate
R
emoteCertificate
{
get
;
internal
set
;
}
public
string
[]
a
cceptableIssuers
{
get
;
internal
set
;
}
public
string
[]
A
cceptableIssuers
{
get
;
internal
set
;
}
public
X509Certificate
c
lientCertificate
{
get
;
set
;
}
public
X509Certificate
C
lientCertificate
{
get
;
set
;
}
public
void
Dispose
()
{
throw
new
NotImplementedException
();
}
}
}
}
}
Titanium.Web.Proxy/Helpers/Network.cs
0 → 100644
View file @
44d042ae
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Titanium.Web.Proxy.Helpers
{
internal
class
NetworkHelper
{
private
static
int
FindProcessIdFromLocalPort
(
int
port
,
IpVersion
ipVersion
)
{
var
tcpRow
=
TcpHelper
.
GetExtendedTcpTable
(
ipVersion
).
FirstOrDefault
(
row
=>
row
.
LocalEndPoint
.
Port
==
port
);
return
tcpRow
?.
ProcessId
??
0
;
}
internal
static
int
GetProcessIdFromPort
(
int
port
,
bool
ipV6Enabled
)
{
var
processId
=
FindProcessIdFromLocalPort
(
port
,
IpVersion
.
Ipv4
);
if
(
processId
>
0
&&
!
ipV6Enabled
)
{
return
processId
;
}
return
FindProcessIdFromLocalPort
(
port
,
IpVersion
.
Ipv6
);
}
/// <summary>
/// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
/// </summary>
/// <param name="address></param>
/// <returns></returns>
internal
static
bool
IsLocalIpAddress
(
IPAddress
address
)
{
try
{
// get local IP addresses
IPAddress
[]
localIPs
=
Dns
.
GetHostAddresses
(
Dns
.
GetHostName
());
// test if any host IP equals to any local IP or to localhost
// is localhost
if
(
IPAddress
.
IsLoopback
(
address
))
return
true
;
// is local address
foreach
(
IPAddress
localIP
in
localIPs
)
{
if
(
address
.
Equals
(
localIP
))
return
true
;
}
}
catch
{
}
return
false
;
}
}
}
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
44d042ae
...
@@ -14,28 +14,39 @@ namespace Titanium.Web.Proxy.Http
...
@@ -14,28 +14,39 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
/// </summary>
public
class
HttpWebClient
public
class
HttpWebClient
{
{
private
int
processId
;
/// <summary>
/// <summary>
/// Connection to server
/// Connection to server
/// </summary>
/// </summary>
internal
TcpConnection
ServerConnection
{
get
;
set
;
}
internal
TcpConnection
ServerConnection
{
get
;
set
;
}
public
Guid
RequestId
{
get
;
private
set
;
}
public
List
<
HttpHeader
>
ConnectHeaders
{
get
;
set
;
}
public
List
<
HttpHeader
>
ConnectHeaders
{
get
;
set
;
}
public
Request
Request
{
get
;
set
;
}
public
Request
Request
{
get
;
set
;
}
public
Response
Response
{
get
;
set
;
}
public
Response
Response
{
get
;
set
;
}
/// <summary>
/// <summary>
/// PID of the process that is created the current session
/// PID of the process that is created the current session when client is running in this machine
/// If client is remote then this will return
/// </summary>
/// </summary>
public
int
ProcessId
{
get
;
internal
set
;
}
public
Lazy
<
int
>
ProcessId
{
get
;
internal
set
;
}
/// <summary>
/// <summary>
/// Is Https?
/// Is Https?
/// </summary>
/// </summary>
public
bool
IsHttps
=>
this
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
public
bool
IsHttps
=>
this
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
internal
HttpWebClient
()
{
this
.
RequestId
=
Guid
.
NewGuid
();
this
.
Request
=
new
Request
();
this
.
Response
=
new
Response
();
}
/// <summary>
/// <summary>
/// Set the tcp connection to server used by this webclient
/// Set the tcp connection to server used by this webclient
/// </summary>
/// </summary>
...
@@ -46,11 +57,6 @@ namespace Titanium.Web.Proxy.Http
...
@@ -46,11 +57,6 @@ namespace Titanium.Web.Proxy.Http
ServerConnection
=
connection
;
ServerConnection
=
connection
;
}
}
internal
HttpWebClient
()
{
this
.
Request
=
new
Request
();
this
.
Response
=
new
Response
();
}
/// <summary>
/// <summary>
/// Prepare & send the http(s) request
/// Prepare & send the http(s) request
...
...
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
View file @
44d042ae
Titanium.Web.Proxy/RequestHandler.cs
View file @
44d042ae
...
@@ -25,31 +25,12 @@ namespace Titanium.Web.Proxy
...
@@ -25,31 +25,12 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
partial
class
ProxyServer
partial
class
ProxyServer
{
{
private
int
FindProcessIdFromLocalPort
(
int
port
,
IpVersion
ipVersion
)
{
var
tcpRow
=
TcpHelper
.
GetExtendedTcpTable
(
ipVersion
).
FirstOrDefault
(
row
=>
row
.
LocalEndPoint
.
Port
==
port
);
return
tcpRow
?.
ProcessId
??
0
;
}
private
int
GetProcessIdFromPort
(
int
port
,
bool
ipV6Enabled
)
{
var
processId
=
FindProcessIdFromLocalPort
(
port
,
IpVersion
.
Ipv4
);
if
(
processId
>
0
&&
!
ipV6Enabled
)
{
return
processId
;
}
return
FindProcessIdFromLocalPort
(
port
,
IpVersion
.
Ipv6
);
}
//This is called when client is aware of proxy
//This is called when client is aware of proxy
//So for HTTPS requests client would send CONNECT header to negotiate a secure tcp tunnel via proxy
//So for HTTPS requests client would send CONNECT header to negotiate a secure tcp tunnel via proxy
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
{
{
var
processId
=
GetProcessIdFromPort
(((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
,
endPoint
.
IpV6Enabled
);
Stream
clientStream
=
tcpClient
.
GetStream
();
Stream
clientStream
=
tcpClient
.
GetStream
();
...
@@ -177,10 +158,10 @@ namespace Titanium.Web.Proxy
...
@@ -177,10 +158,10 @@ namespace Titanium.Web.Proxy
return
;
return
;
}
}
//Now create the request
//Now create the request
await
HandleHttpSessionRequest
(
tcpClient
,
processId
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
await
HandleHttpSessionRequest
(
tcpClient
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
httpRemoteUri
.
Scheme
==
Uri
.
UriSchemeHttps
?
httpRemoteUri
.
Host
:
null
,
connectRequestHeaders
,
null
,
null
);
httpRemoteUri
.
Scheme
==
Uri
.
UriSchemeHttps
?
httpRemoteUri
.
Host
:
null
,
endPoint
,
connectRequestHeaders
,
null
,
null
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
)
{
{
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
}
}
...
@@ -190,7 +171,6 @@ namespace Titanium.Web.Proxy
...
@@ -190,7 +171,6 @@ namespace Titanium.Web.Proxy
//So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
//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
)
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
{
{
var
processId
=
GetProcessIdFromPort
(((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
,
endPoint
.
IpV6Enabled
);
Stream
clientStream
=
tcpClient
.
GetStream
();
Stream
clientStream
=
tcpClient
.
GetStream
();
...
@@ -238,8 +218,8 @@ namespace Titanium.Web.Proxy
...
@@ -238,8 +218,8 @@ namespace Titanium.Web.Proxy
var
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
var
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
//Now create the request
//Now create the request
await
HandleHttpSessionRequest
(
tcpClient
,
processId
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
await
HandleHttpSessionRequest
(
tcpClient
,
httpCmd
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
endPoint
.
EnableSsl
?
endPoint
.
GenericCertificateName
:
null
,
null
);
endPoint
.
EnableSsl
?
endPoint
.
GenericCertificateName
:
null
,
endPoint
,
null
);
}
}
private
async
Task
HandleHttpSessionRequestInternal
(
TcpConnection
connection
,
SessionEventArgs
args
,
ExternalProxy
customUpStreamHttpProxy
,
ExternalProxy
customUpStreamHttpsProxy
,
bool
CloseConnection
)
private
async
Task
HandleHttpSessionRequestInternal
(
TcpConnection
connection
,
SessionEventArgs
args
,
ExternalProxy
customUpStreamHttpProxy
,
ExternalProxy
customUpStreamHttpsProxy
,
bool
CloseConnection
)
...
@@ -371,15 +351,14 @@ namespace Titanium.Web.Proxy
...
@@ -371,15 +351,14 @@ namespace Titanium.Web.Proxy
/// This is the core request handler method for a particular connection from client
/// This is the core request handler method for a particular connection from client
/// </summary>
/// </summary>
/// <param name="client"></param>
/// <param name="client"></param>
/// <param name="processId"></param>
/// <param name="httpCmd"></param>
/// <param name="httpCmd"></param>
/// <param name="clientStream"></param>
/// <param name="clientStream"></param>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamWriter"></param>
/// <param name="clientStreamWriter"></param>
/// <param name="httpsHostName"></param>
/// <param name="httpsHostName"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
HandleHttpSessionRequest
(
TcpClient
client
,
int
processId
,
string
httpCmd
,
Stream
clientStream
,
private
async
Task
HandleHttpSessionRequest
(
TcpClient
client
,
string
httpCmd
,
Stream
clientStream
,
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
httpsHostName
,
List
<
HttpHeader
>
connectHeaders
,
ExternalProxy
customUpStreamHttpProxy
=
null
,
ExternalProxy
customUpStreamHttpsProxy
=
null
)
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
httpsHostName
,
ProxyEndPoint
endPoint
,
List
<
HttpHeader
>
connectHeaders
,
ExternalProxy
customUpStreamHttpProxy
=
null
,
ExternalProxy
customUpStreamHttpsProxy
=
null
)
{
{
TcpConnection
connection
=
null
;
TcpConnection
connection
=
null
;
...
@@ -396,8 +375,21 @@ namespace Titanium.Web.Proxy
...
@@ -396,8 +375,21 @@ namespace Titanium.Web.Proxy
var
args
=
new
SessionEventArgs
(
BUFFER_SIZE
,
HandleHttpSessionResponse
);
var
args
=
new
SessionEventArgs
(
BUFFER_SIZE
,
HandleHttpSessionResponse
);
args
.
ProxyClient
.
TcpClient
=
client
;
args
.
ProxyClient
.
TcpClient
=
client
;
args
.
WebSession
.
ConnectHeaders
=
connectHeaders
;
args
.
WebSession
.
ConnectHeaders
=
connectHeaders
;
args
.
WebSession
.
ProcessId
=
processId
;
args
.
WebSession
.
ProcessId
=
new
Lazy
<
int
>(()
=>
{
var
remoteEndPoint
=
(
IPEndPoint
)
args
.
ProxyClient
.
TcpClient
.
Client
.
RemoteEndPoint
;
//If client is localhost get the process id
if
(
NetworkHelper
.
IsLocalIpAddress
(
remoteEndPoint
.
Address
))
{
return
NetworkHelper
.
GetProcessIdFromPort
(
remoteEndPoint
.
Port
,
endPoint
.
IpV6Enabled
);
}
//can't access process Id of remote request from remote machine
return
-
1
;
});
try
try
{
{
//break up the line into three components (method, remote URL & Http Version)
//break up the line into three components (method, remote URL & Http Version)
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
44d042ae
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
View file @
44d042ae
...
@@ -66,6 +66,7 @@
...
@@ -66,6 +66,7 @@
<Compile
Include=
"EventArguments\CertificateSelectionEventArgs.cs"
/>
<Compile
Include=
"EventArguments\CertificateSelectionEventArgs.cs"
/>
<Compile
Include=
"EventArguments\CertificateValidationEventArgs.cs"
/>
<Compile
Include=
"EventArguments\CertificateValidationEventArgs.cs"
/>
<Compile
Include=
"Extensions\ByteArrayExtensions.cs"
/>
<Compile
Include=
"Extensions\ByteArrayExtensions.cs"
/>
<Compile
Include=
"Helpers\Network.cs"
/>
<Compile
Include=
"Network\CachedCertificate.cs"
/>
<Compile
Include=
"Network\CachedCertificate.cs"
/>
<Compile
Include=
"Network\CertificateMaker.cs"
/>
<Compile
Include=
"Network\CertificateMaker.cs"
/>
<Compile
Include=
"Network\ProxyClient.cs"
/>
<Compile
Include=
"Network\ProxyClient.cs"
/>
...
...
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