Make nameservers configurable

This commit is contained in:
Carlos Quintana 2022-02-24 15:05:05 +01:00
parent 7da06ba424
commit 8f339923f8
No known key found for this signature in database
GPG Key ID: 9A3A2DE1C3E2A4B1
3 changed files with 28 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import socket
import string import string
import subprocess import subprocess
from ast import literal_eval from ast import literal_eval
from typing import Callable, List from typing import Callable, List, Optional
from urllib.parse import urlparse from urllib.parse import urlparse
from dotenv import load_dotenv from dotenv import load_dotenv
@ -37,6 +37,13 @@ def sl_getenv(env_var: str, default_factory: Callable = None):
return literal_eval(value) return literal_eval(value)
def env(var: str, default: Optional[str] = None) -> Optional[str]:
if var in os.environ:
return os.environ[var]
else:
return default
config_file = os.environ.get("CONFIG") config_file = os.environ.get("CONFIG")
if config_file: if config_file:
config_file = get_abs_path(config_file) config_file = get_abs_path(config_file)
@ -431,3 +438,11 @@ def get_allowed_redirect_domains() -> List[str]:
ALLOWED_REDIRECT_DOMAINS = get_allowed_redirect_domains() ALLOWED_REDIRECT_DOMAINS = get_allowed_redirect_domains()
def setup_nameservers():
nameservers = env("NAMESERVERS", "1.1.1.1")
return nameservers.split(",")
NAMESERVERS = setup_nameservers()

View File

@ -1,3 +1,4 @@
from app import config
from typing import Optional, List, Tuple from typing import Optional, List, Tuple
import dns.resolver import dns.resolver
@ -5,16 +6,14 @@ import dns.resolver
def _get_dns_resolver(): def _get_dns_resolver():
my_resolver = dns.resolver.Resolver() my_resolver = dns.resolver.Resolver()
my_resolver.nameservers = config.NAMESERVERS
# 1.1.1.1 is CloudFlare's public DNS server
my_resolver.nameservers = ["1.1.1.1"]
return my_resolver return my_resolver
def get_ns(hostname) -> [str]: def get_ns(hostname) -> [str]:
try: try:
answers = _get_dns_resolver().resolve(hostname, "NS") answers = _get_dns_resolver().resolve(hostname, "NS", search=True)
except Exception: except Exception:
return [] return []
return [a.to_text() for a in answers] return [a.to_text() for a in answers]
@ -23,7 +22,7 @@ def get_ns(hostname) -> [str]:
def get_cname_record(hostname) -> Optional[str]: def get_cname_record(hostname) -> Optional[str]:
"""Return the CNAME record if exists for a domain, WITHOUT the trailing period at the end""" """Return the CNAME record if exists for a domain, WITHOUT the trailing period at the end"""
try: try:
answers = _get_dns_resolver().resolve(hostname, "CNAME") answers = _get_dns_resolver().resolve(hostname, "CNAME", search=True)
except Exception: except Exception:
return None return None
@ -39,7 +38,7 @@ def get_mx_domains(hostname) -> [(int, str)]:
domain name ends with a "." at the end. domain name ends with a "." at the end.
""" """
try: try:
answers = _get_dns_resolver().resolve(hostname, "MX") answers = _get_dns_resolver().resolve(hostname, "MX", search=True)
except Exception: except Exception:
return [] return []
@ -60,7 +59,7 @@ _include_spf = "include:"
def get_spf_domain(hostname) -> [str]: def get_spf_domain(hostname) -> [str]:
"""return all domains listed in *include:*""" """return all domains listed in *include:*"""
try: try:
answers = _get_dns_resolver().resolve(hostname, "TXT") answers = _get_dns_resolver().resolve(hostname, "TXT", search=True)
except Exception: except Exception:
return [] return []
@ -82,7 +81,7 @@ def get_spf_domain(hostname) -> [str]:
def get_txt_record(hostname) -> [str]: def get_txt_record(hostname) -> [str]:
"""return all domains listed in *include:*""" """return all domains listed in *include:*"""
try: try:
answers = _get_dns_resolver().resolve(hostname, "TXT") answers = _get_dns_resolver().resolve(hostname, "TXT", search=True)
except Exception: except Exception:
return [] return []

View File

@ -174,4 +174,8 @@ DISABLE_ONBOARDING=true
#ALIAS_AUTOMATIC_DISABLE=true #ALIAS_AUTOMATIC_DISABLE=true
# domains that can be present in the &next= section when using absolute urls # domains that can be present in the &next= section when using absolute urls
ALLOWED_REDIRECT_DOMAINS=[] ALLOWED_REDIRECT_DOMAINS=[]
# DNS nameservers to be used by the app
# Multiple nameservers can be specified, separated by ','
NAMESERVERS="1.1.1.1"