FreeRDP's shadow server for macOS
=================================

Share this Mac's own screen with RDP clients: the kit's FreeRDP clients, FreeRDP Web,
mstsc. What the user sees is what the client sees; the sound the Mac plays comes along.

  FreeRDP-Shadow.app         the shadow server (FreeRDP's freerdp-shadow-cli, as an app),
                             with freerdp-shadow-agent for the daemon mode
  io.github.congzhangzh.freerdp-shadow.plist
                             a LaunchAgent: starts it at login, in your session (no root)
  io.github.congzhangzh.freerdp-shadow.daemon.plist,
  io.github.congzhangzh.freerdp-shadow.agent.plist,
  install-daemon.sh, uninstall-daemon.sh
                             the daemon mode (root): from the login window on, see
                             "The daemon mode" below
  winpr-hash                 a line of a SAM file (not needed for macOS accounts)

Install one of the two, the LaunchAgent below or the daemon mode: both listen on
127.0.0.1:3388.

Install
-------

  cp -R FreeRDP-Shadow.app /Applications/
  xattr -dr com.apple.quarantine /Applications/FreeRDP-Shadow.app
  cp io.github.congzhangzh.freerdp-shadow.plist ~/Library/LaunchAgents/
  launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/io.github.congzhangzh.freerdp-shadow.plist

The first start asks for two permissions, in System Settings > Privacy & Security:
Screen & System Audio Recording (the picture and the sound) and Accessibility (the
keyboard and mouse). Allow FreeRDP Shadow in both, then restart it:

  launchctl kickstart -k gui/$(id -u)/io.github.congzhangzh.freerdp-shadow

The app is signed with our Developer ID and notarized by Apple. If you used a release
from before the signed one, its name changed with the signature (org.freerdp.shadow is
now io.github.congzhangzh.freerdp-shadow), so macOS asks for both permissions once more:
remove the old FreeRDP Shadow entry from both lists (the minus button), then allow the
new one. Later updates, signed the same way, should keep them.

Already running the LaunchAgent from an older release? It keeps working under its old
name. To move it to the new one, stop and remove the old one, then run the cp and
bootstrap lines above:

  launchctl bootout gui/$(id -u)/org.freerdp.shadow
  rm ~/Library/LaunchAgents/org.freerdp.shadow.plist

The codec follows the client's own order. The kit's FreeRDP clients ask for H.265 first,
so this Mac sends H.265 (VideoToolbox, in hardware), and AV1 to a client without H.265.
AV2 goes only to a client that asks for it first (/gfx:AV2). To turn AV2 off: change
<string>/gfx-av2</string> in the plist to <string>/gfx-av2:off</string>, and restart.

H.264 goes to the clients without AV1 or AV2 (Windows' own Remote Desktop client, most
others), encoded by the Mac's own VideoToolbox (in hardware where the Mac has it), as
AVC420: the plist's -gfx-avc444 leaves AVC444 out, which sent 15-18 times the bytes for
the same screen (its median frame about 60 times bigger). Remove that line to allow
AVC444. VideoToolbox has no AV1 or AV2 encoder: those stay software encodes (libaom,
libavm).

Remove it with

  launchctl bootout gui/$(id -u)/io.github.congzhangzh.freerdp-shadow
  rm ~/Library/LaunchAgents/io.github.congzhangzh.freerdp-shadow.plist
  rm -R /Applications/FreeRDP-Shadow.app

The size of the picture
-----------------------

It shares this Mac's own screen as it is: the displays, their resolution and their scale
stay what macOS has set, and a client's window shows that screen, scaled if the window is
smaller. The server cannot take another size (no Display Control), so a client's
/dynamic-resolution, its /multimon layout and its HiDPI scale factors change nothing here,
and the client logs no "HiDPI:" line.
For a session that follows the client's windows and monitors, HiDPI included, use a server
that starts sessions of its own: xrdp, or Windows' own Remote Desktop.

Connect
-------

The login is the macOS account running it (your own), checked by macOS (PAM) over TLS
security (/sec:tls in the plist; keep it there). Keep the server on 127.0.0.1 as
above and reach it through SSH (System Settings > General > Sharing > Remote Login):

  ssh -L 13388:127.0.0.1:3388 <user>@<this Mac>

then connect a client to 127.0.0.1:13388. It listens on 3388, the kit's shadow port on
every OS (3389 is for the system's own RDP servers).

What it cannot do
-----------------

It runs in your session, after you have logged in: it cannot show the login window,
or this Mac before anyone logs in (a LaunchAgent has no screen until then). The daemon
mode can.

The daemon mode
---------------

The server runs as root, started by launchd at boot, and an agent captures the screen
and posts the input in the session on the console: at the login window, and in each
user's session once they log in. One connection lasts from the login window through the
login to the desktop. The login is any local account but root at the login window, the
user on the console after that (PAM, /sec:tls).

  io.github.congzhangzh.freerdp-shadow.daemon
                             /Library/LaunchDaemons/io.github.congzhangzh.freerdp-shadow.daemon.plist:
                             the server, as root, on 127.0.0.1:3388; Mach service
                             io.github.congzhangzh.freerdp-shadow (FREERDP_SHADOW_MAC_AGENT_SERVICE)
  io.github.congzhangzh.freerdp-shadow.agent
                             /Library/LaunchAgents/io.github.congzhangzh.freerdp-shadow.agent.plist:
                             freerdp-shadow-agent, at the login window (as root) and in
                             each user's session (as that user)
  the app                    /Applications/FreeRDP-Shadow.app (owned by root)

First remove the LaunchAgent above if you installed it
(io.github.congzhangzh.freerdp-shadow, or org.freerdp.shadow from an older release):
both use 3388, and install-daemon.sh refuses while it runs on the same port. Then, in
the unpacked folder:

  sudo ./install-daemon.sh                  (--port <port> for another port)

Then allow freerdp-shadow-agent, once, in your session (System Settings > Privacy &
Security): Screen & System Audio Recording and Accessibility; with +, the program is
/Applications/FreeRDP-Shadow.app/Contents/MacOS/freerdp-shadow-agent. Restart it:

  launchctl kickstart -k gui/$(id -u)/io.github.congzhangzh.freerdp-shadow.agent

The grants hold at the login window too. Updating from a release before the signed one
asks for both once more: remove the old freerdp-shadow-agent entry (the minus button),
then allow the new one. Run install-daemon.sh again to update or to change the port: it
moves an older install to the new names and keeps its port (it prints which).

Logs: one file per job, appended to by every start:

  /var/log/freerdp-shadow/daemon.log
  /var/log/freerdp-shadow/agent.log         (every session's agent)

Clear one while it runs with  sudo sh -c ': > /var/log/freerdp-shadow/daemon.log'
(the same for agent.log); uninstall-daemon.sh --logs removes the folder.

Remove it with the copy the app carries (it outlives this folder), or this folder's:

  sudo bash /Applications/FreeRDP-Shadow.app/Contents/Resources/uninstall-daemon.sh
  sudo bash ./uninstall-daemon.sh          (--app: the app too; --logs: the logs too)

It stops the agents and the daemon and removes both plists, so launchd starts neither
again.
