Skip to content

feat(meshd): relay 模式(去 TUN + tls_mode none + 降权部署) - #3

Merged
kvmaker merged 12 commits into
masterfrom
feat/meshd-relay-mode
Jul 21, 2026
Merged

feat(meshd): relay 模式(去 TUN + tls_mode none + 降权部署)#3
kvmaker merged 12 commits into
masterfrom
feat/meshd-relay-mode

Conversation

@kvmaker

@kvmaker kvmaker commented Jul 20, 2026

Copy link
Copy Markdown
Owner

概述

为 meshd 新增 relay 模式,适配单机多应用部署(如 gz-ubuntu 上 meshd + aigw 共存):meshd 退化为纯中继,不创建 TUN、不分配 server IP,去掉 CAP_NET_ADMIN 依赖,可藏到 Caddy 反代之后。

改动

新配置

  • mode: full | relay(默认 full,向后兼容)— relay 不创建 TUN、不分配 server IP
  • tls_mode: autocert | none(默认 autocert)— none 时纯 HTTP,不启动 autocert、不监听 :80

代码

  • internal/server/config/:Mode / TLSMode 字段 + 归一化校验(空值/合法/非法回退 + warning)
  • internal/server/tunnel/server.go:NewTunnelServer 按 mode 分支;提取 routeClientPacket(对称于 routePacket);Start/Close nil 守卫
  • internal/server/api/server.go:提取 serveMode,ListenAndServeTLS 三分支(autocert / selfsigned / plain)

部署

  • install.sh --mode relay:联动生成 tls_mode: none + listen_addr: 127.0.0.1:8443 + systemd unit CapabilityBoundingSet=!CAP_NET_ADMIN CAP_NET_RAW 显式 deny(即便 root 启动也不具备)
  • docs/deploy/caddy-multi-app.md:Caddy 多应用部署指南(meshd + aigw 共存)

测试

全程 TDD,go test ./... 全绿:

  • config:mode / tls_mode 默认值、合法值、非法回退
  • tunnel:relay 不创建 TUN、routeClientPacket relay 丢弃 server-bound 包/正常转发、Close nil 安全
  • api:serveMode 四种决策(含 TLSTestMode 优先于 tls_mode)

兼容性

  • 存量 yaml 无 mode / tls_mode 字段 → 默认 full + autocert,行为零变化
  • 客户端协议、device.Allocatemeshd init 不变

relay 模式限制(已知)

  • 客户端无法访问 server 本机服务(10.100.0.1 不可达)
  • server 无法主动连客户端

设计文档

  • spec:docs/superpowers/specs/2026-07-20-meshd-relay-mode-design.md
  • plan:docs/superpowers/plans/2026-07-20-meshd-relay-mode.md

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added relay mode to run as a lightweight relay without creating a VPN interface.
    • Added configurable TLS handling via tls_mode (autocert or none), including plain HTTP operation for reverse-proxy setups.
    • Updated the installer to support --mode relay, configure local-only listening in relay mode, and adjust service permissions accordingly.
  • Documentation

    • Added deployment docs for running relay alongside other web apps behind Caddy, including limitations and migration steps.
  • Tests

    • Added coverage for mode/tls_mode parsing and for relay-mode packet routing and server shutdown behavior.

maxyu and others added 11 commits July 20, 2026 17:01
引入 mode: full | relay 配置,relay 模式下 meshd 不创建 TUN、
不分配 server IP,退化为纯中继,去掉 CAP_NET_ADMIN 依赖,
适配降权/容器化/多应用共存场景。默认 full 向后兼容。

Co-Authored-By: Claude <noreply@anthropic.com>
按 TDD 拆为 4 个 task:config mode 字段、tunnel relay 分支与
routeClientPacket 提取、install.sh --mode 与 systemd 去特权、
Caddy 多应用部署文档。

Co-Authored-By: Claude <noreply@anthropic.com>
实施评审发现 relay 未解决 meshd 独占 443/80+自带 TLS(I-1),
且 install.sh 去 CAP 不彻底(M1)。扩展范围:新增 tls_mode
配置(none 时纯 HTTP)、install.sh relay 联动 tls_mode none
与 CapabilityBoundingSet 显式 deny、文档消除架构矛盾。
追加 Task 5/6/7。

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
…修正 Caddy 反代架构

Co-Authored-By: Claude <noreply@anthropic.com>
- install.sh: 新增 listen_addr_line 变量,relay 模式默认写入 127.0.0.1:8443,
  避免 meshd 抢占公网 :443 导致 WS 明文暴露并阻塞 Caddy
- docs/deploy/caddy-multi-app.md: 步骤 2 改为说明默认值已是本地端口
- internal/server/api/server.go: ListenAndServeTLS 文档首行覆盖三种模式

修复终审 I-F1 (Important) 与 M-F1 (Minor)。

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 68a9681a-6a1c-4dc4-9cb2-06a6b01d0704

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change adds full and relay server modes, autocert and none TLS modes, relay-aware tunnel routing, installer and systemd capability handling, API serve-mode dispatch, tests, and Caddy-based multi-application deployment documentation.

Changes

Relay mode

Layer / File(s) Summary
Configuration and TLS contracts
internal/server/config/config.go, internal/server/config/config_test.go, docs/superpowers/specs/..., docs/superpowers/plans/...
Adds mode and TLS configuration fields, defaults, normalization, validation, and coverage for valid and invalid values.
Relay tunnel lifecycle and packet routing
internal/server/tunnel/server.go, internal/server/tunnel/server_test.go, docs/superpowers/specs/..., docs/superpowers/plans/...
Relay mode skips TUN creation, guards lifecycle operations, forwards client-to-client packets, and counts server-bound route misses.
API serve-mode dispatch
internal/server/api/server.go, internal/server/api/api_test.go, docs/superpowers/plans/...
Selects self-signed TLS, plain HTTP, or autocert startup, with test TLS taking precedence.
Installer and systemd integration
install.sh, docs/superpowers/specs/..., docs/superpowers/plans/...
Adds --mode, writes relay-specific YAML settings, and adjusts generated systemd capability configuration.
Caddy multi-application deployment
docs/deploy/caddy-multi-app.md, docs/superpowers/plans/...
Documents Caddy TLS termination and reverse proxying to local meshd and aigw HTTP services, plus relay limitations and maintenance commands.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Caddy
  participant meshd
  participant RelayClient
  Client->>Caddy: HTTPS request
  Caddy->>meshd: HTTP reverse proxy
  meshd->>RelayClient: Relay WebSocket packet
  RelayClient-->>meshd: Client response packet
  meshd-->>Caddy: HTTP response
  Caddy-->>Client: HTTPS response
Loading

Possibly related PRs

  • kvmaker/mesh#2: Both changes cover MESH_TEST_TLS-driven self-signed TLS behavior and serve-mode selection.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title clearly summarizes the main change: meshd relay mode with no TUN, tls_mode none, and reduced-privilege deployment.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/meshd-relay-mode

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
install.sh (1)

149-160: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Set an explicit default to prevent empty lines in the generated YAML.

When mode is not relay, tls_mode_line is left empty, resulting in a blank line in the middle of the generated /etc/mesh/meshd.yaml. Setting it to explicitly use the default value (autocert) keeps the generated configuration clean and explicit.

♻️ Proposed refactor
-    local tls_mode_line=""
+    local tls_mode_line="tls_mode: \"autocert\""
     local listen_addr_line="listen_addr: \":443\""
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@install.sh` around lines 149 - 160, Update the tls_mode_line initialization
in the install configuration-generation flow to explicitly emit the default
autocert value, while retaining the relay-specific none override. Ensure the
generated /etc/mesh/meshd.yaml has a tls_mode entry rather than an empty line
for non-relay modes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@install.sh`:
- Around line 173-179: Correct the systemd capability-negation syntax in
install.sh lines 173-179 by changing the relay-mode CapabilityBoundingSet value
from !CAP_NET_ADMIN to ~CAP_NET_ADMIN while retaining CAP_NET_RAW. Update
docs/deploy/caddy-multi-app.md line 27 to use the same ~CAP_NET_ADMIN syntax.

---

Nitpick comments:
In `@install.sh`:
- Around line 149-160: Update the tls_mode_line initialization in the install
configuration-generation flow to explicitly emit the default autocert value,
while retaining the relay-specific none override. Ensure the generated
/etc/mesh/meshd.yaml has a tls_mode entry rather than an empty line for
non-relay modes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 375f1b58-8536-4a2b-94bb-e0fde0108293

📥 Commits

Reviewing files that changed from the base of the PR and between ff19252 and 9e6029f.

📒 Files selected for processing (10)
  • docs/deploy/caddy-multi-app.md
  • docs/superpowers/plans/2026-07-20-meshd-relay-mode.md
  • docs/superpowers/specs/2026-07-20-meshd-relay-mode-design.md
  • install.sh
  • internal/server/api/api_test.go
  • internal/server/api/server.go
  • internal/server/config/config.go
  • internal/server/config/config_test.go
  • internal/server/tunnel/server.go
  • internal/server/tunnel/server_test.go

Comment thread install.sh
@kvmaker

kvmaker commented Jul 21, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@kvmaker
kvmaker merged commit 24a6e86 into master Jul 21, 2026
2 checks passed
@kvmaker
kvmaker deleted the feat/meshd-relay-mode branch July 21, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant