init xboard node

This commit is contained in:
xboard
2026-03-15 09:27:24 +08:00
commit 37e6c016cb
40 changed files with 11011 additions and 0 deletions
+461
View File
@@ -0,0 +1,461 @@
package cert
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log/slog"
"math/big"
"net"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"github.com/caddyserver/certmagic"
"github.com/libdns/alidns"
"github.com/libdns/cloudflare"
"github.com/cedar2025/xboard-node/internal/config"
)
// Manager handles TLS certificate lifecycle.
//
// Supported modes (CertConfig.CertMode):
// - "http" — ACME HTTP-01 challenge via certmagic (needs port 80).
// - "dns" — ACME DNS-01 challenge via certmagic + libdns provider.
// - "self" — Self-signed certificate. Automatically generated and stored in CertDir.
// Recommended for nodes behind a CDN or reverse proxy.
// - "file" — User-provided certificate and key file paths (CertFile/KeyFile).
// - "content" — Certificate and key PEM content pushed from the panel.
// Written to disk in CertDir for the kernel to consume.
// - "none" — No TLS. The node will handle plain connections.
//
// Priority Logic:
//
// If CertMode is empty, the manager infers the mode in this order:
// 1. "http" (if auto_tls is true)
// 2. "content" (if both CertContent and KeyContent are provided)
// 3. "file" (if both CertFile and KeyFile paths are provided)
// 4. "none" (default)
type Manager struct {
cfg config.CertConfig
certFile string
keyFile string
magic *certmagic.Config
renewed atomic.Bool
acmeStarted bool // prevents double-init of certmagic background goroutines
}
// NewManager creates a certificate manager.
func NewManager(cfg config.CertConfig) *Manager {
m := &Manager{cfg: cfg}
m.setCertPaths()
return m
}
// setCertPaths derives certFile / keyFile from the current m.cfg.
func (m *Manager) setCertPaths() {
switch m.resolveMode() {
case "file":
m.certFile = m.cfg.CertFile
m.keyFile = m.cfg.KeyFile
case "http", "dns", "self", "content":
dir := m.cfg.CertDir
if dir == "" {
dir = "/etc/xboard-node/certs"
}
domain := m.cfg.Domain
if domain == "" {
domain = "localhost"
}
m.certFile = filepath.Join(dir, domain+".crt")
m.keyFile = filepath.Join(dir, domain+".key")
default:
m.certFile = ""
m.keyFile = ""
}
}
// Reconfigure applies a new cert configuration at runtime (e.g. from panel push).
// It is safe to call after Start(). For ACME modes that are already running this
// is a no-op — a process restart is required to change ACME settings.
// Returns true when the on-disk cert paths changed (caller should restart kernel).
func (m *Manager) Reconfigure(ctx context.Context, newCfg config.CertConfig) (bool, error) {
// Preserve the storage directory so panel-pushed configs don't change
// where certs live on disk (only the operator controls this path).
if newCfg.CertDir == "" {
newCfg.CertDir = m.cfg.CertDir
}
oldCertFile, oldKeyFile := m.certFile, m.keyFile
m.cfg = newCfg
m.setCertPaths()
if err := m.Start(ctx); err != nil {
return false, fmt.Errorf("cert reconfigure: %w", err)
}
return m.certFile != oldCertFile || m.keyFile != oldKeyFile, nil
}
// resolveMode returns the effective cert mode, handling backward compat for auto_tls.
func (m *Manager) resolveMode() string {
mode := strings.ToLower(strings.TrimSpace(m.cfg.CertMode))
if mode != "" {
return mode
}
// Backward compat: auto_tls: true → "http"
if m.cfg.AutoTLS {
return "http"
}
// Inline PEM content provided → "content"
if m.cfg.CertContent != "" && m.cfg.KeyContent != "" {
return "content"
}
// If cert/key file paths are provided → "file"
if m.cfg.CertFile != "" && m.cfg.KeyFile != "" {
return "file"
}
return "none"
}
func (m *Manager) CertFile() string { return m.certFile }
func (m *Manager) KeyFile() string { return m.keyFile }
func (m *Manager) HasCert() bool { return m.certFile != "" && m.keyFile != "" }
func (m *Manager) CertRenewed() bool { return m.renewed.Swap(false) }
// Start initializes the cert manager based on the resolved mode.
func (m *Manager) Start(ctx context.Context) error {
mode := m.resolveMode()
slog.Info("cert manager starting", "mode", mode, "domain", m.cfg.Domain)
switch mode {
case "none", "":
return nil
case "file":
return m.startFile()
case "self":
return m.startSelfSigned()
case "content":
return m.startContent()
case "http":
return m.startACME(ctx, nil)
case "dns":
solver, err := m.buildDNSSolver()
if err != nil {
return fmt.Errorf("build dns solver: %w", err)
}
return m.startACME(ctx, solver)
default:
return fmt.Errorf("unknown cert_mode: %q (supported: http, dns, self, file, content, none)", mode)
}
}
// Stop is a no-op: certmagic's background goroutine is cancelled via the
// context passed to ManageAsync.
func (m *Manager) Stop() {}
// ─── Mode: file ────────────────────────────────────────────────────────────
func (m *Manager) startFile() error {
if _, err := os.Stat(m.cfg.CertFile); err != nil {
return fmt.Errorf("cert file: %w", err)
}
if _, err := os.Stat(m.cfg.KeyFile); err != nil {
return fmt.Errorf("key file: %w", err)
}
slog.Info("using manual TLS certificates", "cert", m.certFile, "key", m.keyFile)
return nil
}
// ─── Mode: self ────────────────────────────────────────────────────────────
func (m *Manager) startSelfSigned() error {
// If files already exist and are valid, skip regeneration.
if fileExists(m.certFile) && fileExists(m.keyFile) {
slog.Info("self-signed cert already exists, reusing", "cert", m.certFile)
return nil
}
if err := os.MkdirAll(filepath.Dir(m.certFile), 0o755); err != nil {
return fmt.Errorf("create cert dir: %w", err)
}
domain := m.cfg.Domain
if domain == "" {
domain = "localhost"
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return fmt.Errorf("generate key: %w", err)
}
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return fmt.Errorf("generate serial: %w", err)
}
template := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{CommonName: domain},
NotBefore: time.Now().Add(-1 * time.Hour),
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour), // 10 years
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
// Add SANs
if ip := net.ParseIP(domain); ip != nil {
template.IPAddresses = []net.IP{ip}
} else {
template.DNSNames = []string{domain}
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return fmt.Errorf("create certificate: %w", err)
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalECPrivateKey(key)
if err != nil {
return fmt.Errorf("marshal key: %w", err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
if err := atomicWriteFile(m.certFile, certPEM, 0o644); err != nil {
return fmt.Errorf("write cert: %w", err)
}
if err := atomicWriteFile(m.keyFile, keyPEM, 0o600); err != nil {
return fmt.Errorf("write key: %w", err)
}
slog.Info("self-signed certificate generated",
"domain", domain, "cert", m.certFile, "valid_years", 10)
return nil
}
// ─── Mode: content ────────────────────────────────────────────────────────
// startContent writes panel-supplied PEM strings to disk so the kernel can
// reference them as ordinary cert/key files.
func (m *Manager) startContent() error {
if m.cfg.CertContent == "" || m.cfg.KeyContent == "" {
return fmt.Errorf("cert_mode 'content' requires both cert_content and key_content")
}
if err := os.MkdirAll(filepath.Dir(m.certFile), 0o755); err != nil {
return fmt.Errorf("create cert dir: %w", err)
}
if err := atomicWriteFile(m.certFile, []byte(m.cfg.CertContent), 0o644); err != nil {
return fmt.Errorf("write cert content: %w", err)
}
if err := atomicWriteFile(m.keyFile, []byte(m.cfg.KeyContent), 0o600); err != nil {
return fmt.Errorf("write key content: %w", err)
}
slog.Info("TLS certificate written from panel content", "cert", m.certFile)
return nil
}
// ─── Mode: http / dns (ACME) ──────────────────────────────────────────────
func (m *Manager) startACME(ctx context.Context, dnsSolver *certmagic.DNS01Solver) error {
// Guard: certmagic's background goroutines must only be started once.
// A process restart is required to change ACME settings at runtime.
if m.acmeStarted {
slog.Info("cert: ACME already active, skipping re-init (restart to change ACME settings)")
return nil
}
if m.cfg.Domain == "" {
return fmt.Errorf("cert.domain is required for ACME modes (http/dns)")
}
if err := os.MkdirAll(m.cfg.CertDir, 0o755); err != nil {
return fmt.Errorf("create cert dir: %w", err)
}
storage := &certmagic.FileStorage{Path: m.cfg.CertDir}
var magic *certmagic.Config
cache := certmagic.NewCache(certmagic.CacheOptions{
GetConfigForCert: func(_ certmagic.Certificate) (*certmagic.Config, error) {
return magic, nil
},
})
magic = certmagic.New(cache, certmagic.Config{
Storage: storage,
OnEvent: func(evtCtx context.Context, event string, data map[string]any) error {
if event == "cert_obtained" {
m.syncCertFiles(evtCtx, storage, data)
}
return nil
},
})
issuer := certmagic.ACMEIssuer{
CA: certmagic.LetsEncryptProductionCA,
Email: m.cfg.Email,
}
if dnsSolver != nil {
// DNS-01 mode: no HTTP port needed, supports wildcards.
issuer.DNS01Solver = dnsSolver
issuer.DisableHTTPChallenge = true
issuer.DisableTLSALPNChallenge = true
} else {
// HTTP-01 mode.
httpPort := m.cfg.HTTPPort
if httpPort == 0 {
httpPort = 80
}
issuer.AltHTTPPort = httpPort
issuer.DisableTLSALPNChallenge = true
}
magic.Issuers = []certmagic.Issuer{
certmagic.NewACMEIssuer(magic, issuer),
}
m.magic = magic
if err := magic.ObtainCertSync(ctx, m.cfg.Domain); err != nil {
return fmt.Errorf("obtain certificate: %w", err)
}
if err := magic.ManageAsync(ctx, []string{m.cfg.Domain}); err != nil {
return fmt.Errorf("start cert manager: %w", err)
}
m.acmeStarted = true
return nil
}
// ─── DNS Provider Factory ──────────────────────────────────────────────────
// buildDNSSolver builds a certmagic DNS01Solver from the configured provider.
func (m *Manager) buildDNSSolver() (*certmagic.DNS01Solver, error) {
provider, err := m.newDNSProvider()
if err != nil {
return nil, err
}
return &certmagic.DNS01Solver{DNSManager: certmagic.DNSManager{DNSProvider: provider}}, nil
}
func (m *Manager) newDNSProvider() (certmagic.DNSProvider, error) {
name := strings.ToLower(strings.TrimSpace(m.cfg.DNSProvider))
env := m.cfg.DNSEnv
if env == nil {
env = map[string]string{}
}
switch name {
case "cloudflare", "cf":
token := firstOf(env, "CF_API_TOKEN", "CLOUDFLARE_API_TOKEN")
if token == "" {
return nil, fmt.Errorf("cloudflare requires CF_API_TOKEN in dns_env")
}
return &cloudflare.Provider{APIToken: token}, nil
case "alidns", "aliyun":
keyID := firstOf(env, "ALICLOUD_ACCESS_KEY_ID", "ALI_ACCESS_KEY_ID")
keySecret := firstOf(env, "ALICLOUD_ACCESS_KEY_SECRET", "ALI_ACCESS_KEY_SECRET")
if keyID == "" || keySecret == "" {
return nil, fmt.Errorf("alidns requires ALICLOUD_ACCESS_KEY_ID and ALICLOUD_ACCESS_KEY_SECRET in dns_env")
}
return &alidns.Provider{
CredentialInfo: alidns.CredentialInfo{
AccessKeyID: keyID,
AccessKeySecret: keySecret,
},
}, nil
default:
return nil, fmt.Errorf("unsupported dns_provider: %q (supported: cloudflare, alidns)", name)
}
}
// ─── Helpers ───────────────────────────────────────────────────────────────
// syncCertFiles copies the cert and key from certmagic's storage to stable paths.
func (m *Manager) syncCertFiles(ctx context.Context, storage certmagic.Storage, data map[string]any) {
certKey, _ := data["certificate_path"].(string)
keyKey, _ := data["private_key_path"].(string)
if certKey == "" || keyKey == "" {
slog.Warn("cert_obtained event missing storage paths", "data", data)
return
}
certPEM, err := storage.Load(ctx, certKey)
if err != nil {
slog.Error("failed to load cert from storage", "error", err)
return
}
keyPEM, err := storage.Load(ctx, keyKey)
if err != nil {
slog.Error("failed to load key from storage", "error", err)
return
}
if err := atomicWriteFile(m.certFile, certPEM, 0o644); err != nil {
slog.Error("failed to write cert file", "path", m.certFile, "error", err)
return
}
if err := atomicWriteFile(m.keyFile, keyPEM, 0o600); err != nil {
slog.Error("failed to write key file", "path", m.keyFile, "error", err)
return
}
renewal, _ := data["renewal"].(bool)
slog.Info("TLS certificate synced", "domain", m.cfg.Domain, "renewal", renewal,
"cert", m.certFile)
m.renewed.Store(true)
}
func atomicWriteFile(path string, data []byte, perm os.FileMode) error {
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, ".tmp-*")
if err != nil {
return err
}
tmpName := tmp.Name()
if _, err := tmp.Write(data); err != nil {
tmp.Close()
os.Remove(tmpName)
return err
}
if err := tmp.Chmod(perm); err != nil {
tmp.Close()
os.Remove(tmpName)
return err
}
if err := tmp.Close(); err != nil {
os.Remove(tmpName)
return err
}
return os.Rename(tmpName, path)
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// firstOf returns the first non-empty value for any of the given keys in the map.
func firstOf(m map[string]string, keys ...string) string {
for _, k := range keys {
if v := m[k]; v != "" {
return v
}
}
return ""
}
+370
View File
@@ -0,0 +1,370 @@
package config
import (
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strconv"
"gopkg.in/yaml.v3"
)
type Config struct {
Panel PanelConfig `yaml:"panel"`
Node NodeConfig `yaml:"node"`
Kernel KernelConfig `yaml:"kernel"`
Cert CertConfig `yaml:"cert"`
Log LogConfig `yaml:"log"`
Runtime RuntimeConfig `yaml:"runtime"`
// HealthPort enables a lightweight HTTP health-check endpoint on the
// given port (e.g. 65530). 0 = disabled (default).
HealthPort int `yaml:"health_port"`
// Nodes enables multi-node mode. When set, Panel.NodeID is ignored and
// one service instance is started per entry. All entries share the same
// panel URL/token, kernel type, log settings and runtime tuning.
Nodes []NodeEntry `yaml:"nodes,omitempty"`
}
// NodeEntry describes a single node in multi-node mode.
type NodeEntry struct {
NodeID int `yaml:"node_id"`
NodeType string `yaml:"node_type,omitempty"`
// Kernel allows per-node overrides (e.g. config_dir). nil = inherit global.
Kernel *KernelOverride `yaml:"kernel,omitempty"`
// Cert allows per-node certificate overrides. nil = inherit global.
Cert *CertConfig `yaml:"cert,omitempty"`
}
// KernelOverride holds the subset of KernelConfig that is useful to override
// per-node. Only non-zero fields replace the global value.
type KernelOverride struct {
ConfigDir string `yaml:"config_dir,omitempty"`
GeoDataDir string `yaml:"geo_data_dir,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
CustomConfig string `yaml:"custom_config,omitempty"`
}
// RuntimeConfig tunes Go runtime memory behaviour.
// These knobs let operators trade CPU against memory on constrained machines
// without recompiling.
//
// Example (config.yml):
//
// runtime:
// gomemlimit: "30MiB" # soft RSS cap; GC becomes more aggressive above this
// gogc: 50 # halve GC target → lower peak RSS, slightly more CPU
type RuntimeConfig struct {
// GoMemLimit is a human-readable soft memory limit passed to runtime/debug.SetMemoryLimit.
// Valid suffixes: B, KiB, MiB, GiB, TiB. Empty = no limit (default).
// Recommended starting point: set to ~80% of the machine's available RAM.
GoMemLimit string `yaml:"gomemlimit"`
// GoGCPercent overrides GOGC (default 100).
// Lower values (e.g. 50) trigger GC more often → lower memory, slightly higher CPU.
// 0 means "use the default (100)".
GoGCPercent int `yaml:"gogc"`
}
type PanelConfig struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
NodeID int `yaml:"node_id"`
NodeType string `yaml:"node_type"`
}
type NodeConfig struct {
PushInterval int `yaml:"push_interval"`
PullInterval int `yaml:"pull_interval"`
}
type KernelConfig struct {
Type string `yaml:"type"` // "singbox" or "xray"
ConfigDir string `yaml:"config_dir"`
LogLevel string `yaml:"log_level"`
// GeoDataDir is the directory that contains GeoIP/GeoSite database files.
// For sing-box: geoip.db and geosite.db (geoip2-format).
// For xray: geoip.dat and geosite.dat.
// Defaults to config_dir when empty. You only need to set this if your
// geo database files live somewhere other than config_dir.
GeoDataDir string `yaml:"geo_data_dir"`
// CustomOutbound adds outbound entries to the generated kernel config.
// Each item is a raw kernel-native outbound object (sing-box or xray format).
CustomOutbound []map[string]any `yaml:"custom_outbound"`
// CustomRoute adds route rules to the generated kernel config.
// Each item is a raw kernel-native route rule object.
CustomRoute []map[string]any `yaml:"custom_route"`
// CustomConfig is the path to a kernel-native config file (JSON or YAML)
// that is deep-merged into the auto-generated config. This enables full
// customization of dns, outbounds, endpoints, route, experimental, etc.
// Compatible with V2bX OriginalPath format.
CustomConfig string `yaml:"custom_config"`
}
type CertConfig struct {
AutoTLS bool `yaml:"auto_tls"`
Domain string `yaml:"domain"`
Email string `yaml:"email"`
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
CertDir string `yaml:"cert_dir"`
HTTPPort int `yaml:"http_port"` // port for HTTP-01 challenge (default: 80)
// CertMode selects the TLS certificate strategy:
// "" - auto-detect: if CertFile is set → file; if AutoTLS → http; else none
// "http" - ACME HTTP-01 challenge (requires port 80)
// "dns" - ACME DNS-01 challenge (requires DNSProvider + DNSEnv)
// "self" - generate a self-signed certificate (valid 10 years)
// "file" - use manually provided CertFile/KeyFile paths
// "content"- cert/key PEM provided directly in CertContent/KeyContent
// "none" - no TLS
CertMode string `yaml:"cert_mode"`
// DNSProvider specifies the DNS provider for DNS-01 challenge.
// Supported: "cloudflare", "alidns"
DNSProvider string `yaml:"dns_provider"`
// DNSEnv passes credentials to the DNS provider as key=value pairs.
// Example for cloudflare: {"CF_API_TOKEN": "xxxx"}
DNSEnv map[string]string `yaml:"dns_env"`
// CertContent / KeyContent hold PEM-encoded certificate and private key.
// Used when CertMode == "content" (e.g. panel pushes a cert directly).
// The values are written to CertDir and then referenced as files by the kernel.
CertContent string `yaml:"cert_content,omitempty"`
KeyContent string `yaml:"key_content,omitempty"`
}
type LogConfig struct {
Level string `yaml:"level"`
Output string `yaml:"output"`
}
// Load reads configuration from a YAML file, then applies environment variable
// overrides. If the config file does not exist, a config is built entirely from
// environment variables (useful for Docker deployment with -e flags).
func Load(path string) (*Config, error) {
cfg := &Config{}
data, err := os.ReadFile(path)
if err == nil {
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
} else if !os.IsNotExist(err) {
return nil, fmt.Errorf("read config file: %w", err)
}
cfg.applyEnvOverrides()
cfg.setDefaults()
if err := cfg.validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return cfg, nil
}
// envFirst returns the first non-empty value among the given env var names.
func envFirst(names ...string) string {
for _, n := range names {
if v := os.Getenv(n); v != "" {
return v
}
}
return ""
}
func (c *Config) applyEnvOverrides() {
if v := envFirst("apiHost", "API_HOST"); v != "" {
c.Panel.URL = v
}
if v := envFirst("apiKey", "API_KEY"); v != "" {
c.Panel.Token = v
}
if v := envFirst("nodeID", "NODE_ID"); v != "" {
if id, err := strconv.Atoi(v); err == nil {
c.Panel.NodeID = id
}
}
if v := envFirst("nodeType", "NODE_TYPE"); v != "" {
c.Panel.NodeType = v
}
if v := envFirst("kernel", "KERNEL_TYPE"); v != "" {
c.Kernel.Type = v
}
if v := envFirst("certFile", "CERT_FILE"); v != "" {
c.Cert.CertFile = v
}
if v := envFirst("keyFile", "KEY_FILE"); v != "" {
c.Cert.KeyFile = v
}
if v := envFirst("domain", "DOMAIN"); v != "" {
c.Cert.Domain = v
c.Cert.AutoTLS = true
}
if v := envFirst("logLevel", "LOG_LEVEL"); v != "" {
c.Log.Level = v
}
}
func (c *Config) setDefaults() {
if c.Kernel.Type == "" {
c.Kernel.Type = "singbox"
}
if c.Kernel.ConfigDir == "" {
c.Kernel.ConfigDir = "/etc/xboard-node"
}
if c.Kernel.GeoDataDir == "" {
c.Kernel.GeoDataDir = c.Kernel.ConfigDir
}
if c.Kernel.LogLevel == "" {
c.Kernel.LogLevel = "warn"
}
if c.Log.Level == "" {
c.Log.Level = "info"
}
if c.Log.Output == "" {
c.Log.Output = "stdout"
}
if c.Cert.CertDir == "" {
c.Cert.CertDir = filepath.Join(c.Kernel.ConfigDir, "certs")
}
if c.Cert.HTTPPort == 0 {
c.Cert.HTTPPort = 80
}
}
func (c *Config) validate() error {
if c.Panel.URL == "" {
return fmt.Errorf("panel.url is required")
}
if c.Panel.Token == "" {
return fmt.Errorf("panel.token is required")
}
// In multi-node mode panel.node_id is optional; validate each NodeEntry instead.
if len(c.Nodes) == 0 && c.Panel.NodeID <= 0 {
return fmt.Errorf("panel.node_id must be positive (or use 'nodes:' for multi-node)")
}
for i, n := range c.Nodes {
if n.NodeID <= 0 {
return fmt.Errorf("nodes[%d].node_id must be positive", i)
}
}
switch c.Kernel.Type {
case "singbox", "xray":
default:
return fmt.Errorf("kernel.type must be 'singbox' or 'xray', got '%s'", c.Kernel.Type)
}
if c.Cert.AutoTLS && c.Cert.Domain == "" {
return fmt.Errorf("cert.domain is required when cert.auto_tls is enabled")
}
if c.Node.PushInterval < 0 {
return fmt.Errorf("node.push_interval must not be negative")
}
if c.Node.PullInterval < 0 {
return fmt.Errorf("node.pull_interval must not be negative")
}
return nil
}
// ExpandNodes returns one *Config per node to run.
// Single-node mode (Nodes empty): returns a slice containing the receiver.
// Multi-node mode: returns one derived *Config per NodeEntry, each inheriting
// shared settings and applying per-node overrides.
func (c *Config) ExpandNodes() []*Config {
if len(c.Nodes) == 0 {
return []*Config{c}
}
result := make([]*Config, 0, len(c.Nodes))
for _, entry := range c.Nodes {
nodeCfg := *c // shallow copy — safe because slices/maps are not mutated
nodeCfg.Nodes = nil
nodeCfg.Panel.NodeID = entry.NodeID
nodeCfg.Panel.NodeType = entry.NodeType
// Per-node kernel overrides
if entry.Kernel != nil {
if entry.Kernel.ConfigDir != "" {
nodeCfg.Kernel.ConfigDir = entry.Kernel.ConfigDir
if nodeCfg.Kernel.GeoDataDir == c.Kernel.ConfigDir {
// GeoDataDir was defaulted to ConfigDir — keep it pointing at
// the new ConfigDir unless the user set it explicitly.
nodeCfg.Kernel.GeoDataDir = entry.Kernel.ConfigDir
}
}
if entry.Kernel.GeoDataDir != "" {
nodeCfg.Kernel.GeoDataDir = entry.Kernel.GeoDataDir
}
if entry.Kernel.LogLevel != "" {
nodeCfg.Kernel.LogLevel = entry.Kernel.LogLevel
}
if entry.Kernel.CustomConfig != "" {
nodeCfg.Kernel.CustomConfig = entry.Kernel.CustomConfig
}
} else {
// Auto-derive a unique config_dir per node to avoid conflicts.
nodeCfg.Kernel.ConfigDir = fmt.Sprintf("%s/node-%d", c.Kernel.ConfigDir, entry.NodeID)
if nodeCfg.Kernel.GeoDataDir == c.Kernel.ConfigDir {
// Share the geo data dir with the base dir to avoid re-downloading.
nodeCfg.Kernel.GeoDataDir = c.Kernel.GeoDataDir
}
}
// Per-node cert overrides
if entry.Cert != nil {
nodeCfg.Cert = *entry.Cert
}
if nodeCfg.Cert.CertDir == "" {
nodeCfg.Cert.CertDir = filepath.Join(nodeCfg.Kernel.ConfigDir, "certs")
}
result = append(result, &nodeCfg)
}
return result
}
func InitLogger(cfg LogConfig) {
var level slog.Level
switch cfg.Level {
case "debug":
level = slog.LevelDebug
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
default:
level = slog.LevelInfo
}
var w io.Writer
switch cfg.Output {
case "stdout", "":
w = os.Stdout
case "stderr":
w = os.Stderr
default:
dir := filepath.Dir(cfg.Output)
if err := os.MkdirAll(dir, 0o755); err != nil {
slog.Error("failed to create log dir, falling back to stdout", "error", err)
w = os.Stdout
} else {
f, err := os.OpenFile(cfg.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
slog.Error("failed to open log file, falling back to stdout", "error", err)
w = os.Stdout
} else {
w = f
}
}
}
handler := slog.NewTextHandler(w, &slog.HandlerOptions{Level: level})
slog.SetDefault(slog.New(handler))
}
+269
View File
@@ -0,0 +1,269 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func writeTemp(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestLoad_ValidConfig(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://panel.example.com"
token: "secret-token"
node_id: 5
node_type: "v2ray"
kernel:
type: singbox
log_level: warn
log:
level: debug
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Panel.URL != "https://panel.example.com" {
t.Errorf("url: got %q", cfg.Panel.URL)
}
if cfg.Panel.Token != "secret-token" {
t.Errorf("token: got %q", cfg.Panel.Token)
}
if cfg.Panel.NodeID != 5 {
t.Errorf("node_id: got %d", cfg.Panel.NodeID)
}
if cfg.Panel.NodeType != "v2ray" {
t.Errorf("node_type: got %q", cfg.Panel.NodeType)
}
if cfg.Kernel.Type != "singbox" {
t.Errorf("kernel.type: got %q", cfg.Kernel.Type)
}
if cfg.Log.Level != "debug" {
t.Errorf("log.level: got %q", cfg.Log.Level)
}
}
func TestLoad_Defaults(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://panel.example.com"
token: "tok"
node_id: 1
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Kernel.Type != "singbox" {
t.Errorf("default kernel.type: got %q, want singbox", cfg.Kernel.Type)
}
if cfg.Kernel.ConfigDir != "/etc/xboard-node" {
t.Errorf("default config_dir: got %q", cfg.Kernel.ConfigDir)
}
if cfg.Kernel.LogLevel != "warn" {
t.Errorf("default kernel log_level: got %q, want warn", cfg.Kernel.LogLevel)
}
if cfg.Log.Level != "info" {
t.Errorf("default log.level: got %q, want info", cfg.Log.Level)
}
if cfg.Log.Output != "stdout" {
t.Errorf("default log.output: got %q, want stdout", cfg.Log.Output)
}
if cfg.Cert.HTTPPort != 80 {
t.Errorf("default http_port: got %d, want 80", cfg.Cert.HTTPPort)
}
expectedCertDir := filepath.Join("/etc/xboard-node", "certs")
if cfg.Cert.CertDir != expectedCertDir {
t.Errorf("default cert_dir: got %q, want %q", cfg.Cert.CertDir, expectedCertDir)
}
}
func TestLoad_MissingURL(t *testing.T) {
path := writeTemp(t, `
panel:
token: "tok"
node_id: 1
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for missing url")
}
}
func TestLoad_MissingToken(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
node_id: 1
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for missing token")
}
}
func TestLoad_InvalidNodeID(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 0
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for node_id=0")
}
}
func TestLoad_NegativeNodeID(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: -1
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for negative node_id")
}
}
func TestLoad_InvalidKernelType(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
kernel:
type: "invalid"
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for invalid kernel type")
}
}
func TestLoad_XrayKernel(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
kernel:
type: xray
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Kernel.Type != "xray" {
t.Errorf("kernel.type: got %q, want xray", cfg.Kernel.Type)
}
}
func TestLoad_AutoTLS_NoDomain(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
cert:
auto_tls: true
`)
_, err := Load(path)
if err == nil {
t.Fatal("expected error for auto_tls without domain")
}
}
func TestLoad_AutoTLS_WithDomain(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
cert:
auto_tls: true
domain: "node.example.com"
email: "admin@example.com"
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if !cfg.Cert.AutoTLS {
t.Error("auto_tls should be true")
}
if cfg.Cert.Domain != "node.example.com" {
t.Errorf("domain: got %q", cfg.Cert.Domain)
}
}
func TestLoad_FileNotFound(t *testing.T) {
_, err := Load("/nonexistent/path/config.yaml")
if err == nil {
t.Fatal("expected error for missing file")
}
}
func TestLoad_InvalidYAML(t *testing.T) {
path := writeTemp(t, "{{{{invalid yaml}}}")
_, err := Load(path)
if err == nil {
t.Fatal("expected error for invalid YAML")
}
}
func TestLoad_CustomCert(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
cert:
cert_file: "/custom/cert.pem"
key_file: "/custom/key.pem"
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Cert.CertFile != "/custom/cert.pem" {
t.Errorf("cert_file: got %q", cfg.Cert.CertFile)
}
if cfg.Cert.KeyFile != "/custom/key.pem" {
t.Errorf("key_file: got %q", cfg.Cert.KeyFile)
}
}
func TestLoad_CustomIntervals(t *testing.T) {
path := writeTemp(t, `
panel:
url: "https://example.com"
token: "tok"
node_id: 1
node:
push_interval: 30
pull_interval: 60
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Node.PushInterval != 30 {
t.Errorf("push_interval: got %d", cfg.Node.PushInterval)
}
if cfg.Node.PullInterval != 60 {
t.Errorf("pull_interval: got %d", cfg.Node.PullInterval)
}
}
+110
View File
@@ -0,0 +1,110 @@
package kernel
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// LoadCustomConfig reads a custom config file (JSON or YAML) and returns it
// as a generic map. Returns nil if path is empty or file does not exist.
func LoadCustomConfig(path string) (map[string]any, error) {
if path == "" {
return nil, nil
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
slog.Warn("custom config file not found, skipping", "path", path)
return nil, nil
}
return nil, fmt.Errorf("read custom config %s: %w", path, err)
}
if len(data) == 0 {
return nil, nil
}
result := make(map[string]any)
trimmed := strings.TrimSpace(string(data))
if strings.HasPrefix(trimmed, "{") {
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("parse custom config as JSON: %w", err)
}
} else {
if err := yaml.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("parse custom config as YAML: %w", err)
}
}
slog.Info("loaded custom config", "path", path, "keys", mapKeys(result))
return result, nil
}
// MergeAppendList appends items from src list into dst, returning the merged list.
// Each item is converted to map[string]any for JSON-marshal compatibility.
func MergeAppendList(dst []map[string]any, srcRaw any) []map[string]any {
srcList, ok := toSliceOfMaps(srcRaw)
if !ok {
return dst
}
return append(dst, srcList...)
}
// MergePrependList prepends items from src list before dst items.
func MergePrependList(dst []map[string]any, srcRaw any) []map[string]any {
srcList, ok := toSliceOfMaps(srcRaw)
if !ok {
return dst
}
return append(srcList, dst...)
}
func toSliceOfMaps(v any) ([]map[string]any, bool) {
if v == nil {
return nil, false
}
switch items := v.(type) {
case []any:
result := make([]map[string]any, 0, len(items))
for _, item := range items {
if m, ok := toMap(item); ok {
result = append(result, m)
}
}
return result, len(result) > 0
case []map[string]any:
return items, len(items) > 0
default:
return nil, false
}
}
func toMap(v any) (map[string]any, bool) {
switch m := v.(type) {
case map[string]any:
return m, true
case map[any]any:
result := make(map[string]any, len(m))
for k, val := range m {
result[fmt.Sprint(k)] = val
}
return result, true
default:
return nil, false
}
}
func mapKeys(m map[string]any) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
+31
View File
@@ -0,0 +1,31 @@
package kernel
import (
"strings"
"github.com/cedar2025/xboard-node/internal/panel"
)
// NeedsGeoIP returns true when any panel route rule contains a "geoip:" match entry.
func NeedsGeoIP(routes []panel.RouteRule) bool {
for _, r := range routes {
for _, m := range r.Match {
if strings.HasPrefix(m, "geoip:") {
return true
}
}
}
return false
}
// NeedsGeoSite returns true when any panel route rule contains a "geosite:" match entry.
func NeedsGeoSite(routes []panel.RouteRule) bool {
for _, r := range routes {
for _, m := range r.Match {
if strings.HasPrefix(m, "geosite:") {
return true
}
}
}
return false
}
+105
View File
@@ -0,0 +1,105 @@
package geodata
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"time"
)
const (
singboxGeoIPURL = "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db"
singboxGeoSiteURL = "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db"
xrayGeoIPURL = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat"
xrayGeoSiteURL = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat"
)
var httpClient = &http.Client{Timeout: 10 * time.Minute}
func Ensure(dir string, needGeoIP, needGeoSite bool, kernelType string) error {
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("create geo_data_dir %q: %w", dir, err)
}
type entry struct {
name string
url string
}
var files []entry
if kernelType == "singbox" {
if needGeoIP {
files = append(files, entry{"geoip.db", singboxGeoIPURL})
}
if needGeoSite {
files = append(files, entry{"geosite.db", singboxGeoSiteURL})
}
} else {
if needGeoIP {
files = append(files, entry{"geoip.dat", xrayGeoIPURL})
}
if needGeoSite {
files = append(files, entry{"geosite.dat", xrayGeoSiteURL})
}
}
var firstErr error
for _, f := range files {
if err := ensureFile(dir, f.name, f.url); err != nil {
slog.Warn("geo database auto-download failed", "file", f.name, "error", err)
if firstErr == nil {
firstErr = err
}
}
}
return firstErr
}
func ensureFile(dir, name, url string) error {
dst := filepath.Join(dir, name)
if _, err := os.Stat(dst); err == nil {
return nil
}
slog.Info("geo database missing, downloading automatically", "file", name, "url", url)
if err := atomicDownload(dst, url); err != nil {
return err
}
if fi, err := os.Stat(dst); err == nil {
slog.Info("geo database ready", "file", name, "size_kb", fi.Size()/1024)
}
return nil
}
func atomicDownload(dst, url string) error {
resp, err := httpClient.Get(url)
if err != nil {
return fmt.Errorf("http get %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected HTTP status from %s: %s", url, resp.Status)
}
tmp := dst + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
defer os.Remove(tmp)
if _, err := io.Copy(f, resp.Body); err != nil {
f.Close()
return fmt.Errorf("write geo database: %w", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Rename(tmp, dst); err != nil {
return fmt.Errorf("rename to destination: %w", err)
}
return nil
}
+42
View File
@@ -0,0 +1,42 @@
package kernel
import (
"context"
"github.com/cedar2025/xboard-node/internal/panel"
"golang.org/x/time/rate"
)
// Kernel is the interface for proxy kernel backends (sing-box, xray, etc.)
type Kernel interface {
// Name returns the kernel identifier (e.g. "sing-box")
Name() string
// ApplyConfig generates kernel config from panel data and (re)starts the process
ApplyConfig(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error
// Stop gracefully stops the kernel process
Stop()
// IsRunning returns whether the kernel is currently active
IsRunning() bool
// GetConnections returns all active proxy connections
GetConnections(ctx context.Context) ([]Connection, error)
// CloseConnection terminates a specific connection by ID
CloseConnection(ctx context.Context, connID string) error
// SetSpeedLimitFunc configures optional per-user bandwidth throttling.
// The function resolves a user UUID to a *rate.Limiter (nil = unlimited).
// For sing-box, the limiter is embedded in the connection tracker wrapper
// so both byte counting and rate limiting happen in a single CountFunc
// callback, allowing splice/zero-copy paths.
SetSpeedLimitFunc(fn func(uuid string) *rate.Limiter)
// Reload hot-swaps the inbound users and routing rules without full kernel restart.
// Existing connections may be briefly interrupted, but routes/outbounds stay alive.
Reload(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error
}
// Connection represents an active proxy connection reported by the kernel
type Connection struct {
ID string
UserID int // extracted user ID (0 if unknown)
Upload int64 // cumulative upload bytes
Download int64 // cumulative download bytes
SourceIP string // client source IP (cleaned, no port/brackets)
}
+710
View File
@@ -0,0 +1,710 @@
package singbox
import (
"fmt"
"log/slog"
"net"
"path/filepath"
"strconv"
"strings"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/panel"
)
// M is a shorthand for building JSON-like maps
type M = map[string]interface{}
func buildConfig(kcfg config.KernelConfig, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
outbounds := []M{
{"type": "direct", "tag": "direct"},
{"type": "block", "tag": "block"},
}
// Panel-defined custom outbounds (structured, converted to sing-box native)
for _, co := range nc.CustomOutbounds {
outbounds = append(outbounds, outboundConfigToSingbox(co))
}
// Static outbounds from local config file (already kernel-native format)
for _, co := range kcfg.CustomOutbound {
outbounds = append(outbounds, M(co))
}
cfg := M{
"log": M{
"level": kcfg.LogLevel,
"timestamp": true,
},
"outbounds": outbounds,
}
inbound := buildInbound(nc, users, certFile, keyFile)
if inbound != nil {
cfg["inbounds"] = []M{inbound}
}
// Merge panel routes and static config routes
cfg["route"] = buildRoutes(nc.Routes, mergeRouteList(nc.CustomRoutes, kcfg.CustomRoute))
// Automatically enable rule_set caching (cache_file) when panel routes
// reference geoip:/geosite: entries so that the downloaded .srs rule_set
// files survive across process restarts.
if kernel.NeedsGeoIP(nc.Routes) || kernel.NeedsGeoSite(nc.Routes) {
cfg["experimental"] = M{
"cache_file": M{
"enabled": true,
"path": filepath.Join(kcfg.ConfigDir, "cache.db"),
},
}
}
mergeCustomSingbox(cfg, kcfg)
return cfg
}
// outboundConfigToSingbox converts a structured OutboundConfig (from the panel)
// into a sing-box outbound object. sing-box uses a flat layout where all
// protocol-specific fields sit at the top level alongside "type" and "tag".
func outboundConfigToSingbox(oc panel.OutboundConfig) M {
m := M{
"type": oc.Protocol,
"tag": oc.Tag,
}
// Transform common protocol keys to sing-box native format
// WireGuard: secret_key -> private_key; peers: endpoint -> address+port
if oc.Protocol == "wireguard" {
if sk, ok := oc.Settings["secret_key"]; ok {
m["private_key"] = sk
}
if peers, ok := oc.Settings["peers"].([]any); ok {
var wgPeers []M
for _, p := range peers {
if peerMap, ok := p.(map[string]any); ok {
newPeer := M{}
for k, v := range peerMap {
if k == "endpoint" {
if ep, ok := v.(string); ok {
host, portStr, err := net.SplitHostPort(ep)
if err == nil {
newPeer["address"] = host
port, _ := strconv.Atoi(portStr)
newPeer["port"] = port
} else {
newPeer["address"] = ep
}
}
} else {
newPeer[k] = v
}
}
wgPeers = append(wgPeers, newPeer)
}
}
m["peers"] = wgPeers
}
// Copy any other top-level settings not handled above
for k, v := range oc.Settings {
if k != "secret_key" && k != "peers" {
m[k] = v
}
}
} else {
for k, v := range oc.Settings {
m[k] = v
}
}
if oc.ProxyTag != "" {
m["proxy_tag"] = oc.ProxyTag
}
return m
}
func mergeRouteList(a, b []map[string]any) []map[string]any {
res := make([]map[string]any, 0, len(a)+len(b))
res = append(res, a...)
res = append(res, b...)
return res
}
func buildRoutes(panelRoutes []panel.RouteRule, custom []map[string]any) M {
var rules []M
// Custom Routes (Panel-pushed or Local) go FIRST to have highest priority
for _, cr := range custom {
rules = append(rules, M(cr))
}
// Standard blocks for private/loopback
rules = append(rules, M{
"outbound": "block",
"ip_cidr": []string{
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"fc00::/7",
"fe80::/10",
"::1/128",
},
}, M{
"outbound": "block",
"domain": []string{
"geoip:private",
},
})
// Panel-defined routes (usually specific blocks/proxies)
for _, pr := range panelRoutes {
if len(pr.Match) == 0 {
continue
}
var domains, cidrs []string
for _, m := range pr.Match {
m = strings.TrimSpace(m)
if m == "" {
continue
}
// Strip leading wildcard for domain matching
if strings.HasPrefix(m, "*.") {
m = strings.TrimPrefix(m, "*.")
}
// Check if it's a CIDR block (contains /)
if strings.Contains(m, "/") {
cidrs = append(cidrs, m)
} else {
// Otherwise treat as domain
domains = append(domains, m)
}
}
// Determine outbound tag based on action
outbound := "block"
if pr.Action == "direct" {
outbound = "direct"
} else if pr.Action == "dns" {
// Special case for sing-box DNS routing:
// If action is "dns" and action_value is provided, we use it as the server name.
// Otherwise it defaults to "dns-out".
server := "dns-out"
if pr.ActionValue != "" {
server = pr.ActionValue
}
outbound = server
} else if pr.Action == "proxy" {
// If panel provides a specific outbound tag in action_value, use it.
// This allows routing to WARP_JP etc. via normal routes.
if pr.ActionValue != "" {
outbound = pr.ActionValue
}
}
// Create separate rule for domains
if len(domains) > 0 {
rule := M{
"domain_suffix": domains,
"outbound": outbound,
}
rules = append(rules, rule)
}
// Create separate rule for CIDRs
if len(cidrs) > 0 {
rule := M{
"ip_cidr": cidrs,
"outbound": outbound,
}
rules = append(rules, rule)
}
}
return M{
"final": "direct",
"rules": rules,
}
}
func mergeCustomSingbox(cfg M, kcfg config.KernelConfig) {
custom, err := kernel.LoadCustomConfig(kcfg.CustomConfig)
if err != nil {
slog.Error("failed to load custom sing-box config", "error", err)
return
}
if custom == nil {
return
}
// dns — custom replaces
if v, ok := custom["dns"]; ok {
cfg["dns"] = v
}
// experimental — custom replaces
if v, ok := custom["experimental"]; ok {
cfg["experimental"] = v
}
// outbounds — append custom entries
if v, ok := custom["outbounds"]; ok {
if existing, ok := cfg["outbounds"].([]M); ok {
cfg["outbounds"] = kernel.MergeAppendList(existing, v)
}
}
// endpoints (sing-box 1.11+ wireguard etc.) — append
if v, ok := custom["endpoints"]; ok {
if existing, ok := cfg["endpoints"].([]M); ok {
cfg["endpoints"] = kernel.MergeAppendList(existing, v)
} else {
if items := kernel.MergeAppendList(nil, v); len(items) > 0 {
cfg["endpoints"] = items
}
}
}
// route — merge sub-fields
if customRoute, ok := custom["route"]; ok {
if customRouteMap, ok := customRoute.(map[string]any); ok {
mergeCustomSingboxRoute(cfg, customRouteMap)
}
}
}
func mergeCustomSingboxRoute(cfg M, customRoute map[string]any) {
route, ok := cfg["route"].(M)
if !ok {
route = M{}
cfg["route"] = route
}
// rules — custom rules prepended (so they match before panel rules)
if v, ok := customRoute["rules"]; ok {
if existing, ok := route["rules"].([]M); ok {
route["rules"] = kernel.MergePrependList(existing, v)
}
}
// rule_set — custom rule_sets appended
if v, ok := customRoute["rule_set"]; ok {
if existing, ok := route["rule_set"].([]M); ok {
route["rule_set"] = kernel.MergeAppendList(existing, v)
} else {
route["rule_set"] = kernel.MergeAppendList(nil, v)
}
}
// final, auto_detect_interface, default_interface, etc. — custom overrides
for k, v := range customRoute {
if k == "rules" || k == "rule_set" {
continue
}
route[k] = v
}
}
func buildInbound(nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base := M{
"tag": nc.Protocol + "-in",
"listen": "::",
"listen_port": nc.ServerPort,
}
switch nc.Protocol {
case "shadowsocks":
return buildShadowsocks(base, nc, users)
case "vmess":
return buildVMess(base, nc, users, certFile, keyFile)
case "vless":
return buildVLESS(base, nc, users, certFile, keyFile)
case "trojan":
return buildTrojan(base, nc, users, certFile, keyFile)
case "hysteria":
return buildHysteria(base, nc, users, certFile, keyFile)
case "tuic":
return buildTUIC(base, nc, users, certFile, keyFile)
case "anytls":
return buildAnyTLS(base, nc, users, certFile, keyFile)
case "naive":
return buildNaive(base, nc, users, certFile, keyFile)
case "socks":
return buildSocks(base, users)
case "http":
return buildHTTP(base, nc, users, certFile, keyFile)
default:
return nil
}
}
func buildShadowsocks(base M, nc *panel.NodeConfig, users []panel.User) M {
base["type"] = "shadowsocks"
base["method"] = nc.Cipher
if strings.HasPrefix(nc.Cipher, "2022-blake3-") {
base["password"] = nc.ServerKey
}
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
if nc.Plugin != "" {
base["plugin"] = nc.Plugin
base["plugin_opts"] = nc.PluginOpt
}
return base
}
func buildVMess(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "vmess"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"uuid": u.UUID,
"alterId": 0,
})
}
base["users"] = userList
applyTransport(base, nc)
if nc.TLS == 1 {
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
}
return base
}
func buildVLESS(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "vless"
userList := make([]M, 0, len(users))
for _, u := range users {
user := M{
"name": u.UUID,
"uuid": u.UUID,
}
if nc.Flow != "" {
user["flow"] = nc.Flow
}
userList = append(userList, user)
}
base["users"] = userList
applyTransport(base, nc)
if nc.TLS == 1 {
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
} else if nc.TLS == 2 {
base["tls"] = buildRealityConfig(nc)
}
return base
}
func buildTrojan(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "trojan"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
applyTransport(base, nc)
if nc.TLS == 1 {
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
} else if nc.TLS == 2 {
base["tls"] = buildRealityConfig(nc)
}
return base
}
func buildHysteria(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
if nc.Version == 2 {
base["type"] = "hysteria2"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
if nc.Obfs != "" {
base["obfs"] = M{
"type": nc.Obfs,
"password": nc.ObfsPassword,
}
}
} else {
base["type"] = "hysteria"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"auth_str": u.UUID,
})
}
base["users"] = userList
base["up_mbps"] = nc.UpMbps
base["down_mbps"] = nc.DownMbps
if nc.Obfs != "" {
base["obfs"] = nc.Obfs
}
}
tls := buildTLSConfig(nc, certFile, keyFile)
// Hysteria/Hysteria2 uses QUIC and requires ALPN; default to h3 if not set.
if _, ok := tls["alpn"]; !ok {
tls["alpn"] = []string{"h3"}
}
base["tls"] = tls
return base
}
func buildTUIC(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "tuic"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"uuid": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
if nc.CongestionControl != "" {
base["congestion_control"] = nc.CongestionControl
}
tls := buildTLSConfig(nc, certFile, keyFile)
// TUIC requires ALPN for QUIC negotiation; default to h3 if not set by panel.
if _, ok := tls["alpn"]; !ok {
tls["alpn"] = []string{"h3"}
}
base["tls"] = tls
return base
}
func buildAnyTLS(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "anytls"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"name": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
if nc.PaddingScheme != "" {
base["padding_scheme"] = string(nc.PaddingScheme)
}
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
return base
}
func buildNaive(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "naive"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"username": strconv.Itoa(u.ID),
"password": u.UUID,
})
}
base["users"] = userList
if nc.TLS == 1 {
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
}
return base
}
func buildSocks(base M, users []panel.User) M {
base["type"] = "socks"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"username": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
return base
}
func buildHTTP(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["type"] = "http"
userList := make([]M, 0, len(users))
for _, u := range users {
userList = append(userList, M{
"username": u.UUID,
"password": u.UUID,
})
}
base["users"] = userList
if nc.TLS == 1 {
base["tls"] = buildTLSConfig(nc, certFile, keyFile)
}
return base
}
func applyTransport(base M, nc *panel.NodeConfig) {
if nc.Network == "" || nc.Network == "tcp" {
return
}
transport := M{"type": nc.Network}
if nc.NetworkSettings != nil {
switch nc.Network {
case "ws":
if v, ok := nc.NetworkSettings["path"]; ok {
transport["path"] = v
}
if v, ok := nc.NetworkSettings["headers"]; ok {
transport["headers"] = v
}
if v, ok := nc.NetworkSettings["host"]; ok {
if transport["headers"] == nil {
transport["headers"] = M{"Host": v}
}
}
if v, ok := nc.NetworkSettings["max_early_data"]; ok {
transport["max_early_data"] = v
}
if v, ok := nc.NetworkSettings["early_data_header_name"]; ok {
transport["early_data_header_name"] = v
}
case "grpc":
if v, ok := nc.NetworkSettings["service_name"]; ok {
transport["service_name"] = v
}
case "httpupgrade":
if v, ok := nc.NetworkSettings["path"]; ok {
transport["path"] = v
}
if v, ok := nc.NetworkSettings["host"]; ok {
transport["host"] = v
}
case "h2", "http":
transport["type"] = "http"
if v, ok := nc.NetworkSettings["path"]; ok {
transport["path"] = v
}
if v, ok := nc.NetworkSettings["host"]; ok {
transport["host"] = v
}
}
}
base["transport"] = transport
}
func buildTLSConfig(nc *panel.NodeConfig, certFile, keyFile string) M {
tls := M{"enabled": true}
serverName := nc.ServerName
if serverName == "" && nc.Host != "" {
serverName = nc.Host
}
if serverName != "" {
tls["server_name"] = serverName
}
if nc.TLSSettings != nil {
if sn, ok := nc.TLSSettings["server_name"]; ok && sn != "" {
tls["server_name"] = sn
}
if alpn, ok := nc.TLSSettings["alpn"]; ok {
tls["alpn"] = alpn
}
}
if certFile != "" && keyFile != "" {
tls["certificate_path"] = certFile
tls["key_path"] = keyFile
}
return tls
}
func buildRealityConfig(nc *panel.NodeConfig) M {
tls := M{"enabled": true}
if nc.TLSSettings == nil {
return tls
}
reality := M{"enabled": true}
if pk, ok := nc.TLSSettings["private_key"]; ok {
reality["private_key"] = pk
}
if sid, ok := nc.TLSSettings["short_id"]; ok {
switch v := sid.(type) {
case string:
reality["short_id"] = []string{v}
case []interface{}:
reality["short_id"] = v
}
}
handshake := M{}
if dest, ok := nc.TLSSettings["dest"]; ok {
destStr := fmt.Sprintf("%v", dest)
parts := strings.SplitN(destStr, ":", 2)
handshake["server"] = parts[0]
if len(parts) == 2 {
if port, err := strconv.Atoi(parts[1]); err == nil {
handshake["server_port"] = port
}
} else {
handshake["server_port"] = 443
}
}
if sn, ok := nc.TLSSettings["server_name"]; ok {
handshake["server"] = sn
if _, exists := handshake["server_port"]; !exists {
handshake["server_port"] = 443
}
}
if len(handshake) > 0 {
reality["handshake"] = handshake
}
tls["reality"] = reality
return tls
}
+694
View File
@@ -0,0 +1,694 @@
package singbox
import (
"encoding/json"
"testing"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/panel"
)
var testUsers = []panel.User{
{ID: 1, UUID: "aaaaaaaa-1111-2222-3333-444444444444", SpeedLimit: 0, DeviceLimit: 0},
{ID: 2, UUID: "bbbbbbbb-5555-6666-7777-888888888888", SpeedLimit: 3, DeviceLimit: 2},
}
// --- Shadowsocks ---
func TestBuildInbound_Shadowsocks(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 111,
Cipher: "aes-128-gcm",
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "shadowsocks")
assertMapValue(t, inbound, "method", "aes-128-gcm")
assertMapValue(t, inbound, "listen_port", 111)
users := inbound["users"].([]M)
if len(users) != 2 {
t.Fatalf("users: got %d, want 2", len(users))
}
assertMapValue(t, users[0], "name", "aaaaaaaa-1111-2222-3333-444444444444")
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
}
func TestBuildInbound_Shadowsocks2022(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 222,
Cipher: "2022-blake3-aes-128-gcm",
ServerKey: "base64serverkey==",
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "method", "2022-blake3-aes-128-gcm")
assertMapValue(t, inbound, "password", "base64serverkey==")
}
func TestBuildInbound_ShadowsocksWithPlugin(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 333,
Cipher: "aes-128-gcm",
Plugin: "obfs-local",
PluginOpt: "obfs=http;obfs-host=example.com",
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "plugin", "obfs-local")
assertMapValue(t, inbound, "plugin_opts", "obfs=http;obfs-host=example.com")
}
// --- VMess ---
func TestBuildInbound_VMess(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 443,
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "vmess")
assertMapValue(t, inbound, "tag", "vmess-in")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "uuid", "aaaaaaaa-1111-2222-3333-444444444444")
assertMapValue(t, users[0], "alterId", 0)
}
func TestBuildInbound_VMess_WithTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 443,
TLS: 1,
ServerName: "example.com",
}
inbound := buildInbound(nc, testUsers, "/path/cert.pem", "/path/key.pem")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
assertMapValue(t, tls, "server_name", "example.com")
assertMapValue(t, tls, "certificate_path", "/path/cert.pem")
assertMapValue(t, tls, "key_path", "/path/key.pem")
}
func TestBuildInbound_VMess_NoTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 80,
TLS: 0,
}
inbound := buildInbound(nc, testUsers, "", "")
if _, exists := inbound["tls"]; exists {
t.Error("should not have TLS when tls=0")
}
}
func TestBuildInbound_VMess_WithWebSocket(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 80,
Network: "ws",
NetworkSettings: map[string]interface{}{
"path": "/ws",
"headers": map[string]interface{}{
"Host": "example.com",
},
},
}
inbound := buildInbound(nc, testUsers, "", "")
transport := inbound["transport"].(M)
assertMapValue(t, transport, "type", "ws")
assertMapValue(t, transport, "path", "/ws")
}
func TestBuildInbound_VMess_WithGRPC(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 443,
Network: "grpc",
NetworkSettings: map[string]interface{}{
"service_name": "mygrpc",
},
}
inbound := buildInbound(nc, testUsers, "", "")
transport := inbound["transport"].(M)
assertMapValue(t, transport, "type", "grpc")
assertMapValue(t, transport, "service_name", "mygrpc")
}
func TestBuildInbound_VMess_WithH2(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 443,
Network: "h2",
NetworkSettings: map[string]interface{}{
"path": "/h2path",
"host": "example.com",
},
}
inbound := buildInbound(nc, testUsers, "", "")
transport := inbound["transport"].(M)
assertMapValue(t, transport, "type", "http")
assertMapValue(t, transport, "path", "/h2path")
}
func TestBuildInbound_VMess_WithHTTPUpgrade(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vmess",
ServerPort: 443,
Network: "httpupgrade",
NetworkSettings: map[string]interface{}{
"path": "/upgrade",
"host": "example.com",
},
}
inbound := buildInbound(nc, testUsers, "", "")
transport := inbound["transport"].(M)
assertMapValue(t, transport, "type", "httpupgrade")
assertMapValue(t, transport, "path", "/upgrade")
assertMapValue(t, transport, "host", "example.com")
}
// --- VLESS ---
func TestBuildInbound_VLESS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "vless")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "uuid", "aaaaaaaa-1111-2222-3333-444444444444")
}
func TestBuildInbound_VLESS_WithFlow(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
Flow: "xtls-rprx-vision",
}
inbound := buildInbound(nc, testUsers, "", "")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "flow", "xtls-rprx-vision")
}
func TestBuildInbound_VLESS_Reality(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
TLS: 2,
TLSSettings: map[string]interface{}{
"private_key": "test-private-key",
"short_id": "abc123",
"dest": "www.example.com:443",
"server_name": "www.example.com",
},
}
inbound := buildInbound(nc, testUsers, "", "")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
reality := tls["reality"].(M)
assertMapValue(t, reality, "enabled", true)
assertMapValue(t, reality, "private_key", "test-private-key")
handshake := reality["handshake"].(M)
assertMapValue(t, handshake, "server", "www.example.com")
assertMapValue(t, handshake, "server_port", 443)
}
func TestBuildInbound_VLESS_Reality_ShortIDArray(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
TLS: 2,
TLSSettings: map[string]interface{}{
"private_key": "pk",
"short_id": []interface{}{"id1", "id2"},
"dest": "example.com",
},
}
inbound := buildInbound(nc, testUsers, "", "")
tls := inbound["tls"].(M)
reality := tls["reality"].(M)
ids := reality["short_id"].([]interface{})
if len(ids) != 2 {
t.Errorf("short_id: got %d entries, want 2", len(ids))
}
}
// --- Trojan ---
func TestBuildInbound_Trojan(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "trojan",
ServerPort: 443,
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "trojan")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
}
func TestBuildInbound_Trojan_NoTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "trojan",
ServerPort: 80,
TLS: 0,
}
inbound := buildInbound(nc, testUsers, "", "")
if _, exists := inbound["tls"]; exists {
t.Error("trojan with tls=0 should not have TLS config")
}
}
func TestBuildInbound_Trojan_WithTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "trojan",
ServerPort: 443,
TLS: 1,
ServerName: "example.com",
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
}
// --- Hysteria ---
func TestBuildInbound_Hysteria2(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "hysteria",
ServerPort: 444,
Version: 2,
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
assertMapValue(t, inbound, "type", "hysteria2")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
}
func TestBuildInbound_Hysteria2_WithObfs(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "hysteria",
ServerPort: 444,
Version: 2,
Obfs: "salamander",
ObfsPassword: "secret",
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
obfs := inbound["obfs"].(M)
assertMapValue(t, obfs, "type", "salamander")
assertMapValue(t, obfs, "password", "secret")
}
func TestBuildInbound_Hysteria1(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "hysteria",
ServerPort: 444,
Version: 0,
UpMbps: 100,
DownMbps: 200,
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
assertMapValue(t, inbound, "type", "hysteria")
assertMapValue(t, inbound, "up_mbps", 100)
assertMapValue(t, inbound, "down_mbps", 200)
users := inbound["users"].([]M)
assertMapValue(t, users[0], "auth_str", "aaaaaaaa-1111-2222-3333-444444444444")
}
// --- TUIC ---
func TestBuildInbound_TUIC(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "tuic",
ServerPort: 555,
CongestionControl: "bbr",
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
assertMapValue(t, inbound, "type", "tuic")
assertMapValue(t, inbound, "congestion_control", "bbr")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "uuid", "aaaaaaaa-1111-2222-3333-444444444444")
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
}
// --- AnyTLS ---
func TestBuildInbound_AnyTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "anytls",
ServerPort: 443,
PaddingScheme: "stop=8\n0=30-30",
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
assertMapValue(t, inbound, "type", "anytls")
assertMapValue(t, inbound, "padding_scheme", "stop=8\n0=30-30")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
}
func TestBuildInbound_AnyTLS_NoPaddingScheme(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "anytls",
ServerPort: 443,
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
if _, exists := inbound["padding_scheme"]; exists {
t.Error("should not have padding_scheme when empty")
}
}
// --- Naive ---
func TestBuildInbound_Naive(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "naive",
ServerPort: 443,
TLS: 1,
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
assertMapValue(t, inbound, "type", "naive")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "username", "1")
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
}
// --- Socks ---
func TestBuildInbound_Socks(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "socks",
ServerPort: 1080,
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "socks")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "username", "aaaaaaaa-1111-2222-3333-444444444444")
assertMapValue(t, users[0], "password", "aaaaaaaa-1111-2222-3333-444444444444")
if _, exists := inbound["tls"]; exists {
t.Error("socks should not have TLS")
}
}
// --- HTTP ---
func TestBuildInbound_HTTP(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "http",
ServerPort: 8080,
}
inbound := buildInbound(nc, testUsers, "", "")
assertMapValue(t, inbound, "type", "http")
users := inbound["users"].([]M)
assertMapValue(t, users[0], "username", "aaaaaaaa-1111-2222-3333-444444444444")
}
func TestBuildInbound_HTTP_WithTLS(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "http",
ServerPort: 443,
TLS: 1,
}
inbound := buildInbound(nc, testUsers, "/c.pem", "/k.pem")
tls := inbound["tls"].(M)
assertMapValue(t, tls, "enabled", true)
}
// --- Unknown protocol ---
func TestBuildInbound_Unknown(t *testing.T) {
nc := &panel.NodeConfig{
Protocol: "unknown-proto",
ServerPort: 999,
}
inbound := buildInbound(nc, testUsers, "", "")
if inbound != nil {
t.Errorf("unknown protocol should return nil, got %v", inbound)
}
}
// --- buildConfig ---
func TestBuildConfig(t *testing.T) {
kcfg := config.KernelConfig{LogLevel: "info"}
nc := &panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 111,
Cipher: "aes-128-gcm",
}
cfg := buildConfig(kcfg, nc, testUsers, "", "")
data, err := json.Marshal(cfg)
if err != nil {
t.Fatalf("marshal config: %v", err)
}
if len(data) == 0 {
t.Error("empty JSON output")
}
logCfg := cfg["log"].(M)
assertMapValue(t, logCfg, "level", "info")
assertMapValue(t, logCfg, "timestamp", true)
outbounds := cfg["outbounds"].([]M)
if len(outbounds) != 2 {
t.Errorf("outbounds: got %d, want 2", len(outbounds))
}
assertMapValue(t, outbounds[0], "type", "direct")
assertMapValue(t, outbounds[1], "type", "block")
inbounds := cfg["inbounds"].([]M)
if len(inbounds) != 1 {
t.Fatalf("inbounds: got %d, want 1", len(inbounds))
}
assertMapValue(t, inbounds[0], "type", "shadowsocks")
}
func TestBuildConfig_AllProtocols_ValidJSON(t *testing.T) {
protocols := []struct {
name string
nc *panel.NodeConfig
}{
{"shadowsocks", &panel.NodeConfig{Protocol: "shadowsocks", ServerPort: 111, Cipher: "aes-128-gcm"}},
{"vmess", &panel.NodeConfig{Protocol: "vmess", ServerPort: 443}},
{"vless", &panel.NodeConfig{Protocol: "vless", ServerPort: 443}},
{"trojan", &panel.NodeConfig{Protocol: "trojan", ServerPort: 443}},
{"hysteria2", &panel.NodeConfig{Protocol: "hysteria", ServerPort: 444, Version: 2}},
{"hysteria1", &panel.NodeConfig{Protocol: "hysteria", ServerPort: 444, Version: 1}},
{"tuic", &panel.NodeConfig{Protocol: "tuic", ServerPort: 555}},
{"anytls", &panel.NodeConfig{Protocol: "anytls", ServerPort: 443}},
{"naive", &panel.NodeConfig{Protocol: "naive", ServerPort: 443}},
{"socks", &panel.NodeConfig{Protocol: "socks", ServerPort: 1080}},
{"http", &panel.NodeConfig{Protocol: "http", ServerPort: 8080}},
}
for _, tc := range protocols {
t.Run(tc.name, func(t *testing.T) {
cfg := buildConfig(config.KernelConfig{LogLevel: "warn"}, tc.nc, testUsers, "/c.pem", "/k.pem")
data, err := json.Marshal(cfg)
if err != nil {
t.Fatalf("marshal %s: %v", tc.name, err)
}
var parsed map[string]interface{}
if err := json.Unmarshal(data, &parsed); err != nil {
t.Fatalf("round-trip %s: %v", tc.name, err)
}
})
}
}
// --- Routes ---
func TestBuildRoutes_Default(t *testing.T) {
route := buildRoutes(nil, nil)
assertMapValue(t, route, "final", "direct")
rules := route["rules"].([]M)
if len(rules) < 2 {
t.Fatalf("expected at least 2 default rules, got %d", len(rules))
}
assertMapValue(t, rules[0], "outbound", "block")
assertMapValue(t, rules[1], "outbound", "block")
}
func TestBuildRoutes_WithCustomRules(t *testing.T) {
rules := []panel.RouteRule{
{ID: 1, Match: []string{"blocked.com"}, Action: "block"},
{ID: 2, Match: []string{"10.0.0.0/8"}, Action: "block"},
{ID: 3, Match: []string{"allowed.com"}, Action: "direct"},
}
route := buildRoutes(rules, nil)
allRules := route["rules"].([]M)
if len(allRules) != 5 {
t.Fatalf("rules count: got %d, want 5", len(allRules))
}
assertMapValue(t, allRules[2], "outbound", "block")
if _, ok := allRules[2]["domain_suffix"]; !ok {
t.Error("domain rule should use domain_suffix")
}
assertMapValue(t, allRules[3], "outbound", "block")
if _, ok := allRules[3]["ip_cidr"]; !ok {
t.Error("IP rule should use ip_cidr")
}
}
func TestBuildRoutes_MultiMatch(t *testing.T) {
// A single route with mixed domain + CIDR matches should produce separate rules
// Wildcard *.evil.com should become evil.com for domain_suffix
rules := []panel.RouteRule{
{ID: 1, Match: []string{"*.evil.com", "bad.org", "192.168.1.0/24"}, Action: "block"},
{ID: 2, Match: []string{"*.bypass.com"}, Action: "direct"},
}
route := buildRoutes(rules, nil)
allRules := route["rules"].([]M)
// 2 default private-IP rules + 1 domain rule + 1 CIDR rule + 1 domain rule = 5
if len(allRules) != 5 {
t.Fatalf("rules count: got %d, want 5", len(allRules))
}
// Rule #2 (index 2): domains from first route (wildcards stripped)
domains := allRules[2]["domain_suffix"].([]string)
if len(domains) != 2 || domains[0] != "evil.com" || domains[1] != "bad.org" {
t.Errorf("domain_suffix: got %v, want [evil.com bad.org]", domains)
}
assertMapValue(t, allRules[2], "outbound", "block")
// Rule #3 (index 3): CIDRs from first route
cidrs := allRules[3]["ip_cidr"].([]string)
if len(cidrs) != 1 || cidrs[0] != "192.168.1.0/24" {
t.Errorf("ip_cidr: got %v, want [192.168.1.0/24]", cidrs)
}
assertMapValue(t, allRules[3], "outbound", "block")
// Rule #4 (index 4): direct rule (wildcard stripped)
directDomains := allRules[4]["domain_suffix"].([]string)
if len(directDomains) != 1 || directDomains[0] != "bypass.com" {
t.Errorf("direct domain_suffix: got %v, want [bypass.com]", directDomains)
}
assertMapValue(t, allRules[4], "outbound", "direct")
}
// --- TLS Config ---
func TestBuildTLSConfig_WithCert(t *testing.T) {
nc := &panel.NodeConfig{ServerName: "example.com"}
tls := buildTLSConfig(nc, "/cert.pem", "/key.pem")
assertMapValue(t, tls, "enabled", true)
assertMapValue(t, tls, "server_name", "example.com")
assertMapValue(t, tls, "certificate_path", "/cert.pem")
assertMapValue(t, tls, "key_path", "/key.pem")
}
func TestBuildTLSConfig_NoCert(t *testing.T) {
nc := &panel.NodeConfig{ServerName: "example.com"}
tls := buildTLSConfig(nc, "", "")
assertMapValue(t, tls, "enabled", true)
if _, exists := tls["certificate_path"]; exists {
t.Error("should not have certificate_path when empty")
}
}
func TestBuildTLSConfig_FallbackToHost(t *testing.T) {
nc := &panel.NodeConfig{Host: "fallback.com"}
tls := buildTLSConfig(nc, "", "")
assertMapValue(t, tls, "server_name", "fallback.com")
}
func TestBuildTLSConfig_TLSSettingsOverride(t *testing.T) {
nc := &panel.NodeConfig{
ServerName: "original.com",
TLSSettings: map[string]interface{}{
"server_name": "override.com",
},
}
tls := buildTLSConfig(nc, "", "")
assertMapValue(t, tls, "server_name", "override.com")
}
// --- Transport ---
func TestApplyTransport_TCP(t *testing.T) {
base := M{}
nc := &panel.NodeConfig{Network: "tcp"}
applyTransport(base, nc)
if _, exists := base["transport"]; exists {
t.Error("tcp should not add transport")
}
}
func TestApplyTransport_Empty(t *testing.T) {
base := M{}
nc := &panel.NodeConfig{Network: ""}
applyTransport(base, nc)
if _, exists := base["transport"]; exists {
t.Error("empty network should not add transport")
}
}
func TestApplyTransport_WS_MaxEarlyData(t *testing.T) {
base := M{}
nc := &panel.NodeConfig{
Network: "ws",
NetworkSettings: map[string]interface{}{
"path": "/ws",
"max_early_data": 2048,
"early_data_header_name": "Sec-WebSocket-Protocol",
},
}
applyTransport(base, nc)
transport := base["transport"].(M)
assertMapValue(t, transport, "max_early_data", 2048)
assertMapValue(t, transport, "early_data_header_name", "Sec-WebSocket-Protocol")
}
// --- helpers ---
func assertMapValue(t *testing.T, m M, key string, expected interface{}) {
t.Helper()
val, ok := m[key]
if !ok {
t.Errorf("key %q not found in map", key)
return
}
switch e := expected.(type) {
case int:
switch v := val.(type) {
case int:
if v != e {
t.Errorf("%s: got %d, want %d", key, v, e)
}
case float64:
if int(v) != e {
t.Errorf("%s: got %v, want %d", key, v, e)
}
default:
t.Errorf("%s: got %v (type %T), want %d", key, val, val, e)
}
default:
if val != expected {
t.Errorf("%s: got %v, want %v", key, val, expected)
}
}
}
+402
View File
@@ -0,0 +1,402 @@
package singbox
import (
"context"
"io"
"log/slog"
"net"
"strconv"
"sync"
"sync/atomic"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/buf"
singM "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"golang.org/x/time/rate"
"github.com/cedar2025/xboard-node/internal/kernel"
)
// connRecord holds the minimal per-connection state we need.
// Each field is accessed by multiple goroutines; upload/download use atomics.
type connRecord struct {
id string
userID int
sourceIP string
upload atomic.Int64
download atomic.Int64
conn net.Conn // kept for force-close via CloseByID
}
func (r *connRecord) toConnection() kernel.Connection {
return kernel.Connection{
ID: r.id,
UserID: r.userID,
SourceIP: r.sourceIP,
Upload: r.upload.Load(),
Download: r.download.Load(),
}
}
// ConnTracker is a lightweight in-process connection tracker that replaces
// the Clash API TrafficManager. It handles:
// - per-connection byte counting (upload / download)
// - optional per-user speed limiting (when SetSpeedLimitFunc is configured)
// - source IP tracking for device-limit enforcement
// - force-close a connection by ID
//
// Both byte counting and speed limiting are implemented via sing's CountFunc
// callback mechanism (ReadCounter / WriteCounter interfaces). This allows
// sing's copy pipeline to use splice / ReadWaiter zero-copy paths while
// still counting bytes and enforcing bandwidth limits through post-transfer
// callbacks.
//
// Memory model:
// - active: live connections, looked up by ID for snapshots and CloseByID
// - pending: closed connections accumulated since the last Snapshot call,
// drained atomically so the Tracker sees every connection's final bytes
//
// Thread safety: active is guarded by mu; pending by pendingMu.
type ConnTracker struct {
mu sync.RWMutex
active map[string]*connRecord
// idCounter generates unique connection IDs without crypto/rand.
idCounter atomic.Int64
pendingMu sync.Mutex
pending []*connRecord
maxPending int
userMapMu sync.RWMutex
userMap map[string]int // UUID → userID
// speedLimitFunc resolves a user UUID to a *rate.Limiter. When set,
// new connections for users with a speed limit are automatically
// rate-limited without needing a second ConnectionTracker wrapper.
// The function must be safe for concurrent calls.
speedLimitFunc func(uuid string) *rate.Limiter
}
// NewConnTracker creates a tracker.
func NewConnTracker(maxPending int) *ConnTracker {
if maxPending <= 0 {
maxPending = 50000
}
return &ConnTracker{
active: make(map[string]*connRecord),
maxPending: maxPending,
userMap: make(map[string]int),
}
}
// SetSpeedLimitFunc configures the per-user speed limit lookup.
// fn is called for every new connection; it must return nil for unlimited users.
// Thread-safe: fn itself must be safe for concurrent calls.
func (t *ConnTracker) SetSpeedLimitFunc(fn func(uuid string) *rate.Limiter) {
t.speedLimitFunc = fn
}
// SetUserMap replaces the UUID→userID mapping. Must be called whenever the
// user list changes so that new connections are attributed to the correct user.
func (t *ConnTracker) SetUserMap(m map[string]int) {
t.userMapMu.Lock()
t.userMap = m
t.userMapMu.Unlock()
}
// ─── adapter.ConnectionTracker ───────────────────────────────────────────────
// RoutedConnection wraps a TCP conn to count bytes, track lifecycle,
// and optionally rate-limit per-user bandwidth.
func (t *ConnTracker) RoutedConnection(
ctx context.Context, conn net.Conn,
metadata adapter.InboundContext,
_ adapter.Rule, _ adapter.Outbound,
) net.Conn {
rec := t.allocRecord(metadata, conn)
var lim *rate.Limiter
if t.speedLimitFunc != nil {
lim = t.speedLimitFunc(metadata.User)
}
return &trackedConn{Conn: conn, rec: rec, tracker: t, limiter: lim, ctx: ctx}
}
// RoutedPacketConnection wraps a UDP PacketConn to count bytes, track lifecycle,
// and optionally rate-limit per-user bandwidth.
func (t *ConnTracker) RoutedPacketConnection(
ctx context.Context, conn N.PacketConn,
metadata adapter.InboundContext,
_ adapter.Rule, _ adapter.Outbound,
) N.PacketConn {
rec := t.allocRecord(metadata, nil)
var lim *rate.Limiter
if t.speedLimitFunc != nil {
lim = t.speedLimitFunc(metadata.User)
}
return &trackedPacketConn{PacketConn: conn, rec: rec, tracker: t, limiter: lim, ctx: ctx}
}
// ─── Internal helpers ────────────────────────────────────────────────────────
func (t *ConnTracker) allocRecord(metadata adapter.InboundContext, conn net.Conn) *connRecord {
// Use a fast atomic counter instead of crypto/rand UUID generation.
// Under connection floods (thousands of concurrent handshakes), uuid.NewV4()
// becomes a syscall bottleneck; a counter is allocation-free and lock-free.
id := strconv.FormatInt(t.idCounter.Add(1), 36)
t.userMapMu.RLock()
uid := t.userMap[metadata.User]
t.userMapMu.RUnlock()
rec := &connRecord{
id: id,
userID: uid,
sourceIP: metadata.Source.Addr.String(),
conn: conn,
}
t.mu.Lock()
t.active[rec.id] = rec
t.mu.Unlock()
return rec
}
// closeRecord is idempotent: if the record has already been moved to pending,
// the second call is a no-op.
func (t *ConnTracker) closeRecord(rec *connRecord) {
t.mu.Lock()
_, present := t.active[rec.id]
if present {
delete(t.active, rec.id)
}
t.mu.Unlock()
if !present {
return
}
t.pendingMu.Lock()
if len(t.pending) < t.maxPending {
t.pending = append(t.pending, rec)
} else {
slog.Warn("conntracker: pending buffer full, dropping closed connection",
"max_pending", t.maxPending, "user_id", rec.userID)
}
t.pendingMu.Unlock()
}
// ─── Public API ──────────────────────────────────────────────────────────────
// Snapshot returns a point-in-time view of all active connections plus all
// connections that closed since the previous Snapshot call. The pending
// (closed) list is drained atomically so callers never miss a connection.
func (t *ConnTracker) Snapshot() []kernel.Connection {
// Drain pending under its own lock (hot path for closed connections).
t.pendingMu.Lock()
pending := t.pending
t.pending = nil
t.pendingMu.Unlock()
// Two-phase read to minimise RLock hold time at high connection counts:
// Phase 1 (under RLock): collect record *pointers* — a pointer copy is
// ~8 bytes vs ~80 bytes for a full Connection struct, and no atomic loads.
// Phase 2 (lock-free): call toConnection() which atomically reads the
// upload/download counters. Atomics are safe without any mutex.
// This shrinks the RLock window proportionally as connection count grows,
// reducing serialisation against concurrent allocRecord/closeRecord calls.
t.mu.RLock()
ptrs := make([]*connRecord, 0, len(t.active))
for _, rec := range t.active {
ptrs = append(ptrs, rec)
}
t.mu.RUnlock()
result := make([]kernel.Connection, 0, len(ptrs)+len(pending))
for _, rec := range ptrs {
result = append(result, rec.toConnection())
}
for _, rec := range pending {
result = append(result, rec.toConnection())
}
return result
}
// CloseByID force-closes a live connection by its ID.
// Returns true if the connection was found and closed.
func (t *ConnTracker) CloseByID(id string) bool {
t.mu.RLock()
rec, ok := t.active[id]
t.mu.RUnlock()
if !ok {
return false
}
if rec.conn != nil {
rec.conn.Close()
}
return true
}
// ActiveCount returns the number of currently tracked live connections.
func (t *ConnTracker) ActiveCount() int {
t.mu.RLock()
defer t.mu.RUnlock()
return len(t.active)
}
// ─── trackedConn (TCP) ───────────────────────────────────────────────────────
type trackedConn struct {
net.Conn
rec *connRecord
tracker *ConnTracker
limiter *rate.Limiter // nil = unlimited
ctx context.Context // for rate limiter WaitN cancellation
}
// Read is the fallback path used when the copy pipeline cannot unwrap this
// wrapper (should not happen with current code, but kept for robustness).
func (c *trackedConn) Read(b []byte) (int, error) {
if c.limiter != nil {
if burst := c.limiter.Burst(); len(b) > burst {
b = b[:burst]
}
}
n, err := c.Conn.Read(b)
if n > 0 {
c.rec.download.Add(int64(n))
if c.limiter != nil {
waitN := min(n, c.limiter.Burst())
_ = c.limiter.WaitN(c.ctx, waitN)
}
}
return n, err
}
// Write is the fallback path.
func (c *trackedConn) Write(b []byte) (int, error) {
if c.limiter != nil {
burst := c.limiter.Burst()
var n int
for n < len(b) {
chunk := min(len(b)-n, burst)
_ = c.limiter.WaitN(c.ctx, chunk)
written, writeErr := c.Conn.Write(b[n : n+chunk])
n += written
c.rec.upload.Add(int64(written))
if writeErr != nil {
return n, writeErr
}
}
return n, nil
}
n, err := c.Conn.Write(b)
if n > 0 {
c.rec.upload.Add(int64(n))
}
return n, err
}
func (c *trackedConn) Close() error {
c.tracker.closeRecord(c.rec)
return c.Conn.Close()
}
// makeCountFunc builds a CountFunc that does byte accounting and optional
// rate limiting in a single callback. The direction is selected by the
// counter pointer (download or upload).
func (c *trackedConn) makeCountFunc(counter *atomic.Int64) N.CountFunc {
if c.limiter == nil {
// Fast path: no rate limiting, just atomic add.
return func(n int64) { counter.Add(n) }
}
return func(n int64) {
counter.Add(n)
waitN := int(n)
if burst := c.limiter.Burst(); waitN > burst {
waitN = burst
}
_ = c.limiter.WaitN(c.ctx, waitN)
}
}
// UnwrapReader implements N.ReadCounter — lets the copy pipeline strip this
// wrapper, collect a counter callback, and reach the inner conn for
// splice / ReadWaiter zero-copy transfer.
func (c *trackedConn) UnwrapReader() (io.Reader, []N.CountFunc) {
return c.Conn, []N.CountFunc{c.makeCountFunc(&c.rec.download)}
}
// UnwrapWriter implements N.WriteCounter.
func (c *trackedConn) UnwrapWriter() (io.Writer, []N.CountFunc) {
return c.Conn, []N.CountFunc{c.makeCountFunc(&c.rec.upload)}
}
func (c *trackedConn) Upstream() any { return c.Conn }
func (c *trackedConn) ReaderReplaceable() bool { return true }
func (c *trackedConn) WriterReplaceable() bool { return true }
// ─── trackedPacketConn (UDP / QUIC) ──────────────────────────────────────────
type trackedPacketConn struct {
N.PacketConn
rec *connRecord
tracker *ConnTracker
limiter *rate.Limiter
ctx context.Context
}
func (c *trackedPacketConn) ReadPacket(buffer *buf.Buffer) (singM.Socksaddr, error) {
dest, err := c.PacketConn.ReadPacket(buffer)
if err == nil {
n := int64(buffer.Len())
c.rec.download.Add(n)
if c.limiter != nil {
waitN := int(min(n, int64(c.limiter.Burst())))
_ = c.limiter.WaitN(c.ctx, waitN)
}
}
return dest, err
}
func (c *trackedPacketConn) WritePacket(buffer *buf.Buffer, dest singM.Socksaddr) error {
n := int64(buffer.Len())
err := c.PacketConn.WritePacket(buffer, dest)
if err == nil {
c.rec.upload.Add(n)
if c.limiter != nil {
waitN := int(min(n, int64(c.limiter.Burst())))
_ = c.limiter.WaitN(c.ctx, waitN)
}
}
return err
}
func (c *trackedPacketConn) Close() error {
c.tracker.closeRecord(c.rec)
return c.PacketConn.Close()
}
func (c *trackedPacketConn) makeCountFunc(counter *atomic.Int64) N.CountFunc {
if c.limiter == nil {
return func(n int64) { counter.Add(n) }
}
return func(n int64) {
counter.Add(n)
waitN := int(n)
if burst := c.limiter.Burst(); waitN > burst {
waitN = burst
}
_ = c.limiter.WaitN(c.ctx, waitN)
}
}
func (c *trackedPacketConn) UnwrapPacketReader() (N.PacketReader, []N.CountFunc) {
return c.PacketConn, []N.CountFunc{c.makeCountFunc(&c.rec.download)}
}
func (c *trackedPacketConn) UnwrapPacketWriter() (N.PacketWriter, []N.CountFunc) {
return c.PacketConn, []N.CountFunc{c.makeCountFunc(&c.rec.upload)}
}
func (c *trackedPacketConn) Upstream() any { return c.PacketConn }
func (c *trackedPacketConn) ReaderReplaceable() bool { return true }
func (c *trackedPacketConn) WriterReplaceable() bool { return true }
+348
View File
@@ -0,0 +1,348 @@
package singbox
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"sync"
"time"
box "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/include"
singLog "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/auth"
singJSON "github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/service"
"golang.org/x/time/rate"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/panel"
)
// drainTimeout is how long stop() waits for in-flight connections to finish
// naturally after closing all listen sockets, before hard-killing them.
const drainTimeout = 5 * time.Second
// SingBox implements kernel.Kernel by embedding sing-box as a Go library.
//
// Connection tracking is done entirely in-process via ConnTracker, which is
// registered as an adapter.ConnectionTracker with sing-box's router. This
// avoids the HTTP/JSON overhead of the Clash API (saves 5-15 MB at 500+
// concurrent connections).
type SingBox struct {
cfg config.KernelConfig
mu sync.Mutex
box *box.Box
ctx context.Context
cancel context.CancelFunc
// connTracker is our lightweight in-process byte/IP tracker.
// Created fresh on every ApplyConfig (full restart).
// Survives ReloadUsers (hot-swap) since live connections persist.
connTracker *ConnTracker
// speedLimitFunc resolves a user UUID to a *rate.Limiter.
// Set once by SetSpeedLimitFunc and forwarded to every new ConnTracker.
speedLimitFunc func(string) *rate.Limiter
// trackerRegistered prevents duplicate AppendTracker calls on the same
// Router instance during ReloadUsers. Reset to false on full restart.
trackerRegistered bool
}
func New(cfg config.KernelConfig) *SingBox {
return &SingBox{cfg: cfg}
}
func (s *SingBox) Name() string { return "sing-box" }
func (s *SingBox) ApplyConfig(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error {
s.mu.Lock()
defer s.mu.Unlock()
cfgMap := buildConfig(s.cfg, nodeConfig, users, certFile, keyFile)
data, err := json.Marshal(cfgMap)
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
slog.Debug("sing-box config generated", "len", len(data))
ctx, cancel := context.WithCancel(context.Background())
ctx = include.Context(ctx)
opts, err := singJSON.UnmarshalExtendedContext[option.Options](ctx, data)
if err != nil {
cancel()
return fmt.Errorf("parse sing-box options: %w", err)
}
s.stop()
instance, err := box.New(box.Options{
Context: ctx,
Options: opts,
})
if err != nil {
cancel()
return fmt.Errorf("create sing-box instance: %w", err)
}
if err := instance.Start(); err != nil {
instance.Close()
cancel()
return fmt.Errorf("start sing-box: %w", err)
}
s.box = instance
s.ctx = ctx
s.cancel = cancel
// Fresh tracker on full restart (all connections were terminated by s.stop).
s.connTracker = NewConnTracker(0)
s.connTracker.SetUserMap(buildUserMap(users))
if s.speedLimitFunc != nil {
s.connTracker.SetSpeedLimitFunc(s.speedLimitFunc)
}
s.trackerRegistered = false
s.registerTracker(ctx)
slog.Info("sing-box started (in-process)", "users", len(users))
return nil
}
// Reload hot-swaps the inbound users and routing rules without restarting the box.
// Routes, outbounds, and the connTracker stay alive so in-flight connections
// continue to be tracked correctly.
func (s *SingBox) Reload(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.box == nil {
return fmt.Errorf("not running")
}
cfgMap := buildConfig(s.cfg, nodeConfig, users, certFile, keyFile)
data, err := json.Marshal(cfgMap)
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
opts, err := singJSON.UnmarshalExtendedContext[option.Options](s.ctx, data)
if err != nil {
return fmt.Errorf("parse options: %w", err)
}
im := service.FromContext[adapter.InboundManager](s.ctx)
if im == nil {
return fmt.Errorf("inbound manager not available")
}
router := service.FromContext[adapter.Router](s.ctx)
if router == nil {
return fmt.Errorf("router not available")
}
// Update routing rules
if err := router.UpdateRules(opts.Route.Rules, opts.Route.RuleSet); err != nil {
slog.Warn("routing reload failed", "error", err)
} else {
slog.Info("sing-box routing reloaded")
}
nopFactory := singLog.NewNOPFactory()
for _, inb := range opts.Inbounds {
tag := inb.Tag
if existing, ok := im.Get(tag); ok && existing.Type() == inb.Type {
var err error
switch v := existing.(type) {
case adapter.UpdatableInbound[option.VMessUser]:
if opts, ok := inb.Options.(*option.VMessInboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableInbound[option.VLESSUser]:
if opts, ok := inb.Options.(*option.VLESSInboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableInbound[option.TrojanUser]:
if opts, ok := inb.Options.(*option.TrojanInboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableInbound[option.Hysteria2User]:
if opts, ok := inb.Options.(*option.Hysteria2InboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableShadowsocksInbound:
if opts, ok := inb.Options.(*option.ShadowsocksInboundOptions); ok {
err = v.UpdateUsersByOptions(opts.Users)
}
case adapter.UpdatableInbound[option.TUICUser]:
if opts, ok := inb.Options.(*option.TUICInboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableInbound[option.AnyTLSUser]:
if opts, ok := inb.Options.(*option.AnyTLSInboundOptions); ok {
err = v.UpdateUsers(opts.Users)
}
case adapter.UpdatableInbound[auth.User]:
switch opts := inb.Options.(type) {
case *option.NaiveInboundOptions:
err = v.UpdateUsers(opts.Users)
case *option.SocksInboundOptions:
err = v.UpdateUsers(opts.Users)
case *option.HTTPMixedInboundOptions:
err = v.UpdateUsers(opts.Users)
}
}
if err == nil {
continue
}
slog.Warn("incremental update failed, falling back to recreate", "tag", tag, "error", err)
}
// Remove the existing inbound first so im.Create() can bind the same port.
// im.Create() cannot atomically swap a TCP listener — it tries to start the
// new socket before the old one is closed, causing "address already in use".
// The brief listen gap (< 1 ms) is far less disruptive than a full restart.
_ = im.Remove(tag) // ignore error when tag doesn't exist yet
logger := nopFactory.NewLogger(fmt.Sprintf("inbound/%s[%s]", inb.Type, tag))
if err := im.Create(s.ctx, router, logger, tag, inb.Type, inb.Options); err != nil {
return fmt.Errorf("recreate inbound %s: %w", tag, err)
}
}
// Trackers remain registered on the Router (which survives ReloadUsers).
// Only update the user map — do NOT re-register or traffic is double-counted.
if s.connTracker != nil {
s.connTracker.SetUserMap(buildUserMap(users))
}
slog.Info("sing-box reloaded (users and routes hot-swapped)", "users", len(users))
return nil
}
// registerTracker wires the ConnTracker to the current Router exactly once.
// The ConnTracker handles both byte counting and optional rate limiting
// in a single wrapper, so no additional trackers are needed.
func (s *SingBox) registerTracker(ctx context.Context) {
if s.trackerRegistered {
return
}
router := service.FromContext[adapter.Router](ctx)
if router == nil {
return
}
if s.connTracker != nil {
router.AppendTracker(s.connTracker)
}
s.trackerRegistered = true
}
func (s *SingBox) Stop() {
s.mu.Lock()
defer s.mu.Unlock()
s.stop()
}
func (s *SingBox) stop() {
if s.box == nil {
return
}
// Step 1: close all listen sockets so no new connections are accepted.
// im.Close() sets m.started=false so the subsequent box.Close() call is a no-op
// for inbounds (avoids double-close), but box.Close() still tears down routers
// and outbounds.
if im := service.FromContext[adapter.InboundManager](s.ctx); im != nil {
_ = im.Close()
}
// Step 2: wait for in-flight connections to drain naturally (best-effort).
if s.connTracker != nil {
deadline := time.Now().Add(drainTimeout)
for time.Now().Before(deadline) {
if s.connTracker.ActiveCount() == 0 {
break
}
time.Sleep(100 * time.Millisecond)
}
}
// Step 3: hard-close everything that is still open.
s.box.Close()
s.box = nil
if s.cancel != nil {
s.cancel()
s.cancel = nil
}
s.ctx = nil
}
func (s *SingBox) IsRunning() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.box != nil
}
// connTrackerLocked returns the connTracker under a read-optimised lock.
func (s *SingBox) connTrackerSafe() *ConnTracker {
s.mu.Lock()
ct := s.connTracker
s.mu.Unlock()
return ct
}
// SetSpeedLimitFunc configures per-user bandwidth throttling.
// The function is forwarded to the ConnTracker, which embeds the rate limiter
// in each tracked connection wrapper. This merges byte counting and rate limiting
// into a single CountFunc callback, allowing splice/zero-copy paths.
func (s *SingBox) SetSpeedLimitFunc(fn func(uuid string) *rate.Limiter) {
s.mu.Lock()
defer s.mu.Unlock()
s.speedLimitFunc = fn
if s.connTracker != nil {
s.connTracker.SetSpeedLimitFunc(fn)
}
}
// GetConnections returns a snapshot of all active and recently-closed
// connections directly from the in-process ConnTracker.
func (s *SingBox) GetConnections(_ context.Context) ([]kernel.Connection, error) {
ct := s.connTrackerSafe()
if ct == nil {
return nil, nil
}
conns := ct.Snapshot()
slog.Debug("GetConnections snapshot", "count", len(conns))
return conns, nil
}
// CloseConnection force-closes a specific connection by its ID.
func (s *SingBox) CloseConnection(_ context.Context, connID string) error {
ct := s.connTrackerSafe()
if ct == nil {
return fmt.Errorf("not running")
}
if !ct.CloseByID(connID) {
slog.Debug("CloseConnection: connection not found (already closed?)", "id", connID)
}
return nil
}
// buildUserMap creates a UUID→userID mapping used by ConnTracker to attribute
// connections to the correct user. All sing-box protocols use the user's UUID
// as the inbound name/username, so this covers every protocol.
func buildUserMap(users []panel.User) map[string]int {
m := make(map[string]int, len(users))
for _, u := range users {
m[u.UUID] = u.ID
}
return m
}
+553
View File
@@ -0,0 +1,553 @@
package xray
import (
"fmt"
"log/slog"
"strings"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/panel"
)
// M is a shorthand for building JSON-like maps
type M = map[string]interface{}
func buildConfig(kcfg config.KernelConfig, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
outbounds := []M{
{"protocol": "freedom", "tag": "direct"},
{"protocol": "blackhole", "tag": "block"},
}
// Panel-defined custom outbounds (structured, converted to Xray native)
for _, co := range nc.CustomOutbounds {
outbounds = append(outbounds, outboundConfigToXray(co))
}
// Static outbounds from local config file (already kernel-native format)
for _, co := range kcfg.CustomOutbound {
outbounds = append(outbounds, M(co))
}
cfg := M{
"log": M{
"loglevel": xrayLogLevel(kcfg.LogLevel),
},
"stats": M{},
"policy": M{
"levels": M{
"0": M{
"statsUserUplink": true,
"statsUserDownlink": true,
},
},
"system": M{
"statsInboundUplink": true,
"statsInboundDownlink": true,
"statsOutboundUplink": true,
"statsOutboundDownlink": true,
},
},
"outbounds": outbounds,
}
inbound := buildInbound(nc, users, certFile, keyFile)
if inbound != nil {
cfg["inbounds"] = []M{inbound}
} else {
slog.Warn("xray: unsupported protocol, no inbound configured — node will not accept connections",
"protocol", nc.Protocol,
"supported", "vmess, vless, trojan, shadowsocks, socks, http")
}
// Merge panel routes and static config routes
cfg["routing"] = buildRouting(nc.Routes, mergeRouteList(nc.CustomRoutes, kcfg.CustomRoute))
mergeCustomXray(cfg, kcfg)
return cfg
}
// outboundConfigToXray converts a structured OutboundConfig (from the panel)
// into an Xray outbound object. Xray uses a nested layout where protocol-
// specific fields go inside a "settings" key, and chain proxying uses "proxySettings".
func outboundConfigToXray(oc panel.OutboundConfig) M {
m := M{
"protocol": oc.Protocol,
"tag": oc.Tag,
}
if len(oc.Settings) > 0 {
m["settings"] = oc.Settings
}
if oc.ProxyTag != "" {
m["proxySettings"] = M{"tag": oc.ProxyTag}
}
return m
}
func mergeRouteList(a, b []map[string]any) []map[string]any {
res := make([]map[string]any, 0, len(a)+len(b))
res = append(res, a...)
res = append(res, b...)
return res
}
// mergeCustomXray deep-merges a custom Xray config file into the generated config.
// Merge strategy (compatible with V2bX/XrayR custom DNS etc.):
// - dns: custom replaces auto-generated
// - outbounds: custom entries appended
// - routing.rules: custom rules prepended (matched first)
// - policy, api: custom deep-merges
// - inbounds: NOT merged (panel-managed, authoritative)
func mergeCustomXray(cfg M, kcfg config.KernelConfig) {
custom, err := kernel.LoadCustomConfig(kcfg.CustomConfig)
if err != nil {
slog.Error("failed to load custom xray config", "error", err)
return
}
if custom == nil {
return
}
// dns — custom replaces (same as V2bX DnsConfigPath)
if v, ok := custom["dns"]; ok {
cfg["dns"] = v
}
// outbounds — append custom entries
if v, ok := custom["outbounds"]; ok {
if existing, ok := cfg["outbounds"].([]M); ok {
cfg["outbounds"] = kernel.MergeAppendList(existing, v)
}
}
// routing — merge sub-fields
if customRouting, ok := custom["routing"]; ok {
if customRoutingMap, ok := customRouting.(map[string]any); ok {
mergeCustomXrayRouting(cfg, customRoutingMap)
}
}
// other top-level keys (policy, api, transport, etc.) — custom overrides,
// but we protect auto-generated inbounds, stats, log, routing, outbounds
protected := map[string]bool{
"inbounds": true, "outbounds": true, "routing": true,
"dns": true, "log": true, "stats": true, "policy": true,
}
for k, v := range custom {
if !protected[k] {
cfg[k] = v
}
}
}
func mergeCustomXrayRouting(cfg M, customRouting map[string]any) {
routing, ok := cfg["routing"].(M)
if !ok {
routing = M{}
cfg["routing"] = routing
}
// rules — custom rules prepended (so they match before panel rules)
if v, ok := customRouting["rules"]; ok {
if existing, ok := routing["rules"].([]M); ok {
routing["rules"] = kernel.MergePrependList(existing, v)
}
}
// domainStrategy, balancers, etc. — custom overrides
for k, v := range customRouting {
if k == "rules" {
continue
}
routing[k] = v
}
}
func xrayLogLevel(singboxLevel string) string {
switch singboxLevel {
case "trace", "debug":
return "debug"
case "info":
return "info"
case "warn":
return "warning"
case "error":
return "error"
case "fatal", "panic":
return "error"
default:
return "warning"
}
}
func buildInbound(nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
listenAddr := "::"
if nc.ListenIP != "" {
listenAddr = nc.ListenIP
}
base := M{
"tag": nc.Protocol + "-in",
"listen": listenAddr,
"port": nc.ServerPort,
"protocol": nc.Protocol,
}
switch nc.Protocol {
case "vmess":
return buildVMess(base, nc, users, certFile, keyFile)
case "vless":
return buildVLESS(base, nc, users, certFile, keyFile)
case "trojan":
return buildTrojan(base, nc, users, certFile, keyFile)
case "shadowsocks":
return buildShadowsocks(base, nc, users)
case "socks":
return buildSocks(base, users)
case "http":
return buildHTTP(base, nc, users, certFile, keyFile)
default:
return nil
}
}
// userEmail returns the stats-tracking email for a user.
// Format: "user@<id>" so we can parse back the user ID from stats counters.
func userEmail(userID int) string {
return fmt.Sprintf("user@%d", userID)
}
func buildVMess(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
clients := make([]M, 0, len(users))
for _, u := range users {
clients = append(clients, M{
"id": u.UUID,
"alterId": 0,
"email": userEmail(u.ID),
})
}
base["settings"] = M{"clients": clients}
applyStreamSettings(base, nc, certFile, keyFile)
return base
}
func buildVLESS(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
clients := make([]M, 0, len(users))
for _, u := range users {
client := M{
"id": u.UUID,
"email": userEmail(u.ID),
}
if nc.Flow != "" {
client["flow"] = nc.Flow
}
clients = append(clients, client)
}
base["settings"] = M{
"clients": clients,
"decryption": "none",
}
applyStreamSettings(base, nc, certFile, keyFile)
return base
}
func buildTrojan(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
clients := make([]M, 0, len(users))
for _, u := range users {
clients = append(clients, M{
"password": u.UUID,
"email": userEmail(u.ID),
})
}
base["settings"] = M{"clients": clients}
applyStreamSettings(base, nc, certFile, keyFile)
return base
}
func buildShadowsocks(base M, nc *panel.NodeConfig, users []panel.User) M {
// Single-user mode for traditional ciphers
if !strings.HasPrefix(nc.Cipher, "2022-blake3-") {
if len(users) > 0 {
base["settings"] = M{
"method": nc.Cipher,
"password": users[0].UUID,
"email": userEmail(users[0].ID),
"network": "tcp,udp",
}
}
return base
}
// Multi-user mode for 2022-blake3 ciphers
clients := make([]M, 0, len(users))
for _, u := range users {
clients = append(clients, M{
"password": u.UUID,
"email": userEmail(u.ID),
})
}
base["settings"] = M{
"method": nc.Cipher,
"password": nc.ServerKey,
"clients": clients,
"network": "tcp,udp",
}
return base
}
func buildSocks(base M, users []panel.User) M {
base["protocol"] = "socks"
accounts := make([]M, 0, len(users))
for _, u := range users {
accounts = append(accounts, M{
"user": u.UUID,
"pass": u.UUID,
})
}
base["settings"] = M{
"auth": "password",
"accounts": accounts,
"udp": true,
}
return base
}
func buildHTTP(base M, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
base["protocol"] = "http"
accounts := make([]M, 0, len(users))
for _, u := range users {
accounts = append(accounts, M{
"user": u.UUID,
"pass": u.UUID,
})
}
base["settings"] = M{
"accounts": accounts,
}
if nc.TLS == 1 {
applyStreamSettings(base, nc, certFile, keyFile)
}
return base
}
func applyStreamSettings(base M, nc *panel.NodeConfig, certFile, keyFile string) {
ss := M{}
// Network / transport
network := nc.Network
if network == "" {
network = "tcp"
}
ss["network"] = network
switch network {
case "ws":
wsSettings := M{}
if nc.NetworkSettings != nil {
if v, ok := nc.NetworkSettings["path"]; ok {
wsSettings["path"] = v
}
headers := M{}
if v, ok := nc.NetworkSettings["headers"]; ok {
if headersMap, ok := v.(map[string]interface{}); ok {
for k, val := range headersMap {
headers[k] = val
}
}
}
if v, ok := nc.NetworkSettings["host"]; ok {
headers["Host"] = v
}
if len(headers) > 0 {
wsSettings["headers"] = headers
}
}
ss["wsSettings"] = wsSettings
case "grpc":
grpcSettings := M{}
if nc.NetworkSettings != nil {
if v, ok := nc.NetworkSettings["service_name"]; ok {
grpcSettings["serviceName"] = v
}
}
ss["grpcSettings"] = grpcSettings
case "httpupgrade":
huSettings := M{}
if nc.NetworkSettings != nil {
if v, ok := nc.NetworkSettings["path"]; ok {
huSettings["path"] = v
}
if v, ok := nc.NetworkSettings["host"]; ok {
huSettings["host"] = v
}
}
ss["httpupgradeSettings"] = huSettings
case "h2", "http":
ss["network"] = "h2"
h2Settings := M{}
if nc.NetworkSettings != nil {
if v, ok := nc.NetworkSettings["path"]; ok {
h2Settings["path"] = v
}
if v, ok := nc.NetworkSettings["host"]; ok {
h2Settings["host"] = []interface{}{v}
}
}
ss["httpSettings"] = h2Settings
case "tcp":
// default, no extra settings
}
// TLS
if nc.TLS == 1 {
tlsSettings := M{}
serverName := nc.ServerName
if serverName == "" && nc.Host != "" {
serverName = nc.Host
}
if serverName != "" {
tlsSettings["serverName"] = serverName
}
if nc.TLSSettings != nil {
if sn, ok := nc.TLSSettings["server_name"]; ok && sn != "" {
tlsSettings["serverName"] = sn
}
}
if certFile != "" && keyFile != "" {
tlsSettings["certificates"] = []M{
{
"certificateFile": certFile,
"keyFile": keyFile,
},
}
}
ss["security"] = "tls"
ss["tlsSettings"] = tlsSettings
} else if nc.TLS == 2 {
ss["security"] = "reality"
ss["realitySettings"] = buildRealitySettings(nc)
}
base["streamSettings"] = ss
}
func buildRealitySettings(nc *panel.NodeConfig) M {
reality := M{"show": false}
if nc.TLSSettings == nil {
return reality
}
if pk, ok := nc.TLSSettings["private_key"]; ok {
reality["privateKey"] = pk
}
if sid, ok := nc.TLSSettings["short_id"]; ok {
switch v := sid.(type) {
case string:
reality["shortIds"] = []string{v}
case []interface{}:
ids := make([]string, 0, len(v))
for _, item := range v {
ids = append(ids, fmt.Sprintf("%v", item))
}
reality["shortIds"] = ids
}
}
if dest, ok := nc.TLSSettings["dest"]; ok {
destStr := fmt.Sprintf("%v", dest)
reality["dest"] = destStr
}
if sn, ok := nc.TLSSettings["server_name"]; ok {
reality["serverNames"] = []string{fmt.Sprintf("%v", sn)}
if _, exists := reality["dest"]; !exists {
reality["dest"] = fmt.Sprintf("%v:443", sn)
}
}
return reality
}
func buildRouting(rules []panel.RouteRule, customRules []map[string]any) M {
var xrayRules []M
// Custom route rules (from Panel CustomRoutes or local config) have HIGHEST priority
for _, cr := range customRules {
xrayRules = append(xrayRules, M(cr))
}
xrayRules = append(xrayRules, M{
"type": "field",
"ip": []string{
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"fc00::/7",
"fe80::/10",
"::1/128",
},
"outboundTag": "block",
})
for _, rule := range rules {
if len(rule.Match) == 0 {
continue
}
var ips, domains []string
for _, m := range rule.Match {
m = strings.TrimSpace(m)
if m == "" {
continue
}
if strings.HasPrefix(m, "geoip:") {
ips = append(ips, m) // "geoip:cn" → ip field, passed as-is
} else if strings.HasPrefix(m, "geosite:") {
domains = append(domains, m) // "geosite:google" → domain field, passed as-is
} else if strings.Contains(m, "/") {
ips = append(ips, m)
} else {
m = strings.TrimPrefix(m, "*.")
domains = append(domains, "domain:"+m)
}
}
outbound := "block"
if rule.Action == "direct" {
outbound = "direct"
} else if rule.Action == "proxy" && rule.ActionValue != "" {
outbound = rule.ActionValue
}
if len(domains) > 0 {
xrayRules = append(xrayRules, M{
"type": "field",
"domain": domains,
"outboundTag": outbound,
})
}
if len(ips) > 0 {
xrayRules = append(xrayRules, M{
"type": "field",
"ip": ips,
"outboundTag": outbound,
})
}
}
return M{
"domainStrategy": "AsIs",
"rules": xrayRules,
}
}
+364
View File
@@ -0,0 +1,364 @@
package xray
import (
"encoding/json"
"testing"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/panel"
)
var testKernelCfg = config.KernelConfig{
Type: "xray",
LogLevel: "warn",
}
var testUsers = []panel.User{
{ID: 1, UUID: "279d4f89-3a2c-488d-a67c-2d39a72acdde"},
{ID: 5, UUID: "4d5965c8-a60c-452a-a943-af83ec0bb0db"},
}
func TestBuildConfig_AllProtocols_ValidJSON(t *testing.T) {
protocols := []struct {
name string
nc panel.NodeConfig
}{
{
name: "vmess",
nc: panel.NodeConfig{
Protocol: "vmess",
ServerPort: 10086,
Network: "ws",
TLS: 1,
NetworkSettings: map[string]interface{}{
"path": "/vmess",
"host": "example.com",
},
},
},
{
name: "vless",
nc: panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
Network: "tcp",
TLS: 2,
Flow: "xtls-rprx-vision",
TLSSettings: map[string]interface{}{
"private_key": "test-pk",
"short_id": "abcd",
"server_name": "www.example.com",
"dest": "www.example.com:443",
},
},
},
{
name: "trojan",
nc: panel.NodeConfig{
Protocol: "trojan",
ServerPort: 443,
Network: "grpc",
TLS: 1,
ServerName: "example.com",
NetworkSettings: map[string]interface{}{
"service_name": "trojan-grpc",
},
},
},
{
name: "shadowsocks-aes",
nc: panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 8388,
Cipher: "aes-128-gcm",
},
},
{
name: "shadowsocks-2022",
nc: panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 8388,
Cipher: "2022-blake3-aes-128-gcm",
ServerKey: "test-server-key",
},
},
{
name: "socks",
nc: panel.NodeConfig{
Protocol: "socks",
ServerPort: 1080,
},
},
{
name: "http",
nc: panel.NodeConfig{
Protocol: "http",
ServerPort: 8080,
},
},
}
for _, tc := range protocols {
t.Run(tc.name, func(t *testing.T) {
cfg := buildConfig(testKernelCfg, &tc.nc, testUsers, "/cert.pem", "/key.pem")
data, err := json.Marshal(cfg)
if err != nil {
t.Fatalf("marshal failed: %v", err)
}
var parsed map[string]interface{}
if err := json.Unmarshal(data, &parsed); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
// Check required top-level fields
for _, key := range []string{"log", "stats", "policy", "outbounds", "routing"} {
if _, ok := parsed[key]; !ok {
t.Errorf("missing top-level key: %s", key)
}
}
if _, ok := parsed["inbounds"]; !ok {
t.Error("missing inbounds")
}
t.Logf("config size: %d bytes", len(data))
})
}
}
func TestBuildConfig_VMess_Users(t *testing.T) {
nc := panel.NodeConfig{
Protocol: "vmess",
ServerPort: 10086,
}
cfg := buildConfig(testKernelCfg, &nc, testUsers, "", "")
data, _ := json.Marshal(cfg)
var parsed map[string]interface{}
json.Unmarshal(data, &parsed)
inbounds := parsed["inbounds"].([]interface{})
ib := inbounds[0].(map[string]interface{})
if ib["protocol"] != "vmess" {
t.Errorf("expected protocol vmess, got %v", ib["protocol"])
}
settings := ib["settings"].(map[string]interface{})
clients := settings["clients"].([]interface{})
if len(clients) != 2 {
t.Fatalf("expected 2 clients, got %d", len(clients))
}
c1 := clients[0].(map[string]interface{})
if c1["email"] != "user@1" {
t.Errorf("expected email user@1, got %v", c1["email"])
}
if c1["id"] != "279d4f89-3a2c-488d-a67c-2d39a72acdde" {
t.Errorf("unexpected UUID: %v", c1["id"])
}
}
func TestBuildConfig_VLESS_Flow(t *testing.T) {
nc := panel.NodeConfig{
Protocol: "vless",
ServerPort: 443,
Flow: "xtls-rprx-vision",
TLS: 2,
TLSSettings: map[string]interface{}{
"private_key": "pk",
"server_name": "example.com",
},
}
cfg := buildConfig(testKernelCfg, &nc, testUsers, "", "")
data, _ := json.Marshal(cfg)
var parsed map[string]interface{}
json.Unmarshal(data, &parsed)
inbounds := parsed["inbounds"].([]interface{})
ib := inbounds[0].(map[string]interface{})
settings := ib["settings"].(map[string]interface{})
clients := settings["clients"].([]interface{})
c1 := clients[0].(map[string]interface{})
if c1["flow"] != "xtls-rprx-vision" {
t.Errorf("expected flow xtls-rprx-vision, got %v", c1["flow"])
}
ss := ib["streamSettings"].(map[string]interface{})
if ss["security"] != "reality" {
t.Errorf("expected security reality, got %v", ss["security"])
}
}
func TestBuildRouting_Default(t *testing.T) {
routing := buildRouting(nil, nil)
rules := routing["rules"].([]M)
if len(rules) != 1 {
t.Fatalf("expected 1 default rule, got %d", len(rules))
}
if rules[0]["outboundTag"] != "block" {
t.Errorf("expected block outbound, got %v", rules[0]["outboundTag"])
}
ips := rules[0]["ip"].([]string)
if len(ips) < 5 {
t.Errorf("expected multiple private CIDRs, got %d", len(ips))
}
}
func TestBuildRouting_WithRules(t *testing.T) {
rules := []panel.RouteRule{
{
ID: 1,
Match: []string{"*.baidu.com", "*.qq.com", "10.0.0.0/8"},
Action: "block",
},
{
ID: 2,
Match: []string{"*.google.com"},
Action: "direct",
},
}
routing := buildRouting(rules, nil)
xrayRules := routing["rules"].([]M)
// 1 default + 2 domain rules + 1 IP rule = 4
if len(xrayRules) != 4 {
t.Fatalf("expected 4 rules, got %d", len(xrayRules))
}
// Rule 1: domains block
r1 := xrayRules[1]
domains := r1["domain"].([]string)
if len(domains) != 2 {
t.Fatalf("expected 2 domains, got %d", len(domains))
}
if domains[0] != "domain:baidu.com" || domains[1] != "domain:qq.com" {
t.Errorf("unexpected domains: %v", domains)
}
if r1["outboundTag"] != "block" {
t.Errorf("expected block, got %v", r1["outboundTag"])
}
// Rule 2: IP block
r2 := xrayRules[2]
ips := r2["ip"].([]string)
if len(ips) != 1 || ips[0] != "10.0.0.0/8" {
t.Errorf("unexpected IPs: %v", ips)
}
// Rule 3: direct
r3 := xrayRules[3]
if r3["outboundTag"] != "direct" {
t.Errorf("expected direct, got %v", r3["outboundTag"])
}
}
func TestBuildConfig_LogLevel(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"debug", "debug"},
{"info", "info"},
{"warn", "warning"},
{"error", "error"},
{"", "warning"},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
result := xrayLogLevel(tc.input)
if result != tc.expected {
t.Errorf("xrayLogLevel(%q) = %q, want %q", tc.input, result, tc.expected)
}
})
}
}
func TestBuildConfig_StatsEnabled(t *testing.T) {
nc := panel.NodeConfig{
Protocol: "vmess",
ServerPort: 10086,
}
cfg := buildConfig(testKernelCfg, &nc, testUsers, "", "")
data, _ := json.Marshal(cfg)
var parsed map[string]interface{}
json.Unmarshal(data, &parsed)
// Verify stats is enabled
if _, ok := parsed["stats"]; !ok {
t.Error("stats not enabled")
}
// Verify policy enables user stats
policy := parsed["policy"].(map[string]interface{})
levels := policy["levels"].(map[string]interface{})
level0 := levels["0"].(map[string]interface{})
if v, ok := level0["statsUserUplink"]; !ok || v != true {
t.Error("statsUserUplink not enabled")
}
if v, ok := level0["statsUserDownlink"]; !ok || v != true {
t.Error("statsUserDownlink not enabled")
}
}
func TestBuildConfig_Shadowsocks_SingleUser(t *testing.T) {
nc := panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 8388,
Cipher: "aes-128-gcm",
}
cfg := buildConfig(testKernelCfg, &nc, testUsers, "", "")
data, _ := json.Marshal(cfg)
var parsed map[string]interface{}
json.Unmarshal(data, &parsed)
inbounds := parsed["inbounds"].([]interface{})
ib := inbounds[0].(map[string]interface{})
settings := ib["settings"].(map[string]interface{})
// Single-user mode: password directly, no clients array
if settings["method"] != "aes-128-gcm" {
t.Errorf("expected method aes-128-gcm, got %v", settings["method"])
}
if settings["password"] != testUsers[0].UUID {
t.Errorf("expected password %s, got %v", testUsers[0].UUID, settings["password"])
}
}
func TestBuildConfig_Shadowsocks_MultiUser(t *testing.T) {
nc := panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 8388,
Cipher: "2022-blake3-aes-128-gcm",
ServerKey: "server-key",
}
cfg := buildConfig(testKernelCfg, &nc, testUsers, "", "")
data, _ := json.Marshal(cfg)
var parsed map[string]interface{}
json.Unmarshal(data, &parsed)
inbounds := parsed["inbounds"].([]interface{})
ib := inbounds[0].(map[string]interface{})
settings := ib["settings"].(map[string]interface{})
if settings["password"] != "server-key" {
t.Errorf("expected server key, got %v", settings["password"])
}
clients := settings["clients"].([]interface{})
if len(clients) != 2 {
t.Fatalf("expected 2 clients, got %d", len(clients))
}
}
+428
View File
@@ -0,0 +1,428 @@
package xray
import (
"context"
"errors"
"log/slog"
"reflect"
"sort"
"strconv"
"sync"
"sync/atomic"
_ "unsafe"
xrayDispatcher "github.com/xtls/xray-core/app/dispatcher"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport"
"golang.org/x/time/rate"
"github.com/cedar2025/xboard-node/internal/kernel"
)
// Access xray's internal config creator registry so we can replace the
// default dispatcher factory with ours. This runs AFTER xray's init()
// functions because our package imports xray (dependency order guarantee).
//
//go:linkname typeCreatorRegistry github.com/xtls/xray-core/common.typeCreatorRegistry
var typeCreatorRegistry map[reflect.Type]common.ConfigCreator
var origDispatcherFactory common.ConfigCreator
// globalLimitDispatcher is set when the factory creates a LimitDispatcher.
// The Xray kernel reads it to configure limits and get connections.
var globalLimitDispatcher atomic.Pointer[LimitDispatcher]
func init() {
configType := reflect.TypeOf((*xrayDispatcher.Config)(nil))
origDispatcherFactory = typeCreatorRegistry[configType]
typeCreatorRegistry[configType] = limitDispatcherFactory
}
func limitDispatcherFactory(ctx context.Context, config interface{}) (interface{}, error) {
orig, err := origDispatcherFactory(ctx, config)
if err != nil {
return nil, err
}
inner, ok := orig.(routing.Dispatcher)
if !ok {
return orig, nil
}
ld := &LimitDispatcher{
inner: orig,
innerDisp: inner,
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
globalLimitDispatcher.Store(ld)
slog.Debug("xray: limit dispatcher installed")
return ld, nil
}
// LimitDispatcher wraps xray's DefaultDispatcher to add per-user device
// limit enforcement, speed limiting, and per-connection source IP tracking.
// It hooks into Dispatch/DispatchLink which are called AFTER the protocol
// handler has identified the user, so it works for ALL protocols.
type LimitDispatcher struct {
inner interface{} // original DefaultDispatcher (Feature + Dispatcher)
innerDisp routing.Dispatcher // same object, typed as Dispatcher
mu sync.RWMutex
conns map[string]*dispatchedConn // connID → conn
userIPs map[string]map[string]int // email → sourceIP → count
deviceLimits map[string]int // email → max devices
speedLimits map[string]int // email → Mbps
speedBuckets sync.Map // email → *rate.Limiter
emailToUID map[string]int // email → panel user ID
connIDSeq atomic.Uint64
}
type dispatchedConn struct {
id string
email string
sourceIP string
userID int
upload atomic.Int64
download atomic.Int64
closed atomic.Bool
}
// ─── routing.Dispatcher ──────────────────────────────────────────────────────
func (d *LimitDispatcher) Dispatch(ctx context.Context, dest net.Destination) (*transport.Link, error) {
email, sourceIP, isTCP, err := d.identifyAndCheck(ctx, dest)
if err != nil {
return nil, err
}
link, err := d.innerDisp.Dispatch(ctx, dest)
if err != nil {
if email != "" && isTCP {
d.delConn(email, sourceIP)
}
return nil, err
}
if email != "" {
d.wrapLink(ctx, link, email, sourceIP, isTCP)
}
return link, nil
}
func (d *LimitDispatcher) DispatchLink(ctx context.Context, dest net.Destination, link *transport.Link) error {
email, sourceIP, isTCP, err := d.identifyAndCheck(ctx, dest)
if err != nil {
return err
}
if email != "" {
d.wrapLink(ctx, link, email, sourceIP, isTCP)
}
return d.innerDisp.DispatchLink(ctx, dest, link)
}
// identifyAndCheck extracts user identity from the session context, enforces
// device limits, and returns the user's email, source IP, and TCP flag.
// Returns a non-nil error only when the connection should be rejected.
func (d *LimitDispatcher) identifyAndCheck(ctx context.Context, dest net.Destination) (email, sourceIP string, isTCP bool, err error) {
si := session.InboundFromContext(ctx)
if si == nil || si.User == nil || len(si.User.Email) == 0 {
return "", "", false, nil
}
email = si.User.Email
sourceIP = si.Source.Address.IP().String()
isTCP = dest.Network == net.Network_TCP
if d.checkDeviceLimit(email, sourceIP, isTCP) {
slog.Info("xray: device limit exceeded", "email", email, "ip", sourceIP)
return "", "", false, errors.New("device limit exceeded for " + email)
}
return email, sourceIP, isTCP, nil
}
// wrapLink instruments a transport.Link with per-connection byte counting,
// rate limiting, and lifecycle tracking. Must only be called when email != "".
func (d *LimitDispatcher) wrapLink(ctx context.Context, link *transport.Link, email, sourceIP string, isTCP bool) {
connID := "xd-" + strconv.FormatUint(d.connIDSeq.Add(1), 36)
dc := &dispatchedConn{
id: connID,
email: email,
sourceIP: sourceIP,
userID: d.getUID(email),
}
d.mu.Lock()
d.conns[connID] = dc
d.mu.Unlock()
onClose := func() {
if dc.closed.CompareAndSwap(false, true) {
if isTCP {
d.delConn(email, sourceIP)
}
d.mu.Lock()
delete(d.conns, connID)
d.mu.Unlock()
}
}
limiter := d.getBucket(email)
link.Reader = &statsCloseReader{
Reader: link.Reader,
counter: &dc.upload,
limiter: limiter,
ctx: ctx,
}
link.Writer = &statsCloseWriter{
Writer: link.Writer,
counter: &dc.download,
limiter: limiter,
ctx: ctx,
onClose: onClose,
}
}
// ─── features.Feature (delegated) ───────────────────────────────────────────
func (d *LimitDispatcher) Type() interface{} { return routing.DispatcherType() }
func (d *LimitDispatcher) Start() error {
if s, ok := d.inner.(interface{ Start() error }); ok {
return s.Start()
}
return nil
}
func (d *LimitDispatcher) Close() error {
if c, ok := d.inner.(interface{ Close() error }); ok {
return c.Close()
}
return nil
}
// ─── Limit management (called by Xray kernel) ──────────────────────────────
func (d *LimitDispatcher) UpdateLimits(emailToUID map[string]int, deviceLimits, speedLimits map[string]int) {
d.mu.Lock()
d.emailToUID = emailToUID
d.deviceLimits = deviceLimits
d.speedLimits = speedLimits
d.mu.Unlock()
d.speedBuckets.Range(func(key, _ interface{}) bool {
if _, ok := speedLimits[key.(string)]; !ok {
d.speedBuckets.Delete(key)
}
return true
})
}
func (d *LimitDispatcher) ResetConns() {
d.mu.Lock()
d.conns = make(map[string]*dispatchedConn)
d.userIPs = make(map[string]map[string]int)
d.mu.Unlock()
d.speedBuckets.Range(func(key, _ interface{}) bool {
d.speedBuckets.Delete(key)
return true
})
}
func (d *LimitDispatcher) Snapshot() []kernel.Connection {
d.mu.RLock()
defer d.mu.RUnlock()
result := make([]kernel.Connection, 0, len(d.conns))
for _, c := range d.conns {
result = append(result, kernel.Connection{
ID: c.id,
UserID: c.userID,
SourceIP: c.sourceIP,
Upload: c.upload.Load(),
Download: c.download.Load(),
})
}
return result
}
func (d *LimitDispatcher) CloseConn(id string) bool {
d.mu.RLock()
c, ok := d.conns[id]
d.mu.RUnlock()
if !ok {
return false
}
c.closed.Store(true)
d.delConn(c.email, c.sourceIP)
d.mu.Lock()
delete(d.conns, id)
d.mu.Unlock()
return true
}
// ─── Internal helpers ───────────────────────────────────────────────────────
func (d *LimitDispatcher) checkDeviceLimit(email, sourceIP string, isTCP bool) bool {
d.mu.Lock()
defer d.mu.Unlock()
limit, hasLimit := d.deviceLimits[email]
if !hasLimit || limit <= 0 {
if isTCP {
if d.userIPs[email] == nil {
d.userIPs[email] = make(map[string]int)
}
d.userIPs[email][sourceIP]++
}
return false
}
ips := d.userIPs[email]
if ips == nil {
ips = make(map[string]int)
d.userIPs[email] = ips
}
if ips[sourceIP] > 0 {
if isTCP {
ips[sourceIP]++
}
return false
}
if len(ips) < limit {
if isTCP {
ips[sourceIP]++
}
return false
}
// Over limit — deterministic: allow lowest IPs lexicographically
ipList := make([]string, 0, len(ips)+1)
for ip := range ips {
ipList = append(ipList, ip)
}
ipList = append(ipList, sourceIP)
sort.Strings(ipList)
for i := 0; i < limit && i < len(ipList); i++ {
if ipList[i] == sourceIP {
if isTCP {
ips[sourceIP]++
}
return false
}
}
return true
}
func (d *LimitDispatcher) delConn(email, sourceIP string) {
d.mu.Lock()
defer d.mu.Unlock()
if ips, ok := d.userIPs[email]; ok {
ips[sourceIP]--
if ips[sourceIP] <= 0 {
delete(ips, sourceIP)
}
if len(ips) == 0 {
delete(d.userIPs, email)
}
}
}
func (d *LimitDispatcher) getUID(email string) int {
d.mu.RLock()
defer d.mu.RUnlock()
return d.emailToUID[email]
}
func (d *LimitDispatcher) getBucket(email string) *rate.Limiter {
d.mu.RLock()
mbps, ok := d.speedLimits[email]
d.mu.RUnlock()
if !ok || mbps <= 0 {
return nil
}
bytesPerSec := int(mbps) * 1_000_000 / 8
burst := bytesPerSec
if burst < 64*1024 {
burst = 64 * 1024
}
v, loaded := d.speedBuckets.LoadOrStore(email, rate.NewLimiter(rate.Limit(bytesPerSec), burst))
lim := v.(*rate.Limiter)
if loaded {
// Update existing limiter in case speed limit changed.
lim.SetLimit(rate.Limit(bytesPerSec))
lim.SetBurst(burst)
}
return lim
}
// ─── I/O wrappers ───────────────────────────────────────────────────────────
type statsCloseReader struct {
buf.Reader
counter *atomic.Int64
limiter *rate.Limiter
ctx context.Context
}
func (r *statsCloseReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
mb, err := r.Reader.ReadMultiBuffer()
if n := int64(mb.Len()); n > 0 {
r.counter.Add(n)
if r.limiter != nil {
waitN := int(n)
if burst := r.limiter.Burst(); waitN > burst {
waitN = burst
}
_ = r.limiter.WaitN(r.ctx, waitN)
}
}
return mb, err
}
func (r *statsCloseReader) Close() error { return common.Close(r.Reader) }
func (r *statsCloseReader) Interrupt() { common.Interrupt(r.Reader) }
type statsCloseWriter struct {
buf.Writer
counter *atomic.Int64
limiter *rate.Limiter
ctx context.Context
onClose func()
closed atomic.Bool
}
func (w *statsCloseWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
n := int64(mb.Len())
if w.limiter != nil {
waitN := int(n)
if burst := w.limiter.Burst(); waitN > burst {
waitN = burst
}
_ = w.limiter.WaitN(w.ctx, waitN)
}
w.counter.Add(n)
return w.Writer.WriteMultiBuffer(mb)
}
func (w *statsCloseWriter) Close() error {
if w.closed.CompareAndSwap(false, true) {
w.onClose()
}
return common.Close(w.Writer)
}
func (w *statsCloseWriter) Interrupt() {
if w.closed.CompareAndSwap(false, true) {
w.onClose()
}
common.Interrupt(w.Writer)
}
+204
View File
@@ -0,0 +1,204 @@
package xray
import (
"testing"
"github.com/cedar2025/xboard-node/internal/panel"
)
func TestLimitDispatcher_DeviceLimitCheck(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
users := []panel.User{
{ID: 1, UUID: "uuid-1", DeviceLimit: 2, SpeedLimit: 0},
{ID: 2, UUID: "uuid-2", DeviceLimit: 0, SpeedLimit: 10},
}
emailToUID := make(map[string]int)
deviceLimits := make(map[string]int)
speedLimits := make(map[string]int)
for _, u := range users {
email := userEmail(u.ID)
emailToUID[email] = u.ID
if u.DeviceLimit > 0 {
deviceLimits[email] = u.DeviceLimit
}
if u.SpeedLimit > 0 {
speedLimits[email] = u.SpeedLimit
}
}
ld.UpdateLimits(emailToUID, deviceLimits, speedLimits)
email1 := userEmail(1)
// First IP should be allowed
if ld.checkDeviceLimit(email1, "1.1.1.1", true) {
t.Error("first IP should be allowed")
}
// Second IP should be allowed (limit=2)
if ld.checkDeviceLimit(email1, "2.2.2.2", true) {
t.Error("second IP should be allowed")
}
// Third unique IP should be rejected
if !ld.checkDeviceLimit(email1, "3.3.3.3", true) {
t.Error("third IP should be rejected (limit=2)")
}
// Same IP as first should be allowed (already connected)
if ld.checkDeviceLimit(email1, "1.1.1.1", true) {
t.Error("same IP should always be allowed")
}
// User 2 has no device limit — should always be allowed
email2 := userEmail(2)
for i := 0; i < 10; i++ {
ip := "10.0.0." + string(rune('0'+i))
if ld.checkDeviceLimit(email2, ip, true) {
t.Errorf("user with no device limit should always be allowed (ip=%s)", ip)
}
}
}
func TestLimitDispatcher_DelConn(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
email := userEmail(1)
deviceLimits := map[string]int{email: 2}
ld.UpdateLimits(map[string]int{email: 1}, deviceLimits, nil)
// Add 2 IPs
ld.checkDeviceLimit(email, "1.1.1.1", true)
ld.checkDeviceLimit(email, "2.2.2.2", true)
// Third should be rejected
if !ld.checkDeviceLimit(email, "3.3.3.3", true) {
t.Error("third IP should be rejected")
}
// Remove first IP
ld.delConn(email, "1.1.1.1")
// Now third IP should be allowed
if ld.checkDeviceLimit(email, "3.3.3.3", true) {
t.Error("after deleting one IP, new IP should be allowed")
}
}
func TestLimitDispatcher_SpeedBucket(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
email1 := userEmail(1)
email2 := userEmail(2)
speedLimits := map[string]int{email1: 10} // 10 Mbps
ld.UpdateLimits(map[string]int{email1: 1, email2: 2}, nil, speedLimits)
// User 1 has speed limit — should get a limiter
limiter := ld.getBucket(email1)
if limiter == nil {
t.Error("user with speed limit should get a limiter")
}
// Same user should get the same limiter (cached)
limiter2 := ld.getBucket(email1)
if limiter != limiter2 {
t.Error("same user should get cached limiter")
}
// User 2 has no speed limit — should get nil
limiter3 := ld.getBucket(email2)
if limiter3 != nil {
t.Error("user without speed limit should get nil")
}
}
func TestLimitDispatcher_Snapshot(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
// Add some connections
ld.conns["conn-1"] = &dispatchedConn{
id: "conn-1", email: "user@1", sourceIP: "1.1.1.1", userID: 1,
}
ld.conns["conn-2"] = &dispatchedConn{
id: "conn-2", email: "user@2", sourceIP: "2.2.2.2", userID: 2,
}
ld.conns["conn-1"].upload.Store(1000)
ld.conns["conn-1"].download.Store(2000)
snapshot := ld.Snapshot()
if len(snapshot) != 2 {
t.Fatalf("expected 2 connections, got %d", len(snapshot))
}
found := false
for _, c := range snapshot {
if c.ID == "conn-1" {
found = true
if c.UserID != 1 || c.SourceIP != "1.1.1.1" || c.Upload != 1000 || c.Download != 2000 {
t.Errorf("unexpected connection data: %+v", c)
}
}
}
if !found {
t.Error("conn-1 not found in snapshot")
}
}
func TestLimitDispatcher_CloseConn(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
email := userEmail(1)
ld.userIPs[email] = map[string]int{"1.1.1.1": 1}
ld.conns["conn-1"] = &dispatchedConn{
id: "conn-1", email: email, sourceIP: "1.1.1.1", userID: 1,
}
ok := ld.CloseConn("conn-1")
if !ok {
t.Error("CloseConn should return true for existing connection")
}
if len(ld.conns) != 0 {
t.Error("connection should be removed after close")
}
ok = ld.CloseConn("nonexistent")
if ok {
t.Error("CloseConn should return false for nonexistent connection")
}
}
func TestLimitDispatcher_ResetConns(t *testing.T) {
ld := &LimitDispatcher{
conns: make(map[string]*dispatchedConn),
userIPs: make(map[string]map[string]int),
}
ld.conns["c1"] = &dispatchedConn{id: "c1"}
ld.userIPs["user@1"] = map[string]int{"1.1.1.1": 1}
ld.ResetConns()
if len(ld.conns) != 0 {
t.Error("conns should be empty after reset")
}
if len(ld.userIPs) != 0 {
t.Error("userIPs should be empty after reset")
}
}
+331
View File
@@ -0,0 +1,331 @@
package xray
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"log/slog"
"os"
"sort"
"sync"
"time"
xrayCore "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/infra/conf/serial"
"golang.org/x/time/rate"
_ "github.com/xtls/xray-core/main/distro/all"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/kernel/geodata"
"github.com/cedar2025/xboard-node/internal/panel"
)
// drainTimeout is how long stop() waits for in-flight connections to finish
// naturally before hard-killing the xray instance.
const drainTimeout = 5 * time.Second
// Xray implements kernel.Kernel by embedding xray-core as a Go library.
//
// Device limits and speed limits are enforced through a custom dispatcher
// wrapper (LimitDispatcher) that hooks into session.InboundFromContext(ctx)
// — the same approach used by V2bX and XrayR. This works for ALL protocols
// because the dispatcher is called AFTER the protocol handler has identified
// the user and recorded the source IP in the session context.
type Xray struct {
cfg config.KernelConfig
mu sync.Mutex
instance *xrayCore.Instance
users []panel.User
protocol string
inboundTag string
// limitDispatcher is the per-instance LimitDispatcher created during
// xrayCore.New(). Stored here (instead of using the global pointer)
// so that multi-node mode with multiple Xray instances works correctly.
limitDispatcher *LimitDispatcher
// lastKernelHash caches a hash of the kernel-affecting config + user
// identities. Used by Reload to skip unnecessary full restarts.
lastKernelHash string
// cumTraffic keeps running totals for the aggregate stats fallback.
cumTraffic map[int][2]int64
}
func New(cfg config.KernelConfig) *Xray {
return &Xray{
cfg: cfg,
cumTraffic: make(map[int][2]int64),
}
}
func (x *Xray) Name() string { return "xray" }
func (x *Xray) ApplyConfig(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error {
x.mu.Lock()
defer x.mu.Unlock()
// Auto-download geo databases and set XRAY_LOCATION_ASSET when panel
// routes reference geoip:/geosite: entries.
if kernel.NeedsGeoIP(nodeConfig.Routes) || kernel.NeedsGeoSite(nodeConfig.Routes) {
geoDataDir := x.cfg.GeoDataDir
if err := geodata.Ensure(geoDataDir,
kernel.NeedsGeoIP(nodeConfig.Routes),
kernel.NeedsGeoSite(nodeConfig.Routes),
"xray",
); err != nil {
slog.Warn("geo database unavailable; geoip/geosite rules may not match", "error", err)
}
os.Setenv("XRAY_LOCATION_ASSET", geoDataDir)
}
cfgMap := buildConfig(x.cfg, nodeConfig, users, certFile, keyFile)
data, err := json.Marshal(cfgMap)
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
slog.Debug("xray config generated", "len", len(data))
pbConfig, err := serial.LoadJSONConfig(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("parse xray config: %w", err)
}
x.stop()
// Hold the global creation mutex around xrayCore.New() + capture so that
// concurrent Xray instances (multi-node) don't clobber each other's
// LimitDispatcher reference.
xrayCreationMu.Lock()
instance, err := xrayCore.New(pbConfig)
if err != nil {
xrayCreationMu.Unlock()
return fmt.Errorf("create xray instance: %w", err)
}
x.limitDispatcher = globalLimitDispatcher.Load()
xrayCreationMu.Unlock()
if err := instance.Start(); err != nil {
instance.Close()
return fmt.Errorf("start xray: %w", err)
}
x.instance = instance
x.users = users
x.protocol = nodeConfig.Protocol
x.inboundTag = nodeConfig.Protocol + "-in"
x.cumTraffic = make(map[int][2]int64)
x.lastKernelHash = computeKernelHash(nodeConfig, users)
// Configure the limit dispatcher with user limits
x.updateDispatcherLimits(users)
slog.Info("xray started (in-process)",
"users", len(users),
"protocol", nodeConfig.Protocol,
"device_limits", "enabled (all protocols via dispatcher hook)",
"speed_limits", "enabled (all protocols via dispatcher hook)",
)
return nil
}
func (x *Xray) Reload(nodeConfig *panel.NodeConfig, users []panel.User, certFile, keyFile string) error {
// Always update dispatcher limits (instant, no restart).
x.updateDispatcherLimits(users)
// Skip full restart if kernel-affecting fields are unchanged.
// This covers user-only limit changes where identities (ID, UUID) and
// config (routes, protocol settings, port) haven't changed.
newHash := computeKernelHash(nodeConfig, users)
x.mu.Lock()
same := x.lastKernelHash == newHash
x.mu.Unlock()
if same {
x.mu.Lock()
x.users = users
x.mu.Unlock()
slog.Info("xray: config/users unchanged, limits updated without restart")
return nil
}
return x.ApplyConfig(nodeConfig, users, certFile, keyFile)
}
func (x *Xray) Stop() {
x.mu.Lock()
defer x.mu.Unlock()
x.stop()
}
func (x *Xray) stop() {
if x.instance != nil {
// Drain: wait for in-flight connections to finish naturally.
if x.limitDispatcher != nil {
deadline := time.Now().Add(drainTimeout)
for time.Now().Before(deadline) {
x.limitDispatcher.mu.RLock()
n := len(x.limitDispatcher.conns)
x.limitDispatcher.mu.RUnlock()
if n == 0 {
break
}
time.Sleep(100 * time.Millisecond)
}
}
x.instance.Close()
x.instance = nil
}
if x.limitDispatcher != nil {
x.limitDispatcher.ResetConns()
x.limitDispatcher = nil
}
}
func (x *Xray) IsRunning() bool {
x.mu.Lock()
defer x.mu.Unlock()
return x.instance != nil
}
// GetConnections returns per-connection data from the dispatcher.
// Each connection has a real source IP and user ID, enabling device limit
// enforcement by the upper-layer limiter.
func (x *Xray) GetConnections(ctx context.Context) ([]kernel.Connection, error) {
x.mu.Lock()
defer x.mu.Unlock()
if x.instance == nil {
return nil, nil
}
// Primary: use LimitDispatcher's per-connection tracking
if x.limitDispatcher != nil {
conns := x.limitDispatcher.Snapshot()
if len(conns) > 0 {
return conns, nil
}
}
// Fallback: aggregate stats (for edge cases)
return x.getAggregateStats()
}
func (x *Xray) getAggregateStats() ([]kernel.Connection, error) {
sm := x.instance.GetFeature(stats.ManagerType())
if sm == nil {
return nil, nil
}
statsManager, ok := sm.(stats.Manager)
if !ok {
return nil, nil
}
var conns []kernel.Connection
for _, u := range x.users {
email := userEmail(u.ID)
var deltaUp, deltaDown int64
if c := statsManager.GetCounter(fmt.Sprintf("user>>>%s>>>traffic>>>uplink", email)); c != nil {
deltaUp = c.Set(0)
}
if c := statsManager.GetCounter(fmt.Sprintf("user>>>%s>>>traffic>>>downlink", email)); c != nil {
deltaDown = c.Set(0)
}
if deltaUp > 0 || deltaDown > 0 {
cum := x.cumTraffic[u.ID]
cum[0] += deltaUp
cum[1] += deltaDown
x.cumTraffic[u.ID] = cum
}
cum := x.cumTraffic[u.ID]
if cum[0] > 0 || cum[1] > 0 {
conns = append(conns, kernel.Connection{
ID: fmt.Sprintf("xray-%d", u.ID),
UserID: u.ID,
Upload: cum[0],
Download: cum[1],
})
}
}
return conns, nil
}
func (x *Xray) CloseConnection(_ context.Context, connID string) error {
x.mu.Lock()
ld := x.limitDispatcher
x.mu.Unlock()
if ld != nil {
ld.CloseConn(connID)
}
return nil
}
func (x *Xray) SetSpeedLimitFunc(_ func(string) *rate.Limiter) {}
var _ kernel.Kernel = (*Xray)(nil)
// updateDispatcherLimits configures the global LimitDispatcher with
// per-user device limits and speed limits.
func (x *Xray) updateDispatcherLimits(users []panel.User) {
x.mu.Lock()
ld := x.limitDispatcher
x.mu.Unlock()
if ld == nil {
return
}
emailToUID := make(map[string]int, len(users))
deviceLimits := make(map[string]int)
speedLimits := make(map[string]int)
for _, u := range users {
email := userEmail(u.ID)
emailToUID[email] = u.ID
// SOCKS/HTTP protocols use the raw UUID as User.Email in xray's session,
// while VMess/VLESS/Trojan use the "user@<id>" format. Map both so that
// limits work for all protocols.
emailToUID[u.UUID] = u.ID
if u.DeviceLimit > 0 {
deviceLimits[email] = u.DeviceLimit
deviceLimits[u.UUID] = u.DeviceLimit
}
if u.SpeedLimit > 0 {
speedLimits[email] = u.SpeedLimit
speedLimits[u.UUID] = u.SpeedLimit
}
}
ld.UpdateLimits(emailToUID, deviceLimits, speedLimits)
}
// xrayCreationMu serialises xrayCore.New() + globalLimitDispatcher capture
// so that concurrent Xray instances in multi-node mode each capture their
// own LimitDispatcher.
var xrayCreationMu sync.Mutex
// computeKernelHash returns a hash of the config + user identities (ID, UUID)
// that would require a kernel restart if changed. Limit-only changes are
// excluded so they can be applied without restart.
func computeKernelHash(nc *panel.NodeConfig, users []panel.User) string {
h := sha256.New()
configData, _ := json.Marshal(nc)
h.Write(configData)
sorted := make([]panel.User, len(users))
copy(sorted, users)
sort.Slice(sorted, func(i, j int) bool { return sorted[i].ID < sorted[j].ID })
for _, u := range sorted {
fmt.Fprintf(h, "%d:%s,", u.ID, u.UUID)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
+177
View File
@@ -0,0 +1,177 @@
package limiter
import (
"fmt"
"log/slog"
"net/netip"
"sort"
"sync"
"sync/atomic"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/panel"
)
// KickAction represents a connection that should be closed.
type KickAction struct {
ConnID string
UserID int
Reason string
}
// Limiter enforces per-user device limits and detects removed users.
type Limiter struct {
mu sync.RWMutex
users map[int]panel.User
deviceLimitEvents atomic.Uint64
}
func New() *Limiter {
return &Limiter{
users: make(map[int]panel.User),
}
}
// UpdateUsers refreshes the user limit configuration and returns
// the IDs of users that were present before but are now removed.
func (l *Limiter) UpdateUsers(users []panel.User) []int {
l.mu.Lock()
defer l.mu.Unlock()
newUsers := make(map[int]panel.User, len(users))
for _, u := range users {
newUsers[u.ID] = u
}
// Find removed users
var removed []int
for id := range l.users {
if _, ok := newUsers[id]; !ok {
removed = append(removed, id)
}
}
l.users = newUsers
return removed
}
// Check inspects connections against device limits.
// Returns a list of connections that should be closed.
func (l *Limiter) Check(conns []kernel.Connection) []KickAction {
l.mu.RLock()
defer l.mu.RUnlock()
var kicks []KickAction
// Device limit check
userIPs := make(map[int]map[string]bool)
userConnsByIP := make(map[int]map[string][]string) // user_id -> ip -> []conn_ids
for _, conn := range conns {
if conn.UserID == 0 || conn.SourceIP == "" {
continue
}
if userIPs[conn.UserID] == nil {
userIPs[conn.UserID] = make(map[string]bool)
userConnsByIP[conn.UserID] = make(map[string][]string)
}
userIPs[conn.UserID][conn.SourceIP] = true
userConnsByIP[conn.UserID][conn.SourceIP] = append(
userConnsByIP[conn.UserID][conn.SourceIP], conn.ID,
)
}
for uid, ips := range userIPs {
user, ok := l.users[uid]
if !ok || user.DeviceLimit == 0 {
continue
}
if len(ips) <= user.DeviceLimit {
continue
}
// Count a device-limit violation event for metrics reporting.
l.deviceLimitEvents.Add(1)
slog.Warn("user exceeded device limit",
"user_id", uid,
"devices", len(ips),
"limit", user.DeviceLimit,
)
// Sort IPs by numeric address so the "allowed" set is deterministic
// across calls. netip.Addr comparison avoids the lexicographic ordering
// quirks of string sort (e.g. "10.x" < "192.x", IPv6 always last).
// Parse all addresses up front to avoid O(n log n) repeated parsing.
type parsedIP struct {
raw string
addr netip.Addr
}
ipList := make([]parsedIP, 0, len(ips))
for ip := range ips {
a, _ := netip.ParseAddr(ip)
ipList = append(ipList, parsedIP{raw: ip, addr: a})
}
sort.Slice(ipList, func(i, j int) bool {
return ipList[i].addr.Compare(ipList[j].addr) < 0
})
allowed := make(map[string]bool, user.DeviceLimit)
for i := 0; i < user.DeviceLimit && i < len(ipList); i++ {
allowed[ipList[i].raw] = true
}
for ip, connIDs := range userConnsByIP[uid] {
if allowed[ip] {
continue
}
for _, connID := range connIDs {
kicks = append(kicks, KickAction{
ConnID: connID,
UserID: uid,
Reason: fmt.Sprintf("devices %d > limit %d", len(ips), user.DeviceLimit),
})
}
}
}
return kicks
}
// LimiterMetrics holds aggregated limiter statistics.
type LimiterMetrics struct {
// DeviceLimitEvents is the total number of times a user exceeded
// the configured device limit (since process start).
DeviceLimitEvents uint64
}
// SnapshotMetrics returns a snapshot of limiter metrics.
func (l *Limiter) SnapshotMetrics() LimiterMetrics {
return LimiterMetrics{
DeviceLimitEvents: l.deviceLimitEvents.Load(),
}
}
// KickUsers returns KickActions for all connections belonging to the given user IDs.
func (l *Limiter) KickUsers(conns []kernel.Connection, userIDs []int) []KickAction {
if len(userIDs) == 0 {
return nil
}
remove := make(map[int]bool, len(userIDs))
for _, id := range userIDs {
remove[id] = true
}
var kicks []KickAction
for _, conn := range conns {
if remove[conn.UserID] {
kicks = append(kicks, KickAction{
ConnID: conn.ID,
UserID: conn.UserID,
Reason: "user removed from panel",
})
}
}
return kicks
}
+339
View File
@@ -0,0 +1,339 @@
package limiter
import (
"sort"
"testing"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/panel"
)
func TestNew(t *testing.T) {
l := New()
if l == nil {
t.Fatal("New returned nil")
}
}
func TestUpdateUsers(t *testing.T) {
l := New()
users := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 3, DeviceLimit: 2},
{ID: 2, UUID: "u2", SpeedLimit: 0, DeviceLimit: 0},
}
removed := l.UpdateUsers(users)
if len(removed) != 0 {
t.Errorf("first update should have no removed users, got %v", removed)
}
// Verify by running a check
conns := []kernel.Connection{{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"}}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("expected no kicks, got %d", len(kicks))
}
}
func TestUpdateUsers_DetectsRemoved(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, UUID: "u1"},
{ID: 2, UUID: "u2"},
{ID: 3, UUID: "u3"},
})
// Remove user 2
removed := l.UpdateUsers([]panel.User{
{ID: 1, UUID: "u1"},
{ID: 3, UUID: "u3"},
})
if len(removed) != 1 || removed[0] != 2 {
t.Errorf("expected removed=[2], got %v", removed)
}
}
func TestUpdateUsers_DetectsMultipleRemoved(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, UUID: "u1"},
{ID: 2, UUID: "u2"},
{ID: 3, UUID: "u3"},
})
// Remove users 1 and 3
removed := l.UpdateUsers([]panel.User{
{ID: 2, UUID: "u2"},
})
sort.Ints(removed)
if len(removed) != 2 || removed[0] != 1 || removed[1] != 3 {
t.Errorf("expected removed=[1,3], got %v", removed)
}
}
func TestUpdateUsers_NoRemovals(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, UUID: "u1"},
})
// Add user, keep existing
removed := l.UpdateUsers([]panel.User{
{ID: 1, UUID: "u1"},
{ID: 2, UUID: "u2"},
})
if len(removed) != 0 {
t.Errorf("expected no removals, got %v", removed)
}
}
func TestUpdateUsers_ReplacesAll(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{{ID: 1, DeviceLimit: 1}})
// Replace with different users
removed := l.UpdateUsers([]panel.User{{ID: 2, DeviceLimit: 1}})
if len(removed) != 1 || removed[0] != 1 {
t.Errorf("expected removed=[1], got %v", removed)
}
}
func TestDeviceLimit_NoKick_UnderLimit(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 3},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, SourceIP: "2.2.2.2"},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("2 devices under limit 3, got %d kicks", len(kicks))
}
}
func TestDeviceLimit_Kick_OverLimit(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 2},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 1, SourceIP: "3.3.3.3"},
}
kicks := l.Check(conns)
if len(kicks) == 0 {
t.Fatal("expected kicks for exceeding device limit")
}
if len(kicks) < 1 {
t.Errorf("expected at least 1 kick, got %d", len(kicks))
}
}
func TestDeviceLimit_ExactlyAtLimit(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 2},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, SourceIP: "2.2.2.2"},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("exactly at limit should not kick, got %d", len(kicks))
}
}
func TestDeviceLimit_UnlimitedDevices(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 0}, // 0 = unlimited
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 1, SourceIP: "3.3.3.3"},
{ID: "c4", UserID: 1, SourceIP: "4.4.4.4"},
{ID: "c5", UserID: 1, SourceIP: "5.5.5.5"},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("unlimited devices should not be kicked, got %d", len(kicks))
}
}
func TestDeviceLimit_SameIPMultipleConnections(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 1},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c3", UserID: 1, SourceIP: "1.1.1.1"},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("same IP multiple conns should count as 1 device, got %d kicks", len(kicks))
}
}
func TestDeviceLimit_EmptySourceIP(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 1},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: ""},
{ID: "c2", UserID: 1, SourceIP: ""},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("empty source IP should be ignored, got %d kicks", len(kicks))
}
}
func TestMultipleUsers_DeviceLimits(t *testing.T) {
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, DeviceLimit: 1},
{ID: 2, DeviceLimit: 1},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 2, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 2, SourceIP: "3.3.3.3"},
}
kicks := l.Check(conns)
deviceKicks := 0
for _, k := range kicks {
if k.UserID == 2 {
deviceKicks++
}
}
if deviceKicks == 0 {
t.Error("expected device kick for user 2")
}
for _, k := range kicks {
if k.UserID == 1 {
t.Error("user 1 should not be kicked")
}
}
}
func TestNoSpeedKick(t *testing.T) {
// Speed limits should NOT cause kicks — only device limits and user removal
l := New()
l.UpdateUsers([]panel.User{
{ID: 1, SpeedLimit: 1, DeviceLimit: 0},
})
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
}
kicks := l.Check(conns)
if len(kicks) != 0 {
t.Errorf("speed limits should not cause kicks, got %d", len(kicks))
}
}
func TestKickUsers_Basic(t *testing.T) {
l := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 2, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 1, SourceIP: "1.1.1.2"},
{ID: "c4", UserID: 3, SourceIP: "3.3.3.3"},
}
kicks := l.KickUsers(conns, []int{1})
if len(kicks) != 2 {
t.Fatalf("expected 2 kicks for user 1, got %d", len(kicks))
}
for _, k := range kicks {
if k.UserID != 1 {
t.Errorf("expected kick for user 1, got user %d", k.UserID)
}
if k.Reason != "user removed from panel" {
t.Errorf("unexpected reason: %s", k.Reason)
}
}
}
func TestKickUsers_MultipleUsers(t *testing.T) {
l := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 2, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 3, SourceIP: "3.3.3.3"},
}
kicks := l.KickUsers(conns, []int{1, 3})
if len(kicks) != 2 {
t.Fatalf("expected 2 kicks, got %d", len(kicks))
}
kickedUsers := map[int]bool{}
for _, k := range kicks {
kickedUsers[k.UserID] = true
}
if !kickedUsers[1] || !kickedUsers[3] {
t.Errorf("expected users 1 and 3 kicked, got %v", kickedUsers)
}
}
func TestKickUsers_NoUsers(t *testing.T) {
l := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
}
kicks := l.KickUsers(conns, nil)
if len(kicks) != 0 {
t.Errorf("expected no kicks for nil user list, got %d", len(kicks))
}
kicks = l.KickUsers(conns, []int{})
if len(kicks) != 0 {
t.Errorf("expected no kicks for empty user list, got %d", len(kicks))
}
}
func TestKickUsers_NoMatchingConnections(t *testing.T) {
l := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, SourceIP: "1.1.1.1"},
}
kicks := l.KickUsers(conns, []int{99})
if len(kicks) != 0 {
t.Errorf("expected no kicks for non-existent user, got %d", len(kicks))
}
}
+99
View File
@@ -0,0 +1,99 @@
package limiter
import (
"sync"
"sync/atomic"
"golang.org/x/time/rate"
)
// SpeedTracker manages per-user token-bucket rate limiters.
// It does NOT wrap connections itself — instead, ConnTracker consults it
// via GetLimiter to embed rate limiting in the same tracked connection
// wrapper that does byte counting.
type SpeedTracker struct {
limiter *Limiter
mu sync.RWMutex
buckets map[int]*rate.Limiter // userID → shared rate limiter
uuidMap map[string]int // UUID → userID
// Fast-path: when no users have a speed limit, GetLimiter returns nil
// immediately without any map lookup.
hasLimits atomic.Bool
}
// NewSpeedTracker creates a bucket manager for per-user bandwidth throttling.
func NewSpeedTracker(l *Limiter) *SpeedTracker {
return &SpeedTracker{
limiter: l,
buckets: make(map[int]*rate.Limiter),
uuidMap: make(map[string]int),
}
}
// UpdateBuckets rebuilds rate limiter buckets based on current user config.
// Call this whenever the user list changes.
func (t *SpeedTracker) UpdateBuckets() {
t.mu.Lock()
defer t.mu.Unlock()
t.limiter.mu.RLock()
defer t.limiter.mu.RUnlock()
newBuckets := make(map[int]*rate.Limiter, len(t.limiter.users))
newUUIDMap := make(map[string]int, len(t.limiter.users))
for uid, user := range t.limiter.users {
if user.UUID != "" {
newUUIDMap[user.UUID] = uid
}
if user.SpeedLimit <= 0 {
continue
}
bytesPerSec := int(user.SpeedLimit) * 1_000_000 / 8 // Mbps → bytes/s
// Burst = 1 second of data, floored at 64 KiB, capped at 4 s.
// A generous burst ensures downloads ramp up quickly (good UX) while
// the token bucket still converges to the target rate within seconds.
// The 4 s cap prevents excessive burst at very high limits.
burst := bytesPerSec
if burst < 64*1024 {
burst = 64 * 1024
}
if cap4s := bytesPerSec * 4; cap4s > 64*1024 && burst > cap4s {
burst = cap4s
}
if existing, ok := t.buckets[uid]; ok {
existing.SetLimit(rate.Limit(bytesPerSec))
existing.SetBurst(burst)
newBuckets[uid] = existing
} else {
newBuckets[uid] = rate.NewLimiter(rate.Limit(bytesPerSec), burst)
}
}
t.buckets = newBuckets
t.uuidMap = newUUIDMap
t.hasLimits.Store(len(newBuckets) > 0)
}
// GetLimiter returns the rate limiter for the given user UUID, or nil if
// no limit applies. This is called by ConnTracker for every new connection.
// Thread-safe.
func (t *SpeedTracker) GetLimiter(user string) *rate.Limiter {
if !t.hasLimits.Load() {
return nil
}
t.mu.RLock()
defer t.mu.RUnlock()
return t.buckets[t.uuidMap[user]]
}
// HasLimits returns true if any user currently has a speed limit configured.
func (t *SpeedTracker) HasLimits() bool {
return t.hasLimits.Load()
}
// LimitedUserCount returns the number of users with active speed limits.
func (t *SpeedTracker) LimitedUserCount() int {
t.mu.RLock()
defer t.mu.RUnlock()
return len(t.buckets)
}
+123
View File
@@ -0,0 +1,123 @@
package limiter
import (
"testing"
"github.com/cedar2025/xboard-node/internal/panel"
)
func TestSpeedTracker_UpdateBuckets(t *testing.T) {
l := New()
st := NewSpeedTracker(l)
users := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 3, DeviceLimit: 0}, // 3 Mbps
{ID: 2, UUID: "u2", SpeedLimit: 0, DeviceLimit: 0}, // unlimited
{ID: 3, UUID: "u3", SpeedLimit: 10, DeviceLimit: 2}, // 10 Mbps
}
l.UpdateUsers(users)
st.UpdateBuckets()
// User 1: 3 Mbps = 375000 bytes/sec
b1 := st.GetLimiter("u1")
if b1 == nil {
t.Fatal("expected bucket for user 1")
}
expectedRate := float64(3 * 1000000 / 8)
if float64(b1.Limit()) != expectedRate {
t.Errorf("user 1 rate: got %v, want %v", float64(b1.Limit()), expectedRate)
}
// Burst should be max(bytesPerSec, 64KB) = max(375000, 65536) = 375000
expectedBurst := 3 * 1000000 / 8
if b1.Burst() != expectedBurst {
t.Errorf("user 1 burst: got %v, want %v", b1.Burst(), expectedBurst)
}
// User 2: no speed limit, no bucket
b2 := st.GetLimiter("u2")
if b2 != nil {
t.Fatal("expected no bucket for user 2")
}
// User 3: 10 Mbps = 1250000 bytes/sec
b3 := st.GetLimiter("u3")
if b3 == nil {
t.Fatal("expected bucket for user 3")
}
expectedRate3 := float64(10 * 1000000 / 8)
if float64(b3.Limit()) != expectedRate3 {
t.Errorf("user 3 rate: got %v, want %v", float64(b3.Limit()), expectedRate3)
}
// Burst should be max(1250000, 64KB) = 1250000
expectedBurst3 := 10 * 1000000 / 8
if b3.Burst() != expectedBurst3 {
t.Errorf("user 3 burst: got %v, want %v", b3.Burst(), expectedBurst3)
}
}
func TestSpeedTracker_UpdateBuckets_PreservesExisting(t *testing.T) {
l := New()
st := NewSpeedTracker(l)
users := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 3, DeviceLimit: 0},
}
l.UpdateUsers(users)
st.UpdateBuckets()
b1 := st.GetLimiter("u1")
if b1 == nil {
t.Fatal("expected bucket for user 1")
}
// Update same user with different speed — should reuse same *rate.Limiter pointer
users2 := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 5, DeviceLimit: 0},
}
l.UpdateUsers(users2)
st.UpdateBuckets()
b1After := st.GetLimiter("u1")
if b1After == nil {
t.Fatal("expected bucket for user 1 after update")
}
if b1 != b1After {
t.Error("expected same rate.Limiter instance to be reused")
}
// Rate should be updated
expectedRate := float64(5 * 1000000 / 8)
if float64(b1After.Limit()) != expectedRate {
t.Errorf("user 1 updated rate: got %v, want %v", float64(b1After.Limit()), expectedRate)
}
}
func TestSpeedTracker_UpdateBuckets_RemovesUsers(t *testing.T) {
l := New()
st := NewSpeedTracker(l)
users := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 3, DeviceLimit: 0},
{ID: 2, UUID: "u2", SpeedLimit: 5, DeviceLimit: 0},
}
l.UpdateUsers(users)
st.UpdateBuckets()
if st.GetLimiter("u1") == nil || st.GetLimiter("u2") == nil {
t.Fatal("expected buckets for both users")
}
// Remove user 2
users2 := []panel.User{
{ID: 1, UUID: "u1", SpeedLimit: 3, DeviceLimit: 0},
}
l.UpdateUsers(users2)
st.UpdateBuckets()
if st.GetLimiter("u1") == nil {
t.Fatal("expected bucket for user 1")
}
if st.GetLimiter("u2") != nil {
t.Error("expected no bucket for removed user 2")
}
}
+82
View File
@@ -0,0 +1,82 @@
package monitor
import (
"log/slog"
"runtime"
"time"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/mem"
)
var startTime = time.Now()
func init() {
// Warm up the CPU sampler. The first cpu.Percent call with interval=0
// always returns 0% because it has no prior sample. This throwaway call
// seeds the baseline so subsequent Collect() calls return real values.
cpu.Percent(500*time.Millisecond, false)
}
// Status holds system resource metrics
type Status struct {
Uptime uint64
CPU float64
CPUPerCore []float64
MemTotal uint64
MemUsed uint64
SwapTotal uint64
SwapUsed uint64
DiskTotal uint64
DiskUsed uint64
// GC metrics (process-wide)
NumGC uint32
LastPauseMS float64
}
// Collect gathers current system metrics
func Collect() Status {
var s Status
s.Uptime = uint64(time.Since(startTime).Seconds())
if cpuPercent, err := cpu.Percent(0, false); err == nil && len(cpuPercent) > 0 {
s.CPU = cpuPercent[0]
} else if err != nil {
slog.Debug("failed to get CPU usage", "error", err)
}
// Per-core CPU usage (best-effort; safe if it fails).
if perCore, err := cpu.Percent(0, true); err == nil && len(perCore) > 0 {
s.CPUPerCore = perCore
}
if vmStat, err := mem.VirtualMemory(); err == nil {
s.MemTotal = vmStat.Total
s.MemUsed = vmStat.Used
}
if swapStat, err := mem.SwapMemory(); err == nil {
s.SwapTotal = swapStat.Total
s.SwapUsed = swapStat.Used
}
if diskStat, err := disk.Usage("/"); err == nil {
s.DiskTotal = diskStat.Total
s.DiskUsed = diskStat.Used
}
// GC metrics
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
s.NumGC = ms.NumGC
if ms.NumGC > 0 {
// PauseNs is a ring buffer of the most recent GC pause times.
idx := (ms.NumGC - 1) % uint32(len(ms.PauseNs))
s.LastPauseMS = float64(ms.PauseNs[idx]) / 1e6
}
return s
}
+318
View File
@@ -0,0 +1,318 @@
package panel
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/cedar2025/xboard-node/internal/config"
)
// Client communicates with the Xboard panel API
type Client struct {
baseURL string
token string
nodeID int
nodeType string
httpClient *http.Client
configETag string
userETag string
apiSuccess atomic.Uint64
apiFailure atomic.Uint64
}
// NewClient creates a new panel API client
func NewClient(cfg config.PanelConfig) *Client {
return &Client{
baseURL: strings.TrimRight(cfg.URL, "/"),
token: cfg.Token,
nodeID: cfg.NodeID,
nodeType: cfg.NodeType,
httpClient: &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
},
},
}
}
// Handshake calls the new v2 API to get WS config + initial data in one shot.
func (c *Client) Handshake() (*HandshakeResponse, error) {
resp, err := c.doRequest("POST", "/api/v2/server/handshake", nil, "")
if err != nil {
return nil, fmt.Errorf("handshake: %w", err)
}
defer drainAndClose(resp.Body)
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return nil, fmt.Errorf("handshake status %d: %s", resp.StatusCode, body)
}
var hs HandshakeResponse
if err := json.NewDecoder(resp.Body).Decode(&hs); err != nil {
return nil, fmt.Errorf("decode handshake: %w", err)
}
return &hs, nil
}
// Report sends consolidated traffic + alive + status data to the panel.
// The optional metrics map allows the node to submit richer telemetry
// (active connections, per-core CPU, GC stats, limiter hits, etc.)
// without changing the core schema of status.
func (c *Client) Report(traffic map[int][2]int64, alive map[int][]string, online map[int]int,
cpu float64, mem, swap, disk [2]uint64,
metrics map[string]interface{},
) error {
payload := map[string]interface{}{}
if len(traffic) > 0 {
t := make(map[string][2]int64, len(traffic))
for uid, d := range traffic {
t[strconv.Itoa(uid)] = d
}
payload["traffic"] = t
}
if len(alive) > 0 {
a := make(map[string][]string, len(alive))
for uid, ips := range alive {
a[strconv.Itoa(uid)] = ips
}
payload["alive"] = a
}
if len(online) > 0 {
o := make(map[string]int, len(online))
for uid, count := range online {
o[strconv.Itoa(uid)] = count
}
payload["online"] = o
}
status := map[string]interface{}{
"cpu": cpu,
"mem": map[string]interface{}{"total": mem[0], "used": mem[1]},
"swap": map[string]interface{}{"total": swap[0], "used": swap[1]},
"disk": map[string]interface{}{"total": disk[0], "used": disk[1]},
}
payload["status"] = status
if len(metrics) > 0 {
payload["metrics"] = metrics
}
return c.postJSON("/api/v2/server/report", payload)
}
// GetConfig fetches node configuration. Returns nil if not modified (304).
func (c *Client) GetConfig() (*NodeConfig, error) {
resp, err := c.doRequest("GET", "/api/v1/server/UniProxy/config", nil, c.configETag)
if err != nil {
return nil, fmt.Errorf("get config: %w", err)
}
defer drainAndClose(resp.Body)
if resp.StatusCode == http.StatusNotModified {
return nil, nil
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, body)
}
var cfg NodeConfig
if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if etag := resp.Header.Get("ETag"); etag != "" {
c.configETag = etag
}
return &cfg, nil
}
// GetUsers fetches available users. Returns nil if not modified (304).
func (c *Client) GetUsers() ([]User, error) {
resp, err := c.doRequest("GET", "/api/v1/server/UniProxy/user", nil, c.userETag)
if err != nil {
return nil, fmt.Errorf("get users: %w", err)
}
defer drainAndClose(resp.Body)
if resp.StatusCode == http.StatusNotModified {
return nil, nil
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, body)
}
var usersResp UsersResponse
if err := json.NewDecoder(resp.Body).Decode(&usersResp); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if etag := resp.Header.Get("ETag"); etag != "" {
c.userETag = etag
}
return usersResp.Users, nil
}
// PushTraffic submits per-user traffic data
func (c *Client) PushTraffic(data map[int][2]int64) error {
if len(data) == 0 {
return nil
}
payload := make(map[string]interface{}, len(data))
for uid, traffic := range data {
payload[strconv.Itoa(uid)] = traffic
}
return c.postJSON("/api/v1/server/UniProxy/push", payload)
}
// PushAlive submits online user IPs
func (c *Client) PushAlive(data map[int][]string) error {
if len(data) == 0 {
return nil
}
payload := make(map[string]interface{}, len(data))
for uid, ips := range data {
payload[strconv.Itoa(uid)] = ips
}
return c.postJSON("/api/v1/server/UniProxy/alive", payload)
}
// PushStatus submits system status to the panel
func (c *Client) PushStatus(cpu float64, mem, swap, disk [2]uint64) error {
payload := map[string]interface{}{
"cpu": cpu,
"mem": map[string]interface{}{"total": mem[0], "used": mem[1]},
"swap": map[string]interface{}{"total": swap[0], "used": swap[1]},
"disk": map[string]interface{}{"total": disk[0], "used": disk[1]},
}
return c.postJSON("/api/v1/server/UniProxy/status", payload)
}
// ResetETags clears cached ETags, forcing full responses
func (c *Client) ResetETags() {
c.configETag = ""
c.userETag = ""
}
// postJSON marshals a map payload (with auth fields injected) and POSTs it.
// Auth fields are injected before marshalling — single marshal pass.
func (c *Client) postJSON(path string, payload map[string]interface{}) error {
payload["token"] = c.token
payload["node_id"] = c.nodeID
if c.nodeType != "" {
payload["node_type"] = c.nodeType
}
body, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
resp, err := c.doRequest("POST", path, body, "")
if err != nil {
return fmt.Errorf("post %s: %w", path, err)
}
defer drainAndClose(resp.Body)
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return fmt.Errorf("status %d: %s", resp.StatusCode, respBody)
}
c.apiSuccess.Add(1)
return nil
}
func (c *Client) doRequest(method, path string, body []byte, ifNoneMatch string) (*http.Response, error) {
fullURL := c.baseURL + path
var bodyReader io.Reader
if method == "GET" {
q := url.Values{}
q.Set("token", c.token)
q.Set("node_id", strconv.Itoa(c.nodeID))
if c.nodeType != "" {
q.Set("node_type", c.nodeType)
}
fullURL += "?" + q.Encode()
} else if body != nil {
bodyReader = bytes.NewReader(body)
} else {
authOnly := map[string]interface{}{
"token": c.token,
"node_id": c.nodeID,
}
if c.nodeType != "" {
authOnly["node_type"] = c.nodeType
}
merged, _ := json.Marshal(authOnly)
bodyReader = bytes.NewReader(merged)
}
req, err := http.NewRequest(method, fullURL, bodyReader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if ifNoneMatch != "" {
req.Header.Set("If-None-Match", ifNoneMatch)
}
slog.Debug("panel request", "method", method, "path", path)
resp, err := c.httpClient.Do(req)
if err != nil {
c.apiFailure.Add(1)
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
c.apiSuccess.Add(1)
} else {
c.apiFailure.Add(1)
}
slog.Debug("panel response", "method", method, "path", path, "status", resp.StatusCode)
return resp, nil
}
// drainAndClose reads any remaining bytes (up to 512B) and closes the body.
// This allows the HTTP transport to reuse the underlying TCP connection.
func drainAndClose(body io.ReadCloser) {
io.CopyN(io.Discard, body, 512)
body.Close()
}
// APIMetrics holds aggregated API call statistics.
type APIMetrics struct {
Success uint64
Failure uint64
}
// SnapshotMetrics returns a snapshot of API metrics.
func (c *Client) SnapshotMetrics() APIMetrics {
return APIMetrics{
Success: c.apiSuccess.Load(),
Failure: c.apiFailure.Load(),
}
}
+295
View File
@@ -0,0 +1,295 @@
package panel
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/cedar2025/xboard-node/internal/config"
)
func newTestServer(handler http.HandlerFunc) (*httptest.Server, *Client) {
ts := httptest.NewServer(handler)
client := NewClient(config.PanelConfig{
URL: ts.URL,
Token: "test-token",
NodeID: 1,
})
return ts, client
}
func TestGetConfig_Success(t *testing.T) {
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/server/UniProxy/config" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
if r.URL.Query().Get("token") != "test-token" {
t.Errorf("missing token in query")
}
if r.URL.Query().Get("node_id") != "1" {
t.Errorf("missing node_id in query")
}
w.Header().Set("ETag", `"etag-1"`)
json.NewEncoder(w).Encode(NodeConfig{
Protocol: "shadowsocks",
ServerPort: 111,
Cipher: "aes-128-gcm",
})
})
defer ts.Close()
cfg, err := client.GetConfig()
if err != nil {
t.Fatalf("GetConfig: %v", err)
}
if cfg == nil {
t.Fatal("expected non-nil config")
}
if cfg.Protocol != "shadowsocks" {
t.Errorf("protocol: got %q", cfg.Protocol)
}
if cfg.ServerPort != 111 {
t.Errorf("server_port: got %d", cfg.ServerPort)
}
}
func TestGetConfig_NotModified(t *testing.T) {
callCount := 0
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
callCount++
if callCount == 1 {
w.Header().Set("ETag", `"etag-1"`)
json.NewEncoder(w).Encode(NodeConfig{Protocol: "shadowsocks"})
return
}
if r.Header.Get("If-None-Match") != `"etag-1"` {
t.Errorf("expected If-None-Match header, got %q", r.Header.Get("If-None-Match"))
}
w.WriteHeader(http.StatusNotModified)
})
defer ts.Close()
// First call — get config
cfg, err := client.GetConfig()
if err != nil || cfg == nil {
t.Fatalf("first GetConfig: err=%v cfg=%v", err, cfg)
}
// Second call — should be 304
cfg, err = client.GetConfig()
if err != nil {
t.Fatalf("second GetConfig: %v", err)
}
if cfg != nil {
t.Error("expected nil config for 304")
}
}
func TestGetConfig_ServerError(t *testing.T) {
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal error"))
})
defer ts.Close()
_, err := client.GetConfig()
if err == nil {
t.Fatal("expected error for 500 response")
}
}
func TestGetUsers_Success(t *testing.T) {
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/server/UniProxy/user" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
w.Header().Set("ETag", `"users-etag-1"`)
json.NewEncoder(w).Encode(UsersResponse{
Users: []User{
{ID: 1, UUID: "uuid-1", SpeedLimit: 3, DeviceLimit: 2},
{ID: 2, UUID: "uuid-2", SpeedLimit: 0, DeviceLimit: 0},
},
})
})
defer ts.Close()
users, err := client.GetUsers()
if err != nil {
t.Fatalf("GetUsers: %v", err)
}
if len(users) != 2 {
t.Fatalf("users count: got %d", len(users))
}
if users[0].UUID != "uuid-1" {
t.Errorf("user[0].uuid: got %q", users[0].UUID)
}
}
func TestGetUsers_NotModified(t *testing.T) {
callCount := 0
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
callCount++
if callCount == 1 {
w.Header().Set("ETag", `"u-etag"`)
json.NewEncoder(w).Encode(UsersResponse{Users: []User{{ID: 1, UUID: "u1"}}})
return
}
w.WriteHeader(http.StatusNotModified)
})
defer ts.Close()
users, _ := client.GetUsers()
if len(users) != 1 {
t.Fatalf("first call: got %d users", len(users))
}
users, err := client.GetUsers()
if err != nil {
t.Fatalf("second GetUsers: %v", err)
}
if users != nil {
t.Error("expected nil for 304")
}
}
func TestPushTraffic_Success(t *testing.T) {
var received map[string]interface{}
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/server/UniProxy/push" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
json.NewDecoder(r.Body).Decode(&received)
w.WriteHeader(http.StatusOK)
})
defer ts.Close()
data := map[int][2]int64{
1: {1024, 2048},
2: {512, 256},
}
if err := client.PushTraffic(data); err != nil {
t.Fatalf("PushTraffic: %v", err)
}
if received == nil {
t.Fatal("server received nil payload")
}
// Verify token was injected
if received["token"] != "test-token" {
t.Errorf("token: got %v", received["token"])
}
}
func TestPushTraffic_Empty(t *testing.T) {
called := false
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusOK)
})
defer ts.Close()
if err := client.PushTraffic(nil); err != nil {
t.Fatalf("PushTraffic nil: %v", err)
}
if called {
t.Error("should not call server for empty traffic")
}
}
func TestPushAlive_Success(t *testing.T) {
var received map[string]interface{}
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/server/UniProxy/alive" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
json.NewDecoder(r.Body).Decode(&received)
w.WriteHeader(http.StatusOK)
})
defer ts.Close()
data := map[int][]string{
1: {"1.2.3.4", "5.6.7.8"},
}
if err := client.PushAlive(data); err != nil {
t.Fatalf("PushAlive: %v", err)
}
if received == nil {
t.Fatal("server received nil")
}
}
func TestPushStatus_Success(t *testing.T) {
var received map[string]interface{}
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/server/UniProxy/status" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
json.NewDecoder(r.Body).Decode(&received)
w.WriteHeader(http.StatusOK)
})
defer ts.Close()
err := client.PushStatus(50.0, [2]uint64{8192, 4096}, [2]uint64{2048, 1024}, [2]uint64{100000, 50000})
if err != nil {
t.Fatalf("PushStatus: %v", err)
}
if received["cpu"].(float64) != 50.0 {
t.Errorf("cpu: got %v", received["cpu"])
}
}
func TestResetETags(t *testing.T) {
callCount := 0
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
callCount++
if callCount <= 2 {
w.Header().Set("ETag", `"e1"`)
json.NewEncoder(w).Encode(NodeConfig{Protocol: "ss"})
return
}
// After reset, should NOT send If-None-Match
if r.Header.Get("If-None-Match") != "" {
t.Errorf("expected no If-None-Match after reset, got %q", r.Header.Get("If-None-Match"))
}
json.NewEncoder(w).Encode(NodeConfig{Protocol: "ss"})
})
defer ts.Close()
client.GetConfig()
client.ResetETags()
client.GetConfig()
}
func TestPushTraffic_ServerError(t *testing.T) {
ts, client := newTestServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("unavailable"))
})
defer ts.Close()
data := map[int][2]int64{1: {100, 200}}
err := client.PushTraffic(data)
if err == nil {
t.Fatal("expected error for 503")
}
}
func TestClient_NodeTypeInQuery(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("node_type") != "vmess" {
t.Errorf("node_type: got %q, want vmess", r.URL.Query().Get("node_type"))
}
json.NewEncoder(w).Encode(NodeConfig{Protocol: "vmess"})
}))
defer ts.Close()
client := NewClient(config.PanelConfig{
URL: ts.URL,
Token: "tok",
NodeID: 1,
NodeType: "vmess",
})
client.GetConfig()
}
+159
View File
@@ -0,0 +1,159 @@
package panel
import (
"encoding/json"
"fmt"
"strings"
)
// StringOrArray is a type that can unmarshal from either a JSON string or an array of strings.
// When it's an array, the elements are joined with newlines.
type StringOrArray string
func (s *StringOrArray) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err == nil {
*s = StringOrArray(str)
return nil
}
var arr []string
if err := json.Unmarshal(data, &arr); err == nil {
*s = StringOrArray(strings.Join(arr, "\n"))
return nil
}
return fmt.Errorf("StringOrArray: expected string or []string, got %s", string(data))
}
// HandshakeResponse is the response from POST /api/v2/server/handshake
type HandshakeResponse struct {
WebSocket WSConfig `json:"websocket"`
Settings Settings `json:"settings"`
}
func (h *HandshakeResponse) UnmarshalJSON(data []byte) error {
type Alias HandshakeResponse
aux := &struct {
Settings json.RawMessage `json:"settings"`
*Alias
}{
Alias: (*Alias)(h),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// If settings is an empty array [], skip unmarshaling it into the struct
if len(aux.Settings) > 0 && string(aux.Settings) != "[]" {
if err := json.Unmarshal(aux.Settings, &h.Settings); err != nil {
return err
}
}
return nil
}
// WSConfig holds WebSocket connection settings from the panel
type WSConfig struct {
Enabled bool `json:"enabled"`
WSURL string `json:"ws_url,omitempty"`
}
// Settings holds panel-defined intervals
type Settings struct {
PushInterval int `json:"push_interval"`
PullInterval int `json:"pull_interval"`
}
// NodeConfig is the response from GET /api/v1/server/UniProxy/config
type NodeConfig struct {
Protocol string `json:"protocol"`
ListenIP string `json:"listen_ip"`
ServerPort int `json:"server_port"`
Network string `json:"network"`
NetworkSettings map[string]interface{} `json:"networkSettings"`
BaseConfig BaseConfig `json:"base_config"`
Routes []RouteRule `json:"routes"`
// Kernel settings (Xboard extension)
KernelType string `json:"kernel_type,omitempty"` // "singbox" or "xray"
KernelLogLevel string `json:"kernel_log_level,omitempty"` // "info", "warn", etc.
CustomOutbounds []OutboundConfig `json:"custom_outbounds,omitempty"`
CustomRoutes []map[string]any `json:"custom_routes,omitempty"`
// Certificate settings (Xboard extension)
CertConfig *CertConfig `json:"cert_config,omitempty"`
AutoTLS bool `json:"auto_tls,omitempty"` // Deprecated: use CertConfig
Domain string `json:"domain,omitempty"` // Deprecated: use CertConfig
// Shadowsocks
Cipher string `json:"cipher,omitempty"`
Plugin string `json:"plugin,omitempty"`
PluginOpt string `json:"plugin_opts,omitempty"`
ServerKey string `json:"server_key,omitempty"`
// VMess / VLESS
TLS int `json:"tls,omitempty"`
Flow string `json:"flow,omitempty"`
TLSSettings map[string]interface{} `json:"tls_settings,omitempty"`
// Trojan
Host string `json:"host,omitempty"`
ServerName string `json:"server_name,omitempty"`
// Hysteria
Version int `json:"version,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs string `json:"obfs,omitempty"`
ObfsPassword string `json:"obfs-password,omitempty"`
// TUIC
CongestionControl string `json:"congestion_control,omitempty"`
// AnyTLS
PaddingScheme StringOrArray `json:"padding_scheme,omitempty"`
}
// CertConfig holds certificate automation settings from the panel
type CertConfig struct {
CertMode string `json:"cert_mode"` // none, dns, http, self, file
Domain string `json:"domain"` // Certificate domain
Email string `json:"email"` // ACME email
DNSProvider string `json:"dns_provider"` // dns mode: cloudflare, alidns, etc.
DNSEnv map[string]string `json:"dns_env"` // Provider-specific API keys/tokens
HTTPPort int `json:"http_port"` // ACME HTTP-01 local port (default 80)
CertFile string `json:"cert_file"` // file mode: path to cert file
KeyFile string `json:"key_file"` // file mode: path to key file
CertContent string `json:"cert_content"` // content mode: certificate raw string
KeyContent string `json:"key_content"` // content mode: private key raw string
}
// OutboundConfig defines a custom outbound for kernel
type OutboundConfig struct {
Tag string `json:"tag"` // Unique tag for routing
Protocol string `json:"protocol"` // vmess, vless, shadowsocks, wireguard, etc.
Settings map[string]any `json:"settings,omitempty"` // Protocol-specific settings
ProxyTag string `json:"proxy_tag,omitempty"` // Chain proxy: next outbound tag
}
type BaseConfig struct {
PushInterval int `json:"push_interval"`
PullInterval int `json:"pull_interval"`
}
type RouteRule struct {
ID int `json:"id"`
Match []string `json:"match"`
Action string `json:"action"`
ActionValue string `json:"action_value,omitempty"`
}
// User represents a user returned by the panel
type User struct {
ID int `json:"id"`
UUID string `json:"uuid"`
SpeedLimit int `json:"speed_limit"` // Mbps, 0 = unlimited
DeviceLimit int `json:"device_limit"` // max devices, 0 = unlimited
}
type UsersResponse struct {
Users []User `json:"users"`
}
+140
View File
@@ -0,0 +1,140 @@
package panel
import (
"encoding/json"
"testing"
)
func TestStringOrArray_UnmarshalString(t *testing.T) {
input := `"hello world"`
var s StringOrArray
if err := json.Unmarshal([]byte(input), &s); err != nil {
t.Fatalf("unmarshal string: %v", err)
}
if string(s) != "hello world" {
t.Errorf("got %q, want %q", s, "hello world")
}
}
func TestStringOrArray_UnmarshalArray(t *testing.T) {
input := `["stop=8","0=30-30","1=100-400"]`
var s StringOrArray
if err := json.Unmarshal([]byte(input), &s); err != nil {
t.Fatalf("unmarshal array: %v", err)
}
want := "stop=8\n0=30-30\n1=100-400"
if string(s) != want {
t.Errorf("got %q, want %q", s, want)
}
}
func TestStringOrArray_UnmarshalEmptyArray(t *testing.T) {
input := `[]`
var s StringOrArray
if err := json.Unmarshal([]byte(input), &s); err != nil {
t.Fatalf("unmarshal empty array: %v", err)
}
if string(s) != "" {
t.Errorf("got %q, want empty", s)
}
}
func TestStringOrArray_UnmarshalNull(t *testing.T) {
input := `null`
var s StringOrArray
if err := json.Unmarshal([]byte(input), &s); err != nil {
t.Fatalf("unmarshal null: %v", err)
}
if string(s) != "" {
t.Errorf("got %q, want empty", s)
}
}
func TestNodeConfig_UnmarshalWithPaddingSchemeArray(t *testing.T) {
input := `{
"protocol": "anytls",
"server_port": 443,
"padding_scheme": ["stop=8", "0=30-30", "1=100-400"]
}`
var nc NodeConfig
if err := json.Unmarshal([]byte(input), &nc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if nc.Protocol != "anytls" {
t.Errorf("protocol: got %q, want %q", nc.Protocol, "anytls")
}
want := "stop=8\n0=30-30\n1=100-400"
if string(nc.PaddingScheme) != want {
t.Errorf("padding_scheme: got %q, want %q", nc.PaddingScheme, want)
}
}
func TestNodeConfig_UnmarshalWithPaddingSchemeString(t *testing.T) {
input := `{
"protocol": "anytls",
"server_port": 443,
"padding_scheme": "stop=8\n0=30-30"
}`
var nc NodeConfig
if err := json.Unmarshal([]byte(input), &nc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
want := "stop=8\n0=30-30"
if string(nc.PaddingScheme) != want {
t.Errorf("padding_scheme: got %q, want %q", nc.PaddingScheme, want)
}
}
func TestNodeConfig_UnmarshalFullPanelResponse(t *testing.T) {
input := `{
"protocol": "shadowsocks",
"listen_ip": "0.0.0.0",
"server_port": 111,
"cipher": "aes-128-gcm",
"network": null,
"networkSettings": null,
"server_name": "",
"base_config": {
"push_interval": 60,
"pull_interval": 60
}
}`
var nc NodeConfig
if err := json.Unmarshal([]byte(input), &nc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if nc.Protocol != "shadowsocks" {
t.Errorf("protocol: got %q", nc.Protocol)
}
if nc.ServerPort != 111 {
t.Errorf("server_port: got %d", nc.ServerPort)
}
if nc.Cipher != "aes-128-gcm" {
t.Errorf("cipher: got %q", nc.Cipher)
}
if nc.BaseConfig.PushInterval != 60 {
t.Errorf("push_interval: got %d", nc.BaseConfig.PushInterval)
}
}
func TestUsersResponse_Unmarshal(t *testing.T) {
input := `{
"users": [
{"id": 1, "uuid": "abc-123", "speed_limit": 3, "device_limit": 2},
{"id": 2, "uuid": "def-456", "speed_limit": 0, "device_limit": 0}
]
}`
var resp UsersResponse
if err := json.Unmarshal([]byte(input), &resp); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(resp.Users) != 2 {
t.Fatalf("users count: got %d, want 2", len(resp.Users))
}
if resp.Users[0].SpeedLimit != 3 {
t.Errorf("user[0].speed_limit: got %d", resp.Users[0].SpeedLimit)
}
if resp.Users[1].DeviceLimit != 0 {
t.Errorf("user[1].device_limit: got %d", resp.Users[1].DeviceLimit)
}
}
+302
View File
@@ -0,0 +1,302 @@
package panel
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/url"
"strconv"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
// WSEvent types
const (
WSEventSyncConfig = "sync.config"
WSEventSyncUsers = "sync.users"
WSEventSyncUserDelta = "sync.user.delta"
)
// WSEvent is a parsed data event delivered to the service layer.
type WSEvent struct {
Type string
Config *NodeConfig
Users []User
DeltaAction string // "add" or "remove" (only for sync.user.delta)
DeltaUsers []User // users affected by the delta
}
// WSStatusChange notifies the service when WS connectivity changes.
type WSStatusChange struct {
Connected bool
}
// wsMessage is the JSON envelope for all WS messages.
type wsMessage struct {
Event string `json:"event"`
Data json.RawMessage `json:"data,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
}
// Payload structures for data events
type syncConfigPayload struct {
Config NodeConfig `json:"config"`
Timestamp int64 `json:"timestamp"`
}
type syncUsersPayload struct {
Users []User `json:"users"`
Timestamp int64 `json:"timestamp"`
}
type syncUserDeltaPayload struct {
Action string `json:"action"`
Users []User `json:"users"`
Timestamp int64 `json:"timestamp"`
}
// WSClient connects to the panel's Workerman WS server using native WebSocket.
// Authentication is done via query parameters (token + node_id) during the
// WebSocket handshake — no separate auth step needed.
type WSClient struct {
wsURL string // base WS URL, e.g. ws://panel.example.com:8076
token string
nodeID int
onEvent func(WSEvent)
onStatus func(WSStatusChange)
connected atomic.Bool
}
// NewWSClient creates a new WebSocket client.
// wsURL is the base WebSocket URL (e.g. "ws://panel.example.com:8076").
// token and nodeID are used for authentication via query parameters.
func NewWSClient(wsURL string, token string, nodeID int, onEvent func(WSEvent), onStatus func(WSStatusChange)) *WSClient {
return &WSClient{
wsURL: wsURL,
token: token,
nodeID: nodeID,
onEvent: onEvent,
onStatus: onStatus,
}
}
func (w *WSClient) IsConnected() bool { return w.connected.Load() }
func (w *WSClient) notifyStatus(connected bool) {
if w.onStatus != nil {
w.onStatus(WSStatusChange{Connected: connected})
}
}
// Run connects and reconnects until ctx is cancelled.
func (w *WSClient) Run(ctx context.Context) {
backoff := time.Second
for {
start := time.Now()
err := w.connect(ctx)
wasConnected := w.connected.Swap(false)
if wasConnected {
w.notifyStatus(false)
}
if err != nil {
slog.Warn("ws disconnected", "error", err)
if !wasConnected {
w.notifyStatus(false)
}
}
select {
case <-ctx.Done():
return
default:
}
// Reset backoff if the connection was up for a meaningful duration.
if time.Since(start) > 2*time.Minute {
backoff = time.Second
}
timer := time.NewTimer(backoff)
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
}
if backoff < 30*time.Second {
backoff = min(backoff*2, 30*time.Second)
}
}
}
func (w *WSClient) connect(ctx context.Context) error {
// Build WS URL with auth query params
u, err := url.Parse(w.wsURL)
if err != nil {
return fmt.Errorf("parse ws url: %w", err)
}
q := u.Query()
q.Set("token", w.token)
q.Set("node_id", strconv.Itoa(w.nodeID))
u.RawQuery = q.Encode()
slog.Debug("ws connecting", "url", u.Redacted())
dialer := websocket.Dialer{
HandshakeTimeout: 15 * time.Second,
}
conn, _, err := dialer.DialContext(ctx, u.String(), nil)
if err != nil {
return fmt.Errorf("dial: %w", err)
}
defer conn.Close()
conn.SetReadLimit(10 << 20) // 10MB max message size
// Read first message — expect auth.success or error
var firstMsg wsMessage
if err := conn.ReadJSON(&firstMsg); err != nil {
return fmt.Errorf("read auth response: %w", err)
}
if firstMsg.Event == "error" {
var errData struct {
Message string `json:"message"`
}
json.Unmarshal(firstMsg.Data, &errData)
return fmt.Errorf("auth failed: %s", errData.Message)
}
if firstMsg.Event != "auth.success" {
// It might be a data event already (server pushed sync before auth.success)
// Process it and continue
w.connected.Store(true)
w.notifyStatus(true)
slog.Info("ws connected (implicit auth)")
w.handleMessage(firstMsg)
} else {
w.connected.Store(true)
w.notifyStatus(true)
slog.Info("ws connected and authenticated")
}
// Ping interval: send pong responses to server pings
const pingInterval = 50 * time.Second
msgCh := make(chan wsMessage, 16)
errCh := make(chan error, 1)
done := make(chan struct{})
go func() {
defer close(done)
for {
var msg wsMessage
if err := conn.ReadJSON(&msg); err != nil {
select {
case errCh <- err:
default:
}
return
}
select {
case msgCh <- msg:
case <-ctx.Done():
return
}
}
}()
pingTimer := time.NewTimer(pingInterval)
defer pingTimer.Stop()
for {
select {
case <-ctx.Done():
conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
<-done
return nil
case err := <-errCh:
return fmt.Errorf("read: %w", err)
case msg := <-msgCh:
pingTimer.Reset(pingInterval)
w.handleMessage(msg)
case <-pingTimer.C:
// Send pong to keep connection alive
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err := conn.WriteJSON(wsMessage{Event: "pong"}); err != nil {
return fmt.Errorf("pong: %w", err)
}
pingTimer.Reset(pingInterval)
}
}
}
func (w *WSClient) handleMessage(msg wsMessage) {
switch msg.Event {
case "ping":
// Server ping — handled by the pong timer reset above
slog.Debug("ws received ping")
case "auth.success":
slog.Debug("ws auth confirmed")
case WSEventSyncConfig:
w.handleDataEvent(msg)
case WSEventSyncUsers:
w.handleDataEvent(msg)
case WSEventSyncUserDelta:
w.handleDataEvent(msg)
default:
slog.Debug("ws unknown event", "event", msg.Event)
}
}
func (w *WSClient) handleDataEvent(msg wsMessage) {
var event WSEvent
event.Type = msg.Event
switch msg.Event {
case WSEventSyncConfig:
slog.Info("ws sync config event received")
var p syncConfigPayload
if err := json.Unmarshal(msg.Data, &p); err != nil {
slog.Warn("ws: cannot decode config payload", "error", err)
return
}
event.Config = &p.Config
case WSEventSyncUsers:
slog.Info("ws sync users event received")
var p syncUsersPayload
if err := json.Unmarshal(msg.Data, &p); err != nil {
slog.Warn("ws: cannot decode users payload", "error", err)
return
}
event.Users = p.Users
case WSEventSyncUserDelta:
slog.Info("ws sync user delta event received")
var p syncUserDeltaPayload
if err := json.Unmarshal(msg.Data, &p); err != nil {
slog.Warn("ws: cannot decode user delta payload", "error", err)
return
}
event.DeltaAction = p.Action
event.DeltaUsers = p.Users
}
w.onEvent(event)
}
+291
View File
@@ -0,0 +1,291 @@
package panel
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/gorilla/websocket"
)
// fakeWSServer simulates a minimal Workerman-style WS server for testing.
// It authenticates via token/node_id query params, sends auth.success, then
// delivers the provided events and handles pongs until the client disconnects.
func fakeWSServer(t *testing.T, events []wsMessage) *httptest.Server {
t.Helper()
upgrader := websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Validate query params
q := r.URL.Query()
if q.Get("token") == "" || q.Get("node_id") == "" {
http.Error(w, "missing auth params", http.StatusUnauthorized)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
t.Logf("upgrade error: %v", err)
return
}
defer conn.Close()
// Send auth.success
if err := conn.WriteJSON(wsMessage{Event: "auth.success"}); err != nil {
return
}
// Send test events
for _, evt := range events {
time.Sleep(50 * time.Millisecond)
if err := conn.WriteJSON(evt); err != nil {
return
}
}
// Handle pongs/messages until connection closes
for {
var msg wsMessage
if err := conn.ReadJSON(&msg); err != nil {
return
}
if msg.Event == "pong" {
// Client pong — acknowledged
}
}
}))
}
func TestWSClient_ConnectAndReceiveDataEvents(t *testing.T) {
configPayload := syncConfigPayload{
Config: NodeConfig{Protocol: "vless", ServerPort: 443},
Timestamp: 1234,
}
usersPayload := syncUsersPayload{
Users: []User{{ID: 1, UUID: "abc", SpeedLimit: 100, DeviceLimit: 3}},
Timestamp: 1235,
}
configData, _ := json.Marshal(configPayload)
usersData, _ := json.Marshal(usersPayload)
events := []wsMessage{
{Event: WSEventSyncConfig, Data: configData},
{Event: WSEventSyncUsers, Data: usersData},
}
server := fakeWSServer(t, events)
defer server.Close()
host := strings.TrimPrefix(server.URL, "http://")
var mu sync.Mutex
var received []WSEvent
ws := NewWSClient("ws://"+host, "test-token", 1, func(event WSEvent) {
mu.Lock()
received = append(received, event)
mu.Unlock()
}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
go ws.Run(ctx)
// Wait for events
deadline := time.After(1500 * time.Millisecond)
for {
mu.Lock()
n := len(received)
mu.Unlock()
if n >= 2 {
break
}
select {
case <-deadline:
t.Fatalf("timed out waiting for events, got %d", n)
case <-time.After(50 * time.Millisecond):
}
}
mu.Lock()
defer mu.Unlock()
if len(received) != 2 {
t.Fatalf("expected 2 events, got %d", len(received))
}
// Verify config event
if received[0].Type != WSEventSyncConfig {
t.Errorf("event[0].Type = %q, want %q", received[0].Type, WSEventSyncConfig)
}
if received[0].Config == nil {
t.Fatal("event[0].Config is nil")
}
if received[0].Config.Protocol != "vless" {
t.Errorf("config.Protocol = %q, want %q", received[0].Config.Protocol, "vless")
}
if received[0].Config.ServerPort != 443 {
t.Errorf("config.ServerPort = %d, want 443", received[0].Config.ServerPort)
}
// Verify users event
if received[1].Type != WSEventSyncUsers {
t.Errorf("event[1].Type = %q, want %q", received[1].Type, WSEventSyncUsers)
}
if len(received[1].Users) != 1 {
t.Fatalf("expected 1 user, got %d", len(received[1].Users))
}
if received[1].Users[0].UUID != "abc" {
t.Errorf("user.UUID = %q, want %q", received[1].Users[0].UUID, "abc")
}
if !ws.IsConnected() {
t.Error("expected IsConnected() = true while server is running")
}
}
func TestWSClient_ReconnectOnDisconnect(t *testing.T) {
upgrader := websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
var mu sync.Mutex
connectCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
mu.Lock()
connectCount++
count := connectCount
mu.Unlock()
// Send auth.success
conn.WriteJSON(wsMessage{Event: "auth.success"})
// Close immediately on first connection to trigger reconnect
if count == 1 {
conn.Close()
return
}
// Keep second connection alive
for {
var msg wsMessage
if err := conn.ReadJSON(&msg); err != nil {
return
}
}
}))
defer server.Close()
host := strings.TrimPrefix(server.URL, "http://")
ws := NewWSClient("ws://"+host, "reconnect-token", 1, func(WSEvent) {}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
go ws.Run(ctx)
// Wait for reconnection
deadline := time.After(4 * time.Second)
for {
mu.Lock()
n := connectCount
mu.Unlock()
if n >= 2 {
break
}
select {
case <-deadline:
mu.Lock()
t.Fatalf("expected at least 2 connections, got %d", connectCount)
mu.Unlock()
case <-time.After(100 * time.Millisecond):
}
}
if !ws.IsConnected() {
t.Error("expected IsConnected() = true after reconnect")
}
}
func TestWSClient_FallbackWhenNoServer(t *testing.T) {
ws := NewWSClient("ws://127.0.0.1:19999", "fallback-token", 1, func(WSEvent) {}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
go ws.Run(ctx)
time.Sleep(500 * time.Millisecond)
if ws.IsConnected() {
t.Error("expected IsConnected() = false when no server")
}
}
func TestWSClient_UserDeltaEvent(t *testing.T) {
deltaPayload := syncUserDeltaPayload{
Action: "add",
Users: []User{{ID: 42, UUID: "delta-uuid"}},
Timestamp: 1236,
}
deltaData, _ := json.Marshal(deltaPayload)
events := []wsMessage{
{Event: WSEventSyncUserDelta, Data: deltaData},
}
server := fakeWSServer(t, events)
defer server.Close()
host := strings.TrimPrefix(server.URL, "http://")
var mu sync.Mutex
var received []WSEvent
ws := NewWSClient("ws://"+host, "delta-token", 1, func(event WSEvent) {
mu.Lock()
received = append(received, event)
mu.Unlock()
}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
go ws.Run(ctx)
deadline := time.After(1500 * time.Millisecond)
for {
mu.Lock()
n := len(received)
mu.Unlock()
if n >= 1 {
break
}
select {
case <-deadline:
t.Fatal("timed out waiting for delta event")
case <-time.After(50 * time.Millisecond):
}
}
mu.Lock()
defer mu.Unlock()
if received[0].Type != WSEventSyncUserDelta {
t.Errorf("event.Type = %q, want %q", received[0].Type, WSEventSyncUserDelta)
}
if received[0].DeltaAction != "add" {
t.Errorf("event.DeltaAction = %q, want %q", received[0].DeltaAction, "add")
}
if len(received[0].DeltaUsers) != 1 || received[0].DeltaUsers[0].ID != 42 {
t.Errorf("unexpected DeltaUsers: %+v", received[0].DeltaUsers)
}
}
+874
View File
@@ -0,0 +1,874 @@
package service
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"log/slog"
"math"
"sort"
"time"
"github.com/cedar2025/xboard-node/internal/cert"
"github.com/cedar2025/xboard-node/internal/config"
"github.com/cedar2025/xboard-node/internal/kernel"
"github.com/cedar2025/xboard-node/internal/kernel/singbox"
"github.com/cedar2025/xboard-node/internal/kernel/xray"
"github.com/cedar2025/xboard-node/internal/limiter"
"github.com/cedar2025/xboard-node/internal/monitor"
"github.com/cedar2025/xboard-node/internal/panel"
"github.com/cedar2025/xboard-node/internal/tracker"
)
type Service struct {
cfg *config.Config
panel *panel.Client
kernel kernel.Kernel
tracker *tracker.Tracker
limiter *limiter.Limiter
speedTracker *limiter.SpeedTracker
cert *cert.Manager
lastConfig *panel.NodeConfig
lastUsers []panel.User
lastAppliedPort int // the port currently bound by the kernel
lastAppliedIP string // the listen IP currently bound by the kernel
pushInterval int // seconds
pullInterval int // seconds
lastUserHash string // hash of user list for change detection
lastConfigHash string // hash of full config for change detection
pullBackoff apiBackoff // backoff for panel pull failures
pushBackoff apiBackoff // backoff for panel push failures
wsClient *panel.WSClient // WebSocket client (nil if WS not enabled)
wsEvents chan panel.WSEvent // receives data events from WS client
wsStatusCh chan panel.WSStatusChange // receives WS connect/disconnect notifications
wsCancel context.CancelFunc // cancels the WS client goroutine
wsDisconnectAt time.Time // when WS last disconnected (zero if connected)
}
// apiBackoff implements simple exponential backoff for API failures.
type apiBackoff struct {
skipRemaining int
}
func (b *apiBackoff) shouldSkip() bool {
if b.skipRemaining > 0 {
b.skipRemaining--
return true
}
return false
}
func (b *apiBackoff) onSuccess() { b.skipRemaining = 0 }
func (b *apiBackoff) onFailure() {
if b.skipRemaining <= 0 {
b.skipRemaining = 1
} else if b.skipRemaining < 8 {
b.skipRemaining *= 2
}
}
func New(cfg *config.Config) *Service {
panelClient := panel.NewClient(cfg.Panel)
certMgr := cert.NewManager(cfg.Cert)
var k kernel.Kernel
switch cfg.Kernel.Type {
case "singbox":
k = singbox.New(cfg.Kernel)
case "xray":
k = xray.New(cfg.Kernel)
default:
slog.Error("unsupported kernel type, defaulting to sing-box", "type", cfg.Kernel.Type)
k = singbox.New(cfg.Kernel)
}
l := limiter.New()
st := limiter.NewSpeedTracker(l)
return &Service{
cfg: cfg,
panel: panelClient,
kernel: k,
tracker: tracker.New(),
limiter: l,
speedTracker: st,
cert: certMgr,
wsEvents: make(chan panel.WSEvent, 16),
wsStatusCh: make(chan panel.WSStatusChange, 4),
}
}
func (s *Service) Run(ctx context.Context) error {
// Start cert manager (handles auto-TLS or manual cert verification)
if err := s.cert.Start(ctx); err != nil {
return fmt.Errorf("cert manager: %w", err)
}
defer s.cert.Stop()
// Handshake: get WS config + initial data in one call
if err := s.initialSetup(ctx); err != nil {
return fmt.Errorf("initial setup: %w", err)
}
defer s.kernel.Stop()
// Set up tickers
trackTicker := time.NewTicker(10 * time.Second)
pushInterval := time.Duration(math.Max(float64(s.pushInterval), 5)) * time.Second
pullInterval := time.Duration(s.pullInterval) * time.Second
reportTicker := time.NewTicker(pushInterval)
pullTicker := time.NewTicker(pullInterval)
// WS discovery: when in REST-only mode, periodically re-handshake to check
// if WS has been enabled. When WS is disconnected for too long, re-check
// if it's still available.
wsDiscoveryTicker := time.NewTicker(5 * time.Minute)
defer trackTicker.Stop()
defer reportTicker.Stop()
defer pullTicker.Stop()
defer wsDiscoveryTicker.Stop()
s.startWSClient(ctx)
sMode := "disabled"
if s.wsClient != nil {
sMode = "connecting"
}
slog.Info("service started",
"kernel", s.kernel.Name(),
"push_interval", s.pushInterval,
"pull_interval", s.pullInterval,
"websocket", sMode,
)
for {
select {
case <-ctx.Done():
slog.Info("service shutting down")
s.pushReport()
return nil
case <-trackTicker.C:
s.trackAndEnforce(ctx)
case <-reportTicker.C:
s.pushReport()
case <-pullTicker.C:
// When WebSocket is connected, skip REST polling entirely.
// Config/user updates arrive via WS push.
if s.wsClient != nil && s.wsClient.IsConnected() {
slog.Debug("ws connected, skipping REST pull")
continue
}
slog.Info("ws not connected or disabled, polling from API")
s.pullViaAPI(ctx)
case <-wsDiscoveryTicker.C:
s.wsDiscovery(ctx)
case status := <-s.wsStatusCh:
s.handleWSStatus(ctx, status)
case event := <-s.wsEvents:
s.handleWSEvent(ctx, event)
}
}
}
func (s *Service) initialSetup(ctx context.Context) error {
// Register speed limit lookup with kernel unconditionally (before WS/V1 branch).
// This ensures speedLimitFunc is set regardless of which code path applies config.
s.kernel.SetSpeedLimitFunc(s.speedTracker.GetLimiter)
slog.Info("performing handshake with panel")
var hs *panel.HandshakeResponse
var err error
// Retry loop for initial handshake
for attempt := 1; ; attempt++ {
hs, err = s.panel.Handshake()
if err != nil {
slog.Error("handshake failed", "attempt", attempt, "error", err)
if attempt >= 5 {
return fmt.Errorf("handshake failed after %d attempts: %w", attempt, err)
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Duration(attempt*5) * time.Second):
continue
}
}
break
}
// Apply settings from panel handshake (Handshake provides default intervals)
if s.cfg.Node.PushInterval == 0 && hs.Settings.PushInterval > 0 {
s.pushInterval = hs.Settings.PushInterval
} else {
s.pushInterval = s.cfg.Node.PushInterval
}
if s.pushInterval == 0 {
s.pushInterval = 60
}
if s.cfg.Node.PullInterval == 0 && hs.Settings.PullInterval > 0 {
s.pullInterval = hs.Settings.PullInterval
} else {
s.pullInterval = s.cfg.Node.PullInterval
}
if s.pullInterval == 0 {
s.pullInterval = 60
}
// If WebSocket is enabled, skip initial V1 APIs fetch and wait for推送
if hs.WebSocket.Enabled && hs.WebSocket.WSURL != "" {
s.wsClient = s.newWSClient(hs.WebSocket.WSURL)
slog.Info("handshake complete (websocket enabled), waiting for data push...",
"ws_url", hs.WebSocket.WSURL,
)
return nil
}
// Falls back to V1 APIs only if WebSocket is disabled in panel
slog.Info("websocket disabled or missing, falling back to V1 APIs")
// Fetch initial config and users via V1 APIs
nodeConfig, err := s.panel.GetConfig()
if err != nil {
return fmt.Errorf("initial config fetch: %w", err)
}
if nodeConfig == nil {
return fmt.Errorf("initial config is nil")
}
users, err := s.panel.GetUsers()
if err != nil {
return fmt.Errorf("initial user fetch: %w", err)
}
// Apply initial data
s.lastConfig = nodeConfig
s.lastUsers = users
s.lastUserHash = computeUserHash(users)
s.lastConfigHash = computeConfigHash(nodeConfig)
s.limiter.UpdateUsers(users)
s.speedTracker.UpdateBuckets()
slog.Info("handshake complete (V1 fallback)",
"protocol", nodeConfig.Protocol,
"port", nodeConfig.ServerPort,
"users", len(users),
)
if len(users) == 0 {
slog.Warn("no users, kernel will not start until users are available")
return nil
}
// Update service-level config overrides from remote NodeConfig if present
s.applyRemoteOverrides(ctx, nodeConfig)
s.lastAppliedPort = nodeConfig.ServerPort
s.lastAppliedIP = nodeConfig.ListenIP
certFile, keyFile := s.cert.CertFile(), s.cert.KeyFile()
if err := s.kernel.ApplyConfig(nodeConfig, users, certFile, keyFile); err != nil {
return fmt.Errorf("apply config: %w", err)
}
return nil
}
// applyRemoteOverrides updates service-level settings (log level, cert config)
// from the panel's NodeConfig. Returns true if cert paths changed (kernel restart needed).
func (s *Service) applyRemoteOverrides(ctx context.Context, nc *panel.NodeConfig) bool {
if nc == nil {
return false
}
// Dynamic Log Level (Kernel)
if nc.KernelLogLevel != "" && nc.KernelLogLevel != s.cfg.Kernel.LogLevel {
slog.Info("cert: kernel log level override", "old", s.cfg.Kernel.LogLevel, "new", nc.KernelLogLevel)
s.cfg.Kernel.LogLevel = nc.KernelLogLevel
}
// Certificate configuration from panel (panel-first: takes precedence over local config)
if nc.CertConfig != nil {
return s.applyPanelCert(ctx, nc.CertConfig)
}
// Legacy fields (deprecated: prefer cert_config)
if nc.AutoTLS != s.cfg.Cert.AutoTLS {
slog.Info("cert: auto_tls policy changed (deprecated field)", "new", nc.AutoTLS)
s.cfg.Cert.AutoTLS = nc.AutoTLS
}
if nc.Domain != "" && nc.Domain != s.cfg.Cert.Domain {
s.cfg.Cert.Domain = nc.Domain
}
return false
}
// applyPanelCert converts a panel CertConfig into the local config format and
// reconfigures the cert manager. Reports whether cert paths changed.
func (s *Service) applyPanelCert(ctx context.Context, pc *panel.CertConfig) bool {
newCfg := config.CertConfig{
CertMode: pc.CertMode,
Domain: pc.Domain,
Email: pc.Email,
DNSProvider: pc.DNSProvider,
DNSEnv: pc.DNSEnv,
HTTPPort: pc.HTTPPort,
CertFile: pc.CertFile,
KeyFile: pc.KeyFile,
CertContent: pc.CertContent,
KeyContent: pc.KeyContent,
// Preserve local storage dir — only the operator controls where certs live.
CertDir: s.cfg.Cert.CertDir,
}
changed, err := s.cert.Reconfigure(ctx, newCfg)
if err != nil {
slog.Error("failed to apply panel cert config", "mode", pc.CertMode, "error", err)
return false
}
s.cfg.Cert = newCfg
if changed {
slog.Info("cert: paths updated from panel", "cert", s.cert.CertFile(), "key", s.cert.KeyFile())
}
return changed
}
// startWSClient starts the WS client goroutine if a client is configured.
func (s *Service) startWSClient(ctx context.Context) {
if s.wsClient == nil {
return
}
wsCtx, wsCancel := context.WithCancel(ctx)
s.wsCancel = wsCancel
go s.wsClient.Run(wsCtx)
}
// newWSClient creates a WSClient with standard event/status callbacks.
func (s *Service) newWSClient(wsURL string) *panel.WSClient {
return panel.NewWSClient(
wsURL,
s.cfg.Panel.Token,
s.cfg.Panel.NodeID,
func(event panel.WSEvent) {
select {
case s.wsEvents <- event:
default:
slog.Warn("ws event channel full, dropping event", "type", event.Type)
}
},
func(status panel.WSStatusChange) {
select {
case s.wsStatusCh <- status:
default:
}
},
)
}
// handleWSStatus reacts to WS connectivity changes.
//
// - On disconnect: record timestamp, immediately REST poll.
// - On reconnect: clear disconnect timestamp, REST poll to catch missed events.
func (s *Service) handleWSStatus(ctx context.Context, status panel.WSStatusChange) {
if status.Connected {
s.wsDisconnectAt = time.Time{}
slog.Info("ws connected, waiting for server-side full sync push")
// No REST pull needed — server detects new connection and pushes
// full config + users automatically on auth success.
} else {
if s.wsDisconnectAt.IsZero() {
s.wsDisconnectAt = time.Now()
}
slog.Info("ws disconnected, falling back to REST polling")
s.pullViaAPI(ctx)
}
}
// wsDiscovery periodically checks WS availability:
//
// 1. REST-only mode (wsClient == nil): Re-handshake to check if panel now has
// WS enabled. If so, create and start a WS client. This handles the case
// where WS was not enabled at startup but enabled later.
//
// 2. WS disconnected for >10 min: Re-handshake to check if WS config changed.
// If WS is now disabled, stop the WS client and switch to REST-only.
// If WS config changed (different URL/channel), restart with new config.
func (s *Service) wsDiscovery(ctx context.Context) {
needsCheck := false
if s.wsClient == nil {
needsCheck = true
slog.Debug("ws discovery: no WS client, checking if panel enabled WS")
} else if !s.wsDisconnectAt.IsZero() && time.Since(s.wsDisconnectAt) > 10*time.Minute {
needsCheck = true
slog.Debug("ws discovery: WS disconnected for >10min, re-checking WS config")
}
if !needsCheck {
return
}
hs, err := s.panel.Handshake()
if err != nil {
slog.Debug("ws discovery: handshake failed", "error", err)
return
}
// Apply any latest config/user changes via dedicated APIs
s.pullViaAPI(ctx)
if hs.WebSocket.Enabled && hs.WebSocket.WSURL != "" {
if s.wsClient == nil {
slog.Info("ws discovery: panel has WS enabled, creating WS client")
s.wsClient = s.newWSClient(hs.WebSocket.WSURL)
s.wsDisconnectAt = time.Time{}
s.startWSClient(ctx)
}
} else if s.wsClient != nil {
slog.Info("ws discovery: panel disabled WS, switching to REST-only")
if s.wsCancel != nil {
s.wsCancel()
}
s.wsClient = nil
s.wsCancel = nil
s.wsDisconnectAt = time.Time{}
}
}
// handleWSEvent processes data events received via WebSocket
func (s *Service) handleWSEvent(ctx context.Context, event panel.WSEvent) {
switch event.Type {
case panel.WSEventSyncConfig:
if event.Config == nil {
slog.Warn("ws: sync.config event with nil config, ignoring")
return
}
newConfigHash := computeConfigHash(event.Config)
if newConfigHash == s.lastConfigHash {
slog.Debug("ws: config unchanged (same hash), skipping")
return
}
slog.Info("ws: applying config update", "protocol", event.Config.Protocol)
s.lastConfig = event.Config
s.lastConfigHash = newConfigHash
s.applyRemoteOverrides(ctx, event.Config)
s.applyChanges(ctx, true, false)
case panel.WSEventSyncUsers:
if event.Users == nil {
slog.Warn("ws: sync.users event with nil users, ignoring")
return
}
newHash := computeUserHash(event.Users)
if newHash == s.lastUserHash {
slog.Debug("ws: users unchanged (same hash), skipping")
return
}
slog.Info("ws: applying user update", "count", len(event.Users))
s.applyUserUpdate(ctx, event.Users, newHash)
case panel.WSEventSyncUserDelta:
if len(event.DeltaUsers) == 0 {
slog.Warn("ws: sync.user.delta event with empty users, ignoring")
return
}
slog.Info("ws: applying user delta", "action", event.DeltaAction, "count", len(event.DeltaUsers))
s.applyUserDelta(ctx, event.DeltaAction, event.DeltaUsers)
default:
slog.Debug("ws: unknown event type", "type", event.Type)
}
}
// pullViaAPI re-calls the config and user APIs for polling fallback.
// Also opportunistically detects WS enablement when in REST-only mode.
func (s *Service) pullViaAPI(ctx context.Context) {
if s.pullBackoff.shouldSkip() {
slog.Debug("skipping pull due to backoff")
return
}
config, err := s.panel.GetConfig()
if err != nil {
slog.Error("poll config failed", "error", err)
s.pullBackoff.onFailure()
return
}
users, err := s.panel.GetUsers()
if err != nil {
slog.Error("poll users failed", "error", err)
s.pullBackoff.onFailure()
return
}
s.pullBackoff.onSuccess()
configChanged := false
if s.cert.CertRenewed() {
slog.Info("certificate renewed, kernel restart needed")
configChanged = true
}
if config != nil {
newConfigHash := computeConfigHash(config)
if s.lastConfigHash != newConfigHash {
configChanged = true
slog.Info("config updated from panel",
"protocol", config.Protocol, "port", config.ServerPort)
s.lastConfig = config
s.lastConfigHash = newConfigHash
// Apply kernel overrides and cert updates from REST pull
if s.applyRemoteOverrides(ctx, config) {
configChanged = true // Restart kernel if cert paths changed
}
}
}
if users != nil {
newHash := computeUserHash(users)
usersChanged := newHash != s.lastUserHash
if usersChanged && !configChanged {
s.applyUserUpdate(ctx, users, newHash)
} else if usersChanged {
// Config is also changing — ApplyConfig will restart the kernel with the
// updated user list, so skip the intermediate ReloadUsers.
s.syncUserState(ctx, users, newHash, false)
}
}
if configChanged {
s.applyChanges(ctx, true, false)
}
}
// syncUserState updates user tracking state (limiter, speedTracker, lastUsers)
// without touching the kernel. Set kickRemoved=false when a full kernel restart
// is about to happen anyway — removed users' connections will be killed then.
func (s *Service) syncUserState(ctx context.Context, users []panel.User, newHash string, kickRemoved bool) {
removedIDs := s.limiter.UpdateUsers(users)
s.speedTracker.UpdateBuckets()
if kickRemoved && len(removedIDs) > 0 && s.kernel.IsRunning() {
conns, err := s.kernel.GetConnections(ctx)
if err == nil {
kicks := s.limiter.KickUsers(conns, removedIDs)
s.executeKicks(ctx, kicks)
slog.Info("kicked removed users", "removed_ids", removedIDs, "kicked_conns", len(kicks))
}
}
s.lastUsers = users
s.lastUserHash = newHash
slog.Info("users updated", "count", len(users))
}
// applyUserUpdate updates user state and hot-reloads the kernel inbounds.
func (s *Service) applyUserUpdate(ctx context.Context, users []panel.User, newHash string) {
s.syncUserState(ctx, users, newHash, true)
s.applyChanges(ctx, false, true)
}
// applyUserDelta applies an incremental user change (add or remove).
func (s *Service) applyUserDelta(ctx context.Context, action string, deltaUsers []panel.User) {
switch action {
case "add":
// Build a map of current users for fast lookup
userMap := make(map[int]panel.User, len(s.lastUsers))
for _, u := range s.lastUsers {
userMap[u.ID] = u
}
for _, u := range deltaUsers {
userMap[u.ID] = u // add or update
}
merged := make([]panel.User, 0, len(userMap))
for _, u := range userMap {
merged = append(merged, u)
}
newHash := computeUserHash(merged)
s.applyUserUpdate(ctx, merged, newHash)
case "remove":
removeSet := make(map[int]struct{}, len(deltaUsers))
for _, u := range deltaUsers {
removeSet[u.ID] = struct{}{}
}
filtered := make([]panel.User, 0, len(s.lastUsers))
for _, u := range s.lastUsers {
if _, ok := removeSet[u.ID]; !ok {
filtered = append(filtered, u)
}
}
newHash := computeUserHash(filtered)
s.applyUserUpdate(ctx, filtered, newHash)
default:
slog.Warn("ws: unknown user delta action", "action", action)
}
}
// applyChanges applies config and/or user changes to the kernel
func (s *Service) applyChanges(ctx context.Context, configChanged, usersChanged bool) {
certFile, keyFile := s.cert.CertFile(), s.cert.KeyFile()
if configChanged {
if s.lastConfig != nil && len(s.lastUsers) > 0 {
// If the port or listen IP has changed, we MUST perform a full restart
// because hot-reload (s.kernel.Reload) typically only updates users/rules
// and doesn't re-bind existing listeners in most kernels.
needsRestart := s.kernel.IsRunning() && (s.lastConfig.ServerPort != s.lastAppliedPort || s.lastConfig.ListenIP != s.lastAppliedIP)
if !needsRestart {
// Try hot-reload for other config changes (could be route/user changes).
if err := s.kernel.Reload(s.lastConfig, s.lastUsers, certFile, keyFile); err != nil {
slog.Warn("reload failed, falling back to full restart", "error", err)
needsRestart = true
}
}
if needsRestart || !s.kernel.IsRunning() {
if err := s.kernel.ApplyConfig(s.lastConfig, s.lastUsers, certFile, keyFile); err != nil {
slog.Error("failed to apply config", "error", err)
} else {
// Update tracking state for next change
s.lastAppliedPort = s.lastConfig.ServerPort
s.lastAppliedIP = s.lastConfig.ListenIP
}
}
} else if len(s.lastUsers) == 0 {
slog.Warn("no users, stopping kernel")
s.kernel.Stop()
}
} else if usersChanged && s.kernel.IsRunning() {
// Hot-reload for user-only changes
if err := s.kernel.Reload(s.lastConfig, s.lastUsers, certFile, keyFile); err != nil {
slog.Warn("inbound reload failed, falling back to full restart", "error", err)
if err := s.kernel.ApplyConfig(s.lastConfig, s.lastUsers, certFile, keyFile); err != nil {
slog.Error("failed to apply config (fallback)", "error", err)
} else {
s.lastAppliedPort = s.lastConfig.ServerPort
s.lastAppliedIP = s.lastConfig.ListenIP
}
}
} else if usersChanged && !s.kernel.IsRunning() && len(s.lastUsers) > 0 {
// Kernel was stopped (no users), now we have users — start it
if err := s.kernel.ApplyConfig(s.lastConfig, s.lastUsers, certFile, keyFile); err != nil {
slog.Error("failed to start kernel with new users", "error", err)
}
}
}
func (s *Service) trackAndEnforce(ctx context.Context) {
if !s.kernel.IsRunning() {
return
}
conns, err := s.kernel.GetConnections(ctx)
if err != nil {
slog.Debug("get connections failed", "error", err)
return
}
s.tracker.Process(conns)
s.tracker.LogStats()
kicks := s.limiter.Check(conns)
s.executeKicks(ctx, kicks)
}
func (s *Service) executeKicks(ctx context.Context, kicks []limiter.KickAction) {
for _, kick := range kicks {
slog.Info("kicking connection",
"user_id", kick.UserID,
"conn_id", kick.ConnID,
"reason", kick.Reason,
)
if err := s.kernel.CloseConnection(ctx, kick.ConnID); err != nil {
slog.Error("failed to close connection", "conn_id", kick.ConnID, "error", err)
}
}
}
// pushReport sends consolidated traffic + alive + status to the panel
func (s *Service) pushReport() {
if s.pushBackoff.shouldSkip() {
slog.Debug("skipping report due to backoff")
return
}
traffic := s.tracker.FlushTraffic()
aliveIPs := s.tracker.FlushAliveIPs()
online := s.tracker.CurrentOnline()
status := monitor.Collect()
metrics := s.buildMetrics(status)
if len(traffic) == 0 && len(aliveIPs) == 0 && len(online) == 0 {
// Still send status periodically
if err := s.panel.Report(
nil, nil, nil,
status.CPU,
[2]uint64{status.MemTotal, status.MemUsed},
[2]uint64{status.SwapTotal, status.SwapUsed},
[2]uint64{status.DiskTotal, status.DiskUsed},
metrics,
); err != nil {
slog.Error("failed to push status report", "error", err)
s.pushBackoff.onFailure()
return
}
s.pushBackoff.onSuccess()
return
}
if err := s.panel.Report(
traffic, aliveIPs, online,
status.CPU,
[2]uint64{status.MemTotal, status.MemUsed},
[2]uint64{status.SwapTotal, status.SwapUsed},
[2]uint64{status.DiskTotal, status.DiskUsed},
metrics,
); err != nil {
slog.Error("failed to push report", "error", err)
s.tracker.RestoreTraffic(traffic)
s.tracker.RestoreAliveIPs(aliveIPs)
s.pushBackoff.onFailure()
return
}
s.pushBackoff.onSuccess()
slog.Info("report pushed", "users", len(traffic))
}
// buildMetrics aggregates node-level metrics to be reported to the panel.
// This includes active connections, per-core CPU, GC stats, API call stats,
// WebSocket status, and limiter hit counts.
func (s *Service) buildMetrics(status monitor.Status) map[string]interface{} {
m := make(map[string]interface{})
m["uptime"] = status.Uptime
// Active connections (last measured during tracker.Process()).
m["active_connections"] = s.tracker.ActiveConnections()
m["total_connections"] = s.tracker.TotalConnections()
// Speed
m["inbound_speed"] = s.tracker.InboundSpeed()
m["outbound_speed"] = s.tracker.OutboundSpeed()
// Per-core CPU usage (if available).
if len(status.CPUPerCore) > 0 {
m["cpu_per_core"] = status.CPUPerCore
}
// Speed Limiter metrics
m["speed_limiter"] = map[string]interface{}{
"has_limits": s.speedTracker.HasLimits(),
"limited_users": s.speedTracker.LimitedUserCount(),
}
// GC metrics.
m["gc"] = map[string]interface{}{
"num_gc": status.NumGC,
"last_pause_ms": status.LastPauseMS,
}
// API metrics.
api := s.panel.SnapshotMetrics()
m["api"] = map[string]interface{}{
"success": api.Success,
"failure": api.Failure,
}
// WebSocket status.
wsEnabled := s.wsClient != nil
wsConnected := wsEnabled && s.wsClient.IsConnected()
m["ws"] = map[string]interface{}{
"enabled": wsEnabled,
"connected": wsConnected,
}
// Limiter metrics.
lm := s.limiter.SnapshotMetrics()
m["limits"] = map[string]interface{}{
"device_limit_events": lm.DeviceLimitEvents,
"speed_limited_users": s.speedTracker.LimitedUserCount(),
}
return m
}
// computeConfigHash returns a SHA-256 hash of the kernel-relevant node config
// fields. BaseConfig (PushInterval/PullInterval) is excluded because it only
// affects service-layer polling intervals and must not trigger a kernel restart.
func computeConfigHash(cfg *panel.NodeConfig) string {
if cfg == nil {
return ""
}
tmp := *cfg
tmp.BaseConfig = panel.BaseConfig{}
data, err := json.Marshal(&tmp)
if err != nil {
return ""
}
h := sha256.Sum256(data)
return fmt.Sprintf("%x", h)
}
// computeUserHash returns a deterministic hash of the user list for change detection.
// Uses direct byte encoding instead of binary.Write to avoid reflection overhead.
func computeUserHash(users []panel.User) string {
sorted := make([]panel.User, len(users))
copy(sorted, users)
sort.Slice(sorted, func(i, j int) bool { return sorted[i].ID < sorted[j].ID })
h := sha256.New()
var buf [8]byte
for _, u := range sorted {
buf[0] = byte(u.ID)
buf[1] = byte(u.ID >> 8)
buf[2] = byte(u.ID >> 16)
buf[3] = byte(u.ID >> 24)
buf[4] = byte(u.ID >> 32)
buf[5] = byte(u.ID >> 40)
buf[6] = byte(u.ID >> 48)
buf[7] = byte(u.ID >> 56)
h.Write(buf[:])
h.Write([]byte(u.UUID))
buf[0] = byte(u.SpeedLimit)
buf[1] = byte(u.SpeedLimit >> 8)
buf[2] = byte(u.SpeedLimit >> 16)
buf[3] = byte(u.SpeedLimit >> 24)
buf[4] = byte(u.SpeedLimit >> 32)
buf[5] = byte(u.SpeedLimit >> 40)
buf[6] = byte(u.SpeedLimit >> 48)
buf[7] = byte(u.SpeedLimit >> 56)
h.Write(buf[:])
buf[0] = byte(u.DeviceLimit)
buf[1] = byte(u.DeviceLimit >> 8)
buf[2] = byte(u.DeviceLimit >> 16)
buf[3] = byte(u.DeviceLimit >> 24)
buf[4] = byte(u.DeviceLimit >> 32)
buf[5] = byte(u.DeviceLimit >> 40)
buf[6] = byte(u.DeviceLimit >> 48)
buf[7] = byte(u.DeviceLimit >> 56)
h.Write(buf[:])
}
return fmt.Sprintf("%x", h.Sum(nil))
}
+275
View File
@@ -0,0 +1,275 @@
package tracker
import (
"log/slog"
"github.com/cedar2025/xboard-node/internal/kernel"
)
const (
// DefaultMaxConnState caps how many connection-state entries we maintain.
// Prevents unbounded memory growth from rogue / DDoS connection floods.
DefaultMaxConnState = 100_000
// DefaultMaxAliveIPsPerUser caps the per-user IP set tracked per push interval.
// 200 IPs per user is generous for any legitimate use case.
DefaultMaxAliveIPsPerUser = 200
)
// Tracker tracks per-connection traffic deltas and accumulates per-user totals.
//
// Thread safety model: Tracker is designed for SINGLE-GOROUTINE use only.
// All methods (Process, Flush*, Restore*, CurrentOnline, speed accessors) are
// called exclusively from the service goroutine's select loop. No internal
// locking is provided. Do NOT call Tracker methods from multiple goroutines.
//
// Hot-path optimisation (②): activeConns is allocated once and cleared in-place
// every Process() call instead of being reallocated, saving GC pressure at high
// connection counts.
//
// OOM protection (④): connState and aliveIPs are capped at configurable maximums.
type Tracker struct {
connState map[string][2]int64 // conn_id → last [upload, download]
traffic map[int][2]int64 // user_id → accumulated [upload, download]
aliveIPs map[int]map[string]bool // user_id → set of source IPs
online map[int]int // user_id → current active connection count
// ② Reused buffer: cleared each Process() call, not reallocated.
activeConns map[string]struct{}
// ④ Limits (set at construction, never mutated).
maxConnState int
maxAliveIPsPerUser int
// lastActiveConns stores the last observed number of active connections.
// This is updated on each Process() call and read by the service when
// building node metrics for panel reporting.
lastActiveConns int
totalConns int64 // cumulative number of connections seen since startup
// lastUserCount is used to pre-size intervalTraffic each cycle, avoiding
// repeated map rehashing under heavy user counts.
lastUserCount int
inSpeed int64
outSpeed int64
}
func New() *Tracker {
return NewWithLimits(DefaultMaxConnState, DefaultMaxAliveIPsPerUser)
}
func NewWithLimits(maxConnState, maxAliveIPsPerUser int) *Tracker {
return &Tracker{
connState: make(map[string][2]int64),
traffic: make(map[int][2]int64),
aliveIPs: make(map[int]map[string]bool),
online: make(map[int]int),
activeConns: make(map[string]struct{}),
maxConnState: maxConnState,
maxAliveIPsPerUser: maxAliveIPsPerUser,
}
}
// Process calculates per-user traffic deltas for this interval.
// Internally accumulates totals for panel push and tracks alive IPs.
// Returns per-user interval traffic [upload, download] for the limiter.
func (t *Tracker) Process(conns []kernel.Connection) map[int][2]int64 {
// Pre-size using last cycle's user count to avoid rehashing under heavy load.
intervalTraffic := make(map[int][2]int64, t.lastUserCount)
// Clear activeConns in-place (no alloc, backing array retained).
for k := range t.activeConns {
delete(t.activeConns, k)
}
// Rebuild aliveIPs each cycle so only currently-active IPs are reported.
// Previously, IPs accumulated across cycles leading to stale entries.
for uid := range t.aliveIPs {
for ip := range t.aliveIPs[uid] {
delete(t.aliveIPs[uid], ip)
}
}
// Clear online counts
for uid := range t.online {
delete(t.online, uid)
}
for _, conn := range conns {
t.activeConns[conn.ID] = struct{}{}
if conn.UserID == 0 {
continue
}
t.online[conn.UserID]++
var deltaUp, deltaDown int64
prev, exists := t.connState[conn.ID]
if exists {
deltaUp = conn.Upload - prev[0]
deltaDown = conn.Download - prev[1]
if deltaUp < 0 {
deltaUp = conn.Upload
}
if deltaDown < 0 {
deltaDown = conn.Download
}
} else {
// ④ New connection: only track if below the state cap.
if len(t.connState) >= t.maxConnState {
slog.Debug("connState cap reached, skipping new connection",
"cap", t.maxConnState, "conn_id", conn.ID)
continue
}
deltaUp = conn.Upload
deltaDown = conn.Download
t.totalConns++
}
t.connState[conn.ID] = [2]int64{conn.Upload, conn.Download}
if deltaUp > 0 || deltaDown > 0 {
cur := t.traffic[conn.UserID]
cur[0] += deltaUp
cur[1] += deltaDown
t.traffic[conn.UserID] = cur
it := intervalTraffic[conn.UserID]
it[0] += deltaUp
it[1] += deltaDown
intervalTraffic[conn.UserID] = it
}
// ④ Track alive IPs, capped per user.
if conn.SourceIP != "" {
ips := t.aliveIPs[conn.UserID]
if ips == nil {
ips = make(map[string]bool)
t.aliveIPs[conn.UserID] = ips
}
if len(ips) < t.maxAliveIPsPerUser {
ips[conn.SourceIP] = true
}
}
}
// Cleanup stale connState entries for connections that are no longer active.
for id := range t.connState {
if _, active := t.activeConns[id]; !active {
delete(t.connState, id)
}
}
// Snapshot current active connection count for metrics reporting.
t.lastActiveConns = len(t.connState)
// Derive speed directly from intervalTraffic deltas accumulated this cycle.
// This avoids the negative-diff bug that occurs when connState entries for
// closed connections are removed, which made cumulative totals drop and
// clamped speed to 0 for one cycle after a mass disconnect.
var cycleIn, cycleOut int64
for _, it := range intervalTraffic {
cycleOut += it[0]
cycleIn += it[1]
}
t.inSpeed = cycleIn
t.outSpeed = cycleOut
// Update hint for next cycle's map pre-sizing.
if n := len(intervalTraffic); n > 0 {
t.lastUserCount = n
}
return intervalTraffic
}
// FlushTraffic returns accumulated per-user traffic and resets the counter.
func (t *Tracker) FlushTraffic() map[int][2]int64 {
data := t.traffic
t.traffic = make(map[int][2]int64, len(data))
return data
}
// RestoreTraffic adds traffic back (used when push to panel fails).
func (t *Tracker) RestoreTraffic(data map[int][2]int64) {
for uid, d := range data {
cur := t.traffic[uid]
cur[0] += d[0]
cur[1] += d[1]
t.traffic[uid] = cur
}
}
// HasTraffic returns true if there is accumulated traffic to report.
func (t *Tracker) HasTraffic() bool {
return len(t.traffic) > 0
}
// FlushAliveIPs returns per-user alive IPs and resets the tracker.
func (t *Tracker) FlushAliveIPs() map[int][]string {
data := make(map[int][]string, len(t.aliveIPs))
for uid, ips := range t.aliveIPs {
ipList := make([]string, 0, len(ips))
for ip := range ips {
ipList = append(ipList, ip)
}
data[uid] = ipList
}
t.aliveIPs = make(map[int]map[string]bool, len(t.aliveIPs))
return data
}
// CurrentOnline returns a map of user_id to active connection count.
func (t *Tracker) CurrentOnline() map[int]int {
return t.online
}
// RestoreAliveIPs merges alive IPs back in (used when push to panel fails).
func (t *Tracker) RestoreAliveIPs(data map[int][]string) {
for uid, ipList := range data {
ips := t.aliveIPs[uid]
if ips == nil {
ips = make(map[string]bool, len(ipList))
t.aliveIPs[uid] = ips
}
for _, ip := range ipList {
if len(ips) < t.maxAliveIPsPerUser {
ips[ip] = true
}
}
}
}
// LogStats logs current tracking statistics.
func (t *Tracker) LogStats() {
slog.Debug("tracker stats",
"active_connections", len(t.connState),
"users_with_traffic", len(t.traffic),
"users_with_ips", len(t.aliveIPs),
)
}
// ActiveConnections returns the last observed active connection count.
// This is cheap and safe because Tracker is only mutated from the service
// goroutine; reads happen from the same goroutine when pushing reports.
func (t *Tracker) ActiveConnections() int {
return t.lastActiveConns
}
// TotalConnections returns the cumulative connection count since startup.
func (t *Tracker) TotalConnections() int64 {
return t.totalConns
}
// InboundSpeed returns the last observed inbound (download) speed in bytes/second.
// Process() is called every 10 seconds, so we divide the 10-second total by 10.
func (t *Tracker) InboundSpeed() int64 {
return t.inSpeed / 10
}
// OutboundSpeed returns the last observed outbound (upload) speed in bytes/second.
// Process() is called every 10 seconds, so we divide the 10-second total by 10.
func (t *Tracker) OutboundSpeed() int64 {
return t.outSpeed / 10
}
+278
View File
@@ -0,0 +1,278 @@
package tracker
import (
"testing"
"github.com/cedar2025/xboard-node/internal/kernel"
)
func TestProcess_InitialTraffic(t *testing.T) {
tr := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 2, Upload: 50, Download: 80, SourceIP: "2.2.2.2"},
}
interval := tr.Process(conns)
if interval[1] != [2]int64{100, 200} {
t.Errorf("user 1 interval: got %v", interval[1])
}
if interval[2] != [2]int64{50, 80} {
t.Errorf("user 2 interval: got %v", interval[2])
}
}
func TestProcess_DeltaCalculation(t *testing.T) {
tr := New()
// First tick
conns := []kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
}
tr.Process(conns)
// Second tick — counters advanced
conns[0].Upload = 300
conns[0].Download = 500
interval := tr.Process(conns)
if interval[1] != [2]int64{200, 300} {
t.Errorf("delta: got %v, want [200,300]", interval[1])
}
}
func TestProcess_CounterReset(t *testing.T) {
tr := New()
// First tick with high counters
conns := []kernel.Connection{
{ID: "c1", UserID: 1, Upload: 1000, Download: 2000},
}
tr.Process(conns)
// Counter reset (new value < old value) — treat as fresh
conns[0].Upload = 50
conns[0].Download = 80
interval := tr.Process(conns)
if interval[1] != [2]int64{50, 80} {
t.Errorf("counter reset: got %v, want [50,80]", interval[1])
}
}
func TestProcess_ConnectionCleanup(t *testing.T) {
tr := New()
// Tick 1: connection exists
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
})
// Tick 2: connection gone — should clean up state
tr.Process([]kernel.Connection{})
// Tick 3: same conn ID reappears — should count full bytes (not delta)
interval := tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 50, Download: 30},
})
if interval[1] != [2]int64{50, 30} {
t.Errorf("after cleanup: got %v, want [50,30]", interval[1])
}
}
func TestProcess_ZeroUserID(t *testing.T) {
tr := New()
interval := tr.Process([]kernel.Connection{
{ID: "c1", UserID: 0, Upload: 100, Download: 200},
})
if len(interval) != 0 {
t.Errorf("expected no interval traffic for userID=0, got %v", interval)
}
}
func TestProcess_MultipleConnectionsSameUser(t *testing.T) {
tr := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
{ID: "c2", UserID: 1, Upload: 50, Download: 80},
}
interval := tr.Process(conns)
if interval[1] != [2]int64{150, 280} {
t.Errorf("aggregated: got %v, want [150,280]", interval[1])
}
}
func TestFlushTraffic(t *testing.T) {
tr := New()
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
{ID: "c2", UserID: 2, Upload: 50, Download: 80},
})
flushed := tr.FlushTraffic()
if flushed[1] != [2]int64{100, 200} {
t.Errorf("flushed user 1: got %v", flushed[1])
}
if flushed[2] != [2]int64{50, 80} {
t.Errorf("flushed user 2: got %v", flushed[2])
}
// After flush, should be empty
if tr.HasTraffic() {
t.Error("expected no traffic after flush")
}
flushed2 := tr.FlushTraffic()
if len(flushed2) != 0 {
t.Errorf("expected empty after second flush, got %v", flushed2)
}
}
func TestRestoreTraffic(t *testing.T) {
tr := New()
// Generate some traffic
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
})
flushed := tr.FlushTraffic()
// Restore (simulating failed push)
tr.RestoreTraffic(flushed)
if !tr.HasTraffic() {
t.Error("expected traffic after restore")
}
restored := tr.FlushTraffic()
if restored[1] != [2]int64{100, 200} {
t.Errorf("restored: got %v", restored[1])
}
}
func TestRestoreTraffic_Additive(t *testing.T) {
tr := New()
// Generate traffic
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
})
// Generate more traffic
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 200, Download: 400},
})
// Flush and restore
first := tr.FlushTraffic()
tr.RestoreTraffic(first)
// Generate additional traffic
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 250, Download: 500},
})
final := tr.FlushTraffic()
// Should be restored(200,400) + delta(50,100) = (250,500)
if final[1][0] != 250 || final[1][1] != 500 {
t.Errorf("additive restore: got %v", final[1])
}
}
func TestFlushAliveIPs(t *testing.T) {
tr := New()
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, Upload: 50, Download: 80, SourceIP: "2.2.2.2"},
{ID: "c3", UserID: 2, Upload: 30, Download: 40, SourceIP: "3.3.3.3"},
})
aliveIPs := tr.FlushAliveIPs()
if len(aliveIPs[1]) != 2 {
t.Errorf("user 1 IPs: got %d, want 2", len(aliveIPs[1]))
}
if len(aliveIPs[2]) != 1 {
t.Errorf("user 2 IPs: got %d, want 1", len(aliveIPs[2]))
}
// After flush, should be empty
aliveIPs2 := tr.FlushAliveIPs()
if len(aliveIPs2) != 0 {
t.Errorf("expected empty after second flush, got %v", aliveIPs2)
}
}
func TestFlushAliveIPs_DedupSameIP(t *testing.T) {
tr := New()
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200, SourceIP: "1.1.1.1"},
{ID: "c2", UserID: 1, Upload: 50, Download: 80, SourceIP: "1.1.1.1"},
})
aliveIPs := tr.FlushAliveIPs()
if len(aliveIPs[1]) != 1 {
t.Errorf("expected dedup to 1 IP, got %d", len(aliveIPs[1]))
}
}
func TestHasTraffic(t *testing.T) {
tr := New()
if tr.HasTraffic() {
t.Error("new tracker should not have traffic")
}
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
})
if !tr.HasTraffic() {
t.Error("should have traffic after process")
}
tr.FlushTraffic()
if tr.HasTraffic() {
t.Error("should not have traffic after flush")
}
}
func TestProcess_NoTrafficDelta(t *testing.T) {
tr := New()
conns := []kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
}
tr.Process(conns)
// Same values — no delta
interval := tr.Process(conns)
if len(interval) != 0 {
t.Errorf("expected no interval traffic for zero delta, got %v", interval)
}
}
func TestTrafficAccumulation(t *testing.T) {
tr := New()
// Tick 1
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 100, Download: 200},
})
// Tick 2
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 300, Download: 500},
})
// Tick 3
tr.Process([]kernel.Connection{
{ID: "c1", UserID: 1, Upload: 350, Download: 600},
})
flushed := tr.FlushTraffic()
// Total: 100+200+50 = 350 upload, 200+300+100 = 600 download
if flushed[1] != [2]int64{350, 600} {
t.Errorf("accumulated: got %v, want [350,600]", flushed[1])
}
}