8 using System.Text.RegularExpressions;
28 [GeneratedRegex(
@"(?:[a-z0-9-](?:[a-z0-9-]{0,61}[a-z0-9-])?\.)+[a-z0-9-][a-z0-9-]{0,61}[a-z0-9-]", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
31 public static List<string>
ValidTlds {
get; } =
new List<string>();
42 if (
string.IsNullOrEmpty(tld) ||
string.IsNullOrEmpty(domainName)) {
throw new MalformedDomainException($
"{ domainName }.{ tld }"); }
61 var splits = fqdn.Split(
'.').Where(x => x !=
".").ToArray();
89 public int Id {
get;
set; }
97 public string Tld {
get;
set; }
133 public static bool IsValidDomain(
string fqdn, out
int? tldLevels, out
string? tld, out
string? domain) {
139 string.IsNullOrEmpty(fqdn) ||
140 string.IsNullOrWhiteSpace(fqdn) ||
146 fqdn = fqdn.ToLowerInvariant();
148 var containsValidTld =
false;
149 var domainLevels = fqdn.Split(
'.').Where(x => x !=
".").ToList();
150 var firstNonTopLevel = -1;
154 for (
int i = 0; i < domainLevels.Count; i++) {
156 firstNonTopLevel = i;
160 containsValidTld =
true;
163 if (firstNonTopLevel == 0) {
167 firstNonTopLevel = 1;
170 tldLevels = firstNonTopLevel;
171 tld =
string.Join(
'.', domainLevels.GetRange(0, firstNonTopLevel));
172 domain =
string.Join(
'.', domainLevels.GetRange(firstNonTopLevel, domainLevels.Count - firstNonTopLevel));
174 return containsValidTld;
177 private static bool ContainsInvalidChars(
char c) => !
char.IsDigit(c) && !
char.IsLetter(c) && c !=
'-' && c !=
'.';
184 public static bool IsFqdnInBounds(
string fqdn) => fqdn.Length is (<= 255) and (> 0);
202 using var httpClient =
new HttpClient { };
203 using var httpResponse = httpClient.GetAsync(
IanaValidTldListUrl).GetAwaiter().GetResult();
205 if (httpResponse is
null) {
return ValidTlds.Count > 0; }
207 using (var sReader =
new StreamReader(httpResponse.Content.ReadAsStream())) {
209 while (sReader.Peek() != -1) {
210 var line = sReader.ReadLine();
211 if (line is
null) {
break; }
212 if (line.StartsWith(
"#") || line.Length == 0) {
continue; }
218 return ValidTlds.Count > 0;
226 using var httpClient =
new HttpClient { };
229 if (httpResponse is
null) {
return ValidTlds.Count > 0; }
231 using (var sReader =
new StreamReader(await httpResponse.Content.ReadAsStreamAsync())) {
233 while (sReader.Peek() != -1) {
234 var line = await sReader.ReadLineAsync();
235 if (line is
null) {
break; }
236 if (line.StartsWith(
"#") || line.Length == 0) {
continue; }
242 return ValidTlds.Count > 0;
Represents a single domain.
string DomainName
The name of the domain.
static bool IsFqdnInBounds(string fqdn)
Gets a value indicating whether or not an FQDN is within bounds set by the ICANN.
Domain(Domain domain, bool includeUsers=false)
Copy-constructor.
static bool DownloadCurrentValidTldListFromIana()
Downloads the latest list of currently valid TLDs from IANA.
static bool ContainsInvalidChars(char c)
const string IanaValidTldListUrl
List< DomainUser > DomainUsers
A list of users belonging to the domain.
static List< string > ValidTlds
Domain(int id, string fqdn, params DomainUser[] users)
Constructs a new instance of this class.
Domain(int id, string tld, string domainName, params DomainUser[] users)
Creates a new instance of this class.
string Tld
The top-level domain.
static bool IsValidTld(string tld)
Gets a value indicating whether or not the tld is valid.
static partial Regex DomainRegex()
Domain-validation RegEx inspired by .
static bool IsValidDomain(string fqdn, out int? tldLevels, out string? tld, out string? domain)
Gets a value indicating whether or not a given domain is valid.
static async Task< bool > DownloadCurrentValidTldListFromIanaAsync()
Asynchronously downloads the latest list of TLDs from the IANA.
This class represents a single user (email account) in a domain.
Exception class that is thrown when a malformed FQDN (fully-qualified domain name) was passed somewhe...