Files
pangolin/server/internal/agentd/singbox.go
T
wangjia f03d2dc8a6 feat(agent): node agent — enroll/mTLS, heartbeat, command stream, sing-box 用户表 (tsk__R8M4jEw43JR)
与控制面同仓同 go.mod,新增节点 agent 实现:

- proto/agent/v1/agent.proto + internal/pb/agentv1:冻结的控制面↔agent gRPC 契约
  (Enroll/Register/Heartbeat/Subscribe/Ack/ReportUsage)。仓库尚无 protoc 流水线,
  暂以手写 Go 类型 + JSON gRPC codec 实现,与 proto 1:1 对应,待 protoc 接入即可替换。
- internal/agentd:
  - enroll.go:首启生成 EC 密钥+CSR,持 bootstrap token 调 Enroll 换 90d 节点证书
    (CN=node_uuid),落 /etc/pangolin-agent/,此后 mTLS。
  - conn.go(agent.go)+creds.go:mTLS 主动拨号 + 指数退避重连;重连携带 last_command_id;
    Register 取 ConfigSnapshot 全量配置覆盖本地。
  - heartbeat.go:30s 上报 peer/带宽/CPU + config_version;need_full_resync→全量同步。
  - command.go:消费 Subscribe,Upsert/Revoke/Rotate/ApplyConfig/Lifecycle 幂等处理后
    Ack(at-least-once,按 command_id 去重)。
  - singbox.go+render.go:内存用户表 + 落盘 state.json(仅 dp_uuid+expires_at);任何变更
    渲染完整 sing-box 配置(REALITY users[uuid,flow] + Hy2 users[派生口令])→ 500ms 去抖
    合并 → systemd 重启。
  - ttl.go:凭证 TTL 定时移除并上报。
  - usage.go:按 dp_uuid 聚合上报,绝无 user_id/email/目的地址。
  - derive.go:Hy2 口令 = HMAC-SHA256(key, dp_uuid),与控制面同源派生。
- cmd/agent:入口(flag/env 配置)。
- infra/cloud-init/{node.yaml.tmpl,install-node.sh,README.md}:一段式安装,下载锁定版本
  二进制并校验 SHA-256,systemd 拉管,首启即 Enroll/Register。shellcheck -S warning 通过。

测试(bufconn mock 控制面,无需 docker):Enroll→Register→Heartbeat 全流转;Upsert/Revoke
渲染正确;Rotate 宽限期新旧并存到点移除;TTL 自动移除并上报;断流重连 last_command_id
续发不丢不重;need_full_resync 触发重注册;state.json 恢复;去抖合并;扫描确认无身份字段。
go test -race ./internal/agentd/... ./internal/pb/... 通过;go vet ./... 通过。

落实 doc/04 §2 节点无状态化与 doc/06 §3 数据面红线(节点仅见 dp_uuid)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 12:26:58 +08:00

355 lines
9.6 KiB
Go

package agentd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"sync"
"time"
agentv1 "github.com/wangjia/pangolin/server/internal/pb/agentv1"
)
// Restarter applies a freshly-rendered sing-box config. sing-box has no hot
// reload, so the production implementation restarts the systemd unit (a ~1-2s
// data-plane blip). Tests inject a fake.
type Restarter interface {
Restart(ctx context.Context) error
}
// Cred is the agent's in-memory view of one data-plane credential. It is the
// authoritative local state together with the rendered sing-box config.
type Cred struct {
DpUUID string
Protocol agentv1.Protocol
Flow string
ExpiresAt int64 // unix seconds; 0 = no expiry
}
// persistedCred is the on-disk shape. Per the no-state invariant we persist ONLY
// dp_uuid + expires_at — protocol/flow are reconstructed (defaults) on load and
// then corrected by the next Register reconciliation.
type persistedCred struct {
DpUUID string `json:"dp_uuid"`
ExpiresAt int64 `json:"expires_at"`
}
type persistedState struct {
Creds []persistedCred `json:"creds"`
}
// SingBox owns the credential table, state.json persistence, sing-box config
// rendering and the debounced restart loop.
type SingBox struct {
cfg Config
restarter Restarter
clock func() time.Time
// OnExpire, if set, is invoked (outside the lock) when the TTL sweep removes
// a credential, so the agent can surface the removal upstream.
OnExpire func(dpUUID string)
mu sync.Mutex
creds map[string]*Cred
reality *agentv1.RealityInbound
hy2 *agentv1.Hy2Inbound
configVersion int64
// debounce machinery
dirty chan struct{}
done chan struct{}
closed bool
startWG sync.WaitGroup
}
// NewSingBox builds a manager. restarter may be nil (renders only, no restart).
func NewSingBox(cfg Config, restarter Restarter) *SingBox {
if restarter == nil {
restarter = noopRestarter{}
}
return &SingBox{
cfg: cfg.withDefaults(),
restarter: restarter,
clock: time.Now,
creds: make(map[string]*Cred),
dirty: make(chan struct{}, 1),
done: make(chan struct{}),
}
}
// LoadState restores the credential table from state.json after a restart. Only
// dp_uuid + expires_at survive; protocol defaults to BOTH and flow to the
// configured default until the next Register overwrites them. Expired entries are
// dropped on load.
func (s *SingBox) LoadState() error {
data, err := os.ReadFile(s.cfg.StatePath())
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return fmt.Errorf("agentd: read state: %w", err)
}
var ps persistedState
if err := json.Unmarshal(data, &ps); err != nil {
return fmt.Errorf("agentd: parse state: %w", err)
}
now := s.clock().Unix()
s.mu.Lock()
defer s.mu.Unlock()
for _, pc := range ps.Creds {
if pc.ExpiresAt != 0 && pc.ExpiresAt <= now {
continue
}
s.creds[pc.DpUUID] = &Cred{
DpUUID: pc.DpUUID,
Protocol: agentv1.ProtocolBoth,
Flow: DefaultFlow,
ExpiresAt: pc.ExpiresAt,
}
}
return nil
}
// saveStateLocked writes state.json atomically. Caller holds s.mu.
func (s *SingBox) saveStateLocked() error {
ps := persistedState{Creds: make([]persistedCred, 0, len(s.creds))}
for _, c := range s.creds {
ps.Creds = append(ps.Creds, persistedCred{DpUUID: c.DpUUID, ExpiresAt: c.ExpiresAt})
}
sort.Slice(ps.Creds, func(i, j int) bool { return ps.Creds[i].DpUUID < ps.Creds[j].DpUUID })
data, err := json.MarshalIndent(ps, "", " ")
if err != nil {
return err
}
return atomicWrite(s.cfg.StatePath(), data, 0o600)
}
// ─── mutations (all idempotent, all schedule a render) ───────────────────────────
// Upsert adds or replaces a credential.
func (s *SingBox) Upsert(c *Cred) {
s.mu.Lock()
cp := *c
if cp.Flow == "" {
cp.Flow = DefaultFlow
}
s.creds[cp.DpUUID] = &cp
_ = s.saveStateLocked()
s.mu.Unlock()
s.markDirty()
}
// Revoke removes a credential. No-op if absent (idempotent).
func (s *SingBox) Revoke(dpUUID string) {
s.mu.Lock()
_, existed := s.creds[dpUUID]
delete(s.creds, dpUUID)
if existed {
_ = s.saveStateLocked()
}
s.mu.Unlock()
if existed {
s.markDirty()
}
}
// Rotate installs new alongside old, keeping old alive until graceUntil. The TTL
// sweep removes old when its grace expires, giving "宽限期内新旧并存".
func (s *SingBox) Rotate(oldDpUUID string, newCred *Cred, graceUntil int64) {
s.mu.Lock()
if old, ok := s.creds[oldDpUUID]; ok && oldDpUUID != newCred.DpUUID {
// Clamp old credential's expiry to the grace deadline.
if graceUntil > 0 && (old.ExpiresAt == 0 || graceUntil < old.ExpiresAt) {
old.ExpiresAt = graceUntil
}
}
nc := *newCred
if nc.Flow == "" {
nc.Flow = DefaultFlow
}
s.creds[nc.DpUUID] = &nc
_ = s.saveStateLocked()
s.mu.Unlock()
s.markDirty()
}
// ApplyConfig replaces the node-wide inbound parameters and (if the snapshot
// carries credentials) the full credential table. Used by APPLY_CONFIG commands
// and full resync.
func (s *SingBox) ApplyConfig(snap *agentv1.ConfigSnapshot, replaceCreds bool) {
s.mu.Lock()
if snap.Reality != nil {
s.reality = snap.Reality
}
if snap.Hy2 != nil {
s.hy2 = snap.Hy2
}
if snap.ConfigVersion != 0 {
s.configVersion = snap.ConfigVersion
}
if replaceCreds {
s.creds = make(map[string]*Cred, len(snap.Credentials))
for _, c := range snap.Credentials {
flow := c.Flow
if flow == "" {
flow = DefaultFlow
}
s.creds[c.DpUUID] = &Cred{
DpUUID: c.DpUUID,
Protocol: c.Protocol,
Flow: flow,
ExpiresAt: c.ExpiresAtUnix,
}
}
}
_ = s.saveStateLocked()
s.mu.Unlock()
s.markDirty()
}
// SetConfigVersion records the authoritative config version (e.g. from Register).
func (s *SingBox) SetConfigVersion(v int64) {
s.mu.Lock()
s.configVersion = v
s.mu.Unlock()
}
// ─── read accessors ──────────────────────────────────────────────────────────
func (s *SingBox) ConfigVersion() int64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.configVersion
}
func (s *SingBox) OnlinePeers() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.creds)
}
// Has reports whether a dp_uuid is currently provisioned.
func (s *SingBox) Has(dpUUID string) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.creds[dpUUID]
return ok
}
// Snapshot returns a copy of the current credential set (sorted by dp_uuid).
func (s *SingBox) Snapshot() []Cred {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]Cred, 0, len(s.creds))
for _, c := range s.creds {
out = append(out, *c)
}
sort.Slice(out, func(i, j int) bool { return out[i].DpUUID < out[j].DpUUID })
return out
}
// ─── TTL sweep ───────────────────────────────────────────────────────────────
// sweepExpired removes every credential whose expires_at has passed. Returns the
// removed dp_uuids and whether anything changed.
func (s *SingBox) sweepExpired() []string {
now := s.clock().Unix()
s.mu.Lock()
var removed []string
for id, c := range s.creds {
if c.ExpiresAt != 0 && c.ExpiresAt <= now {
delete(s.creds, id)
removed = append(removed, id)
}
}
if len(removed) > 0 {
_ = s.saveStateLocked()
}
s.mu.Unlock()
if len(removed) > 0 {
s.markDirty()
if s.OnExpire != nil {
for _, id := range removed {
s.OnExpire(id)
}
}
}
sort.Strings(removed)
return removed
}
// ─── rendering ───────────────────────────────────────────────────────────────
// RenderConfig builds the sing-box server config JSON from the current state.
// Exported (and pure w.r.t. the snapshot) so tests can assert its contents.
func (s *SingBox) RenderConfig() ([]byte, error) {
s.mu.Lock()
creds := make([]Cred, 0, len(s.creds))
for _, c := range s.creds {
creds = append(creds, *c)
}
reality := s.reality
hy2 := s.hy2
s.mu.Unlock()
sort.Slice(creds, func(i, j int) bool { return creds[i].DpUUID < creds[j].DpUUID })
return renderSingboxConfig(creds, reality, hy2, s.cfg.DeriveKey)
}
// writeAndRestart renders, writes the config file and restarts sing-box.
func (s *SingBox) writeAndRestart(ctx context.Context) error {
data, err := s.RenderConfig()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(s.cfg.SingboxConfigPath), 0o755); err != nil {
return fmt.Errorf("agentd: mkdir singbox cfg: %w", err)
}
if err := atomicWrite(s.cfg.SingboxConfigPath, data, 0o644); err != nil {
return err
}
return s.restarter.Restart(ctx)
}
// markDirty signals the debounce loop that a render is pending (non-blocking).
func (s *SingBox) markDirty() {
select {
case s.dirty <- struct{}{}:
default:
}
}
// Run drives the debounce loop: it coalesces bursts of changes within
// DebounceWindow into a single write+restart. It blocks until ctx is cancelled.
func (s *SingBox) Run(ctx context.Context) {
timer := time.NewTimer(time.Hour)
timer.Stop()
pending := false
for {
select {
case <-ctx.Done():
timer.Stop()
return
case <-s.dirty:
if !pending {
pending = true
timer.Reset(s.cfg.DebounceWindow)
}
case <-timer.C:
pending = false
if err := s.writeAndRestart(ctx); err != nil {
logf("singbox render/restart failed: %v", err)
}
}
}
}
// noopRestarter renders without restarting (dev / config-check only).
type noopRestarter struct{}
func (noopRestarter) Restart(context.Context) error { return nil }