diff options
-rwxr-xr-x | tool/aio.py | 22 | ||||
-rw-r--r-- | tool/modules/config.py | 5 | ||||
-rw-r--r-- | tool/modules/dns.py | 42 | ||||
-rwxr-xr-x | tool/modules/nginx.py | 4 |
4 files changed, 67 insertions, 6 deletions
diff --git a/tool/aio.py b/tool/aio.py index 6678b63..a4d84fd 100755 --- a/tool/aio.py +++ b/tool/aio.py @@ -26,6 +26,7 @@ from modules.backup import * from modules.download_tools import * from modules.helper import * from modules.test import * +from modules.dns import * console = Console() @@ -108,6 +109,10 @@ test_parser = subparsers.add_parser("test", help="Test things.") test_parser.add_argument( "test_action", help="Test action.", choices=["crupest-api"]) +dns_parser = subparsers.add_parser("dns", help="Generate dns zone.") + +dns_parser.add_argument("-i", "--ip", help="IP address of the server.") + args = parser.parse_args() if args.yes: @@ -123,9 +128,9 @@ if args.yes: Confirm.ask = new_ask -if args.action == "certbot": - if args.create or args.renew or args.expand: - args.no_hello = True +if (args.action == "certbot" and (args.create or args.renew or args.expand)) or (args.action == "dns" and args.ip is not None): + args.no_hello = True + args.no_bye_bye = True if not args.no_check_python_version: @@ -479,7 +484,16 @@ def run(): test_crupest_api(console) case _: console.print("Test action invalid.", style="red") - + case "dns": + domain = check_domain_is_defined() + if domain is not None: + if args.ip is None: + ip = Prompt.ask( + "Please enter your server ip", console=console) + else: + ip = args.ip + console.print(generate_dns_zone_with_dkim( + domain, ip), soft_wrap=True, highlight=False) case _: console.print("First let's check all the templates...") diff --git a/tool/modules/config.py b/tool/modules/config.py index 28b09a3..b9ad818 100644 --- a/tool/modules/config.py +++ b/tool/modules/config.py @@ -1,18 +1,19 @@ import pwd import grp import os +import typing from rich.prompt import Prompt from .path import config_file_path class ConfigVar: - def __init__(self, name: str, description: str, default_value_generator, /, default_value_for_ask=None): + def __init__(self, name: str, description: str, default_value_generator: typing.Callable[[], str] | str, /, default_value_for_ask=str | None): """Create a config var. Args: name (str): The name of the config var. description (str): The description of the config var. - default_value_generator (typing.Callable([], str) | str): The default value generator of the config var. If it is a string, it will be used as the input prompt and let user input the value. + default_value_generator (typing.Callable[[], str] | str): The default value generator of the config var. If it is a string, it will be used as the input prompt and let user input the value. """ self.name = name self.description = description diff --git a/tool/modules/dns.py b/tool/modules/dns.py new file mode 100644 index 0000000..0334353 --- /dev/null +++ b/tool/modules/dns.py @@ -0,0 +1,42 @@ +from os.path import * +from io import StringIO +import re +from .nginx import * + + +def generate_dns_zone(domain: str, ip: str, /, ttl: str | int = 600, *, enable_mail: bool = True, dkim: str | None = None) -> str: + result = f"$ORIGIN {domain}.\n\n" + result += "; A records\n" + result += f"@ {ttl} IN A {ip}\n" + subdomains = list_subdomain_names() + for subdomain in subdomains: + result += f"{subdomain} {ttl} IN A {ip}\n" + + if enable_mail: + result += "\n; MX records\n" + result += f"@ {ttl} IN MX 10 mail.{domain}.\n" + result += "\n; SPF record\n" + result += f"@ {ttl} IN TXT \"v=spf1 mx ~all\"\n" + if dkim is not None: + result += "\n; DKIM record\n" + result += f"mail._domainkey {ttl} IN TEXT \"{dkim}\"" + result += "\n; DMARC record\n" + result += "_dmarc {ttl} IN TXT \"v=DMARC1; p=none; rua=mailto:dmarc.report@{domain}; ruf=mailto:dmarc.report@{domain}; sp=none; ri=86400\"\n" + return result + + +def get_dkim_from_mailserver(domain: str) -> str | None: + dkim_path = join(data_dir, "dms/config/opendkim/keys", domain, "mail.txt") + if not exists(dkim_path): + return None + + buffer = StringIO() + subprocess.run(["sudo", "cat", dkim_path], stdout=buffer, check=True) + value = "" + for match in re.finditer("\"(.*)\"", str(buffer)): + value += match.groups[1] + return value + + +def generate_dns_zone_with_dkim(domain: str, ip: str, /, ttl: str | int = 600) -> str: + return generate_dns_zone(domain, ip, ttl, enable_mail=True, dkim=get_dkim_from_mailserver(domain)) diff --git a/tool/modules/nginx.py b/tool/modules/nginx.py index 087422b..4fc31cc 100755 --- a/tool/modules/nginx.py +++ b/tool/modules/nginx.py @@ -38,6 +38,10 @@ nginx_var_set = set.union(root_template.var_set, static_file_template.var_set, reverse_proxy_template.var_set) +def list_subdomain_names() -> list: + return [s["subdomain"] for s in server["sites"]] + + def list_subdomains(domain: str) -> list: return [f"{s['subdomain']}.{domain}" for s in server["sites"]] |