EonaCat.DnsTester/EonaCat.DnsTester/Helpers/UrlHelper.cs

107 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace EonaCat.DnsTester.Helpers
{
internal class UrlHelper
{
private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create();
public static event EventHandler<string> Log;
public static void GetRandomUrls(ref List<string> urlList, int totalUrls)
{
var letters = GetRandomLetters();
Dictionary<string, string> searchEngineUrls = new Dictionary<string, string>
{
{ "Yahoo", "https://search.yahoo.com/search?p=" },
{ "Bing", "https://www.bing.com/search?q=" },
{ "Google", "https://www.google.com/search?q=" },
{ "Ask", "https://www.ask.com/web?q=" },
{ "WolframAlpha", "https://www.wolframalpha.com/input/?i=" },
{ "StartPage", "https://www.startpage.com/do/dsearch?query=" },
{ "Yandex", "https://www.yandex.com/search/?text=" },
{ "Qwant", "https://www.qwant.com/?q=" }
};
Random rand = new Random();
List<string> urls = urlList;
while (urls.Count < totalUrls)
{
int index = rand.Next(searchEngineUrls.Count);
KeyValuePair<string, string> searchEngine = searchEngineUrls.ElementAt(index);
string url = searchEngine.Value + letters;
try
{
using (var client = new WebClient())
{
string responseString = client.DownloadString(url);
// find all .xxx.com addresses
MatchCollection hostNames = Regex.Matches(responseString, @"[.](\w+[.]com)");
// Loop through the match collection to retrieve all matches and delete the leading "."
HashSet<string> uniqueNames = new HashSet<string>();
foreach (Match match in hostNames)
{
string name = match.Groups[1].Value;
if (name != $"{searchEngine.Key.ToLower()}.com")
{
uniqueNames.Add(name);
}
}
// Add the names to the list
foreach (string name in uniqueNames)
{
if (urls.Count >= totalUrls)
{
break;
}
if (!urls.Contains(name))
{
urls.Add(name);
}
}
}
}
catch (Exception ex)
{
searchEngineUrls.Remove(searchEngine.Key);
SetStatus($"{searchEngine.Key}: {ex.Message}");
}
finally
{
letters = GetRandomLetters();
}
}
urlList = urls;
var urlText = "url" + (urlList.Count > 1 ? "'s" : string.Empty);
SetStatus($"{urlList.Count} random {urlText} found");
}
private static string GetRandomLetters()
{
// Generate a cryptographically strong random string
byte[] randomBytes = new byte[32];
_randomNumberGenerator.GetBytes(randomBytes);
return Convert.ToBase64String(randomBytes);
}
private static void SetStatus(string text)
{
Log?.Invoke(null, text);
}
}
}