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
fc3a9308
Commit
fc3a9308
authored
May 15, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use light weight lock in example
parent
de7c2da0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
26 deletions
+40
-26
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+40
-26
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
fc3a9308
...
@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
...
@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Net
;
using
System.Net
;
using
System.Net.Security
;
using
System.Net.Security
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Exceptions
;
using
Titanium.Web.Proxy.Exceptions
;
...
@@ -14,7 +15,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -14,7 +15,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
{
{
public
class
ProxyTestController
public
class
ProxyTestController
{
{
private
readonly
object
lockObj
=
new
object
(
);
private
readonly
SemaphoreSlim
@lock
=
new
SemaphoreSlim
(
1
);
private
readonly
ProxyServer
proxyServer
;
private
readonly
ProxyServer
proxyServer
;
...
@@ -30,27 +31,34 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -30,27 +31,34 @@ namespace Titanium.Web.Proxy.Examples.Basic
//proxyServer.CertificateManager.TrustRootCertificate();
//proxyServer.CertificateManager.TrustRootCertificate();
//proxyServer.CertificateManager.TrustRootCertificateAsAdmin();
//proxyServer.CertificateManager.TrustRootCertificateAsAdmin();
proxyServer
.
ExceptionFunc
=
exception
=>
proxyServer
.
ExceptionFunc
=
async
exception
=>
{
{
lock
(
lockObj
)
await
@lock
.
WaitAsync
();
try
{
var
color
=
Console
.
ForegroundColor
;
Console
.
ForegroundColor
=
ConsoleColor
.
Red
;
if
(
exception
is
ProxyHttpException
phex
)
{
Console
.
WriteLine
(
exception
.
Message
+
": "
+
phex
.
InnerException
?.
Message
);
}
else
{
Console
.
WriteLine
(
exception
.
Message
);
}
Console
.
ForegroundColor
=
color
;
}
finally
{
{
var
color
=
Console
.
ForegroundColor
;
@lock
.
Release
();
Console
.
ForegroundColor
=
ConsoleColor
.
Red
;
if
(
exception
is
ProxyHttpException
phex
)
{
Console
.
WriteLine
(
exception
.
Message
+
": "
+
phex
.
InnerException
?.
Message
);
}
else
{
Console
.
WriteLine
(
exception
.
Message
);
}
Console
.
ForegroundColor
=
color
;
}
}
};
};
proxyServer
.
ForwardToUpstreamGateway
=
true
;
proxyServer
.
ForwardToUpstreamGateway
=
true
;
proxyServer
.
CertificateManager
.
SaveFakeCertificates
=
true
;
proxyServer
.
CertificateManager
.
SaveFakeCertificates
=
true
;
// optionally set the Certificate Engine
// optionally set the Certificate Engine
// Under Mono or Non-Windows runtimes only BouncyCastle will be supported
// Under Mono or Non-Windows runtimes only BouncyCastle will be supported
//proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
//proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
...
@@ -122,7 +130,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -122,7 +130,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
proxyServer
.
ClientCertificateSelectionCallback
-=
OnCertificateSelection
;
proxyServer
.
ClientCertificateSelectionCallback
-=
OnCertificateSelection
;
proxyServer
.
Stop
();
proxyServer
.
Stop
();
// remove the generated certificates
// remove the generated certificates
//proxyServer.CertificateManager.RemoveTrustedRootCertificates();
//proxyServer.CertificateManager.RemoveTrustedRootCertificates();
}
}
...
@@ -130,7 +138,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -130,7 +138,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
private
async
Task
OnBeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
private
async
Task
OnBeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
{
{
string
hostname
=
e
.
WebSession
.
Request
.
RequestUri
.
Host
;
string
hostname
=
e
.
WebSession
.
Request
.
RequestUri
.
Host
;
WriteToConsole
(
"Tunnel to: "
+
hostname
);
await
WriteToConsole
(
"Tunnel to: "
+
hostname
);
if
(
hostname
.
Contains
(
"dropbox.com"
))
if
(
hostname
.
Contains
(
"dropbox.com"
))
{
{
...
@@ -148,8 +156,8 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -148,8 +156,8 @@ namespace Titanium.Web.Proxy.Examples.Basic
// intecept & cancel redirect or update requests
// intecept & cancel redirect or update requests
private
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
private
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
{
{
WriteToConsole
(
"Active Client Connections:"
+
((
ProxyServer
)
sender
).
ClientConnectionCount
);
await
WriteToConsole
(
"Active Client Connections:"
+
((
ProxyServer
)
sender
).
ClientConnectionCount
);
WriteToConsole
(
e
.
WebSession
.
Request
.
Url
);
await
WriteToConsole
(
e
.
WebSession
.
Request
.
Url
);
// store it in the UserData property
// store it in the UserData property
// It can be a simple integer, Guid, or any type
// It can be a simple integer, Guid, or any type
...
@@ -187,19 +195,19 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -187,19 +195,19 @@ namespace Titanium.Web.Proxy.Examples.Basic
}
}
// Modify response
// Modify response
private
void
MultipartRequestPartSent
(
object
sender
,
MultipartRequestPartSentEventArgs
e
)
private
async
Task
MultipartRequestPartSent
(
object
sender
,
MultipartRequestPartSentEventArgs
e
)
{
{
var
session
=
(
SessionEventArgs
)
sender
;
var
session
=
(
SessionEventArgs
)
sender
;
WriteToConsole
(
"Multipart form data headers:"
);
await
WriteToConsole
(
"Multipart form data headers:"
);
foreach
(
var
header
in
e
.
Headers
)
foreach
(
var
header
in
e
.
Headers
)
{
{
WriteToConsole
(
header
.
ToString
());
await
WriteToConsole
(
header
.
ToString
());
}
}
}
}
private
async
Task
OnResponse
(
object
sender
,
SessionEventArgs
e
)
private
async
Task
OnResponse
(
object
sender
,
SessionEventArgs
e
)
{
{
WriteToConsole
(
"Active Server Connections:"
+
((
ProxyServer
)
sender
).
ServerConnectionCount
);
await
WriteToConsole
(
"Active Server Connections:"
+
((
ProxyServer
)
sender
).
ServerConnectionCount
);
string
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsolutePath
);
string
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsolutePath
);
...
@@ -271,12 +279,18 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -271,12 +279,18 @@ namespace Titanium.Web.Proxy.Examples.Basic
return
Task
.
FromResult
(
0
);
return
Task
.
FromResult
(
0
);
}
}
private
void
WriteToConsole
(
string
message
)
private
async
Task
WriteToConsole
(
string
message
)
{
{
lock
(
lockObj
)
await
@lock
.
WaitAsync
();
try
{
{
Console
.
WriteLine
(
message
);
Console
.
WriteLine
(
message
);
}
}
finally
{
@lock
.
Release
();
}
}
}
///// <summary>
///// <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