-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking dc connection cache out of utils
- Loading branch information
1 parent
ee7d009
commit 1cef248
Showing
3 changed files
with
90 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.Concurrent; | ||
using System.DirectoryServices.Protocols; | ||
|
||
namespace SharpHoundCommonLib | ||
{ | ||
public class DCConnectionCache | ||
{ | ||
private readonly ConcurrentDictionary<LDAPConnectionCacheKey, LdapConnection> _ldapConnectionCache; | ||
|
||
public DCConnectionCache() | ||
{ | ||
_ldapConnectionCache = new ConcurrentDictionary<LDAPConnectionCacheKey, LdapConnection>(); | ||
} | ||
|
||
public bool TryGet(string domainName, bool isGlobalCatalog, out LdapConnection connection) | ||
{ | ||
var key = GetKey(domainName, isGlobalCatalog); | ||
return _ldapConnectionCache.TryGetValue(key, out connection); | ||
} | ||
|
||
public LdapConnection AddOrUpdate(string domainName, bool isGlobalCatalog, LdapConnection connection) | ||
{ | ||
var cacheKey = GetKey(domainName, isGlobalCatalog); | ||
return _ldapConnectionCache.AddOrUpdate(cacheKey, connection, (_, existingConnection) => | ||
{ | ||
existingConnection.Dispose(); | ||
return connection; | ||
}); | ||
} | ||
|
||
public LdapConnection TryAdd(string domainName, bool isGlobalCatalog, LdapConnection connection) | ||
{ | ||
var cacheKey = GetKey(domainName, isGlobalCatalog); | ||
return _ldapConnectionCache.AddOrUpdate(cacheKey, connection, (_, existingConnection) => | ||
{ | ||
connection.Dispose(); | ||
return existingConnection; | ||
}); | ||
} | ||
|
||
private LDAPConnectionCacheKey GetKey(string domainName, bool isGlobalCatalog) | ||
{ | ||
return new LDAPConnectionCacheKey(domainName, isGlobalCatalog); | ||
} | ||
|
||
private class LDAPConnectionCacheKey | ||
{ | ||
public bool GlobalCatalog { get; } | ||
public string Domain { get; } | ||
public string Server { get; set; } | ||
|
||
public LDAPConnectionCacheKey(string domain, bool globalCatalog) | ||
{ | ||
GlobalCatalog = globalCatalog; | ||
Domain = domain; | ||
} | ||
|
||
protected bool Equals(LDAPConnectionCacheKey other) | ||
{ | ||
return GlobalCatalog == other.GlobalCatalog && Domain == other.Domain; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
if (obj.GetType() != this.GetType()) return false; | ||
return Equals((LDAPConnectionCacheKey)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return (GlobalCatalog.GetHashCode() * 397) ^ (Domain != null ? Domain.GetHashCode() : 0); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters