{"id":482513,"date":"2026-06-05T10:09:53","date_gmt":"2026-06-05T10:09:53","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=482513"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=482513","title":{"rendered":"\u0421\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0439 DDNS"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0434\u043e\u043c\u0430\u0448\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0430\u0447\u043e\u043a, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0438\u0441\u044c \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b . \u0422\u0430\u043a \u043a\u0430\u043a \u0437\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 IP \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0443 \u044f \u043f\u043b\u0430\u0442\u0438\u0442\u044c \u043d\u0435 \u0445\u043e\u0447\u0443, \u0442\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b DDNS \u043e\u0442 TP-Link. \u0418 \u0430\u0434\u0440\u0435\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b <a href=\"https:\/\/my-adress.tplinkdns.com\" rel=\"noopener noreferrer nofollow\">https:\/\/my-adress.tplinkdns.com<\/a>. TP-Link \u0434\u0430\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d \u0438 \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b 2-\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0443\u043c\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ddclient \u0438 Cloudfare. \u0412\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c \u0447\u0442\u043e Cloudfare \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 ru \u0437\u043e\u043d\u043e\u0439. \u0412\u043e\u0442 \u0440\u0430\u0441\u0438\u0441\u0442\u044b!<\/p>\n<p>\u041f\u043e\u0438\u0441\u043a\u0430\u043b \u0445\u043e\u0441\u0442\u0435\u0440\u043e\u0432 \u0441 API \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f DNS-\u0437\u0430\u043f\u0438\u0441\u044f\u043c\u0438. \u041a\u0430\u043a\u043e\u0435 \u043c\u043e\u0451 \u0431\u044b\u043b\u043e \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u044b \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u043e\u0433\u043e \u0437\u0430 \u0434\u043e\u043c\u0435\u043d. \u0415\u0449\u0451 \u0438 \u0432\u0432\u043e\u0434\u044f\u0442 \u0432 \u0437\u0430\u0431\u043b\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0432\u0441\u044f\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438. \u041f\u0435\u0440\u0432\u044b\u0439 \u0433\u043e\u0434 \u0431\u0435\u0440\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u0430 \u043f\u043e\u0442\u043e\u043c \u0442\u044b\u0441\u044f\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 REG.ru \u0437\u0430 \u0434\u043e\u043c\u0435\u043d \u043d\u0430 ru \u0431\u0435\u0440\u0451\u0442 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0433\u043e\u0434 169 \u0440\u0443\u0431\u043b\u0435\u0439, \u0442\u0438\u043f\u043e \u043f\u043e \u0430\u043a\u0446\u0438\u0438 \u0438 \u0441\u043e \u0441\u043a\u0438\u0434\u043a\u043e\u0439 25%. \u0410 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0434\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u044f\u0447\u0435\u0441\u043a\u0438 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0434\u0435\u0431\u0440\u044f\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438, \u0447\u0442\u043e \u043e\u043d\u0438 \u0432\u043e\u0437\u044c\u043c\u0443\u0442 1424 \u0440\u0443\u0431\u043b\u044f \u0437\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0433\u043e\u0434\u044b.<\/p>\n<p>\u0425\u043e\u0441\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c \u0441 2014, \u043d\u0435 \u0432\u0432\u043e\u0434\u0438\u043b \u043c\u0435\u043d\u044f \u0432 \u0437\u0430\u0431\u043b\u0443\u0436\u0434\u0435\u043d\u0438\u0435. \u0418 \u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u043f\u043b\u0430\u0442\u0438\u043b \u0437\u0430 ru \u0437\u043e\u043d\u0443 99 \u0440\u0443\u0431\u043b\u0435\u0439, \u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u043c\u043d\u0435 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u0441\u044f 299 \u0440\u0443\u0431\u043b\u0435\u0439. \u041f\u0440\u0430\u0432\u0434\u0430 \u044f \u043d\u0430\u0445\u043e\u0436\u0443\u0441\u044c \u043d\u0430 \u0430\u0440\u0445\u0438\u0432\u043d\u043e\u043c \u0442\u0430\u0440\u0438\u0444\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0430\u0432\u043d\u043e \u0443\u0436\u0435 \u043d\u0435\u0442.<\/p>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u044b\u0431\u043e\u0440 \u043f\u0430\u043b \u043d\u0430 Beget. \u041f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u044f \u0443\u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0438\u0445 \u0432\u043f\u0441\u043a\u0430\u043c\u0438. \u041a\u0443\u043f\u0438\u043b \u0443 \u043d\u0438\u0445 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u0432 ru \u0437\u043e\u043d\u0435 \u0437\u0430 199 \u0440\u0443\u0431\u043b\u0435\u0439, \u043f\u0440\u043e\u0434\u043b\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0442\u043e\u0438\u0442\u044c 420. \u0417\u0430\u0440\u0435\u0433\u0430\u043b \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b \u0434\u043b\u044f \u0441\u0432\u043e\u0438\u0445 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041d\u0430 \u0434\u043e\u043c\u0435\u043d\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b A-\u0437\u0430\u043f\u0438\u0441\u044c, \u0443\u043a\u0430\u0437\u0430\u043b \u0441\u0432\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP. \u0412 \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u0430\u0445 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b CNAME, \u0432\u0435\u0434\u0443\u0449\u0438\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0434\u043e\u043c\u0435\u043d.<\/p>\n<p>\u0423 \u043c\u0435\u043d\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e \u0442\u0430\u0439\u043c\u0435\u0440\u0443, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP. \u0415\u0441\u043b\u0438 \u043e\u043d \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f, \u0442\u043e \u043c\u0435\u043d\u044f\u0442\u044c A-\u0437\u0430\u043f\u0438\u0441\u044c \u0443 \u0434\u043e\u043c\u0435\u043d\u0430, \u0447\u0435\u0440\u0435\u0437 API Beget. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0430\u043f\u0438\u0448\u043a\u0438, \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 DNS \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u0441\u044c \u043d\u0430\u0431\u043e\u0440 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043b\u044f FQDN. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0437\u0430\u043f\u0438\u0441\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0435\u0451.<\/p>\n<p>\u0412 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u0435\u0441\u0442\u044c \u043b\u043e\u0433\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0440\u043e\u0442\u0430\u0446\u0438\u044f \u043b\u043e\u0433\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u044d\u0442\u0438\u043c \u0436\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430: \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p><code>BEGET_LOGIN<\/code> \u2014 \u043b\u043e\u0433\u0438\u043d Beget API<\/p>\n<\/li>\n<li>\n<p><code>BEGET_PASSWORD<\/code> \u2014 \u043f\u0430\u0440\u043e\u043b\u044c Beget API<\/p>\n<\/li>\n<li>\n<p><code>BEGET_FQDN<\/code> \u2014 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0434\u043e\u043c\u0435\u043d, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>my-adress.ru<\/code> \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435:<\/p>\n<\/li>\n<li>\n<p><code>BEGET_A_PRIORITY<\/code> \u2014 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 A (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>10<\/code>)<\/p>\n<\/li>\n<li>\n<p><code>STATE_FILE<\/code> \u2014 \u0444\u0430\u0439\u043b, \u043a\u0443\u0434\u0430 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 IP (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>.state\/last_ip.txt<\/code> \u0440\u044f\u0434\u043e\u043c \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c)<\/p>\n<\/li>\n<li>\n<p><code>IP_URL<\/code> \u2014 URL, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>https:\/\/api.ipify.org<\/code>) \u041b\u043e\u0433\u0438:<\/p>\n<\/li>\n<li>\n<p><code>LOG_FILE<\/code> \u2014 \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443 \u043b\u043e\u0433\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>logs\/beget-ddns.log<\/code> \u0440\u044f\u0434\u043e\u043c \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c)<\/p>\n<\/li>\n<li>\n<p><code>LOG_MAX_BYTES<\/code> \u2014 \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u0430 \u043b\u043e\u0433\u0430 \u0434\u043e \u0440\u043e\u0442\u0430\u0446\u0438\u0438 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 1_000_000)<\/p>\n<\/li>\n<li>\n<p><code>LOG_BACKUP_COUNT<\/code> \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0445\u0440\u0430\u043d\u0438\u0442\u044c (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 5)<\/p>\n<\/li>\n<li>\n<p><code>LOG_LEVEL<\/code> \u2014 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (<code>INFO<\/code>, <code>DEBUG<\/code>, \u2026; \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>INFO<\/code>)<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">#!\/usr\/bin\/env python3from __future__ import annotationsimport jsonimport loggingimport logging.handlersimport osimport reimport sysimport timeimport urllib.parseimport urllib.requestfrom pathlib import Pathfrom typing import Any, Dict, List, TupleBEGET_API_BASE = \"https:\/\/api.beget.com\/api\/dns\"LOG = logging.getLogger(\"beget-ddns\")def _setup_logging() -&gt; None:    script_dir = Path(__file__).resolve().parent    log_file = Path(os.environ.get(\"LOG_FILE\", str(script_dir \/ \"logs\" \/ \"beget-ddns.log\"))).expanduser()    log_file.parent.mkdir(parents=True, exist_ok=True)    max_bytes = int(os.environ.get(\"LOG_MAX_BYTES\", str(1_000_000)))    backup_count = int(os.environ.get(\"LOG_BACKUP_COUNT\", str(5)))    level_name = os.environ.get(\"LOG_LEVEL\", \"INFO\").upper()    level = getattr(logging, level_name, logging.INFO)    LOG.setLevel(level)    handler = logging.handlers.RotatingFileHandler(        log_file,        maxBytes=max_bytes,        backupCount=backup_count,        encoding=\"utf-8\",    )    formatter = logging.Formatter(\"%(asctime)s %(levelname)s %(message)s\")    handler.setFormatter(formatter)    LOG.addHandler(handler)    # Also emit to stdout (useful under systemd\/journal).    stream = logging.StreamHandler(sys.stdout)    stream.setFormatter(formatter)    LOG.addHandler(stream)def _env_required(name: str) -&gt; str:    value = os.environ.get(name)    if not value:        raise RuntimeError(f\"Missing required env var: {name}\")    return valuedef _http_get_json(url: str, timeout_s: int = 20) -&gt; Any:    req = urllib.request.Request(url, headers={\"User-Agent\": \"jhon-mosk-beget-ddns\/1.0\"})    with urllib.request.urlopen(req, timeout=timeout_s) as resp:        body = resp.read()    return json.loads(body.decode(\"utf-8\"))def _http_get_text(url: str, timeout_s: int = 20) -&gt; str:    req = urllib.request.Request(url, headers={\"User-Agent\": \"jhon-mosk-beget-ddns\/1.0\"})    with urllib.request.urlopen(req, timeout=timeout_s) as resp:        body = resp.read()    return body.decode(\"utf-8\").strip()def _beget_call(method: str, login: str, password: str, input_data: Dict[str, Any]) -&gt; Any:    query = {        \"login\": login,        \"passwd\": password,        \"input_format\": \"json\",        \"output_format\": \"json\",        \"input_data\": json.dumps(input_data, ensure_ascii=False, separators=(\",\", \":\")),    }    url = f\"{BEGET_API_BASE}\/{method}?{urllib.parse.urlencode(query)}\"    payload = _http_get_json(url)    dump_path = os.environ.get(\"DEBUG_DUMP_JSON\")    if dump_path:        try:            Path(dump_path).expanduser().write_text(                json.dumps(payload, ensure_ascii=False, indent=2) + \"\\n\",                encoding=\"utf-8\",            )        except Exception as e:            LOG.warning(\"failed to write DEBUG_DUMP_JSON=%s: %s\", dump_path, e)    return payloaddef _unwrap_beget_payload(payload: Any) -&gt; Any:    \"\"\"    Beget API responses are not consistently documented.    We try to normalize the response by unwrapping common envelopes:    - {\"answer\": {...}} (with status, errors, and\/or result\/data)    - {\"answer\": {\"status\": \"error\", \"errors\": [...]}} -&gt; raise a helpful error    \"\"\"    if isinstance(payload, dict) and \"answer\" in payload:        answer = payload[\"answer\"]        if isinstance(answer, dict):            status = answer.get(\"status\")            if status == \"error\":                errors = answer.get(\"errors\") or []                # best-effort extract                err_texts: List[str] = []                if isinstance(errors, list):                    for e in errors:                        if isinstance(e, dict):                            t = e.get(\"error_text\") or e.get(\"text\") or e.get(\"message\")                            if isinstance(t, dict):                                t = t.get(\"text\") or t.get(\"type\") or str(t)                            if t:                                err_texts.append(str(t))                        elif isinstance(e, str):                            err_texts.append(e)                msg = \"; \".join(err_texts) or repr(errors) or \"unknown error\"                raise RuntimeError(f\"Beget API error: {msg}\")            # Success case: sometimes data is nested            for key in (\"result\", \"data\"):                if key in answer:                    return answer[key]        return answer    return payloaddef _extract_records_from_getdata(payload: Any) -&gt; Dict[str, List[Dict[str, Any]]]:    \"\"\"    Beget getData docs show an \"array\" response with a dict-like content. In practice    the API may return either:    - a list with one object\/dict inside, or    - a dict directly.    We normalize to the records dict.    \"\"\"    payload = _unwrap_beget_payload(payload)    if isinstance(payload, list) and payload:        obj = payload[0]    else:        obj = payload    if not isinstance(obj, dict):        raise RuntimeError(f\"Unexpected getData response type: {type(obj)}\")    # Some shapes may nest actual object under known keys.    for key in (\"result\", \"data\"):        if key in obj and isinstance(obj[key], list) and obj[key]:            candidate = obj[key][0]            if isinstance(candidate, dict):                obj = candidate                break    records = obj.get(\"records\")    if not isinstance(records, dict):        LOG.debug(\"getData raw object keys: %s\", sorted(obj.keys()))        raise RuntimeError(\"getData response has no 'records' dict\")    out: Dict[str, List[Dict[str, Any]]] = {}    for key in (\"A\", \"MX\", \"TXT\"):        items = records.get(key, [])        if items is None:            items = []        if not isinstance(items, list):            raise RuntimeError(f\"getData records.{key} is not a list\")        out[key] = items    return outdef _normalize_mx_txt_from_getdata(records: Dict[str, List[Dict[str, Any]]]) -&gt; Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:    \"\"\"    getData returns different shapes than changeRecords expects.    - changeRecords wants: [{\"priority\": &lt;int&gt;, \"value\": &lt;str&gt;}]    - getData may return: [{\"priority\": \"10\", \"value\": \"mx1...\"}] (as in docs)      or other internal keys (observed by community integrations).    We support both by mapping known variants.    \"\"\"    def pick_priority(item: Dict[str, Any]) -&gt; int:        for k in (\"priority\", \"preference\"):            if k in item:                return int(item[k])        # fall back        return 10    def pick_value(item: Dict[str, Any], candidates: Tuple[str, ...]) -&gt; str:        for k in candidates:            if k in item and item[k] is not None:                return str(item[k])        return \"\"    mx_out: List[Dict[str, Any]] = []    for mx in records.get(\"MX\", []):        if not isinstance(mx, dict):            continue        value = pick_value(mx, (\"value\", \"exchange\", \"host\"))        if value:            mx_out.append({\"priority\": pick_priority(mx), \"value\": value})    txt_out: List[Dict[str, Any]] = []    for txt in records.get(\"TXT\", []):        if not isinstance(txt, dict):            continue        value = pick_value(txt, (\"value\", \"txtdata\", \"text\"))        if value is None:            value = \"\"        txt_out.append({\"priority\": pick_priority(txt), \"value\": value})    return mx_out, txt_outdef _parse_ip(s: str) -&gt; str:    s = s.strip()    if re.fullmatch(r\"\\d{1,3}(\\.\\d{1,3}){3}\", s):        return s    raise RuntimeError(f\"IP_URL did not return an IPv4 address. Got: {s!r}\")def main() -&gt; int:    _setup_logging()    login = _env_required(\"BEGET_LOGIN\")    password = _env_required(\"BEGET_PASSWORD\")    fqdn = _env_required(\"BEGET_FQDN\")    ip_url = os.environ.get(\"IP_URL\", \"https:\/\/api.ipify.org\")    a_priority = int(os.environ.get(\"BEGET_A_PRIORITY\", \"10\"))    script_dir = Path(__file__).resolve().parent    default_state_file = script_dir \/ \".state\" \/ \"last_ip.txt\"    state_file = Path(os.environ.get(\"STATE_FILE\", str(default_state_file))).expanduser()    state_file.parent.mkdir(parents=True, exist_ok=True)    now = int(time.time())    current_ip = _parse_ip(_http_get_text(ip_url))    last_ip = None    if state_file.exists():        last_ip = state_file.read_text(encoding=\"utf-8\").strip() or None    if last_ip == current_ip:        LOG.info(\"ip unchanged: %s\", current_ip)        return 0    # Read existing records to preserve MX\/TXT.    LOG.info(\"ip changed: %s -&gt; %s; updating %s\", last_ip, current_ip, fqdn)    getdata = _beget_call(\"getData\", login, password, {\"fqdn\": fqdn})    records = _extract_records_from_getdata(getdata)    mx_out, txt_out = _normalize_mx_txt_from_getdata(records)    change_payload = {        \"fqdn\": fqdn,        \"records\": {            \"A\": [{\"priority\": a_priority, \"value\": current_ip}],            \"MX\": mx_out,            \"TXT\": txt_out,        },    }    result = _beget_call(\"changeRecords\", login, password, change_payload)    # Beget docs show `true` on success. Some APIs wrap into objects.    ok = False    if result is True:        ok = True    elif isinstance(result, dict):        # best-effort compatibility        if result.get(\"answer\") is True or result.get(\"result\") is True:            ok = True        if result.get(\"answer\", {}).get(\"status\") == \"success\":            ok = True    if not ok:        LOG.error(\"changeRecords unexpected response: %r\", result)        raise RuntimeError(f\"Beget changeRecords failed or returned unexpected response: {result!r}\")    state_file.write_text(current_ip + \"\\n\", encoding=\"utf-8\")    LOG.info(\"updated %s A -&gt; %s (was %s)\", fqdn, current_ip, last_ip)    return 0if __name__ == \"__main__\":    try:        raise SystemExit(main())    except Exception as e:        _setup_logging()        LOG.exception(\"fatal error: %s\", e)        raise SystemExit(1)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043b systemd \u044e\u043d\u0438\u0442 \u0438 \u0442\u0430\u0439\u043c\u0435\u0440:<\/p>\n<p><code>\/etc\/systemd\/system\/beget-ddns.service<\/code>:<\/p>\n<p>\u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0433\u0440\u0443\u043f\u043f\u0443, \u043f\u0443\u0442\u0438 \u0434\u043e \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438, \u0444\u0430\u0439\u043b\u0430 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0438 \u0434\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430<\/p>\n<pre><code>[Unit]Description=Beget DDNS updater (my-adress.ru A record)After=network-online.targetWants=network-online.target  [Service]Type=oneshotUser=your-userGroup=your-user-groupWorkingDirectory=\/script\/working\/directoryEnvironmentFile=\/path\/to\/environment\/file.envExecStart=\/usr\/bin\/python3 \/path\/to\/script.py<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><code>\/etc\/systemd\/system\/beget-ddns.timer<\/code>:<\/p>\n<pre><code>[Unit]Description=Run Beget DDNS updater every 5 minutes  [Timer]OnBootSec=2minOnUnitActiveSec=5minRandomizedDelaySec=30sPersistent=true  [Install]WantedBy=timers.target<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430:<\/p>\n<pre><code class=\"bash\">sudo systemctl daemon-reloadsudo systemctl enable --now beget-ddns.timersudo systemctl start beget-ddns.service<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u044b \u0447\u0442\u043e \u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043b\u043e\u0433\u0438:<\/p>\n<pre><code class=\"bash\">sudo systemctl status beget-ddns.timersudo systemctl status beget-ddns.servicesudo systemctl list-timers --all | grep beget-ddnssudo journalctl -u beget-ddns.service -n 200 --no-pagertail -n 200 \/path\/to\/log\/file\/beget-ddns.log<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u0430 \u0441\u0435\u0440\u0432\u0430\u0447\u043a\u0435 \u0443 \u043c\u0435\u043d\u044f Caddy \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u043a\u0441\u0438 \u0438 \u043e\u043d \u0441\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u044b. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0434\u043e\u0432\u043e\u043b\u0435\u043d. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u043c \u0447\u0435\u0440\u0435\u0437 TP-Link.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1044016\/\">https:\/\/habr.com\/ru\/articles\/1044016\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0434\u043e\u043c\u0430\u0448\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0430\u0447\u043e\u043a, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0438\u0441\u044c \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b . \u0422\u0430\u043a \u043a\u0430\u043a \u0437\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 IP \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0443 \u044f \u043f\u043b\u0430\u0442\u0438\u0442\u044c \u043d\u0435 \u0445\u043e\u0447\u0443, \u0442\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b DDNS \u043e\u0442 TP-Link. \u0418 \u0430\u0434\u0440\u0435\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b https:\/\/my-adress.tplinkdns.com. TP-Link \u0434\u0430\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d \u0438 \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b 2-\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0443\u043c\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ddclient \u0438 Cloudfare. \u0412\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c \u0447\u0442\u043e Cloudfare \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 ru \u0437\u043e\u043d\u043e\u0439. \u0412\u043e\u0442 \u0440\u0430\u0441\u0438\u0441\u0442\u044b!\u041f\u043e\u0438\u0441\u043a\u0430\u043b \u0445\u043e\u0441\u0442\u0435\u0440\u043e\u0432 \u0441 API \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f DNS-\u0437\u0430\u043f\u0438\u0441\u044f\u043c\u0438. \u041a\u0430\u043a\u043e\u0435 \u043c\u043e\u0451 \u0431\u044b\u043b\u043e \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u044b \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u043e\u0433\u043e \u0437\u0430 \u0434\u043e\u043c\u0435\u043d. \u0415\u0449\u0451 \u0438 \u0432\u0432\u043e\u0434\u044f\u0442 \u0432 \u0437\u0430\u0431\u043b\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0432\u0441\u044f\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438. \u041f\u0435\u0440\u0432\u044b\u0439 \u0433\u043e\u0434 \u0431\u0435\u0440\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u0430 \u043f\u043e\u0442\u043e\u043c \u0442\u044b\u0441\u044f\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 REG.ru \u0437\u0430 \u0434\u043e\u043c\u0435\u043d \u043d\u0430 ru \u0431\u0435\u0440\u0451\u0442 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0433\u043e\u0434 169 \u0440\u0443\u0431\u043b\u0435\u0439, \u0442\u0438\u043f\u043e \u043f\u043e \u0430\u043a\u0446\u0438\u0438 \u0438 \u0441\u043e \u0441\u043a\u0438\u0434\u043a\u043e\u0439 25%. \u0410 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0434\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u044f\u0447\u0435\u0441\u043a\u0438 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0434\u0435\u0431\u0440\u044f\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438, \u0447\u0442\u043e \u043e\u043d\u0438 \u0432\u043e\u0437\u044c\u043c\u0443\u0442 1424 \u0440\u0443\u0431\u043b\u044f \u0437\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0433\u043e\u0434\u044b.\u0425\u043e\u0441\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c \u0441 2014, \u043d\u0435 \u0432\u0432\u043e\u0434\u0438\u043b \u043c\u0435\u043d\u044f \u0432 \u0437\u0430\u0431\u043b\u0443\u0436\u0434\u0435\u043d\u0438\u0435. \u0418 \u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u043f\u043b\u0430\u0442\u0438\u043b \u0437\u0430 ru \u0437\u043e\u043d\u0443 99 \u0440\u0443\u0431\u043b\u0435\u0439, \u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u043c\u043d\u0435 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u0441\u044f 299 \u0440\u0443\u0431\u043b\u0435\u0439. \u041f\u0440\u0430\u0432\u0434\u0430 \u044f \u043d\u0430\u0445\u043e\u0436\u0443\u0441\u044c \u043d\u0430 \u0430\u0440\u0445\u0438\u0432\u043d\u043e\u043c \u0442\u0430\u0440\u0438\u0444\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0430\u0432\u043d\u043e \u0443\u0436\u0435 \u043d\u0435\u0442.\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u044b\u0431\u043e\u0440 \u043f\u0430\u043b \u043d\u0430 Beget. \u041f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u044f \u0443\u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0438\u0445 \u0432\u043f\u0441\u043a\u0430\u043c\u0438. \u041a\u0443\u043f\u0438\u043b \u0443 \u043d\u0438\u0445 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u0432 ru \u0437\u043e\u043d\u0435 \u0437\u0430 199 \u0440\u0443\u0431\u043b\u0435\u0439, \u043f\u0440\u043e\u0434\u043b\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0442\u043e\u0438\u0442\u044c 420. \u0417\u0430\u0440\u0435\u0433\u0430\u043b \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u044b \u0434\u043b\u044f \u0441\u0432\u043e\u0438\u0445 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u041d\u0430 \u0434\u043e\u043c\u0435\u043d\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b A-\u0437\u0430\u043f\u0438\u0441\u044c, \u0443\u043a\u0430\u0437\u0430\u043b \u0441\u0432\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP. \u0412 \u043f\u043e\u0434\u0434\u043e\u043c\u0435\u043d\u0430\u0445 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043b CNAME, \u0432\u0435\u0434\u0443\u0449\u0438\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0434\u043e\u043c\u0435\u043d.\u0423 \u043c\u0435\u043d\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e \u0442\u0430\u0439\u043c\u0435\u0440\u0443, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP. \u0415\u0441\u043b\u0438 \u043e\u043d \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f, \u0442\u043e \u043c\u0435\u043d\u044f\u0442\u044c A-\u0437\u0430\u043f\u0438\u0441\u044c \u0443 \u0434\u043e\u043c\u0435\u043d\u0430, \u0447\u0435\u0440\u0435\u0437 API Beget. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0430\u043f\u0438\u0448\u043a\u0438, \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 DNS \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u0441\u044c \u043d\u0430\u0431\u043e\u0440 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043b\u044f FQDN. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0437\u0430\u043f\u0438\u0441\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0435\u0451.\u0412 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u0435\u0441\u0442\u044c \u043b\u043e\u0433\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0440\u043e\u0442\u0430\u0446\u0438\u044f \u043b\u043e\u0433\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u044d\u0442\u0438\u043c \u0436\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c.\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430: \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435:BEGET_LOGIN \u2014 \u043b\u043e\u0433\u0438\u043d Beget APIBEGET_PASSWORD \u2014 \u043f\u0430\u0440\u043e\u043b\u044c Beget APIBEGET_FQDN \u2014 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0434\u043e\u043c\u0435\u043d, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 my-adress.ru \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435:BEGET_A_PRIORITY \u2014 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 A (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 10)STATE_FILE \u2014 \u0444\u0430\u0439\u043b, \u043a\u0443\u0434\u0430 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 IP (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e .state\/last_ip.txt \u0440\u044f\u0434\u043e\u043c \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c)IP_URL \u2014 URL, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0439 IP \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e https:\/\/api.ipify.org) \u041b\u043e\u0433\u0438:LOG_FILE \u2014 \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443 \u043b\u043e\u0433\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e logs\/beget-ddns.log \u0440\u044f\u0434\u043e\u043c \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c)LOG_MAX_BYTES \u2014 \u0440\u0430\u0437\u043c\u0435\u0440 \u0444\u0430\u0439\u043b\u0430 \u043b\u043e\u0433\u0430 \u0434\u043e \u0440\u043e\u0442\u0430\u0446\u0438\u0438 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 1_000_000)LOG_BACKUP_COUNT \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0445\u0440\u0430\u043d\u0438\u0442\u044c (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 5)LOG_LEVEL \u2014 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (INFO, DEBUG, \u2026; \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e INFO)#!\/usr\/bin\/env python3from __future__ import annotationsimport jsonimport loggingimport logging.handlersimport osimport reimport sysimport timeimport urllib.parseimport urllib.requestfrom pathlib import Pathfrom typing import Any, Dict, List, TupleBEGET_API_BASE = &#171;https:\/\/api.beget.com\/api\/dns&#187;LOG = logging.getLogger(&#171;beget-ddns&#187;)def _setup_logging() -&gt; None:    script_dir = Path(__file__).resolve().parent    log_file = Path(os.environ.get(&#171;LOG_FILE&#187;, str(script_dir \/ &#171;logs&#187; \/ &#171;beget-ddns.log&#187;))).expanduser()    log_file.parent.mkdir(parents=True, exist_ok=True)    max_bytes = int(os.environ.get(&#171;LOG_MAX_BYTES&#187;, str(1_000_000)))    backup_count = int(os.environ.get(&#171;LOG_BACKUP_COUNT&#187;, str(5)))    level_name = os.environ.get(&#171;LOG_LEVEL&#187;, &#171;INFO&#187;).upper()    level = getattr(logging, level_name, logging.INFO)    LOG.setLevel(level)    handler = logging.handlers.RotatingFileHandler(        log_file,        maxBytes=max_bytes,        backupCount=backup_count,        encoding=&#187;utf-8&#8243;,    )    formatter = logging.Formatter(&#171;%(asctime)s %(levelname)s %(message)s&#187;)    handler.setFormatter(formatter)    LOG.addHandler(handler)    # Also emit to stdout (useful under systemd\/journal).    stream = logging.StreamHandler(sys.stdout)    stream.setFormatter(formatter)    LOG.addHandler(stream)def _env_required(name: str) -&gt; str:    value = os.environ.get(name)    if not value:        raise RuntimeError(f&#187;Missing required env var: {name}&#187;)    return valuedef _http_get_json(url: str, timeout_s: int = 20) -&gt; Any:    req = urllib.request.Request(url, headers={&#171;User-Agent&#187;: &#171;jhon-mosk-beget-ddns\/1.0&#187;})    with urllib.request.urlopen(req, timeout=timeout_s) as resp:        body = resp.read()    return json.loads(body.decode(&#171;utf-8&#187;))def _http_get_text(url: str, timeout_s: int = 20) -&gt; str:    req = urllib.request.Request(url, headers={&#171;User-Agent&#187;: &#171;jhon-mosk-beget-ddns\/1.0&#187;})    with urllib.request.urlopen(req, timeout=timeout_s) as resp:        body = resp.read()    return body.decode(&#171;utf-8&#187;).strip()def _beget_call(method: str, login: str, password: str, input_data: Dict[str, Any]) -&gt; Any:    query = {        &#171;login&#187;: login,        &#171;passwd&#187;: password,        &#171;input_format&#187;: &#171;json&#187;,        &#171;output_format&#187;: &#171;json&#187;,        &#171;input_data&#187;: json.dumps(input_data, ensure_ascii=False, separators=(&#171;,&#187;, &#171;:&#187;)),    }    url = f&#187;{BEGET_API_BASE}\/{method}?{urllib.parse.urlencode(query)}&#187;    payload = _http_get_json(url)    dump_path = os.environ.get(&#171;DEBUG_DUMP_JSON&#187;)    if dump_path:        try:            Path(dump_path).expanduser().write_text(                json.dumps(payload, ensure_ascii=False, indent=2) + &#171;\\n&#187;,                encoding=&#187;utf-8&#8243;,            )        except Exception as e:            LOG.warning(&#171;failed to write DEBUG_DUMP_JSON=%s: %s&#187;, dump_path, e)    return payloaddef _unwrap_beget_payload(payload: Any) -&gt; Any:    &#171;&#187;&#187;    Beget API responses are not consistently documented.    We try to normalize the response by unwrapping common envelopes:    &#8212; {&#171;answer&#187;: {&#8230;}} (with status, errors, and\/or result\/data)    &#8212; {&#171;answer&#187;: {&#171;status&#187;: &#171;error&#187;, &#171;errors&#187;: [&#8230;]}} -&gt; raise a helpful error    &#171;&#187;&#187;    if isinstance(payload, dict) and &#171;answer&#187; in payload:        answer = payload[&#171;answer&#187;]        if isinstance(answer, dict):            status = answer.get(&#171;status&#187;)            if status == &#171;error&#187;:                errors = answer.get(&#171;errors&#187;) or []                # best-effort extract                err_texts: List[str] = []                if isinstance(errors, list):                    for e in errors:                        if isinstance(e, dict):                            t = e.get(&#171;error_text&#187;) or e.get(&#171;text&#187;) or e.get(&#171;message&#187;)                            if isinstance(t, dict):                                t = t.get(&#171;text&#187;) or t.get(&#171;type&#187;) or str(t)                            if t:                                err_texts.append(str(t))                        elif isinstance(e, str):                            err_texts.append(e)                msg = &#171;; &#171;.join(err_texts) or repr(errors) or &#171;unknown error&#187;                raise RuntimeError(f&#187;Beget API error: {msg}&#187;)            # Success case: sometimes data is nested            for key in (&#171;result&#187;, &#171;data&#187;):                if key in answer:                    return answer[key]        return answer    return payloaddef _extract_records_from_getdata(payload: Any) -&gt; Dict[str, List[Dict[str, Any]]]:    &#171;&#187;&#187;    Beget getData docs show an &#171;array&#187; response with a dict-like content. In practice    the API may return either:    &#8212; a list with one object\/dict inside, or    &#8212; a dict directly.    We normalize to the records dict.    &#171;&#187;&#187;    payload = _unwrap_beget_payload(payload)    if isinstance(payload, list) and payload:        obj = payload[0]    else:        obj = payload    if not isinstance(obj, dict):        raise RuntimeError(f&#187;Unexpected getData response type: {type(obj)}&#187;)    # Some shapes may nest actual object under known keys.    for key in (&#171;result&#187;, &#171;data&#187;):        if key in obj and isinstance(obj[key], list) and obj[key]:            candidate = obj[key][0]            if isinstance(candidate, dict):                obj = candidate                break    records = obj.get(&#171;records&#187;)    if not isinstance(records, dict):        LOG.debug(&#171;getData raw object keys: %s&#187;, sorted(obj.keys()))        raise RuntimeError(&#171;getData response has no &#8216;records&#8217; dict&#187;)    out: Dict[str, List[Dict[str, Any]]] = {}    for key in (&#171;A&#187;, &#171;MX&#187;, &#171;TXT&#187;):        items = records.get(key, [])        if items is None:            items = []        if not isinstance(items, list):            raise RuntimeError(f&#187;getData records.{key} is not a list&#187;)        out[key] = items    return outdef _normalize_mx_txt_from_getdata(records: Dict[str, List[Dict[str, Any]]]) -&gt; Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:    &#171;&#187;&#187;    getData returns different shapes than changeRecords expects.    &#8212; changeRecords wants: [{&#171;priority&#187;: &lt;int&gt;, &#171;value&#187;: &lt;str&gt;}]    &#8212; getData may return: [{&#171;priority&#187;: &#171;10&#187;, &#171;value&#187;: &#171;mx1&#8230;&#187;}] (as in docs)      or other internal keys (observed by community integrations).    We support both by mapping known variants.    &#171;&#187;&#187;    def pick_priority(item: Dict[str, Any]) -&gt; int:        for k in (&#171;priority&#187;, &#171;preference&#187;):            if k in item:                return int(item[k])        # fall back        return 10    def pick_value(item: Dict[str, Any], candidates: Tuple[str, &#8230;]) -&gt; str:        for k in candidates:            if k in item and item[k] is not None:                return str(item[k])        return &#171;&#187;    mx_out: List[Dict[str, Any]] = []    for mx in records.get(&#171;MX&#187;, []):        if not isinstance(mx, dict):            continue        value = pick_value(mx, (&#171;value&#187;, &#171;exchange&#187;, &#171;host&#187;))        if value:            mx_out.append({&#171;priority&#187;: pick_priority(mx), &#171;value&#187;: value})    txt_out: List[Dict[str, Any]] = []    for txt in records.get(&#171;TXT&#187;, []):        if not isinstance(txt, dict):            continue        value = pick_value(txt, (&#171;value&#187;, &#171;txtdata&#187;, &#171;text&#187;))        if value is None:            value = &#171;&#187;        txt_out.append({&#171;priority&#187;: pick_priority(txt), &#171;value&#187;: value})    return mx_out, txt_outdef _parse_ip(s: str) -&gt; str:    s = s.strip()    if re.fullmatch(r&#187;\\d{1,3}(\\.\\d{1,3}){3}&#187;, s):        return s    raise RuntimeError(f&#187;IP_URL did not return an IPv4 address. Got: {s!r}&#187;)def main() -&gt; int:    _setup_logging()    login = _env_required(&#171;BEGET_LOGIN&#187;)    &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-482513","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/482513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482513"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/482513\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}