Initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Models.Model.Backend;
|
||||
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static class FilesystemHelper
|
||||
{
|
||||
public static DatabaseSizes GetDatabaseSizes()
|
||||
{
|
||||
DatabaseSizes databaseSizes = new();
|
||||
|
||||
FileInfo fileInfo = new("../../../../Models/mydb.db");
|
||||
databaseSizes.MyDbSize = fileInfo.Length.ToSize(SizeUnits.KB);
|
||||
|
||||
databaseSizes.DiscardedDbSize = GetDiscardedDbSizes().ToSize(SizeUnits.KB);
|
||||
|
||||
fileInfo = new("../../../../Models/Filtered.db");
|
||||
databaseSizes.FilteredDbSize = fileInfo.Length.ToSize(SizeUnits.KB);
|
||||
|
||||
return databaseSizes;
|
||||
}
|
||||
|
||||
private static long GetDiscardedDbSizes()
|
||||
{
|
||||
const string folder = "../../../../Models";
|
||||
string[] files = Directory.GetFiles(folder, "*.db");
|
||||
|
||||
long size = 0;
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
if (!files[i].Contains("Discarded")) continue;
|
||||
FileInfo fileInfo = new(files[i]);
|
||||
size += fileInfo.Length;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
private static double ToSize(this long value, SizeUnits unit)
|
||||
{
|
||||
return double.Parse((value / Math.Pow(1024, (long)unit)).ToString("0.00"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static partial class FilterHelper
|
||||
{
|
||||
// https://stackoverflow.com/a/56116499
|
||||
private const string DomainPattern = @"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$";
|
||||
private const string TitlePattern = "<title>(.*)</title>";
|
||||
private const string DescriptionPattern = "<meta name=\"description\" content=\"(.*?)\"";
|
||||
|
||||
public static void GetDomain(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "CN=";
|
||||
|
||||
if (!data.Contains("subject:") || !data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
|
||||
bool isValid = ValidateUrl(result);
|
||||
|
||||
if (!isValid)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetServerType(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "Server:";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetTitle(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
|
||||
Regex titleRegex = MyRegex1();
|
||||
Match match = titleRegex.Match(data);
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
result = match.Groups[1].Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetDescription(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
|
||||
Regex titleRegex = MyRegex2();
|
||||
Match match = titleRegex.Match(data);
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
result = match.Groups[1].Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetHttpVersion(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "> HEAD / ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetALPN(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "* ALPN: server accepted ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetCertificateIssuerCountry(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "issuer: C=";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.IndexOf(';', StringComparison.Ordinal);
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetCertificateOrganizationName(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "O=";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.IndexOf(';', StringComparison.Ordinal);
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetIpV6(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "IPv6: ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetTlsVersion(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "SSL connection using ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.IndexOf('/') - 1;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetConnection(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "< Connection: ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetEncoding(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "< Vary: ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetPublicKeyType(string data, out string result, string searchTerm)
|
||||
{
|
||||
result = "";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
||||
int end = data.Length;
|
||||
|
||||
result = GetSubstring(data, start, end);
|
||||
}
|
||||
|
||||
public static void GetKeyExchangeAlgorithm(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "* SSL connection using ";
|
||||
|
||||
if (!data.Contains(searchTerm) || string.IsNullOrWhiteSpace(data)) return;
|
||||
|
||||
string[] temp = data.Split('/');
|
||||
|
||||
switch (temp.Length)
|
||||
{
|
||||
case 0:
|
||||
return;
|
||||
case 2:
|
||||
result = $"{temp[1].Trim()} UUHHHMMM";
|
||||
return;
|
||||
default:
|
||||
result = temp[2].Trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetCipherSuite(string data, out string result)
|
||||
{
|
||||
result = "";
|
||||
const string searchTerm = "* SSL connection using ";
|
||||
|
||||
if (!data.Contains(searchTerm)) return;
|
||||
|
||||
string[] temp = data.Split('/');
|
||||
|
||||
result = temp[1].Trim();
|
||||
}
|
||||
|
||||
private static string GetSubstring(string input, int start, int end)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (start >= end)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return input.AsSpan().Slice(start, end - start).ToString();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"start index {start} end index {end}");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static bool ValidateUrl(string url)
|
||||
{
|
||||
Regex rgx = MyRegex();
|
||||
return rgx.IsMatch(url);
|
||||
}
|
||||
|
||||
//Generate the RegEx at compile time.
|
||||
[GeneratedRegex(DomainPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-US")]
|
||||
private static partial Regex MyRegex();
|
||||
|
||||
[GeneratedRegex(TitlePattern)]
|
||||
private static partial Regex MyRegex1();
|
||||
|
||||
[GeneratedRegex(DescriptionPattern)]
|
||||
private static partial Regex MyRegex2();
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static class HttpClientHelper
|
||||
{
|
||||
public static async Task<string> GetHtml(string url, int port)
|
||||
{
|
||||
using HttpClient client = new();
|
||||
|
||||
if (port == 80)
|
||||
{
|
||||
client.BaseAddress = new($"http://{url}");
|
||||
}
|
||||
else
|
||||
{
|
||||
client.BaseAddress = new($"https://{url}");
|
||||
}
|
||||
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
|
||||
try
|
||||
{
|
||||
response = await client.GetAsync("/");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
if (response is null || !response.IsSuccessStatusCode)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public static async Task<bool> HasRobotsTxt(string url, int port)
|
||||
{
|
||||
using HttpClient client = new();
|
||||
|
||||
if (port == 80)
|
||||
{
|
||||
client.BaseAddress = new($"http://{url}");
|
||||
}
|
||||
else
|
||||
{
|
||||
client.BaseAddress = new($"https://{url}");
|
||||
}
|
||||
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
|
||||
try
|
||||
{
|
||||
response = await client.GetAsync("/robots.txt");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return response is not null && response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using FuzzySharp;
|
||||
using Models.Handler;
|
||||
using Models.Model.External;
|
||||
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static class SearchHelper
|
||||
{
|
||||
public static SearchResults Search(string searchText, DbHandler dbHandler)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(searchText))
|
||||
{
|
||||
SearchResult searchResult = new();
|
||||
searchResult.Description = "asd";
|
||||
searchResult.Title = "asd";
|
||||
searchResult.Url = "asd";
|
||||
SearchResults searchResults = new();
|
||||
searchResults.Results =
|
||||
[
|
||||
searchResult
|
||||
];
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
List<SearchResult?> temp = dbHandler.GetSearchResults();
|
||||
SearchResults searchResultsList = new();
|
||||
|
||||
for (int i = 0; i < temp.Count; i++)
|
||||
{
|
||||
if (temp[i] is null) continue;
|
||||
|
||||
SearchResult result = new();
|
||||
|
||||
if (Fuzz.Ratio(searchText, temp[i]!.Url) <= 50 && Fuzz.Ratio(searchText, temp[i]!.Title) <= 50) continue;
|
||||
|
||||
result.Url = temp[i]?.Url;
|
||||
result.Title = temp[i]?.Title;
|
||||
|
||||
searchResultsList.Results.Add(result);
|
||||
}
|
||||
|
||||
return searchResultsList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static class TcpClientHelper
|
||||
{
|
||||
public static (int, int) CheckPort(string ip, params int[] ports)
|
||||
{
|
||||
// This would be way cleaner if the TcpClient didn't throw an exception if the destination couldn't be reached,
|
||||
// and it would just return a result.error, for example.
|
||||
for (int i = 0; i < ports.Length; i++) {
|
||||
try
|
||||
{
|
||||
using TcpClient client = new();
|
||||
client.Connect(ip, ports[i]);
|
||||
// If the connection is successful, update the result array with the port number
|
||||
}
|
||||
catch
|
||||
{
|
||||
ports[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return (ports[0], ports[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Backend.Helper;
|
||||
|
||||
public static class Urlverifier
|
||||
{
|
||||
public static bool VerifyUrl(this string url)
|
||||
{
|
||||
return url.Contains("https://") || url.Contains("http://");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user