Files
SideRelay/Interface/CompagnonService.py
2026-11-13 19:17:49 +01:00

458 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Serve device-scoped status and refresh requests over Tailscale Serve."""
from contextlib import closing
from datetime import datetime
from functools import lru_cache
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import http.client
import base64
import binascii
import hashlib
import json
import os
from pathlib import Path
import plistlib
import re
import secrets
import signal
import sqlite3
import subprocess
import sys
import tempfile
import threading
import time
from urllib.parse import quote, urlencode
from uuid import NAMESPACE_URL, uuid5
from zipfile import BadZipFile
from .ArchiveIPA import devicectl_json
from .CatalogueIPA import private_work
from .CompagnonIPA import BUNDLE_ID, PORT, enabled, endpoint, tailnet_ip
from .CompagnonMisesAJour import AutoUpdates, engine_request
from .ImportLiveContainer import TAILSCALE, tailscale
from .InspectionIPA import archive_icon_preview
def authorized_device(db, request, owner, aliases):
if request.headers.get("Tailscale-User-Login") != owner:
return None
address = tailnet_ip(request.headers.get("X-Forwarded-For", ""))
device_id = request.headers.get("X-SideRelay-Device", "")
if address is None or not re.fullmatch(r"[a-fA-F\d-]{36}", device_id):
return None
device = db.execute("SELECT ip FROM devices WHERE id = ?", (device_id,)).fetchone()
registered = tailnet_ip(device[0]) if device else None
return device_id if registered and (address == registered or address in aliases.get(registered, ())) else None
def status(db, root, device_id):
rows = db.execute("""
SELECT a.id, a.display_name, a.bundle_id, a.version,
COALESCE(a.profile_expires_at, max(COALESCE(a.last_refreshed, a.installed_at), a.installed_at) + 604800),
a.account_id, a.ipa_path, j.status, j.error, a.signing_repair,
coalesce((SELECT max(i.installed_at) FROM ipa_installations i WHERE i.app_id = a.id), a.installed_at)
FROM apps a LEFT JOIN jobs j ON j.rowid = (SELECT last.rowid FROM jobs last WHERE last.app_id = a.id ORDER BY last.rowid DESC LIMIT 1)
WHERE a.device_id = ? AND a.installed_at IS NOT NULL
ORDER BY lower(a.display_name)
""", (device_id,))
apps = []
for key, name, bundle, version, expiry, account, ipa, job, error, repair, installed in rows:
busy = job in ("queued", "running", "pending")
if busy:
state = "Refresh or installation in progress."
elif repair:
state = "Re-signing required."
elif job == "failed":
state = ("The iPhone’s Apple pairing service is unreachable. Try USB or a reachable Wi-Fi connection."
if (error or "").startswith("[pairing-unreachable]") else "Last operation failed. See Activity on the Mac.")
else:
state = None
apps.append({"id": key, "name": name, "bundleID": bundle, "version": version, "expiresAt": expiry, "busy": busy,
"canRefresh": bool(account and ipa and (root / ipa).is_file()), "state": state,
"iconRevision": installed})
return {"apps": apps}
def queue_refresh(port, app_id):
job = engine_request(port, "refresh_app", {"app_id": app_id})
return job if isinstance(job, str) and re.fullmatch(r"[a-fA-F\d-]{36}", job) else None
@lru_cache(maxsize=128)
def cached_app_icon(root, path, modified, size):
with tempfile.TemporaryDirectory(prefix="companion-app-icon-", dir=private_work(root)) as work:
return archive_icon_preview(path, Path(work))
def installed_app_icon(db, root, device, app, *, include_companion=False):
row = db.execute("""
SELECT a.ipa_path, p.path FROM apps a
LEFT JOIN ipa_installations i ON i.rowid = (
SELECT last.rowid FROM ipa_installations last WHERE last.app_id=a.id
ORDER BY last.installed_at DESC,last.rowid DESC LIMIT 1
)
LEFT JOIN ipa_packages p ON p.id=i.package_id
WHERE a.id=? AND a.device_id=? AND a.installed_at IS NOT NULL
AND (? OR (lower(a.bundle_id) != ? AND lower(a.bundle_id) NOT LIKE ?))
""", (app, device, include_companion, BUNDLE_ID, BUNDLE_ID + ".%")).fetchone()
if not row:
return None
for relative in dict.fromkeys(row):
if not relative:
continue
try:
path = (root / relative).resolve(strict=True)
if not path.is_relative_to((root / "ipas").resolve()) or not path.is_file():
continue
stat = path.stat()
image = cached_app_icon(root, path, stat.st_mtime_ns, stat.st_size)
if image:
return image
except (OSError, ValueError, BadZipFile, RuntimeError, subprocess.SubprocessError):
continue
return None
HOME_SCREEN_PROFILE_ID = "fr.arkemys.siderelay.homescreen"
MAX_HOME_SCREEN_APPS = 256
MAX_PROFILE_REQUEST = 24 * 1024 * 1024
MAX_PROFILE_CACHE = 32 * 1024 * 1024
PROFILE_DESCRIPTION = "Adds Home Screen icons for apps imported into SideRelay."
def home_screen_clip(data):
name, bundle, folder = (data.get(key) for key in ("name", "bundle", "folder"))
if (not isinstance(name, str) or not 0 < len(name.strip()) <= 100 or not name.isprintable()
or not isinstance(bundle, str) or not bundle.endswith(".app") or not 0 < len(bundle) <= 255
or not bundle.isprintable() or any(c in bundle for c in "/\\")
or (folder is not None and (not isinstance(folder, str) or not 0 < len(folder) <= 255
or not folder.isprintable() or folder in (".", "..")
or any(c in folder for c in "/\\")))):
raise ValueError("Invalid Home Screen shortcut")
query = {"bundle-name": bundle}
if folder:
query["container-folder-name"] = folder
encoded_query = urlencode(query, quote_via=quote)
app_key = hashlib.sha256(bundle.encode()).hexdigest()[:24]
clip_id = f"{HOME_SCREEN_PROFILE_ID}.{app_key}"
clip = {
"PayloadType": "com.apple.webClip.managed",
"PayloadIdentifier": clip_id,
"PayloadUUID": str(uuid5(NAMESPACE_URL, clip_id)),
"PayloadVersion": 1,
"PayloadDisplayName": name,
"Label": name,
"IsRemovable": True,
"Precomposed": True,
"FullScreen": True,
"URL": "livecontainer://livecontainer-launch?" + encoded_query,
}
icon = data.get("icon")
if icon is not None:
if not isinstance(icon, str) or len(icon) > 175000:
raise ValueError("Invalid Home Screen icon")
try:
png = base64.b64decode(icon, validate=True)
except binascii.Error as error:
raise ValueError("Invalid Home Screen icon") from error
if (len(png) < 24 or png[:8] != b"\x89PNG\r\n\x1a\n" or png[12:16] != b"IHDR"
or not all(0 < int.from_bytes(png[offset:offset + 4], "big") <= 400 for offset in (16, 20))):
raise ValueError("Invalid Home Screen icon")
clip["Icon"] = png
return clip
def home_screen_clips(data, include_icons=False):
apps = data.get("apps")
if not isinstance(apps, list) or not 0 < len(apps) <= MAX_HOME_SCREEN_APPS:
raise ValueError("Invalid Home Screen app list")
clips = {}
for app in apps:
if not isinstance(app, dict):
raise ValueError("Invalid Home Screen app")
clip = home_screen_clip(app if include_icons else {**app, "icon": None})
clips[clip["PayloadIdentifier"]] = clip
return [clips[key] for key in sorted(clips)]
def home_screen_revision(clips):
destinations = [(clip["PayloadIdentifier"], clip["Label"], clip["URL"]) for clip in clips]
return hashlib.sha256(json.dumps(destinations, ensure_ascii=False).encode()).hexdigest()[:24]
def home_screen_profile(data):
restore = data.get("restore", False)
if not isinstance(restore, bool):
raise ValueError("Invalid Home Screen restore request")
clips = home_screen_clips(data, include_icons=True)
revision = home_screen_revision(clips)
if restore:
# iOS matches Web Clips by payload identifier. Give restored shortcuts new
# identities under the same profile so iOS installs them as new clips.
generation = secrets.token_hex(8)
for clip in clips:
clip["PayloadIdentifier"] += f".{generation}"
clip["PayloadUUID"] = str(uuid5(NAMESPACE_URL, clip["PayloadIdentifier"]))
return plistlib.dumps({
"PayloadType": "Configuration",
"PayloadIdentifier": HOME_SCREEN_PROFILE_ID,
"PayloadUUID": str(uuid5(NAMESPACE_URL, HOME_SCREEN_PROFILE_ID)),
"PayloadVersion": 1,
"PayloadDisplayName": "SideRelay Home Screen Shortcuts",
"PayloadDescription": f"{PROFILE_DESCRIPTION} Revision: {revision}.",
"PayloadOrganization": "SideRelay",
"PayloadContent": clips,
})
def home_screen_profile_state(udid, data):
clips = home_screen_clips(data)
try:
result = devicectl_json(["device", "profile", "list", "--device", udid,
"--type", "configuration", "--search", HOME_SCREEN_PROFILE_ID], timeout=5)
profiles = result["configurationProfiles"]
if not isinstance(profiles, list):
return "unknown"
profile = next((p for p in profiles if p.get("payloadIdentifier") == HOME_SCREEN_PROFILE_ID), None)
except (OSError, ValueError, KeyError, AttributeError, subprocess.SubprocessError):
return "unknown"
if profile is None:
return "missing"
description = profile.get("profileDescription") or ""
marker = re.search(r"Revision: ([a-f0-9]{24})\.$", description)
if marker:
return "current" if marker[1] == home_screen_revision(clips) else "outdated"
# Earlier releases used one fixed description. A newer import still requires an update.
try:
installed_at = datetime.fromisoformat(profile["creationDate"]).timestamp()
except (TypeError, ValueError, KeyError, OverflowError):
return "installed"
if any(isinstance(app.get("installedAt"), (int, float)) and app["installedAt"] > installed_at
for app in data["apps"]):
return "outdated"
return "installed"
def handler(root, owner, engine_port, aliases=None, updates=None):
aliases = aliases or {}
profiles = {}
profile_lock = threading.Lock()
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def reply(self, status_code, result):
data = json.dumps(result).encode()
self.send_response(status_code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(data)
def do_GET(self):
if match := re.fullmatch(r"/home-screen/([a-zA-Z0-9_-]{43})/SideRelay.mobileconfig", self.path):
self.download_profile(match[1])
return
self.handle_companion(self.path if self.path.startswith(("/jobs/", "/apps/")) else "/status")
def do_POST(self):
if self.path in ("/refresh", "/auto-update", "/home-screen", "/home-screen/status"):
self.handle_companion(self.path)
else:
self.reply(404, {})
def download_profile(self, token):
address = tailnet_ip(self.headers.get("X-Forwarded-For", ""))
if not address or self.headers.get("Tailscale-User-Login") != owner:
self.reply(403, {})
return
with profile_lock:
item = profiles.get(token)
if (not item or item[0] <= time.monotonic()
or (address != item[1] and address not in aliases.get(item[1], ()))):
item = None
if not item:
self.reply(404, {})
return
data = item[2]
self.send_response(200)
self.send_header("Content-Type", "application/x-apple-aspen-config")
self.send_header("Content-Disposition", 'attachment; filename="SideRelay.mobileconfig"')
self.send_header("Content-Length", str(len(data)))
self.send_header("Cache-Control", "no-store")
self.send_header("X-Content-Type-Options", "nosniff")
self.end_headers()
try:
self.wfile.write(data)
except OSError:
pass
def read_profile_selection(self, maximum):
length = self.headers.get("Content-Length", "")
if not length.isascii() or not length.isdecimal() or len(length) > 8 or not 0 < int(length) <= maximum:
raise ValueError("Invalid Home Screen request size")
selection = json.loads(self.rfile.read(int(length)))
if not isinstance(selection, dict):
raise ValueError("Expected app selection")
return selection
def prepare_profile(self):
try:
data = home_screen_profile(self.read_profile_selection(MAX_PROFILE_REQUEST))
except (ValueError, OverflowError):
self.reply(400, {})
return
address = tailnet_ip(self.headers.get("X-Forwarded-For", ""))
token = secrets.token_urlsafe(32)
now = time.monotonic()
with profile_lock:
for key, (expires, _, _) in list(profiles.items()):
if expires <= now:
del profiles[key]
if len(profiles) >= 16 or sum(len(item[2]) for item in profiles.values()) + len(data) > MAX_PROFILE_CACHE:
self.reply(429, {})
return
profiles[token] = (now + 300, address, data)
self.reply(200, {"token": token})
def handle_companion(self, path):
self.connection.settimeout(5)
if self.path != path:
self.reply(404, {})
return
try:
uri = (root / "signature.db").as_uri() + "?mode=ro"
with closing(sqlite3.connect(uri, uri=True, timeout=2)) as db:
device = authorized_device(db, self, owner, aliases)
if not device:
self.reply(403, {})
return
if path == "/home-screen":
self.prepare_profile()
return
if path == "/home-screen/status":
udid, = db.execute("SELECT udid FROM devices WHERE id=?", (device,)).fetchone()
try:
state = home_screen_profile_state(udid, self.read_profile_selection(512 * 1024))
except (ValueError, OverflowError):
self.reply(400, {})
return
self.reply(200, {"state": state})
return
if path.startswith("/apps/"):
match = re.fullmatch(r"/apps/([a-fA-F\d-]{36})/icon", path)
image = installed_app_icon(db, root, device, match[1]) if match else None
self.reply(200, {"png": base64.b64encode(image).decode("ascii")} if image else {})
return
if path.startswith("/jobs/"):
job_id = path.removeprefix("/jobs/")
row = db.execute("""
SELECT j.status, j.error FROM jobs j JOIN apps a ON a.id=j.app_id
WHERE j.id=? AND a.device_id=? AND j.kind='refresh'
""", (job_id, device)).fetchone() if re.fullmatch(r"[a-fA-F\d-]{36}", job_id) else None
if not row:
self.reply(404, {})
else:
code = re.match(r"^\[([a-z-]+)\]", row[1] or "")
self.reply(200, {"status": row[0], "errorCode": code[1] if code else None})
return
result = status(db, root, device)
if path in ("/refresh", "/auto-update"):
length = self.headers.get("Content-Length", "")
if not length.isascii() or not length.isdecimal() or len(length) > 3 or not 0 < int(length) <= 256:
self.reply(400, {})
return
try:
data = json.loads(self.rfile.read(int(length)))
except (ValueError, UnicodeError):
data = None
if not isinstance(data, dict):
self.reply(400, {})
return
if path == "/auto-update":
value = data.get("enabled")
if not isinstance(value, bool) or updates is None:
self.reply(400, {})
return
if not updates.set_enabled(db, device, value):
self.reply(409, {})
return
if path == "/refresh":
key = data.get("appID")
app = next((a for a in result["apps"] if a["id"] == key and a["canRefresh"]), None)
if not app:
self.reply(409, {"error": "refresh-unavailable"})
return
job = None if app["busy"] else queue_refresh(engine_port, app["id"])
result = status(db, root, device)
if job is None and not any(a["id"] == key and a["busy"] for a in result["apps"]):
self.reply(409, {"error": "refresh-unavailable"})
return
result["refreshJobID"] = job
if updates:
result.update(updates.state(device))
self.reply(200, result)
except (OSError, ValueError, sqlite3.Error, http.client.HTTPException):
self.close_connection = True
try:
self.reply(503, {})
except OSError:
pass
return Handler
def serve(root, engine_port):
if not enabled(root).is_file():
return
_, owner = endpoint()
peers = tailscale("status", "--json").get("Peer") or {}
aliases = {}
for peer in peers.values():
if peer.get("OS") != "iOS":
continue
addresses = {address for ip in peer.get("TailscaleIPs", []) if (address := tailnet_ip(ip))}
for address in addresses:
aliases[address] = addresses
config = tailscale("serve", "status", "--json")
if str(PORT) in (config.get("TCP") or {}) or any(
host.endswith(f":{PORT}") for host in ((config.get("Web") or {}) | (config.get("AllowFunnel") or {}))
):
raise ValueError("The Tailscale port for the iPhone companion is already in use.")
stop = threading.Event()
for sig in (signal.SIGTERM, signal.SIGINT):
signal.signal(sig, lambda *_: stop.set())
updates = AutoUpdates(root, engine_port, stop)
with ThreadingHTTPServer(("127.0.0.1", 0), handler(root, owner, engine_port, aliases, updates)) as server:
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
relay = subprocess.Popen(
[TAILSCALE, "serve", "--yes", "--https", str(PORT), f"http://127.0.0.1:{server.server_port}"],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
env={**os.environ, "TAILSCALE_BE_CLI": "1"},
)
try:
updates.start()
while relay.poll() is None and not stop.wait(1):
pass
finally:
stop.set()
if relay.poll() is None:
relay.terminate()
try:
relay.wait(timeout=5)
except subprocess.TimeoutExpired:
relay.kill()
relay.wait()
server.shutdown()
thread.join()
updates.close()
if __name__ == "__main__":
try:
serve(Path(sys.argv[1]), int(sys.argv[2]))
except (OSError, ValueError) as error:
sys.exit(str(error))