mirror of
https://github.com/saphid/frame-control.git
synced 2026-10-06 01:00:18 +02:00
Merge remote-tracking branch 'origin/main' into fix/windows-ssh-config-acl
This commit is contained in:
commit
e63dc43c2f
5 files changed
+258
-11
No files matched your search
+54
-7
@@ -5,10 +5,12 @@ Everything here runs on your computer, not the Frame. Python stdlib only.
|
||||
CLI (used by the Electron app, so terminal handling lives in one place):
|
||||
python3 ui/frame_host.py terminal -- CMD [ARG...] # open CMD in a terminal window
|
||||
"""
|
||||
import hashlib
|
||||
import io
|
||||
import os
|
||||
import shlex
|
||||
import shutil
|
||||
import socket
|
||||
import ssl
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -34,6 +36,10 @@ class HostError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class Unreachable(HostError):
|
||||
"""The Frame, or a service on it, didn't answer: the person's to sort out, not a fault here."""
|
||||
|
||||
|
||||
def run_ssh(argv, **kwargs):
|
||||
"""Run an OpenSSH tool without Windows' redirected-stderr pipe hang.
|
||||
|
||||
@@ -350,24 +356,65 @@ def open_steam_link():
|
||||
return "Steam Link isn't installed; opened its download page"
|
||||
|
||||
|
||||
RDP_PORT = 3389
|
||||
RDP_USER = "steamos" # xrdp signs in with the Developer Mode password, not this computer's
|
||||
# xrdp's certificate is its own, so every client warns about it first.
|
||||
RDP_LOGIN = (f"accept the warning about the Frame's certificate, then sign in as {RDP_USER} "
|
||||
"with your Developer Mode password")
|
||||
|
||||
|
||||
def check_rdp(host, timeout=3):
|
||||
"""Raise Unreachable, saying why, unless the Frame's RDP port takes a connection."""
|
||||
try:
|
||||
with socket.create_connection((host, RDP_PORT), timeout=timeout):
|
||||
return
|
||||
except ConnectionRefusedError:
|
||||
raise Unreachable(f"The Frame at {host} is on but isn't accepting remote desktop (port {RDP_PORT} "
|
||||
"refused). Turn on Developer Mode in Steam Settings > System on the headset, "
|
||||
"then restart it and try again.") from None
|
||||
except socket.gaierror:
|
||||
raise Unreachable(f"Can't find {host} on the network for remote desktop. Check the headset's "
|
||||
"address on the Devices tab.") from None
|
||||
except OSError as e:
|
||||
raise Unreachable(f"The Frame didn't answer remote desktop at {host} ({e}). It may be asleep, "
|
||||
"switched off or on another network; if it's on, check Developer Mode is on "
|
||||
"in Steam Settings > System.") from None
|
||||
|
||||
|
||||
def rdp_file(host):
|
||||
"""A Remote Desktop connection file for the Frame. mstsc /v: alone offers this
|
||||
computer's Windows account, which xrdp turns away; the file names steamos instead."""
|
||||
if any(c in host for c in "\r\n"):
|
||||
raise HostError("That headset address can't be used for remote desktop")
|
||||
# One file per address, so two launches close together can't swap headsets.
|
||||
path = cache_dir(f"frame-{hashlib.sha256(host.encode()).hexdigest()[:16]}.rdp")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8", newline="\r\n") as f: # Path.write_text(newline=) is 3.10+
|
||||
f.write(f"full address:s:{host}\nusername:s:{RDP_USER}\n")
|
||||
return path
|
||||
|
||||
|
||||
def open_rdp(alias, host=None):
|
||||
"""Remote desktop to the Frame's xrdp (user steamos), at `host` or where the alias points."""
|
||||
host = host or ssh_hostname(alias)
|
||||
# The client would open either way and then fail on its own, with nothing said here.
|
||||
check_rdp(host)
|
||||
if MAC:
|
||||
if subprocess.run(["open", "-a", "Windows App"], capture_output=True).returncode == 0:
|
||||
return "Opened Windows App"
|
||||
return f"Opened Windows App: connect to {host} and {RDP_LOGIN}"
|
||||
open_url("https://apps.apple.com/app/windows-app/id1295203466")
|
||||
return "Windows App isn't installed; opened its App Store page"
|
||||
if WINDOWS:
|
||||
_spawn(["mstsc.exe", f"/v:{host}"])
|
||||
return f"Opened Remote Desktop to {host}"
|
||||
_spawn(["mstsc.exe", str(rdp_file(host))])
|
||||
# Windows asks about the unsigned connection file first.
|
||||
return f"Opened Remote Desktop to {host}: choose Connect, {RDP_LOGIN}"
|
||||
if which("remmina"):
|
||||
_spawn(["remmina", "-c", f"rdp://steamos@{host}"])
|
||||
return f"Opened Remmina to {host}"
|
||||
_spawn(["remmina", "-c", f"rdp://{RDP_USER}@{host}"])
|
||||
return f"Opened Remmina to {host}: {RDP_LOGIN}"
|
||||
for name in ("xfreerdp3", "xfreerdp"):
|
||||
if which(name):
|
||||
_spawn([name, f"/v:{host}", "/u:steamos", "/dynamic-resolution"])
|
||||
return f"Opened FreeRDP to {host}"
|
||||
_spawn([name, f"/v:{host}", f"/u:{RDP_USER}", "/dynamic-resolution"])
|
||||
return f"Opened FreeRDP to {host}: {RDP_LOGIN}"
|
||||
raise HostError("No RDP client found: install Remmina or FreeRDP")
|
||||
|
||||
|
||||
|
||||
+22
-2
@@ -1225,6 +1225,8 @@ def open_thing(body):
|
||||
raise Failure("That screenshot isn't saved on this computer yet", 404)
|
||||
frame_host.reveal_path(saved)
|
||||
return {"message": f"Showed {saved.name} in {frame_host.FILE_MANAGER}"}
|
||||
except frame_host.Unreachable as e:
|
||||
raise Failure(str(e), 400) # theirs to turn on; nothing failed here
|
||||
except frame_host.HostError as e:
|
||||
raise Failure(str(e), 500)
|
||||
raise Failure("unknown target", 400)
|
||||
@@ -2288,6 +2290,11 @@ def push_file(path, dest="Downloads/"):
|
||||
return f"Sent {name} to ~/{dest}"
|
||||
|
||||
|
||||
class ClientGone(Exception):
|
||||
"""The page went away (a reload, the app quitting) before its reply was written:
|
||||
nobody to answer, and nothing went wrong here."""
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
server_version = "FrameControl/1"
|
||||
timeout = 60 # per socket operation, so a stalled client can't hold a thread
|
||||
@@ -2320,8 +2327,11 @@ class Handler(BaseHTTPRequestHandler):
|
||||
# Nobody may frame the UI (clickjacking).
|
||||
self.send_header("X-Frame-Options", "DENY")
|
||||
self.send_header("Content-Security-Policy", "frame-ancestors 'none'")
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
try:
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
except ConnectionError as e: # Windows says ConnectionAbortedError, others BrokenPipeError
|
||||
raise ClientGone() from e
|
||||
|
||||
def send_json(self, obj, status=200):
|
||||
self.send_bytes(json.dumps(obj).encode(), "application/json", status)
|
||||
@@ -2364,6 +2374,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
from apk_sources import _images
|
||||
try:
|
||||
self.send_bytes(*_images.image(path.rsplit("/", 1)[-1]))
|
||||
except ClientGone:
|
||||
raise
|
||||
except Exception:
|
||||
self.send_json({"error": "Artwork unavailable"}, 404)
|
||||
elif path == "/api/sources/details":
|
||||
@@ -2441,6 +2453,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
headers=[("X-Capture-Source", "gamescope")])
|
||||
else:
|
||||
self.send_json({"error": "not found"}, 404)
|
||||
except ClientGone:
|
||||
raise
|
||||
except Failure as e:
|
||||
self.send_error_json(str(e), e.status, e.apk)
|
||||
except ValueError as e:
|
||||
@@ -2475,6 +2489,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
with (contextlib.nullcontext() if path == "/api/devices" else working(meant)):
|
||||
result = handler(body)
|
||||
self.send_json(result)
|
||||
except ClientGone:
|
||||
raise
|
||||
except Failure as e:
|
||||
if e.status >= 500:
|
||||
frame_telemetry.diagnostic(f"POST {path} {action_of(body)}", e)
|
||||
@@ -2650,6 +2666,10 @@ class LoopbackServer(ThreadingHTTPServer):
|
||||
socketserver.TCPServer.server_bind(self)
|
||||
self.server_name, self.server_port = "127.0.0.1", self.server_address[1]
|
||||
|
||||
def handle_error(self, request, client_address):
|
||||
if not isinstance(sys.exc_info()[1], ClientGone):
|
||||
super().handle_error(request, client_address)
|
||||
|
||||
|
||||
_ONE_SERVER = None
|
||||
|
||||
|
||||
Reference in new issue
Block a user