From ac57efa250b6ae7f85a0b52c88f8932cc3e99ad4 Mon Sep 17 00:00:00 2001 From: Alexy DA CRUZ Date: Tue, 14 Nov 2023 17:12:10 +0100 Subject: [PATCH 1/3] (WIP) Add isValidDomainName method to check for valid domain names based on RFC 1035 --- SRC/Aura_OS/System/Utils/Misc.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/SRC/Aura_OS/System/Utils/Misc.cs b/SRC/Aura_OS/System/Utils/Misc.cs index c6af741b..728daa1c 100644 --- a/SRC/Aura_OS/System/Utils/Misc.cs +++ b/SRC/Aura_OS/System/Utils/Misc.cs @@ -31,6 +31,36 @@ public static bool IsIPv4Address(string[] items) } } + /// + /// Check if the domain name is valid based on information from RFC 1035 + /// + /// Valid or not valid domain name + /// Boolean depending of the domain name param + 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; + } + + // to implement + // string pattern = @"^(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$"; + // https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address + + return true; + } + public static bool IsIPv4Address(string ip) { string[] items = ip.Split('.'); From b76afc63a63255448679daee4e08ece38d586203 Mon Sep 17 00:00:00 2001 From: Alexy DA CRUZ Date: Mon, 20 Nov 2023 16:24:52 +0100 Subject: [PATCH 2/3] Add Regex class implementation --- SRC/Aura_OS/System/Utils/Regex.cs | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 SRC/Aura_OS/System/Utils/Regex.cs diff --git a/SRC/Aura_OS/System/Utils/Regex.cs b/SRC/Aura_OS/System/Utils/Regex.cs new file mode 100644 index 00000000..b750fcdf --- /dev/null +++ b/SRC/Aura_OS/System/Utils/Regex.cs @@ -0,0 +1,80 @@ +/* +* PROJECT: Aura Operating System Development +* CONTENT: Implement Regex +* PROGRAMMER(S): Alexy DA CRUZ +*/ + +using System.Collections.Generic; +using System.Text; + +namespace Aura_OS.System.Utils +{ + public class Regex + { + private readonly List patterns; + + public Regex(params string[] patterns) + { + this.patterns = new List(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; + } + } +} \ No newline at end of file From c52d8a080f8228f0b7b2d19007b00ac3529f04c2 Mon Sep 17 00:00:00 2001 From: Alexy DA CRUZ Date: Mon, 20 Nov 2023 16:24:56 +0100 Subject: [PATCH 3/3] Fix domain name validation and add IP address validation and command line parsing --- SRC/Aura_OS/System/Utils/Misc.cs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/SRC/Aura_OS/System/Utils/Misc.cs b/SRC/Aura_OS/System/Utils/Misc.cs index 728daa1c..85820301 100644 --- a/SRC/Aura_OS/System/Utils/Misc.cs +++ b/SRC/Aura_OS/System/Utils/Misc.cs @@ -36,6 +36,7 @@ public static bool IsIPv4Address(string[] items) /// /// Valid or not valid domain name /// Boolean depending of the domain name param + // https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address public static bool isValidDomainName(string domainName) { if (domainName.Length < 1) @@ -49,18 +50,28 @@ public static bool isValidDomainName(string domainName) } string[] labels = domainName.Split('.'); - if (labels.length > 63) + if (labels.Length > 63) { return false; } - // to implement - // string pattern = @"^(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$"; - // https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address - + string pattern = @"^(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,}$"; + Regex regex = new Regex(pattern); + + if (regex.IsMatch(domainName)) { + return true; + } else { + return false + } + return true; } + /// + /// Check if the IP address is valid based on information from RFC 791 + /// + /// + /// public static bool IsIPv4Address(string ip) { string[] items = ip.Split('.'); @@ -74,6 +85,12 @@ public static bool IsIPv4Address(string ip) } } + + /// + /// Parse a command line + /// + /// + /// //https://stackoverflow.com/questions/59638467/parsing-command-line-args-with-quotes public static List ParseCommandLine(string cmdLine) {