# selfcontrolapp/selfcontrol architecture

> Architecture diagram of the GitHub repository selfcontrolapp/selfcontrol, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/selfcontrolapp/selfcontrol
- Repository: https://github.com/selfcontrolapp/selfcontrol

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Clients
    subgraph "User Interfaces"  
        direction TB
        SelfControlGUI["SelfControl GUI App"]:::frontend
        CLI["selfcontrol-cli (CLI App)"]:::frontend
        KillerUI["SelfControl Killer UI"]:::frontend
    end

    %% IPC Layer
    subgraph "IPC Layer"  
        direction TB
        XPCClient["SCXPCClient"]:::ipc
        SCDaemonProt["SCDaemonProtocol"]:::ipc
    end

    %% Daemon
    subgraph "Daemon Process"  
        direction TB
        DaemonSrv["selfcontrold (SCDaemon)"]:::daemon
    end

    %% Privileged Helpers
    subgraph "Privileged Helpers"  
        direction TB
        Helper["SCKillerHelper / LaunchctlHelper"]:::helper
    end

    %% Block Management
    subgraph "Block Management Library"  
        direction TB
        BlockMgr["HostFileBlocker, PacketFilter, etc."]:::block
    end

    %% OS & Network Subsystem
    subgraph "OS & Network Subsystem"  
        direction TB
        OSHosts["/etc/hosts"]:::os
        OSFilter["PF Packet Filter"]:::os
    end

    %% File Watcher Utility
    FileWatcher["SCFileWatcher (File Watcher)"]:::util

    %% Sparkle Updater
    Sparkle["Sparkle Updater"]:::updater

    %% Localization Resources
    Localization["Localization (.lproj dirs)"]:::resources

    %% Connections
    SelfControlGUI -->|writes settings & calls| XPCClient
    CLI -->|writes settings & calls| XPCClient
    KillerUI -->|invokes kill via| XPCClient
    XPCClient -->|calls methods defined in| SCDaemonProt
    SCDaemonProt -->|IPC communicates| DaemonSrv
    DaemonSrv -->|spawns| Helper
    Helper -->|uses| BlockMgr
    BlockMgr -->|modifies| OSHosts
    BlockMgr -->|installs rules in| OSFilter
    DaemonSrv -->|monitors| FileWatcher
    FileWatcher -->|reports tamper events| DaemonSrv
    SelfControlGUI -->|checks for updates| Sparkle
    Localization --- SelfControlGUI

    %% Click Events
    click SelfControlGUI "https://github.com/selfcontrolapp/selfcontrol/blob/master/AppController.h"
    click CLI "https://github.com/selfcontrolapp/selfcontrol/blob/master/cli-main.m"
    click KillerUI "https://github.com/selfcontrolapp/selfcontrol/blob/master/SelfControl Killer/AppDelegate.h"
    click XPCClient "https://github.com/selfcontrolapp/selfcontrol/blob/master/Common/SCXPCClient.h"
    click SCDaemonProt "https://github.com/selfcontrolapp/selfcontrol/blob/master/Daemon/SCDaemonProtocol.h"
    click DaemonSrv "https://github.com/selfcontrolapp/selfcontrol/blob/master/Daemon/DaemonMain.m"
    click Helper "https://github.com/selfcontrolapp/selfcontrol/blob/master/SCKillerHelper/main.m"
    click BlockMgr "https://github.com/selfcontrolapp/selfcontrol/blob/master/Block Management/BlockManager.h"
    click FileWatcher "https://github.com/selfcontrolapp/selfcontrol/blob/master/Common/SCFileWatcher.h"
    click Sparkle "https://github.com/selfcontrolapp/selfcontrol/blob/master/Sparkle.framework"

    %% Styles
    classDef frontend fill:#f9f,stroke:#333,stroke-width:1px
    classDef ipc fill:#bbf,stroke:#333,stroke-width:1px
    classDef daemon fill:#fbf,stroke:#333,stroke-width:1px
    classDef helper fill:#ffb,stroke:#333,stroke-width:1px
    classDef block fill:#bfb,stroke:#333,stroke-width:1px
    classDef os fill:#ff9,stroke:#333,stroke-width:1px
    classDef util fill:#9bf,stroke:#333,stroke-width:1px
    classDef updater fill:#dfd,stroke:#333,stroke-width:1px
    classDef resources fill:#eee,stroke:#333,stroke-width:1px,title
```
