P7: restore agent node type, agent heartbeat expiry, build-version injection, remove log/history/xray-metrics modules, optional sub port
- nodes: port node-type (traditional/agent) picker back from web-src into the embedded frontend; backend annotates NodeView.nodeType from agent_nodes bindings; fix agent-node update path (skip reachability probe and apiToken requirement for agent-managed nodes); restore i18n keys in all 13 locales; install command now uses the Gitea mirror - jobs: expire agent-bound nodes whose agent_nodes.last_seen is older than max(3x heartbeat interval, 120s) = 180s; last_seen=0 (never registered) is left untouched; transitions reuse UpdateHeartbeat + node event broadcast - config: GetBaseVersion honors ldflags-injected buildVersion; release builds inject 3.7.0-agent.1 so the panel no longer reports a phantom update against the Gitea release tag - logger/server: remove the three log viewers (panel/xray/amneziawg), their API endpoints and the 10240-entry in-memory log buffer; file logging via lumberjack unchanged; tests assert via a new optional logger hook instead of the removed buffer - server: remove system history and xray metrics modules end to end (endpoints, sampling appends in RefreshStatus, per-minute persist cron, gob snapshot/restore, XrayMetricsService); realtime status and per-node history are untouched - settings: subPort may be 0/unset - subscription links are generated without a port while the subscription service keeps binding the default 2096; the settings form declares the cleared state via onNumber's new onClear option instead of snapping back to 1
This commit is contained in:
@@ -2,6 +2,7 @@ package job
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
@@ -10,6 +11,22 @@ import (
|
||||
|
||||
// The protocol gate must let hysteria through: XrayAPI supports it, and the
|
||||
// skip left over-limit Hysteria2 sessions alive until the fail2ban ban caught up.
|
||||
// captureLogLines installs a logger hook collecting "LEVEL message" lines
|
||||
// for the rest of the test. It replaces reads of the in-memory log buffer,
|
||||
// which was removed together with the web log viewer.
|
||||
func captureLogLines(t *testing.T) *[]string {
|
||||
t.Helper()
|
||||
var mu sync.Mutex
|
||||
lines := &[]string{}
|
||||
prev := logger.SetLogHook(func(level, msg string) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
*lines = append(*lines, level+" "+msg)
|
||||
})
|
||||
t.Cleanup(func() { logger.SetLogHook(prev) })
|
||||
return lines
|
||||
}
|
||||
|
||||
func TestDisconnectClientTemporarilyAllowsHysteria(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
|
||||
@@ -22,10 +39,11 @@ func TestDisconnectClientTemporarilyAllowsHysteria(t *testing.T) {
|
||||
}
|
||||
clients := []model.Client{{Email: email, Auth: "secret"}}
|
||||
|
||||
logLines := captureLogLines(t)
|
||||
(&CheckClientIpJob{}).disconnectClientTemporarily(inbound, email, clients)
|
||||
|
||||
var unsupported, attempted bool
|
||||
for _, line := range logger.GetLogs(500, "warning") {
|
||||
for _, line := range *logLines {
|
||||
if strings.Contains(line, "Temporary disconnect is not supported for protocol hysteria") {
|
||||
unsupported = true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
func TestAgentHeartbeatStale(t *testing.T) {
|
||||
now := time.Unix(1_700_000_000, 0)
|
||||
ms := func(d time.Duration) int64 { return now.Add(-d).UnixMilli() }
|
||||
cases := []struct {
|
||||
name string
|
||||
lastSeenMs int64
|
||||
want bool
|
||||
}{
|
||||
{"never checked in", 0, false},
|
||||
{"negative stamp", -1, false},
|
||||
{"fresh heartbeat", ms(60 * time.Second), false},
|
||||
{"two missed heartbeats", ms(179 * time.Second), false},
|
||||
{"past threshold", ms(181 * time.Second), true},
|
||||
{"long gone", ms(24 * time.Hour), true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := agentHeartbeatStale(tc.lastSeenMs, now); got != tc.want {
|
||||
t.Errorf("%s: agentHeartbeatStale(%d) = %v, want %v", tc.name, tc.lastSeenMs, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func seedAgentNode(t *testing.T, name, status string, lastSeenMs int64) *model.Node {
|
||||
t.Helper()
|
||||
db := database.GetDB()
|
||||
n := &model.Node{
|
||||
Name: name,
|
||||
Address: "agent",
|
||||
Port: 2053,
|
||||
Enable: true,
|
||||
Status: status,
|
||||
}
|
||||
if err := db.Create(n).Error; err != nil {
|
||||
t.Fatalf("create node: %v", err)
|
||||
}
|
||||
a := &model.AgentNode{
|
||||
TokenHash: "hash-" + name,
|
||||
Name: name,
|
||||
NodeID: &n.Id,
|
||||
LastSeen: lastSeenMs,
|
||||
}
|
||||
if err := db.Create(a).Error; err != nil {
|
||||
t.Fatalf("create agent: %v", err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func nodeStatus(t *testing.T, id int) string {
|
||||
t.Helper()
|
||||
var n model.Node
|
||||
if err := database.GetDB().Where("id = ?", id).First(&n).Error; err != nil {
|
||||
t.Fatalf("reload node: %v", err)
|
||||
}
|
||||
return n.Status
|
||||
}
|
||||
|
||||
func TestProbeOneExpiresStaleAgentNode(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
stale := time.Now().Add(-10 * time.Minute).UnixMilli()
|
||||
n := seedAgentNode(t, "stale-agent", "online", stale)
|
||||
|
||||
(&NodeHeartbeatJob{}).probeOne(n)
|
||||
|
||||
if got := nodeStatus(t, n.Id); got != "offline" {
|
||||
t.Fatalf("stale agent node status = %q, want offline", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeOneKeepsFreshAgentNodeOnline(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
fresh := time.Now().Add(-30 * time.Second).UnixMilli()
|
||||
n := seedAgentNode(t, "fresh-agent", "online", fresh)
|
||||
|
||||
(&NodeHeartbeatJob{}).probeOne(n)
|
||||
|
||||
if got := nodeStatus(t, n.Id); got != "online" {
|
||||
t.Fatalf("fresh agent node status = %q, want online", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeOneLeavesUnactivatedAgentNodeAlone(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
// Token issued, agent never checked in: last_seen = 0. The node must not
|
||||
// be forced offline — it was never online/activated in the first place.
|
||||
n := seedAgentNode(t, "dormant-agent", "unknown", 0)
|
||||
|
||||
(&NodeHeartbeatJob{}).probeOne(n)
|
||||
|
||||
if got := nodeStatus(t, n.Id); got != "unknown" {
|
||||
t.Fatalf("unactivated agent node status = %q, want unknown (untouched)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeOnePanelNodeStillProbed(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
db := database.GetDB()
|
||||
// No agent bound: the legacy probe path must still run and mark an
|
||||
// unreachable panel node offline.
|
||||
n := &model.Node{
|
||||
Name: "panel-node",
|
||||
Scheme: "http",
|
||||
Address: "127.0.0.1",
|
||||
Port: 1, // nothing listens here; probe fails fast
|
||||
Enable: true,
|
||||
Status: "online",
|
||||
}
|
||||
if err := db.Create(n).Error; err != nil {
|
||||
t.Fatalf("create node: %v", err)
|
||||
}
|
||||
|
||||
(&NodeHeartbeatJob{}).probeOne(n)
|
||||
|
||||
if got := nodeStatus(t, n.Id); got != "offline" {
|
||||
t.Fatalf("unreachable panel node status = %q, want offline", got)
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,13 @@ const (
|
||||
nodeHeartbeatRequestTimeout = 4 * time.Second
|
||||
)
|
||||
|
||||
// agentHeartbeatStaleAfter is how long an agent-managed node may go without
|
||||
// an agent heartbeat before the heartbeat job marks it offline. The node-side
|
||||
// agent heartbeats every 60s (defaultHeartbeatInterval in internal/agent);
|
||||
// the threshold is max(3 intervals, 120s) = 180s, so two consecutive lost
|
||||
// heartbeats still don't flap the node offline.
|
||||
const agentHeartbeatStaleAfter = 180 * time.Second
|
||||
|
||||
type NodeHeartbeatJob struct {
|
||||
nodeService service.NodeService
|
||||
running sync.Mutex
|
||||
@@ -75,10 +82,13 @@ func (j *NodeHeartbeatJob) Run() {
|
||||
func (j *NodeHeartbeatJob) probeOne(n *model.Node) {
|
||||
// Agent-managed nodes are heartbeated via the agent token, not the
|
||||
// legacy panel probe. Skip the probe so we don't clobber the
|
||||
// agent-driven online status.
|
||||
// agent-driven online status — but do expire that status when the
|
||||
// agent stops heartbeating, or an uninstalled agent would leave its
|
||||
// node showing online forever.
|
||||
var agentCount int64
|
||||
_ = database.GetDB().Model(&model.AgentNode{}).Where("node_id = ?", n.Id).Count(&agentCount).Error
|
||||
if agentCount > 0 {
|
||||
j.expireStaleAgentNode(n)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), nodeHeartbeatRequestTimeout)
|
||||
@@ -106,6 +116,69 @@ func (j *NodeHeartbeatJob) probeOne(n *model.Node) {
|
||||
}
|
||||
}
|
||||
|
||||
// agentLastSeenMs returns the newest last_seen (Unix milliseconds, the unit
|
||||
// the agent controller stamps on register/heartbeat) across the agents bound
|
||||
// to the node, or 0 when no bound agent has ever checked in.
|
||||
func agentLastSeenMs(nodeID int) int64 {
|
||||
var lastSeen *int64 // MAX() over zero rows is NULL
|
||||
_ = database.GetDB().Model(&model.AgentNode{}).
|
||||
Where("node_id = ?", nodeID).
|
||||
Select("MAX(last_seen)").
|
||||
Scan(&lastSeen).Error
|
||||
if lastSeen == nil {
|
||||
return 0
|
||||
}
|
||||
return *lastSeen
|
||||
}
|
||||
|
||||
// agentHeartbeatStale decides expiry from the agent's last_seen stamp. A
|
||||
// zero stamp means the token was issued but the agent never checked in: the
|
||||
// node was never activated, so expiry leaves it alone (its status stays
|
||||
// whatever creation left it — "unknown") instead of forcing a just-created
|
||||
// node offline before its agent had a chance to come up.
|
||||
func agentHeartbeatStale(lastSeenMs int64, now time.Time) bool {
|
||||
if lastSeenMs <= 0 {
|
||||
return false
|
||||
}
|
||||
return now.Sub(time.UnixMilli(lastSeenMs)) > agentHeartbeatStaleAfter
|
||||
}
|
||||
|
||||
// expireStaleAgentNode flips an agent-managed node offline once its agent's
|
||||
// heartbeats stop. Only the status changes: the patch carries the node's
|
||||
// stored heartbeat fields through unchanged (including the last_heartbeat
|
||||
// stamp — the truth is "last seen then", not "now"), and the transition goes
|
||||
// through the same UpdateHeartbeat + broadcast path as a failed panel probe,
|
||||
// so node.down subscribers see agent expiries too.
|
||||
func (j *NodeHeartbeatJob) expireStaleAgentNode(n *model.Node) {
|
||||
if n.Status == "offline" {
|
||||
return
|
||||
}
|
||||
if !agentHeartbeatStale(agentLastSeenMs(n.Id), time.Now()) {
|
||||
return
|
||||
}
|
||||
patch := service.HeartbeatPatch{
|
||||
Status: "offline",
|
||||
LastHeartbeat: n.LastHeartbeat,
|
||||
LatencyMs: n.LatencyMs,
|
||||
XrayVersion: n.XrayVersion,
|
||||
PanelVersion: n.PanelVersion,
|
||||
Guid: n.Guid,
|
||||
CpuPct: n.CpuPct,
|
||||
MemPct: n.MemPct,
|
||||
UptimeSecs: n.UptimeSecs,
|
||||
NetUp: n.NetUp,
|
||||
NetDown: n.NetDown,
|
||||
LastError: n.LastError,
|
||||
XrayState: n.XrayState,
|
||||
XrayError: n.XrayError,
|
||||
}
|
||||
if err := j.nodeService.UpdateHeartbeat(n.Id, patch); err != nil {
|
||||
logger.Warning("node heartbeat: expire agent node", n.Id, "failed:", err)
|
||||
return
|
||||
}
|
||||
publishNodeTransition(n, n.Status, patch)
|
||||
}
|
||||
|
||||
// publishNodeTransition emits node.down / node.up only on a genuine state change.
|
||||
// An "unknown"/empty previous status (fresh start) is treated as not-online, so a
|
||||
// node coming up for the first time fires node.up but never a spurious node.down.
|
||||
|
||||
Reference in New Issue
Block a user