Recycle the macOS VNC agent per connection so permission prompts work

This commit is contained in:
Viktor Liu
2026-08-07 15:59:45 +02:00
parent ed1fb4b309
commit e95365f8b1
9 changed files with 222 additions and 103 deletions
+4 -2
View File
@@ -74,6 +74,8 @@ var vncAgentCmd = &cobra.Command{
log.Debugf("chmod %s: %v", vncAgentSocket, err)
}
ctx := cmd.Context()
capturer, injector, err := newAgentResources()
if err != nil {
_ = ln.Close()
@@ -87,12 +89,12 @@ var vncAgentCmd = &cobra.Command{
Listener: ln,
})
if err := srv.Start(cmd.Context(), netip.AddrPort{}, netip.Prefix{}); err != nil {
if err := srv.Start(ctx, netip.AddrPort{}, netip.Prefix{}); err != nil {
return fmt.Errorf("start vnc server: %w", err)
}
log.Infof("vnc-agent listening on %s, ready", vncAgentSocket)
<-cmd.Context().Done()
<-ctx.Done()
log.Info("vnc-agent context cancelled, shutting down")
return srv.Stop()
},
+6 -6
View File
@@ -9,12 +9,12 @@ import (
)
func newAgentResources() (vncserver.ScreenCapturer, vncserver.InputInjector, error) {
// Ask for Screen Recording here and nowhere else: this process runs as the
// console user, which is what TCC requires for a user-scope service, and it
// is the point where somebody is demonstrably trying to view the screen.
// Granting it also requires the capturing process to restart, which comes
// for free since the agent is respawned per session.
vncserver.PrimeScreenCapturePermission()
// Ask for Screen Recording here and nowhere else. This process runs as the
// console user, which TCC requires for a user-scope service, and it is fresh
// per connection, which is what makes the dialog appear at all: TCC shows it
// once per process. The request blocks until the user answers, so it also
// keeps the Accessibility ask that follows the first input out of its way.
vncserver.RequestScreenRecording()
capturer := vncserver.NewMacPoller()
injector, err := vncserver.NewMacInputInjector()