A modern and elegant dashboard for network traffic visualization and analysis.
fix(agent): service-managed status detection, shutdown drain, stable policy dedup
luoleid87786f2c140
diff --git a/apps/agent/internal/agent/runner.go b/apps/agent/internal/agent/runner.goindex d510733..6eaf4f4 100644--- a/apps/agent/internal/agent/runner.go+++ b/apps/agent/internal/agent/runner.go@@ -158,12 +158,22 @@ func (r *Runner) releaseLock() {}}-// isProcessRunning checks if a process with given PID is running+// isProcessRunning checks whether the PID is alive AND belongs to a+// neko-agent process. On Linux we cross-check /proc/<pid>/comm so that a+// stale PID later reused by an unrelated process cannot permanently block+// agent startup. On non-Linux (or if /proc isn't available) we fall back to+// the signal-0 liveness check.func isProcessRunning(pid int) bool {- // On Unix, use syscall.Kill with signal 0 to check if process exists- // Signal 0 performs error checking without actually sending a signal- err := syscall.Kill(pid, 0)- return err == nil+ if err := syscall.Kill(pid, 0); err != nil {+ return false+ }+ data, err := os.ReadFile(fmt.Sprintf("/proc/%d/comm", pid))+ if err != nil {+ // /proc not readable (non-Linux, restricted hidepid, etc.) — treat+ // liveness check as authoritative.+ return true+ }+ return strings.Contains(strings.TrimSpace(string(data)), "neko-agent")}func (r *Runner) Run(ctx context.Context) {@@ -190,8 +200,21 @@ func (r *Runner) Run(ctx context.Context) {shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()- if err := r.flushOnce(shutdownCtx); err != nil {- log.Printf("[agent:%s] final flush failed: %v", r.cfg.AgentID, err)+ // Drain the queue in batches until empty or the deadline hits — flushOnce+ // only sends one ReportBatchSize chunk per call, so a single invocation+ // would silently drop anything beyond it.+ for {+ if err := r.flushOnce(shutdownCtx); err != nil {+ log.Printf("[agent:%s] final flush failed: %v", r.cfg.AgentID, err)+ break+ }+ pending, _ := r.queueStats()+ if pending == 0 {+ break+ }+ if shutdownCtx.Err() != nil {+ break+ }}wg.Wait()diff --git a/apps/agent/internal/gateway/config.go b/apps/agent/internal/gateway/config.goindex 82234f5..5a1ed07 100644--- a/apps/agent/internal/gateway/config.go+++ b/apps/agent/internal/gateway/config.go@@ -7,6 +7,7 @@ import ("io""net/http""net/url"+ "sort""github.com/foru17/neko-master/apps/agent/internal/domain")@@ -226,8 +227,11 @@ func (c *Client) getClashPolicyState(ctx context.Context) (*domain.PolicyStateSnproviderProxies[p.Type] = append(providerProxies[p.Type], proxy)}- // Create providers by type+ // Create providers by type. Sort the proxy slice for stable JSON+ // marshalling so the runner's md5 dedup actually catches no-op syncs —+ // upstream proxiesData is a map (random iteration order) without this.for typ, proxies := range providerProxies {+ sort.Slice(proxies, func(i, j int) bool { return proxies[i].Name < proxies[j].Name })snap.Providers[typ] = domain.GatewayProvider{Name: typ,Type: typ,diff --git a/apps/agent/nekoagent b/apps/agent/nekoagentindex dc37e1f..8c63915 100755--- a/apps/agent/nekoagent+++ b/apps/agent/nekoagent@@ -8,7 +8,7 @@ AGENT_BIN="${NEKO_AGENT_BIN:-$INSTALL_DIR/neko-agent}"CONFIG_DIR="${NEKO_AGENT_CONFIG_DIR:-/etc/neko-agent}"STATE_DIR="${NEKO_AGENT_STATE_DIR:-/var/run/neko-agent}"AGENT_REPO="${NEKO_AGENT_REPO:-foru17/neko-master}"-VERSION="0.2.2"+VERSION="0.2.3"log() {printf '[%s] %s\n' "$PROG" "$1"@@ -39,6 +39,33 @@ log_file() {is_running() {name="$1"+ # When a service manager owns this instance, the pidfile is never written+ # (cmd_start delegates to systemctl/launchctl/init.d). Ask the service+ # manager directly so list/status/restart-all/upgrade reflect reality.+ if is_service_managed "$name"; then+ manager="$(detect_service_manager)"+ service="$(service_name_for_instance "$name")"+ case "$manager" in+ systemd)+ systemctl is-active --quiet "$service"+ return $?+ ;;+ launchd)+ label="io.neko-master.agent.${name}"+ if launchctl list 2>/dev/null | awk -v lbl="$label" 'NR>1 && $3==lbl {found=1} END {exit !found}'; then+ return 0+ fi+ return 1+ ;;+ openwrt)+ init_path="/etc/init.d/${service}"+ if [ -x "$init_path" ]; then+ "$init_path" status >/dev/null 2>&1+ return $?+ fi+ ;;+ esac+ fipf="$(pid_file "$name")"if [ ! -f "$pf" ]; thenreturn 1@@ -248,6 +275,11 @@ Restart=alwaysRestartSec=5StartLimitIntervalSec=300StartLimitBurst=10+LimitNOFILE=65536+NoNewPrivileges=true+PrivateTmp=true+ProtectSystem=full+ProtectHome=true[Install]WantedBy=multi-user.target@@ -976,7 +1008,25 @@ cmd_status() {managed=" (managed by $(detect_service_manager))"fiif is_running "$name"; then- log "instance '$name' is running (pid $(cat "$(pid_file "$name")"))${managed}"+ pid=""+ if is_service_managed "$name"; then+ manager="$(detect_service_manager)"+ service="$(service_name_for_instance "$name")"+ case "$manager" in+ systemd)+ pid="$(systemctl show -p MainPID --value "$service" 2>/dev/null || true)"+ ;;+ esac+ fi+ if [ -z "$pid" ] || [ "$pid" = "0" ]; then+ pf="$(pid_file "$name")"+ [ -f "$pf" ] && pid="$(cat "$pf" 2>/dev/null || true)"+ fi+ if [ -n "$pid" ] && [ "$pid" != "0" ]; then+ log "instance '$name' is running (pid $pid)${managed}"+ else+ log "instance '$name' is running${managed}"+ fiexit 0filog "instance '$name' is stopped${managed}"@@ -986,14 +1036,27 @@ cmd_status() {cmd_logs() {name="$1"- # If managed by systemd, prefer journalctl for proper log rotation+ # Service-managed instances don't write the wrapper's $lf; route to the+ # appropriate native log source instead.if is_service_managed "$name"; thenmanager="$(detect_service_manager)"- if [ "$manager" = "systemd" ]; then- service="$(service_name_for_instance "$name")"- journalctl -u "$service" -f --no-pager- return 0- fi+ service="$(service_name_for_instance "$name")"+ case "$manager" in+ systemd)+ journalctl -u "$service" -f --no-pager+ return 0+ ;;+ openwrt)+ # procd_set_param stdout/stderr 1 routes to syslog → logread+ if command -v logread >/dev/null 2>&1; then+ logread -e "$service" -f+ return 0+ fi+ ;;+ launchd)+ # launchd plist writes to $lf directly, fall through to tail+ ;;+ esacfilf="$(log_file "$name")"