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
a9d1011b
Commit
a9d1011b
authored
Oct 20, 2016
by
Hakan Arıcı
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IPv6 support is added to finding PID from port mechanism.
parent
12814410
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
82 deletions
+104
-82
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+13
-4
EndPoint.cs
Titanium.Web.Proxy/Models/EndPoint.cs
+68
-64
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+23
-14
No files found.
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
a9d1011b
...
...
@@ -15,9 +15,16 @@ using Titanium.Web.Proxy.Tcp;
namespace
Titanium.Web.Proxy.Helpers
{
internal
enum
IpVersion
{
Ipv4
=
1
,
Ipv6
=
2
,
}
internal
partial
class
NativeMethods
{
internal
const
int
AfInet
=
2
;
internal
const
int
AfInet6
=
23
;
internal
enum
TcpTableType
{
...
...
@@ -75,19 +82,21 @@ namespace Titanium.Web.Proxy.Helpers
/// Gets the extended TCP table.
/// </summary>
/// <returns>Collection of <see cref="TcpRow"/>.</returns>
internal
static
TcpTable
GetExtendedTcpTable
()
internal
static
TcpTable
GetExtendedTcpTable
(
IpVersion
ipVersion
)
{
List
<
TcpRow
>
tcpRows
=
new
List
<
TcpRow
>();
IntPtr
tcpTable
=
IntPtr
.
Zero
;
int
tcpTableLength
=
0
;
int
tcpTableLength
=
0
;
var
ipVersionValue
=
ipVersion
==
IpVersion
.
Ipv4
?
NativeMethods
.
AfInet
:
NativeMethods
.
AfInet6
;
if
(
NativeMethods
.
GetExtendedTcpTable
(
tcpTable
,
ref
tcpTableLength
,
false
,
NativeMethods
.
AfInet
,
(
int
)
NativeMethods
.
TcpTableType
.
OwnerPidAll
,
0
)
!=
0
)
if
(
NativeMethods
.
GetExtendedTcpTable
(
tcpTable
,
ref
tcpTableLength
,
false
,
ipVersionValue
,
(
int
)
NativeMethods
.
TcpTableType
.
OwnerPidAll
,
0
)
!=
0
)
{
try
{
tcpTable
=
Marshal
.
AllocHGlobal
(
tcpTableLength
);
if
(
NativeMethods
.
GetExtendedTcpTable
(
tcpTable
,
ref
tcpTableLength
,
true
,
NativeMethods
.
AfInet
,
(
int
)
NativeMethods
.
TcpTableType
.
OwnerPidAll
,
0
)
==
0
)
if
(
NativeMethods
.
GetExtendedTcpTable
(
tcpTable
,
ref
tcpTableLength
,
true
,
ipVersionValue
,
(
int
)
NativeMethods
.
TcpTableType
.
OwnerPidAll
,
0
)
==
0
)
{
NativeMethods
.
TcpTable
table
=
(
NativeMethods
.
TcpTable
)
Marshal
.
PtrToStructure
(
tcpTable
,
typeof
(
NativeMethods
.
TcpTable
));
...
...
Titanium.Web.Proxy/Models/EndPoint.cs
View file @
a9d1011b
using
System.Collections.Generic
;
using
System.Net
;
using
System.Net.Sockets
;
namespace
Titanium.Web.Proxy.Models
{
/// <summary>
/// An abstract endpoint where the proxy listens
/// </summary>
public
abstract
class
ProxyEndPoint
{
public
ProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
{
this
.
IpAddress
=
IpAddress
;
this
.
Port
=
Port
;
this
.
EnableSsl
=
EnableSsl
;
}
public
IPAddress
IpAddress
{
get
;
internal
set
;
}
public
int
Port
{
get
;
internal
set
;
}
public
bool
EnableSsl
{
get
;
internal
set
;
}
internal
TcpListener
listener
{
get
;
set
;
}
}
/// <summary>
/// A proxy endpoint that the client is aware of
/// So client application know that it is communicating with a proxy server
/// </summary>
public
class
ExplicitProxyEndPoint
:
ProxyEndPoint
{
internal
bool
IsSystemHttpProxy
{
get
;
set
;
}
internal
bool
IsSystemHttpsProxy
{
get
;
set
;
}
public
List
<
string
>
ExcludedHttpsHostNameRegex
{
get
;
set
;
}
public
ExplicitProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
{
}
}
/// <summary>
/// A proxy end point client is not aware of
/// Usefull when requests are redirected to this proxy end point through port forwarding
/// </summary>
public
class
TransparentProxyEndPoint
:
ProxyEndPoint
{
//Name of the Certificate need to be sent (same as the hostname we want to proxy)
//This is valid only when UseServerNameIndication is set to false
public
string
GenericCertificateName
{
get
;
set
;
}
// public bool UseServerNameIndication { get; set; }
public
TransparentProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
{
this
.
GenericCertificateName
=
"localhost"
;
}
}
}
using
System.Collections.Generic
;
using
System.Net
;
using
System.Net.Sockets
;
namespace
Titanium.Web.Proxy.Models
{
/// <summary>
/// An abstract endpoint where the proxy listens
/// </summary>
public
abstract
class
ProxyEndPoint
{
public
ProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
{
this
.
IpAddress
=
IpAddress
;
this
.
Port
=
Port
;
this
.
EnableSsl
=
EnableSsl
;
}
public
IPAddress
IpAddress
{
get
;
internal
set
;
}
public
int
Port
{
get
;
internal
set
;
}
public
bool
EnableSsl
{
get
;
internal
set
;
}
public
bool
IpV6Enabled
=>
IpAddress
==
IPAddress
.
IPv6Any
||
IpAddress
==
IPAddress
.
IPv6Loopback
||
IpAddress
==
IPAddress
.
IPv6None
;
internal
TcpListener
listener
{
get
;
set
;
}
}
/// <summary>
/// A proxy endpoint that the client is aware of
/// So client application know that it is communicating with a proxy server
/// </summary>
public
class
ExplicitProxyEndPoint
:
ProxyEndPoint
{
internal
bool
IsSystemHttpProxy
{
get
;
set
;
}
internal
bool
IsSystemHttpsProxy
{
get
;
set
;
}
public
List
<
string
>
ExcludedHttpsHostNameRegex
{
get
;
set
;
}
public
ExplicitProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
{
}
}
/// <summary>
/// A proxy end point client is not aware of
/// Usefull when requests are redirected to this proxy end point through port forwarding
/// </summary>
public
class
TransparentProxyEndPoint
:
ProxyEndPoint
{
//Name of the Certificate need to be sent (same as the hostname we want to proxy)
//This is valid only when UseServerNameIndication is set to false
public
string
GenericCertificateName
{
get
;
set
;
}
// public bool UseServerNameIndication { get; set; }
public
TransparentProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
{
this
.
GenericCertificateName
=
"localhost"
;
}
}
}
Titanium.Web.Proxy/RequestHandler.cs
View file @
a9d1011b
...
...
@@ -16,7 +16,6 @@ using Titanium.Web.Proxy.Shared;
using
Titanium.Web.Proxy.Http
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Extensions
;
using
System.Text
;
namespace
Titanium.Web.Proxy
{
...
...
@@ -25,16 +24,32 @@ namespace Titanium.Web.Proxy
/// </summary>
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
//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
)
{
var
tcpRow
=
TcpHelper
.
GetExtendedTcpTable
().
FirstOrDefault
(
row
=>
row
.
LocalEndPoint
.
Port
==
((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
);
var
processId
=
tcpRow
?.
ProcessId
??
0
;
var
processId
=
GetProcessIdFromPort
(((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
,
endPoint
.
IpV6Enabled
);
Stream
clientStream
=
tcpClient
.
GetStream
();
clientStream
.
ReadTimeout
=
ConnectionTimeOutSeconds
*
1000
;
...
...
@@ -175,10 +190,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
tcpRow
=
TcpHelper
.
GetExtendedTcpTable
().
FirstOrDefault
(
row
=>
row
.
LocalEndPoint
.
Port
==
((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
);
var
processId
=
tcpRow
?.
ProcessId
??
0
;
var
processId
=
GetProcessIdFromPort
(((
IPEndPoint
)
tcpClient
.
Client
.
RemoteEndPoint
).
Port
,
endPoint
.
IpV6Enabled
);
Stream
clientStream
=
tcpClient
.
GetStream
();
...
...
@@ -230,7 +242,6 @@ namespace Titanium.Web.Proxy
endPoint
.
EnableSsl
?
endPoint
.
GenericCertificateName
:
null
,
null
);
}
private
async
Task
HandleHttpSessionRequestInternal
(
TcpConnection
connection
,
SessionEventArgs
args
,
ExternalProxy
customUpStreamHttpProxy
,
ExternalProxy
customUpStreamHttpsProxy
,
bool
CloseConnection
)
{
try
...
...
@@ -357,6 +368,7 @@ namespace Titanium.Web.Proxy
connection
.
Dispose
();
}
}
/// <summary>
/// This is the core request handler method for a particular connection from client
/// </summary>
...
...
@@ -568,7 +580,6 @@ namespace Titanium.Web.Proxy
webRequest
.
Request
.
RequestHeaders
=
requestHeaders
;
}
/// <summary>
/// This is called when the request is PUT/POST to read the body
/// </summary>
...
...
@@ -590,9 +601,7 @@ namespace Titanium.Web.Proxy
else
if
(
args
.
WebSession
.
Request
.
IsChunked
)
{
await
args
.
ProxyClient
.
ClientStreamReader
.
CopyBytesToStreamChunked
(
BUFFER_SIZE
,
postStream
);
}
}
}
}
\ No newline at end of file
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