# hnimminh/libresbc architecture

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

- Interactive diagram: https://gitdiagram.com/hnimminh/libresbc
- Repository: https://github.com/hnimminh/libresbc

## Overview

1. **What this repo is**
   LibreSBC is a Session Border Controller built around a split between a **real-time SIP/media engine** and a **management/control layer**. The repo contains the runtime logic, deployment automation, UI, and config generation needed to run the SBC on a host or in containers.

2. **Core data plane: FreeSWITCH**
   The SBC’s call processing and media handling are centered on **FreeSWITCH**. The `liberator/fscfg` tree shows generated FreeSWITCH configuration, and `prebuild/Dockerfile.freeswitch` indicates a packaged FreeSWITCH image. This is the boundary where SIP signaling and RTP/SRTP media are actually terminated and relayed.

3. **Call-routing logic inside FreeSWITCH: `callng/`**
   The `callng` directory is a Lua-based call-control layer that appears to run inside the FreeSWITCH runtime. Its files are organized by event:
   - startup and initialization
   - call initiation
   - capacity checks
   - unauthorized-call handling
   - CDR emission
   - redirection and utilities
   This is the main policy/routing engine for live call decisions, sitting directly on the call path.

4. **Control plane / management service: `liberator/`**
   `liberator` is the administrative backend. It includes API modules (`api.py`, `libreapi.py`, `cfgapi.py`), configuration management, CDR handling, and utility code. It likely generates or updates FreeSWITCH and related system configuration, exposes management endpoints, and coordinates runtime behavior.

5. **Configuration generation boundary**
   The `liberator/fscfg/xml/*.j2.xml` templates show that FreeSWITCH XML config is rendered from templates rather than hand-edited. That makes configuration a first-class output of the control plane. Related templates also exist for nftables, systemd units, and env files, so the backend owns the authoritative runtime config.

6. **UI layer: `webui/`**
   The `webui` directory is a separate Go module that serves the operator-facing UI. It is not the SBC itself; it is the presentation layer for administration and monitoring. The presence of static assets and a standalone `main.go` suggests it serves compiled/static content and likely talks to the Liberator API.

7. **Deployment automation: Ansible**
   The `ansible/` tree is the host provisioning and deployment system. It installs and configures:
   - Docker
   - nftables firewalling
   - platform/kernel/sysctl tuning
   - SSH and sudo hardening
   - the LibreSBC container stack
   - the LibreUI service
   - a “shield” service
   This is the infrastructure layer that turns a machine into a SBC node.

8. **Containerized runtime**
   `ansible/roles/libresbc-container` plus `docker-compose.yml.j2`, `libre.env.j2`, and `redis.conf` indicate the primary runtime can be deployed as a composed container stack. That stack likely includes FreeSWITCH, Liberator, and supporting services such as Redis for shared state/caching in the SBC runtime.

9. **Network and security boundary**
   `ansible/roles/nftables` and `liberator/nft/nftables.j2.conf` show firewall rules are part of the design, not an afterthought. The repo also includes a `shield` service and platform sysctl tuning, which means traffic filtering, attack mitigation, and kernel/network tuning are treated as core SBC concerns.

10. **Operational hooks and external integrations**
   The project explicitly supports HTTP-based integration for routing and CDR export. The `examples/` directory includes sample HTTP route and CDR servers, showing external systems can influence routing decisions and receive call records. This is an important architecture boundary: LibreSBC can delegate policy or observability to third-party services over HTTP.

11. **Runtime packaging and service management**
   Besides containers, the repo also contains systemd units and logrotate/rsyslog configs under `liberator/system`, plus a systemd service for `liberator`. That suggests the project supports both containerized and host-managed service modes, with logging and startup handled at the OS service layer.

12. **How the main flow fits together**
   Traffic enters the SBC at the network edge, passes through firewall rules, reaches FreeSWITCH for SIP/media handling, and is governed by Lua call logic in `callng`. The Liberator control plane supplies config, manages policies and APIs, and emits/collects operational data such as CDRs. The web UI sits on top for operators, while Ansible and Docker handle repeatable deployment of the whole stack.

Main entry points: [`liberator/main.py`](https://github.com/hnimminh/libresbc/blob/master/liberator/main.py) (Liberator API), [`callng/main.lua`](https://github.com/hnimminh/libresbc/blob/master/callng/main.lua) (Call logic), [`ansible/playbooks/deployment.yml`](https://github.com/hnimminh/libresbc/blob/master/ansible/playbooks/deployment.yml) (Host deploy), [`ansible/roles/libresbc-container/tasks/main.yml`](https://github.com/hnimminh/libresbc/blob/master/ansible/roles/libresbc-container/tasks/main.yml) (Container stack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime"]
  node_n1["FreeSWITCH<br/>media engine<br/>[freeswitch.xml]"]
  node_n2["Call logic<br/>lua policy<br/>[main.lua]"]
  node_n3["Startup<br/>event hook<br/>[event.startup.lua]"]
  node_n4["Initiation<br/>event hook"]
  node_n5["Capacity<br/>event hook<br/>[event.capacity.lua]"]
  node_n6["Unauthorized<br/>event hook<br/>[event.unauth.lua]"]
  node_n7["CDR event<br/>event hook<br/>[event.cdr.lua]"]
  node_n8["Redirection<br/>routing helper<br/>[redirection.lua]"]
  node_n9["Utilities<br/>shared helpers<br/>[utilities.lua]"]
end

subgraph group_grp_control["Control plane"]
  node_n10["Liberator API<br/>backend service<br/>[main.py]"]
  node_n11["API modules<br/>python api<br/>[api.py]"]
  node_n12["Config API<br/>python api<br/>[cfgapi.py]"]
  node_n13["CDR service<br/>records<br/>[cdr.py]"]
  node_n14["Config core<br/>config engine<br/>[configuration.py]"]
  node_n15["FreeSWITCH cfg<br/>template set<br/>[directory.j2.xml]"]
  node_n16["Firewall cfg<br/>nft template<br/>[nftables.j2.conf]"]
end

subgraph group_grp_ops["Operations"]
  node_n17["Host deploy<br/>ansible playbook<br/>[deployment.yml]"]
  node_n18["Container stack<br/>ansible role<br/>[main.yml]"]
  node_n19["Host hardening<br/>ansible role<br/>[main.yml]"]
  node_n20["Nftables role<br/>ansible role<br/>[main.yml]"]
end

subgraph group_grp_ui["UI"]
  node_n21["LibreUI<br/>web ui<br/>[main.go]"]
end

subgraph group_grp_ext["External integrations"]
  node_n22["HTTP examples<br/>sample services"]
end

node_n2 -->|"loads"| node_n3
node_n2 -->|"routes"| node_n4
node_n2 -->|"checks"| node_n5
node_n2 -->|"blocks"| node_n6
node_n2 -->|"emits"| node_n7
node_n2 -->|"uses"| node_n8
node_n2 -->|"shares"| node_n9
node_n10 -->|"exposes"| node_n11
node_n10 -->|"manages"| node_n12
node_n10 -->|"handles"| node_n13
node_n10 -->|"loads"| node_n14
node_n10 -->|"renders"| node_n15
node_n10 -->|"renders"| node_n16
node_n15 -->|"configures"| node_n1
node_n16 -->|"applies"| node_n20
node_n17 -->|"deploys"| node_n18
node_n17 -->|"configures"| node_n19
node_n17 -->|"configures"| node_n20
node_n17 -->|"installs"| node_n21
node_n18 -->|"runs"| node_n1
node_n18 -->|"hosts"| node_n10
node_n18 -.->|"pairs with"| node_n21
node_n21 -->|"calls"| node_n10
node_n22 -.->|"integrates"| node_n10
node_n22 -.->|"receives"| node_n13

click node_n1 "https://github.com/hnimminh/libresbc/blob/master/liberator/fscfg/xml/freeswitch.xml"
click node_n2 "https://github.com/hnimminh/libresbc/blob/master/callng/main.lua"
click node_n3 "https://github.com/hnimminh/libresbc/blob/master/callng/event.startup.lua"
click node_n4 "https://github.com/hnimminh/libresbc/blob/master/callng/event.initiation.lua"
click node_n5 "https://github.com/hnimminh/libresbc/blob/master/callng/event.capacity.lua"
click node_n6 "https://github.com/hnimminh/libresbc/blob/master/callng/event.unauth.lua"
click node_n7 "https://github.com/hnimminh/libresbc/blob/master/callng/event.cdr.lua"
click node_n8 "https://github.com/hnimminh/libresbc/blob/master/callng/redirection.lua"
click node_n9 "https://github.com/hnimminh/libresbc/blob/master/callng/utilities.lua"
click node_n10 "https://github.com/hnimminh/libresbc/blob/master/liberator/main.py"
click node_n11 "https://github.com/hnimminh/libresbc/blob/master/liberator/api.py"
click node_n12 "https://github.com/hnimminh/libresbc/blob/master/liberator/cfgapi.py"
click node_n13 "https://github.com/hnimminh/libresbc/blob/master/liberator/cdr.py"
click node_n14 "https://github.com/hnimminh/libresbc/blob/master/liberator/configuration.py"
click node_n15 "https://github.com/hnimminh/libresbc/blob/master/liberator/fscfg/xml/directory.j2.xml"
click node_n16 "https://github.com/hnimminh/libresbc/blob/master/liberator/nft/nftables.j2.conf"
click node_n17 "https://github.com/hnimminh/libresbc/blob/master/ansible/playbooks/deployment.yml"
click node_n18 "https://github.com/hnimminh/libresbc/blob/master/ansible/roles/libresbc-container/tasks/main.yml"
click node_n19 "https://github.com/hnimminh/libresbc/blob/master/ansible/roles/platform/tasks/main.yml"
click node_n20 "https://github.com/hnimminh/libresbc/blob/master/ansible/roles/nftables/tasks/main.yml"
click node_n21 "https://github.com/hnimminh/libresbc/blob/master/webui/main.go"
click node_n22 "https://github.com/hnimminh/libresbc/blob/master/examples/httproute-server.py"

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_n1,node_n2,node_n3,node_n4,node_n5,node_n6,node_n7,node_n8,node_n9 toneBlue
class node_n10,node_n11,node_n12,node_n13,node_n14,node_n15,node_n16 toneAmber
class node_n17,node_n18,node_n19,node_n20 toneMint
class node_n21 toneRose
class node_n22 toneIndigo
```

## Components

### Runtime

Live call handling and SIP/media termination

- [FreeSWITCH](https://github.com/hnimminh/libresbc/blob/master/liberator/fscfg/xml/freeswitch.xml): media engine · Core SIP signaling and media termination runtime · `liberator/fscfg/xml/freeswitch.xml`
- [Call logic](https://github.com/hnimminh/libresbc/blob/master/callng/main.lua): lua policy · Event-driven call control executed inside FreeSWITCH · `callng/main.lua`
- [Startup](https://github.com/hnimminh/libresbc/blob/master/callng/event.startup.lua): event hook · Bootstraps callng runtime and initialization flow · `callng/event.startup.lua`
- [Initiation](https://github.com/hnimminh/libresbc/blob/master/callng/event.initiation.lua): event hook · Handles new call setup and routing decisions · `callng/event.initiation.lua`
- [Capacity](https://github.com/hnimminh/libresbc/blob/master/callng/event.capacity.lua): event hook · Checks capacity and admission constraints · `callng/event.capacity.lua`
- [Unauthorized](https://github.com/hnimminh/libresbc/blob/master/callng/event.unauth.lua): event hook · Rejects or handles unauthorized call attempts · `callng/event.unauth.lua`
- [CDR event](https://github.com/hnimminh/libresbc/blob/master/callng/event.cdr.lua): event hook · Emits call detail records from the call path · `callng/event.cdr.lua`
- [Redirection](https://github.com/hnimminh/libresbc/blob/master/callng/redirection.lua): routing helper · Redirect and manipulation helpers for call flow · `callng/redirection.lua`
- [Utilities](https://github.com/hnimminh/libresbc/blob/master/callng/utilities.lua): shared helpers · Common Lua helpers used by callng modules · `callng/utilities.lua`

### Control plane

Management APIs, config generation, and CDR handling

- [Liberator API](https://github.com/hnimminh/libresbc/blob/master/liberator/main.py): backend service · Administrative backend exposing control and config APIs · `liberator/main.py`
- [API modules](https://github.com/hnimminh/libresbc/blob/master/liberator/api.py): python api · Primary API surface for management operations · `liberator/api.py`
- [Config API](https://github.com/hnimminh/libresbc/blob/master/liberator/cfgapi.py): python api · Configuration management and generation endpoints · `liberator/cfgapi.py`
- [CDR service](https://github.com/hnimminh/libresbc/blob/master/liberator/cdr.py): records · Call detail record processing and export support · `liberator/cdr.py`
- [Config core](https://github.com/hnimminh/libresbc/blob/master/liberator/configuration.py): config engine · Shared configuration and runtime settings model · `liberator/configuration.py`
- [FreeSWITCH cfg](https://github.com/hnimminh/libresbc/blob/master/liberator/fscfg/xml/directory.j2.xml): template set · Generated FreeSWITCH XML configuration templates · `liberator/fscfg/xml/directory.j2.xml`
- [Firewall cfg](https://github.com/hnimminh/libresbc/blob/master/liberator/nft/nftables.j2.conf): nft template · Generated nftables policy for network boundary control · `liberator/nft/nftables.j2.conf`

### Operations

Deployment, provisioning, and host/runtime management

- [Host deploy](https://github.com/hnimminh/libresbc/blob/master/ansible/playbooks/deployment.yml): ansible playbook · Provisioning and deployment entrypoint · `ansible/playbooks/deployment.yml`
- [Container stack](https://github.com/hnimminh/libresbc/blob/master/ansible/roles/libresbc-container/tasks/main.yml): ansible role · Builds and runs the LibreSBC containerized runtime · `ansible/roles/libresbc-container/tasks/main.yml`
- [Host hardening](https://github.com/hnimminh/libresbc/blob/master/ansible/roles/platform/tasks/main.yml): ansible role · System tuning, SSH, sudo, and platform configuration · `ansible/roles/platform/tasks/main.yml`
- [Nftables role](https://github.com/hnimminh/libresbc/blob/master/ansible/roles/nftables/tasks/main.yml): ansible role · Installs and configures firewall service and rules · `ansible/roles/nftables/tasks/main.yml`

### UI

Operator-facing web interface

- [LibreUI](https://github.com/hnimminh/libresbc/blob/master/webui/main.go): web ui · Standalone operator interface for administration and monitoring · `webui/main.go`

### External integrations

Example HTTP services and exported operational hooks

- [HTTP examples](https://github.com/hnimminh/libresbc/blob/master/examples/httproute-server.py): sample services · Example route and CDR HTTP endpoints for integration · `examples/httproute-server.py`

## Connections

- Call logic → Startup: loads · callng initializes through its startup handler
- Call logic → Initiation: routes · call initiation drives per-call policy
- Call logic → Capacity: checks · capacity enforcement during live call handling
- Call logic → Unauthorized: blocks · unauthorized calls are rejected by policy
- Call logic → CDR event: emits · CDR generation is part of the call path
- Call logic → Redirection: uses · routing helpers support call manipulation
- Call logic → Utilities: shares · common utilities support the event modules
- Liberator API → API modules: exposes · main backend service exposes API modules
- Liberator API → Config API: manages · configuration endpoints are part of the backend
- Liberator API → CDR service: handles · backend processes and exports call records
- Liberator API → Config core: loads · shared configuration model feeds backend behavior
- Liberator API → FreeSWITCH cfg: renders · backend generates FreeSWITCH XML config
- Liberator API → Firewall cfg: renders · backend generates firewall policy templates
- FreeSWITCH cfg → FreeSWITCH: configures · generated XML config feeds the FreeSWITCH runtime
- Firewall cfg → Nftables role: applies · nftables configuration is deployed through the firewall role
- Host deploy → Container stack: deploys · playbook provisions the containerized SBC stack
- Host deploy → Host hardening: configures · playbook applies platform hardening and tuning
- Host deploy → Nftables role: configures · playbook installs and enables firewalling
- Host deploy → LibreUI: installs · playbook can deploy the LibreUI service
- Container stack → FreeSWITCH: runs · container stack includes the media engine runtime
- Container stack → Liberator API: hosts · container stack includes the Liberator control plane
- Container stack → LibreUI: pairs with · UI is deployed alongside the SBC stack
- LibreUI → Liberator API: calls · UI uses backend APIs for administration
- HTTP examples → Liberator API: integrates · example HTTP services show external routing/CDR hooks
- HTTP examples → CDR service: receives · example CDR server consumes exported call records
