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
3da0c593
Commit
3da0c593
authored
Feb 06, 2016
by
titanium007
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite system proxy set
parent
de0ec9c4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
60 deletions
+113
-60
ProxyTestController.cs
Titanium.Web.Proxy.Test/ProxyTestController.cs
+6
-4
EndPoint.cs
Titanium.Web.Proxy/Models/EndPoint.cs
+4
-2
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+102
-53
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+1
-1
No files found.
Titanium.Web.Proxy.Test/ProxyTestController.cs
View file @
3da0c593
...
@@ -20,20 +20,22 @@ namespace Titanium.Web.Proxy.Test
...
@@ -20,20 +20,22 @@ namespace Titanium.Web.Proxy.Test
//Usefull for clients that use certificate pinning
//Usefull for clients that use certificate pinning
//for example dropbox.com
//for example dropbox.com
var
explicitEndPoint
=
new
ExplicitProxyEndPoint
(
IPAddress
.
Loopback
,
8000
,
true
){
var
explicitEndPoint
=
new
ExplicitProxyEndPoint
(
IPAddress
.
Loopback
,
8000
,
true
){
ExcludedHostNameRegex
=
new
List
<
string
>()
{
"dropbox.com"
}
ExcludedH
ttpsH
ostNameRegex
=
new
List
<
string
>()
{
"dropbox.com"
}
};
};
var
transparentEndPoint
=
new
TransparentProxyEndPoint
(
IPAddress
.
Loopback
,
8001
,
true
);
var
transparentEndPoint
=
new
TransparentProxyEndPoint
(
IPAddress
.
Loopback
,
8001
,
true
);
ProxyServer
.
AddEndPoint
(
explicitEndPoint
);
ProxyServer
.
AddEndPoint
(
explicitEndPoint
);
ProxyServer
.
AddEndPoint
(
transparentEndPoint
);
ProxyServer
.
Start
();
ProxyServer
.
Start
();
//You can also add/remove end points after proxy has been started
ProxyServer
.
AddEndPoint
(
transparentEndPoint
);
foreach
(
var
endPoint
in
ProxyServer
.
ProxyEndPoints
)
foreach
(
var
endPoint
in
ProxyServer
.
ProxyEndPoints
)
Console
.
WriteLine
(
"Listening on '{0}' endpoint at Ip {1} and port: {2} "
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
Console
.
WriteLine
(
"Listening on '{0}' endpoint at Ip {1} and port: {2} "
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
ProxyServer
.
SetAsSystemProxy
(
explicitEndPoint
);
ProxyServer
.
SetAsSystem
Http
Proxy
(
explicitEndPoint
);
ProxyServer
.
SetAsSystemHttpsProxy
(
explicitEndPoint
);
}
}
public
void
Stop
()
public
void
Stop
()
...
...
Titanium.Web.Proxy/Models/EndPoint.cs
View file @
3da0c593
...
@@ -25,8 +25,10 @@ namespace Titanium.Web.Proxy.Models
...
@@ -25,8 +25,10 @@ namespace Titanium.Web.Proxy.Models
public
class
ExplicitProxyEndPoint
:
ProxyEndPoint
public
class
ExplicitProxyEndPoint
:
ProxyEndPoint
{
{
internal
bool
IsSystemProxy
{
get
;
set
;
}
internal
bool
IsSystemHttpProxy
{
get
;
set
;
}
public
List
<
string
>
ExcludedHostNameRegex
{
get
;
set
;
}
internal
bool
IsSystemHttpsProxy
{
get
;
set
;
}
public
List
<
string
>
ExcludedHttpsHostNameRegex
{
get
;
set
;
}
public
ExplicitProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
public
ExplicitProxyEndPoint
(
IPAddress
IpAddress
,
int
Port
,
bool
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
:
base
(
IpAddress
,
Port
,
EnableSsl
)
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
3da0c593
...
@@ -35,7 +35,7 @@ namespace Titanium.Web.Proxy
...
@@ -35,7 +35,7 @@ namespace Titanium.Web.Proxy
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
#if NET45
#if NET45
internal
static
SslProtocols
SupportedProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
internal
static
SslProtocols
SupportedProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
#else
#else
public
static
SslProtocols
SupportedProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Ssl3
;
public
static
SslProtocols
SupportedProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Ssl3
;
#endif
#endif
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
private
static
CertificateManager
CertManager
{
get
;
set
;
}
private
static
CertificateManager
CertManager
{
get
;
set
;
}
private
static
bool
EnableSsl
{
get
;
set
;
}
private
static
bool
EnableSsl
{
get
;
set
;
}
private
static
bool
certTrusted
{
get
;
set
;
}
private
static
bool
certTrusted
{
get
;
set
;
}
private
static
bool
proxy
Started
{
get
;
set
;
}
private
static
bool
proxy
Running
{
get
;
set
;
}
public
static
string
RootCertificateName
{
get
;
set
;
}
public
static
string
RootCertificateName
{
get
;
set
;
}
...
@@ -69,52 +69,78 @@ namespace Titanium.Web.Proxy
...
@@ -69,52 +69,78 @@ namespace Titanium.Web.Proxy
public
static
void
AddEndPoint
(
ProxyEndPoint
endPoint
)
public
static
void
AddEndPoint
(
ProxyEndPoint
endPoint
)
{
{
if
(
proxyStarted
)
throw
new
Exception
(
"Cannot add end points after proxy started."
);
ProxyEndPoints
.
Add
(
endPoint
);
ProxyEndPoints
.
Add
(
endPoint
);
if
(
proxyRunning
)
Listen
(
endPoint
);
}
}
public
static
void
SetAsSystemProxy
(
Explicit
ProxyEndPoint
endPoint
)
public
static
void
RemoveEndPoint
(
ProxyEndPoint
endPoint
)
{
{
if
(
ProxyEndPoints
.
Contains
(
endPoint
)
==
false
)
if
(
ProxyEndPoints
.
Contains
(
endPoint
)
==
false
)
throw
new
Exception
(
"Cannot
set endPoints not added to proxy as system
proxy"
);
throw
new
Exception
(
"Cannot
remove endPoints not added to
proxy"
);
if
(!
proxyStarted
)
ProxyEndPoints
.
Remove
(
endPoint
);
throw
new
Exception
(
"Cannot set system proxy settings before proxy has been started."
);
if
(
proxyRunning
)
QuitListen
(
endPoint
);
}
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemProxy
=
false
);
endPoint
.
IsSystemProxy
=
true
;
public
static
void
SetAsSystemHttpProxy
(
ExplicitProxyEndPoint
endPoint
)
{
VerifyProxy
(
endPoint
);
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpProxy
=
false
);
SystemProxyHelper
.
EnableProxyHttp
(
SystemProxyHelper
.
EnableProxyHttp
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
endPoint
.
IsSystemHttpProxy
=
true
;
#if !DEBUG
#if !DEBUG
FireFoxHelper
.
AddFirefox
();
FireFoxHelper
.
AddFirefox
();
#endif
#endif
Console
.
WriteLine
(
"Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy"
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
}
public
static
void
SetAsSystemHttpsProxy
(
ExplicitProxyEndPoint
endPoint
)
{
VerifyProxy
(
endPoint
);
if
(!
endPoint
.
EnableSsl
)
{
throw
new
Exception
(
"Endpoint do not support Https connections"
);
}
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpsProxy
=
false
);
RootCertificateName
=
RootCertificateName
??
"Titanium_Proxy_Test_Root"
;
if
(
endPoint
.
EnableSsl
)
//If certificate was trusted by the machine
if
(
certTrusted
)
{
{
RootCertificateName
=
RootCertificateName
??
"Titanium_Proxy_Test_Root"
;
SystemProxyHelper
.
EnableProxyHttps
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
//If certificate was trusted by the machine
endPoint
.
Port
);
if
(
certTrusted
)
{
SystemProxyHelper
.
EnableProxyHttps
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
}
}
}
Console
.
WriteLine
(
"Set endpoint at Ip {1} and port: {2} as System Proxy"
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
)
;
endPoint
.
IsSystemHttpsProxy
=
true
;
#if !DEBUG
FireFoxHelper
.
AddFirefox
();
#endif
Console
.
WriteLine
(
"Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy"
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
}
}
public
static
void
Start
()
public
static
void
Start
()
{
{
if
(
proxyRunning
)
throw
new
Exception
(
"Proxy is already running."
);
EnableSsl
=
ProxyEndPoints
.
Any
(
x
=>
x
.
EnableSsl
);
EnableSsl
=
ProxyEndPoints
.
Any
(
x
=>
x
.
EnableSsl
);
if
(
EnableSsl
)
if
(
EnableSsl
)
...
@@ -122,22 +148,65 @@ namespace Titanium.Web.Proxy
...
@@ -122,22 +148,65 @@ namespace Titanium.Web.Proxy
foreach
(
var
endPoint
in
ProxyEndPoints
)
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
{
Listen
(
endPoint
);
}
proxyRunning
=
true
;
}
public
static
void
Stop
()
{
if
(!
proxyRunning
)
throw
new
Exception
(
"Proxy is not running."
);
endPoint
.
listener
=
new
TcpListener
(
endPoint
.
IpAddress
,
endPoint
.
Port
);
var
SetAsSystemProxy
=
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
Any
(
x
=>
x
.
IsSystemHttpProxy
||
x
.
IsSystemHttpsProxy
);
endPoint
.
listener
.
Start
();
endPoint
.
Port
=
((
IPEndPoint
)
endPoint
.
listener
.
LocalEndpoint
).
Port
;
if
(
SetAsSystemProxy
)
// accept clients asynchronously
{
endPoint
.
listener
.
BeginAcceptTcpClient
(
OnAcceptConnection
,
endPoint
);
SystemProxyHelper
.
DisableAllProxy
();
#if !DEBUG
FireFoxHelper
.
RemoveFirefox
();
#endif
}
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
endPoint
.
listener
.
Stop
();
}
}
proxyStarted
=
true
;
CertManager
.
Dispose
();
proxyRunning
=
false
;
}
private
static
void
Listen
(
ProxyEndPoint
endPoint
)
{
endPoint
.
listener
=
new
TcpListener
(
endPoint
.
IpAddress
,
endPoint
.
Port
);
endPoint
.
listener
.
Start
();
endPoint
.
Port
=
((
IPEndPoint
)
endPoint
.
listener
.
LocalEndpoint
).
Port
;
// accept clients asynchronously
endPoint
.
listener
.
BeginAcceptTcpClient
(
OnAcceptConnection
,
endPoint
);
}
private
static
void
QuitListen
(
ProxyEndPoint
endPoint
)
{
endPoint
.
listener
.
Stop
();
}
private
static
void
VerifyProxy
(
ExplicitProxyEndPoint
endPoint
)
{
if
(
ProxyEndPoints
.
Contains
(
endPoint
)
==
false
)
throw
new
Exception
(
"Cannot set endPoints not added to proxy as system proxy"
);
if
(!
proxyRunning
)
throw
new
Exception
(
"Cannot set system proxy settings before proxy has been started."
);
}
}
private
static
void
OnAcceptConnection
(
IAsyncResult
asyn
)
private
static
void
OnAcceptConnection
(
IAsyncResult
asyn
)
{
{
var
endPoint
=
(
ProxyEndPoint
)
asyn
.
AsyncState
;
var
endPoint
=
(
ProxyEndPoint
)
asyn
.
AsyncState
;
var
client
=
endPoint
.
listener
.
EndAcceptTcpClient
(
asyn
);
var
client
=
endPoint
.
listener
.
EndAcceptTcpClient
(
asyn
);
try
try
...
@@ -151,30 +220,10 @@ namespace Titanium.Web.Proxy
...
@@ -151,30 +220,10 @@ namespace Titanium.Web.Proxy
{
{
// ignored
// ignored
}
}
// Get the listener that handles the client request.
// Get the listener that handles the client request.
endPoint
.
listener
.
BeginAcceptTcpClient
(
OnAcceptConnection
,
endPoint
);
endPoint
.
listener
.
BeginAcceptTcpClient
(
OnAcceptConnection
,
endPoint
);
}
public
static
void
Stop
()
{
var
SetAsSystemProxy
=
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
Any
(
x
=>
x
.
IsSystemProxy
);
if
(
SetAsSystemProxy
)
{
SystemProxyHelper
.
DisableAllProxy
();
#if !DEBUG
FireFoxHelper
.
RemoveFirefox
();
#endif
}
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
endPoint
.
listener
.
Stop
();
}
CertManager
.
Dispose
();
}
}
}
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/RequestHandler.cs
View file @
3da0c593
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
var
httpVersion
=
httpCmdSplit
[
2
];
var
httpVersion
=
httpCmdSplit
[
2
];
var
excluded
=
endPoint
.
ExcludedH
ostNameRegex
!=
null
?
endPoint
.
Excluded
HostNameRegex
.
Any
(
x
=>
Regex
.
IsMatch
(
httpRemoteUri
.
Host
,
x
))
:
false
;
var
excluded
=
endPoint
.
ExcludedH
ttpsHostNameRegex
!=
null
?
endPoint
.
ExcludedHttps
HostNameRegex
.
Any
(
x
=>
Regex
.
IsMatch
(
httpRemoteUri
.
Host
,
x
))
:
false
;
//Client wants to create a secure tcp tunnel (its a HTTPS request)
//Client wants to create a secure tcp tunnel (its a HTTPS request)
if
(
httpVerb
.
ToUpper
()
==
"CONNECT"
&&
!
excluded
&&
httpRemoteUri
.
Port
!=
80
)
if
(
httpVerb
.
ToUpper
()
==
"CONNECT"
&&
!
excluded
&&
httpRemoteUri
.
Port
!=
80
)
...
...
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