How selfcontrolapp/selfcontrol works
Project type and purpose
• This is an open-source macOS desktop application (“SelfControl”) with three main user interfaces (GUI app, CLI, and a helper UI “SelfControl Killer”), a privileged daemon, and helper tools. Its purpose is to let users block access to specified domains/network services for a set time period.
Key top-level components (from file tree & README)
• GUI App (“SelfControl”) – Xcode targets under AppController.*, .xib UI resources, Preferences* controllers, Sparkle updater integration via Sparkle.framework.
• CLI App (cli-main.m) – command-line entrypoint that uses the same core services.
• Helper UI (“SelfControl Killer”) – a small GUI to forcibly kill processes when block is active.
• Daemon (Daemon/) – a privileged background service (selfcontrold) that actually enforces blocks. Communicates via XPC.
• Privileged Helper (SCKillerHelper, LaunchctlHelper) – launched by the daemon or app to perform root-level operations (e.g., modifying /etc/hosts or PF rules).
• Block Management layer (Block Management/) – classes that implement host-file editing, PF packet filter rules, allowlist scraping, Thunderbird prefs parsing, etc.
• Common Utilities (Common/) – settings storage, file watchers, XPC client/server interfaces, authorization wrappers, error handling.
• Resources & Localization – dozens of .lproj directories for multi-language support.
Core interactions & data flow
1. User sets a timer & block list in the GUI or via CLI.
2. App writes settings (SCSettings) and invokes the daemon over XPC (SCXPCClient) calling methods defined in SCDaemonProtocol.
3. Daemon (SCDaemon) spawns or uses the privileged helper (SCKillerHelper / LaunchctlHelper) to gain root.
4. Helper invokes the Block Management classes (HostFileBlocker, PacketFilter, etc.) to apply OS-level blocks (modify /etc/hosts, install PF rules).
5. Daemon starts file watchers (SCFileWatcher) to monitor for tampering.
6. Daemon handles timer countdown. Once time expires or user requests unblock, daemon instructs helper to remove rules.
7. Helper UI (“SelfControl Killer”) can be invoked to kill blacklisted processes—communicating back to daemon/XPC.
8. Sparkle updater integrated into the GUI app checks for new releases.
Architectural patterns & design principles
• Separation of concerns: UI vs. business logic vs. enforcement layer.
• Client-server via XPC: unprivileged UI/CLI clients talk to privileged daemon.
• Principle of least privilege: only helper tools run as root for specific tasks.
• Modular block management: each blocking technique (hosts file, PF, mail scrapers) is encapsulated.
• Observer pattern: file-watching to detect and revert unauthorized changes.