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
ncdevice · 2.6 KB · 60 lines
| 1 | #!/bin/sh |
| 2 | # CUPS backend for the "My device" printer: print on the device you are |
| 3 | # connected from. CUPS hands it the job as a PDF (the queue's PPD asks for |
| 4 | # one); it goes to nc-host's send socket with print=1, and the phone, iPad, |
| 5 | # Mac or browser opens its own print dialog with it (AirPrint on iOS). |
| 6 | # Nobody connected: the PDF is kept in ~/Documents/Printed and a notification |
| 7 | # says so, rather than the job vanishing. |
| 8 | # |
| 9 | # Installed 0700 root so CUPS runs it as root (it needs the send socket and to |
| 10 | # write into the desk user's home). Arguments: JOB USER TITLE COPIES OPTIONS [FILE]. |
| 11 | SOCK=${NC_SEND_SOCKET:-/run/nc-host/send.sock} |
| 12 | if [ $# -eq 0 ]; then |
| 13 | echo 'direct ncdevice "Unknown" "Print on my device (network-computer)"' |
| 14 | exit 0 |
| 15 | fi |
| 16 | [ $# -ge 5 ] || { echo "usage: ncdevice JOB USER TITLE COPIES OPTIONS [FILE]" >&2; exit 1; } |
| 17 | TITLE=$3 |
| 18 | tell() { # a desktop notification in the desk user's session, if there is one |
| 19 | nc-as-user notify-send -i printer "My device" "$1" >/dev/null 2>&1 || true |
| 20 | } |
| 21 | |
| 22 | # a file name from the job's title: "Welcome.txt" -> "Welcome.pdf" |
| 23 | base=$(basename "$TITLE" 2>/dev/null | sed 's/\.[A-Za-z0-9]\{1,5\}$//; s/[^A-Za-z0-9 ._()-]/_/g; s/^[ .]*//' | cut -c1-80) |
| 24 | [ -n "$base" ] || base="print-$1" |
| 25 | NAME="$base.pdf" |
| 26 | |
| 27 | TMP=$(mktemp /tmp/ncdevice-XXXXXX.pdf) || exit 1 |
| 28 | trap 'rm -f "$TMP"' EXIT |
| 29 | if [ -n "$6" ]; then cat "$6" > "$TMP"; else cat > "$TMP"; fi |
| 30 | [ -s "$TMP" ] || { echo "ERROR: empty job" >&2; exit 1; } |
| 31 | |
| 32 | Q=$(python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$NAME") |
| 33 | echo "INFO: sending $NAME to the connected device" >&2 |
| 34 | out=$(curl -sS --unix-socket "$SOCK" -T "$TMP" -w '\n%{http_code}' "http://desk/send?name=$Q&print=1" 2>&1) |
| 35 | code=$(echo "$out" | tail -n 1) |
| 36 | if [ "$code" = 200 ]; then |
| 37 | echo "INFO: $(echo "$out" | sed '$d')" >&2 |
| 38 | tell "Printing $NAME on your device" |
| 39 | exit 0 |
| 40 | fi |
| 41 | |
| 42 | # not delivered: keep it for the desk user |
| 43 | [ -r /etc/nc/env ] && . /etc/nc/env |
| 44 | U=${NC_DESK_USER:-user}; H=$(getent passwd "$U" | cut -d: -f6) |
| 45 | case "$code" in |
| 46 | 503) why="no device is connected" ;; |
| 47 | 000|"") why="the desk's connection service is not running" ;; |
| 48 | *) why=$(echo "$out" | sed '$d' | head -1); [ -n "$why" ] || why="the device did not take it" ;; |
| 49 | esac |
| 50 | if [ -n "$H" ] && [ -d "$H" ]; then |
| 51 | runuser -u "$U" -- mkdir -p "$H/Documents/Printed" |
| 52 | dest="$H/Documents/Printed/$NAME"; n=2 |
| 53 | while [ -e "$dest" ]; do dest="$H/Documents/Printed/$base ($n).pdf"; n=$((n+1)); done |
| 54 | install -o "$U" -g "$(id -gn "$U")" -m 644 "$TMP" "$dest" |
| 55 | echo "WARNING: $why; saved $dest" >&2 |
| 56 | tell "Not printed ($why). Saved as Documents/Printed/$(basename "$dest")" |
| 57 | exit 0 |
| 58 | fi |
| 59 | echo "ERROR: $why" >&2 |
| 60 | exit 1 |