减少与主控断开时的重试频率

This commit is contained in:
iluobei
2026-04-02 23:23:38 +08:00
parent de13820517
commit f50ee83e11
3 changed files with 94 additions and 39 deletions
+41
View File
@@ -0,0 +1,41 @@
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.work.sum
# env file
.env
traffic-info-*
.claude
build
internal/web/dist
data/*
.vscode
VERSION.md
miaomiaowu.db
miaomiaowux-frontend/.vite
.idea
VALIDATION_INTEGRATION_PLAN.md
*.md
+15 -2
View File
@@ -17,14 +17,27 @@ import (
func main() { func main() {
configPath := flag.String("config", "", "Path to config file") configPath := flag.String("config", "", "Path to config file")
configPathShort := flag.String("c", "", "Path to config file (shorthand)")
flag.Parse() flag.Parse()
// -c takes effect if -config is not set
cfgFile := *configPath
if cfgFile == "" {
cfgFile = *configPathShort
}
// Default to config.yaml in working directory
if cfgFile == "" {
if _, err := os.Stat("config.yaml"); err == nil {
cfgFile = "config.yaml"
}
}
// Load configuration // Load configuration
var cfg *config.Config var cfg *config.Config
var err error var err error
if *configPath != "" { if cfgFile != "" {
cfg, err = config.Load(*configPath) cfg, err = config.Load(cfgFile)
if err != nil { if err != nil {
log.Fatalf("Failed to load config: %v", err) log.Fatalf("Failed to load config: %v", err)
} }
+32 -31
View File
@@ -11,6 +11,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"sync/atomic"
"time" "time"
"mmw-agent/internal/config" "mmw-agent/internal/config"
@@ -20,6 +21,8 @@ import (
"github.com/xtls/xray-core/infra/conf" "github.com/xtls/xray-core/infra/conf"
) )
var nginxInstalling atomic.Bool
// ManageHandler handles management API requests for child servers // ManageHandler handles management API requests for child servers
type ManageHandler struct { type ManageHandler struct {
configToken string configToken string
@@ -163,7 +166,14 @@ func (h *ManageHandler) getXrayStatus() *ServiceStatus {
func (h *ManageHandler) getNginxStatus() *ServiceStatus { func (h *ManageHandler) getNginxStatus() *ServiceStatus {
status := &ServiceStatus{} status := &ServiceStatus{}
// Check PATH first, then compiled install path
nginxPath, err := exec.LookPath("nginx") nginxPath, err := exec.LookPath("nginx")
if err != nil {
if _, statErr := os.Stat("/usr/local/nginx/sbin/nginx"); statErr == nil {
nginxPath = "/usr/local/nginx/sbin/nginx"
err = nil
}
}
if err == nil { if err == nil {
status.Installed = true status.Installed = true
cmd := exec.Command(nginxPath, "-v") cmd := exec.Command(nginxPath, "-v")
@@ -173,6 +183,10 @@ func (h *ManageHandler) getNginxStatus() *ServiceStatus {
} }
} }
if nginxInstalling.Load() {
status.Version = "安装中..."
}
// Check systemctl first // Check systemctl first
cmd := exec.Command("systemctl", "is-active", "nginx") cmd := exec.Command("systemctl", "is-active", "nginx")
output, _ := cmd.Output() output, _ := cmd.Output()
@@ -716,37 +730,35 @@ func (h *ManageHandler) HandleNginxInstall(w http.ResponseWriter, r *http.Reques
return return
} }
log.Printf("[Manage] Installing Nginx...") if nginxInstalling.Load() {
writeError(w, http.StatusConflict, "Nginx installation already in progress")
var cmd *exec.Cmd
if _, err := exec.LookPath("apt-get"); err == nil {
cmd = exec.Command("bash", "-c", "apt-get update && apt-get install -y nginx")
} else if _, err := exec.LookPath("yum"); err == nil {
cmd = exec.Command("bash", "-c", "yum install -y nginx")
} else if _, err := exec.LookPath("dnf"); err == nil {
cmd = exec.Command("bash", "-c", "dnf install -y nginx")
} else {
writeError(w, http.StatusInternalServerError, "No supported package manager found")
return return
} }
log.Printf("[Manage] Starting Nginx installation (async)...")
nginxInstalling.Store(true)
go func() {
defer nginxInstalling.Store(false)
cmd := exec.Command("bash", "-c",
`curl -fsSL https://raw.githubusercontent.com/iluobei/miaomiaowuX/main/install-nginx.sh | bash`)
cmd.Env = os.Environ()
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
err := cmd.Run() if err := cmd.Run(); err != nil {
if err != nil {
log.Printf("[Manage] Nginx installation failed: %v, stderr: %s", err, stderr.String()) log.Printf("[Manage] Nginx installation failed: %v, stderr: %s", err, stderr.String())
writeError(w, http.StatusInternalServerError, fmt.Sprintf("Installation failed: %v", err))
return return
} }
log.Printf("[Manage] Nginx installed successfully") log.Printf("[Manage] Nginx installed successfully")
}()
writeJSON(w, http.StatusOK, map[string]interface{}{ writeJSON(w, http.StatusOK, map[string]interface{}{
"success": true, "success": true,
"message": "Nginx installed successfully", "message": "Nginx installation started, please check status later",
"output": stdout.String(),
}) })
} }
@@ -764,19 +776,9 @@ func (h *ManageHandler) HandleNginxRemove(w http.ResponseWriter, r *http.Request
log.Printf("[Manage] Removing Nginx...") log.Printf("[Manage] Removing Nginx...")
exec.Command("systemctl", "stop", "nginx").Run() cmd := exec.Command("bash", "-c",
`curl -fsSL https://raw.githubusercontent.com/iluobei/miaomiaowuX/main/uninstall-nginx.sh | bash -s -- -y`)
var cmd *exec.Cmd cmd.Env = os.Environ()
if _, err := exec.LookPath("apt-get"); err == nil {
cmd = exec.Command("bash", "-c", "apt-get remove -y nginx nginx-common")
} else if _, err := exec.LookPath("yum"); err == nil {
cmd = exec.Command("bash", "-c", "yum remove -y nginx")
} else if _, err := exec.LookPath("dnf"); err == nil {
cmd = exec.Command("bash", "-c", "dnf remove -y nginx")
} else {
writeError(w, http.StatusInternalServerError, "No supported package manager found")
return
}
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
@@ -794,7 +796,6 @@ func (h *ManageHandler) HandleNginxRemove(w http.ResponseWriter, r *http.Request
writeJSON(w, http.StatusOK, map[string]interface{}{ writeJSON(w, http.StatusOK, map[string]interface{}{
"success": true, "success": true,
"message": "Nginx removed successfully", "message": "Nginx removed successfully",
"output": stdout.String(),
}) })
} }