settings["numberofrules"] = numberofrules
mergefile.close()
- if outputfile is None:
- return finalfile
-
+# Dot-separated labels of [a-z0-9_-], hyphens not at label ends, at least two
+# labels. Expects a lowercased hostname.
+VALID_DOMAIN_REGEX = re.compile(
+ r"(?:[a-z0-9_]|[a-z0-9_][a-z0-9_-]*[a-z0-9_])"
+ r"(?:\.(?:[a-z0-9_]|[a-z0-9_][a-z0-9_-]*[a-z0-9_]))+"
+)
+
+
+def is_valid_domain(hostname):
+ """
+ Check whether a lowercased hostname looks like a valid domain name.
+
+ Parameters
+ ----------
+ hostname : str
+ The hostname to validate.
+
+ Returns
+ -------
+ valid : bool
+ Whether the hostname only contains characters allowed in a domain
+ name and has a valid label structure.
+ """
+
+ return bool(VALID_DOMAIN_REGEX.fullmatch(hostname))
+
+
def normalize_rule(rule, targetip, keep_domain_comments):
"""
Standardize and format the rule string provided.