feat(system): add restart server endpoint with authentication

This commit is contained in:
2026-05-10 18:09:49 +08:00
parent 02bcfea745
commit 933c12fca1
3 changed files with 63 additions and 3 deletions
+36 -3
View File
@@ -4,10 +4,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"os"
"time"
"os/exec"
"strconv"
"strings"
"time"
"super-frpc/database"
"super-frpc/global"
@@ -166,4 +167,36 @@ func GetCmdParams(source string, param string) string {
func IsFileExist(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
}
func RestartHandler(w http.ResponseWriter, r *http.Request) {
userID, err := Auth(w, r, http.MethodGet, "superuser", "admin")
if err != nil {
SendErrorResponse(w, http.StatusUnauthorized, "invalid token or timestamp")
postLog.Warning(fmt.Sprintf("[RestartHandler] Auth failed: %v, userID: %d", err, userID))
return
}
err = RestartProcess()
if err != nil {
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to restart: %v", err))
postLog.Error(fmt.Sprintf("[RestartHandler] [%d] failed to restart: %v", userID, err))
return
}
SendSuccessResponse(w, "Restarting server...", nil)
postLog.Info(fmt.Sprintf("[RestartHandler] [%d] Restarting server", userID))
}
func RestartProcess() error {
executable, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
cmd := exec.Command(executable)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start new process: %w", err)
}
os.Exit(0)
return nil
}