Files
ddns-service/src/ddns_service/__init__.py

85 lines
2.0 KiB
Python

"""
DDNS Service - Dynamic DNS update service.
A daemon that accepts HTTP(S) requests to dynamically update DNS entries.
Includes CLI administration tools for user and hostname management.
"""
import datetime
__version__ = "1.0.0"
__author__ = "Thomas Oettli <spacefreak@noop.ch>"
# DynDNS-compatible response statuses
STATUS_GOOD = "good"
STATUS_NOCHG = "nochg"
STATUS_BADAUTH = "badauth"
STATUS_NOHOST = "nohost"
STATUS_DNSERR = "dnserr"
STATUS_ABUSE = "abuse"
STATUS_BADIP = "badip"
__all__ = [
"app",
"cleanup",
"cli",
"config",
"datetime_str",
"dns",
"email",
"logging",
"main",
"models",
"ratelimit",
"server",
"STATUS_GOOD",
"STATUS_NOCHG",
"STATUS_BADAUTH",
"STATUS_NOHOST",
"STATUS_DNSERR",
"STATUS_ABUSE",
"STATUS_BADIP",
]
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S %Z"
# Datetime convention:
# All datetime objects in this codebase are naive UTC to match database storage.
# - utc_now(): returns naive UTC datetime
# - datetime_str(): converts naive UTC to display string (adds tzinfo for formatting)
# - Database stores/returns naive datetimes (always UTC by convention)
def datetime_str(dt, utc=False):
"""
Convert datetime to display string.
Assumes naive datetimes are UTC per codebase convention.
Args:
dt: Datetime object (naive UTC or timezone-aware).
utc: If True, display in UTC; otherwise convert to local timezone.
Returns:
Formatted datetime string, or "Never" if dt is not a datetime.
"""
if not isinstance(dt, datetime.datetime):
return "Never"
aware_dt = dt.replace(tzinfo=datetime.UTC) if not dt.tzinfo else dt
if utc:
return aware_dt.strftime(DATETIME_FORMAT)
else:
return aware_dt.astimezone().strftime(DATETIME_FORMAT)
def utc_now():
"""
Get current time as naive UTC datetime.
Returns naive datetime to match database storage behavior.
All naive datetimes in this codebase are assumed to be UTC.
"""
return datetime.datetime.now(datetime.UTC).replace(tzinfo=None)