-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Test-ValentiaCredential, Remove-ValentiaCredential Revised Ping-ValentiaAsync
- Loading branch information
1 parent
85165b5
commit 506752d
Showing
10 changed files
with
378 additions
and
135 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
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,169 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
|
||
namespace PingEx | ||
{ | ||
public class DnsResponse | ||
{ | ||
public string HostName { get; private set; } | ||
public IPAddress IPAddress { get; private set; } | ||
public bool IsResolved | ||
{ | ||
get | ||
{ | ||
IPAddress item = null; | ||
return !IPAddress.TryParse(this.HostName, out item); | ||
} | ||
} | ||
|
||
public DnsResponse(string hostName, IPAddress ip) | ||
{ | ||
this.HostName = hostName; | ||
this.IPAddress = ip; | ||
} | ||
} | ||
|
||
public class DnsResolver | ||
{ | ||
public static DnsResponse ResolveIP(IPAddress ip, TimeSpan timeout) | ||
{ | ||
Func<IPAddress, IPHostEntry> callback = s => Dns.GetHostEntry(s); | ||
var result = callback.BeginInvoke(ip, null, null); | ||
if (!result.AsyncWaitHandle.WaitOne(timeout, false)) | ||
{ | ||
return new DnsResponse(ip.ToString(), ip); | ||
} | ||
var hostEntry = callback.EndInvoke(result); | ||
return new DnsResponse(hostEntry.HostName, ip); | ||
} | ||
|
||
public static DnsResponse ResolveHostName(string hostNameOrAddress, TimeSpan timeout) | ||
{ | ||
Func<string, IPHostEntry> callback = s => Dns.GetHostEntry(s); | ||
var result = callback.BeginInvoke(hostNameOrAddress, null, null); | ||
if (!result.AsyncWaitHandle.WaitOne(timeout, false)) | ||
{ | ||
return new DnsResponse(hostNameOrAddress, null); | ||
} | ||
var hostEntry = callback.EndInvoke(result); | ||
var ip = hostEntry.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork); | ||
return new DnsResponse(hostNameOrAddress, ip); | ||
} | ||
} | ||
|
||
public class PingResponse | ||
{ | ||
public string HostNameOrAddress { get; set; } | ||
public IPAddress IPAddress { get; set; } | ||
public IPStatus Status { get; set; } | ||
public bool IsSuccess { get; set; } | ||
public long RoundTripTime { get; set; } | ||
public bool IsResolved { get; set; } | ||
} | ||
|
||
public class NetworkInformationExtensions | ||
{ | ||
private static readonly byte[] _buffer = new byte[16]; | ||
private static readonly PingOptions _options = new PingOptions(64, false); | ||
private static readonly TimeSpan _pingTimeout = TimeSpan.FromMilliseconds(10); | ||
private static readonly TimeSpan _dnsTimeout = TimeSpan.FromMilliseconds(20); | ||
private static bool _resolveDns = true; | ||
|
||
public static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress) | ||
{ | ||
return await PingAsync(hostNameOrAddress, _pingTimeout, _resolveDns, _dnsTimeout); | ||
} | ||
|
||
public static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress, TimeSpan pingTimeout) | ||
{ | ||
return await PingAsync(hostNameOrAddress, pingTimeout, _resolveDns, _dnsTimeout); | ||
} | ||
|
||
public static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress, bool resolveDns) | ||
{ | ||
return await PingAsync(hostNameOrAddress, _pingTimeout, resolveDns, _dnsTimeout); | ||
} | ||
|
||
public static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress, TimeSpan pingTimeout, bool resolveDns) | ||
{ | ||
return await PingAsync(hostNameOrAddress, pingTimeout, resolveDns, _dnsTimeout); | ||
} | ||
|
||
public static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress, TimeSpan pingTimeout, TimeSpan dnsTimeout) | ||
{ | ||
return await PingAsync(hostNameOrAddress, pingTimeout, _resolveDns, _dnsTimeout); | ||
} | ||
|
||
private static async Task<PingResponse[]> PingAsync(string[] hostNameOrAddress, TimeSpan pingTimeout, bool resolveDns, TimeSpan dnsTimeout) | ||
{ | ||
var pingResult = await Task.WhenAll(hostNameOrAddress.Select(async x => | ||
{ | ||
// Resolve only when incoming is HostName. | ||
IPAddress ip = null; | ||
DnsResponse resolve = null; | ||
var isIpAddress = IPAddress.TryParse(x, out ip); | ||
if (!isIpAddress) | ||
{ | ||
resolve = DnsResolver.ResolveHostName(x, dnsTimeout); | ||
ip = resolve.IPAddress; | ||
} | ||
|
||
// Execute PingAsync | ||
PingReply reply = null; | ||
using (var ping = new Ping()) | ||
{ | ||
try | ||
{ | ||
reply = await ping.SendPingAsync(ip, (int)pingTimeout.TotalMilliseconds, _buffer, _options); | ||
} | ||
catch | ||
{ | ||
// ping throw should never stop operation. just return null. | ||
} | ||
} | ||
|
||
// set RoundtripTime | ||
long roundTripTime = 0; | ||
if (reply != null) roundTripTime = reply.RoundtripTime; | ||
|
||
// set Status | ||
var status = IPStatus.DestinationHostUnreachable; | ||
if (reply != null) status = reply.Status; | ||
|
||
// set IsSuccess | ||
var isSuccess = status == IPStatus.Success; | ||
|
||
// return when PingFailed || HostName || OmitResolveDns | ||
if (!isSuccess || !isIpAddress || !resolveDns) | ||
return new PingResponse | ||
{ | ||
HostNameOrAddress = x, | ||
IPAddress = ip, | ||
Status = status, | ||
RoundTripTime = roundTripTime, | ||
IsSuccess = isSuccess, | ||
IsResolved = !isIpAddress && ip != null, | ||
}; | ||
|
||
// Resolve Dns only for success host entry. | ||
var host = x; | ||
resolve = DnsResolver.ResolveIP(ip, dnsTimeout); | ||
if (resolve != null) host = resolve.HostName; | ||
return new PingResponse | ||
{ | ||
HostNameOrAddress = host, | ||
IPAddress = ip, | ||
Status = status, | ||
RoundTripTime = roundTripTime, | ||
IsSuccess = true, | ||
IsResolved = resolve != null && resolve.IsResolved, | ||
}; | ||
}).ToArray()); | ||
return pingResult; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
valentia/Functions/Helper/Credential/Remove-ValentiaCredential.ps1
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,48 @@ | ||
#Requires -Version 3.0 | ||
|
||
function Remove-ValentiaCredential | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(mandatory = $false, position = 0)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$TargetName = $valentia.name, | ||
|
||
[Parameter(mandatory = $false, position = 1)] | ||
[ValidateNotNullOrEmpty()] | ||
[ValentiaWindowsCredentialManagerType]$Type = [ValentiaWindowsCredentialManagerType]::Generic | ||
) | ||
|
||
try | ||
{ | ||
$private:CSPath = Join-Path $valentia.modulePath $valentia.cSharpPath -Resolve | ||
$private:CredReadCS = Join-Path $CSPath CredRead.cs -Resolve | ||
$private:sig = Get-Content -Path $CredReadCS -Raw | ||
|
||
$private:addType = @{ | ||
MemberDefinition = $sig | ||
Namespace = "Advapi32" | ||
Name = "Util" | ||
} | ||
Add-ValentiaTypeMemberDefinition @addType -PassThru ` | ||
| select -First 1 ` | ||
| %{ | ||
$CredentialType = $_.AssemblyQualifiedName -as [type] | ||
$private:typeFullName = $_.FullName | ||
} | ||
|
||
$private:nCredPtr= New-Object IntPtr | ||
if ($CredentialType::CredDelete($TargetName, $Type.value__, 0)) | ||
{ | ||
} | ||
else | ||
{ | ||
throw "No credentials found in Windows Credential Manager for TargetName: '{0}' with Type '{1}'" -f $TargetName, $Type | ||
} | ||
} | ||
catch | ||
{ | ||
throw $_ | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
valentia/Functions/Helper/Credential/Test-ValentiaCredential.ps1
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,26 @@ | ||
#Requires -Version 3.0 | ||
|
||
function Test-ValentiaCredential | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(mandatory = $false, position = 0)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$TargetName = $valentia.name, | ||
|
||
[Parameter(mandatory = $false, position = 1)] | ||
[ValidateNotNullOrEmpty()] | ||
[ValentiaWindowsCredentialManagerType]$Type = [ValentiaWindowsCredentialManagerType]::Generic | ||
) | ||
|
||
try | ||
{ | ||
$result = Get-ValentiaCredential -TargetName $targetName | ||
return $true; | ||
} | ||
catch | ||
{ | ||
return $false; | ||
} | ||
} |
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
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
Oops, something went wrong.