askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
nc-webapp · 3.9 KB · 106 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """nc-webapp: a web page as a desktop app, in its own window. |
| 3 | |
| 4 | nc-webapp NAME URL [ICON] |
| 5 | |
| 6 | No tabs, no address bar: a window with the app's name, its own taskbar entry |
| 7 | and icon, and its own cookie store (so "Talk" stays signed in as itself). |
| 8 | Links to other sites open in the normal browser. The microphone and camera |
| 9 | are allowed for local pages only (the object server on this desk). |
| 10 | Keys: Ctrl+R reload, Ctrl+[ / Alt+Left back, Ctrl+W or Ctrl+Q close. |
| 11 | |
| 12 | Built on WebKitGTK (the engine of GNOME Web). The object server's pages use it |
| 13 | through the desk's sign-in helper: nc-webapp Talk http://localhost:8009/open?next=/talk |
| 14 | """ |
| 15 | import os |
| 16 | import re |
| 17 | import subprocess |
| 18 | import sys |
| 19 | from urllib.parse import urlsplit |
| 20 | |
| 21 | import gi |
| 22 | |
| 23 | gi.require_version("Gtk", "3.0") |
| 24 | gi.require_version("WebKit2", "4.1") |
| 25 | from gi.repository import GLib, Gtk, WebKit2 # noqa: E402 |
| 26 | |
| 27 | LOCAL = {"localhost", "127.0.0.1"} |
| 28 | |
| 29 | |
| 30 | def main(): |
| 31 | if len(sys.argv) < 3: |
| 32 | print(__doc__) |
| 33 | sys.exit(2) |
| 34 | name, url = sys.argv[1], sys.argv[2] |
| 35 | icon = sys.argv[3] if len(sys.argv) > 3 else "applications-internet" |
| 36 | slug = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-") or "app" |
| 37 | GLib.set_prgname(f"nc-{slug}") # its own taskbar group |
| 38 | GLib.set_application_name(name) |
| 39 | |
| 40 | data = os.path.join(GLib.get_user_data_dir(), "nc-webapp", slug) |
| 41 | cache = os.path.join(GLib.get_user_cache_dir(), "nc-webapp", slug) |
| 42 | manager = WebKit2.WebsiteDataManager(base_data_directory=data, base_cache_directory=cache) |
| 43 | ctx = WebKit2.WebContext.new_with_website_data_manager(manager) |
| 44 | manager.get_cookie_manager().set_persistent_storage(os.path.join(data, "cookies.sqlite"), |
| 45 | WebKit2.CookiePersistentStorage.SQLITE) |
| 46 | view = WebKit2.WebView.new_with_context(ctx) |
| 47 | s = view.get_settings() |
| 48 | s.set_enable_media_stream(True) |
| 49 | s.set_enable_developer_extras(True) |
| 50 | |
| 51 | win = Gtk.Window(title=name) |
| 52 | win.set_icon_name(icon) |
| 53 | win.set_default_size(1100, 800) |
| 54 | win.add(view) |
| 55 | win.connect("destroy", Gtk.main_quit) |
| 56 | view.connect("notify::title", lambda v, _: win.set_title( |
| 57 | f"{v.get_title()} — {name}" if v.get_title() and v.get_title() != name else name)) |
| 58 | |
| 59 | def outside(uri): |
| 60 | host = urlsplit(uri).hostname or "" |
| 61 | return uri.startswith(("http://", "https://")) and host not in LOCAL |
| 62 | |
| 63 | def browser(uri): |
| 64 | subprocess.Popen(["xdg-open", uri], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 65 | |
| 66 | def decide(v, decision, kind): |
| 67 | if kind in (WebKit2.PolicyDecisionType.NAVIGATION_ACTION, WebKit2.PolicyDecisionType.NEW_WINDOW_ACTION): |
| 68 | uri = decision.get_navigation_action().get_request().get_uri() |
| 69 | if outside(uri) or kind == WebKit2.PolicyDecisionType.NEW_WINDOW_ACTION: |
| 70 | browser(uri) # other sites, and new windows, go to the real browser |
| 71 | decision.ignore() |
| 72 | return True |
| 73 | return False |
| 74 | |
| 75 | def permission(v, request): |
| 76 | local = (urlsplit(v.get_uri() or "").hostname or "") in LOCAL |
| 77 | if isinstance(request, WebKit2.UserMediaPermissionRequest) and local: |
| 78 | request.allow() |
| 79 | else: |
| 80 | request.deny() |
| 81 | return True |
| 82 | |
| 83 | def keys(w, ev): |
| 84 | ctrl = ev.state & 4 # Gdk.ModifierType.CONTROL_MASK |
| 85 | alt = ev.state & 8 # Gdk.ModifierType.MOD1_MASK |
| 86 | k = ev.keyval |
| 87 | if ctrl and k in (ord("r"), ord("R")): |
| 88 | view.reload() |
| 89 | elif (ctrl and k == ord("[")) or (alt and k == 0xff51): # Left |
| 90 | view.go_back() |
| 91 | elif ctrl and k in (ord("w"), ord("W"), ord("q"), ord("Q")): |
| 92 | win.destroy() |
| 93 | else: |
| 94 | return False |
| 95 | return True |
| 96 | |
| 97 | view.connect("decide-policy", decide) |
| 98 | view.connect("permission-request", permission) |
| 99 | win.connect("key-press-event", keys) |
| 100 | view.load_uri(url) |
| 101 | win.show_all() |
| 102 | Gtk.main() |
| 103 | |
| 104 | |
| 105 | if __name__ == "__main__": |
| 106 | main() |