mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-06 07:36:37 +08:00
docs: sync and translate english documentation
This commit is contained in:
+127
-16
@@ -1,33 +1,144 @@
|
||||
# Architecture
|
||||
# System Architecture
|
||||
|
||||
OpenFlare consists of Server, Agent, and local OpenResty on each node.
|
||||
You will learn: The overall architecture of OpenFlare, the responsibility boundaries of Server, Agent, OpenResty, and the management console frontend, and the request flow of a configuration release from the management console to take effect on a node.
|
||||
|
||||
OpenFlare consists of the Server, the Agent, local OpenResty on each node, and the management console frontend. The Server is the control plane, the Agent is the only controlled landing entry point on the node side, and OpenResty is the actual data plane.
|
||||
|
||||
```text
|
||||
OpenFlare Server (Gin + SQLite/PostgreSQL + Web UI)
|
||||
|
|
||||
| HTTP API / Config Pull
|
||||
v
|
||||
OpenFlare Agent (register / heartbeat / sync / apply / update)
|
||||
|
|
||||
v
|
||||
Browser
|
||||
|
|
||||
| Management UI / API
|
||||
v
|
||||
OpenFlare Server (Gin + GORM + SQLite/PostgreSQL)
|
||||
|
|
||||
| Agent API / heartbeat / config pull
|
||||
v
|
||||
OpenFlare Agent
|
||||
|
|
||||
| write config / openresty -t / reload / rollback
|
||||
v
|
||||
OpenResty binary
|
||||
|
|
||||
v
|
||||
|
|
||||
| reverse proxy
|
||||
v
|
||||
Origin
|
||||
```
|
||||
|
||||
## Component Responsibilities
|
||||
|
||||
| Component | Responsibility |
|
||||
| --- | --- |
|
||||
| Server | Management UI, admin APIs, Agent APIs, configuration rendering, version publishing, data storage, and aggregate queries |
|
||||
| Agent | Registration, heartbeats, synchronization, writing files, configuration validation, reloads, fallback/rollbacks, self-updating, and lightweight data collection |
|
||||
| OpenResty | Receives real traffic, executes WAF, PoW, authentication, and reverse proxying according to configurations rendered by OpenFlare |
|
||||
| Frontend | Manages website configurations, WAF, origins, certificates, nodes, versions, users, settings, and observability pages |
|
||||
|
||||
## Server
|
||||
|
||||
`openflare_server` is a monolithic control plane based on Gin, GORM, SQLite/PostgreSQL, the existing login/session system, and the static frontend build.
|
||||
`openflare_server` is a monolithic control plane:
|
||||
|
||||
It owns the admin UI and API, Agent API, configuration rendering, version publishing, storage, and aggregate queries.
|
||||
* Gin provides HTTP services.
|
||||
* GORM accesses SQLite or PostgreSQL.
|
||||
* The existing login system provides management console Sessions.
|
||||
* Authentication source and external account binding support GitHub OAuth and standard OIDC.
|
||||
* The Go Server hosts the static build output of `openflare_server/web`.
|
||||
|
||||
The Server does not directly SSH into nodes, nor does it modify node files online. It only saves the control plane state, generates complete configuration versions, and lets nodes actively pull them via the Agent API.
|
||||
|
||||
## Agent
|
||||
|
||||
`openflare_agent` is a single Go binary that runs on each node. It controls OpenResty through `openresty_path`, or `openresty` by default. Docker deployments use an Agent image that already includes OpenResty and follows the same binary-control flow.
|
||||
`openflare_agent` is a Go monolithic application:
|
||||
|
||||
It handles registration, heartbeat, sync, file writes, `openresty -t`, reload, rollback, self-update, and lightweight collection.
|
||||
* Runs on nodes as a single binary.
|
||||
* Reads or generates local node information upon startup.
|
||||
* Performs periodic heartbeats to report status and fetch the active version summary.
|
||||
* Pulls configurations, backs up old files, writes new files, validates, and reloads upon discovering a new version.
|
||||
* Attempts to restore execution and roll back when the application fails.
|
||||
* Maintains the WAF GeoIP mmdb; writes the built-in initial database on startup and updates it regularly based on configuration.
|
||||
|
||||
The Agent uniformly executes validations, reloads, starts, and restarts via the OpenResty binary pointed to by `openresty_path`; it falls back to calling `openresty` by default when not configured. In Docker deployments, the Agent image includes the OpenResty binary and follows the same binary control logic.
|
||||
|
||||
## Frontend
|
||||
|
||||
`openflare_server/web` is the production frontend baseline: Next.js App Router, React 19, TypeScript, and Tailwind CSS.
|
||||
`openflare_server/web` is the official management console frontend:
|
||||
|
||||
* Next.js App Router.
|
||||
* React 19.
|
||||
* TypeScript.
|
||||
* Tailwind CSS.
|
||||
* TanStack Query manages server state.
|
||||
|
||||
The frontend is hosted by the Go Server after static export. All API requests must go through `lib/api/` uniformly and handle the `success/message/data` response structure.
|
||||
|
||||
## Data and Request Flow
|
||||
|
||||
### Management Console Request Flow
|
||||
|
||||
```text
|
||||
Browser -> Frontend -> /api/* -> controller -> service -> model -> database
|
||||
```
|
||||
|
||||
Mutation APIs on the management console use `POST`, while read-only APIs use `GET`. Both success and failure return a clear `message`.
|
||||
|
||||
### Agent Sync Flow
|
||||
|
||||
```text
|
||||
Agent heartbeat -> Server returns active version summary
|
||||
Agent discovers new version -> Pulls configuration details
|
||||
Agent writes main configuration / route configuration / certificates / Lua resources / WAF runtime configuration
|
||||
Agent executes OpenResty validation and reload
|
||||
Agent reports application result
|
||||
```
|
||||
|
||||
When WebSocket (WS) connection upgrade is enabled by default, the Agent first obtains settings through the HTTP heartbeat, and then attempts to connect to the Agent WebSocket. Once the WS connection is successful, periodic status reporting is carried by WS; when the Server publishes or activates a version, it broadcasts the active version summary to connected Agents, allowing them to enter the synchronization flow immediately. When the WS connection is disconnected or fails to establish, the Agent automatically falls back to the HTTP heartbeat.
|
||||
|
||||
### Reverse Proxy Flow
|
||||
|
||||
```text
|
||||
Client -> OpenResty server block -> WAF Lua -> named upstream -> Origin
|
||||
```
|
||||
|
||||
Website configuration is the aggregation boundary of reverse proxies. A website configuration can bind multiple domains and share site-level traffic limits, reverse proxies, and caching configurations.
|
||||
|
||||
WAF is executed in the OpenResty `access_by_lua_file` phase. Rules come from `waf_config.json` carried in the current active version; the global rule group takes effect by default, and websites can overlay custom rule groups.
|
||||
|
||||
## Core Objects
|
||||
|
||||
Currently active entities include:
|
||||
|
||||
* `proxy_routes`
|
||||
* `origins`
|
||||
* `config_versions`
|
||||
* `nodes`
|
||||
* `auth_sources`
|
||||
* `external_accounts`
|
||||
* `node_system_profiles`
|
||||
* `apply_logs`
|
||||
* `tls_certificates`
|
||||
* `managed_domains`
|
||||
* `node_request_reports`
|
||||
* `node_access_logs`
|
||||
* `node_metric_snapshots`
|
||||
* `traffic_analytics_rollups`
|
||||
* `node_health_events`
|
||||
* `waf_rule_groups`
|
||||
* `waf_rule_group_bindings`
|
||||
|
||||
## Key Design Decisions
|
||||
|
||||
| Decision | Reason |
|
||||
| --- | --- |
|
||||
| Complete configuration versions, instead of online patches | Gives previews, activations, history, and rollbacks stable boundaries |
|
||||
| Active pull by Agents | Server does not need SSH permissions, nor does it expose remote command execution entry points |
|
||||
| Global single active version | Reduces MVP complexity and ensures all nodes are consistent by default |
|
||||
| Website configurations aggregate multiple domains | Supports sharing site-level policies for a business site while allowing certificate binding per domain |
|
||||
| Server-side aggregation of observability data | Avoids inconsistent results caused by temporary frontend calculations |
|
||||
|
||||
## Contributor Reading Suggestions
|
||||
|
||||
If you want to modify architecture-related code, read these first:
|
||||
|
||||
1. [Product Boundary](./index.md)
|
||||
2. [Release Model](./release-model.md)
|
||||
3. [Development Constraints](./development.md)
|
||||
4. [Repository Structure](../reference/repository.md)
|
||||
|
||||
@@ -148,6 +148,8 @@ Currently active entities:
|
||||
* `traffic_analytics_rollups`
|
||||
* `node_health_events`
|
||||
* `options`
|
||||
* `waf_rule_groups`
|
||||
* `waf_rule_group_bindings`
|
||||
|
||||
General constraints:
|
||||
|
||||
@@ -161,6 +163,7 @@ General constraints:
|
||||
* Upstreams uniformly use named `upstream` + keepalive; for a single upstream carrying a base path or query, the original URI should be added back to `proxy_pass`. For multiple upstreams, only pure `scheme://host[:port]` is allowed.
|
||||
* Rate limits, reverse proxy, and cache configurations currently belong to the site-level `proxy_routes`.
|
||||
* HTTPS certificate binding must be saved on a per-domain basis through `domain_cert_ids` parallel to `domains`; domains not bound to a certificate must not participate in HTTPS rendering.
|
||||
* WAF global rule groups are applied to all websites by default, while custom rule groups are bound to site configurations via `waf_rule_group_bindings`; they must be included in the complete configuration version snapshot during publishing.
|
||||
* `config_versions` must save complete snapshots and rendering results.
|
||||
* There can only be one activated version globally at a time.
|
||||
* Rollback is achieved by reactivating older versions.
|
||||
@@ -232,14 +235,18 @@ Version constraints:
|
||||
The Agent must satisfy:
|
||||
|
||||
* Read or generate local `node_id` after startup.
|
||||
* Periodic heartbeat and synchronization.
|
||||
* Periodic heartbeats and synchronization.
|
||||
* Conventional synchronization prioritizes judging based on the version summary returned by the heartbeat.
|
||||
* When WS connection upgrade is enabled and the connection is successful, the Agent can receive active version summaries via WS and immediately synchronize; WS failure or disconnection must fall back to HTTP heartbeats.
|
||||
* Back up old files first when discovering a new version.
|
||||
* Write main configurations, route configurations, and necessary certificate files.
|
||||
* Write WAF/PoW runtime configurations, and ensure WAF Lua resources are managed uniformly by the Agent.
|
||||
* Execute `openresty -t -c <main_config_path>` after writing the new configuration, and then reload; direct startup of OpenResty is allowed when reload finds that it is not running.
|
||||
* Periodic runtime health checks must not call `openresty -t`, preventing health probes from triggering synchronous upstream domain name resolutions; they should prioritize requesting `/openflare/stub_status` on the local `openresty_observability_port`, using HTTP `200 OK` as the basis for judging that the OpenResty main process and workers are serving.
|
||||
* If the activation of the new configuration fails, the Agent must first try to restore execution with the target configuration, then roll back to the old configuration and pull up OpenResty again.
|
||||
* Report warning when OpenResty recovers normally after rollback; report failure when it still cannot recover after rollback.
|
||||
* Once a target `version + checksum` fails to apply and rolls back, the Agent must block repeated applications of this target in its local state.
|
||||
* Report warning when OpenResty recovers normally after rollback; if there is no historical main configuration to restore locally, it must be allowed to write the built-in safe fallback configuration and pull up an OpenResty runtime state that only listens to port `80` externally and uniformly returns `503 Service Unavailable` and `OpenFlare: No Valid Configuration`, while retaining the local `stub_status` health check entry. The fallback runtime state must not clear the blocked status of the failed target; the application logs must reflect that the target version failed but the fallback runtime has started. Report failure when there is a historical main configuration but it still cannot recover after rollback.
|
||||
* Once a target `version + checksum` application fails and rolls back, the Agent must block repeated applications of this target in its local state.
|
||||
* When the Agent maintains the local MaxMind mmdb, download or refresh failures can only record warnings, and must not block heartbeats, synchronization, configuration application, or OpenResty health checks.
|
||||
|
||||
## Frontend Requests, State, and Types
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ OpenFlare is currently not positioned as a general-purpose log platform, service
|
||||
| Agent Synchronization | Supports registration, heartbeat, synchronization, application result reporting, and self-updating |
|
||||
| OpenResty Hosting | Manages main configuration templates, performance parameters, cache parameters, and Lua resources |
|
||||
| HTTPS/TLS | Hosts certificates and domain assets, and binds certificates on a per-domain basis |
|
||||
| WAF | Maintains IP/IP ranges black/whitelists and country-level geographical black/whitelists with global and website-customized rule groups |
|
||||
| Basic Observability | Aggregates node requests, resource snapshots, health events, and access analytics |
|
||||
| Node Management | Node status, token systems, deployment, and update links |
|
||||
| Console Frontend | Next.js-based official management console |
|
||||
@@ -76,6 +77,8 @@ Currently active entities:
|
||||
* `node_metric_snapshots`
|
||||
* `traffic_analytics_rollups`
|
||||
* `node_health_events`
|
||||
* `waf_rule_groups`
|
||||
* `waf_rule_group_bindings`
|
||||
|
||||
## Site Configuration Constraints
|
||||
|
||||
@@ -116,6 +119,24 @@ During publishing rendering:
|
||||
* Domains not bound to a certificate must not be automatically brought into HTTPS.
|
||||
* All domains in `proxy_routes.domains` must be included in the same site configuration to avoid the same site being split in version snapshots.
|
||||
|
||||
## WAF Constraints
|
||||
|
||||
WAF uses rule groups as configuration boundaries. The system fixes a global rule group, which is applied to all websites by default; websites can overlay multiple custom rule groups.
|
||||
|
||||
Phase 1 supports:
|
||||
|
||||
* IP / IP range whitelists and blacklists.
|
||||
* Country-level region whitelists and blacklists.
|
||||
* Rule group-level blocking status codes and response pages, defaulting to `418` and an empty page.
|
||||
|
||||
Evaluation order:
|
||||
|
||||
* Whitelists are bypass exceptions; if any enabled rule group matches a whitelist, the request is allowed.
|
||||
* If no whitelist is matched, blacklists continue to be evaluated.
|
||||
* When multiple blacklists match, the global rule group takes precedence, followed by custom rule groups in ascending order of their IDs.
|
||||
|
||||
Region recognition is based on the MaxMind mmdb maintained locally on the node by the Agent, and the OpenResty Lua reads the local database during the request path. When GeoIP dependencies are unavailable, region rules must be skipped, without affecting IP rules and the reverse proxy main link.
|
||||
|
||||
## Authentication Source Constraints
|
||||
|
||||
`auth_sources` is the configuration object for third-party login entries on the management console, currently supporting only two types: `github` and `oidc`. Enabled authentication sources will be displayed on the login page.
|
||||
|
||||
@@ -15,13 +15,14 @@ Modify rules -> Preview / View diff -> Publish -> Generate complete configuratio
|
||||
When publishing, the Server must:
|
||||
|
||||
1. Read all enabled `proxy_routes`.
|
||||
2. Read the OpenResty main configuration template, performance parameters, cache parameters, and necessary Lua resources on the Server side.
|
||||
2. Read the Server side OpenResty main configuration template, performance parameters, cache parameters, and necessary Lua resources.
|
||||
3. Read domain and certificate binding relationships.
|
||||
4. Render the complete OpenResty configuration.
|
||||
5. Calculate the `checksum`.
|
||||
6. Write to `config_versions`.
|
||||
7. Switch the activated version.
|
||||
8. Let the Agent discover and apply it in subsequent heartbeats.
|
||||
4. Read the WAF global rule group, custom rule groups, and website binding relationships.
|
||||
5. Render the complete OpenResty configuration and WAF runtime configuration.
|
||||
6. Calculate the `checksum`.
|
||||
7. Write to `config_versions`.
|
||||
8. Switch the activated version.
|
||||
9. Let the Agent discover and apply it in subsequent heartbeats.
|
||||
|
||||
The version number format is fixed as `YYYYMMDD-NNN`.
|
||||
|
||||
@@ -33,9 +34,9 @@ Publishing generates a new complete configuration version. The version must cont
|
||||
|
||||
## Activating Version
|
||||
|
||||
There can only be one activated version globally at a time.Differentiated versions grouped by nodes are currently not supported.
|
||||
There can only be one activated version globally at a time. Differentiated versions grouped by nodes are currently not supported.
|
||||
|
||||
The Agent obtains the activated version summary through the heartbeat; only when the remote version or checksum is inconsistent with the local state does the Agent enter the synchronization flow.
|
||||
The Agent obtains the activated version summary through the heartbeat; only when the remote version or checksum is inconsistent with the local state does the Agent enter the synchronization flow. When Agent WS connection upgrade is enabled and the connection is available, the Server will broadcast the latest active version summary after successfully publishing or activating a version. Upon receiving it, the Agent immediately pulls and applies the configuration using the ordinary synchronization flow. When WS is unavailable, changes are still discovered at HTTP heartbeat intervals.
|
||||
|
||||
## Immutable History
|
||||
|
||||
@@ -53,12 +54,12 @@ When discovering a new version, the Agent will:
|
||||
|
||||
1. Pull the details of the target version.
|
||||
2. Back up old files.
|
||||
3. Write the main configuration, route configurations, certificates, and necessary Lua resources.
|
||||
3. Write the main configuration, route configurations, certificates, necessary Lua resources, and WAF/PoW runtime configurations.
|
||||
4. Execute OpenResty configuration verification.
|
||||
5. reload; if it is not started during runtime, try to start OpenResty with the current configuration.
|
||||
6. Report success, warning, or failure.
|
||||
|
||||
If the activation of the new configuration fails, the Agent must try to restore execution; report a warning when the rollback succeeds, and report a failure when it still cannot recover after rollback.
|
||||
If the activation of the new configuration fails, the Agent must try to restore execution; report a warning when the rollback succeeds. If there is no historical main configuration to roll back to locally, the Agent will write the built-in safe fallback configuration and try to pull up OpenResty: this configuration only listens to port `80` externally, contains no user routes, uniformly returns `503 Service Unavailable` and `OpenFlare: No Valid Configuration`, and retains the local `stub_status` health check entry. If fallback startup is successful, it still blocks the failed target version and reports a warning; report a failure when there is a historical main configuration but it still cannot recover after rollback.
|
||||
|
||||
Once a target `version + checksum` application fails and rolls back, the Agent will block repeated applications of this target in its local state. Only when the remote activated version or checksum changes is it allowed to try again.
|
||||
|
||||
@@ -69,3 +70,4 @@ Once a target `version + checksum` application fails and rolls back, the Agent w
|
||||
* The Agent API is fixed to use the node-exclusive `agent_token`; the first access can use the `discovery_token`.
|
||||
* The Server does not provide remote shell or arbitrary command execution entries.
|
||||
* The configuration version must save complete snapshots, rendering results, and `checksum`.
|
||||
* WAF rule groups and website binding relationships must enter the snapshot and checksum along with the complete configuration version, and must not rely on the current mutable WAF configuration when rolling back.
|
||||
|
||||
Reference in New Issue
Block a user