Commit a940f69d authored by justcoding121's avatar justcoding121 Committed by justcoding121

cleanup

parent d10bb71a
...@@ -7,13 +7,15 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -7,13 +7,15 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary> /// </summary>
internal class RunTime internal class RunTime
{ {
private static Lazy<bool> isMonoRuntime
= new Lazy<bool>(()=> Type.GetType("Mono.Runtime") != null);
/// <summary> /// <summary>
/// Checks if current run time is Mono /// Checks if current run time is Mono
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal static bool IsRunningOnMono() internal static bool IsRunningOnMono()
{ {
return Type.GetType("Mono.Runtime") != null; return isMonoRuntime.Value;
} }
} }
} }
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
this.Credentials = new Common.SecurityHandle(0); this.Credentials = new Common.SecurityHandle(0);
this.Context = new Common.SecurityHandle(0); this.Context = new Common.SecurityHandle(0);
this.LastSeen = DateTime.MinValue; this.LastSeen = DateTime.Now;
} }
/// <summary> /// <summary>
...@@ -30,11 +30,6 @@ ...@@ -30,11 +30,6 @@
/// </summary> /// </summary>
internal DateTime LastSeen; internal DateTime LastSeen;
internal bool isOlder(int seconds)
{
return (this.LastSeen.AddSeconds(seconds) < DateTime.UtcNow) ? true : false;
}
internal void ResetHandles() internal void ResetHandles()
{ {
this.Credentials.Reset(); this.Credentials.Reset();
...@@ -43,7 +38,7 @@ ...@@ -43,7 +38,7 @@
internal void UpdatePresence() internal void UpdatePresence()
{ {
this.LastSeen = DateTime.UtcNow; this.LastSeen = DateTime.Now;
} }
} }
} }
...@@ -3,13 +3,15 @@ ...@@ -3,13 +3,15 @@
namespace Titanium.Web.Proxy.Network.WinAuth.Security namespace Titanium.Web.Proxy.Network.WinAuth.Security
{ {
using System; using System;
using System.Linq;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security.Principal; using System.Security.Principal;
using static Common; using static Common;
using System.Threading.Tasks;
internal class EndPoint internal class WinAuthEndPoint
{ {
/// <summary> /// <summary>
/// Keep track of auth states for reuse in final challenge response /// Keep track of auth states for reuse in final challenge response
...@@ -117,6 +119,8 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security ...@@ -117,6 +119,8 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
var state = authStates[requestId]; var state = authStates[requestId];
state.UpdatePresence();
result = InitializeSecurityContext(ref state.Credentials, result = InitializeSecurityContext(ref state.Credentials,
ref state.Context, ref state.Context,
hostname, hostname,
...@@ -136,7 +140,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security ...@@ -136,7 +140,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
// Client challenge issue operation failed. // Client challenge issue operation failed.
return null; return null;
} }
authStates.Remove(requestId); authStates.Remove(requestId);
token = clientToken.GetBytes(); token = clientToken.GetBytes();
} }
...@@ -148,6 +152,28 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security ...@@ -148,6 +152,28 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
return token; 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);
}
#region Native calls to secur32.dll #region Native calls to secur32.dll
[DllImport("secur32.dll", SetLastError = true)] [DllImport("secur32.dll", SetLastError = true)]
......
...@@ -21,7 +21,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth ...@@ -21,7 +21,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth
public static string GetInitialAuthToken(string serverHostname, public static string GetInitialAuthToken(string serverHostname,
string authScheme, Guid requestId) string authScheme, Guid requestId)
{ {
var tokenBytes = EndPoint.AcquireInitialSecurityToken(serverHostname, authScheme, requestId); var tokenBytes = WinAuthEndPoint.AcquireInitialSecurityToken(serverHostname, authScheme, requestId);
return string.Concat(" ", Convert.ToBase64String(tokenBytes)); return string.Concat(" ", Convert.ToBase64String(tokenBytes));
} }
...@@ -36,7 +36,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth ...@@ -36,7 +36,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth
public static string GetFinalAuthToken(string serverHostname, public static string GetFinalAuthToken(string serverHostname,
string serverToken, Guid requestId) string serverToken, Guid requestId)
{ {
var tokenBytes = EndPoint.AcquireFinalSecurityToken(serverHostname, var tokenBytes = WinAuthEndPoint.AcquireFinalSecurityToken(serverHostname,
Convert.FromBase64String(serverToken), requestId); Convert.FromBase64String(serverToken), requestId);
return string.Concat(" ", Convert.ToBase64String(tokenBytes)); return string.Concat(" ", Convert.ToBase64String(tokenBytes));
......
...@@ -12,6 +12,7 @@ using Titanium.Web.Proxy.Helpers; ...@@ -12,6 +12,7 @@ using Titanium.Web.Proxy.Helpers;
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; using Titanium.Web.Proxy.Network.Tcp;
using Titanium.Web.Proxy.Network.WinAuth.Security;
namespace Titanium.Web.Proxy namespace Titanium.Web.Proxy
{ {
...@@ -475,6 +476,11 @@ namespace Titanium.Web.Proxy ...@@ -475,6 +476,11 @@ namespace Titanium.Web.Proxy
CertificateManager.ClearIdleCertificates(CertificateCacheTimeOutMinutes); CertificateManager.ClearIdleCertificates(CertificateCacheTimeOutMinutes);
if (!RunTime.IsRunningOnMono())
{
WinAuthEndPoint.ClearIdleStates(2);
}
proxyRunning = true; proxyRunning = true;
} }
......
...@@ -37,8 +37,9 @@ namespace Titanium.Web.Proxy ...@@ -37,8 +37,9 @@ namespace Titanium.Web.Proxy
} }
//check for windows authentication //check for windows authentication
if(EnableWinAuth && if(EnableWinAuth &&
args.WebSession.Response.ResponseStatusCode == "401") args.WebSession.Response.ResponseStatusCode == "401"
&& !RunTime.IsRunningOnMono())
{ {
var disposed = await Handle401UnAuthorized(args); var disposed = await Handle401UnAuthorized(args);
......
...@@ -108,7 +108,7 @@ ...@@ -108,7 +108,7 @@
<Compile Include="Http\HttpWebClient.cs" /> <Compile Include="Http\HttpWebClient.cs" />
<Compile Include="Network\WinAuth\Security\BufferWrapper.cs" /> <Compile Include="Network\WinAuth\Security\BufferWrapper.cs" />
<Compile Include="Network\WinAuth\Security\Common.cs" /> <Compile Include="Network\WinAuth\Security\Common.cs" />
<Compile Include="Network\WinAuth\Security\EndPoint.cs" /> <Compile Include="Network\WinAuth\Security\WinAuthEndPoint.cs" />
<Compile Include="Network\WinAuth\Security\LittleEndian.cs" /> <Compile Include="Network\WinAuth\Security\LittleEndian.cs" />
<Compile Include="Network\WinAuth\Security\Message.cs" /> <Compile Include="Network\WinAuth\Security\Message.cs" />
<Compile Include="Network\WinAuth\Security\State.cs" /> <Compile Include="Network\WinAuth\Security\State.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment