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
6836d313
Commit
6836d313
authored
Feb 10, 2015
by
justcoding121
Committed by
justcoding121
Feb 10, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace Thread with Task, Accept TCP Async
parent
0a0ce603
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
60 additions
and
40 deletions
+60
-40
ProxyServer.cs
Titanium.HTTPProxyServer/ProxyServer.cs
+40
-16
RequestHandler.cs
Titanium.HTTPProxyServer/Request/RequestHandler.cs
+1
-1
RequestManager.cs
Titanium.HTTPProxyServer/Request/RequestManager.cs
+2
-4
ResponseManager.cs
Titanium.HTTPProxyServer/Response/ResponseManager.cs
+1
-1
CompressionUtility.cs
Titanium.HTTPProxyServer/Utility/CompressionUtility.cs
+8
-8
RawTCPRelay.cs
Titanium.HTTPProxyServer/Utility/RawTCPRelay.cs
+8
-10
No files found.
Titanium.HTTPProxyServer/ProxyServer.cs
View file @
6836d313
...
...
@@ -32,18 +32,18 @@ namespace Titanium.HTTPProxyServer
private
static
Dictionary
<
string
,
X509Certificate2
>
certificateCache
=
new
Dictionary
<
string
,
X509Certificate2
>();
private
static
X509Store
_store
=
new
X509Store
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
private
TcpListener
_listener
;
private
Thread
_listenerThread
;
private
static
TcpListener
_listener
;
private
static
Thread
_listenerThread
;
public
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
public
IPAddress
ListeningIPInterface
{
get
;
set
;
}
public
Int32
ListeningPort
public
static
Int32
ListeningPort
{
get
{
...
...
@@ -73,7 +73,7 @@ namespace Titanium.HTTPProxyServer
}
public
bool
Start
()
public
static
bool
Start
()
{
_listener
=
new
TcpListener
(
IPAddress
.
Any
,
0
);
_listener
.
Start
();
...
...
@@ -81,20 +81,20 @@ namespace Titanium.HTTPProxyServer
_listenerThread
.
Start
(
_listener
);
_listenerThread
.
IsBackground
=
true
;
return
true
;
}
public
void
Stop
()
public
static
void
Stop
()
{
_listener
.
Stop
();
_listenerThread
.
Abort
();
_listenerThread
.
Join
();
}
private
void
Listen
(
Object
obj
)
// Thread signal.
public
static
ManualResetEvent
tcpClientConnected
=
new
ManualResetEvent
(
false
);
private
static
void
Listen
(
Object
obj
)
{
TcpListener
listener
=
(
TcpListener
)
obj
;
...
...
@@ -102,8 +102,13 @@ namespace Titanium.HTTPProxyServer
{
while
(
true
)
{
TcpClient
client
=
listener
.
AcceptTcpClient
();
while
(!
ThreadPool
.
UnsafeQueueUserWorkItem
(
new
WaitCallback
(
ProcessClient
),
client
))
;
// Set the event to nonsignaled state.
tcpClientConnected
.
Reset
();
listener
.
BeginAcceptTcpClient
(
new
AsyncCallback
(
DoAcceptTcpClientCallback
),
listener
);
// Wait until a connection is made and processed before
// continuing.
tcpClientConnected
.
WaitOne
();
}
}
catch
(
ThreadAbortException
ex
)
{
Debug
.
WriteLine
(
ex
.
Message
);
}
...
...
@@ -114,16 +119,35 @@ namespace Titanium.HTTPProxyServer
}
public
static
void
DoAcceptTcpClientCallback
(
IAsyncResult
ar
)
{
// Get the listener that handles the client request.
TcpListener
listener
=
(
TcpListener
)
ar
.
AsyncState
;
private
void
ProcessClient
(
Object
obj
)
// End the operation and display the received data on
// the console.
TcpClient
client
=
listener
.
EndAcceptTcpClient
(
ar
);
while
(!
ThreadPool
.
UnsafeQueueUserWorkItem
(
new
WaitCallback
(
ProcessClient
),
client
))
;
// Signal the calling thread to continue.
tcpClientConnected
.
Set
();
}
private
static
int
pending
=
0
;
private
static
void
ProcessClient
(
Object
param
)
{
try
{
TcpClient
client
=
(
TcpClient
)
obj
;
TcpClient
client
=
param
as
TcpClient
;
DoHttpProcessing
(
client
);
client
.
Close
();
}
catch
{
}
catch
(
Exception
ex
)
{
Debug
.
WriteLine
(
ex
.
Message
);
}
}
...
...
Titanium.HTTPProxyServer/Request/RequestHandler.cs
View file @
6836d313
...
...
@@ -10,7 +10,7 @@ namespace Titanium.HTTPProxyServer
{
public
partial
class
ProxyServer
{
private
void
GetRequestStreamCallback
(
IAsyncResult
asynchronousResult
)
private
static
void
GetRequestStreamCallback
(
IAsyncResult
asynchronousResult
)
{
var
Args
=
(
SessionEventArgs
)
asynchronousResult
.
AsyncState
;
...
...
Titanium.HTTPProxyServer/Request/RequestManager.cs
View file @
6836d313
...
...
@@ -19,12 +19,10 @@ namespace Titanium.HTTPProxyServer
partial
class
ProxyServer
{
private
int
pending
=
0
;
private
void
DoHttpProcessing
(
TcpClient
client
)
{
pending
++;
private
static
void
DoHttpProcessing
(
TcpClient
client
)
{
string
ConnectionGroup
=
null
;
...
...
Titanium.HTTPProxyServer/Response/ResponseManager.cs
View file @
6836d313
...
...
@@ -14,7 +14,7 @@ namespace Titanium.HTTPProxyServer
{
partial
class
ProxyServer
{
private
void
GetResponseCallback
(
IAsyncResult
asynchronousResult
)
private
static
void
GetResponseCallback
(
IAsyncResult
asynchronousResult
)
{
SessionEventArgs
Args
=
(
SessionEventArgs
)
asynchronousResult
.
AsyncState
;
...
...
Titanium.HTTPProxyServer/Utility/CompressionUtility.cs
View file @
6836d313
...
...
@@ -9,7 +9,7 @@ namespace Titanium.HTTPProxyServer
public
partial
class
ProxyServer
{
private
void
SendNormal
(
Stream
inStream
,
Stream
outStream
)
private
static
void
SendNormal
(
Stream
inStream
,
Stream
outStream
)
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
...
...
@@ -23,7 +23,7 @@ namespace Titanium.HTTPProxyServer
}
}
private
void
SendChunked
(
Stream
inStream
,
Stream
outStream
)
private
static
void
SendChunked
(
Stream
inStream
,
Stream
outStream
)
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
...
...
@@ -45,7 +45,7 @@ namespace Titanium.HTTPProxyServer
outStream
.
Write
(
ChunkEnd
,
0
,
ChunkEnd
.
Length
);
}
private
void
SendChunked
(
byte
[]
data
,
Stream
outStream
)
private
static
void
SendChunked
(
byte
[]
data
,
Stream
outStream
)
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
...
...
@@ -67,14 +67,14 @@ namespace Titanium.HTTPProxyServer
}
private
byte
[]
EncodeData
(
string
ResponseData
,
Encoding
e
)
private
static
byte
[]
EncodeData
(
string
ResponseData
,
Encoding
e
)
{
return
e
.
GetBytes
(
ResponseData
);
}
private
byte
[]
CompressZlib
(
string
ResponseData
,
Encoding
e
)
private
static
byte
[]
CompressZlib
(
string
ResponseData
,
Encoding
e
)
{
Byte
[]
bytes
=
e
.
GetBytes
(
ResponseData
);
...
...
@@ -95,7 +95,7 @@ namespace Titanium.HTTPProxyServer
}
}
private
byte
[]
CompressDeflate
(
string
ResponseData
,
Encoding
e
)
private
static
byte
[]
CompressDeflate
(
string
ResponseData
,
Encoding
e
)
{
Byte
[]
bytes
=
e
.
GetBytes
(
ResponseData
);
...
...
@@ -112,7 +112,7 @@ namespace Titanium.HTTPProxyServer
}
}
private
byte
[]
CompressGzip
(
string
ResponseData
,
Encoding
e
)
private
static
byte
[]
CompressGzip
(
string
ResponseData
,
Encoding
e
)
{
Byte
[]
bytes
=
e
.
GetBytes
(
ResponseData
);
...
...
@@ -131,7 +131,7 @@ namespace Titanium.HTTPProxyServer
}
}
private
void
sendData
(
Stream
outStream
,
byte
[]
data
,
bool
isChunked
)
private
static
void
sendData
(
Stream
outStream
,
byte
[]
data
,
bool
isChunked
)
{
if
(!
isChunked
)
{
...
...
Titanium.HTTPProxyServer/Utility/RawTCPRelay.cs
View file @
6836d313
...
...
@@ -2,10 +2,10 @@
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading
;
using
System.Net.Security
;
using
System.IO
;
using
System.Net
;
using
System.Threading.Tasks
;
namespace
Titanium.HTTPProxyServer
{
...
...
@@ -19,14 +19,13 @@ namespace Titanium.HTTPProxyServer
var
tunnelStream
=
tunnelClient
.
GetStream
();
var
tunnelReadBuffer
=
new
byte
[
BUFFER_SIZE
];
T
hread
sendRelay
=
new
Thread
(()
=>
StreamUtilities
.
CopyTo
(
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
T
hread
receiveRelay
=
new
Thread
(()
=>
StreamUtilities
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
T
ask
sendRelay
=
new
Task
(()
=>
StreamUtilities
.
CopyTo
(
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
T
ask
receiveRelay
=
new
Task
(()
=>
StreamUtilities
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
receiveRelay
.
Start
();
sendRelay
.
Join
();
receiveRelay
.
Join
();
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
if
(
tunnelStream
!=
null
)
tunnelStream
.
Close
();
...
...
@@ -81,7 +80,7 @@ namespace Titanium.HTTPProxyServer
}
System
.
Net
.
Sockets
.
TcpClient
tunnelClient
=
new
System
.
Net
.
Sockets
.
TcpClient
(
hostname
,
tunnelPort
);
var
tunnelStream
=
(
System
.
IO
.
Stream
)
tunnelClient
.
GetStream
()
;
var
tunnelStream
=
tunnelClient
.
GetStream
()
as
System
.
IO
.
Stream
;
if
(
isSecure
)
{
...
...
@@ -91,14 +90,13 @@ namespace Titanium.HTTPProxyServer
}
Thread
sendRelay
=
new
Thread
(()
=>
StreamUtilities
.
CopyTo
(
sb
.
ToString
(),
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
Thread
receiveRelay
=
new
Thread
(()
=>
StreamUtilities
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
var
sendRelay
=
new
Task
(()
=>
StreamUtilities
.
CopyTo
(
sb
.
ToString
(),
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
var
receiveRelay
=
new
Task
(()
=>
StreamUtilities
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
receiveRelay
.
Start
();
sendRelay
.
Join
();
receiveRelay
.
Join
();
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
if
(
tunnelStream
!=
null
)
tunnelStream
.
Close
();
...
...
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