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
9115e03f
Commit
9115e03f
authored
Sep 01, 2015
by
justcoding121
Committed by
justcoding121
Sep 01, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix stack overflow
Fixed stack overflow by avoiding recursive calls using threadpool
parent
affaacf2
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
368 additions
and
299 deletions
+368
-299
Program.cs
Titanium.Web.Proxy.Test/Program.cs
+22
-18
ProxyTestController.cs
Titanium.Web.Proxy.Test/ProxyTestController.cs
+22
-2
Titanium.Web.Proxy.sln
Titanium.Web.Proxy.sln
+3
-0
CertificateManager.cs
Titanium.Web.Proxy/Helpers/CertificateManager.cs
+8
-8
Compression.cs
Titanium.Web.Proxy/Helpers/Compression.cs
+10
-8
CustomBinaryReader.cs
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
+8
-8
NetFramework.cs
Titanium.Web.Proxy/Helpers/NetFramework.cs
+1
-0
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+11
-7
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+15
-15
SessionEventArgs.cs
Titanium.Web.Proxy/Models/SessionEventArgs.cs
+59
-44
AssemblyInfo.cs
Titanium.Web.Proxy/Properties/AssemblyInfo.cs
+33
-0
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+23
-19
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+63
-74
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+88
-93
Titanium.Web.Proxy.csproj
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
+2
-3
No files found.
Titanium.Web.Proxy.Test/Program.cs
View file @
9115e03f
...
@@ -16,14 +16,14 @@ namespace Titanium.Web.Proxy.Test
...
@@ -16,14 +16,14 @@ namespace Titanium.Web.Proxy.Test
public
static
void
Main
(
string
[]
args
)
public
static
void
Main
(
string
[]
args
)
{
{
//On Console exit make sure we also exit the proxy
//On Console exit make sure we also exit the proxy
handler
=
new
ConsoleEventDelegate
(
ConsoleEventCallback
);
NativeMethods
.
handler
=
new
NativeMethods
.
ConsoleEventDelegate
(
ConsoleEventCallback
);
SetConsoleCtrlHandler
(
handler
,
true
);
NativeMethods
.
SetConsoleCtrlHandler
(
NativeMethods
.
handler
,
true
);
Console
.
Write
(
"Do you want to monitor HTTPS? (Y/N):"
);
Console
.
Write
(
"Do you want to monitor HTTPS? (Y/N):"
);
if
(
Console
.
ReadLine
().
Trim
().
ToLower
()==
"y"
)
if
(
Console
.
ReadLine
().
Trim
().
ToLower
()
==
"y"
)
{
{
controller
.
EnableSSL
=
true
;
controller
.
EnableSSL
=
true
;
...
@@ -61,15 +61,19 @@ namespace Titanium.Web.Proxy.Test
...
@@ -61,15 +61,19 @@ namespace Titanium.Web.Proxy.Test
}
}
return
false
;
return
false
;
}
}
// Keeps it from getting garbage collected
private
static
ConsoleEventDelegate
handler
;
}
// Pinvoke
internal
static
class
NativeMethods
private
delegate
bool
ConsoleEventDelegate
(
int
eventType
);
{
[
DllImport
(
"kernel32.dll"
,
SetLastError
=
true
)]
[
DllImport
(
"kernel32.dll"
,
SetLastError
=
true
)]
private
static
extern
bool
SetConsoleCtrlHandler
(
ConsoleEventDelegate
callback
,
bool
add
);
internal
static
extern
bool
SetConsoleCtrlHandler
(
ConsoleEventDelegate
callback
,
bool
add
);
// Pinvoke
internal
delegate
bool
ConsoleEventDelegate
(
int
eventType
);
// Keeps it from getting garbage collected
internal
static
ConsoleEventDelegate
handler
;
}
}
}
}
Titanium.Web.Proxy.Test/ProxyTestController.cs
View file @
9115e03f
...
@@ -59,14 +59,34 @@ namespace Titanium.Web.Proxy.Test
...
@@ -59,14 +59,34 @@ namespace Titanium.Web.Proxy.Test
Console
.
WriteLine
(
e
.
RequestURL
);
Console
.
WriteLine
(
e
.
RequestURL
);
//To cancel a request with a custom HTML content
//Filter URL
//if (e.RequestURL.Contains("somewebsite.com"))
//{
// e.Ok("<!DOCTYPE html><html><body><h1>Blocked</h1><p>website blocked.</p></body></html>");
//}
}
}
//Test script injection
//Test script injection
//Insert script to read the Browser URL and send it back to proxy
//Insert script to read the Browser URL and send it back to proxy
public
void
OnResponse
(
object
sender
,
SessionEventArgs
e
)
public
void
OnResponse
(
object
sender
,
SessionEventArgs
e
)
{
{
//To modify a response
//if (e.ServerResponse.StatusCode == HttpStatusCode.OK)
//{
// if (e.ServerResponse.ContentType.Trim().ToLower().Contains("text/html"))
// {
// //Get response body
// string responseHtmlBody = e.GetResponseHtmlBody();
// //Modify e.ServerResponse
// responseHtmlBody = "<html><head></head><body>Response is modified!</body></html>";
// //Set modifed response Html Body
// e.SetResponseHtmlBody(responseHtmlBody);
// }
//}
}
}
...
...
Titanium.Web.Proxy.sln
View file @
9115e03f
...
@@ -56,4 +56,7 @@ Global
...
@@ -56,4 +56,7 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35
EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35
EndGlobalSection
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
EndGlobal
Titanium.Web.Proxy/Helpers/CertificateManager.cs
View file @
9115e03f
...
@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Helpers
private
const
string
CERT_CREATE_FORMAT
=
private
const
string
CERT_CREATE_FORMAT
=
"-ss {0} -n \"CN={1}, O={2}\" -sky {3} -cy {4} -m 120 -a sha256 -eku 1.3.6.1.5.5.7.3.1 -b {5:MM/dd/yyyy} {6}"
;
"-ss {0} -n \"CN={1}, O={2}\" -sky {3} -cy {4} -m 120 -a sha256 -eku 1.3.6.1.5.5.7.3.1 -b {5:MM/dd/yyyy} {6}"
;
private
readonly
IDictionary
<
string
,
X509Certificate2
>
_
certificateCache
;
private
readonly
IDictionary
<
string
,
X509Certificate2
>
certificateCache
;
public
string
Issuer
{
get
;
private
set
;
}
public
string
Issuer
{
get
;
private
set
;
}
public
string
RootCertificateName
{
get
;
private
set
;
}
public
string
RootCertificateName
{
get
;
private
set
;
}
...
@@ -27,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -27,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
MyStore
=
new
X509Store
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
MyStore
=
new
X509Store
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
RootStore
=
new
X509Store
(
StoreName
.
Root
,
StoreLocation
.
CurrentUser
);
RootStore
=
new
X509Store
(
StoreName
.
Root
,
StoreLocation
.
CurrentUser
);
_
certificateCache
=
new
Dictionary
<
string
,
X509Certificate2
>();
certificateCache
=
new
Dictionary
<
string
,
X509Certificate2
>();
}
}
/// <summary>
/// <summary>
...
@@ -69,8 +69,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -69,8 +69,8 @@ namespace Titanium.Web.Proxy.Helpers
}
}
protected
virtual
X509Certificate2
CreateCertificate
(
X509Store
store
,
string
certificateName
)
protected
virtual
X509Certificate2
CreateCertificate
(
X509Store
store
,
string
certificateName
)
{
{
if
(
_
certificateCache
.
ContainsKey
(
certificateName
))
if
(
certificateCache
.
ContainsKey
(
certificateName
))
return
_
certificateCache
[
certificateName
];
return
certificateCache
[
certificateName
];
lock
(
store
)
lock
(
store
)
{
{
...
@@ -102,8 +102,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -102,8 +102,8 @@ namespace Titanium.Web.Proxy.Helpers
finally
finally
{
{
store
.
Close
();
store
.
Close
();
if
(
certificate
!=
null
&&
!
_
certificateCache
.
ContainsKey
(
certificateName
))
if
(
certificate
!=
null
&&
!
certificateCache
.
ContainsKey
(
certificateName
))
_
certificateCache
.
Add
(
certificateName
,
certificate
);
certificateCache
.
Add
(
certificateName
,
certificate
);
}
}
}
}
}
}
...
@@ -151,9 +151,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -151,9 +151,9 @@ namespace Titanium.Web.Proxy.Helpers
{
{
store
.
Close
();
store
.
Close
();
if
(
certificates
==
null
&&
if
(
certificates
==
null
&&
_
certificateCache
.
ContainsKey
(
certificateName
))
certificateCache
.
ContainsKey
(
certificateName
))
{
{
_
certificateCache
.
Remove
(
certificateName
);
certificateCache
.
Remove
(
certificateName
);
}
}
}
}
}
}
...
...
Titanium.Web.Proxy/Helpers/Compression.cs
View file @
9115e03f
...
@@ -3,6 +3,7 @@ using System.Collections.Generic;
...
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using
System.Linq
;
using
System.Linq
;
using
System.Text
;
using
System.Text
;
using
System.IO
;
using
System.IO
;
using
System.Diagnostics.CodeAnalysis
;
namespace
Titanium.Web.Proxy.Helpers
namespace
Titanium.Web.Proxy.Helpers
{
{
...
@@ -32,10 +33,11 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -32,10 +33,11 @@ namespace Titanium.Web.Proxy.Helpers
}
}
public
static
byte
[]
CompressZlib
(
string
ResponseData
,
Encoding
e
)
[
System
.
Diagnostics
.
CodeAnalysis
.
SuppressMessage
(
"Microsoft.Usage"
,
"CA2202:Do not dispose objects multiple times"
)]
public
static
byte
[]
CompressZlib
(
string
responseData
,
Encoding
e
)
{
{
Byte
[]
bytes
=
e
.
GetBytes
(
R
esponseData
);
Byte
[]
bytes
=
e
.
GetBytes
(
r
esponseData
);
using
(
MemoryStream
ms
=
new
MemoryStream
())
using
(
MemoryStream
ms
=
new
MemoryStream
())
{
{
...
@@ -45,17 +47,16 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -45,17 +47,16 @@ namespace Titanium.Web.Proxy.Helpers
zip
.
Write
(
bytes
,
0
,
bytes
.
Length
);
zip
.
Write
(
bytes
,
0
,
bytes
.
Length
);
}
}
return
ms
.
ToArray
();
return
ms
.
ToArray
();
}
}
}
}
public
static
byte
[]
CompressDeflate
(
string
ResponseData
,
Encoding
e
)
[
System
.
Diagnostics
.
CodeAnalysis
.
SuppressMessage
(
"Microsoft.Usage"
,
"CA2202:Do not dispose objects multiple times"
)]
public
static
byte
[]
CompressDeflate
(
string
responseData
,
Encoding
e
)
{
{
Byte
[]
bytes
=
e
.
GetBytes
(
R
esponseData
);
Byte
[]
bytes
=
e
.
GetBytes
(
r
esponseData
);
using
(
MemoryStream
ms
=
new
MemoryStream
())
using
(
MemoryStream
ms
=
new
MemoryStream
())
{
{
...
@@ -70,9 +71,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -70,9 +71,10 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
}
public
static
byte
[]
CompressGzip
(
string
ResponseData
,
Encoding
e
)
[
System
.
Diagnostics
.
CodeAnalysis
.
SuppressMessage
(
"Microsoft.Usage"
,
"CA2202:Do not dispose objects multiple times"
)]
public
static
byte
[]
CompressGzip
(
string
responseData
,
Encoding
e
)
{
{
Byte
[]
bytes
=
e
.
GetBytes
(
R
esponseData
);
Byte
[]
bytes
=
e
.
GetBytes
(
r
esponseData
);
using
(
MemoryStream
ms
=
new
MemoryStream
())
using
(
MemoryStream
ms
=
new
MemoryStream
())
{
{
...
...
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
View file @
9115e03f
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Helpers
public
string
ReadLine
()
public
string
ReadLine
()
{
{
char
[]
buf
=
new
char
[
1
];
char
[]
buf
=
new
char
[
1
];
StringBuilder
_
readBuffer
=
new
StringBuilder
();
StringBuilder
readBuffer
=
new
StringBuilder
();
try
try
{
{
var
charsRead
=
0
;
var
charsRead
=
0
;
...
@@ -29,24 +29,24 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -29,24 +29,24 @@ namespace Titanium.Web.Proxy.Helpers
{
{
if
(
lastChar
==
'\r'
&&
buf
[
0
]
==
'\n'
)
if
(
lastChar
==
'\r'
&&
buf
[
0
]
==
'\n'
)
{
{
return
_readBuffer
.
Remove
(
_
readBuffer
.
Length
-
1
,
1
).
ToString
();
return
readBuffer
.
Remove
(
readBuffer
.
Length
-
1
,
1
).
ToString
();
}
}
else
else
if
(
buf
[
0
]
==
'\0'
)
if
(
buf
[
0
]
==
'\0'
)
{
{
return
_
readBuffer
.
ToString
();
return
readBuffer
.
ToString
();
}
}
else
else
_
readBuffer
.
Append
(
buf
[
0
]);
readBuffer
.
Append
(
buf
[
0
]);
lastChar
=
buf
[
0
];
lastChar
=
buf
[
0
];
}
}
return
_
readBuffer
.
ToString
();
return
readBuffer
.
ToString
();
}
}
catch
(
IOException
)
catch
(
IOException
)
{
return
_
readBuffer
.
ToString
();
}
{
return
readBuffer
.
ToString
();
}
catch
(
Exception
e
)
catch
(
Exception
)
{
throw
e
;
}
{
throw
;
}
}
}
...
...
Titanium.Web.Proxy/Helpers/NetFramework.cs
View file @
9115e03f
...
@@ -8,6 +8,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -8,6 +8,7 @@ namespace Titanium.Web.Proxy.Helpers
{
{
public
class
NetFrameworkHelper
public
class
NetFrameworkHelper
{
{
//Fix bug in .Net 4.0 HttpWebRequest (don't use this for 4.5 and above)
//http://stackoverflow.com/questions/856885/httpwebrequest-to-url-with-dot-at-the-end
//http://stackoverflow.com/questions/856885/httpwebrequest-to-url-with-dot-at-the-end
public
static
void
URLPeriodFix
()
public
static
void
URLPeriodFix
()
{
{
...
...
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
9115e03f
...
@@ -7,10 +7,14 @@ using System.Runtime.InteropServices;
...
@@ -7,10 +7,14 @@ using System.Runtime.InteropServices;
namespace
Titanium.Web.Proxy.Helpers
namespace
Titanium.Web.Proxy.Helpers
{
{
public
static
class
SystemProxyHelper
internal
static
class
NativeMethods
{
{
[
DllImport
(
"wininet.dll"
)]
[
DllImport
(
"wininet.dll"
)]
public
static
extern
bool
InternetSetOption
(
IntPtr
hInternet
,
int
dwOption
,
IntPtr
lpBuffer
,
int
dwBufferLength
);
internal
static
extern
bool
InternetSetOption
(
IntPtr
hInternet
,
int
dwOption
,
IntPtr
lpBuffer
,
int
dwBufferLength
);
}
public
static
class
SystemProxyHelper
{
public
const
int
INTERNET_OPTION_SETTINGS_CHANGED
=
39
;
public
const
int
INTERNET_OPTION_SETTINGS_CHANGED
=
39
;
public
const
int
INTERNET_OPTION_REFRESH
=
37
;
public
const
int
INTERNET_OPTION_REFRESH
=
37
;
static
bool
settingsReturn
,
refreshReturn
;
static
bool
settingsReturn
,
refreshReturn
;
...
@@ -23,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -23,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
prevProxyEnable
=
reg
.
GetValue
(
"ProxyEnable"
);
prevProxyEnable
=
reg
.
GetValue
(
"ProxyEnable"
);
prevProxyServer
=
reg
.
GetValue
(
"ProxyServer"
);
prevProxyServer
=
reg
.
GetValue
(
"ProxyServer"
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
"http="
+
hostname
+
":"
+
port
+
";"
);
reg
.
SetValue
(
"ProxyServer"
,
"http="
+
hostname
+
":"
+
port
+
";"
);
refresh
();
refresh
();
}
}
public
static
void
EnableProxyHTTPS
(
string
hostname
,
int
port
)
public
static
void
EnableProxyHTTPS
(
string
hostname
,
int
port
)
...
@@ -43,8 +47,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -43,8 +47,8 @@ namespace Titanium.Web.Proxy.Helpers
}
}
private
static
void
refresh
()
private
static
void
refresh
()
{
{
settingsReturn
=
InternetSetOption
(
IntPtr
.
Zero
,
INTERNET_OPTION_SETTINGS_CHANGED
,
IntPtr
.
Zero
,
0
);
settingsReturn
=
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
INTERNET_OPTION_SETTINGS_CHANGED
,
IntPtr
.
Zero
,
0
);
refreshReturn
=
InternetSetOption
(
IntPtr
.
Zero
,
INTERNET_OPTION_REFRESH
,
IntPtr
.
Zero
,
0
);
refreshReturn
=
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
INTERNET_OPTION_REFRESH
,
IntPtr
.
Zero
,
0
);
}
}
}
}
}
}
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
9115e03f
...
@@ -13,16 +13,16 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -13,16 +13,16 @@ namespace Titanium.Web.Proxy.Helpers
{
{
private
static
readonly
int
BUFFER_SIZE
=
8192
;
private
static
readonly
int
BUFFER_SIZE
=
8192
;
private
static
readonly
String
[]
colonSpaceSplit
=
new
string
[]
{
": "
};
private
static
readonly
String
[]
colonSpaceSplit
=
new
string
[]
{
": "
};
public
static
void
SendRaw
(
string
Hostname
,
int
TunnelPort
,
System
.
IO
.
Stream
C
lientStream
)
public
static
void
SendRaw
(
string
hostname
,
int
tunnelPort
,
System
.
IO
.
Stream
c
lientStream
)
{
{
System
.
Net
.
Sockets
.
TcpClient
tunnelClient
=
new
System
.
Net
.
Sockets
.
TcpClient
(
Hostname
,
T
unnelPort
);
System
.
Net
.
Sockets
.
TcpClient
tunnelClient
=
new
System
.
Net
.
Sockets
.
TcpClient
(
hostname
,
t
unnelPort
);
var
tunnelStream
=
tunnelClient
.
GetStream
();
var
tunnelStream
=
tunnelClient
.
GetStream
();
var
tunnelReadBuffer
=
new
byte
[
BUFFER_SIZE
];
var
tunnelReadBuffer
=
new
byte
[
BUFFER_SIZE
];
Task
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
C
lientStream
,
tunnelStream
,
BUFFER_SIZE
));
Task
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
c
lientStream
,
tunnelStream
,
BUFFER_SIZE
));
Task
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
C
lientStream
,
BUFFER_SIZE
));
Task
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
c
lientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
sendRelay
.
Start
();
receiveRelay
.
Start
();
receiveRelay
.
Start
();
...
@@ -35,21 +35,21 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -35,21 +35,21 @@ namespace Titanium.Web.Proxy.Helpers
if
(
tunnelClient
!=
null
)
if
(
tunnelClient
!=
null
)
tunnelClient
.
Close
();
tunnelClient
.
Close
();
}
}
public
static
void
SendRaw
(
string
HttpCmd
,
string
SecureHostName
,
ref
List
<
string
>
RequestLines
,
bool
IsHttps
,
Stream
C
lientStream
)
public
static
void
SendRaw
(
string
httpCmd
,
string
secureHostName
,
List
<
string
>
requestLines
,
bool
isHttps
,
Stream
c
lientStream
)
{
{
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
Append
(
H
ttpCmd
);
sb
.
Append
(
h
ttpCmd
);
sb
.
Append
(
Environment
.
NewLine
);
sb
.
Append
(
Environment
.
NewLine
);
string
hostname
=
S
ecureHostName
;
string
hostname
=
s
ecureHostName
;
for
(
int
i
=
1
;
i
<
R
equestLines
.
Count
;
i
++)
for
(
int
i
=
1
;
i
<
r
equestLines
.
Count
;
i
++)
{
{
var
header
=
R
equestLines
[
i
];
var
header
=
r
equestLines
[
i
];
if
(
S
ecureHostName
==
null
)
if
(
s
ecureHostName
==
null
)
{
{
String
[]
headerParsed
=
H
ttpCmd
.
Split
(
colonSpaceSplit
,
2
,
StringSplitOptions
.
None
);
String
[]
headerParsed
=
h
ttpCmd
.
Split
(
colonSpaceSplit
,
2
,
StringSplitOptions
.
None
);
switch
(
headerParsed
[
0
].
ToLower
())
switch
(
headerParsed
[
0
].
ToLower
())
{
{
case
"host"
:
case
"host"
:
...
@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.Helpers
sb
.
Append
(
Environment
.
NewLine
);
sb
.
Append
(
Environment
.
NewLine
);
int
tunnelPort
=
80
;
int
tunnelPort
=
80
;
if
(
I
sHttps
)
if
(
i
sHttps
)
{
{
tunnelPort
=
443
;
tunnelPort
=
443
;
...
@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.Helpers
System
.
Net
.
Sockets
.
TcpClient
tunnelClient
=
new
System
.
Net
.
Sockets
.
TcpClient
(
hostname
,
tunnelPort
);
System
.
Net
.
Sockets
.
TcpClient
tunnelClient
=
new
System
.
Net
.
Sockets
.
TcpClient
(
hostname
,
tunnelPort
);
var
tunnelStream
=
tunnelClient
.
GetStream
()
as
System
.
IO
.
Stream
;
var
tunnelStream
=
tunnelClient
.
GetStream
()
as
System
.
IO
.
Stream
;
if
(
I
sHttps
)
if
(
i
sHttps
)
{
{
var
sslStream
=
new
SslStream
(
tunnelStream
);
var
sslStream
=
new
SslStream
(
tunnelStream
);
sslStream
.
AuthenticateAsClient
(
hostname
);
sslStream
.
AuthenticateAsClient
(
hostname
);
...
@@ -87,8 +87,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -87,8 +87,8 @@ namespace Titanium.Web.Proxy.Helpers
}
}
var
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
sb
.
ToString
(),
C
lientStream
,
tunnelStream
,
BUFFER_SIZE
));
var
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
sb
.
ToString
(),
c
lientStream
,
tunnelStream
,
BUFFER_SIZE
));
var
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
C
lientStream
,
BUFFER_SIZE
));
var
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
c
lientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
sendRelay
.
Start
();
receiveRelay
.
Start
();
receiveRelay
.
Start
();
...
...
Titanium.Web.Proxy/Models/SessionEventArgs.cs
View file @
9115e03f
...
@@ -3,48 +3,73 @@ using System.Text;
...
@@ -3,48 +3,73 @@ using System.Text;
using
System.IO
;
using
System.IO
;
using
System.Net
;
using
System.Net
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
System.Net.Sockets
;
namespace
Titanium.Web.Proxy.Models
namespace
Titanium.Web.Proxy.Models
{
{
public
class
SessionEventArgs
:
EventArgs
public
class
SessionEventArgs
:
EventArgs
,
IDisposable
{
{
public
string
RequestURL
{
get
;
set
;
}
internal
int
BUFFER_SIZE
;
public
string
RequestHostname
{
get
;
set
;
}
internal
Stream
ClientStream
{
get
;
set
;
}
internal
CustomBinaryReader
ClientStreamReader
{
get
;
set
;
}
internal
StreamWriter
ClientStreamWriter
{
get
;
set
;
}
internal
string
UpgradeProtocol
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
int
RequestLength
{
get
;
set
;
}
internal
Version
RequestHttpVersion
{
get
;
set
;
}
internal
bool
RequestIsAlive
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
string
RequestHtmlBody
{
get
;
set
;
}
internal
bool
RequestWasModified
{
get
;
set
;
}
internal
Stream
ServerResponseStream
{
get
;
set
;
}
internal
string
ResponseHtmlBody
{
get
;
set
;
}
internal
bool
ResponseWasModified
{
get
;
set
;
}
public
bool
IsSSLRequest
{
get
;
set
;
}
public
TcpClient
Client
{
get
;
set
;
}
public
int
ClientPort
{
get
;
set
;
}
public
int
ClientPort
{
get
;
set
;
}
public
IPAddress
ClientIpAddress
{
get
;
set
;
}
public
IPAddress
ClientIpAddress
{
get
;
set
;
}
public
string
tunnelHostName
{
get
;
set
;
}
public
string
securehost
{
get
;
set
;
}
public
bool
IsSSLRequest
{
get
;
set
;
}
public
string
RequestURL
{
get
;
set
;
}
public
string
RequestHostname
{
get
;
set
;
}
public
HttpWebRequest
ProxyRequest
{
get
;
set
;
}
public
HttpWebRequest
ProxyRequest
{
get
;
set
;
}
public
HttpWebResponse
ServerResponse
{
get
;
set
;
}
public
HttpWebResponse
ServerResponse
{
get
;
set
;
}
public
void
Dispose
()
{
if
(
this
.
ProxyRequest
!=
null
)
this
.
ProxyRequest
.
Abort
();
internal
int
BUFFER_SIZE
;
if
(
this
.
ServerResponseStream
!=
null
)
this
.
ServerResponseStream
.
Dispose
();
internal
int
RequestLength
{
get
;
set
;
}
if
(
this
.
ServerResponse
!=
null
)
internal
Version
RequestHttpVersion
{
get
;
set
;
}
this
.
ServerResponse
.
Close
();
internal
bool
RequestIsAlive
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
CustomBinaryReader
ClientStreamReader
{
get
;
set
;
}
internal
Stream
ClientStream
{
get
;
set
;
}
internal
Stream
ServerResponseStream
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
bool
RequestWasModified
{
get
;
set
;
}
internal
bool
ResponseWasModified
{
get
;
set
;
}
internal
string
UpgradeProtocol
{
get
;
set
;
}
if
(
this
.
ClientStreamReader
!=
null
)
this
.
ClientStreamReader
.
Dispose
();
if
(
this
.
ClientStreamWriter
!=
null
)
this
.
ClientStreamWriter
.
Dispose
();
internal
string
RequestHtmlBody
{
get
;
set
;
}
if
(
this
.
ClientStream
!=
null
)
internal
string
ResponseHtmlBody
{
get
;
set
;
}
this
.
ClientStream
.
Dispose
();
if
(
this
.
Client
!=
null
)
this
.
Client
.
Close
();
}
public
SessionEventArgs
(
int
B
ufferSize
)
public
SessionEventArgs
(
int
b
ufferSize
)
{
{
BUFFER_SIZE
=
B
ufferSize
;
BUFFER_SIZE
=
b
ufferSize
;
}
}
public
string
GetRequestHtmlBody
()
public
string
GetRequestHtmlBody
()
{
{
...
@@ -68,9 +93,9 @@ namespace Titanium.Web.Proxy.Models
...
@@ -68,9 +93,9 @@ namespace Titanium.Web.Proxy.Models
RequestWasModified
=
true
;
RequestWasModified
=
true
;
return
RequestHtmlBody
;
return
RequestHtmlBody
;
}
}
public
void
SetRequestHtmlBody
(
string
B
ody
)
public
void
SetRequestHtmlBody
(
string
b
ody
)
{
{
this
.
RequestHtmlBody
=
B
ody
;
this
.
RequestHtmlBody
=
b
ody
;
RequestWasModified
=
true
;
RequestWasModified
=
true
;
}
}
public
string
GetResponseHtmlBody
()
public
string
GetResponseHtmlBody
()
...
@@ -106,25 +131,25 @@ namespace Titanium.Web.Proxy.Models
...
@@ -106,25 +131,25 @@ namespace Titanium.Web.Proxy.Models
return
ResponseHtmlBody
;
return
ResponseHtmlBody
;
}
}
public
void
SetResponseHtmlBody
(
string
B
ody
)
public
void
SetResponseHtmlBody
(
string
b
ody
)
{
{
this
.
ResponseHtmlBody
=
B
ody
;
this
.
ResponseHtmlBody
=
b
ody
;
}
}
//stream reader not recomended for images
//stream reader not recomended for images
private
string
DecodeData
(
Stream
R
esponseStream
,
Encoding
e
)
private
string
DecodeData
(
Stream
r
esponseStream
,
Encoding
e
)
{
{
StreamReader
reader
=
new
StreamReader
(
R
esponseStream
,
e
);
StreamReader
reader
=
new
StreamReader
(
r
esponseStream
,
e
);
return
reader
.
ReadToEnd
();
return
reader
.
ReadToEnd
();
}
}
public
void
Ok
(
string
H
tml
)
public
void
Ok
(
string
h
tml
)
{
{
if
(
H
tml
==
null
)
if
(
h
tml
==
null
)
H
tml
=
string
.
Empty
;
h
tml
=
string
.
Empty
;
var
result
=
Encoding
.
Default
.
GetBytes
(
H
tml
);
var
result
=
Encoding
.
Default
.
GetBytes
(
h
tml
);
StreamWriter
connectStreamWriter
=
new
StreamWriter
(
ClientStream
);
StreamWriter
connectStreamWriter
=
new
StreamWriter
(
ClientStream
);
var
s
=
String
.
Format
(
"HTTP/{0}.{1} {2} {3}"
,
RequestHttpVersion
.
Major
,
RequestHttpVersion
.
Minor
,
200
,
"Ok"
);
var
s
=
String
.
Format
(
"HTTP/{0}.{1} {2} {3}"
,
RequestHttpVersion
.
Major
,
RequestHttpVersion
.
Minor
,
200
,
"Ok"
);
...
@@ -153,16 +178,6 @@ namespace Titanium.Web.Proxy.Models
...
@@ -153,16 +178,6 @@ namespace Titanium.Web.Proxy.Models
}
}
public
System
.
Net
.
Sockets
.
TcpClient
Client
{
get
;
set
;
}
public
string
tunnelHostName
{
get
;
set
;
}
public
string
securehost
{
get
;
set
;
}
}
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/Properties/AssemblyInfo.cs
0 → 100644
View file @
9115e03f
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.InteropServices
;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Titanium.Web.Proxy.Properties")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Titanium.Web.Proxy.Properties")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5036e0b7-a0d0-4070-8eb0-72c129dee9b3")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Titanium.Web.Proxy/ProxyServer.cs
View file @
9115e03f
...
@@ -38,6 +38,8 @@ namespace Titanium.Web.Proxy
...
@@ -38,6 +38,8 @@ namespace Titanium.Web.Proxy
private
static
TcpListener
listener
;
private
static
TcpListener
listener
;
private
static
Thread
listenerThread
;
private
static
Thread
listenerThread
;
private
static
bool
ShouldListen
{
get
;
set
;
}
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
...
@@ -64,6 +66,7 @@ namespace Titanium.Web.Proxy
...
@@ -64,6 +66,7 @@ namespace Titanium.Web.Proxy
CertManager
=
new
CertificateManager
(
"Titanium"
,
CertManager
=
new
CertificateManager
(
"Titanium"
,
"Titanium Root Certificate Authority"
);
"Titanium Root Certificate Authority"
);
}
}
public
ProxyServer
()
public
ProxyServer
()
{
{
...
@@ -87,7 +90,26 @@ namespace Titanium.Web.Proxy
...
@@ -87,7 +90,26 @@ namespace Titanium.Web.Proxy
}
}
private
static
bool
ShouldListen
{
get
;
set
;
}
private
static
void
Listen
(
Object
obj
)
{
TcpListener
listener
=
(
TcpListener
)
obj
;
try
{
while
(
ShouldListen
)
{
var
client
=
listener
.
AcceptTcpClient
();
Task
.
Factory
.
StartNew
(()
=>
HandleClient
(
client
));
}
}
catch
(
ThreadInterruptedException
)
{
}
catch
(
SocketException
)
{
}
}
public
static
bool
Start
()
public
static
bool
Start
()
{
{
...
@@ -138,25 +160,7 @@ namespace Titanium.Web.Proxy
...
@@ -138,25 +160,7 @@ namespace Titanium.Web.Proxy
}
}
private
static
void
Listen
(
Object
obj
)
{
TcpListener
listener
=
(
TcpListener
)
obj
;
try
{
while
(
ShouldListen
)
{
var
client
=
listener
.
AcceptTcpClient
();
Task
.
Factory
.
StartNew
(()
=>
HandleClient
(
client
));
}
}
catch
(
ThreadInterruptedException
)
{
}
catch
(
SocketException
)
{
}
}
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
9115e03f
...
@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy
...
@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy
partial
class
ProxyServer
partial
class
ProxyServer
{
{
private
static
void
HandleClient
(
TcpClient
C
lient
)
private
static
void
HandleClient
(
TcpClient
c
lient
)
{
{
...
@@ -36,7 +36,7 @@ namespace Titanium.Web.Proxy
...
@@ -36,7 +36,7 @@ namespace Titanium.Web.Proxy
{
{
clientStream
=
C
lient
.
GetStream
();
clientStream
=
c
lient
.
GetStream
();
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
Encoding
.
ASCII
);
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
Encoding
.
ASCII
);
string
securehost
=
null
;
string
securehost
=
null
;
...
@@ -138,7 +138,7 @@ namespace Titanium.Web.Proxy
...
@@ -138,7 +138,7 @@ namespace Titanium.Web.Proxy
sslStream
.
AuthenticateAsServer
(
_certificate
,
false
,
SslProtocols
.
Tls
|
SslProtocols
.
Ssl3
|
SslProtocols
.
Ssl2
,
false
);
sslStream
.
AuthenticateAsServer
(
_certificate
,
false
,
SslProtocols
.
Tls
|
SslProtocols
.
Ssl3
|
SslProtocols
.
Ssl2
,
false
);
}
}
catch
(
AuthenticationException
ex
)
catch
(
AuthenticationException
)
{
{
//if authentication failed it could be because client uses pinned certificates
//if authentication failed it could be because client uses pinned certificates
//So add the hostname to this list so that next time we can relay without touching it (tunnel the request)
//So add the hostname to this list so that next time we can relay without touching it (tunnel the request)
...
@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy
...
@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy
{
{
pinnedCertificateClients
.
Add
(
tunnelHostName
);
pinnedCertificateClients
.
Add
(
tunnelHostName
);
}
}
throw
ex
;
throw
;
}
}
}
}
...
@@ -180,7 +180,7 @@ namespace Titanium.Web.Proxy
...
@@ -180,7 +180,7 @@ namespace Titanium.Web.Proxy
}
}
//Now create the request
//Now create the request
HandleHttpSessionRequest
(
Client
,
httpCmd
,
clientStream
,
tunnelHostName
,
requestLines
,
clientStreamReader
,
securehost
);
Task
.
Factory
.
StartNew
(()=>
HandleHttpSessionRequest
(
client
,
httpCmd
,
clientStream
,
tunnelHostName
,
requestLines
,
clientStreamReader
,
securehost
)
);
...
@@ -211,7 +211,7 @@ namespace Titanium.Web.Proxy
...
@@ -211,7 +211,7 @@ namespace Titanium.Web.Proxy
}
}
}
}
private
static
void
HandleHttpSessionRequest
(
TcpClient
C
lient
,
string
httpCmd
,
Stream
clientStream
,
string
tunnelHostName
,
List
<
string
>
requestLines
,
CustomBinaryReader
clientStreamReader
,
string
securehost
)
private
static
void
HandleHttpSessionRequest
(
TcpClient
c
lient
,
string
httpCmd
,
Stream
clientStream
,
string
tunnelHostName
,
List
<
string
>
requestLines
,
CustomBinaryReader
clientStreamReader
,
string
securehost
)
{
{
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy
var
args
=
new
SessionEventArgs
(
BUFFER_SIZE
);
var
args
=
new
SessionEventArgs
(
BUFFER_SIZE
);
args
.
Client
=
C
lient
;
args
.
Client
=
c
lient
;
args
.
tunnelHostName
=
tunnelHostName
;
args
.
tunnelHostName
=
tunnelHostName
;
args
.
securehost
=
securehost
;
args
.
securehost
=
securehost
;
try
try
...
@@ -229,7 +229,7 @@ namespace Titanium.Web.Proxy
...
@@ -229,7 +229,7 @@ namespace Titanium.Web.Proxy
if
(
splitBuffer
.
Length
!=
3
)
if
(
splitBuffer
.
Length
!=
3
)
{
{
TcpHelper
.
SendRaw
(
httpCmd
,
tunnelHostName
,
re
f
re
questLines
,
args
.
IsSSLRequest
,
clientStreamReader
.
BaseStream
);
TcpHelper
.
SendRaw
(
httpCmd
,
tunnelHostName
,
requestLines
,
args
.
IsSSLRequest
,
clientStreamReader
.
BaseStream
);
if
(
clientStream
!=
null
)
if
(
clientStream
!=
null
)
clientStream
.
Close
();
clientStream
.
Close
();
...
@@ -272,7 +272,7 @@ namespace Titanium.Web.Proxy
...
@@ -272,7 +272,7 @@ namespace Titanium.Web.Proxy
if
((
header
[
0
]
==
"upgrade"
)
&&
(
header
[
1
]
==
"websocket"
))
if
((
header
[
0
]
==
"upgrade"
)
&&
(
header
[
1
]
==
"websocket"
))
{
{
TcpHelper
.
SendRaw
(
httpCmd
,
tunnelHostName
,
re
f
re
questLines
,
args
.
IsSSLRequest
,
clientStreamReader
.
BaseStream
);
TcpHelper
.
SendRaw
(
httpCmd
,
tunnelHostName
,
requestLines
,
args
.
IsSSLRequest
,
clientStreamReader
.
BaseStream
);
if
(
clientStream
!=
null
)
if
(
clientStream
!=
null
)
clientStream
.
Close
();
clientStream
.
Close
();
...
@@ -281,7 +281,7 @@ namespace Titanium.Web.Proxy
...
@@ -281,7 +281,7 @@ namespace Titanium.Web.Proxy
}
}
}
}
ReadRequestHeaders
(
ref
requestLines
,
args
.
ProxyRequest
);
SetClientRequestHeaders
(
requestLines
,
args
.
ProxyRequest
);
int
contentLen
=
(
int
)
args
.
ProxyRequest
.
ContentLength
;
int
contentLen
=
(
int
)
args
.
ProxyRequest
.
ContentLength
;
...
@@ -298,8 +298,8 @@ namespace Titanium.Web.Proxy
...
@@ -298,8 +298,8 @@ namespace Titanium.Web.Proxy
args
.
RequestLength
=
contentLen
;
args
.
RequestLength
=
contentLen
;
args
.
RequestHttpVersion
=
version
;
args
.
RequestHttpVersion
=
version
;
args
.
ClientPort
=
((
IPEndPoint
)
C
lient
.
Client
.
RemoteEndPoint
).
Port
;
args
.
ClientPort
=
((
IPEndPoint
)
c
lient
.
Client
.
RemoteEndPoint
).
Port
;
args
.
ClientIpAddress
=
((
IPEndPoint
)
C
lient
.
Client
.
RemoteEndPoint
).
Address
;
args
.
ClientIpAddress
=
((
IPEndPoint
)
c
lient
.
Client
.
RemoteEndPoint
).
Address
;
args
.
RequestIsAlive
=
args
.
ProxyRequest
.
KeepAlive
;
args
.
RequestIsAlive
=
args
.
ProxyRequest
.
KeepAlive
;
BeforeRequest
(
null
,
args
);
BeforeRequest
(
null
,
args
);
...
@@ -342,29 +342,35 @@ namespace Titanium.Web.Proxy
...
@@ -342,29 +342,35 @@ namespace Titanium.Web.Proxy
//If its a post/put request, then read the client html body and send it to server
//If its a post/put request, then read the client html body and send it to server
if
(
method
.
ToUpper
()
==
"POST"
||
method
.
ToUpper
()
==
"PUT"
)
if
(
method
.
ToUpper
()
==
"POST"
||
method
.
ToUpper
()
==
"PUT"
)
{
{
args
.
ProxyRequest
.
BeginGetRequestStream
(
new
AsyncCallback
(
GetRequestStreamCallback
),
args
);
SendClientRequestBody
(
args
);
//Http request body sent, now wait asynchronously for response
args
.
ProxyRequest
.
BeginGetResponse
(
new
AsyncCallback
(
HandleHttpSessionResponse
),
args
);
}
}
else
else
{
{
//otherwise wait for response asynchronously
//otherwise wait for response asynchronously
args
.
ProxyRequest
.
BeginGetResponse
(
new
AsyncCallback
(
HandleHttpSessionResponse
),
args
);
args
.
ProxyRequest
.
BeginGetResponse
(
new
AsyncCallback
(
HandleHttpSessionResponse
),
args
);
}
//Now read the next request (if keep-Alive is enabled, otherwise exit thu
s thread)
//Now read the next request (if keep-Alive is enabled, otherwise exit thi
s thread)
//If client is pipeling the request, this will be immediately hit before response for previous request was made
//If client is pipeling the request, this will be immediately hit before response for previous request was made
if
(
args
.
ProxyRequest
.
KeepAlive
)
if
(
args
.
ProxyRequest
.
KeepAlive
)
{
{
requestLines
=
new
List
<
string
>()
;
tmpLine
=
null
;
requestLines
.
Clear
();
requestLines
.
Clear
();
while
(!
String
.
IsNullOrEmpty
(
tmpLine
=
args
.
ClientStreamReader
.
ReadLine
()))
while
(!
String
.
IsNullOrEmpty
(
tmpLine
=
args
.
ClientStreamReader
.
ReadLine
()))
{
{
requestLines
.
Add
(
tmpLine
);
requestLines
.
Add
(
tmpLine
);
}
}
httpCmd
=
requestLines
.
Count
()
>
0
?
requestLines
[
0
]
:
null
;
httpCmd
=
requestLines
.
Count
()
>
0
?
requestLines
[
0
]
:
null
;
HandleHttpSessionRequest
(
Client
,
httpCmd
,
args
.
ClientStream
,
args
.
tunnelHostName
,
requestLines
,
args
.
ClientStreamReader
,
args
.
securehost
);
TcpClient
Client
=
args
.
Client
;
}
}
//Http request body sent, now wait for next request
Task
.
Factory
.
StartNew
(()
=>
HandleHttpSessionRequest
(
Client
,
httpCmd
,
args
.
ClientStream
,
args
.
tunnelHostName
,
requestLines
,
args
.
ClientStreamReader
,
args
.
securehost
));
}
}
}
}
}
...
@@ -387,13 +393,13 @@ namespace Titanium.Web.Proxy
...
@@ -387,13 +393,13 @@ namespace Titanium.Web.Proxy
}
}
private
static
void
ReadRequestHeaders
(
ref
List
<
string
>
RequestLines
,
HttpWebRequest
W
ebRequest
)
private
static
void
SetClientRequestHeaders
(
List
<
string
>
requestLines
,
HttpWebRequest
w
ebRequest
)
{
{
for
(
int
i
=
1
;
i
<
R
equestLines
.
Count
;
i
++)
for
(
int
i
=
1
;
i
<
r
equestLines
.
Count
;
i
++)
{
{
String
httpCmd
=
R
equestLines
[
i
];
String
httpCmd
=
r
equestLines
[
i
];
String
[]
header
=
httpCmd
.
Split
(
colonSpaceSplit
,
2
,
StringSplitOptions
.
None
);
String
[]
header
=
httpCmd
.
Split
(
colonSpaceSplit
,
2
,
StringSplitOptions
.
None
);
...
@@ -401,75 +407,75 @@ namespace Titanium.Web.Proxy
...
@@ -401,75 +407,75 @@ namespace Titanium.Web.Proxy
switch
(
header
[
0
].
ToLower
())
switch
(
header
[
0
].
ToLower
())
{
{
case
"accept"
:
case
"accept"
:
W
ebRequest
.
Accept
=
header
[
1
];
w
ebRequest
.
Accept
=
header
[
1
];
break
;
break
;
case
"accept-encoding"
:
case
"accept-encoding"
:
W
ebRequest
.
Headers
.
Add
(
header
[
0
],
"gzip,deflate,zlib"
);
w
ebRequest
.
Headers
.
Add
(
header
[
0
],
"gzip,deflate,zlib"
);
break
;
break
;
case
"cookie"
:
case
"cookie"
:
W
ebRequest
.
Headers
[
"Cookie"
]
=
header
[
1
];
w
ebRequest
.
Headers
[
"Cookie"
]
=
header
[
1
];
break
;
break
;
case
"connection"
:
case
"connection"
:
if
(
header
[
1
].
ToLower
()
==
"keep-alive"
)
if
(
header
[
1
].
ToLower
()
==
"keep-alive"
)
W
ebRequest
.
KeepAlive
=
true
;
w
ebRequest
.
KeepAlive
=
true
;
break
;
break
;
case
"content-length"
:
case
"content-length"
:
int
contentLen
;
int
contentLen
;
int
.
TryParse
(
header
[
1
],
out
contentLen
);
int
.
TryParse
(
header
[
1
],
out
contentLen
);
if
(
contentLen
!=
0
)
if
(
contentLen
!=
0
)
W
ebRequest
.
ContentLength
=
contentLen
;
w
ebRequest
.
ContentLength
=
contentLen
;
break
;
break
;
case
"content-type"
:
case
"content-type"
:
W
ebRequest
.
ContentType
=
header
[
1
];
w
ebRequest
.
ContentType
=
header
[
1
];
break
;
break
;
case
"expect"
:
case
"expect"
:
if
(
header
[
1
].
ToLower
()
==
"100-continue"
)
if
(
header
[
1
].
ToLower
()
==
"100-continue"
)
W
ebRequest
.
ServicePoint
.
Expect100Continue
=
true
;
w
ebRequest
.
ServicePoint
.
Expect100Continue
=
true
;
else
else
W
ebRequest
.
Expect
=
header
[
1
];
w
ebRequest
.
Expect
=
header
[
1
];
break
;
break
;
case
"host"
:
case
"host"
:
W
ebRequest
.
Host
=
header
[
1
];
w
ebRequest
.
Host
=
header
[
1
];
break
;
break
;
case
"if-modified-since"
:
case
"if-modified-since"
:
String
[]
sb
=
header
[
1
].
Trim
().
Split
(
semiSplit
);
String
[]
sb
=
header
[
1
].
Trim
().
Split
(
semiSplit
);
DateTime
d
;
DateTime
d
;
if
(
DateTime
.
TryParse
(
sb
[
0
],
out
d
))
if
(
DateTime
.
TryParse
(
sb
[
0
],
out
d
))
W
ebRequest
.
IfModifiedSince
=
d
;
w
ebRequest
.
IfModifiedSince
=
d
;
break
;
break
;
case
"proxy-connection"
:
case
"proxy-connection"
:
if
(
header
[
1
].
ToLower
()
==
"keep-alive"
)
if
(
header
[
1
].
ToLower
()
==
"keep-alive"
)
W
ebRequest
.
KeepAlive
=
true
;
w
ebRequest
.
KeepAlive
=
true
;
break
;
break
;
case
"range"
:
case
"range"
:
var
startEnd
=
header
[
1
].
Replace
(
Environment
.
NewLine
,
""
).
Remove
(
0
,
6
).
Split
(
'-'
);
var
startEnd
=
header
[
1
].
Replace
(
Environment
.
NewLine
,
""
).
Remove
(
0
,
6
).
Split
(
'-'
);
if
(
startEnd
.
Length
>
1
)
{
if
(!
String
.
IsNullOrEmpty
(
startEnd
[
1
]))
WebRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]),
int
.
Parse
(
startEnd
[
1
]));
else
W
ebRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]));
}
if
(
startEnd
.
Length
>
1
)
{
if
(!
String
.
IsNullOrEmpty
(
startEnd
[
1
]))
webRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]),
int
.
Parse
(
startEnd
[
1
]));
else
w
ebRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]));
}
else
else
W
ebRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]));
w
ebRequest
.
AddRange
(
int
.
Parse
(
startEnd
[
0
]));
break
;
break
;
case
"referer"
:
case
"referer"
:
W
ebRequest
.
Referer
=
header
[
1
];
w
ebRequest
.
Referer
=
header
[
1
];
break
;
break
;
case
"user-agent"
:
case
"user-agent"
:
W
ebRequest
.
UserAgent
=
header
[
1
];
w
ebRequest
.
UserAgent
=
header
[
1
];
break
;
break
;
//revisit this, transfer-encoding is not a request header according to spec
//revisit this, transfer-encoding is not a request header according to spec
//But how to identify if client is sending chunked body for PUT/POST?
//But how to identify if client is sending chunked body for PUT/POST?
case
"transfer-encoding"
:
case
"transfer-encoding"
:
if
(
header
[
1
].
ToLower
()
==
"chunked"
)
if
(
header
[
1
].
ToLower
()
==
"chunked"
)
W
ebRequest
.
SendChunked
=
true
;
w
ebRequest
.
SendChunked
=
true
;
else
else
W
ebRequest
.
SendChunked
=
false
;
w
ebRequest
.
SendChunked
=
false
;
break
;
break
;
case
"upgrade"
:
case
"upgrade"
:
if
(
header
[
1
].
ToLower
()
==
"http/1.1"
)
if
(
header
[
1
].
ToLower
()
==
"http/1.1"
)
W
ebRequest
.
Headers
.
Add
(
header
[
0
],
header
[
1
]);
w
ebRequest
.
Headers
.
Add
(
header
[
0
],
header
[
1
]);
break
;
break
;
default
:
default
:
if
(
header
.
Length
>=
2
)
if
(
header
.
Length
>=
2
)
W
ebRequest
.
Headers
.
Add
(
header
[
0
],
header
[
1
]);
w
ebRequest
.
Headers
.
Add
(
header
[
0
],
header
[
1
]);
break
;
break
;
}
}
...
@@ -480,12 +486,12 @@ namespace Titanium.Web.Proxy
...
@@ -480,12 +486,12 @@ namespace Titanium.Web.Proxy
}
}
//This is called when the request is PUT/POST to read the body
//This is called when the request is PUT/POST to read the body
private
static
void
GetRequestStreamCallback
(
IAsyncResult
AsynchronousResult
)
private
static
void
SendClientRequestBody
(
SessionEventArgs
args
)
{
{
var
args
=
(
SessionEventArgs
)
AsynchronousResult
.
AsyncState
;
// End the operation
// End the operation
Stream
postStream
=
args
.
ProxyRequest
.
EndGetRequestStream
(
AsynchronousResult
);
Stream
postStream
=
args
.
ProxyRequest
.
GetRequestStream
(
);
if
(
args
.
ProxyRequest
.
ContentLength
>
0
)
if
(
args
.
ProxyRequest
.
ContentLength
>
0
)
...
@@ -525,13 +531,13 @@ namespace Titanium.Web.Proxy
...
@@ -525,13 +531,13 @@ namespace Titanium.Web.Proxy
{
{
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
return
;
throw
;
}
}
catch
(
WebException
)
catch
(
WebException
)
{
{
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
return
;
throw
;
}
}
...
@@ -603,7 +609,7 @@ namespace Titanium.Web.Proxy
...
@@ -603,7 +609,7 @@ namespace Titanium.Web.Proxy
postStream
.
Close
();
postStream
.
Close
();
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
return
;
throw
;
}
}
catch
(
WebException
)
catch
(
WebException
)
{
{
...
@@ -611,30 +617,13 @@ namespace Titanium.Web.Proxy
...
@@ -611,30 +617,13 @@ namespace Titanium.Web.Proxy
postStream
.
Close
();
postStream
.
Close
();
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
return
;
throw
;
}
}
}
}
//Http request body sent, now wait asynchronously for response
args
.
ProxyRequest
.
BeginGetResponse
(
new
AsyncCallback
(
HandleHttpSessionResponse
),
args
);
//Now read the next request (if keep-Alive is enabled, otherwise exit thus thread)
//If client is pipeling the request, this will be immediately hit before response for previous request was made
if
(
args
.
ProxyRequest
.
KeepAlive
)
{
string
httpCmd
,
tmpLine
;
List
<
string
>
requestLines
=
new
List
<
string
>();
requestLines
.
Clear
();
while
(!
String
.
IsNullOrEmpty
(
tmpLine
=
args
.
ClientStreamReader
.
ReadLine
()))
{
requestLines
.
Add
(
tmpLine
);
}
httpCmd
=
requestLines
.
Count
()
>
0
?
requestLines
[
0
]
:
null
;
TcpClient
Client
=
args
.
Client
;
//Http request body sent, now wait for next request
HandleHttpSessionRequest
(
Client
,
httpCmd
,
args
.
ClientStream
,
args
.
tunnelHostName
,
requestLines
,
args
.
ClientStreamReader
,
args
.
securehost
);
}
}
}
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
9115e03f
...
@@ -18,13 +18,13 @@ namespace Titanium.Web.Proxy
...
@@ -18,13 +18,13 @@ namespace Titanium.Web.Proxy
partial
class
ProxyServer
partial
class
ProxyServer
{
{
//Called asynchronously when a request was successfully and we received the response
//Called asynchronously when a request was successfully and we received the response
private
static
void
HandleHttpSessionResponse
(
IAsyncResult
A
synchronousResult
)
private
static
void
HandleHttpSessionResponse
(
IAsyncResult
a
synchronousResult
)
{
{
SessionEventArgs
args
=
(
SessionEventArgs
)
A
synchronousResult
.
AsyncState
;
SessionEventArgs
args
=
(
SessionEventArgs
)
a
synchronousResult
.
AsyncState
;
try
try
{
{
args
.
ServerResponse
=
(
HttpWebResponse
)
args
.
ProxyRequest
.
EndGetResponse
(
A
synchronousResult
);
args
.
ServerResponse
=
(
HttpWebResponse
)
args
.
ProxyRequest
.
EndGetResponse
(
a
synchronousResult
);
}
}
catch
(
WebException
webEx
)
catch
(
WebException
webEx
)
{
{
...
@@ -32,21 +32,18 @@ namespace Titanium.Web.Proxy
...
@@ -32,21 +32,18 @@ namespace Titanium.Web.Proxy
//args.ProxyRequest.KeepAlive = false;
//args.ProxyRequest.KeepAlive = false;
args
.
ServerResponse
=
webEx
.
Response
as
HttpWebResponse
;
args
.
ServerResponse
=
webEx
.
Response
as
HttpWebResponse
;
}
}
Stream
serverResponseStream
=
null
;
Stream
clientWriteStream
=
args
.
ClientStream
;
StreamWriter
responseWriter
=
null
;
try
try
{
{
if
(
args
.
ClientStreamWriter
==
null
)
{
args
.
ClientStreamWriter
=
new
StreamWriter
(
args
.
ClientStream
);
}
responseWriter
=
new
StreamWriter
(
clientWriteStream
);
if
(
args
.
ServerResponse
!=
null
)
if
(
args
.
ServerResponse
!=
null
)
{
{
List
<
Tuple
<
String
,
String
>>
responseHeaders
=
ProcessResponse
(
args
.
ServerResponse
);
List
<
Tuple
<
String
,
String
>>
responseHeaders
=
ProcessResponse
(
args
.
ServerResponse
);
args
.
ServerResponseStream
=
args
.
ServerResponse
.
GetResponseStream
();
serverResponseStream
=
args
.
ServerResponse
.
GetResponseStream
();
args
.
ServerResponseStream
=
serverResponseStream
;
if
(
args
.
ServerResponse
.
Headers
.
Count
==
0
&&
args
.
ServerResponse
.
ContentLength
==
-
1
)
if
(
args
.
ServerResponse
.
Headers
.
Count
==
0
&&
args
.
ServerResponse
.
ContentLength
==
-
1
)
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
...
@@ -66,44 +63,44 @@ namespace Titanium.Web.Proxy
...
@@ -66,44 +63,44 @@ namespace Titanium.Web.Proxy
{
{
case
"gzip"
:
case
"gzip"
:
data
=
CompressionHelper
.
CompressGzip
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressGzip
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
response
Writer
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStream
Writer
);
WriteResponseHeaders
(
response
Writer
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStream
Writer
,
responseHeaders
,
data
.
Length
);
SendData
(
clientWrite
Stream
,
data
,
isChunked
);
SendData
(
args
.
Client
Stream
,
data
,
isChunked
);
break
;
break
;
case
"deflate"
:
case
"deflate"
:
data
=
CompressionHelper
.
CompressDeflate
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressDeflate
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
response
Writer
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStream
Writer
);
WriteResponseHeaders
(
response
Writer
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStream
Writer
,
responseHeaders
,
data
.
Length
);
SendData
(
clientWrite
Stream
,
data
,
isChunked
);
SendData
(
args
.
Client
Stream
,
data
,
isChunked
);
break
;
break
;
case
"zlib"
:
case
"zlib"
:
data
=
CompressionHelper
.
CompressZlib
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressZlib
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
response
Writer
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStream
Writer
);
WriteResponseHeaders
(
response
Writer
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStream
Writer
,
responseHeaders
,
data
.
Length
);
SendData
(
clientWrite
Stream
,
data
,
isChunked
);
SendData
(
args
.
Client
Stream
,
data
,
isChunked
);
break
;
break
;
default
:
default
:
data
=
EncodeData
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
data
=
EncodeData
(
args
.
ResponseHtmlBody
,
args
.
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
response
Writer
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStream
Writer
);
WriteResponseHeaders
(
response
Writer
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStream
Writer
,
responseHeaders
,
data
.
Length
);
SendData
(
clientWrite
Stream
,
data
,
isChunked
);
SendData
(
args
.
Client
Stream
,
data
,
isChunked
);
break
;
break
;
}
}
}
}
else
else
{
{
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
response
Writer
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStream
Writer
);
WriteResponseHeaders
(
response
Writer
,
responseHeaders
);
WriteResponseHeaders
(
args
.
ClientStream
Writer
,
responseHeaders
);
if
(
isChunked
)
if
(
isChunked
)
SendChunked
(
serverResponseStream
,
clientWrite
Stream
);
SendChunked
(
args
.
ServerResponseStream
,
args
.
Client
Stream
);
else
else
SendNormal
(
serverResponseStream
,
clientWrite
Stream
);
SendNormal
(
args
.
ServerResponseStream
,
args
.
Client
Stream
);
}
}
clientWrite
Stream
.
Flush
();
args
.
Client
Stream
.
Flush
();
}
}
else
else
...
@@ -115,7 +112,7 @@ namespace Titanium.Web.Proxy
...
@@ -115,7 +112,7 @@ namespace Titanium.Web.Proxy
{
{
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
}
}
catch
(
SocketException
ex
)
catch
(
SocketException
)
{
{
args
.
ProxyRequest
.
KeepAlive
=
false
;
args
.
ProxyRequest
.
KeepAlive
=
false
;
}
}
...
@@ -129,48 +126,48 @@ namespace Titanium.Web.Proxy
...
@@ -129,48 +126,48 @@ namespace Titanium.Web.Proxy
}
}
finally
finally
{
{
//If this is false then terminate the tcp client
if
(
args
.
ProxyRequest
.
KeepAlive
==
false
)
{
args
.
Dispose
();
}
else
{
//Close this HttpWebRequest session
//Close this HttpWebRequest session
if
(
args
.
ProxyRequest
!=
null
)
args
.
ProxyRequest
.
Abort
();
if
(
args
.
ProxyRequest
!=
null
)
if
(
args
.
ServerResponseStream
!=
null
)
args
.
ServerResponseStream
.
Close
();
args
.
ProxyRequest
.
Abort
();
if
(
args
.
ServerResponse
!=
null
)
if
(
args
.
ServerResponse
!=
null
)
args
.
ServerResponse
.
Close
();
args
.
ServerResponse
.
Close
();
}
if
(
args
.
ServerResponseStream
!=
null
)
args
.
ServerResponseStream
.
Close
();
//If this is false then terminate the tcp client
if
(
args
.
ProxyRequest
.
KeepAlive
==
false
)
{
if
(
responseWriter
!=
null
)
responseWriter
.
Close
();
if
(
clientWriteStream
!=
null
)
clientWriteStream
.
Close
();
args
.
Client
.
Close
();
}
}
}
}
}
private
static
List
<
Tuple
<
String
,
String
>>
ProcessResponse
(
HttpWebResponse
R
esponse
)
static
List
<
Tuple
<
String
,
String
>>
ProcessResponse
(
HttpWebResponse
r
esponse
)
{
{
String
value
=
null
;
String
value
=
null
;
String
header
=
null
;
String
header
=
null
;
List
<
Tuple
<
String
,
String
>>
returnHeaders
=
new
List
<
Tuple
<
String
,
String
>>();
List
<
Tuple
<
String
,
String
>>
returnHeaders
=
new
List
<
Tuple
<
String
,
String
>>();
foreach
(
String
s
in
R
esponse
.
Headers
.
Keys
)
foreach
(
String
s
in
r
esponse
.
Headers
.
Keys
)
{
{
if
(
s
.
ToLower
()
==
"set-cookie"
)
if
(
s
.
ToLower
()
==
"set-cookie"
)
{
{
header
=
s
;
header
=
s
;
value
=
R
esponse
.
Headers
[
s
];
value
=
r
esponse
.
Headers
[
s
];
}
}
else
else
returnHeaders
.
Add
(
new
Tuple
<
String
,
String
>(
s
,
R
esponse
.
Headers
[
s
]));
returnHeaders
.
Add
(
new
Tuple
<
String
,
String
>(
s
,
r
esponse
.
Headers
[
s
]));
}
}
if
(!
String
.
IsNullOrWhiteSpace
(
value
))
if
(!
String
.
IsNullOrWhiteSpace
(
value
))
{
{
R
esponse
.
Headers
.
Remove
(
header
);
r
esponse
.
Headers
.
Remove
(
header
);
String
[]
cookies
=
cookieSplitRegEx
.
Split
(
value
);
String
[]
cookies
=
cookieSplitRegEx
.
Split
(
value
);
foreach
(
String
cookie
in
cookies
)
foreach
(
String
cookie
in
cookies
)
returnHeaders
.
Add
(
new
Tuple
<
String
,
String
>(
"Set-Cookie"
,
cookie
));
returnHeaders
.
Add
(
new
Tuple
<
String
,
String
>(
"Set-Cookie"
,
cookie
));
...
@@ -180,123 +177,121 @@ namespace Titanium.Web.Proxy
...
@@ -180,123 +177,121 @@ namespace Titanium.Web.Proxy
return
returnHeaders
;
return
returnHeaders
;
}
}
private
static
void
WriteResponseStatus
(
Version
Version
,
HttpStatusCode
Code
,
String
Description
,
StreamWriter
R
esponseWriter
)
static
void
WriteResponseStatus
(
Version
version
,
HttpStatusCode
code
,
String
description
,
StreamWriter
r
esponseWriter
)
{
{
String
s
=
String
.
Format
(
"HTTP/{0}.{1} {2} {3}"
,
Version
.
Major
,
Version
.
Minor
,
(
Int32
)
Code
,
D
escription
);
String
s
=
String
.
Format
(
"HTTP/{0}.{1} {2} {3}"
,
version
.
Major
,
version
.
Minor
,
(
Int32
)
code
,
d
escription
);
R
esponseWriter
.
WriteLine
(
s
);
r
esponseWriter
.
WriteLine
(
s
);
}
}
private
static
void
WriteResponseHeaders
(
StreamWriter
ResponseWriter
,
List
<
Tuple
<
String
,
String
>>
H
eaders
)
static
void
WriteResponseHeaders
(
StreamWriter
responseWriter
,
List
<
Tuple
<
String
,
String
>>
h
eaders
)
{
{
if
(
H
eaders
!=
null
)
if
(
h
eaders
!=
null
)
{
{
foreach
(
Tuple
<
String
,
String
>
header
in
H
eaders
)
foreach
(
Tuple
<
String
,
String
>
header
in
h
eaders
)
{
{
R
esponseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
header
.
Item1
,
header
.
Item2
));
r
esponseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
header
.
Item1
,
header
.
Item2
));
}
}
}
}
R
esponseWriter
.
WriteLine
();
r
esponseWriter
.
WriteLine
();
R
esponseWriter
.
Flush
();
r
esponseWriter
.
Flush
();
}
}
private
static
void
WriteResponseHeaders
(
StreamWriter
ResponseWriter
,
List
<
Tuple
<
String
,
String
>>
Headers
,
int
L
ength
)
static
void
WriteResponseHeaders
(
StreamWriter
responseWriter
,
List
<
Tuple
<
String
,
String
>>
headers
,
int
l
ength
)
{
{
if
(
H
eaders
!=
null
)
if
(
h
eaders
!=
null
)
{
{
foreach
(
Tuple
<
String
,
String
>
header
in
H
eaders
)
foreach
(
Tuple
<
String
,
String
>
header
in
h
eaders
)
{
{
if
(
header
.
Item1
.
ToLower
()
!=
"content-length"
)
if
(
header
.
Item1
.
ToLower
()
!=
"content-length"
)
R
esponseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
header
.
Item1
,
header
.
Item2
));
r
esponseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
header
.
Item1
,
header
.
Item2
));
else
else
ResponseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
"content-length"
,
L
ength
.
ToString
()));
responseWriter
.
WriteLine
(
String
.
Format
(
"{0}: {1}"
,
"content-length"
,
l
ength
.
ToString
()));
}
}
}
}
R
esponseWriter
.
WriteLine
();
r
esponseWriter
.
WriteLine
();
R
esponseWriter
.
Flush
();
r
esponseWriter
.
Flush
();
}
}
public
static
void
SendNormal
(
Stream
InStream
,
Stream
O
utStream
)
static
void
SendNormal
(
Stream
inStream
,
Stream
o
utStream
)
{
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
int
bytesRead
;
int
bytesRead
;
while
((
bytesRead
=
I
nStream
.
Read
(
buffer
,
0
,
buffer
.
Length
))
>
0
)
while
((
bytesRead
=
i
nStream
.
Read
(
buffer
,
0
,
buffer
.
Length
))
>
0
)
{
{
O
utStream
.
Write
(
buffer
,
0
,
bytesRead
);
o
utStream
.
Write
(
buffer
,
0
,
bytesRead
);
}
}
}
}
//Send chunked response
//Send chunked response
public
static
void
SendChunked
(
Stream
InStream
,
Stream
O
utStream
)
static
void
SendChunked
(
Stream
inStream
,
Stream
o
utStream
)
{
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
var
C
hunkTrail
=
Encoding
.
ASCII
.
GetBytes
(
Environment
.
NewLine
);
var
c
hunkTrail
=
Encoding
.
ASCII
.
GetBytes
(
Environment
.
NewLine
);
int
bytesRead
;
int
bytesRead
;
while
((
bytesRead
=
I
nStream
.
Read
(
buffer
,
0
,
buffer
.
Length
))
>
0
)
while
((
bytesRead
=
i
nStream
.
Read
(
buffer
,
0
,
buffer
.
Length
))
>
0
)
{
{
var
C
hunkHead
=
Encoding
.
ASCII
.
GetBytes
(
bytesRead
.
ToString
(
"x2"
));
var
c
hunkHead
=
Encoding
.
ASCII
.
GetBytes
(
bytesRead
.
ToString
(
"x2"
));
OutStream
.
Write
(
ChunkHead
,
0
,
C
hunkHead
.
Length
);
outStream
.
Write
(
chunkHead
,
0
,
c
hunkHead
.
Length
);
OutStream
.
Write
(
ChunkTrail
,
0
,
C
hunkTrail
.
Length
);
outStream
.
Write
(
chunkTrail
,
0
,
c
hunkTrail
.
Length
);
O
utStream
.
Write
(
buffer
,
0
,
bytesRead
);
o
utStream
.
Write
(
buffer
,
0
,
bytesRead
);
OutStream
.
Write
(
ChunkTrail
,
0
,
C
hunkTrail
.
Length
);
outStream
.
Write
(
chunkTrail
,
0
,
c
hunkTrail
.
Length
);
}
}
var
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
var
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
O
utStream
.
Write
(
ChunkEnd
,
0
,
ChunkEnd
.
Length
);
o
utStream
.
Write
(
ChunkEnd
,
0
,
ChunkEnd
.
Length
);
}
}
public
static
void
SendChunked
(
byte
[]
Data
,
Stream
O
utStream
)
static
void
SendChunked
(
byte
[]
data
,
Stream
o
utStream
)
{
{
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
Byte
[]
buffer
=
new
Byte
[
BUFFER_SIZE
];
var
C
hunkTrail
=
Encoding
.
ASCII
.
GetBytes
(
Environment
.
NewLine
);
var
c
hunkTrail
=
Encoding
.
ASCII
.
GetBytes
(
Environment
.
NewLine
);
var
ChunkHead
=
Encoding
.
ASCII
.
GetBytes
(
D
ata
.
Length
.
ToString
(
"x2"
));
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
d
ata
.
Length
.
ToString
(
"x2"
));
OutStream
.
Write
(
ChunkHead
,
0
,
C
hunkHead
.
Length
);
outStream
.
Write
(
chunkHead
,
0
,
c
hunkHead
.
Length
);
OutStream
.
Write
(
ChunkTrail
,
0
,
C
hunkTrail
.
Length
);
outStream
.
Write
(
chunkTrail
,
0
,
c
hunkTrail
.
Length
);
OutStream
.
Write
(
Data
,
0
,
D
ata
.
Length
);
outStream
.
Write
(
data
,
0
,
d
ata
.
Length
);
OutStream
.
Write
(
ChunkTrail
,
0
,
C
hunkTrail
.
Length
);
outStream
.
Write
(
chunkTrail
,
0
,
c
hunkTrail
.
Length
);
var
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
var
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
O
utStream
.
Write
(
ChunkEnd
,
0
,
ChunkEnd
.
Length
);
o
utStream
.
Write
(
ChunkEnd
,
0
,
ChunkEnd
.
Length
);
}
}
public
static
byte
[]
EncodeData
(
string
R
esponseData
,
Encoding
e
)
static
byte
[]
EncodeData
(
string
r
esponseData
,
Encoding
e
)
{
{
return
e
.
GetBytes
(
responseData
);
return
e
.
GetBytes
(
ResponseData
);
}
}
public
static
void
SendData
(
Stream
OutStream
,
byte
[]
Data
,
bool
I
sChunked
)
static
void
SendData
(
Stream
outStream
,
byte
[]
data
,
bool
i
sChunked
)
{
{
if
(!
I
sChunked
)
if
(!
i
sChunked
)
{
{
OutStream
.
Write
(
Data
,
0
,
D
ata
.
Length
);
outStream
.
Write
(
data
,
0
,
d
ata
.
Length
);
}
}
else
else
SendChunked
(
Data
,
O
utStream
);
SendChunked
(
data
,
o
utStream
);
}
}
...
...
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
View file @
9115e03f
...
@@ -79,6 +79,7 @@
...
@@ -79,6 +79,7 @@
<Compile
Include=
"Helpers\CertificateManager.cs"
/>
<Compile
Include=
"Helpers\CertificateManager.cs"
/>
<Compile
Include=
"Helpers\Firefox.cs"
/>
<Compile
Include=
"Helpers\Firefox.cs"
/>
<Compile
Include=
"Helpers\SystemProxy.cs"
/>
<Compile
Include=
"Helpers\SystemProxy.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"RequestHandler.cs"
/>
<Compile
Include=
"RequestHandler.cs"
/>
<Compile
Include=
"ResponseHandler.cs"
/>
<Compile
Include=
"ResponseHandler.cs"
/>
<Compile
Include=
"Helpers\CustomBinaryReader.cs"
/>
<Compile
Include=
"Helpers\CustomBinaryReader.cs"
/>
...
@@ -89,9 +90,7 @@
...
@@ -89,9 +90,7 @@
<Compile
Include=
"Helpers\Tcp.cs"
/>
<Compile
Include=
"Helpers\Tcp.cs"
/>
<Compile
Include=
"Helpers\Stream.cs"
/>
<Compile
Include=
"Helpers\Stream.cs"
/>
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup
/>
<Folder
Include=
"Properties\"
/>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<None
Include=
"packages.config"
/>
<None
Include=
"packages.config"
/>
<None
Include=
"Titanium_Proxy_Test_Root.cer"
>
<None
Include=
"Titanium_Proxy_Test_Root.cer"
>
...
...
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