add logic for dynamic IP lookup for deltachat links and install #31

This commit is contained in:
n8fr8 2026-07-01 13:31:24 -04:00
parent d4b4b5a9e0
commit 8bb7792ee3
2 changed files with 64 additions and 2 deletions

View file

@ -1,8 +1,36 @@
import pexpect
import json
import socket
from app import app
from subprocess import run
def resolve_butterbox_ip(hostname):
"""Best-effort lookup of the box's current address, falling back to the
configured default. Mirrors app.routes.resolve_butterbox_ip but avoids a
circular import (routes imports this module) and reuses the hostname that
was already loaded from settings.txt."""
# 1. Resolve the box's own mDNS/.local name.
try:
ip = socket.gethostbyname(f"{hostname}.local")
if ip and not ip.startswith("127."):
return ip
except OSError:
pass
# 2. Fall back to the address of the primary outbound interface. The
# connect() picks a route without sending any packets.
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
if ip and not ip.startswith("127."):
return ip
except OSError:
pass
# 3. Give up and use the configured access-point default.
return app.config['BUTTERBOX_DEFAULT_IP']
def run_madmail_installer():
with open("./settings.txt", "r") as f:
settings = json.load(f)
@ -18,7 +46,7 @@ def run_madmail_installer():
child.expect("MX record")
child.sendline(f"{butterbox_hostname}.local")
child.expect("Public IP address")
child.sendline(app.config['BUTTERBOX_DEFAULT_IP'])
child.sendline(resolve_butterbox_ip(butterbox_hostname))
child.expect("Additional domains")
child.sendline("")
child.expect("State directory")