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
347adaab
Commit
347adaab
authored
Mar 12, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into beta
parents
1405c3ab
3b066c2e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
89 deletions
+105
-89
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+40
-40
CertificateManagerTests.cs
...s/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
+0
-1
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+55
-17
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+10
-31
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
347adaab
...
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
proxyServer
.
ExceptionFunc
=
exception
=>
Console
.
WriteLine
(
exception
.
Message
);
proxyServer
.
ForwardToUpstreamGateway
=
true
;
proxyServer
.
CertificateManager
.
SaveFakeCertificates
=
true
;
//optionally set the Certificate Engine
//Under Mono or Non-Windows runtimes only BouncyCastle will be supported
//proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
...
...
@@ -165,31 +165,31 @@ namespace Titanium.Web.Proxy.Examples.Basic
// e.MultipartRequestPartSent += MultipartRequestPartSent;
//}
if
(
e
.
WebSession
.
Request
.
HasBody
)
{
//Get/Set request body bytes
var
bodyBytes
=
await
e
.
GetRequestBody
();
await
e
.
SetRequestBody
(
bodyBytes
);
//
if (e.WebSession.Request.HasBody)
//
{
//
//Get/Set request body bytes
//
var bodyBytes = await e.GetRequestBody();
//
await e.SetRequestBody(bodyBytes);
//Get/Set request body as string
string
bodyString
=
await
e
.
GetRequestBodyAsString
();
await
e
.
SetRequestBodyString
(
bodyString
);
//
//Get/Set request body as string
//
string bodyString = await e.GetRequestBodyAsString();
//
await e.SetRequestBodyString(bodyString);
//requestBodyHistory[e.Id] = bodyString;
}
//
//requestBodyHistory[e.Id] = bodyString;
//
}
//To cancel a request with a custom HTML content
//Filter URL
if
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsoluteUri
.
Contains
(
"yahoo.com"
))
{
e
.
Ok
(
"<!DOCTYPE html>"
+
"<html><body><h1>"
+
"Website Blocked"
+
"</h1>"
+
"<p>Blocked by titanium web proxy.</p>"
+
"</body>"
+
"</html>"
);
}
//
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("yahoo.com"))
//
{
//
e.Ok("<!DOCTYPE html>" +
//
"<html><body><h1>" +
//
"Website Blocked" +
//
"</h1>" +
//
"<p>Blocked by titanium web proxy.</p>" +
//
"</body>" +
//
"</html>");
//
}
////Redirect example
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
...
...
@@ -219,27 +219,27 @@ namespace Titanium.Web.Proxy.Examples.Basic
// var requestBody = requestBodyHistory[e.Id];
//}
//read response headers
responseHeaderHistory
[
e
.
Id
]
=
e
.
WebSession
.
Response
.
Headers
;
//
//
read response headers
//
responseHeaderHistory[e.Id] = e.WebSession.Response.Headers;
// print out process id of current session
//Console.WriteLine($"PID: {e.WebSession.ProcessId.Value}");
//
//
print out process id of current session
//
//
Console.WriteLine($"PID: {e.WebSession.ProcessId.Value}");
//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
.
Response
.
StatusCode
==
(
int
)
HttpStatusCode
.
OK
)
{
if
(
e
.
WebSession
.
Response
.
ContentType
!=
null
&&
e
.
WebSession
.
Response
.
ContentType
.
Trim
().
ToLower
().
Contains
(
"text/html"
))
{
var
bodyBytes
=
await
e
.
GetResponseBody
();
await
e
.
SetResponseBody
(
bodyBytes
);
string
body
=
await
e
.
GetResponseBodyAsString
();
await
e
.
SetResponseBodyString
(
body
);
}
}
}
//
//
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.Response.StatusCode == (int)HttpStatusCode.OK)
//
{
//
if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
//
{
//
var bodyBytes = await e.GetResponseBody();
//
await e.SetResponseBody(bodyBytes);
//
string body = await e.GetResponseBodyAsString();
//
await e.SetResponseBodyString(body);
//
}
//
}
//
}
}
/// <summary>
...
...
Tests/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
View file @
347adaab
...
...
@@ -43,7 +43,6 @@ namespace Titanium.Web.Proxy.UnitTests
}
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
//cannot run this test in build server since trusting the certificate won't happen successfully
[
TestMethod
]
public
async
Task
Simple_Create_Win_Certificate_Test
()
{
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
347adaab
...
...
@@ -67,18 +67,21 @@ namespace Titanium.Web.Proxy.Network
internal
bool
CertValidated
=>
RootCertificate
!=
null
;
/// <summary>
/// Trust the RootCertificate used by this proxy server
/// Note that this do not make the client trust the certificate!
/// This would import the root certificate to the certificate store of machine that runs this proxy server
/// Trust the RootCertificate used by this proxy server for current user
/// </summary>
internal
bool
UserTrustRoot
{
get
;
set
;
}
=
false
;
/// <summary>
/// Needs elevated permission. Works only on Windows.
/// <para>Puts the certificate to the local machine's certificate store.</para>
/// <para>Certutil.exe is a command-line program that is installed as part of Certificate Services</para>
/// Trust the RootCertificate used by this proxy server for current machine
/// Needs elevated permission, otherwise will fail silently.
/// </summary>
internal
bool
MachineTrustRootAsAdministrator
{
get
;
set
;
}
=
false
;
internal
bool
MachineTrustRoot
{
get
;
set
;
}
=
false
;
/// <summary>
/// Whether trust operations should be done with elevated privillages
/// Will prompt with UAC if required. Works only on Windows.
/// </summary>
internal
bool
TrustRootAsAdministrator
{
get
;
set
;
}
=
false
;
/// <summary>
/// Select Certificate Engine
...
...
@@ -186,9 +189,36 @@ namespace Titanium.Web.Proxy.Network
public
X509KeyStorageFlags
StorageFlag
{
get
;
set
;
}
=
X509KeyStorageFlags
.
Exportable
;
internal
CertificateManager
(
Action
<
Exception
>
exceptionFunc
)
/// <summary>
/// Constructor.
/// </summary>
/// <param name="rootCertificateName">Name of root certificate.</param>
/// <param name="rootCertificateIssuerName">Name of root certificate issuer.</param>
/// <param name="userTrustRootCertificate"></param>
/// <param name="machineTrustRootCertificate">Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true</param>
/// <param name="trustRootCertificateAsAdmin "></param>
/// <param name="exceptionFunc"></param>
internal
CertificateManager
(
string
rootCertificateName
,
string
rootCertificateIssuerName
,
bool
userTrustRootCertificate
,
bool
machineTrustRootCertificate
,
bool
trustRootCertificateAsAdmin
,
Action
<
Exception
>
exceptionFunc
)
{
this
.
exceptionFunc
=
exceptionFunc
;
UserTrustRoot
=
userTrustRootCertificate
;
if
(
machineTrustRootCertificate
)
{
MachineTrustRoot
=
machineTrustRootCertificate
;
}
TrustRootAsAdministrator
=
trustRootCertificateAsAdmin
;
if
(
rootCertificateName
!=
null
)
{
RootCertificateName
=
rootCertificateName
;
}
if
(
rootCertificateIssuerName
!=
null
)
{
RootCertificateIssuerName
=
rootCertificateIssuerName
;
}
if
(
RunTime
.
IsWindows
)
{
//this is faster in Windows based on tests (see unit test project CertificateManagerTests.cs)
...
...
@@ -617,6 +647,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary>
public
void
TrustRootCertificate
(
bool
machineTrusted
=
false
)
{
//currentUser\personal
InstallCertificate
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
...
...
@@ -698,21 +729,20 @@ namespace Titanium.Web.Proxy.Network
/// Ensure certificates are setup (creates root if required)
/// Also makes root certificate trusted based on initial setup from proxy constructor for user/machine trust.
/// </summary>
public
void
EnsureRootCertificate
(
bool
machineTrustRootCertificate
=
false
)
public
void
EnsureRootCertificate
()
{
if
(!
CertValidated
)
{
CreateRootCertificate
();
}
if
(
UserTrustRoot
)
if
(
TrustRootAsAdministrator
)
{
TrustRootCertificate
(
machineTrustRootCertificate
);
TrustRootCertificate
AsAdmin
(
MachineTrustRoot
);
}
if
(
MachineTrustRootAsAdministrator
)
else
if
(
UserTrustRoot
)
{
TrustRootCertificate
AsAdmin
(
);
TrustRootCertificate
(
MachineTrustRoot
);
}
}
...
...
@@ -720,12 +750,20 @@ namespace Titanium.Web.Proxy.Network
/// <summary>
/// Ensure certificates are setup (creates root if required)
/// Also makes root certificate trusted based on provided parameters
/// Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true
/// </summary>
public
void
EnsureRootCertificate
(
bool
userTrustRootCertificate
,
bool
machineTrustRootCertificate
,
bool
machineTrustRootCertificateAsAdmin
)
public
void
EnsureRootCertificate
(
bool
userTrustRootCertificate
=
true
,
bool
machineTrustRootCertificate
=
false
,
bool
trustRootCertificateAsAdmin
=
false
)
{
if
(
machineTrustRootCertificate
)
{
userTrustRootCertificate
=
true
;
}
UserTrustRoot
=
userTrustRootCertificate
;
MachineTrustRootAsAdministrator
=
machineTrustRootCertificateAsAdmin
;
EnsureRootCertificate
(
machineTrustRootCertificate
);
MachineTrustRoot
=
machineTrustRootCertificate
;
TrustRootAsAdministrator
=
trustRootCertificateAsAdmin
;
EnsureRootCertificate
();
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
347adaab
...
...
@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy
/// <summary>
/// Seconds client/server connection are to be kept alive when waiting for read/write to complete
/// </summary>
public
int
ConnectionTimeOutSeconds
{
get
;
set
;
}
public
int
ConnectionTimeOutSeconds
{
get
;
set
;
}
/// <summary>
/// Total number of active client connections
...
...
@@ -208,31 +208,22 @@ namespace Titanium.Web.Proxy
/// <summary>
/// Constructor
/// </summary>
public
ProxyServer
()
:
this
(
null
,
null
,
true
,
false
)
/// <param name="userTrustRootCertificate"></param>
/// <param name="machineTrustRootCertificate">Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true</param>
/// <param name="trustRootCertificateAsAdmin "></param>
public
ProxyServer
(
bool
userTrustRootCertificate
=
true
,
bool
machineTrustRootCertificate
=
false
,
bool
trustRootCertificateAsAdmin
=
false
)
:
this
(
null
,
null
,
userTrustRootCertificate
,
machineTrustRootCertificate
,
trustRootCertificateAsAdmin
)
{
}
/// <summary>
/// Constructor
/// </summary>
public
ProxyServer
(
bool
trustRoot
)
:
this
(
null
,
null
,
trustRoot
,
false
)
{
}
/// <summary>
/// Constructor
/// </summary>
public
ProxyServer
(
bool
trustRoot
,
bool
trustRootAsAdmin
)
:
this
(
null
,
null
,
trustRoot
,
trustRootAsAdmin
)
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="rootCertificateName">Name of root certificate.</param>
/// <param name="rootCertificateIssuerName">Name of root certificate issuer.</param>
public
ProxyServer
(
string
rootCertificateName
,
string
rootCertificateIssuerName
,
bool
trustRootCertificate
,
bool
trustRootCertificateAsAdmin
)
/// <param name="userTrustRootCertificate"></param>
/// <param name="machineTrustRootCertificate">Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true</param>
/// <param name="trustRootCertificateAsAdmin "></param>
public
ProxyServer
(
string
rootCertificateName
,
string
rootCertificateIssuerName
,
bool
userTrustRootCertificate
=
true
,
bool
machineTrustRootCertificate
=
false
,
bool
trustRootCertificateAsAdmin
=
false
)
{
//default values
ConnectionTimeOutSeconds
=
30
;
...
...
@@ -244,19 +235,7 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager
=
new
SystemProxyManager
();
}
CertificateManager
=
new
CertificateManager
(
ExceptionFunc
);
CertificateManager
.
UserTrustRoot
=
trustRootCertificate
;
CertificateManager
.
MachineTrustRootAsAdministrator
=
trustRootCertificateAsAdmin
;
if
(
rootCertificateName
!=
null
)
{
CertificateManager
.
RootCertificateName
=
rootCertificateName
;
}
if
(
rootCertificateIssuerName
!=
null
)
{
CertificateManager
.
RootCertificateIssuerName
=
rootCertificateIssuerName
;
}
CertificateManager
=
new
CertificateManager
(
rootCertificateName
,
rootCertificateIssuerName
,
userTrustRootCertificate
,
machineTrustRootCertificate
,
trustRootCertificateAsAdmin
,
ExceptionFunc
);
}
/// <summary>
...
...
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