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
a7cab15f
Commit
a7cab15f
authored
Nov 20, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#519 handle change in hostname on ReRequest
parent
8311e6b5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
207 additions
and
167 deletions
+207
-167
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+3
-3
MainWindow.xaml.cs
examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
+13
-13
SessionEventArgs.cs
src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+41
-41
SessionEventArgsBase.cs
...Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
+15
-11
TunnelConnectEventArgs.cs
...tanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
+1
-1
ExplicitClientHandler.cs
src/Titanium.Web.Proxy/ExplicitClientHandler.cs
+8
-8
HttpWebClient.cs
src/Titanium.Web.Proxy/Http/HttpWebClient.cs
+17
-17
ProxyClient.cs
src/Titanium.Web.Proxy/Network/ProxyClient.cs
+1
-1
TcpConnectionFactory.cs
src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+7
-7
ProxyAuthorizationHandler.cs
src/Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
+8
-8
ProxyServer.cs
src/Titanium.Web.Proxy/ProxyServer.cs
+1
-1
RequestHandler.cs
src/Titanium.Web.Proxy/RequestHandler.cs
+73
-38
ResponseHandler.cs
src/Titanium.Web.Proxy/ResponseHandler.cs
+8
-7
WebSocketHandler.cs
src/Titanium.Web.Proxy/WebSocketHandler.cs
+1
-1
WinAuthHandler.cs
src/Titanium.Web.Proxy/WinAuthHandler.cs
+8
-8
InterceptionTests.cs
.../Titanium.Web.Proxy.IntegrationTests/InterceptionTests.cs
+2
-2
No files found.
examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
a7cab15f
...
@@ -118,7 +118,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -118,7 +118,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
private
async
Task
OnBeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
private
async
Task
OnBeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
{
{
string
hostname
=
e
.
WebSession
.
Request
.
RequestUri
.
Host
;
string
hostname
=
e
.
HttpClient
.
Request
.
RequestUri
.
Host
;
await
WriteToConsole
(
"Tunnel to: "
+
hostname
);
await
WriteToConsole
(
"Tunnel to: "
+
hostname
);
if
(
hostname
.
Contains
(
"dropbox.com"
))
if
(
hostname
.
Contains
(
"dropbox.com"
))
...
@@ -139,7 +139,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -139,7 +139,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
private
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
private
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
{
{
await
WriteToConsole
(
"Active Client Connections:"
+
((
ProxyServer
)
sender
).
ClientConnectionCount
);
await
WriteToConsole
(
"Active Client Connections:"
+
((
ProxyServer
)
sender
).
ClientConnectionCount
);
await
WriteToConsole
(
e
.
WebSession
.
Request
.
Url
);
await
WriteToConsole
(
e
.
HttpClient
.
Request
.
Url
);
// store it in the UserData property
// store it in the UserData property
// It can be a simple integer, Guid, or any type
// It can be a simple integer, Guid, or any type
...
@@ -191,7 +191,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -191,7 +191,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
{
{
await
WriteToConsole
(
"Active Server Connections:"
+
((
ProxyServer
)
sender
).
ServerConnectionCount
);
await
WriteToConsole
(
"Active Server Connections:"
+
((
ProxyServer
)
sender
).
ServerConnectionCount
);
string
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsolutePath
);
string
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
HttpClient
.
Request
.
RequestUri
.
AbsolutePath
);
//access user data set in request to do something with it
//access user data set in request to do something with it
//var userData = e.WebSession.UserData as CustomUserData;
//var userData = e.WebSession.UserData as CustomUserData;
...
...
examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
View file @
a7cab15f
...
@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
private
async
Task
ProxyServer_BeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
private
async
Task
ProxyServer_BeforeTunnelConnectRequest
(
object
sender
,
TunnelConnectSessionEventArgs
e
)
{
{
string
hostname
=
e
.
WebSession
.
Request
.
RequestUri
.
Host
;
string
hostname
=
e
.
HttpClient
.
Request
.
RequestUri
.
Host
;
if
(
hostname
.
EndsWith
(
"webex.com"
))
if
(
hostname
.
EndsWith
(
"webex.com"
))
{
{
e
.
DecryptSsl
=
false
;
e
.
DecryptSsl
=
false
;
...
@@ -135,7 +135,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -135,7 +135,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
{
{
await
Dispatcher
.
InvokeAsync
(()
=>
await
Dispatcher
.
InvokeAsync
(()
=>
{
{
if
(
sessionDictionary
.
TryGetValue
(
e
.
WebSession
,
out
var
item
))
if
(
sessionDictionary
.
TryGetValue
(
e
.
HttpClient
,
out
var
item
))
{
{
item
.
Update
();
item
.
Update
();
}
}
...
@@ -147,9 +147,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -147,9 +147,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
SessionListItem
item
=
null
;
SessionListItem
item
=
null
;
await
Dispatcher
.
InvokeAsync
(()
=>
{
item
=
AddSession
(
e
);
});
await
Dispatcher
.
InvokeAsync
(()
=>
{
item
=
AddSession
(
e
);
});
if
(
e
.
WebSession
.
Request
.
HasBody
)
if
(
e
.
HttpClient
.
Request
.
HasBody
)
{
{
e
.
WebSession
.
Request
.
KeepBody
=
true
;
e
.
HttpClient
.
Request
.
KeepBody
=
true
;
await
e
.
GetRequestBody
();
await
e
.
GetRequestBody
();
}
}
}
}
...
@@ -159,7 +159,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -159,7 +159,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
SessionListItem
item
=
null
;
SessionListItem
item
=
null
;
await
Dispatcher
.
InvokeAsync
(()
=>
await
Dispatcher
.
InvokeAsync
(()
=>
{
{
if
(
sessionDictionary
.
TryGetValue
(
e
.
WebSession
,
out
item
))
if
(
sessionDictionary
.
TryGetValue
(
e
.
HttpClient
,
out
item
))
{
{
item
.
Update
();
item
.
Update
();
}
}
...
@@ -167,9 +167,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -167,9 +167,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
if
(
item
!=
null
)
if
(
item
!=
null
)
{
{
if
(
e
.
WebSession
.
Response
.
HasBody
)
if
(
e
.
HttpClient
.
Response
.
HasBody
)
{
{
e
.
WebSession
.
Response
.
KeepBody
=
true
;
e
.
HttpClient
.
Response
.
KeepBody
=
true
;
await
e
.
GetResponseBody
();
await
e
.
GetResponseBody
();
await
Dispatcher
.
InvokeAsync
(()
=>
{
item
.
Update
();
});
await
Dispatcher
.
InvokeAsync
(()
=>
{
item
.
Update
();
});
...
@@ -181,7 +181,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -181,7 +181,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
{
{
await
Dispatcher
.
InvokeAsync
(()
=>
await
Dispatcher
.
InvokeAsync
(()
=>
{
{
if
(
sessionDictionary
.
TryGetValue
(
e
.
WebSession
,
out
var
item
))
if
(
sessionDictionary
.
TryGetValue
(
e
.
HttpClient
,
out
var
item
))
{
{
item
.
Exception
=
e
.
Exception
;
item
.
Exception
=
e
.
Exception
;
}
}
...
@@ -192,7 +192,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -192,7 +192,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
{
{
var
item
=
CreateSessionListItem
(
e
);
var
item
=
CreateSessionListItem
(
e
);
Sessions
.
Add
(
item
);
Sessions
.
Add
(
item
);
sessionDictionary
.
Add
(
e
.
WebSession
,
item
);
sessionDictionary
.
Add
(
e
.
HttpClient
,
item
);
return
item
;
return
item
;
}
}
...
@@ -203,16 +203,16 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -203,16 +203,16 @@ namespace Titanium.Web.Proxy.Examples.Wpf
var
item
=
new
SessionListItem
var
item
=
new
SessionListItem
{
{
Number
=
lastSessionNumber
,
Number
=
lastSessionNumber
,
WebSession
=
e
.
WebSession
,
WebSession
=
e
.
HttpClient
,
IsTunnelConnect
=
isTunnelConnect
IsTunnelConnect
=
isTunnelConnect
};
};
if
(
isTunnelConnect
||
e
.
WebSession
.
Request
.
UpgradeToWebSocket
)
if
(
isTunnelConnect
||
e
.
HttpClient
.
Request
.
UpgradeToWebSocket
)
{
{
e
.
DataReceived
+=
(
sender
,
args
)
=>
e
.
DataReceived
+=
(
sender
,
args
)
=>
{
{
var
session
=
(
SessionEventArgs
)
sender
;
var
session
=
(
SessionEventArgs
)
sender
;
if
(
sessionDictionary
.
TryGetValue
(
session
.
WebSession
,
out
var
li
))
if
(
sessionDictionary
.
TryGetValue
(
session
.
HttpClient
,
out
var
li
))
{
{
li
.
ReceivedDataCount
+=
args
.
Count
;
li
.
ReceivedDataCount
+=
args
.
Count
;
}
}
...
@@ -221,7 +221,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
...
@@ -221,7 +221,7 @@ namespace Titanium.Web.Proxy.Examples.Wpf
e
.
DataSent
+=
(
sender
,
args
)
=>
e
.
DataSent
+=
(
sender
,
args
)
=>
{
{
var
session
=
(
SessionEventArgs
)
sender
;
var
session
=
(
SessionEventArgs
)
sender
;
if
(
sessionDictionary
.
TryGetValue
(
session
.
WebSession
,
out
var
li
))
if
(
sessionDictionary
.
TryGetValue
(
session
.
HttpClient
,
out
var
li
))
{
{
li
.
SentDataCount
+=
args
.
Count
;
li
.
SentDataCount
+=
args
.
Count
;
}
}
...
...
src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
a7cab15f
This diff is collapsed.
Click to expand it.
src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
View file @
a7cab15f
...
@@ -8,6 +8,7 @@ using Titanium.Web.Proxy.Helpers;
...
@@ -8,6 +8,7 @@ using Titanium.Web.Proxy.Helpers;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network
;
using
Titanium.Web.Proxy.Network
;
using
Titanium.Web.Proxy.Network.Tcp
;
namespace
Titanium.Web.Proxy.EventArguments
namespace
Titanium.Web.Proxy.EventArguments
{
{
...
@@ -19,8 +20,9 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -19,8 +20,9 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
/// </summary>
public
abstract
class
SessionEventArgsBase
:
EventArgs
,
IDisposable
public
abstract
class
SessionEventArgsBase
:
EventArgs
,
IDisposable
{
{
internal
readonly
CancellationTokenSource
CancellationTokenSource
;
internal
readonly
CancellationTokenSource
CancellationTokenSource
;
internal
TcpServerConnection
ServerConnection
=>
HttpClient
.
Connection
;
internal
TcpClientConnection
ClientConnection
=>
ProxyClient
.
Connection
;
protected
readonly
int
bufferSize
;
protected
readonly
int
bufferSize
;
protected
readonly
IBufferPool
bufferPool
;
protected
readonly
IBufferPool
bufferPool
;
...
@@ -50,10 +52,10 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -50,10 +52,10 @@ namespace Titanium.Web.Proxy.EventArguments
CancellationTokenSource
=
cancellationTokenSource
;
CancellationTokenSource
=
cancellationTokenSource
;
ProxyClient
=
new
ProxyClient
();
ProxyClient
=
new
ProxyClient
();
WebSession
=
new
HttpWebClient
(
request
);
HttpClient
=
new
HttpWebClient
(
request
);
LocalEndPoint
=
endPoint
;
LocalEndPoint
=
endPoint
;
WebSession
.
ProcessId
=
new
Lazy
<
int
>(()
=>
HttpClient
.
ProcessId
=
new
Lazy
<
int
>(()
=>
{
{
if
(
RunTime
.
IsWindows
)
if
(
RunTime
.
IsWindows
)
{
{
...
@@ -85,25 +87,27 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -85,25 +87,27 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
/// </summary>
public
object
UserData
public
object
UserData
{
{
get
=>
WebSession
.
UserData
;
get
=>
HttpClient
.
UserData
;
set
=>
WebSession
.
UserData
=
value
;
set
=>
HttpClient
.
UserData
=
value
;
}
}
/// <summary>
/// <summary>
/// Does this session uses SSL?
/// Does this session uses SSL?
/// </summary>
/// </summary>
public
bool
IsHttps
=>
WebSession
.
Request
.
IsHttps
;
public
bool
IsHttps
=>
HttpClient
.
Request
.
IsHttps
;
/// <summary>
/// <summary>
/// Client End Point.
/// Client End Point.
/// </summary>
/// </summary>
public
IPEndPoint
ClientEndPoint
=>
(
IPEndPoint
)
ProxyClient
.
C
lientC
onnection
.
RemoteEndPoint
;
public
IPEndPoint
ClientEndPoint
=>
(
IPEndPoint
)
ProxyClient
.
Connection
.
RemoteEndPoint
;
/// <summary>
/// <summary>
/// A web session corresponding to a single request/response sequence
/// The web client used to communicate with server for this session.
/// within a proxy connection.
/// </summary>
/// </summary>
public
HttpWebClient
WebSession
{
get
;
}
public
HttpWebClient
HttpClient
{
get
;
}
[
Obsolete
(
"Use HttpClient instead."
)]
public
HttpWebClient
WebSession
=>
HttpClient
;
/// <summary>
/// <summary>
/// Are we using a custom upstream HTTP(S) proxy?
/// Are we using a custom upstream HTTP(S) proxy?
...
@@ -136,7 +140,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -136,7 +140,7 @@ namespace Titanium.Web.Proxy.EventArguments
DataReceived
=
null
;
DataReceived
=
null
;
Exception
=
null
;
Exception
=
null
;
WebSession
.
FinishSession
();
HttpClient
.
FinishSession
();
}
}
/// <summary>
/// <summary>
...
...
src/Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
View file @
a7cab15f
...
@@ -16,7 +16,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -16,7 +16,7 @@ namespace Titanium.Web.Proxy.EventArguments
CancellationTokenSource
cancellationTokenSource
)
CancellationTokenSource
cancellationTokenSource
)
:
base
(
server
,
endPoint
,
cancellationTokenSource
,
connectRequest
)
:
base
(
server
,
endPoint
,
cancellationTokenSource
,
connectRequest
)
{
{
WebSession
.
ConnectRequest
=
connectRequest
;
HttpClient
.
ConnectRequest
=
connectRequest
;
}
}
/// <summary>
/// <summary>
...
...
src/Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
a7cab15f
...
@@ -73,7 +73,7 @@ namespace Titanium.Web.Proxy
...
@@ -73,7 +73,7 @@ namespace Titanium.Web.Proxy
connectArgs
=
new
TunnelConnectSessionEventArgs
(
this
,
endPoint
,
connectRequest
,
connectArgs
=
new
TunnelConnectSessionEventArgs
(
this
,
endPoint
,
connectRequest
,
cancellationTokenSource
);
cancellationTokenSource
);
connectArgs
.
ProxyClient
.
C
lientC
onnection
=
clientConnection
;
connectArgs
.
ProxyClient
.
Connection
=
clientConnection
;
connectArgs
.
ProxyClient
.
ClientStream
=
clientStream
;
connectArgs
.
ProxyClient
.
ClientStream
=
clientStream
;
await
endPoint
.
InvokeBeforeTunnelConnectRequest
(
this
,
connectArgs
,
ExceptionFunc
);
await
endPoint
.
InvokeBeforeTunnelConnectRequest
(
this
,
connectArgs
,
ExceptionFunc
);
...
@@ -83,9 +83,9 @@ namespace Titanium.Web.Proxy
...
@@ -83,9 +83,9 @@ namespace Titanium.Web.Proxy
if
(
connectArgs
.
DenyConnect
)
if
(
connectArgs
.
DenyConnect
)
{
{
if
(
connectArgs
.
WebSession
.
Response
.
StatusCode
==
0
)
if
(
connectArgs
.
HttpClient
.
Response
.
StatusCode
==
0
)
{
{
connectArgs
.
WebSession
.
Response
=
new
Response
connectArgs
.
HttpClient
.
Response
=
new
Response
{
{
HttpVersion
=
HttpHeader
.
Version11
,
HttpVersion
=
HttpHeader
.
Version11
,
StatusCode
=
(
int
)
HttpStatusCode
.
Forbidden
,
StatusCode
=
(
int
)
HttpStatusCode
.
Forbidden
,
...
@@ -94,7 +94,7 @@ namespace Titanium.Web.Proxy
...
@@ -94,7 +94,7 @@ namespace Titanium.Web.Proxy
}
}
// send the response
// send the response
await
clientStreamWriter
.
WriteResponseAsync
(
connectArgs
.
WebSession
.
Response
,
await
clientStreamWriter
.
WriteResponseAsync
(
connectArgs
.
HttpClient
.
Response
,
cancellationToken
:
cancellationToken
);
cancellationToken
:
cancellationToken
);
return
;
return
;
}
}
...
@@ -104,7 +104,7 @@ namespace Titanium.Web.Proxy
...
@@ -104,7 +104,7 @@ namespace Titanium.Web.Proxy
await
endPoint
.
InvokeBeforeTunnectConnectResponse
(
this
,
connectArgs
,
ExceptionFunc
);
await
endPoint
.
InvokeBeforeTunnectConnectResponse
(
this
,
connectArgs
,
ExceptionFunc
);
// send the response
// send the response
await
clientStreamWriter
.
WriteResponseAsync
(
connectArgs
.
WebSession
.
Response
,
await
clientStreamWriter
.
WriteResponseAsync
(
connectArgs
.
HttpClient
.
Response
,
cancellationToken
:
cancellationToken
);
cancellationToken
:
cancellationToken
);
return
;
return
;
}
}
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
// Set ContentLength explicitly to properly handle HTTP 1.0
// Set ContentLength explicitly to properly handle HTTP 1.0
response
.
ContentLength
=
0
;
response
.
ContentLength
=
0
;
response
.
Headers
.
FixProxyHeaders
();
response
.
Headers
.
FixProxyHeaders
();
connectArgs
.
WebSession
.
Response
=
response
;
connectArgs
.
HttpClient
.
Response
=
response
;
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
...
@@ -252,7 +252,7 @@ namespace Titanium.Web.Proxy
...
@@ -252,7 +252,7 @@ namespace Titanium.Web.Proxy
}
}
var
serverHelloInfo
=
await
SslTools
.
PeekServerHello
(
connection
.
Stream
,
BufferPool
,
cancellationToken
);
var
serverHelloInfo
=
await
SslTools
.
PeekServerHello
(
connection
.
Stream
,
BufferPool
,
cancellationToken
);
((
ConnectResponse
)
connectArgs
.
WebSession
.
Response
).
ServerHelloInfo
=
serverHelloInfo
;
((
ConnectResponse
)
connectArgs
.
HttpClient
.
Response
).
ServerHelloInfo
=
serverHelloInfo
;
}
}
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferPool
,
BufferSize
,
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferPool
,
BufferSize
,
...
@@ -321,7 +321,7 @@ namespace Titanium.Web.Proxy
...
@@ -321,7 +321,7 @@ namespace Titanium.Web.Proxy
calledRequestHandler
=
true
;
calledRequestHandler
=
true
;
// Now create the request
// Now create the request
await
handleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
await
handleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
,
prefetchConnectionTask
);
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
HttpClient
.
ConnectRequest
,
prefetchConnectionTask
);
}
}
catch
(
ProxyException
e
)
catch
(
ProxyException
e
)
{
{
...
...
src/Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
a7cab15f
...
@@ -17,16 +17,16 @@ namespace Titanium.Web.Proxy.Http
...
@@ -17,16 +17,16 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
/// </summary>
public
class
HttpWebClient
public
class
HttpWebClient
{
{
internal
HttpWebClient
(
Request
request
=
null
,
Response
response
=
null
)
internal
HttpWebClient
(
Request
request
)
{
{
Request
=
request
??
new
Request
();
Request
=
request
??
new
Request
();
Response
=
response
??
new
Response
();
Response
=
new
Response
();
}
}
/// <summary>
/// <summary>
/// Connection to server
/// Connection to server
/// </summary>
/// </summary>
internal
TcpServerConnection
Server
Connection
{
get
;
set
;
}
internal
TcpServerConnection
Connection
{
get
;
set
;
}
/// <summary>
/// <summary>
/// Should we close the server connection at the end of this HTTP request/response session.
/// Should we close the server connection at the end of this HTTP request/response session.
...
@@ -81,7 +81,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -81,7 +81,7 @@ namespace Titanium.Web.Proxy.Http
internal
void
SetConnection
(
TcpServerConnection
serverConnection
)
internal
void
SetConnection
(
TcpServerConnection
serverConnection
)
{
{
serverConnection
.
LastAccess
=
DateTime
.
Now
;
serverConnection
.
LastAccess
=
DateTime
.
Now
;
Server
Connection
=
serverConnection
;
Connection
=
serverConnection
;
}
}
/// <summary>
/// <summary>
...
@@ -91,11 +91,11 @@ namespace Titanium.Web.Proxy.Http
...
@@ -91,11 +91,11 @@ namespace Titanium.Web.Proxy.Http
internal
async
Task
SendRequest
(
bool
enable100ContinueBehaviour
,
bool
isTransparent
,
internal
async
Task
SendRequest
(
bool
enable100ContinueBehaviour
,
bool
isTransparent
,
CancellationToken
cancellationToken
)
CancellationToken
cancellationToken
)
{
{
var
upstreamProxy
=
Server
Connection
.
UpStreamProxy
;
var
upstreamProxy
=
Connection
.
UpStreamProxy
;
bool
useUpstreamProxy
=
upstreamProxy
!=
null
&&
Server
Connection
.
IsHttps
==
false
;
bool
useUpstreamProxy
=
upstreamProxy
!=
null
&&
Connection
.
IsHttps
==
false
;
var
writer
=
Server
Connection
.
StreamWriter
;
var
writer
=
Connection
.
StreamWriter
;
// prepare the request & headers
// prepare the request & headers
await
writer
.
WriteLineAsync
(
Request
.
CreateRequestLine
(
Request
.
Method
,
await
writer
.
WriteLineAsync
(
Request
.
CreateRequestLine
(
Request
.
Method
,
...
@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy.Http
// Send Authentication to Upstream proxy if needed
// Send Authentication to Upstream proxy if needed
if
(!
isTransparent
&&
upstreamProxy
!=
null
if
(!
isTransparent
&&
upstreamProxy
!=
null
&&
Server
Connection
.
IsHttps
==
false
&&
Connection
.
IsHttps
==
false
&&
!
string
.
IsNullOrEmpty
(
upstreamProxy
.
UserName
)
&&
!
string
.
IsNullOrEmpty
(
upstreamProxy
.
UserName
)
&&
upstreamProxy
.
Password
!=
null
)
&&
upstreamProxy
.
Password
!=
null
)
{
{
...
@@ -134,7 +134,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -134,7 +134,7 @@ namespace Titanium.Web.Proxy.Http
string
httpStatus
;
string
httpStatus
;
try
try
{
{
httpStatus
=
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
httpStatus
=
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
if
(
httpStatus
==
null
)
if
(
httpStatus
==
null
)
{
{
throw
new
ServerConnectionException
(
"Server connection was closed."
);
throw
new
ServerConnectionException
(
"Server connection was closed."
);
...
@@ -153,13 +153,13 @@ namespace Titanium.Web.Proxy.Http
...
@@ -153,13 +153,13 @@ namespace Titanium.Web.Proxy.Http
&&
responseStatusDescription
.
EqualsIgnoreCase
(
"continue"
))
&&
responseStatusDescription
.
EqualsIgnoreCase
(
"continue"
))
{
{
Request
.
Is100Continue
=
true
;
Request
.
Is100Continue
=
true
;
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
}
}
else
if
(
responseStatusCode
==
(
int
)
HttpStatusCode
.
ExpectationFailed
else
if
(
responseStatusCode
==
(
int
)
HttpStatusCode
.
ExpectationFailed
&&
responseStatusDescription
.
EqualsIgnoreCase
(
"expectation failed"
))
&&
responseStatusDescription
.
EqualsIgnoreCase
(
"expectation failed"
))
{
{
Request
.
ExpectationFailed
=
true
;
Request
.
ExpectationFailed
=
true
;
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
}
}
}
}
}
}
...
@@ -180,7 +180,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -180,7 +180,7 @@ namespace Titanium.Web.Proxy.Http
string
httpStatus
;
string
httpStatus
;
try
try
{
{
httpStatus
=
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
httpStatus
=
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
if
(
httpStatus
==
null
)
if
(
httpStatus
==
null
)
{
{
throw
new
ServerConnectionException
(
"Server connection was closed."
);
throw
new
ServerConnectionException
(
"Server connection was closed."
);
...
@@ -193,7 +193,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -193,7 +193,7 @@ namespace Titanium.Web.Proxy.Http
if
(
httpStatus
==
string
.
Empty
)
if
(
httpStatus
==
string
.
Empty
)
{
{
httpStatus
=
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
httpStatus
=
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
}
}
Response
.
ParseResponseLine
(
httpStatus
,
out
var
version
,
out
int
statusCode
,
out
string
statusDescription
);
Response
.
ParseResponseLine
(
httpStatus
,
out
var
version
,
out
int
statusCode
,
out
string
statusDescription
);
...
@@ -209,7 +209,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -209,7 +209,7 @@ namespace Titanium.Web.Proxy.Http
// Read the next line after 100-continue
// Read the next line after 100-continue
Response
.
Is100Continue
=
true
;
Response
.
Is100Continue
=
true
;
Response
.
StatusCode
=
0
;
Response
.
StatusCode
=
0
;
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
// now receive response
// now receive response
await
ReceiveResponse
(
cancellationToken
);
await
ReceiveResponse
(
cancellationToken
);
...
@@ -222,7 +222,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -222,7 +222,7 @@ namespace Titanium.Web.Proxy.Http
// read next line after expectation failed response
// read next line after expectation failed response
Response
.
ExpectationFailed
=
true
;
Response
.
ExpectationFailed
=
true
;
Response
.
StatusCode
=
0
;
Response
.
StatusCode
=
0
;
await
Server
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
await
Connection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
// now receive response
// now receive response
await
ReceiveResponse
(
cancellationToken
);
await
ReceiveResponse
(
cancellationToken
);
...
@@ -230,7 +230,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -230,7 +230,7 @@ namespace Titanium.Web.Proxy.Http
}
}
// Read the response headers in to unique and non-unique header collections
// Read the response headers in to unique and non-unique header collections
await
HeaderParser
.
ReadHeaders
(
Server
Connection
.
Stream
,
Response
.
Headers
,
cancellationToken
);
await
HeaderParser
.
ReadHeaders
(
Connection
.
Stream
,
Response
.
Headers
,
cancellationToken
);
}
}
/// <summary>
/// <summary>
...
@@ -238,7 +238,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -238,7 +238,7 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
/// </summary>
internal
void
FinishSession
()
internal
void
FinishSession
()
{
{
Server
Connection
=
null
;
Connection
=
null
;
ConnectRequest
?.
FinishSession
();
ConnectRequest
?.
FinishSession
();
Request
?.
FinishSession
();
Request
?.
FinishSession
();
...
...
src/Titanium.Web.Proxy/Network/ProxyClient.cs
View file @
a7cab15f
...
@@ -12,7 +12,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -12,7 +12,7 @@ namespace Titanium.Web.Proxy.Network
/// <summary>
/// <summary>
/// TcpClient connection used to communicate with client
/// TcpClient connection used to communicate with client
/// </summary>
/// </summary>
internal
TcpClientConnection
C
lientC
onnection
{
get
;
set
;
}
internal
TcpClientConnection
Connection
{
get
;
set
;
}
/// <summary>
/// <summary>
/// Holds the stream to client
/// Holds the stream to client
...
...
src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
a7cab15f
...
@@ -98,10 +98,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -98,10 +98,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
session
.
CustomUpStreamProxyUsed
=
customUpStreamProxy
;
session
.
CustomUpStreamProxyUsed
=
customUpStreamProxy
;
return
GetConnectionCacheKey
(
return
GetConnectionCacheKey
(
session
.
WebSession
.
Request
.
RequestUri
.
Host
,
session
.
HttpClient
.
Request
.
RequestUri
.
Host
,
session
.
WebSession
.
Request
.
RequestUri
.
Port
,
session
.
HttpClient
.
Request
.
RequestUri
.
Port
,
isHttps
,
applicationProtocols
,
isHttps
,
applicationProtocols
,
server
,
session
.
WebSession
.
UpStreamEndPoint
??
server
.
UpStreamEndPoint
,
server
,
session
.
HttpClient
.
UpStreamEndPoint
??
server
.
UpStreamEndPoint
,
customUpStreamProxy
??
(
isHttps
?
server
.
UpStreamHttpsProxy
:
server
.
UpStreamHttpProxy
));
customUpStreamProxy
??
(
isHttps
?
server
.
UpStreamHttpsProxy
:
server
.
UpStreamHttpProxy
));
}
}
...
@@ -148,11 +148,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -148,11 +148,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
session
.
CustomUpStreamProxyUsed
=
customUpStreamProxy
;
session
.
CustomUpStreamProxyUsed
=
customUpStreamProxy
;
return
await
GetServerConnection
(
return
await
GetServerConnection
(
session
.
WebSession
.
Request
.
RequestUri
.
Host
,
session
.
HttpClient
.
Request
.
RequestUri
.
Host
,
session
.
WebSession
.
Request
.
RequestUri
.
Port
,
session
.
HttpClient
.
Request
.
RequestUri
.
Port
,
session
.
WebSession
.
Request
.
HttpVersion
,
session
.
HttpClient
.
Request
.
HttpVersion
,
isHttps
,
applicationProtocols
,
isConnect
,
isHttps
,
applicationProtocols
,
isConnect
,
server
,
session
,
session
.
WebSession
.
UpStreamEndPoint
??
server
.
UpStreamEndPoint
,
server
,
session
,
session
.
HttpClient
.
UpStreamEndPoint
??
server
.
UpStreamEndPoint
,
customUpStreamProxy
??
(
isHttps
?
server
.
UpStreamHttpsProxy
:
server
.
UpStreamHttpProxy
),
customUpStreamProxy
??
(
isHttps
?
server
.
UpStreamHttpsProxy
:
server
.
UpStreamHttpProxy
),
noCache
,
cancellationToken
);
noCache
,
cancellationToken
);
}
}
...
...
src/Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
View file @
a7cab15f
...
@@ -26,14 +26,14 @@ namespace Titanium.Web.Proxy
...
@@ -26,14 +26,14 @@ namespace Titanium.Web.Proxy
return
true
;
return
true
;
}
}
var
httpHeaders
=
session
.
WebSession
.
Request
.
Headers
;
var
httpHeaders
=
session
.
HttpClient
.
Request
.
Headers
;
try
try
{
{
var
header
=
httpHeaders
.
GetFirstHeader
(
KnownHeaders
.
ProxyAuthorization
);
var
header
=
httpHeaders
.
GetFirstHeader
(
KnownHeaders
.
ProxyAuthorization
);
if
(
header
==
null
)
if
(
header
==
null
)
{
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Required"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Required"
);
return
false
;
return
false
;
}
}
...
@@ -42,7 +42,7 @@ namespace Titanium.Web.Proxy
...
@@ -42,7 +42,7 @@ namespace Titanium.Web.Proxy
if
(
headerValueParts
.
Length
!=
2
)
if
(
headerValueParts
.
Length
!=
2
)
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
...
@@ -57,7 +57,7 @@ namespace Titanium.Web.Proxy
...
@@ -57,7 +57,7 @@ namespace Titanium.Web.Proxy
if
(
result
.
Result
==
ProxyAuthenticationResult
.
ContinuationNeeded
)
if
(
result
.
Result
==
ProxyAuthenticationResult
.
ContinuationNeeded
)
{
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
,
result
.
Continuation
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
,
result
.
Continuation
);
return
false
;
return
false
;
}
}
...
@@ -73,7 +73,7 @@ namespace Titanium.Web.Proxy
...
@@ -73,7 +73,7 @@ namespace Titanium.Web.Proxy
httpHeaders
));
httpHeaders
));
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
}
}
...
@@ -83,7 +83,7 @@ namespace Titanium.Web.Proxy
...
@@ -83,7 +83,7 @@ namespace Titanium.Web.Proxy
if
(!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
if
(!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
...
@@ -92,7 +92,7 @@ namespace Titanium.Web.Proxy
...
@@ -92,7 +92,7 @@ namespace Titanium.Web.Proxy
if
(
colonIndex
==
-
1
)
if
(
colonIndex
==
-
1
)
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
...
@@ -101,7 +101,7 @@ namespace Titanium.Web.Proxy
...
@@ -101,7 +101,7 @@ namespace Titanium.Web.Proxy
bool
authenticated
=
await
ProxyBasicAuthenticateFunc
(
session
,
username
,
password
);
bool
authenticated
=
await
ProxyBasicAuthenticateFunc
(
session
,
username
,
password
);
if
(!
authenticated
)
if
(!
authenticated
)
{
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
HttpClient
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
}
}
return
authenticated
;
return
authenticated
;
...
...
src/Titanium.Web.Proxy/ProxyServer.cs
View file @
a7cab15f
...
@@ -679,7 +679,7 @@ namespace Titanium.Web.Proxy
...
@@ -679,7 +679,7 @@ namespace Titanium.Web.Proxy
/// <returns>The external proxy as task result.</returns>
/// <returns>The external proxy as task result.</returns>
private
Task
<
ExternalProxy
>
getSystemUpStreamProxy
(
SessionEventArgsBase
sessionEventArgs
)
private
Task
<
ExternalProxy
>
getSystemUpStreamProxy
(
SessionEventArgsBase
sessionEventArgs
)
{
{
var
proxy
=
systemProxyResolver
.
GetProxy
(
sessionEventArgs
.
WebSession
.
Request
.
RequestUri
);
var
proxy
=
systemProxyResolver
.
GetProxy
(
sessionEventArgs
.
HttpClient
.
Request
.
RequestUri
);
return
Task
.
FromResult
(
proxy
);
return
Task
.
FromResult
(
proxy
);
}
}
...
...
src/Titanium.Web.Proxy/RequestHandler.cs
View file @
a7cab15f
This diff is collapsed.
Click to expand it.
src/Titanium.Web.Proxy/ResponseHandler.cs
View file @
a7cab15f
...
@@ -22,11 +22,11 @@ namespace Titanium.Web.Proxy
...
@@ -22,11 +22,11 @@ namespace Titanium.Web.Proxy
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
// read response & headers from server
// read response & headers from server
await
args
.
WebSession
.
ReceiveResponse
(
cancellationToken
);
await
args
.
HttpClient
.
ReceiveResponse
(
cancellationToken
);
args
.
TimeLine
[
"Response Received"
]
=
DateTime
.
Now
;
args
.
TimeLine
[
"Response Received"
]
=
DateTime
.
Now
;
var
response
=
args
.
WebSession
.
Response
;
var
response
=
args
.
HttpClient
.
Response
;
args
.
ReRequest
=
false
;
args
.
ReRequest
=
false
;
// check for windows authentication
// check for windows authentication
...
@@ -38,7 +38,7 @@ namespace Titanium.Web.Proxy
...
@@ -38,7 +38,7 @@ namespace Titanium.Web.Proxy
}
}
else
else
{
{
WinAuthEndPoint
.
AuthenticatedResponse
(
args
.
WebSession
.
Data
);
WinAuthEndPoint
.
AuthenticatedResponse
(
args
.
HttpClient
.
Data
);
}
}
}
}
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
...
@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
}
}
// it may changed in the user event
// it may changed in the user event
response
=
args
.
WebSession
.
Response
;
response
=
args
.
HttpClient
.
Response
;
var
clientStreamWriter
=
args
.
ProxyClient
.
ClientStreamWriter
;
var
clientStreamWriter
=
args
.
ProxyClient
.
ClientStreamWriter
;
...
@@ -63,8 +63,8 @@ namespace Titanium.Web.Proxy
...
@@ -63,8 +63,8 @@ namespace Titanium.Web.Proxy
//write custom user response with body and return.
//write custom user response with body and return.
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
if
(
args
.
WebSession
.
Server
Connection
!=
null
if
(
args
.
HttpClient
.
Connection
!=
null
&&
!
args
.
WebSession
.
CloseServerConnection
)
&&
!
args
.
HttpClient
.
CloseServerConnection
)
{
{
// syphon out the original response body from server connection
// syphon out the original response body from server connection
// so that connection will be good to be reused.
// so that connection will be good to be reused.
...
@@ -80,7 +80,8 @@ namespace Titanium.Web.Proxy
...
@@ -80,7 +80,8 @@ namespace Titanium.Web.Proxy
{
{
// clear current response
// clear current response
await
args
.
ClearResponse
(
cancellationToken
);
await
args
.
ClearResponse
(
cancellationToken
);
await
handleHttpSessionRequest
(
args
.
WebSession
.
ServerConnection
,
args
);
await
handleHttpSessionRequest
(
args
,
args
.
HttpClient
.
Connection
,
args
.
ClientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
return
;
return
;
}
}
...
...
src/Titanium.Web.Proxy/WebSocketHandler.cs
View file @
a7cab15f
...
@@ -58,7 +58,7 @@ namespace Titanium.Web.Proxy
...
@@ -58,7 +58,7 @@ namespace Titanium.Web.Proxy
}
}
// If user requested call back then do it
// If user requested call back then do it
if
(!
args
.
WebSession
.
Response
.
Locked
)
if
(!
args
.
HttpClient
.
Response
.
Locked
)
{
{
await
invokeBeforeResponse
(
args
);
await
invokeBeforeResponse
(
args
);
}
}
...
...
src/Titanium.Web.Proxy/WinAuthHandler.cs
View file @
a7cab15f
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy
string
headerName
=
null
;
string
headerName
=
null
;
HttpHeader
authHeader
=
null
;
HttpHeader
authHeader
=
null
;
var
response
=
args
.
WebSession
.
Response
;
var
response
=
args
.
HttpClient
.
Response
;
// check in non-unique headers first
// check in non-unique headers first
var
header
=
response
.
Headers
.
NonUniqueHeaders
.
FirstOrDefault
(
x
=>
authHeaderNames
.
Contains
(
x
.
Key
));
var
header
=
response
.
Headers
.
NonUniqueHeaders
.
FirstOrDefault
(
x
=>
authHeaderNames
.
Contains
(
x
.
Key
));
...
@@ -96,14 +96,14 @@ namespace Titanium.Web.Proxy
...
@@ -96,14 +96,14 @@ namespace Titanium.Web.Proxy
var
expectedAuthState
=
var
expectedAuthState
=
scheme
==
null
?
State
.
WinAuthState
.
INITIAL_TOKEN
:
State
.
WinAuthState
.
UNAUTHORIZED
;
scheme
==
null
?
State
.
WinAuthState
.
INITIAL_TOKEN
:
State
.
WinAuthState
.
UNAUTHORIZED
;
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
WebSession
.
Data
,
expectedAuthState
))
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
HttpClient
.
Data
,
expectedAuthState
))
{
{
// Invalid state, create proper error message to client
// Invalid state, create proper error message to client
await
rewriteUnauthorizedResponse
(
args
);
await
rewriteUnauthorizedResponse
(
args
);
return
;
return
;
}
}
var
request
=
args
.
WebSession
.
Request
;
var
request
=
args
.
HttpClient
.
Request
;
// clear any existing headers to avoid confusing bad servers
// clear any existing headers to avoid confusing bad servers
request
.
Headers
.
RemoveHeader
(
KnownHeaders
.
Authorization
);
request
.
Headers
.
RemoveHeader
(
KnownHeaders
.
Authorization
);
...
@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy
...
@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy
// initial value will match exactly any of the schemes
// initial value will match exactly any of the schemes
if
(
scheme
!=
null
)
if
(
scheme
!=
null
)
{
{
string
clientToken
=
WinAuthHandler
.
GetInitialAuthToken
(
request
.
Host
,
scheme
,
args
.
WebSession
.
Data
);
string
clientToken
=
WinAuthHandler
.
GetInitialAuthToken
(
request
.
Host
,
scheme
,
args
.
HttpClient
.
Data
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
...
@@ -133,7 +133,7 @@ namespace Titanium.Web.Proxy
...
@@ -133,7 +133,7 @@ namespace Titanium.Web.Proxy
authHeader
.
Value
.
Length
>
x
.
Length
+
1
);
authHeader
.
Value
.
Length
>
x
.
Length
+
1
);
string
serverToken
=
authHeader
.
Value
.
Substring
(
scheme
.
Length
+
1
);
string
serverToken
=
authHeader
.
Value
.
Substring
(
scheme
.
Length
+
1
);
string
clientToken
=
WinAuthHandler
.
GetFinalAuthToken
(
request
.
Host
,
serverToken
,
args
.
WebSession
.
Data
);
string
clientToken
=
WinAuthHandler
.
GetFinalAuthToken
(
request
.
Host
,
serverToken
,
args
.
HttpClient
.
Data
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
...
@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy
...
@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy
request
.
ContentLength
=
request
.
Body
.
Length
;
request
.
ContentLength
=
request
.
Body
.
Length
;
}
}
args
.
WebSession
.
Server
Connection
.
IsWinAuthenticated
=
true
;
args
.
HttpClient
.
Connection
.
IsWinAuthenticated
=
true
;
}
}
// Need to revisit this.
// Need to revisit this.
...
@@ -165,7 +165,7 @@ namespace Titanium.Web.Proxy
...
@@ -165,7 +165,7 @@ namespace Titanium.Web.Proxy
/// <returns></returns>
/// <returns></returns>
private
async
Task
rewriteUnauthorizedResponse
(
SessionEventArgs
args
)
private
async
Task
rewriteUnauthorizedResponse
(
SessionEventArgs
args
)
{
{
var
response
=
args
.
WebSession
.
Response
;
var
response
=
args
.
HttpClient
.
Response
;
// Strip authentication headers to avoid credentials prompt in client web browser
// Strip authentication headers to avoid credentials prompt in client web browser
foreach
(
string
authHeaderName
in
authHeaderNames
)
foreach
(
string
authHeaderName
in
authHeaderNames
)
...
@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy
...
@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy
// Add custom div to body to clarify that the proxy (not the client browser) failed authentication
// Add custom div to body to clarify that the proxy (not the client browser) failed authentication
string
authErrorMessage
=
string
authErrorMessage
=
"<div class=\"inserted-by-proxy\"><h2>NTLM authentication through Titanium.Web.Proxy ("
+
"<div class=\"inserted-by-proxy\"><h2>NTLM authentication through Titanium.Web.Proxy ("
+
args
.
ProxyClient
.
C
lientC
onnection
.
LocalEndPoint
+
args
.
ProxyClient
.
Connection
.
LocalEndPoint
+
") failed. Please check credentials.</h2></div>"
;
") failed. Please check credentials.</h2></div>"
;
string
originalErrorMessage
=
string
originalErrorMessage
=
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>"
;
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>"
;
...
...
tests/Titanium.Web.Proxy.IntegrationTests/InterceptionTests.cs
View file @
a7cab15f
...
@@ -46,9 +46,9 @@ namespace Titanium.Web.Proxy.IntegrationTests
...
@@ -46,9 +46,9 @@ namespace Titanium.Web.Proxy.IntegrationTests
public
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
public
async
Task
OnRequest
(
object
sender
,
SessionEventArgs
e
)
{
{
if
(
e
.
WebSession
.
Request
.
Url
.
Contains
(
"interceptthis.com"
))
if
(
e
.
HttpClient
.
Request
.
Url
.
Contains
(
"interceptthis.com"
))
{
{
if
(
e
.
WebSession
.
Request
.
HasBody
)
if
(
e
.
HttpClient
.
Request
.
HasBody
)
{
{
var
body
=
await
e
.
GetRequestBodyAsString
();
var
body
=
await
e
.
GetRequestBodyAsString
();
}
}
...
...
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