+ # Matches the following:
+ # - https://baddomain.net
+ # - https://extra.baddomain.net/
+ # Does NOT match the following:
+ # - https://maybebaddomain.net/
+
+ # *.baddomain.net
+ def subdomain_regex("*." <> domain), do: subdomain_regex(domain)
+
+ # baddomain.net
+ def subdomain_regex(domain) do
+ if String.ends_with?(domain, ".*") do
+ ~r/^(.+\.)?#{Regex.escape(String.replace_suffix(domain, ".*", ""))}\.(.+)$/i
+ else
+ ~r/^(.+\.)?#{Regex.escape(domain)}$/i
+ end
+ end
+