Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isValidDomainName method #256

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions SRC/Aura_OS/System/Utils/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,47 @@ public static bool IsIPv4Address(string[] items)
}
}

/// <summary>
/// Check if the domain name is valid based on information from RFC 1035
/// </summary>
/// <param name="domainName">Valid or not valid domain name</param>
/// <returns>Boolean depending of the domain name param</returns>
// https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
public static bool isValidDomainName(string domainName)
{
if (domainName.Length < 1)
{
return false;
}

if (domainName.Length > 255)
{
return false;
}

string[] labels = domainName.Split('.');
if (labels.Length > 63)
{
return false;
}

string pattern = @"^(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not cover punycode domains https://regex101.com/r/54p0p4/1

Regex regex = new Regex(pattern);

if (regex.IsMatch(domainName)) {
return true;
} else {
return false
}

return true;
}

/// <summary>
/// Check if the IP address is valid based on information from RFC 791
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIPv4Address(string ip)
{
string[] items = ip.Split('.');
Expand All @@ -44,6 +85,12 @@ public static bool IsIPv4Address(string ip)
}
}


/// <summary>
/// Parse a command line
/// </summary>
/// <param name="cmdLine"></param>
/// <returns></returns>
//https://stackoverflow.com/questions/59638467/parsing-command-line-args-with-quotes
public static List<string> ParseCommandLine(string cmdLine)
{
Expand Down
80 changes: 80 additions & 0 deletions SRC/Aura_OS/System/Utils/Regex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* PROJECT: Aura Operating System Development
* CONTENT: Implement Regex
* PROGRAMMER(S): Alexy DA CRUZ <[email protected]>
*/

using System.Collections.Generic;
using System.Text;

namespace Aura_OS.System.Utils
{
public class Regex
{
private readonly List<string> patterns;

public Regex(params string[] patterns)
{
this.patterns = new List<string>(patterns);
}

public bool IsMatch(string input)
{
foreach (var pattern in patterns)
{
if (IsMatchPattern(input, pattern))
{
return true;
}
}
return false;
}

private bool IsMatchPattern(string input, string pattern)
{
int inputIndex = 0;
int patternIndex = 0;

while (inputIndex < input.Length && patternIndex < pattern.Length)
{
if (pattern[patternIndex] == '.')
{
// Dot matches any character
patternIndex++;
inputIndex++;
}
else if (pattern[patternIndex] == '*')
{
// Asterisk matches zero or more occurrences of the previous character
char prevChar = pattern[patternIndex - 1];

while (inputIndex < input.Length && input[inputIndex] == prevChar)
{
inputIndex++;
}

patternIndex++;
}
else
{
// Normal character, must match exactly
if (pattern[patternIndex] != input[inputIndex])
{
return false;
}

patternIndex++;
inputIndex++;
}
}

// Check for remaining characters in the pattern after the main loop
while (patternIndex < pattern.Length && pattern[patternIndex] == '*')
{
patternIndex++;
}

return inputIndex == input.Length && patternIndex == pattern.Length;
}
}
}
Loading