# ericpino/goguard architecture

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

- Interactive diagram: https://gitdiagram.com/ericpino/goguard
- Repository: https://github.com/ericpino/goguard

## Overview

#### Purpose and execution model
goGuard is a Go daemon for detecting malicious HTTP access-log requests and blocking source IPs after repeated threat matches. The sole application entry point is `main.go`; it runs as a native Linux binary or a containerized process. Runtime configuration is environment-driven via `.env.example`.

#### Startup and configuration boundary
At startup, the daemon loads log-tail location, blocking mode, hit/window thresholds, whitelist ranges, and credentials for Cloudflare and Telegram. These settings define the operational boundary between local host enforcement and optional external integrations.
- `main.go`
- `.env.example`

#### Real-time log ingestion
The primary input is a web-server access log, such as Nginx’s access log. The daemon continuously tails this file, parses incoming lines sufficiently to identify the request source IP and requested URL/payload, then passes each event into threat evaluation.
- `main.go`

#### Threat detection and qualification
Requests are evaluated against regex-style signatures for SQLi, XSS, traversal/reconnaissance, RCE, SSTI/JNDI, and common admin probes. A match is not necessarily a ban: suspicious activity is attributed to its source IP and counted within a sliding time window, reducing single-request false positives.
- `main.go`

#### IP state, whitelist, and ban decision
The daemon maintains in-memory operational state for processed lines, detected threats, per-IP hit histories, and blocked IPs used by `--status`. Whitelisted literal IPs and CIDR ranges—including optionally Cloudflare address space—bypass ban handling. Once a non-whitelisted IP reaches `MAX_HITS` during `TIME_WINDOW_SECONDS`, it crosses the enforcement boundary.
- `main.go`
- `.env.example`

#### Enforcement and notifications
Blocking can target local `iptables`, Cloudflare’s Firewall Access Rules API, or both, according to `BLOCK_MODE`. Local enforcement requires Linux and root/sudo-level access to alter firewall rules. Cloudflare enforcement requires zone-scoped API credentials and protects at the edge. Ban events can also be sent through the Telegram Bot API.
- `main.go`
- `.env.example`

#### Deployment/runtime infrastructure
The repository supports direct compilation using the Go module definition and container deployment through Docker Compose. Container deployment must preserve access to the tailed log and, for local firewall mode, sufficient host networking/capabilities to execute `iptables`.
- `go.mod`
- `Dockerfile`
- `docker-compose.yml`

Main entry points: [`main.go`](https://github.com/ericpino/goguard/blob/main/main.go) (Threshold decision), [`docker-compose.yml`](https://github.com/ericpino/goguard/blob/main/docker-compose.yml) (Docker Compose runtime), [`Dockerfile`](https://github.com/ericpino/goguard/blob/main/Dockerfile) (Container image), [`.env.example`](https://github.com/ericpino/goguard/blob/main/.env.example) (Environment config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime Inputs"]
  node_config["Environment config<br/>runtime configuration<br/>[.env.example]"]
  node_access_log["Web access log<br/>tailed input"]
end

subgraph group_daemon["goGuard Daemon"]
  node_entrypoint["Daemon entry point<br/>Go process<br/>[main.go]"]
  node_log_tailer["Log tailer and parser<br/>ingestion<br/>[main.go]"]
  node_signature_matcher["Threat signatures<br/>detection<br/>[main.go]"]
  node_ip_state[("IP hit state<br/>in-memory state<br/>[main.go]")]
  node_whitelist["Whitelist filter<br/>policy<br/>[main.go]"]
  node_ban_decision{{"Threshold decision<br/>policy<br/>[main.go]"}}
end

subgraph group_enforcement["Enforcement"]
  node_iptables["Local iptables<br/>host firewall"]
  node_cloudflare["Cloudflare Firewall API<br/>edge integration"]
  node_telegram["Telegram Bot API<br/>notification integration"]
end

subgraph group_deployment["Deployment"]
  node_go_module["Go module<br/>build definition<br/>[go.mod]"]
  node_dockerfile["Container image<br/>Docker build"]
  node_compose["Docker Compose runtime<br/>container deployment<br/>[docker-compose.yml]"]
end

node_config -->|"loads settings"| node_entrypoint
node_access_log -->|"new lines"| node_log_tailer
node_entrypoint -->|"starts"| node_log_tailer
node_log_tailer -->|"request event"| node_signature_matcher
node_signature_matcher -->|"matched threat"| node_ip_state
node_ip_state -->|"source IP"| node_whitelist
node_whitelist -->|"non-whitelisted IP"| node_ban_decision
node_ban_decision -->|"records blocked IP"| node_ip_state
node_ban_decision -->|"local or both mode"| node_iptables
node_ban_decision -->|"Cloudflare or both mode"| node_cloudflare
node_ban_decision -->|"ban alert"| node_telegram
node_go_module -->|"builds"| node_entrypoint
node_dockerfile -->|"packages"| node_entrypoint
node_compose -->|"runs image"| node_dockerfile
node_compose -->|"mounts log"| node_access_log
node_compose -.->|"host capabilities"| node_iptables

click node_config "https://github.com/ericpino/goguard/blob/main/.env.example"
click node_entrypoint "https://github.com/ericpino/goguard/blob/main/main.go"
click node_log_tailer "https://github.com/ericpino/goguard/blob/main/main.go"
click node_signature_matcher "https://github.com/ericpino/goguard/blob/main/main.go"
click node_ip_state "https://github.com/ericpino/goguard/blob/main/main.go"
click node_whitelist "https://github.com/ericpino/goguard/blob/main/main.go"
click node_ban_decision "https://github.com/ericpino/goguard/blob/main/main.go"
click node_go_module "https://github.com/ericpino/goguard/blob/main/go.mod"
click node_dockerfile "https://github.com/ericpino/goguard/blob/main/Dockerfile"
click node_compose "https://github.com/ericpino/goguard/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_config,node_access_log toneBlue
class node_entrypoint,node_log_tailer,node_signature_matcher,node_ip_state,node_whitelist,node_ban_decision toneAmber
class node_iptables,node_cloudflare,node_telegram toneMint
class node_go_module,node_dockerfile,node_compose toneRose
```

## Components

### Runtime Inputs

- [Environment config](https://github.com/ericpino/goguard/blob/main/.env.example): runtime configuration · Thresholds, whitelist, blocking mode, and integration credentials. · `.env.example`
- **Web access log**: tailed input

### goGuard Daemon

- [Daemon entry point](https://github.com/ericpino/goguard/blob/main/main.go): Go process · `main.go`
- [Log tailer and parser](https://github.com/ericpino/goguard/blob/main/main.go): ingestion · Extracts source IP and request payload from new log lines. · `main.go`
- [Threat signatures](https://github.com/ericpino/goguard/blob/main/main.go): detection · Matches SQLi, XSS, traversal, RCE, SSTI/JNDI, and admin probes. · `main.go`
- [IP hit state](https://github.com/ericpino/goguard/blob/main/main.go): in-memory state · Tracks hit histories, detected threats, processed lines, and blocked IPs. · `main.go`
- [Whitelist filter](https://github.com/ericpino/goguard/blob/main/main.go): policy · Literal IPs, CIDRs, and optional Cloudflare ranges bypass bans. · `main.go`
- [Threshold decision](https://github.com/ericpino/goguard/blob/main/main.go): policy · Bans non-whitelisted IPs reaching MAX_HITS within TIME_WINDOW_SECONDS. · `main.go`

### Enforcement

- **Local iptables**: host firewall · Requires Linux privileges for local blocking.
- **Cloudflare Firewall API**: edge integration · Creates zone-scoped Firewall Access Rules.
- **Telegram Bot API**: notification integration

### Deployment

- [Go module](https://github.com/ericpino/goguard/blob/main/go.mod): build definition · `go.mod`
- [Container image](https://github.com/ericpino/goguard/blob/main/Dockerfile): Docker build · `Dockerfile`
- [Docker Compose runtime](https://github.com/ericpino/goguard/blob/main/docker-compose.yml): container deployment · Mounts the tailed log and supplies host privileges when local blocking is enabled. · `docker-compose.yml`

## Connections

- Environment config → Daemon entry point: loads settings
- Web access log → Log tailer and parser: new lines
- Daemon entry point → Log tailer and parser: starts
- Log tailer and parser → Threat signatures: request event
- Threat signatures → IP hit state: matched threat
- IP hit state → Whitelist filter: source IP
- Whitelist filter → Threshold decision: non-whitelisted IP
- Threshold decision → IP hit state: records blocked IP
- Threshold decision → Local iptables: local or both mode
- Threshold decision → Cloudflare Firewall API: Cloudflare or both mode
- Threshold decision → Telegram Bot API: ban alert
- Go module → Daemon entry point: builds
- Container image → Daemon entry point: packages
- Docker Compose runtime → Container image: runs image
- Docker Compose runtime → Web access log: mounts log
- Docker Compose runtime → Local iptables: host capabilities
