# tinnci/network_firewall architecture

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

- Interactive diagram: https://gitdiagram.com/tinnci/network_firewall
- Repository: https://github.com/tinnci/network_firewall

## Overview

##### Project type and purpose
- This is a desktop application—a simple network firewall written in Python.
- It provides packet interception (via PyDivert), packet analysis, filtering by protocol/IP/port/content, rule management (rules.yaml), and a PyQt6-based GUI for user interaction.

##### File structure
Top-level:
- main.py – application entry point, permission check, starts UI.
- rules.yaml – persisted firewall rules.
- pyproject.toml – dependency and build config.
- logs/ – runtime logs.
firewall/ module:
- config.py, constants.py – global settings.
- core/ – heart of the firewall logic:
– packet_interceptor.py (captures packets via PyDivert)
– packet_analyzer.py (parses/decodes packet contents)
– rule_manager.py (loads, validates, prioritizes rules)
– packet_processor.py (coordinates analysis → filtering → forwarding/drop)
– firewall.py (orchestrates the overall flow)
- ui/ – PyQt6 GUI: main_window.py + tabbed panels for IP, port, content, performance, logs, advanced settings.
- utils/ – cross-cutting helpers (logging, network, performance).

##### README for technical insights
- Uses PyDivert for interception, PyQt6 for GUI.
- Rules priority: whitelist over blacklist; protocol → IP → port → content.
- Needs administrator rights.
- Targets Windows OS.

a. Main components:
1. User Interface Layer (PyQt6 GUI)
2. Core Firewall Engine
- Packet Interceptor (PyDivert)
- Packet Analyzer
- Rule Manager (rules.yaml + validation)
- Packet Processor
3. Persistence/Storage
- rules.yaml (rule definitions)
- logs/ (logging)
4. External Dependencies
- PyDivert library
- Operating System (Windows packet driver)

b. Relationships & data flow:
- UI → Core: user changes rules/settings via tabs → Rule Manager updates rules.yaml.
- Core startup: Packet Interceptor registers callback with OS; streams raw packets → Packet Analyzer.
- Analyzer outputs structured data → Packet Processor consults Rule Manager → decides ALLOW or DROP.
- Decision → Packet Interceptor (forward or block).
- Packet Processor/Rule Manager log to logs/ via Logging Utils.

c. Architectural patterns/design principles:
- Layered architecture: UI layer separated from Core engine.
- Single Responsibility: each core module has one well-defined role.
- Configuration-driven: behavior defined by rules.yaml.
- Event-driven flow: packets trigger processing pipeline.

d. Key technologies/frameworks/libraries:
- PyDivert (packet interception)
- PyQt6 (GUI)
- PDM (via pyproject.toml)
- Python standard libs for I/O, threading, logging.

##### Recommendations for detail level
- Don’t just group everything under “Core.” Break it into the five core modules.
- How rules.yaml is loaded once at startup and can be modified at runtime by the UI.
- Log path as a separate storage node.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Application Entry"
        Main["main.py"]:::entry
    end

    subgraph "UI Layer"
        Main --> UI["GUI Main Window"]:::frontend
        UI --> IPTab["IP Filter Tab"]:::frontend
        UI --> PortTab["Port Filter Tab"]:::frontend
        UI --> ContentTab["Content Filter Tab"]:::frontend
        UI --> PerfTab["Performance Tab"]:::frontend
        UI --> LogTab["Logs Tab"]:::frontend
        UI --> AdvTab["Advanced Settings Tab"]:::frontend
        UI --> TrafficTab["Traffic Monitor Tab"]:::frontend
    end

    subgraph "Core Firewall Engine"
        FI["firewall.py"]:::core
        PI["Packet Interceptor"]:::core
        PA["Packet Analyzer"]:::core
        PP["Packet Processor"]:::core
        RM["Rule Manager"]:::core
        FI --> PI
        PI --> PA
        PA --> PP
        PP --> PI
        PP -->|drop| Drop["Dropped Packet"]:::external
    end

    OS["OS Network Stack"]:::external
    OS --> PI
    Lib["PyDivert Library"]:::external
    Lib --> PI

    UI -->|updates rules| RM
    RM -->|prioritize| PP

    subgraph "Utilities"
        LogUtil["Logging Utilities"]:::utils
        NetUtil["Network Utilities"]:::utils
        PerfUtil["Performance Utilities"]:::utils
    end

    PP -->|log events| LogUtil
    PA -->|network helpers| NetUtil
    PP -->|performance metrics| PerfUtil

    subgraph "Configuration & Storage"
        rules["rules.yaml"]:::storage
        LogsDir["logs/"]:::storage
        ConfigProj["pyproject.toml"]:::storage
    end

    RM -->|load/validate| rules
    LogUtil --> LogsDir
    Main --> FI

    classDef frontend fill:#D0E6FF,stroke:#0366D6;
    classDef core fill:#E6FFED,stroke:#28A745;
    classDef storage fill:#FFF5E5,stroke:#DBAB09;
    classDef external fill:#FEEFFF,stroke:#D15B47;
    classDef entry fill:#EEEEEE,stroke:#999999;
    classDef utils fill:#E8F6FF,stroke:#3794FF;

    click Main "https://github.com/tinnci/network_firewall/blob/main/main.py"
    click rules "https://github.com/tinnci/network_firewall/blob/main/rules.yaml"
    click ConfigProj "https://github.com/tinnci/network_firewall/blob/main/pyproject.toml"
    click FI "https://github.com/tinnci/network_firewall/blob/main/firewall/core/firewall.py"
    click PI "https://github.com/tinnci/network_firewall/blob/main/firewall/core/packet_interceptor.py"
    click PA "https://github.com/tinnci/network_firewall/blob/main/firewall/core/packet_analyzer.py"
    click PP "https://github.com/tinnci/network_firewall/blob/main/firewall/core/packet_processor.py"
    click RM "https://github.com/tinnci/network_firewall/blob/main/firewall/core/rule_manager.py"
    click UI "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/main_window.py"
    click IPTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/ip_filter_tab.py"
    click PortTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/port_filter_tab.py"
    click ContentTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/content_filter_tab.py"
    click PerfTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/performance_tab.py"
    click LogTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/log_tab.py"
    click AdvTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/advanced_settings_tab.py"
    click TrafficTab "https://github.com/tinnci/network_firewall/blob/main/firewall/ui/tabs/traffic_monitor_tab.py"
    click LogUtil "https://github.com/tinnci/network_firewall/blob/main/firewall/utils/logging_utils.py"
    click NetUtil "https://github.com/tinnci/network_firewall/blob/main/firewall/utils/network_utils.py"
    click PerfUtil "https://github.com/tinnci/network_firewall/blob/main/firewall/utils/performance_utils.py"
    click LogsDir "https://github.com/tinnci/network_firewall/tree/main/logs/"
    click Docs "https://github.com/tinnci/network_firewall/tree/main/docs/"
```
