Skip to content

Commit

Permalink
v2.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed Feb 23, 2024
1 parent 8a944aa commit 06b513f
Show file tree
Hide file tree
Showing 118 changed files with 11,128 additions and 7,673 deletions.
59 changes: 37 additions & 22 deletions MsmhToolsClass/MsmhToolsClass/CertificateTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,42 +379,56 @@ public static bool InstallCertificate(string certPath, StoreName storeName, Stor

public static bool IsCertificateInstalled(string subjectName, StoreName storeName, StoreLocation storeLocation)
{
string cn = "CN=";
if (subjectName.StartsWith(cn)) subjectName = subjectName.TrimStart(cn);
try
{
string cn = "CN=";
if (subjectName.StartsWith(cn)) subjectName = subjectName.TrimStart(cn);

X509Store store = new(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
X509Store store = new(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);

X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, false);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, false);

if (certificates != null && certificates.Count > 0)
{
//Debug.WriteLine("Certificate is already installed.");
return true;
if (certificates != null && certificates.Count > 0)
{
//Debug.WriteLine("Certificate is already installed.");
return true;
}
else return false;
}
else
catch (Exception ex)
{
Debug.WriteLine("IsCertificateInstalled: " + ex.Message);
return false;
}
}

public static bool IsCertificateInstalled(X509Certificate2 cert, StoreName storeName, StoreLocation storeLocation)
{
string subjectName = cert.Subject;
string cn = "CN=";
if (subjectName.StartsWith(cn)) subjectName = subjectName.TrimStart(cn);
try
{
string subjectName = cert.Subject;
string cn = "CN=";
if (subjectName.StartsWith(cn)) subjectName = subjectName.TrimStart(cn);

X509Store store = new(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
X509Store store = new(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);

X509Certificate2Collection certsBySubject = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, false);
X509Certificate2Collection certsBySN = store.Certificates.Find(X509FindType.FindBySerialNumber, cert.SerialNumber, false);
X509Certificate2Collection certsBySubject = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, false);
X509Certificate2Collection certsBySN = store.Certificates.Find(X509FindType.FindBySerialNumber, cert.SerialNumber, false);

if (certsBySubject != null && certsBySubject.Any() && certsBySN != null && certsBySN.Any())
{
Debug.WriteLine("Certificate is already installed.");
return true;
if (certsBySubject != null && certsBySubject.Any() && certsBySN != null && certsBySN.Any())
{
Debug.WriteLine("Certificate is already installed.");
return true;
}
else return false;
}
else
catch (Exception ex)
{
Debug.WriteLine("IsCertificateInstalled: " + ex.Message);
return false;
}
}

/// <summary>
Expand Down Expand Up @@ -461,4 +475,5 @@ public static bool UninstallCertificate(string subjectName, StoreName storeName,
return false;
}
}

}
88 changes: 48 additions & 40 deletions MsmhToolsClass/MsmhToolsClass/DnsTool/GetIP.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
Expand Down Expand Up @@ -55,40 +54,43 @@ public static string GetIpFromSystem(string host, bool getIPv6 = false, bool als
/// <returns>Returns Empty List if fail</returns>
public static List<string> GetIpsFromSystem(string host, bool getIPv6 = false)
{
List<string> result = new();
List<string> ips = new();

try
{
//IPAddress[] ipAddresses = System.Net.Dns.GetHostAddresses(host);
IPAddress[] ipAddresses = System.Net.Dns.GetHostEntry(host).AddressList;

if (ipAddresses == null || ipAddresses.Length == 0)
return result;
if (ipAddresses == null || ipAddresses.Length == 0) return ips;

if (!getIPv6)
{
for (int n = 0; n < ipAddresses.Length; n++)
{
AddressFamily addressFamily = ipAddresses[n].AddressFamily;
string isStr = ipAddresses[n].ToString();
if (addressFamily != AddressFamily.InterNetworkV6) result.Add(isStr);
string ip = ipAddresses[n].ToString();
if (addressFamily != AddressFamily.InterNetworkV6)
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip))
ips.Add(ip);
}
}
else
{
for (int n = 0; n < ipAddresses.Length; n++)
{
AddressFamily addressFamily = ipAddresses[n].AddressFamily;
string isStr = ipAddresses[n].ToString();
if (addressFamily == AddressFamily.InterNetworkV6) result.Add(isStr);
string ip = ipAddresses[n].ToString();
if (addressFamily == AddressFamily.InterNetworkV6)
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip))
ips.Add(ip);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return result;
return ips;
}

//================================================= Get From DoH Using Wire Format
Expand All @@ -101,9 +103,9 @@ public static List<string> GetIpsFromSystem(string host, bool getIPv6 = false)
/// <param name="timeoutSec">Timeout (Sec)</param>
/// <param name="proxyScheme">Use Proxy to Get IP</param>
/// <returns>Returns string.Empty if fail</returns>
public static async Task<string> GetIpFromDohUsingWireFormat(string host, string doh, int timeoutSec, string? proxyScheme = null)
public static async Task<string> GetIpFromDohUsingWireFormat(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
List<string> ips = await GetIpsFromDohUsingWireFormat(host, doh, timeoutSec, proxyScheme);
List<string> ips = await GetIpsFromDohUsingWireFormat(host, doh, timeoutSec, proxyScheme, proxyUsername, proxyPassword);
if (ips.Any()) return ips[0];
return string.Empty;
}
Expand All @@ -116,12 +118,12 @@ public static async Task<string> GetIpFromDohUsingWireFormat(string host, string
/// <param name="timeoutSec">Timeout (Sec)</param>
/// <param name="proxyScheme">Use Proxy to Get IPs</param>
/// <returns>Returns an Empty List if fail</returns>
public static async Task<List<string>> GetIpsFromDohUsingWireFormat(string host, string doh, int timeoutSec, string? proxyScheme = null)
public static async Task<List<string>> GetIpsFromDohUsingWireFormat(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
if (string.IsNullOrEmpty(proxyScheme))
return await GetIpsFromDohUsingWireFormatNoProxy(host, doh, timeoutSec);
else
return await GetIpsFromDohUsingWireFormatProxy(host, doh, timeoutSec, proxyScheme);
return await GetIpsFromDohUsingWireFormatProxy(host, doh, timeoutSec, proxyScheme, proxyUsername, proxyPassword);
}

private static async Task<List<string>> GetIpsFromDohUsingWireFormatNoProxy(string host, string doh, int timeoutSec)
Expand Down Expand Up @@ -162,20 +164,17 @@ private static async Task<List<string>> GetIpsFromDohUsingWireFormatNoProxy(stri
{
DnsResourceRecord drr = answer.Answers[n];
string? ip = drr.Resource?.ToString();
if (!string.IsNullOrEmpty(ip) && IPAddress.TryParse(ip, out IPAddress? _))
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip) && IPAddress.TryParse(ip, out IPAddress? _))
ips.Add(ip);
}
}
}
catch (Exception)
{
// do nothing
}
catch (Exception) { }

return ips;
}

private static async Task<List<string>> GetIpsFromDohUsingWireFormatProxy(string host, string doh, int timeoutSec, string? proxyScheme = null)
private static async Task<List<string>> GetIpsFromDohUsingWireFormatProxy(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
List<string> ips = new();

Expand All @@ -189,7 +188,7 @@ private static async Task<List<string>> GetIpsFromDohUsingWireFormatProxy(string
Uri uri = new(doh);

using SocketsHttpHandler socketsHttpHandler = new();
socketsHttpHandler.Proxy = new WebProxy(proxyScheme, true);
socketsHttpHandler.Proxy = new WebProxy(proxyScheme, true, null, new NetworkCredential(proxyUsername, proxyPassword));

using HttpClient httpClient = new(socketsHttpHandler);
httpClient.Timeout = new TimeSpan(0, 0, timeoutSec);
Expand All @@ -216,15 +215,12 @@ private static async Task<List<string>> GetIpsFromDohUsingWireFormatProxy(string
{
DnsResourceRecord drr = answer.Answers[n];
string? ip = drr.Resource?.ToString();
if (!string.IsNullOrEmpty(ip) && IPAddress.TryParse(ip, out IPAddress? _))
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip) && IPAddress.TryParse(ip, out IPAddress? _))
ips.Add(ip);
}
}
}
catch (Exception)
{
// do nothing
}
catch (Exception) { }

return ips;
}
Expand All @@ -239,9 +235,9 @@ private static async Task<List<string>> GetIpsFromDohUsingWireFormatProxy(string
/// <param name="timeoutSec">Timeout (Sec)</param>
/// <param name="proxyScheme">Use Proxy to Get IP</param>
/// <returns>Returns Empty List if fail</returns>
public static async Task<string> GetIpFromDohUsingJsonFormat(string host, string doh, int timeoutSec, string? proxyScheme = null)
public static async Task<string> GetIpFromDohUsingJsonFormat(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
List<string> ips = await GetIpsFromDohUsingJsonFormat(host, doh, timeoutSec, proxyScheme);
List<string> ips = await GetIpsFromDohUsingJsonFormat(host, doh, timeoutSec, proxyScheme, proxyUsername, proxyPassword);
if (ips.Any()) return ips[0];
return string.Empty;
}
Expand All @@ -254,12 +250,12 @@ public static async Task<string> GetIpFromDohUsingJsonFormat(string host, string
/// <param name="timeoutSec">Timeout (Sec)</param>
/// <param name="proxyScheme">Use Proxy to Get IP</param>
/// <returns>Returns Empty List if fail</returns>
public static async Task<List<string>> GetIpsFromDohUsingJsonFormat(string host, string doh, int timeoutSec, string? proxyScheme = null)
public static async Task<List<string>> GetIpsFromDohUsingJsonFormat(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
if (string.IsNullOrEmpty(proxyScheme))
return await GetIpFromDohUsingJsonFormatNoProxy(host, doh, timeoutSec);
else
return await GetIpFromDohUsingJsonFormatProxy(host, doh, timeoutSec, proxyScheme);
return await GetIpFromDohUsingJsonFormatProxy(host, doh, timeoutSec, proxyScheme, proxyUsername, proxyPassword);
}

private static async Task<List<string>> GetIpFromDohUsingJsonFormatNoProxy(string host, string doh, int timeoutSec)
Expand Down Expand Up @@ -301,7 +297,7 @@ private static async Task<List<string>> GetIpFromDohUsingJsonFormatNoProxy(strin
return ips;
}

private static async Task<List<string>> GetIpFromDohUsingJsonFormatProxy(string host, string doh, int timeoutSec, string? proxyScheme = null)
private static async Task<List<string>> GetIpFromDohUsingJsonFormatProxy(string host, string doh, int timeoutSec, string? proxyScheme = null, string? proxyUsername = null, string? proxyPassword = null)
{
List<string> ips = new();

Expand All @@ -315,7 +311,7 @@ private static async Task<List<string>> GetIpFromDohUsingJsonFormatProxy(string
Uri uri = new(doh + path);

using SocketsHttpHandler socketsHttpHandler = new();
socketsHttpHandler.Proxy = new WebProxy(proxyScheme, true);
socketsHttpHandler.Proxy = new WebProxy(proxyScheme, true, null, new NetworkCredential(proxyUsername, proxyPassword));

using HttpClient httpClient = new(socketsHttpHandler);
httpClient.Timeout = new TimeSpan(0, 0, timeoutSec);
Expand Down Expand Up @@ -371,7 +367,7 @@ private static List<string> GetIpFromJson(string jsonContent)
if (answerProperty.Name == "data")
{
string ip = answerProperty.Value.ToString();
if (IPAddress.TryParse(ip, out IPAddress? _))
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip) && IPAddress.TryParse(ip, out IPAddress? _))
ips.Add(ip);
}
}
Expand All @@ -390,6 +386,11 @@ private static List<string> GetIpFromJson(string jsonContent)

//================================================= Get From Plain DNS

public enum PlainDnsProtocol
{
UDP, TCP, Both
}

/// <summary>
/// Get First IP in Answer Section
/// </summary>
Expand All @@ -398,9 +399,9 @@ private static List<string> GetIpFromJson(string jsonContent)
/// <param name="dnsPort">Plain DNS Port</param>
/// <param name="timeoutMS">Timeout (ms)</param>
/// <returns>Returns string.Empty if fail</returns>
public static async Task<string> GetIpFromPlainDNS(string host, string dnsIP, int dnsPort, int timeoutSec)
public static async Task<string> GetIpFromPlainDNS(string host, string dnsIP, int dnsPort, int timeoutSec, PlainDnsProtocol protocol = PlainDnsProtocol.Both)
{
List<string> ips = await GetIpsFromPlainDNS(host, dnsIP, dnsPort, timeoutSec);
List<string> ips = await GetIpsFromPlainDNS(host, dnsIP, dnsPort, timeoutSec, protocol);
if (ips.Any()) return ips[0];
return string.Empty;
}
Expand All @@ -413,13 +414,20 @@ public static async Task<string> GetIpFromPlainDNS(string host, string dnsIP, in
/// <param name="dnsPort">Plain DNS Port</param>
/// <param name="timeoutMS">Timeout (ms)</param>
/// <returns>Returns an Empty List if fail</returns>
public async static Task<List<string>> GetIpsFromPlainDNS(string host, string dnsIP, int dnsPort, int timeoutSec)
public async static Task<List<string>> GetIpsFromPlainDNS(string host, string dnsIP, int dnsPort, int timeoutSec, PlainDnsProtocol protocol = PlainDnsProtocol.Both)
{
List<string> ips;

ips = await GetIpsFromPlainDnsUdp(host, dnsIP, dnsPort, timeoutSec);
if (!ips.Any())
if (protocol == PlainDnsProtocol.UDP)
ips = await GetIpsFromPlainDnsUdp(host, dnsIP, dnsPort, timeoutSec);
else if (protocol == PlainDnsProtocol.TCP)
ips = await GetIpsFromPlainDnsTcp(host, dnsIP, dnsPort, timeoutSec);
else
{
ips = await GetIpsFromPlainDnsUdp(host, dnsIP, dnsPort, timeoutSec);
if (!ips.Any())
ips = await GetIpsFromPlainDnsTcp(host, dnsIP, dnsPort, timeoutSec);
}

return ips;
}
Expand Down Expand Up @@ -465,7 +473,7 @@ private async static Task<List<string>> GetIpsFromPlainDnsUdp(string host, strin
{
DnsResourceRecord drr = answer.Answers[n];
string? ip = drr.Resource?.ToString();
if (!string.IsNullOrEmpty(ip) && IPAddress.TryParse(ip, out IPAddress? _))
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip) && IPAddress.TryParse(ip, out IPAddress? _))
ips.Add(ip);
}
}
Expand Down Expand Up @@ -532,7 +540,7 @@ private async static Task<List<string>> GetIpsFromPlainDnsTcp(string host, strin
{
DnsResourceRecord drr = answer.Answers[n];
string? ip = drr.Resource?.ToString();
if (!string.IsNullOrEmpty(ip) && IPAddress.TryParse(ip, out IPAddress? _))
if (!string.IsNullOrEmpty(ip) && !NetworkTool.IsLocalIP(ip) && IPAddress.TryParse(ip, out IPAddress? _))
ips.Add(ip);
}
}
Expand Down
Loading

0 comments on commit 06b513f

Please sign in to comment.