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
2d85486e
Commit
2d85486e
authored
Jul 11, 2016
by
justcoding121
Committed by
GitHub
Jul 11, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #94 from justcoding121/master
Sync with master
parents
a954c575
216b8c90
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
58 additions
and
40 deletions
+58
-40
default.ps1
.build/default.ps1
+1
-1
README.md
README.md
+19
-16
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+18
-15
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+3
-1
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+1
-1
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+16
-6
No files found.
.build/default.ps1
View file @
2d85486e
...
@@ -56,7 +56,7 @@ Task Restore-Packages {
...
@@ -56,7 +56,7 @@ Task Restore-Packages {
}
}
Task Install-MSBuild
{
Task Install-MSBuild
{
if
(!(
Test-Path
"
${
env
:ProgramFiles
(x86)
}
\MSBuild\1
2
.0\Bin\msbuild.exe"
))
if
(!(
Test-Path
"
${
env
:ProgramFiles
(x86)
}
\MSBuild\1
4
.0\Bin\msbuild.exe"
))
{
{
cinst microsoft-build-tools -y
cinst microsoft-build-tools -y
}
}
...
...
README.md
View file @
2d85486e
...
@@ -35,12 +35,12 @@ After installing nuget package mark following files to be copied to app director
...
@@ -35,12 +35,12 @@ After installing nuget package mark following files to be copied to app director
Setup HTTP proxy:
Setup HTTP proxy:
```
csharp
```
csharp
var
P
roxyServer
=
new
ProxyServer
();
var
p
roxyServer
=
new
ProxyServer
();
P
roxyServer
.
BeforeRequest
+=
OnRequest
;
p
roxyServer
.
BeforeRequest
+=
OnRequest
;
P
roxyServer
.
BeforeResponse
+=
OnResponse
;
p
roxyServer
.
BeforeResponse
+=
OnResponse
;
P
roxyServer
.
ServerCertificateValidationCallback
+=
OnCertificateValidation
;
p
roxyServer
.
ServerCertificateValidationCallback
+=
OnCertificateValidation
;
P
roxyServer
.
ClientCertificateSelectionCallback
+=
OnCertificateSelection
;
p
roxyServer
.
ClientCertificateSelectionCallback
+=
OnCertificateSelection
;
//Exclude Https addresses you don't want to proxy
//Exclude Https addresses you don't want to proxy
...
@@ -53,8 +53,8 @@ Setup HTTP proxy:
...
@@ -53,8 +53,8 @@ Setup HTTP proxy:
//An explicit endpoint is where the client knows about the existance of a proxy
//An explicit endpoint is where the client knows about the existance of a proxy
//So client sends request in a proxy friendly manner
//So client sends request in a proxy friendly manner
P
roxyServer
.
AddEndPoint
(
explicitEndPoint
);
p
roxyServer
.
AddEndPoint
(
explicitEndPoint
);
P
roxyServer
.
Start
();
p
roxyServer
.
Start
();
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
...
@@ -67,26 +67,29 @@ Setup HTTP proxy:
...
@@ -67,26 +67,29 @@ Setup HTTP proxy:
{
{
GenericCertificateName
=
"google.com"
GenericCertificateName
=
"google.com"
};
};
P
roxyServer
.
AddEndPoint
(
transparentEndPoint
);
p
roxyServer
.
AddEndPoint
(
transparentEndPoint
);
//
P
roxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
//
p
roxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
//
P
roxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
//
p
roxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
foreach
(
var
endPoint
in
P
roxyServer
.
ProxyEndPoints
)
foreach
(
var
endPoint
in
p
roxyServer
.
ProxyEndPoints
)
Console
.
WriteLine
(
"Listening on '{0}' endpoint at Ip {1} and port: {2} "
,
Console
.
WriteLine
(
"Listening on '{0}' endpoint at Ip {1} and port: {2} "
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
//Only explicit proxies can be set as system proxy!
//Only explicit proxies can be set as system proxy!
P
roxyServer
.
SetAsSystemHttpProxy
(
explicitEndPoint
);
p
roxyServer
.
SetAsSystemHttpProxy
(
explicitEndPoint
);
P
roxyServer
.
SetAsSystemHttpsProxy
(
explicitEndPoint
);
p
roxyServer
.
SetAsSystemHttpsProxy
(
explicitEndPoint
);
//wait here (You can use something else as a wait function, I am using this as a demo)
//wait here (You can use something else as a wait function, I am using this as a demo)
Console
.
Read
();
Console
.
Read
();
//Unsubscribe & Quit
//Unsubscribe & Quit
ProxyServer
.
BeforeRequest
-=
OnRequest
;
proxyServer
.
BeforeRequest
-=
OnRequest
;
ProxyServer
.
BeforeResponse
-=
OnResponse
;
proxyServer
.
BeforeResponse
-=
OnResponse
;
ProxyServer
.
Stop
();
proxyServer
.
ServerCertificateValidationCallback
-=
OnCertificateValidation
;
proxyServer
.
ClientCertificateSelectionCallback
-=
OnCertificateSelection
;
proxyServer
.
Stop
();
```
```
Sample request and response event handlers
Sample request and response event handlers
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
2d85486e
...
@@ -62,8 +62,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -62,8 +62,7 @@ namespace Titanium.Web.Proxy.EventArguments
ProxyClient
=
new
ProxyClient
();
ProxyClient
=
new
ProxyClient
();
WebSession
=
new
HttpWebClient
();
WebSession
=
new
HttpWebClient
();
}
}
/// <summary>
/// <summary>
/// Read request body content as bytes[] for current session
/// Read request body content as bytes[] for current session
/// </summary>
/// </summary>
...
@@ -98,7 +97,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -98,7 +97,7 @@ namespace Titanium.Web.Proxy.EventArguments
await
this
.
ProxyClient
.
ClientStreamReader
.
CopyBytesToStream
(
bufferSize
,
requestBodyStream
,
WebSession
.
Request
.
ContentLength
);
await
this
.
ProxyClient
.
ClientStreamReader
.
CopyBytesToStream
(
bufferSize
,
requestBodyStream
,
WebSession
.
Request
.
ContentLength
);
}
}
else
if
(
WebSession
.
Request
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Request
.
HttpVersion
.
Minor
==
0
)
else
if
(
WebSession
.
Request
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Request
.
HttpVersion
.
Minor
==
0
)
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
requestBodyStream
,
long
.
MaxValue
);
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
requestBodyStream
,
long
.
MaxValue
);
}
}
WebSession
.
Request
.
RequestBody
=
await
GetDecompressedResponseBody
(
WebSession
.
Request
.
ContentEncoding
,
requestBodyStream
.
ToArray
());
WebSession
.
Request
.
RequestBody
=
await
GetDecompressedResponseBody
(
WebSession
.
Request
.
ContentEncoding
,
requestBodyStream
.
ToArray
());
...
@@ -108,7 +107,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -108,7 +107,7 @@ namespace Titanium.Web.Proxy.EventArguments
//So that next time we can deliver body from cache
//So that next time we can deliver body from cache
WebSession
.
Request
.
RequestBodyRead
=
true
;
WebSession
.
Request
.
RequestBodyRead
=
true
;
}
}
}
}
/// <summary>
/// <summary>
...
@@ -134,7 +133,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -134,7 +133,7 @@ namespace Titanium.Web.Proxy.EventArguments
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
responseBodyStream
,
WebSession
.
Response
.
ContentLength
);
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
responseBodyStream
,
WebSession
.
Response
.
ContentLength
);
}
}
else
if
(
WebSession
.
Response
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Response
.
HttpVersion
.
Minor
==
0
)
else
if
(
WebSession
.
Response
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Response
.
HttpVersion
.
Minor
==
0
)
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
responseBodyStream
,
long
.
MaxValue
);
await
WebSession
.
ServerConnection
.
StreamReader
.
CopyBytesToStream
(
bufferSize
,
responseBodyStream
,
long
.
MaxValue
);
}
}
...
@@ -152,10 +151,13 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -152,10 +151,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// <returns></returns>
/// <returns></returns>
public
async
Task
<
byte
[
]>
GetRequestBody
()
public
async
Task
<
byte
[
]>
GetRequestBody
()
{
{
if
(
WebSession
.
Request
.
RequestLocked
)
if
(!
WebSession
.
Request
.
RequestBodyRead
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
{
if
(
WebSession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
await
ReadRequestBody
();
await
ReadRequestBody
();
}
return
WebSession
.
Request
.
RequestBody
;
return
WebSession
.
Request
.
RequestBody
;
}
}
/// <summary>
/// <summary>
...
@@ -164,12 +166,13 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -164,12 +166,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// <returns></returns>
/// <returns></returns>
public
async
Task
<
string
>
GetRequestBodyAsString
()
public
async
Task
<
string
>
GetRequestBodyAsString
()
{
{
if
(
WebSession
.
Request
.
RequestLocked
)
if
(!
WebSession
.
Request
.
RequestBodyRead
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
{
if
(
WebSession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
await
ReadRequestBody
();
await
ReadRequestBody
();
}
//Use the encoding specified in request to decode the byte[] data to string
//Use the encoding specified in request to decode the byte[] data to string
return
WebSession
.
Request
.
RequestBodyString
??
(
WebSession
.
Request
.
RequestBodyString
=
WebSession
.
Request
.
Encoding
.
GetString
(
WebSession
.
Request
.
RequestBody
));
return
WebSession
.
Request
.
RequestBodyString
??
(
WebSession
.
Request
.
RequestBodyString
=
WebSession
.
Request
.
Encoding
.
GetString
(
WebSession
.
Request
.
RequestBody
));
}
}
...
@@ -285,7 +288,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -285,7 +288,7 @@ namespace Titanium.Web.Proxy.EventArguments
var
bodyBytes
=
WebSession
.
Response
.
Encoding
.
GetBytes
(
body
);
var
bodyBytes
=
WebSession
.
Response
.
Encoding
.
GetBytes
(
body
);
await
SetResponseBody
(
bodyBytes
);
await
SetResponseBody
(
bodyBytes
);
}
}
private
async
Task
<
byte
[
]>
GetDecompressedResponseBody
(
string
encodingType
,
byte
[]
responseBodyStream
)
private
async
Task
<
byte
[
]>
GetDecompressedResponseBody
(
string
encodingType
,
byte
[]
responseBodyStream
)
{
{
...
@@ -345,7 +348,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -345,7 +348,7 @@ namespace Titanium.Web.Proxy.EventArguments
WebSession
.
Request
.
CancelRequest
=
true
;
WebSession
.
Request
.
CancelRequest
=
true
;
}
}
/// a generic responder method
/// a generic responder method
public
async
Task
Respond
(
Response
response
)
public
async
Task
Respond
(
Response
response
)
{
{
...
...
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
2d85486e
...
@@ -115,8 +115,10 @@ namespace Titanium.Web.Proxy.Http
...
@@ -115,8 +115,10 @@ namespace Titanium.Web.Proxy.Http
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
{
{
await
ServerConnection
.
StreamReader
.
ReadLineAsync
();
//Empty content in first-line, try again
httpResult
=
(
await
ServerConnection
.
StreamReader
.
ReadLineAsync
()).
Split
(
ProxyConstants
.
SpaceSplit
,
3
);
}
}
var
httpVersion
=
httpResult
[
0
].
Trim
().
ToLower
();
var
httpVersion
=
httpResult
[
0
].
Trim
().
ToLower
();
var
version
=
new
Version
(
1
,
1
);
var
version
=
new
Version
(
1
,
1
);
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
2d85486e
...
@@ -186,7 +186,7 @@ namespace Titanium.Web.Proxy
...
@@ -186,7 +186,7 @@ namespace Titanium.Web.Proxy
#if !DEBUG
#if !DEBUG
firefoxProxySettingsManager
.
AddFirefox
();
firefoxProxySettingsManager
.
AddFirefox
();
#endif
#endif
Console
.
WriteLine
(
"Set endpoint at Ip {1} and port: {2} as System HTTP
S
Proxy"
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
Console
.
WriteLine
(
"Set endpoint at Ip {1} and port: {2} as System HTTP Proxy"
,
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
}
}
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
2d85486e
...
@@ -181,14 +181,28 @@ namespace Titanium.Web.Proxy
...
@@ -181,14 +181,28 @@ namespace Titanium.Web.Proxy
/// <summary>
/// <summary>
/// Handle dispose of a client/server session
/// Handle dispose of a client/server session
/// </summary>
/// </summary>
/// <param name="
c
lient"></param>
/// <param name="
tcpC
lient"></param>
/// <param name="clientStream"></param>
/// <param name="clientStream"></param>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamWriter"></param>
/// <param name="clientStreamWriter"></param>
/// <param name="args"></param>
/// <param name="args"></param>
private
void
Dispose
(
TcpClient
c
lient
,
IDisposable
clientStream
,
IDisposable
clientStreamReader
,
private
void
Dispose
(
TcpClient
tcpC
lient
,
IDisposable
clientStream
,
IDisposable
clientStreamReader
,
IDisposable
clientStreamWriter
,
IDisposable
args
)
IDisposable
clientStreamWriter
,
IDisposable
args
)
{
{
if
(
clientStream
!=
null
)
{
(
clientStream
as
Stream
).
Close
();
(
clientStream
as
Stream
).
Dispose
();
}
if
(
tcpClient
!=
null
)
{
tcpClient
.
Client
.
Close
();
tcpClient
.
Close
();
tcpClient
.
Client
.
Dispose
();
}
if
(
args
!=
null
)
if
(
args
!=
null
)
args
.
Dispose
();
args
.
Dispose
();
...
@@ -198,11 +212,7 @@ namespace Titanium.Web.Proxy
...
@@ -198,11 +212,7 @@ namespace Titanium.Web.Proxy
if
(
clientStreamWriter
!=
null
)
if
(
clientStreamWriter
!=
null
)
clientStreamWriter
.
Dispose
();
clientStreamWriter
.
Dispose
();
if
(
clientStream
!=
null
)
clientStream
.
Dispose
();
if
(
client
!=
null
)
client
.
Close
();
}
}
}
}
}
}
\ No newline at end of file
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