a6b35268ab
sing-box 该构建未编入 v2ray_api、clash /connections 也不暴露用户,故无法做 准确 per-user 计费。改取节点累计总量(clash downloadTotal/uploadTotal)做 窗口增量,按当前 dp_uuid 均摊上报(单用户=准确,多用户近似,已注释标注)。 - render.go:节点 sing-box 配置加 experimental.clash_api(loopback+secret)。 - ClashUsageSource:轮询 /connections 总量,delta + 重启回绕保护;clash 的 up/down 是代理视角,对调为用户视角(下载→bytes_down)。 - SingBox.DpUUIDs() 供归属;Agent.UseClashUsage() 在 cmd/agent 接上。 已节点 live 验证:usage_daily 实时入库,下载增量正确落 bytes_down。 局限:多 dp_uuid 时均摊(准确计费需重编 sing-box 带 with_v2ray_api)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61 lines
2.6 KiB
Go
61 lines
2.6 KiB
Go
// Command agent is the Pangolin node agent. It runs on every acceleration node,
|
|
// self-enrolls over mTLS, dials the control plane, applies the streamed command
|
|
// feed by managing the local sing-box user table, and reports heartbeat/usage.
|
|
//
|
|
// Configuration comes from flags or PANGOLIN_AGENT_* environment variables
|
|
// (cloud-init injects the latter). The node holds zero user identities on disk.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/agentd"
|
|
)
|
|
|
|
func main() {
|
|
cfg := agentd.Config{}
|
|
var singboxUnit string
|
|
|
|
flag.StringVar(&cfg.ControlPlaneAddr, "control-plane", env("PANGOLIN_AGENT_CONTROL_PLANE", ""), "control plane gRPC address host:port")
|
|
flag.StringVar(&cfg.ServerName, "server-name", env("PANGOLIN_AGENT_SERVER_NAME", ""), "TLS SNI to validate (defaults to control-plane host)")
|
|
flag.StringVar(&cfg.BootstrapToken, "bootstrap-token", env("PANGOLIN_AGENT_BOOTSTRAP_TOKEN", ""), "one-time enrollment token")
|
|
flag.StringVar(&cfg.StateDir, "state-dir", env("PANGOLIN_AGENT_STATE_DIR", agentd.DefaultStateDir), "directory for node key/cert/ca/state")
|
|
flag.StringVar(&cfg.SingboxConfigPath, "singbox-config", env("PANGOLIN_AGENT_SINGBOX_CONFIG", agentd.DefaultSingboxCfg), "path to rendered sing-box config")
|
|
flag.StringVar(&cfg.DeriveKey, "derive-key", env("PANGOLIN_AGENT_DERIVE_KEY", ""), "Hy2 password derivation key")
|
|
flag.StringVar(&cfg.AgentVersion, "agent-version", env("PANGOLIN_AGENT_VERSION", "dev"), "reported agent version")
|
|
flag.StringVar(&singboxUnit, "singbox-unit", env("PANGOLIN_AGENT_SINGBOX_UNIT", "sing-box"), "systemd unit to restart on config change")
|
|
flag.BoolVar(&cfg.Insecure, "insecure", env("PANGOLIN_AGENT_INSECURE", "") == "1", "dial without mTLS (dev only)")
|
|
flag.Parse()
|
|
|
|
if cfg.ControlPlaneAddr == "" {
|
|
log.Fatal("agent: --control-plane (or PANGOLIN_AGENT_CONTROL_PLANE) is required")
|
|
}
|
|
|
|
agent := agentd.New(cfg, agentd.WithRestarter(agentd.SystemdRestarter{Unit: singboxUnit}))
|
|
// 真实流量采集:读本地 clash_api 节点总量,按 dp_uuid 归属上报。
|
|
agent.UseClashUsage()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
log.Printf("[pangolin-agent] starting (control-plane=%s, version=%s)", cfg.ControlPlaneAddr, cfg.AgentVersion)
|
|
start := time.Now()
|
|
if err := agent.Run(ctx); err != nil {
|
|
log.Fatalf("[pangolin-agent] exited after %s: %v", time.Since(start), err)
|
|
}
|
|
log.Printf("[pangolin-agent] shut down cleanly after %s", time.Since(start))
|
|
}
|
|
|
|
func env(key, def string) string {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
return v
|
|
}
|
|
return def
|
|
}
|