# udayk01/securedns architecture

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

- Interactive diagram: https://gitdiagram.com/udayk01/securedns
- Repository: https://github.com/udayk01/securedns

## Overview

##### What this project is (type + purpose)
**SecureDNS** is a **single-process Python DNS server** that:
- Listens for DNS queries (typically UDP/53),
- Checks queried domains against a **remote blocklist** (downloaded via `requests`),
- Responds with a **blocked IP** for blocked domains,
- Otherwise **forwards** the query to an upstream resolver and returns the response,
- Uses **caching** and some **concurrency** to improve throughput/latency.

##### How to read the file structure (and what it implies architecturally)
**File tree:**
- `main.py` → all runtime logic is here (DNS server, parsing, cache, forwarding, blocklist loading).
- `README.md` → documents configuration constants (HOST/PORT/BLOCKED_IP), remote blocklist, performance goals.

##### DNS Clients
Devices/apps sending DNS queries (OS resolver, browsers, routers, etc.)

##### Network / OS
UDP socket on port 53 (and possibly TCP/53 if implemented—verify in `main.py`).

Inside a big “SecureDNS (Python process)” container, split into:

##### DNS Listener / Server Loop
- Binds to `HOST:PORT`
- Receives packets
- Dispatches to worker/concurrency mechanism

##### Concurrency / Request Dispatcher
- Thread pool / asyncio tasks / per-request thread (depends on `main.py`)

##### DNS Message Parser/Serializer
- Parses DNS query to extract QNAME, QTYPE, etc.
- Constructs DNS responses (either blocked response or upstream response)

##### Domain Normalizer
- Canonicalization: lowercase, trailing dot handling, subdomain logic
- Important because blocklists and DNS queries vary in representation

##### Blocklist Manager
- **Remote fetcher** using `requests`
- Parser for hosts-format list
- In-memory set/trie/hash structure of blocked domains
- Update strategy: on startup, periodic refresh, manual refresh (depends on code)

##### Decision Engine (Allow vs Block)
- If domain matches blocklist => synthesize “blocked” DNS answer (A/AAAA?) pointing to `BLOCKED_IP` or 0.0.0.0
- If allowed => forward upstream

##### Cache
- Positive cache: domain -> response (respect TTL if implemented)
- Possibly negative cache
- Eviction policy (LRU/size/time) if present
- Cache lookup should appear *before* upstream forwarding in the flow

##### Upstream Resolver / Forwarder
- Sends query to external DNS (e.g., 8.8.8.8 or system resolver)
- Receives response and returns it to client
- This can be a UDP client socket separate from listener socket

##### Observability (Logging/Metrics)
- Even if it’s just logging, include it as a sidecar component: request logs, blocked counts, cache hit rate, errors fetching blocklist.

##### Remote Blocklist Source
- Steven Black Unified Hosts file URL (external HTTP(S) endpoint)

##### Upstream DNS Resolver
- Public DNS / enterprise resolver / ISP resolver

##### Worker → Cache lookup
- **Cache hit**: return cached response → Serializer → Client
##### If cache miss: Worker → Blocklist check
- **Blocked**: synthesize response with `BLOCKED_IP` → Serializer → Client
- **Allowed**: forward to Upstream Resolver → receive response → store in Cache → Serializer → Client

##### Flow 2: Blocklist acquisition (control path)
SecureDNS Blocklist Manager → (HTTP GET via `requests`) → Remote Blocklist Source
Then parse → update in-memory blocked-domain structure.

SecureDNS components → Logs/Metrics (stdout/file/metrics endpoint—whatever exists)

Include:
- DNS Clients
- SecureDNS (Python process)
- Remote Blocklist Source (HTTP)
- Upstream DNS Resolver (DNS)

Client → Listener → Worker → Parser → Cache → Blocklist → Forwarder → Upstream → back → Cache store → Client
An alternate branch for “Blocked domain”.

##### Notes to ensure you don’t miss important details (given the minimal repo)
- UDP only or UDP+TCP
- Upstream resolver address selection
- Cache: TTL-aware or naive; max size; eviction
- Blocklist refresh cadence (startup-only vs periodic timer/thread)
- Exact “blocked response” behavior (A only? NXDOMAIN? 0.0.0.0? configurable)
- Concurrency mechanism (threading vs asyncio vs multiprocessing)

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =========================================================
  %% Diagram A — C4 Container View (System-level)
  %% =========================================================
  subgraph A["Diagram A — Container View"]
    direction TB

    A_CLIENTS["DNS Clients<br/>(OS resolver, browsers, routers)"]:::external
    A_OS["Network / OS<br/>(UDP/53 socket)"]:::external
    A_SECURE["SecureDNS<br/>(Python 3 process, main.py)<br/>Monolithic DNS server"]:::service
    A_BLOCKSRC["Remote blocklist source<br/>(StevenBlack hosts file)"]:::external
    A_UPSTREAM["Upstream DNS resolver<br/>(e.g., 8.8.8.8 / ISP DNS)"]:::external

    A_CLIENTS -->|"DNS over UDP/53"| A_OS
    A_OS -->|"deliver datagrams"| A_SECURE
    A_SECURE -->|"DNS forward UDP/53"| A_UPSTREAM
    A_SECURE -.->|"HTTP(S) GET blocklist (requests)"| A_BLOCKSRC
  end

  %% =========================================================
  %% Diagram B — Component View (Inside SecureDNS / main.py)
  %% =========================================================
  subgraph B["Diagram B — Component View (Inside SecureDNS)"]
    direction TB

    subgraph B_PROC["SecureDNS (single Python process)"]
      direction TB

      B_LISTENER["DNS Listener / Server Loop<br/>(bind HOST:PORT, recv packets)"]:::component
      B_DISPATCH["Concurrency / Request Dispatcher<br/>(threads/async/tasks; verify in main.py)"]:::component

      subgraph B_PIPE["Hot path: DNS query processing"]
        direction TB
        B_PARSER["DNS Message Parser<br/>(extract QNAME/QTYPE)"]:::component
        B_NORM["Domain Normalizer<br/>(lowercase, trailing dot, subdomain rules)"]:::component

        B_CACHE[("Cache<br/>(in-memory; TTL/eviction: verify)")]:::datastore

        B_DECIDE["Decision Engine<br/>(Cache hit? Blocked?)"]:::component
        B_BLOCKSET[("Blocked-domain set<br/>(in-memory structure)")]:::datastore

        B_FORWARD["Upstream Resolver / Forwarder<br/>(UDP client socket)"]:::component
        B_SERIAL["DNS Serializer / Response Writer<br/>(send response to client)"]:::component
      end

      subgraph B_CTRL["Control path: blocklist acquisition"]
        direction TB
        B_BLMGR["Blocklist Manager<br/>(fetch via requests, parse hosts list,<br/>update blocked set; fail-safe: verify)"]:::component
      end

      B_OBS["Observability<br/>(logging/metrics; if present)"]:::observability
    end

    %% External entities for component view context
    B_CLIENTS["DNS Clients"]:::external
    B_BLOCKSRC["Remote blocklist source<br/>(StevenBlack hosts file)"]:::external
    B_UPSTREAM["Upstream DNS resolver"]:::external

    %% Hot path connections
    B_CLIENTS -->|"DNS over UDP/53"| B_LISTENER
    B_LISTENER -->|"dispatch request"| B_DISPATCH
    B_DISPATCH -->|"handle query"| B_PARSER
    B_PARSER -->|"QNAME/QTYPE"| B_NORM
    B_NORM -->|"lookup"| B_CACHE
    B_CACHE -->|"cache hit/miss"| B_DECIDE

    %% Decision branches (vertical, explicit)
    B_DECIDE -->|"cache hit"| B_SERIAL
    B_DECIDE -->|"cache miss"| B_BLOCKSET
    B_BLOCKSET -->|"match?"| B_DECIDE
    B_DECIDE -->|"blocked: synthesize answer (BLOCKED_IP)"| B_SERIAL
    B_DECIDE -->|"allowed: forward"| B_FORWARD
    B_FORWARD -->|"DNS forward UDP/53"| B_UPSTREAM
    B_UPSTREAM -->|"DNS response"| B_FORWARD
    B_FORWARD -->|"store response"| B_CACHE
    B_FORWARD -->|"return response"| B_SERIAL
    B_SERIAL -->|"DNS response UDP/53"| B_CLIENTS

    %% Control path connections (dashed)
    B_BLMGR -.->|"HTTP(S) GET (requests)"| B_BLOCKSRC
    B_BLMGR -->|"update"| B_BLOCKSET

    %% Observability side arrows
    B_LISTENER -->|"logs/errors"| B_OBS
    B_DECIDE -->|"blocked counts/cache hits"| B_OBS
    B_FORWARD -->|"upstream latency/errors"| B_OBS
    B_BLMGR -->|"fetch status/parse errors"| B_OBS
  end

  %% =========================================================
  %% Diagram C — Sequence Diagram (One DNS query)
  %% =========================================================
  subgraph C["Diagram C — Sequence (Single query)"]
    direction TB

    C_CLIENT["DNS Client"]:::external
    C_LISTENER["Listener"]:::component
    C_WORKER["Worker/Dispatcher"]:::component
    C_PARSER["Parser/Normalizer"]:::component
    C_CACHE["Cache"]:::datastore
    C_BLOCK["Blocklist set"]:::datastore
    C_FORWARD["Forwarder"]:::component
    C_UPSTREAM["Upstream DNS"]:::external
    C_SERIAL["Serializer/Responder"]:::component

    C_CLIENT -->|"DNS query (UDP/53)"| C_LISTENER
    C_LISTENER -->|"dispatch"| C_WORKER
    C_WORKER -->|"parse & normalize"| C_PARSER
    C_PARSER -->|"cache lookup"| C_CACHE
    C_CACHE -->|"hit?"| C_WORKER

    C_WORKER -->|"if miss: check blocklist"| C_BLOCK
    C_BLOCK -->|"blocked?"| C_WORKER

    C_WORKER -->|"alt blocked: build BLOCKED_IP response"| C_SERIAL
    C_SERIAL -->|"DNS response"| C_CLIENT

    C_WORKER -->|"alt allowed: forward query"| C_FORWARD
    C_FORWARD -->|"DNS forward UDP/53"| C_UPSTREAM
    C_UPSTREAM -->|"DNS response"| C_FORWARD
    C_FORWARD -->|"cache store"| C_CACHE
    C_FORWARD -->|"respond"| C_SERIAL
    C_SERIAL -->|"DNS response"| C_CLIENT
  end

  %% =========================================================
  %% Click events (from component_mapping)
  %% Paths must not appear in node labels.
  %% =========================================================
  click A_SECURE "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_PROC "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_LISTENER "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_DISPATCH "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_PARSER "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_SERIAL "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_NORM "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_BLMGR "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_DECIDE "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_CACHE "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_BLOCKSET "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_FORWARD "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_OBS "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_PIPE "https://github.com/udayk01/securedns/blob/main/main.py"
  click B_CTRL "https://github.com/udayk01/securedns/blob/main/main.py"
  click A_SECURE "https://github.com/udayk01/securedns/blob/main/main.py"
  click A_OS "https://github.com/udayk01/securedns/blob/main/main.py"
  click A_CLIENTS "https://github.com/udayk01/securedns/blob/main/README.md"
  click A_BLOCKSRC "https://github.com/udayk01/securedns/blob/main/README.md"
  click A_UPSTREAM "https://github.com/udayk01/securedns/blob/main/README.md"

  %% Documentation node (mapped)
  D_README["README / Usage & Configuration<br/>(HOST/PORT/BLOCKED_IP, blocklist URL)"]:::doc
  click D_README "https://github.com/udayk01/securedns/blob/main/README.md"

  %% =========================================================
  %% Styles (color is mandatory)
  %% =========================================================
  classDef external fill:#E5E7EB,stroke:#6B7280,color:#111827,stroke-width:1px
  classDef service fill:#2563EB,stroke:#1E3A8A,color:#FFFFFF,stroke-width:1.5px
  classDef component fill:#60A5FA,stroke:#1D4ED8,color:#0B1220,stroke-width:1px
  classDef datastore fill:#FBBF24,stroke:#B45309,color:#111827,stroke-width:1px
  classDef observability fill:#A78BFA,stroke:#5B21B6,color:#111827,stroke-width:1px
  classDef doc fill:#34D399,stroke:#065F46,color:#06281B,stroke-width:1px
```
