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
11563d36
Commit
11563d36
authored
Sep 05, 2015
by
titanium007
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix request body modification
parent
6827dbeb
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
230 additions
and
179 deletions
+230
-179
README.md
README.md
+35
-16
ProxyTestController.cs
Titanium.Web.Proxy.Test/ProxyTestController.cs
+35
-22
BodyNotFoundException.cs
Titanium.Web.Proxy/Exceptions/BodyNotFoundException.cs
+17
-0
HttpWebRequestExtensions.cs
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
+32
-0
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+0
-6
SessionEventArgs.cs
Titanium.Web.Proxy/Models/SessionEventArgs.cs
+46
-49
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+12
-13
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+42
-62
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+9
-11
Titanium.Web.Proxy.csproj
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
+2
-0
No files found.
README.md
View file @
11563d36
...
@@ -64,30 +64,49 @@ Sample request and response event handlers
...
@@ -64,30 +64,49 @@ Sample request and response event handlers
```
csharp
```
csharp
public
void
OnRequest
(
object
sender
,
SessionEventArgs
e
)
//Test On Request, intecept requests
//Read browser URL send back to proxy by the injection script in OnResponse event
public
void
OnRequest
(
object
sender
,
SessionEventArgs
e
)
{
{
//To cancel a request with a custom HTML content
//Filter URL
Console
.
WriteLine
(
e
.
RequestURL
);
if
(
e
.
RequestURL
.
Contains
(
"somewebsite.com"
))
if
((
e
.
ProxyRequest
.
Method
.
ToUpper
()
==
"POST"
||
e
.
ProxyRequest
.
Method
.
ToUpper
()
==
"PUT"
)
&&
e
.
ProxyRequest
.
ContentLength
>
0
)
{
var
m
=
e
.
GetRequestBody
().
Replace
(
"a"
,
"b"
);
e
.
SetRequestBody
(
m
);
}
//To cancel a request with a custom HTML content
//Filter URL
if
(
e
.
RequestURL
.
Contains
(
"somewebsite.com"
))
if
(
e
.
RequestURL
.
Contains
(
"somewebsite.com"
))
{
{
e
.
Ok
(
"<!DOCTYPE html><html><body><h1>Blocked</h1><p>
w
ebsite blocked.</p></body></html>"
);
e
.
Ok
(
"<!DOCTYPE html><html><body><h1>Blocked</h1><p>
W
ebsite blocked.</p></body></html>"
);
}
}
}
}
public
void
OnResponse
(
object
sender
,
SessionEventArgs
e
)
public
void
OnResponse
(
object
sender
,
SessionEventArgs
e
)
{
{
if
(
e
.
ServerResponse
.
StatusCode
==
HttpStatusCode
.
OK
)
if
(
e
.
ServerResponse
.
StatusCode
==
HttpStatusCode
.
OK
)
{
{
if
(
e
.
ServerResponse
.
ContentType
.
Trim
().
ToLower
().
Contains
(
"text/html"
))
if
(
e
.
ServerResponse
.
ContentType
.
Trim
().
ToLower
().
Contains
(
"text/html"
))
{
{
//Get response body
//Get response body
string
responseHtmlBody
=
e
.
GetResponseHtmlBody
();
string
responseBody
=
e
.
GetResponseBody
();
//Modify e.ServerResponse
responseHtmlBody
=
"<html><head></head><body>Response is modified!</body></html>"
;
//Modify e.ServerResponse
//Set modifed response Html Body
Regex
rex
=
new
Regex
(
"</body>"
,
RegexOptions
.
RightToLeft
|
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Multiline
);
e
.
SetResponseHtmlBody
(
responseHtmlBody
);
string
modified
=
rex
.
Replace
(
responseBody
,
"<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>"
,
1
);
}
}
//Set modifed response Html Body
e
.
SetResponseBody
(
modified
);
}
}
}
}
```
```
Future updates
Future updates
...
...
Titanium.Web.Proxy.Test/ProxyTestController.cs
View file @
11563d36
...
@@ -24,23 +24,24 @@ namespace Titanium.Web.Proxy.Test
...
@@ -24,23 +24,24 @@ namespace Titanium.Web.Proxy.Test
public
void
StartProxy
()
public
void
StartProxy
()
{
{
ProxyServer
.
BeforeRequest
+=
OnRequest
;
ProxyServer
.
BeforeRequest
+=
OnRequest
;
ProxyServer
.
BeforeResponse
+=
OnResponse
;
ProxyServer
.
BeforeResponse
+=
OnResponse
;
ProxyServer
.
EnableSSL
=
EnableSSL
;
ProxyServer
.
EnableSSL
=
EnableSSL
;
ProxyServer
.
SetAsSystemProxy
=
SetAsSystemProxy
;
ProxyServer
.
SetAsSystemProxy
=
SetAsSystemProxy
;
//Exclude Https addresses you don't want to proxy
//Usefull for clients that use certificate pinning
//for example dropbox.com
ProxyServer
.
ExcludedHttpsHostNameRegex
.
Add
(
".dropbox.com"
);
ProxyServer
.
Start
();
ProxyServer
.
Start
();
ProxyServer
.
ListeningPort
=
ProxyServer
.
ListeningPort
;
ListeningPort
=
ProxyServer
.
ListeningPort
;
Console
.
WriteLine
(
String
.
Format
(
"Proxy listening on local machine port: {0} "
,
ProxyServer
.
ListeningPort
));
Console
.
WriteLine
(
String
.
Format
(
"Proxy listening on local machine port: {0} "
,
ProxyServer
.
ListeningPort
));
}
}
public
void
Stop
()
public
void
Stop
()
{
{
...
@@ -59,14 +60,23 @@ namespace Titanium.Web.Proxy.Test
...
@@ -59,14 +60,23 @@ namespace Titanium.Web.Proxy.Test
Console
.
WriteLine
(
e
.
RequestURL
);
Console
.
WriteLine
(
e
.
RequestURL
);
if
(
e
.
RequestURL
.
Contains
(
"somewebsite.com"
))
if
((
e
.
ProxyRequest
.
Method
.
ToUpper
()
==
"POST"
||
e
.
ProxyRequest
.
Method
.
ToUpper
()
==
"PUT"
)
&&
e
.
ProxyRequest
.
ContentLength
>
0
)
{
var
m
=
e
.
GetRequestBody
().
Replace
(
"a"
,
"b"
);
e
.
SetRequestBody
(
m
);
}
//To cancel a request with a custom HTML content
//To cancel a request with a custom HTML content
//Filter URL
//Filter URL
//
if (e.RequestURL.Contains("somewebsite.com"))
if
(
e
.
RequestURL
.
Contains
(
"somewebsite.com"
))
//
{
{
// e.Ok("<!DOCTYPE html><html><body><h1>Blocked</h1><p>w
ebsite blocked.</p></body></html>");
e
.
Ok
(
"<!DOCTYPE html><html><body><h1>Blocked</h1><p>W
ebsite blocked.</p></body></html>"
);
//
}
}
}
}
//Test script injection
//Test script injection
...
@@ -75,18 +85,21 @@ namespace Titanium.Web.Proxy.Test
...
@@ -75,18 +85,21 @@ namespace Titanium.Web.Proxy.Test
{
{
//To modify a response
//To modify a response
//if (e.ServerResponse.StatusCode == HttpStatusCode.OK)
if
(
e
.
ServerResponse
.
StatusCode
==
HttpStatusCode
.
OK
)
//{
{
// if (e.ServerResponse.ContentType.Trim().ToLower().Contains("text/html"))
if
(
e
.
ServerResponse
.
ContentType
.
Trim
().
ToLower
().
Contains
(
"text/html"
))
// {
{
// //Get response body
//Get response body
// string responseHtmlBody = e.GetResponseHtmlBody();
string
responseBody
=
e
.
GetResponseBody
();
// //Modify e.ServerResponse
// responseHtmlBody = "<html><head></head><body>Response is modified!</body></html>";
//Modify e.ServerResponse
// //Set modifed response Html Body
Regex
rex
=
new
Regex
(
"</body>"
,
RegexOptions
.
RightToLeft
|
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Multiline
);
// e.SetResponseHtmlBody(responseHtmlBody);
string
modified
=
rex
.
Replace
(
responseBody
,
"<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>"
,
1
);
// }
//}
//Set modifed response Html Body
e
.
SetResponseBody
(
modified
);
}
}
}
}
...
...
Titanium.Web.Proxy/Exceptions/BodyNotFoundException.cs
0 → 100644
View file @
11563d36
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
namespace
Titanium.Web.Proxy.Exceptions
{
public
class
BodyNotFoundException
:
Exception
{
public
BodyNotFoundException
(
string
message
)
:
base
(
message
)
{
}
}
}
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
0 → 100644
View file @
11563d36
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net
;
using
System.Text
;
namespace
Titanium.Web.Proxy.Extensions
{
public
static
class
HttpWebRequestExtensions
{
public
static
Encoding
GetEncoding
(
this
HttpWebRequest
request
)
{
try
{
if
(
request
.
ContentType
==
null
)
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
var
contentTypes
=
request
.
ContentType
.
Split
(
';'
);
foreach
(
var
contentType
in
contentTypes
)
{
var
encodingSplit
=
contentType
.
Split
(
'='
);
if
(
encodingSplit
.
Length
==
2
&&
encodingSplit
[
0
].
ToLower
().
Trim
()
==
"charset"
)
{
return
Encoding
.
GetEncoding
(
encodingSplit
[
1
]);
}
}
}
catch
{
}
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
}
}
}
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
11563d36
...
@@ -31,9 +31,6 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -31,9 +31,6 @@ namespace Titanium.Web.Proxy.Helpers
Task
sendRelay
=
Task
.
Factory
.
StartNew
(()
=>
StreamHelper
.
CopyTo
(
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
Task
sendRelay
=
Task
.
Factory
.
StartNew
(()
=>
StreamHelper
.
CopyTo
(
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
Task
receiveRelay
=
Task
.
Factory
.
StartNew
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
Task
receiveRelay
=
Task
.
Factory
.
StartNew
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
receiveRelay
.
Start
();
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
}
}
catch
catch
...
@@ -118,9 +115,6 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -118,9 +115,6 @@ namespace Titanium.Web.Proxy.Helpers
var
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
sb
.
ToString
(),
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
var
sendRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
sb
.
ToString
(),
clientStream
,
tunnelStream
,
BUFFER_SIZE
));
var
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
var
receiveRelay
=
new
Task
(()
=>
StreamHelper
.
CopyTo
(
tunnelStream
,
clientStream
,
BUFFER_SIZE
));
sendRelay
.
Start
();
receiveRelay
.
Start
();
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
Task
.
WaitAll
(
sendRelay
,
receiveRelay
);
}
}
catch
catch
...
...
Titanium.Web.Proxy/Models/SessionEventArgs.cs
View file @
11563d36
...
@@ -4,6 +4,7 @@ using System.IO;
...
@@ -4,6 +4,7 @@ using System.IO;
using
System.Net
;
using
System.Net
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
System.Net.Sockets
;
using
System.Net.Sockets
;
using
Titanium.Web.Proxy.Exceptions
;
namespace
Titanium.Web.Proxy.Models
namespace
Titanium.Web.Proxy.Models
...
@@ -18,25 +19,25 @@ namespace Titanium.Web.Proxy.Models
...
@@ -18,25 +19,25 @@ namespace Titanium.Web.Proxy.Models
internal
CustomBinaryReader
ClientStreamReader
{
get
;
set
;
}
internal
CustomBinaryReader
ClientStreamReader
{
get
;
set
;
}
internal
StreamWriter
ClientStreamWriter
{
get
;
set
;
}
internal
StreamWriter
ClientStreamWriter
{
get
;
set
;
}
internal
string
UpgradeProtocol
{
get
;
set
;
}
internal
string
HttpsHostName
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
string
HttpsDecoratedHostName
{
get
;
set
;
}
internal
int
RequestLength
{
get
;
set
;
}
internal
int
RequestContentLength
{
get
;
set
;
}
internal
Encoding
RequestEncoding
{
get
;
set
;
}
internal
Version
RequestHttpVersion
{
get
;
set
;
}
internal
Version
RequestHttpVersion
{
get
;
set
;
}
internal
bool
RequestIsAlive
{
get
;
set
;
}
internal
bool
RequestIsAlive
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
string
Request
Html
Body
{
get
;
set
;
}
internal
string
RequestBody
{
get
;
set
;
}
internal
bool
Request
WasModifie
d
{
get
;
set
;
}
internal
bool
Request
BodyRea
d
{
get
;
set
;
}
internal
Stream
ServerResponseStream
{
get
;
set
;
}
internal
Encoding
ResponseEncoding
{
get
;
set
;
}
internal
string
ResponseHtmlBody
{
get
;
set
;
}
internal
Stream
ResponseStream
{
get
;
set
;
}
internal
bool
ResponseWasModified
{
get
;
set
;
}
internal
string
ResponseBody
{
get
;
set
;
}
internal
bool
ResponseBodyRead
{
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
IsHttps
{
get
;
set
;
}
public
bool
IsSSLRequest
{
get
;
set
;
}
public
string
RequestURL
{
get
;
set
;
}
public
string
RequestURL
{
get
;
set
;
}
public
string
RequestHostname
{
get
;
set
;
}
public
string
RequestHostname
{
get
;
set
;
}
...
@@ -48,8 +49,8 @@ namespace Titanium.Web.Proxy.Models
...
@@ -48,8 +49,8 @@ namespace Titanium.Web.Proxy.Models
if
(
this
.
ProxyRequest
!=
null
)
if
(
this
.
ProxyRequest
!=
null
)
this
.
ProxyRequest
.
Abort
();
this
.
ProxyRequest
.
Abort
();
if
(
this
.
Server
ResponseStream
!=
null
)
if
(
this
.
ResponseStream
!=
null
)
this
.
Server
ResponseStream
.
Dispose
();
this
.
ResponseStream
.
Dispose
();
if
(
this
.
ServerResponse
!=
null
)
if
(
this
.
ServerResponse
!=
null
)
this
.
ServerResponse
.
Close
();
this
.
ServerResponse
.
Close
();
...
@@ -60,70 +61,66 @@ namespace Titanium.Web.Proxy.Models
...
@@ -60,70 +61,66 @@ namespace Titanium.Web.Proxy.Models
{
{
BUFFER_SIZE
=
bufferSize
;
BUFFER_SIZE
=
bufferSize
;
}
}
public
string
GetRequest
Html
Body
()
public
string
GetRequestBody
()
{
{
if
(
RequestHtmlBody
==
null
)
if
(
(
ProxyRequest
.
Method
.
ToUpper
()
==
"POST"
||
ProxyRequest
.
Method
.
ToUpper
()
==
"PUT"
)
&&
RequestContentLength
>
0
)
{
{
if
(
RequestBody
==
null
)
int
bytesRead
;
int
totalBytesRead
=
0
;
MemoryStream
mw
=
new
MemoryStream
();
var
buffer
=
ClientStreamReader
.
ReadBytes
(
RequestLength
);
while
(
totalBytesRead
<
RequestLength
&&
(
bytesRead
=
buffer
.
Length
)
>
0
)
{
{
totalBytesRead
+=
bytesRead
;
var
buffer
=
ClientStreamReader
.
ReadBytes
(
RequestContentLength
);
mw
.
Write
(
buffer
,
0
,
bytesRead
);
RequestBody
=
RequestEncoding
.
GetString
(
buffer
);
}
}
RequestBodyRead
=
true
;
mw
.
Close
();
return
RequestBody
;
RequestHtmlBody
=
Encoding
.
Default
.
GetString
(
mw
.
ToArray
());
}
}
RequestWasModified
=
true
;
else
return
RequestHtmlBody
;
throw
new
BodyNotFoundException
(
"Request don't have a body."
+
"Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body."
);
}
}
public
void
SetRequest
Html
Body
(
string
body
)
public
void
SetRequestBody
(
string
body
)
{
{
this
.
Request
Html
Body
=
body
;
this
.
RequestBody
=
body
;
Request
WasModifie
d
=
true
;
Request
BodyRea
d
=
true
;
}
}
public
string
GetResponse
Html
Body
()
public
string
GetResponseBody
()
{
{
if
(
Response
Html
Body
==
null
)
if
(
ResponseBody
==
null
)
{
{
Encoding
=
Encoding
.
GetEncoding
(
ServerResponse
.
CharacterSet
);
if
(
ResponseEncoding
==
null
)
Response
Encoding
=
Encoding
.
GetEncoding
(
ServerResponse
.
CharacterSet
);
if
(
ResponseEncoding
==
null
)
ResponseEncoding
=
Encoding
.
Default
;
if
(
Encoding
==
null
)
Encoding
=
Encoding
.
Default
;
switch
(
ServerResponse
.
ContentEncoding
)
switch
(
ServerResponse
.
ContentEncoding
)
{
{
case
"gzip"
:
case
"gzip"
:
Response
HtmlBody
=
CompressionHelper
.
DecompressGzip
(
ServerResponseStream
,
Encoding
);
Response
Body
=
CompressionHelper
.
DecompressGzip
(
ResponseStream
,
Response
Encoding
);
break
;
break
;
case
"deflate"
:
case
"deflate"
:
Response
HtmlBody
=
CompressionHelper
.
DecompressDeflate
(
ServerResponseStream
,
Encoding
);
Response
Body
=
CompressionHelper
.
DecompressDeflate
(
ResponseStream
,
Response
Encoding
);
break
;
break
;
case
"zlib"
:
case
"zlib"
:
Response
HtmlBody
=
CompressionHelper
.
DecompressZlib
(
ServerResponseStream
,
Encoding
);
Response
Body
=
CompressionHelper
.
DecompressZlib
(
ResponseStream
,
Response
Encoding
);
break
;
break
;
default
:
default
:
Response
HtmlBody
=
DecodeData
(
ServerResponseStream
,
Encoding
);
Response
Body
=
DecodeData
(
ResponseStream
,
Response
Encoding
);
break
;
break
;
}
}
Response
WasModifie
d
=
true
;
Response
BodyRea
d
=
true
;
}
}
return
Response
Html
Body
;
return
ResponseBody
;
}
}
public
void
SetResponse
Html
Body
(
string
body
)
public
void
SetResponseBody
(
string
body
)
{
{
this
.
ResponseHtmlBody
=
body
;
if
(
ResponseEncoding
==
null
)
ResponseEncoding
=
Encoding
.
GetEncoding
(
ServerResponse
.
CharacterSet
);
ResponseWasModified
=
true
;
if
(
ResponseEncoding
==
null
)
ResponseEncoding
=
Encoding
.
Default
;
this
.
ResponseBody
=
body
;
ResponseBodyRead
=
true
;
}
}
//stream reader not recomended for images
//stream reader not recomended for images
private
string
DecodeData
(
Stream
responseStream
,
Encoding
e
)
private
string
DecodeData
(
Stream
responseStream
,
Encoding
e
)
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
11563d36
...
@@ -31,29 +31,23 @@ namespace Titanium.Web.Proxy
...
@@ -31,29 +31,23 @@ namespace Titanium.Web.Proxy
private
static
readonly
Regex
cookieSplitRegEx
=
new
Regex
(
@",(?! )"
);
private
static
readonly
Regex
cookieSplitRegEx
=
new
Regex
(
@",(?! )"
);
private
static
object
certificateAccessLock
=
new
object
();
private
static
object
certificateAccessLock
=
new
object
();
private
static
List
<
string
>
pinnedCertificateClients
=
new
List
<
string
>();
private
static
TcpListener
listener
;
private
static
TcpListener
listener
;
private
static
Thread
listenerThread
;
private
static
Thread
listenerThread
;
private
static
bool
ShouldListen
{
get
;
set
;
}
private
static
bool
ShouldListen
{
get
;
set
;
}
public
static
List
<
string
>
ExcludedHttpsHostNameRegex
=
new
List
<
string
>();
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeRequest
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
public
static
event
EventHandler
<
SessionEventArgs
>
BeforeResponse
;
public
static
IPAddress
ListeningIPInterface
{
get
;
set
;
}
public
static
string
RootCertificateName
{
get
;
set
;
}
public
static
string
RootCertificateName
{
get
;
set
;
}
public
static
bool
EnableSSL
{
get
;
set
;
}
public
static
bool
EnableSSL
{
get
;
set
;
}
public
static
bool
SetAsSystemProxy
{
get
;
set
;
}
public
static
bool
SetAsSystemProxy
{
get
;
set
;
}
public
static
Int32
ListeningPort
public
static
Int32
ListeningPort
{
get
;
set
;
}
{
public
static
IPAddress
ListeningIpAddress
{
get
;
set
;
}
get
{
return
((
IPEndPoint
)
listener
.
LocalEndpoint
).
Port
;
}
}
public
static
CertificateManager
CertManager
{
get
;
set
;
}
public
static
CertificateManager
CertManager
{
get
;
set
;
}
...
@@ -61,12 +55,14 @@ namespace Titanium.Web.Proxy
...
@@ -61,12 +55,14 @@ namespace Titanium.Web.Proxy
{
{
CertManager
=
new
CertificateManager
(
"Titanium"
,
CertManager
=
new
CertificateManager
(
"Titanium"
,
"Titanium Root Certificate Authority"
);
"Titanium Root Certificate Authority"
);
ListeningIpAddress
=
IPAddress
.
Any
;
ListeningPort
=
0
;
}
}
public
ProxyServer
()
public
ProxyServer
()
{
{
System
.
Net
.
ServicePointManager
.
Expect100Continue
=
false
;
System
.
Net
.
ServicePointManager
.
Expect100Continue
=
false
;
System
.
Net
.
WebRequest
.
DefaultWebProxy
=
null
;
System
.
Net
.
WebRequest
.
DefaultWebProxy
=
null
;
System
.
Net
.
ServicePointManager
.
DefaultConnectionLimit
=
10
;
System
.
Net
.
ServicePointManager
.
DefaultConnectionLimit
=
10
;
...
@@ -111,13 +107,16 @@ namespace Titanium.Web.Proxy
...
@@ -111,13 +107,16 @@ namespace Titanium.Web.Proxy
public
static
bool
Start
()
public
static
bool
Start
()
{
{
listener
=
new
TcpListener
(
IPAddress
.
Any
,
0
);
listener
=
new
TcpListener
(
ListeningIpAddress
,
ListeningPort
);
listener
.
Start
();
listener
.
Start
();
listenerThread
=
new
Thread
(
new
ParameterizedThreadStart
(
Listen
));
listenerThread
=
new
Thread
(
new
ParameterizedThreadStart
(
Listen
));
listenerThread
.
IsBackground
=
true
;
listenerThread
.
IsBackground
=
true
;
ShouldListen
=
true
;
ShouldListen
=
true
;
listenerThread
.
Start
(
listener
);
listenerThread
.
Start
(
listener
);
ListeningPort
=
((
IPEndPoint
)
listener
.
LocalEndpoint
).
Port
;
if
(
SetAsSystemProxy
)
if
(
SetAsSystemProxy
)
{
{
SystemProxyHelper
.
EnableProxyHTTP
(
"localhost"
,
ListeningPort
);
SystemProxyHelper
.
EnableProxyHTTP
(
"localhost"
,
ListeningPort
);
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
11563d36
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/ResponseHandler.cs
View file @
11563d36
...
@@ -38,41 +38,39 @@ namespace Titanium.Web.Proxy
...
@@ -38,41 +38,39 @@ namespace Titanium.Web.Proxy
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
.
Server
ResponseStream
=
args
.
ServerResponse
.
GetResponseStream
();
args
.
ResponseStream
=
args
.
ServerResponse
.
GetResponseStream
();
bool
isChunked
=
args
.
ServerResponse
.
GetResponseHeader
(
"transfer-encoding"
)
==
null
?
false
:
args
.
ServerResponse
.
GetResponseHeader
(
"transfer-encoding"
).
ToLower
()
==
"chunked"
?
true
:
false
;
bool
isChunked
=
args
.
ServerResponse
.
GetResponseHeader
(
"transfer-encoding"
)
==
null
?
false
:
args
.
ServerResponse
.
GetResponseHeader
(
"transfer-encoding"
).
ToLower
()
==
"chunked"
?
true
:
false
;
args
.
UpgradeProtocol
=
args
.
ServerResponse
.
GetResponseHeader
(
"upgrade"
)
==
null
?
null
:
args
.
ServerResponse
.
GetResponseHeader
(
"upgrade"
);
if
(
BeforeResponse
!=
null
)
if
(
BeforeResponse
!=
null
)
BeforeResponse
(
null
,
args
);
BeforeResponse
(
null
,
args
);
if
(
args
.
Response
WasModifie
d
)
if
(
args
.
Response
BodyRea
d
)
{
{
byte
[]
data
;
byte
[]
data
;
switch
(
args
.
ServerResponse
.
ContentEncoding
)
switch
(
args
.
ServerResponse
.
ContentEncoding
)
{
{
case
"gzip"
:
case
"gzip"
:
data
=
CompressionHelper
.
CompressGzip
(
args
.
Response
HtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressGzip
(
args
.
Response
Body
,
args
.
Response
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
break
;
break
;
case
"deflate"
:
case
"deflate"
:
data
=
CompressionHelper
.
CompressDeflate
(
args
.
Response
HtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressDeflate
(
args
.
Response
Body
,
args
.
Response
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
break
;
break
;
case
"zlib"
:
case
"zlib"
:
data
=
CompressionHelper
.
CompressZlib
(
args
.
Response
HtmlBody
,
args
.
Encoding
);
data
=
CompressionHelper
.
CompressZlib
(
args
.
Response
Body
,
args
.
Response
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
break
;
break
;
default
:
default
:
data
=
EncodeData
(
args
.
Response
HtmlBody
,
args
.
Encoding
);
data
=
EncodeData
(
args
.
Response
Body
,
args
.
Response
Encoding
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseStatus
(
args
.
ServerResponse
.
ProtocolVersion
,
args
.
ServerResponse
.
StatusCode
,
args
.
ServerResponse
.
StatusDescription
,
args
.
ClientStreamWriter
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
,
data
.
Length
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
SendData
(
args
.
ClientStream
,
data
,
isChunked
);
...
@@ -86,9 +84,9 @@ namespace Titanium.Web.Proxy
...
@@ -86,9 +84,9 @@ namespace Titanium.Web.Proxy
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
);
WriteResponseHeaders
(
args
.
ClientStreamWriter
,
responseHeaders
);
if
(
isChunked
)
if
(
isChunked
)
SendChunked
(
args
.
Server
ResponseStream
,
args
.
ClientStream
);
SendChunked
(
args
.
ResponseStream
,
args
.
ClientStream
);
else
else
SendNormal
(
args
.
Server
ResponseStream
,
args
.
ClientStream
);
SendNormal
(
args
.
ResponseStream
,
args
.
ClientStream
);
}
}
...
...
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
View file @
11563d36
...
@@ -76,6 +76,8 @@
...
@@ -76,6 +76,8 @@
<Reference
Include=
"System.Xml"
/>
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Compile
Include=
"Exceptions\BodyNotFoundException.cs"
/>
<Compile
Include=
"Extensions\HttpWebRequestExtensions.cs"
/>
<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"
/>
...
...
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