# mcw0/dahuaconsole architecture

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

- Interactive diagram: https://gitdiagram.com/mcw0/dahuaconsole
- Repository: https://github.com/mcw0/dahuaconsole

## Overview

##### Project type and purpose
- This is a Python-based command-line interface (CLI) tool named “Dahua Console.”
- Its purpose is to connect to Dahua IP devices (cameras, DVRs) over various protocols, perform authentication (including exploit CVE methods), dump configuration or service data, subscribe to events, and optionally relay through SSH.

##### File structure
- Top-level files:
- Console.py – the main entry point and argument parser (CLI layer)
- connection.py – session/connection management
- net.py – raw network I/O (sockets, TCP/SSL)
- relay.py – SSH tunneling wrapper for connections
- servers.py – local server(s) if reverse connections or callbacks are needed
- dahua.py – high-level protocol API (commands, dumps, subscribe)
- dahua_logon_modes.py – implementations of all supported login modes
- events.py – event subscription and message handling
- eventviewer.py – interactive or formatted display of incoming events
- pwdmanager.py – password/credential hashing or transformations
- utils.py – shared helpers (JSON I/O, logging, hexdump, config file persistence)
- Console.json (generated at runtime) – saved host hashes/credentials
- Configuration: requirements.txt, .gitignore, LICENSE, README.md.

##### README for additional insights
- Lists all CLI arguments (--rhost, ‑-proto, ‑-logon, ‑-dump, ‑-events, etc.)
- Describes protocol choices: dhip, dvrip, HTTP(S), 3DES, onvif, loopback, etc.
- Mentions support for SSH relays, JSON dumps, test mode, debug modes.
- Highlights CVE-based exploit flows for authentication bypass.

a. Main components
1. CLI Layer (Console.py)
2. Argument Parser & Dispatcher
3. Core Engine
– Connection Manager (connection.py)
– Network I/O (net.py)
– SSH Relay (relay.py)
– Local Server (servers.py, for callback/event forwarding)
4. Protocol & Auth Modules
– Dahua Protocol API (dahua.py)
– Logon Modes (dahua_logon_modes.py)
5. Feature Modules
– Dump Service (methods in dahua.py)
– Event Subscription (events.py) + Event Viewer (eventviewer.py)
6. Utilities
– Password Manager (pwdmanager.py)
– Shared Helpers (utils.py)
– Config Persistence (dhConsole.json)
7. External Entities
– Target Dahua Device
– SSH Server (if using relay)

b. Relationships & interactions
- User invokes: Console.py → parses CLI args → passes to Core Engine
- Core Engine picks or opens connection via Connection Manager
– If relay URL set → instantiate SSH tunnel (relay.py) → forward net I/O
– Else → direct TCP/SSL socket (net.py)
- Engine selects logon mode implementation → authenticates via dahua_logon_modes.py
- Once authenticated → use dahua.py methods to run dumps or subscribe events
- Event subscription uses events.py → optionally spins up a local server (servers.py) → feeds into eventviewer.py
- All data (JSON dumps, logs, hexdumps) pass through utils.py for formatting, file I/O

c. Architectural patterns & design principles
– Modular separation by responsibility: CLI vs. Connection vs. Protocol vs. Features
– Strategy pattern for selectable “logon modes”
– Adapter-style network abstraction (net.py + relay.py)
– Single source of truth for configuration (JSON file)
– Dependency inversion: high-level engine depends on interface modules (not concrete sockets)

d. Key technologies, frameworks, libraries
– Python 3.x standard library (argparse, socket, ssl, subprocess)
– Paramiko or similar SSH client for relay.py (in requirements.txt)
– JSON for config and dumps
– Optional colorized CLI output (if any library in requirements)

## Diagram (Mermaid)

```mermaid
flowchart TD
    User["User"]:::external
    CLI["Console.py\n(Arg Parser & Dispatcher)"]:::frontend
    subgraph "Core Engine"
        CM["Connection Manager\n(connection.py)"]:::core
        NI["Network I/O\n(net.py)"]:::network
        SR["SSH Relay\n(relay.py)"]:::network
        LS["Local Server\n(servers.py)"]:::network
    end
    subgraph "Protocol & Auth"
        API["Dahua Protocol API\n(dahua.py)"]:::protocol
        LM["Logon Modes\n(dahua_logon_modes.py)"]:::protocol
    end
    subgraph "Feature Modules"
        ES["Event Subscription\n(events.py)"]:::core
        EV["Event Viewer\n(eventviewer.py)"]:::frontend
    end
    subgraph "Utilities & Config"
        PM["Password Manager\n(pwdmanager.py)"]:::util
        UT["Shared Utilities\n(utils.py)"]:::util
        CFG["Console.json"]:::util
    end
    subgraph "External Entities"
        Device["Target Dahua Device"]:::external
        SSH["SSH Server"]:::external
    end

    User -->|"invoke CLI"| CLI
    CLI -->|"initialize Engine"| CM
    CM -->|"direct TCP/SSL"| NI
    CM -->|"if relay"| SR
    SR -->|"forwards to"| NI
    NI -->|"socket I/O"| Device
    CM -->|"select logon mode"| LM
    LM -->|"authenticate"| API
    API -->|"commands, dumps"| Device
    API -->|"dump data"| CLI
    API -->|"subscribe events"| ES
    ES -->|"spins up"| LS
    LS -->|"callback/events"| EV
    PM -.-> CLI
    PM -.-> LM
    PM -.-> API
    UT -.-> CLI
    UT -.-> CM
    UT -.-> API
    UT -.-> ES
    CFG -.-> UT

    click CLI "https://github.com/mcw0/dahuaconsole/blob/master/Console.py"
    click CM "https://github.com/mcw0/dahuaconsole/blob/master/connection.py"
    click NI "https://github.com/mcw0/dahuaconsole/blob/master/net.py"
    click SR "https://github.com/mcw0/dahuaconsole/blob/master/relay.py"
    click LS "https://github.com/mcw0/dahuaconsole/blob/master/servers.py"
    click API "https://github.com/mcw0/dahuaconsole/blob/master/dahua.py"
    click LM "https://github.com/mcw0/dahuaconsole/blob/master/dahua_logon_modes.py"
    click ES "https://github.com/mcw0/dahuaconsole/blob/master/events.py"
    click EV "https://github.com/mcw0/dahuaconsole/blob/master/eventviewer.py"
    click PM "https://github.com/mcw0/dahuaconsole/blob/master/pwdmanager.py"
    click UT "https://github.com/mcw0/dahuaconsole/blob/master/utils.py"
    click CFG "https://github.com/mcw0/dahuaconsole/blob/master/Console.json"

    classDef frontend fill:#ADD8E6,stroke:#000000
    classDef core fill:#90EE90,stroke:#000000
    classDef network fill:#FFA500,stroke:#000000
    classDef protocol fill:#FFFF99,stroke:#000000
    classDef util fill:#D3D3D3,stroke:#000000
    classDef external fill:#FFFFFF,stroke:#000000,stroke-dasharray: 5 5
```
