How tinnci/network_firewall works
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.