Agent consumes singbox_config alongside xray: candidate->check-> flush->swap activation with .previous rollback, wire-level v2ray_api stats client with NexusGate epoch discipline (mid-query flip voids the round, new epoch counts in full, agent-first snapshot baselines), access-log online tracker (authenticated-connection lines, ANSI-safe, merged with snapshot-active users), per-core traffic baselines with the sing-box one carrying its epoch. install-agent.sh installs the self-built sing-box v1.13.21 next to xray (xray/geo removal deferred to the B5 switchover). Env-gated real-core E2E covers vless-reality, ss2022, mixed, trojan and the forwarding leg end to end; hysteria2 is probe-gated (sandbox blocks UDP replies) and stays a B5 pilot item.
212 lines
5.5 KiB
Go
212 lines
5.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakeSingbox installs a shell-script sing-box stand-in into a temp
|
|
// bin folder: `check` rejects configs containing BADSCHEMA, `run`
|
|
// dies instantly on RUNTIMEFAIL and otherwise sleeps (standing in for
|
|
// the long-running core). The real binary's behavior is covered by
|
|
// the env-gated E2E; these tests pin the agent's activation
|
|
// discipline: check gate, no-change no-restart, rollback.
|
|
const fakeSingboxScript = `#!/bin/sh
|
|
case "$1" in
|
|
version)
|
|
echo "sing-box version 9.9.9"
|
|
exit 0
|
|
;;
|
|
check)
|
|
if grep -q BADSCHEMA "$3" 2>/dev/null; then
|
|
echo "check: invalid config" >&2
|
|
exit 1
|
|
fi
|
|
exit 0
|
|
;;
|
|
run)
|
|
if grep -q RUNTIMEFAIL "$3" 2>/dev/null; then
|
|
echo "fatal: cannot start" >&2
|
|
exit 1
|
|
fi
|
|
exec sleep 3600
|
|
;;
|
|
esac
|
|
exit 2
|
|
`
|
|
|
|
func setupFakeSingbox(t *testing.T) (binDir string) {
|
|
t.Helper()
|
|
binDir = t.TempDir()
|
|
t.Setenv("XUI_BIN_FOLDER", binDir)
|
|
path := filepath.Join(binDir, singboxBinaryName())
|
|
if err := os.WriteFile(path, []byte(fakeSingboxScript), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return binDir
|
|
}
|
|
|
|
func newApplyTestAgent() *Agent {
|
|
return New(&Config{MasterURL: "http://127.0.0.1:1", Token: "tok", NodeName: "n"})
|
|
}
|
|
|
|
const (
|
|
sbCfgA = `{"log":{"level":"info"},"inbounds":[],"outbounds":[{"type":"direct","tag":"direct"}]}`
|
|
sbCfgB = `{"log":{"level":"debug"},"inbounds":[],"outbounds":[{"type":"direct","tag":"direct"}]}`
|
|
)
|
|
|
|
func appliedConfig(t *testing.T) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(singboxConfigPath())
|
|
if err != nil {
|
|
t.Fatalf("read applied config: %v", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func TestApplySingboxStartsProcess(t *testing.T) {
|
|
setupFakeSingbox(t)
|
|
a := newApplyTestAgent()
|
|
t.Cleanup(a.stopSingbox)
|
|
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
|
|
t.Fatalf("apply: %v", err)
|
|
}
|
|
a.sbMu.Lock()
|
|
running := a.sbProc.isRunning()
|
|
pid := a.sbProc.pid()
|
|
a.sbMu.Unlock()
|
|
if !running || pid <= 0 {
|
|
t.Fatal("sing-box process not running after apply")
|
|
}
|
|
if got := appliedConfig(t); got != sbCfgA {
|
|
t.Fatalf("applied config = %q, want cfgA", got)
|
|
}
|
|
if _, err := os.Stat(singboxCandidatePath()); !os.IsNotExist(err) {
|
|
t.Fatal("candidate file must not linger after a successful apply")
|
|
}
|
|
}
|
|
|
|
func TestApplySingboxCheckGateKeepsRunningCore(t *testing.T) {
|
|
setupFakeSingbox(t)
|
|
a := newApplyTestAgent()
|
|
t.Cleanup(a.stopSingbox)
|
|
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
|
|
t.Fatalf("apply A: %v", err)
|
|
}
|
|
a.sbMu.Lock()
|
|
pidBefore := a.sbProc.pid()
|
|
a.sbMu.Unlock()
|
|
|
|
bad := `{"log":{"level":"info"},"inbounds":[{"type":"BADSCHEMA"}]}`
|
|
err := a.applySingboxConfig([]byte(bad))
|
|
if err == nil || !strings.Contains(err.Error(), "check") {
|
|
t.Fatalf("apply bad config err = %v, want a check failure", err)
|
|
}
|
|
a.sbMu.Lock()
|
|
pidAfter := a.sbProc.pid()
|
|
running := a.sbProc.isRunning()
|
|
a.sbMu.Unlock()
|
|
if !running || pidAfter != pidBefore {
|
|
t.Fatalf("check failure disturbed the running core: pid %d -> %d running=%v", pidBefore, pidAfter, running)
|
|
}
|
|
if got := appliedConfig(t); got != sbCfgA {
|
|
t.Fatalf("active config changed after failed check: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestApplySingboxUnchangedNoRestart(t *testing.T) {
|
|
setupFakeSingbox(t)
|
|
a := newApplyTestAgent()
|
|
t.Cleanup(a.stopSingbox)
|
|
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
|
|
t.Fatalf("apply A: %v", err)
|
|
}
|
|
a.sbMu.Lock()
|
|
pidBefore := a.sbProc.pid()
|
|
a.sbMu.Unlock()
|
|
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
|
|
t.Fatalf("re-apply A: %v", err)
|
|
}
|
|
a.sbMu.Lock()
|
|
pidAfter := a.sbProc.pid()
|
|
a.sbMu.Unlock()
|
|
if pidAfter != pidBefore {
|
|
t.Fatalf("unchanged config restarted the core: pid %d -> %d", pidBefore, pidAfter)
|
|
}
|
|
}
|
|
|
|
func TestApplySingboxRuntimeFailureRollsBack(t *testing.T) {
|
|
setupFakeSingbox(t)
|
|
a := newApplyTestAgent()
|
|
t.Cleanup(a.stopSingbox)
|
|
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
|
|
t.Fatalf("apply A: %v", err)
|
|
}
|
|
|
|
bad := `{"log":{"level":"info"},"note":"RUNTIMEFAIL","inbounds":[]}`
|
|
if err := a.applySingboxConfig([]byte(bad)); err == nil {
|
|
t.Fatal("apply of a config that dies at startup must fail")
|
|
}
|
|
a.sbMu.Lock()
|
|
running := a.sbProc.isRunning()
|
|
a.sbMu.Unlock()
|
|
if !running {
|
|
t.Fatal("rollback must leave a running core")
|
|
}
|
|
if got := appliedConfig(t); got != sbCfgA {
|
|
t.Fatalf("active config after rollback = %q, want cfgA", got)
|
|
}
|
|
}
|
|
|
|
func TestApplySingboxMissingBinary(t *testing.T) {
|
|
binDir := t.TempDir()
|
|
t.Setenv("XUI_BIN_FOLDER", binDir)
|
|
a := newApplyTestAgent()
|
|
if err := a.applySingboxConfig([]byte(sbCfgA)); !errors.Is(err, errSingboxNotInstalled) {
|
|
t.Fatalf("err = %v, want errSingboxNotInstalled", err)
|
|
}
|
|
}
|
|
|
|
func TestApplySingboxInvalidJSON(t *testing.T) {
|
|
setupFakeSingbox(t)
|
|
a := newApplyTestAgent()
|
|
t.Cleanup(a.stopSingbox)
|
|
if err := a.applySingboxConfig([]byte("{not json")); err == nil {
|
|
t.Fatal("invalid JSON must fail")
|
|
}
|
|
a.sbMu.Lock()
|
|
proc := a.sbProc
|
|
a.sbMu.Unlock()
|
|
if proc != nil && proc.isRunning() {
|
|
t.Fatal("no process may start from invalid JSON")
|
|
}
|
|
}
|
|
|
|
func TestParseSingboxVersion(t *testing.T) {
|
|
cases := []struct {
|
|
out string
|
|
want string
|
|
ok bool
|
|
}{
|
|
{"sing-box version 1.13.21\n\nEnvironment: go1.26.9 linux/amd64", "1.13.21", true},
|
|
{"sing-box version unknown\n", "unknown", true},
|
|
{"sing-box version \n", "", false},
|
|
{"xray version 1.0\n", "", false},
|
|
{"", "", false},
|
|
}
|
|
for _, tc := range cases {
|
|
got, ok := parseSingboxVersion(tc.out)
|
|
if got != tc.want || ok != tc.ok {
|
|
t.Errorf("parseSingboxVersion(%q) = %q,%v want %q,%v", tc.out, got, ok, tc.want, tc.ok)
|
|
}
|
|
}
|
|
}
|