# ukanth/afwall architecture

> Architecture diagram of the GitHub repository ukanth/afwall, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ukanth/afwall
- Repository: https://github.com/ukanth/afwall

## Overview

##### Main Components

a. Core Components:
- IPTables Management Layer
- Firewall Rules Engine
- Application Interface Layer
- Storage & Persistence Layer
- System Services Layer

b. Supporting Components:
- Native Binary Components (busybox, iptables)
- Root Access Management
- Event Handling System
- Profile Management
- Logging System

##### Component Relationships

a. User Interface Layer:
- Main Activity (MainActivity.java)
- Preference Management (preferences/*)
- Profile Management (profiles/*)
- Log Viewer (log/*)
- Custom Rules Interface (customrules/*)

b. Business Logic Layer:
- Firewall Service (service/FirewallService.java)
- Rules Application Service (service/RulesApplyService.java)
- Root Shell Service (service/RootShellService.java)
- Interface Tracker (InterfaceTracker.java)

c. Data Layer:
- Profile Database (profiles/ProfilesDatabase.java)
- Log Database (log/LogDatabase.java)
- Custom Rules Database (customrules/CustomRuleDatabase.java)
- Preferences Storage (preferences/*)

##### Architectural Patterns to Highlight

- Event-driven architecture (using RxJava events)
- Service-oriented architecture for background operations
- Repository pattern for data access
- Observer pattern for interface tracking
- Factory pattern for rule creation

##### External Integrations

- Root Access Interface
- System IPTables
- Network Interfaces
- VPN Service
- Xposed Framework Integration

##### Data Flow Indicators

Following flows:
- User input → Rules Engine → IPTables
- Network Events → Interface Tracker → Rule Application
- Application Events → Log Service → Storage
- Profile Selection → Rules Application

##### Security Boundaries

Highlight:
- Root access boundary
- System service boundary
- Application sandbox boundary
- Network interface boundary

**┌─ User Interface Layer ─────────────────────┐**
- Activities | Fragments | Preferences | UI   │
- Business Logic Layer ────────────────────┤
- Services | Rules Engine | Event Handlers   │
- Data Access Layer ──────────────────────┤
- Databases | Preferences | File Storage    │
- System Integration Layer ───────────────┤
- Root Access | IPTables | Network Interface│
- ┘

- Component types and their visual representations
- Security boundary markings
- Integration points
- Event flow indicators

Keep the abstraction level high enough to show the overall architecture while including sufficient detail to understand the system's operation. Focus on the security-critical paths and data flows, as this is a firewall application.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef ui fill:#b3d9ff,stroke:#333,stroke-width:2px
    classDef service fill:#90EE90,stroke:#333,stroke-width:2px
    classDef data fill:#FFE5B4,stroke:#333,stroke-width:2px
    classDef security fill:#FFB6C1,stroke:#333,stroke-width:2px
    classDef system fill:#D3D3D3,stroke:#333,stroke-width:2px
    
    %% User Interface Layer
    subgraph UI
        MA[MainActivity]:::ui
        PA[ProfileActivity]:::ui
        PREF[Preferences]:::ui
        LOG_UI[Log Viewer]:::ui
        CR_UI[Custom Rules UI]:::ui
    end

    %% Business Logic Layer
    subgraph BL
        FWS[FirewallService]:::service
        RAS[RulesApplyService]:::service
        RSS[RootShellService]:::service
        RE[Rules Engine]:::service
        IT[Interface Tracker]:::service
    end

    %% Data Layer
    subgraph DATA
        PDB[(Profile Database)]:::data
        LDB[(Log Database)]:::data
        CRDB[(Custom Rules DB)]:::data
        PS[(Preferences Storage)]:::data
    end

    %% System Integration Layer
    subgraph SYS
        IPT[IPTables Manager]:::security
        ROOT[Root Access]:::security
        NI[Network Interfaces]:::system
        BB[BusyBox]:::system
        IPTB[IPTables Binary]:::system
    end

    %% Event Handling System
    subgraph EVENTS
        EH[Event Handler]:::service
        BC[Broadcast Receiver]:::service
    end

    %% Relationships
    MA --> FWS
    MA --> PA
    MA --> PREF
    MA --> LOG_UI
    MA --> CR_UI
    
    FWS --> RAS
    FWS --> RSS
    RAS --> RE
    RE --> IPT
    RSS --> ROOT
    
    IT --> NI
    IT --> RE
    
    EH --> FWS
    BC --> EH
    
    FWS --> PDB
    FWS --> LDB
    FWS --> CRDB
    FWS --> PS
    
    IPT --> BB
    IPT --> IPTB
    
    %% Click events for component mapping
    click MA "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/MainActivity.java"
    click PA "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/activity/ProfileActivity.java"
    click FWS "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/service/FirewallService.java"
    click RAS "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/service/RulesApplyService.java"
    click RSS "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/service/RootShellService.java"
    click IPT "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/Api.java"
    click PDB "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/profiles/ProfilesDatabase.java"
    click LDB "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/log/LogDatabase.java"
    click CRDB "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/customrules/CustomRuleDatabase.java"
    click ROOT "https://github.com/ukanth/afwall/blob/beta/app/src/main/java/com/stericson/rootshell/RootShell.java"
    click EH "https://github.com/ukanth/afwall/tree/beta/app/src/main/java/dev/ukanth/ufirewall/events"
    click BC "https://github.com/ukanth/afwall/tree/beta/app/src/main/java/dev/ukanth/ufirewall/broadcast"
    
    %% Legend
    subgraph Legend
        L1[UI Components]:::ui
        L2[Services]:::service
        L3[Data Storage]:::data
        L4[Security Components]:::security
        L5[System Components]:::system
    end
```
