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
7e28bf14
Commit
7e28bf14
authored
May 06, 2018
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use of Guid as a session identifer #427
parent
a1d4de48
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
339 additions
and
409 deletions
+339
-409
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+6
-2
WinAuthTests.cs
Tests/Titanium.Web.Proxy.UnitTests/WinAuthTests.cs
+2
-1
SessionEventArgsBase.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
+7
-3
ExplicitClientHandler.cs
Titanium.Web.Proxy/ExplicitClientHandler.cs
+3
-1
TcpHelper.cs
Titanium.Web.Proxy/Helpers/TcpHelper.cs
+258
-346
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+8
-3
InternalDataStore.cs
Titanium.Web.Proxy/Http/InternalDataStore.cs
+27
-0
WinAuthEndPoint.cs
...ium.Web.Proxy/Network/WinAuth/Security/WinAuthEndPoint.cs
+16
-37
WinAuthHandler.cs
Titanium.Web.Proxy/Network/WinAuth/WinAuthHandler.cs
+8
-7
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+0
-5
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+1
-1
WinAuthHandler.cs
Titanium.Web.Proxy/WinAuthHandler.cs
+3
-3
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
7e28bf14
...
...
@@ -164,8 +164,12 @@ namespace Titanium.Web.Proxy.Examples.Basic
WriteToConsole
(
"Active Client Connections:"
+
((
ProxyServer
)
sender
).
ClientConnectionCount
);
WriteToConsole
(
e
.
WebSession
.
Request
.
Url
);
// create custom id for the request and store it in the UserData property
// It can be a simple integer, Guid, or any type
e
.
UserData
=
Guid
.
NewGuid
();
// read request headers
requestHeaderHistory
[
e
.
Id
]
=
e
.
WebSession
.
Request
.
Headers
;
requestHeaderHistory
[
(
Guid
)
e
.
UserData
]
=
e
.
WebSession
.
Request
.
Headers
;
////This sample shows how to get the multipart form data headers
//if (e.WebSession.Request.Host == "mail.yahoo.com" && e.WebSession.Request.IsMultipartFormData)
...
...
@@ -221,7 +225,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
{
WriteToConsole
(
"Active Server Connections:"
+
((
ProxyServer
)
sender
).
ServerConnectionCount
);
var
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsolutePath
);
string
ext
=
System
.
IO
.
Path
.
GetExtension
(
e
.
WebSession
.
Request
.
RequestUri
.
AbsolutePath
);
//if (ext == ".gif" || ext == ".png" || ext == ".jpg")
//{
...
...
Tests/Titanium.Web.Proxy.UnitTests/WinAuthTests.cs
View file @
7e28bf14
using
System
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Network.WinAuth
;
namespace
Titanium.Web.Proxy.UnitTests
...
...
@@ -10,7 +11,7 @@ namespace Titanium.Web.Proxy.UnitTests
[
TestMethod
]
public
void
Test_Acquire_Client_Token
()
{
string
token
=
WinAuthHandler
.
GetInitialAuthToken
(
"mylocalserver.com"
,
"NTLM"
,
Guid
.
NewGuid
());
string
token
=
WinAuthHandler
.
GetInitialAuthToken
(
"mylocalserver.com"
,
"NTLM"
,
new
InternalDataStore
());
Assert
.
IsTrue
(
token
.
Length
>
1
);
}
}
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
View file @
7e28bf14
...
...
@@ -74,10 +74,14 @@ namespace Titanium.Web.Proxy.EventArguments
internal
ProxyClient
ProxyClient
{
get
;
}
/// <summary>
/// Returns a u
nique Id
for this request/response session which is
/// same as the
RequestId
of WebSession.
/// Returns a u
ser data
for this request/response session which is
/// same as the
user data
of WebSession.
/// </summary>
public
Guid
Id
=>
WebSession
.
RequestId
;
public
object
UserData
{
get
=>
WebSession
.
UserData
;
set
=>
WebSession
.
UserData
=
value
;
}
/// <summary>
/// Does this session uses SSL?
...
...
Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
7e28bf14
...
...
@@ -272,10 +272,12 @@ namespace Titanium.Web.Proxy
await
connection
.
StreamWriter
.
WriteLineAsync
(
"SM"
,
cancellationToken
);
await
connection
.
StreamWriter
.
WriteLineAsync
(
cancellationToken
);
await
TcpHelper
.
SendHttp2
(
clientStream
,
connection
.
Stream
,
BufferSize
,
#if NETCOREAPP2_1
await
Http2Helper
.
SendHttp2
(
clientStream
,
connection
.
Stream
,
BufferSize
,
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
connectArgs
.
CancellationTokenSource
,
clientConnection
.
Id
,
ExceptionFunc
);
#endif
}
}
}
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
→
Titanium.Web.Proxy/Helpers/Tcp
Helper
.cs
View file @
7e28bf14
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
7e28bf14
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Net
;
using
System.Threading
;
...
...
@@ -20,7 +21,6 @@ namespace Titanium.Web.Proxy.Http
{
this
.
bufferSize
=
bufferSize
;
RequestId
=
Guid
.
NewGuid
();
Request
=
request
??
new
Request
();
Response
=
response
??
new
Response
();
}
...
...
@@ -31,9 +31,14 @@ namespace Titanium.Web.Proxy.Http
internal
TcpServerConnection
ServerConnection
{
get
;
set
;
}
/// <summary>
///
Request ID
.
///
Stores internal data for the session
.
/// </summary>
public
Guid
RequestId
{
get
;
}
internal
InternalDataStore
Data
{
get
;
}
=
new
InternalDataStore
();
/// <summary>
/// Gets or sets the user data.
/// </summary>
public
object
UserData
{
get
;
set
;
}
/// <summary>
/// Override UpStreamEndPoint for this request; Local NIC via request is made
...
...
Titanium.Web.Proxy/Http/InternalDataStore.cs
0 → 100644
View file @
7e28bf14
using
System.Collections.Generic
;
namespace
Titanium.Web.Proxy.Http
{
class
InternalDataStore
:
Dictionary
<
string
,
object
>
{
public
bool
TryGetValueAs
<
T
>(
string
key
,
out
T
value
)
{
bool
result
=
TryGetValue
(
key
,
out
var
value1
);
if
(
result
)
{
value
=
(
T
)
value1
;
}
else
{
value
=
default
;
}
return
result
;
}
public
T
GetAs
<
T
>(
string
key
)
{
return
(
T
)
this
[
key
];
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/Network/WinAuth/Security/WinAuthEndPoint.cs
View file @
7e28bf14
...
...
@@ -7,6 +7,7 @@ using System.Linq;
using
System.Runtime.InteropServices
;
using
System.Security.Principal
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Http
;
namespace
Titanium.Web.Proxy.Network.WinAuth.Security
{
...
...
@@ -14,19 +15,16 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
internal
class
WinAuthEndPoint
{
/// <summary>
/// Keep track of auth states for reuse in final challenge response
/// </summary>
private
static
readonly
IDictionary
<
Guid
,
State
>
authStates
=
new
ConcurrentDictionary
<
Guid
,
State
>();
private
const
string
authStateKey
=
"AuthState"
;
/// <summary>
/// Acquire the intial client token to send
/// </summary>
/// <param name="hostname"></param>
/// <param name="authScheme"></param>
/// <param name="
requestId
"></param>
/// <param name="
data
"></param>
/// <returns></returns>
internal
static
byte
[]
AcquireInitialSecurityToken
(
string
hostname
,
string
authScheme
,
Guid
requestId
)
internal
static
byte
[]
AcquireInitialSecurityToken
(
string
hostname
,
string
authScheme
,
InternalDataStore
data
)
{
byte
[]
token
;
...
...
@@ -75,7 +73,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
state
.
AuthState
=
State
.
WinAuthState
.
INITIAL_TOKEN
;
token
=
clientToken
.
GetBytes
();
authStates
.
Add
(
requestId
,
state
);
data
.
Add
(
authStateKey
,
state
);
}
finally
{
...
...
@@ -91,9 +89,9 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
/// </summary>
/// <param name="hostname"></param>
/// <param name="serverChallenge"></param>
/// <param name="
requestId
"></param>
/// <param name="
data
"></param>
/// <returns></returns>
internal
static
byte
[]
AcquireFinalSecurityToken
(
string
hostname
,
byte
[]
serverChallenge
,
Guid
requestId
)
internal
static
byte
[]
AcquireFinalSecurityToken
(
string
hostname
,
byte
[]
serverChallenge
,
InternalDataStore
data
)
{
byte
[]
token
;
...
...
@@ -104,7 +102,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
try
{
var
state
=
authStates
[
requestId
]
;
var
state
=
data
.
GetAs
<
State
>(
authStateKey
)
;
state
.
UpdatePresence
();
...
...
@@ -120,7 +118,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
out
clientToken
,
out
NewContextAttributes
,
out
NewLifeTime
);
if
(
result
!=
SuccessfulResult
)
{
return
null
;
...
...
@@ -138,35 +136,16 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
return
token
;
}
/// <summary>
/// Clear any hanging states
/// </summary>
/// <param name="stateCacheTimeOutMinutes"></param>
internal
static
async
void
ClearIdleStates
(
int
stateCacheTimeOutMinutes
)
{
var
cutOff
=
DateTime
.
Now
.
AddMinutes
(-
1
*
stateCacheTimeOutMinutes
);
var
outdated
=
authStates
.
Where
(
x
=>
x
.
Value
.
LastSeen
<
cutOff
).
ToList
();
foreach
(
var
cache
in
outdated
)
{
authStates
.
Remove
(
cache
.
Key
);
}
// after a minute come back to check for outdated certificates in cache
await
Task
.
Delay
(
1000
*
60
);
}
/// <summary>
/// Validates that the current WinAuth state of the connection matches the
/// expectation, used to detect failed authentication
/// </summary>
/// <param name="
requestId
"></param>
/// <param name="
data
"></param>
/// <param name="expectedAuthState"></param>
/// <returns></returns>
internal
static
bool
ValidateWinAuthState
(
Guid
requestId
,
State
.
WinAuthState
expectedAuthState
)
internal
static
bool
ValidateWinAuthState
(
InternalDataStore
data
,
State
.
WinAuthState
expectedAuthState
)
{
bool
stateExists
=
authStates
.
TryGetValue
(
requestId
,
out
var
state
);
bool
stateExists
=
data
.
TryGetValueAs
(
authStateKey
,
out
State
state
);
if
(
expectedAuthState
==
State
.
WinAuthState
.
UNAUTHORIZED
)
{
...
...
@@ -188,10 +167,10 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
/// <summary>
/// Set the AuthState to authorized and update the connection state lifetime
/// </summary>
/// <param name="
requestId
"></param>
internal
static
void
AuthenticatedResponse
(
Guid
requestId
)
/// <param name="
data
"></param>
internal
static
void
AuthenticatedResponse
(
InternalDataStore
data
)
{
if
(
authStates
.
TryGetValue
(
requestId
,
out
var
state
))
if
(
data
.
TryGetValueAs
(
authStateKey
,
out
State
state
))
{
state
.
AuthState
=
State
.
WinAuthState
.
AUTHORIZED
;
state
.
UpdatePresence
();
...
...
Titanium.Web.Proxy/Network/WinAuth/WinAuthHandler.cs
View file @
7e28bf14
using
System
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Network.WinAuth.Security
;
namespace
Titanium.Web.Proxy.Network.WinAuth
...
...
@@ -16,26 +17,26 @@ namespace Titanium.Web.Proxy.Network.WinAuth
/// </summary>
/// <param name="serverHostname"></param>
/// <param name="authScheme"></param>
/// <param name="
requestId
"></param>
/// <param name="
data
"></param>
/// <returns></returns>
internal
static
string
GetInitialAuthToken
(
string
serverHostname
,
string
authScheme
,
Guid
requestId
)
internal
static
string
GetInitialAuthToken
(
string
serverHostname
,
string
authScheme
,
InternalDataStore
data
)
{
var
tokenBytes
=
WinAuthEndPoint
.
AcquireInitialSecurityToken
(
serverHostname
,
authScheme
,
requestId
);
var
tokenBytes
=
WinAuthEndPoint
.
AcquireInitialSecurityToken
(
serverHostname
,
authScheme
,
data
);
return
string
.
Concat
(
" "
,
Convert
.
ToBase64String
(
tokenBytes
));
}
/// <summary>
/// Get the final token given the server challenge token
/// </summary>
/// <param name="serverHostname"></param>
/// <param name="serverToken"></param>
/// <param name="
requestId
"></param>
/// <param name="
data
"></param>
/// <returns></returns>
internal
static
string
GetFinalAuthToken
(
string
serverHostname
,
string
serverToken
,
Guid
requestId
)
internal
static
string
GetFinalAuthToken
(
string
serverHostname
,
string
serverToken
,
InternalDataStore
data
)
{
var
tokenBytes
=
WinAuthEndPoint
.
AcquireFinalSecurityToken
(
serverHostname
,
Convert
.
FromBase64String
(
serverToken
),
requestId
);
data
);
return
string
.
Concat
(
" "
,
Convert
.
ToBase64String
(
tokenBytes
));
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
7e28bf14
...
...
@@ -511,11 +511,6 @@ namespace Titanium.Web.Proxy
CertificateManager
.
ClearIdleCertificates
();
if
(
RunTime
.
IsWindows
&&
!
RunTime
.
IsRunningOnMono
)
{
WinAuthEndPoint
.
ClearIdleStates
(
2
);
}
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
Listen
(
endPoint
);
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
7e28bf14
...
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy
}
else
{
WinAuthEndPoint
.
AuthenticatedResponse
(
args
.
WebSession
.
RequestId
);
WinAuthEndPoint
.
AuthenticatedResponse
(
args
.
WebSession
.
Data
);
}
}
...
...
Titanium.Web.Proxy/WinAuthHandler.cs
View file @
7e28bf14
...
...
@@ -96,7 +96,7 @@ namespace Titanium.Web.Proxy
var
expectedAuthState
=
scheme
==
null
?
State
.
WinAuthState
.
INITIAL_TOKEN
:
State
.
WinAuthState
.
UNAUTHORIZED
;
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
WebSession
.
RequestId
,
expectedAuthState
))
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
WebSession
.
Data
,
expectedAuthState
))
{
// Invalid state, create proper error message to client
await
RewriteUnauthorizedResponse
(
args
);
...
...
@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy
// initial value will match exactly any of the schemes
if
(
scheme
!=
null
)
{
string
clientToken
=
WinAuthHandler
.
GetInitialAuthToken
(
request
.
Host
,
scheme
,
args
.
Id
);
string
clientToken
=
WinAuthHandler
.
GetInitialAuthToken
(
request
.
Host
,
scheme
,
args
.
WebSession
.
Data
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
...
...
@@ -133,7 +133,7 @@ namespace Titanium.Web.Proxy
authHeader
.
Value
.
Length
>
x
.
Length
+
1
);
string
serverToken
=
authHeader
.
Value
.
Substring
(
scheme
.
Length
+
1
);
string
clientToken
=
WinAuthHandler
.
GetFinalAuthToken
(
request
.
Host
,
serverToken
,
args
.
Id
);
string
clientToken
=
WinAuthHandler
.
GetFinalAuthToken
(
request
.
Host
,
serverToken
,
args
.
WebSession
.
Data
);
string
auth
=
string
.
Concat
(
scheme
,
clientToken
);
...
...
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