small fixes

This commit is contained in:
2026-04-14 17:53:35 +02:00
parent 9930123606
commit 4bd28552bc
2 changed files with 45 additions and 38 deletions
@@ -1,15 +1,4 @@
#!/usr/bin/env python3
"""Apple mobileconfig profile generator HTTP server.
Example:
./mobileconfig-generator.py \\
--domain billenius.com \\
--mail-host mail.billenius.com \\
--radicale-host cal.billenius.com \\
--account love:love@billenius.com:love_billenius.com \\
--port 8426
"""
import argparse
import hashlib
import re
@@ -98,7 +87,9 @@ def resolve_account(params):
def deterministic_uuid(seed):
digest = hashlib.sha256(seed.encode("utf-8")).hexdigest()
return f"{digest[:8]}-{digest[8:12]}-{digest[12:16]}-{digest[16:20]}-{digest[20:32]}"
return (
f"{digest[:8]}-{digest[8:12]}-{digest[12:16]}-{digest[16:20]}-{digest[20:32]}"
)
def mobileconfig_payload(email, full_name):
@@ -219,7 +210,7 @@ def mobileconfig_payload(email, full_name):
<key>PayloadDescription</key>
<string>Configures mail, calendar, and contacts for {values["email"]}.</string>
<key>PayloadDisplayName</key>
<string>{values["domain"]} Mail</string>
<string>{values["email"]}</string>
<key>PayloadIdentifier</key>
<string>com.billenius.mobileconfig.{values["identifier"]}</string>
<key>PayloadOrganization</key>
@@ -388,7 +379,10 @@ class Handler(BaseHTTPRequestHandler):
payload = mobileconfig_payload(account["email"], full_name).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/x-apple-aspen-config")
self.send_header("Content-Disposition", f'attachment; filename="{filename_base}.mobileconfig"')
self.send_header(
"Content-Disposition",
f'attachment; filename="{filename_base}.mobileconfig"',
)
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
@@ -409,7 +403,10 @@ class Handler(BaseHTTPRequestHandler):
if parsed.path in ("/mobileconfig/", "/mobileconfig/index.html"):
account = resolve_account(params)
if account is None:
has_query = any(first(params.get(key)) for key in ("username", "user", "emailaddress"))
has_query = any(
first(params.get(key))
for key in ("username", "user", "emailaddress")
)
if has_query:
self.send_text("Unknown account", status=404)
else:
@@ -432,8 +429,10 @@ class Handler(BaseHTTPRequestHandler):
self.send_profile(account, full_name, "billenius")
return
if parsed.path.startswith("/mobileconfig/") and parsed.path.endswith(".mobileconfig"):
basename = parsed.path[len("/mobileconfig/"):-len(".mobileconfig")]
if parsed.path.startswith("/mobileconfig/") and parsed.path.endswith(
".mobileconfig"
):
basename = parsed.path[len("/mobileconfig/") : -len(".mobileconfig")]
account = ACCOUNT_BY_PATH.get(basename)
if account is None:
self.send_text("Unknown account", status=404)
@@ -455,7 +454,11 @@ if __name__ == "__main__":
parser.add_argument("--radicale-host", required=True)
parser.add_argument("--default-username", default=None)
parser.add_argument("--port", type=int, default=8426)
parser.add_argument("--account", type=parse_account, action="append",
help="username:email:legacyPath (repeatable)")
parser.add_argument(
"--account",
type=parse_account,
action="append",
help="username:email:legacyPath (repeatable)",
)
init(parser.parse_args())
ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()