add ip change notification and refine sender display
This commit is contained in:
@@ -10,7 +10,10 @@ import httpx
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import Settings
|
||||
from app.models.public_ip import PublicIPHistory, PublicIPState
|
||||
from app.services.config_page import build_runtime_settings
|
||||
from app.services.email import EmailConfigurationError, EmailDeliveryError, send_public_ip_changed_email
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -31,6 +34,8 @@ class PublicIPCheckResult:
|
||||
status: PublicIPResultStatus
|
||||
checked_at: datetime
|
||||
changed: bool
|
||||
previous_ipv4: str | None = None
|
||||
current_ipv4: str | None = None
|
||||
|
||||
|
||||
def check_public_ipv4(
|
||||
@@ -77,7 +82,12 @@ def check_public_ipv4(
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return PublicIPCheckResult(status="first_seen", checked_at=checked_at, changed=False)
|
||||
return PublicIPCheckResult(
|
||||
status="first_seen",
|
||||
checked_at=checked_at,
|
||||
changed=False,
|
||||
current_ipv4=current_ipv4,
|
||||
)
|
||||
|
||||
if state.current_ipv4 == current_ipv4:
|
||||
state.last_checked_at = checked_at
|
||||
@@ -85,9 +95,15 @@ def check_public_ipv4(
|
||||
state.last_check_error = None
|
||||
state.last_provider = provider_name
|
||||
session.commit()
|
||||
return PublicIPCheckResult(status="unchanged", checked_at=checked_at, changed=False)
|
||||
return PublicIPCheckResult(
|
||||
status="unchanged",
|
||||
checked_at=checked_at,
|
||||
changed=False,
|
||||
current_ipv4=current_ipv4,
|
||||
)
|
||||
|
||||
state.previous_ipv4 = state.current_ipv4
|
||||
previous_ipv4 = state.current_ipv4
|
||||
state.previous_ipv4 = previous_ipv4
|
||||
state.current_ipv4 = current_ipv4
|
||||
state.last_checked_at = checked_at
|
||||
state.last_changed_at = checked_at
|
||||
@@ -103,7 +119,43 @@ def check_public_ipv4(
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return PublicIPCheckResult(status="changed", checked_at=checked_at, changed=True)
|
||||
return PublicIPCheckResult(
|
||||
status="changed",
|
||||
checked_at=checked_at,
|
||||
changed=True,
|
||||
previous_ipv4=previous_ipv4,
|
||||
current_ipv4=current_ipv4,
|
||||
)
|
||||
|
||||
|
||||
def check_public_ipv4_and_notify(
|
||||
session: Session,
|
||||
*,
|
||||
bootstrap_settings: Settings,
|
||||
fetch_public_ipv4: PublicIPv4Fetcher | None = None,
|
||||
provider_name: str = PUBLIC_IP_PROVIDER_NAME,
|
||||
) -> PublicIPCheckResult:
|
||||
result = check_public_ipv4(
|
||||
session,
|
||||
fetch_public_ipv4=fetch_public_ipv4,
|
||||
provider_name=provider_name,
|
||||
)
|
||||
|
||||
if result.status != "changed" or result.previous_ipv4 is None or result.current_ipv4 is None:
|
||||
return result
|
||||
|
||||
runtime_settings = build_runtime_settings(session, bootstrap_settings)
|
||||
try:
|
||||
send_public_ip_changed_email(
|
||||
runtime_settings,
|
||||
previous_ipv4=result.previous_ipv4,
|
||||
current_ipv4=result.current_ipv4,
|
||||
detected_at=result.checked_at,
|
||||
)
|
||||
except (EmailConfigurationError, EmailDeliveryError) as exc:
|
||||
logger.warning("Public IPv4 change notification failed: %s", exc)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def fetch_public_ipv4_from_provider() -> str:
|
||||
|
||||
Reference in New Issue
Block a user