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

cleanup

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